• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Winner Microelectronics Co., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @file    wm_wl_mbox.c
18  *
19  * @brief   mailbox (mbox) APIs
20  *
21  * @author  dave
22  *
23  * Copyright (c) 2015 Winner Microelectronics Co., Ltd.
24  */
25 
26 #include "wm_wl_task.h"
27 #include "wm_mem.h"
28 #include "wm_wl_mbox.h"
29 
30 #define FIVE 5
31 #define TEN 10
32 #define ONE_THOUSAND 1000
33 
34 const void * const tls_null_pointer = (void *)0;
35 
36 /**
37  * @brief          create a malibox
38  *
39  * @param[out]     *mbox              pointer to the mailbox
40  * @param[in]      size               size of mailbox
41  *
42  * @retval         TLS_OS_SUCCESS     success
43  * @retval         TLS_OS_ERROR       failed
44  *
45  * @note           None
46  */
tls_mbox_new(tls_mbox_t * mbox,int size)47 s8 tls_mbox_new(tls_mbox_t *mbox, int size)
48 {
49     s8 err;
50     int mbox_size;
51     tls_os_status_t status;
52 
53     if (size == 0) {
54         mbox_size = TEN;
55     }
56 
57     status = tls_os_queue_create(mbox, mbox_size);
58     if (status == TLS_OS_SUCCESS) {
59         err = TLS_OS_SUCCESS;
60     } else {
61         err = TLS_OS_ERROR;
62     }
63 
64     return err;
65 }
66 
67 #ifndef tls_mbox_valid
68 /**
69  * @brief          check if an mbox is valid/allocated
70  *
71  * @param[in]      mbox    pointer to the mailbox
72  *
73  * @retval         0       invalid
74  * @retval         1       valid
75  *
76  * @note           None
77  */
tls_mbox_valid(tls_mbox_t mbox)78 int tls_mbox_valid(tls_mbox_t mbox)
79 {
80     if (mbox == NULL) {
81         return 0;
82     } else {
83         return 1;
84     }
85 }
86 #endif
87 
88 /**
89  * @brief          sends a message to a mailbox
90  *
91  * @param[in]      mbox    pointer to the mailbox
92  * @param[in]      *msg    pointer to the message to be post
93  *
94  * @return         None
95  *
96  * @note           None
97  */
tls_mbox_post(tls_mbox_t mbox,void * msg)98 void tls_mbox_post(tls_mbox_t mbox, void *msg)
99 {
100     u8 err;
101     u8 i = 0;
102 
103     if (msg == NULL) {
104         msg = (void *)tls_null_pointer;
105     }
106 
107     /* try 10 times */
108     while (i < TEN) {
109         err = tls_os_queue_send(mbox, msg, 0);
110         if (err == TLS_OS_SUCCESS) {
111             break;
112         }
113         i++;
114         tls_os_time_delay(FIVE);
115     }
116 }
117 
118 /**
119  * @brief          posts the "msg" to the mailbox.
120  *
121  * @param[in]      mbox               pointer to the mailbox
122  * @param[in]      *msg               pointer to the message to be post
123  *
124  * @retval         TLS_OS_SUCCESS     success
125  * @retval         TLS_OS_ERROR       failed
126  *
127  * @note           this function have to block until the "msg" is really posted.
128  */
tls_mbox_trypost(tls_mbox_t mbox,void * msg)129 s8 tls_mbox_trypost(tls_mbox_t mbox, void *msg)
130 {
131     u8 err;
132 
133     if (msg == NULL) {
134         msg = (void *)tls_null_pointer;
135     }
136 
137     err = tls_os_queue_send(mbox, msg, 0);
138     if (err == TLS_OS_SUCCESS) {
139         return TLS_OS_SUCCESS;
140     }
141 
142     return TLS_OS_ERROR;
143 }
144 
145 /**
146  * @brief          waits for a message within the specified time
147  *
148  * @param[in]      mbox         pointer to the mailbox
149  * @param[out]      **msg       pointer to the message to be received
150  * @param[in]      timeout      the specified time
151  *
152  * @retval         SYS_ARCH_TIMEOUT     time out
153  * @retval         other                time of elapsed
154  *
155  * @note           None
156  */
tls_arch_mbox_fetch(tls_mbox_t mbox,void ** msg,u32 timeout)157 u32 tls_arch_mbox_fetch(tls_mbox_t mbox, void **msg, u32 timeout)
158 {
159     u8        err;
160     u32       ucos_timeout = 0;
161     u32       in_timeout = timeout * HZ / ONE_THOUSAND;
162     u32       tick_start, tick_stop, tick_elapsed;
163 
164     if (timeout && in_timeout == 0) {
165         in_timeout = 1;
166     }
167 
168     tick_start = tls_os_get_time();
169     err = tls_os_queue_receive(mbox, msg, 0, in_timeout);
170     if (err != TLS_OS_SUCCESS) {
171         ucos_timeout = SYS_ARCH_TIMEOUT;
172         return ucos_timeout;
173     }
174     tick_stop = tls_os_get_time();
175     /* Take care of wrap-around. */
176     if (tick_stop >= tick_start) {
177         tick_elapsed = tick_stop - tick_start;
178     } else {
179         tick_elapsed = 0xFFFFFFFF - tick_start + tick_stop;
180     }
181 
182     return tick_elapsed * ONE_THOUSAND/HZ;
183 }
184 
185