Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: DebugLib Socket lwip lwip-sys
Fork of NetworkingCoreLib by
main/lwip/arch/sys_arch.cpp@0:013f5d54248c, 2012-05-24 (annotated)
- Committer:
- donatien
- Date:
- Thu May 24 14:49:40 2012 +0000
- Revision:
- 0:013f5d54248c
Initial Commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| donatien | 0:013f5d54248c | 1 | /* sysarch.cpp */ |
| donatien | 0:013f5d54248c | 2 | /* |
| donatien | 0:013f5d54248c | 3 | Copyright (C) 2012 ARM Limited. |
| donatien | 0:013f5d54248c | 4 | |
| donatien | 0:013f5d54248c | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of |
| donatien | 0:013f5d54248c | 6 | this software and associated documentation files (the "Software"), to deal in |
| donatien | 0:013f5d54248c | 7 | the Software without restriction, including without limitation the rights to |
| donatien | 0:013f5d54248c | 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| donatien | 0:013f5d54248c | 9 | of the Software, and to permit persons to whom the Software is furnished to do |
| donatien | 0:013f5d54248c | 10 | so, subject to the following conditions: |
| donatien | 0:013f5d54248c | 11 | |
| donatien | 0:013f5d54248c | 12 | The above copyright notice and this permission notice shall be included in all |
| donatien | 0:013f5d54248c | 13 | copies or substantial portions of the Software. |
| donatien | 0:013f5d54248c | 14 | |
| donatien | 0:013f5d54248c | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| donatien | 0:013f5d54248c | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| donatien | 0:013f5d54248c | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| donatien | 0:013f5d54248c | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| donatien | 0:013f5d54248c | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| donatien | 0:013f5d54248c | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| donatien | 0:013f5d54248c | 21 | SOFTWARE. |
| donatien | 0:013f5d54248c | 22 | */ |
| donatien | 0:013f5d54248c | 23 | |
| donatien | 0:013f5d54248c | 24 | #define __DEBUG__ 0 |
| donatien | 0:013f5d54248c | 25 | #ifndef __MODULE__ |
| donatien | 0:013f5d54248c | 26 | #define __MODULE__ "sys_arch.cpp" |
| donatien | 0:013f5d54248c | 27 | #endif |
| donatien | 0:013f5d54248c | 28 | |
| donatien | 0:013f5d54248c | 29 | extern "C" |
| donatien | 0:013f5d54248c | 30 | { |
| donatien | 0:013f5d54248c | 31 | #include "lwip/opt.h" |
| donatien | 0:013f5d54248c | 32 | #include "lwip/def.h" |
| donatien | 0:013f5d54248c | 33 | #include "lwip/sys.h" |
| donatien | 0:013f5d54248c | 34 | #include "lwip/arch.h" |
| donatien | 0:013f5d54248c | 35 | #include "cc.h" |
| donatien | 0:013f5d54248c | 36 | #include "sys_arch.h" |
| donatien | 0:013f5d54248c | 37 | } |
| donatien | 0:013f5d54248c | 38 | |
| donatien | 0:013f5d54248c | 39 | #include "core/fwk.h" |
| donatien | 0:013f5d54248c | 40 | #include "rtos.h" |
| donatien | 0:013f5d54248c | 41 | #include "mbed.h" //From mbed library |
| donatien | 0:013f5d54248c | 42 | |
| donatien | 0:013f5d54248c | 43 | /* |
| donatien | 0:013f5d54248c | 44 | From http://lwip.wikia.com/wiki/Porting_for_an_OS: |
| donatien | 0:013f5d54248c | 45 | |
| donatien | 0:013f5d54248c | 46 | The sys_arch provides semaphores and mailboxes to lwIP. For the full |
| donatien | 0:013f5d54248c | 47 | lwIP functionality, multiple threads support can be implemented in the |
| donatien | 0:013f5d54248c | 48 | sys_arch, but this is not required for the basic lwIP functionality. |
| donatien | 0:013f5d54248c | 49 | */ |
| donatien | 0:013f5d54248c | 50 | |
| donatien | 0:013f5d54248c | 51 | extern "C" |
| donatien | 0:013f5d54248c | 52 | /* |
| donatien | 0:013f5d54248c | 53 | The file is .cpp to be able to use the C++ abstraction of Mutexes, Semaphores, etc. |
| donatien | 0:013f5d54248c | 54 | however it should be linked in a C-fashion |
| donatien | 0:013f5d54248c | 55 | */ |
| donatien | 0:013f5d54248c | 56 | { |
| donatien | 0:013f5d54248c | 57 | |
| donatien | 0:013f5d54248c | 58 | #if SYS_LIGHTWEIGHT_PROT == 0 |
| donatien | 0:013f5d54248c | 59 | #error "SYS_LIGHTWEIGHT_PROT must be set" |
| donatien | 0:013f5d54248c | 60 | #endif |
| donatien | 0:013f5d54248c | 61 | |
| donatien | 0:013f5d54248c | 62 | #if SYS_LIGHTWEIGHT_PROT |
| donatien | 0:013f5d54248c | 63 | //Critical regions protection |
| donatien | 0:013f5d54248c | 64 | static Mutex sys_arch_protect_mtx; |
| donatien | 0:013f5d54248c | 65 | |
| donatien | 0:013f5d54248c | 66 | sys_prot_t sys_arch_protect(void) |
| donatien | 0:013f5d54248c | 67 | { |
| donatien | 0:013f5d54248c | 68 | |
| donatien | 0:013f5d54248c | 69 | sys_arch_protect_mtx.lock(); |
| donatien | 0:013f5d54248c | 70 | |
| donatien | 0:013f5d54248c | 71 | return 0; |
| donatien | 0:013f5d54248c | 72 | } |
| donatien | 0:013f5d54248c | 73 | |
| donatien | 0:013f5d54248c | 74 | void sys_arch_unprotect(sys_prot_t pval) |
| donatien | 0:013f5d54248c | 75 | { |
| donatien | 0:013f5d54248c | 76 | |
| donatien | 0:013f5d54248c | 77 | LWIP_UNUSED_ARG(pval); |
| donatien | 0:013f5d54248c | 78 | sys_arch_protect_mtx.unlock(); |
| donatien | 0:013f5d54248c | 79 | |
| donatien | 0:013f5d54248c | 80 | } |
| donatien | 0:013f5d54248c | 81 | #endif |
| donatien | 0:013f5d54248c | 82 | |
| donatien | 0:013f5d54248c | 83 | //Mutexes |
| donatien | 0:013f5d54248c | 84 | |
| donatien | 0:013f5d54248c | 85 | /** Create a new mutex |
| donatien | 0:013f5d54248c | 86 | * @param mutex pointer to the mutex to create |
| donatien | 0:013f5d54248c | 87 | * @return a new mutex */ |
| donatien | 0:013f5d54248c | 88 | err_t sys_mutex_new(sys_mutex_t *pMtx) |
| donatien | 0:013f5d54248c | 89 | { |
| donatien | 0:013f5d54248c | 90 | |
| donatien | 0:013f5d54248c | 91 | SYS_ARCH_DECL_PROTECT(lev); |
| donatien | 0:013f5d54248c | 92 | DBG("Trying to create new mutex"); |
| donatien | 0:013f5d54248c | 93 | |
| donatien | 0:013f5d54248c | 94 | SYS_ARCH_PROTECT(lev); //Protect this section |
| donatien | 0:013f5d54248c | 95 | pMtx->pMtx = new Mutex(); |
| donatien | 0:013f5d54248c | 96 | SYS_ARCH_UNPROTECT(lev); |
| donatien | 0:013f5d54248c | 97 | |
| donatien | 0:013f5d54248c | 98 | if(pMtx->pMtx == NULL) |
| donatien | 0:013f5d54248c | 99 | { |
| donatien | 0:013f5d54248c | 100 | DBG("No mem"); |
| donatien | 0:013f5d54248c | 101 | |
| donatien | 0:013f5d54248c | 102 | return ERR_MEM; |
| donatien | 0:013f5d54248c | 103 | } |
| donatien | 0:013f5d54248c | 104 | |
| donatien | 0:013f5d54248c | 105 | pMtx->valid = true; |
| donatien | 0:013f5d54248c | 106 | |
| donatien | 0:013f5d54248c | 107 | DBG("mutex created OK"); |
| donatien | 0:013f5d54248c | 108 | |
| donatien | 0:013f5d54248c | 109 | return ERR_OK; //Return the semaphore |
| donatien | 0:013f5d54248c | 110 | } |
| donatien | 0:013f5d54248c | 111 | |
| donatien | 0:013f5d54248c | 112 | /** Lock a mutex |
| donatien | 0:013f5d54248c | 113 | * @param mutex the mutex to lock */ |
| donatien | 0:013f5d54248c | 114 | void sys_mutex_lock(sys_mutex_t *pMtx) |
| donatien | 0:013f5d54248c | 115 | { |
| donatien | 0:013f5d54248c | 116 | |
| donatien | 0:013f5d54248c | 117 | ((Mutex*)(pMtx->pMtx))->lock(); |
| donatien | 0:013f5d54248c | 118 | |
| donatien | 0:013f5d54248c | 119 | } |
| donatien | 0:013f5d54248c | 120 | |
| donatien | 0:013f5d54248c | 121 | /** Unlock a mutex |
| donatien | 0:013f5d54248c | 122 | * @param mutex the mutex to unlock */ |
| donatien | 0:013f5d54248c | 123 | void sys_mutex_unlock(sys_mutex_t *pMtx) |
| donatien | 0:013f5d54248c | 124 | { |
| donatien | 0:013f5d54248c | 125 | |
| donatien | 0:013f5d54248c | 126 | ((Mutex*)(pMtx->pMtx))->unlock(); |
| donatien | 0:013f5d54248c | 127 | |
| donatien | 0:013f5d54248c | 128 | } |
| donatien | 0:013f5d54248c | 129 | |
| donatien | 0:013f5d54248c | 130 | /** Delete a mutex |
| donatien | 0:013f5d54248c | 131 | * @param mutex the mutex to delete */ |
| donatien | 0:013f5d54248c | 132 | void sys_mutex_free(sys_mutex_t *pMtx) |
| donatien | 0:013f5d54248c | 133 | { |
| donatien | 0:013f5d54248c | 134 | |
| donatien | 0:013f5d54248c | 135 | SYS_ARCH_DECL_PROTECT(lev); |
| donatien | 0:013f5d54248c | 136 | SYS_ARCH_PROTECT(lev); //Protect this section |
| donatien | 0:013f5d54248c | 137 | delete ((Mutex*)(pMtx->pMtx)); |
| donatien | 0:013f5d54248c | 138 | SYS_ARCH_UNPROTECT(lev); |
| donatien | 0:013f5d54248c | 139 | |
| donatien | 0:013f5d54248c | 140 | } |
| donatien | 0:013f5d54248c | 141 | |
| donatien | 0:013f5d54248c | 142 | /** Check if a mutex is valid/allocated: return 1 for valid, 0 for invalid */ |
| donatien | 0:013f5d54248c | 143 | int sys_mutex_valid(sys_mutex_t *pMtx) |
| donatien | 0:013f5d54248c | 144 | { |
| donatien | 0:013f5d54248c | 145 | |
| donatien | 0:013f5d54248c | 146 | if (pMtx->pMtx != NULL) |
| donatien | 0:013f5d54248c | 147 | { |
| donatien | 0:013f5d54248c | 148 | |
| donatien | 0:013f5d54248c | 149 | return pMtx->valid; |
| donatien | 0:013f5d54248c | 150 | } |
| donatien | 0:013f5d54248c | 151 | |
| donatien | 0:013f5d54248c | 152 | return false; |
| donatien | 0:013f5d54248c | 153 | } |
| donatien | 0:013f5d54248c | 154 | |
| donatien | 0:013f5d54248c | 155 | /** Set a mutex invalid so that sys_mutex_valid returns 0 */ |
| donatien | 0:013f5d54248c | 156 | void sys_mutex_set_invalid(sys_mutex_t *pMtx) |
| donatien | 0:013f5d54248c | 157 | { |
| donatien | 0:013f5d54248c | 158 | |
| donatien | 0:013f5d54248c | 159 | pMtx->valid = false; |
| donatien | 0:013f5d54248c | 160 | |
| donatien | 0:013f5d54248c | 161 | } |
| donatien | 0:013f5d54248c | 162 | |
| donatien | 0:013f5d54248c | 163 | //Semaphores |
| donatien | 0:013f5d54248c | 164 | |
| donatien | 0:013f5d54248c | 165 | /* |
| donatien | 0:013f5d54248c | 166 | The implementation reserves a pool of semaphores that can be used by |
| donatien | 0:013f5d54248c | 167 | LwIP in order to only use static allocation |
| donatien | 0:013f5d54248c | 168 | */ |
| donatien | 0:013f5d54248c | 169 | |
| donatien | 0:013f5d54248c | 170 | //static sys_sem sys_sem_pool[LWIP_SEMAPHORES_COUNT] = { 0 }; |
| donatien | 0:013f5d54248c | 171 | |
| donatien | 0:013f5d54248c | 172 | /* |
| donatien | 0:013f5d54248c | 173 | Creates and returns a new semaphore. The count argument specifies the |
| donatien | 0:013f5d54248c | 174 | initial state of the semaphore. Returns the semaphore, or SYS_SEM_NULL |
| donatien | 0:013f5d54248c | 175 | on error. |
| donatien | 0:013f5d54248c | 176 | */ |
| donatien | 0:013f5d54248c | 177 | err_t sys_sem_new_mul(sys_sem_t *pSem, u8_t size, u8_t count) |
| donatien | 0:013f5d54248c | 178 | { |
| donatien | 0:013f5d54248c | 179 | |
| donatien | 0:013f5d54248c | 180 | SYS_ARCH_DECL_PROTECT(lev); |
| donatien | 0:013f5d54248c | 181 | DBG("Trying to create new semaphore of size=%d", size); |
| donatien | 0:013f5d54248c | 182 | |
| donatien | 0:013f5d54248c | 183 | SYS_ARCH_PROTECT(lev); //Protect this section |
| donatien | 0:013f5d54248c | 184 | pSem->pSphre = new Semaphore(size); |
| donatien | 0:013f5d54248c | 185 | SYS_ARCH_UNPROTECT(lev); |
| donatien | 0:013f5d54248c | 186 | if(pSem->pSphre == NULL) |
| donatien | 0:013f5d54248c | 187 | { |
| donatien | 0:013f5d54248c | 188 | ERR("Failed!"); |
| donatien | 0:013f5d54248c | 189 | return ERR_MEM; |
| donatien | 0:013f5d54248c | 190 | } |
| donatien | 0:013f5d54248c | 191 | |
| donatien | 0:013f5d54248c | 192 | while(count < size) |
| donatien | 0:013f5d54248c | 193 | { |
| donatien | 0:013f5d54248c | 194 | ((Semaphore*)(pSem->pSphre))->wait(); |
| donatien | 0:013f5d54248c | 195 | count++; |
| donatien | 0:013f5d54248c | 196 | } |
| donatien | 0:013f5d54248c | 197 | pSem->valid = true; |
| donatien | 0:013f5d54248c | 198 | |
| donatien | 0:013f5d54248c | 199 | |
| donatien | 0:013f5d54248c | 200 | return ERR_OK; //Return the semaphore |
| donatien | 0:013f5d54248c | 201 | } |
| donatien | 0:013f5d54248c | 202 | |
| donatien | 0:013f5d54248c | 203 | |
| donatien | 0:013f5d54248c | 204 | /* |
| donatien | 0:013f5d54248c | 205 | Creates and returns a new semaphore. The count argument specifies the |
| donatien | 0:013f5d54248c | 206 | initial state of the semaphore. Returns the semaphore, or SYS_SEM_NULL |
| donatien | 0:013f5d54248c | 207 | on error. |
| donatien | 0:013f5d54248c | 208 | */ |
| donatien | 0:013f5d54248c | 209 | err_t sys_sem_new(sys_sem_t *pSem, u8_t count) |
| donatien | 0:013f5d54248c | 210 | { |
| donatien | 0:013f5d54248c | 211 | |
| donatien | 0:013f5d54248c | 212 | SYS_ARCH_DECL_PROTECT(lev); |
| donatien | 0:013f5d54248c | 213 | DBG("Trying to create new semaphore of count=%d", count); |
| donatien | 0:013f5d54248c | 214 | |
| donatien | 0:013f5d54248c | 215 | SYS_ARCH_PROTECT(lev); //Protect this section |
| donatien | 0:013f5d54248c | 216 | pSem->pSphre = new Semaphore(1); |
| donatien | 0:013f5d54248c | 217 | SYS_ARCH_UNPROTECT(lev); |
| donatien | 0:013f5d54248c | 218 | if(pSem->pSphre == NULL) |
| donatien | 0:013f5d54248c | 219 | { |
| donatien | 0:013f5d54248c | 220 | ERR("Failed!"); |
| donatien | 0:013f5d54248c | 221 | return ERR_MEM; |
| donatien | 0:013f5d54248c | 222 | } |
| donatien | 0:013f5d54248c | 223 | |
| donatien | 0:013f5d54248c | 224 | if(!count) |
| donatien | 0:013f5d54248c | 225 | { |
| donatien | 0:013f5d54248c | 226 | ((Semaphore*)(pSem->pSphre))->wait(); |
| donatien | 0:013f5d54248c | 227 | } |
| donatien | 0:013f5d54248c | 228 | pSem->valid = true; |
| donatien | 0:013f5d54248c | 229 | |
| donatien | 0:013f5d54248c | 230 | |
| donatien | 0:013f5d54248c | 231 | return ERR_OK; //Return the semaphore |
| donatien | 0:013f5d54248c | 232 | } |
| donatien | 0:013f5d54248c | 233 | |
| donatien | 0:013f5d54248c | 234 | /* |
| donatien | 0:013f5d54248c | 235 | Frees a semaphore created by sys_sem_new. Since these two functions |
| donatien | 0:013f5d54248c | 236 | provide the entry and exit point for all semaphores used by lwIP, you |
| donatien | 0:013f5d54248c | 237 | have great flexibility in how these are allocated and deallocated (for |
| donatien | 0:013f5d54248c | 238 | example, from the heap, a memory pool, a semaphore pool, etc). |
| donatien | 0:013f5d54248c | 239 | */ |
| donatien | 0:013f5d54248c | 240 | void sys_sem_free(sys_sem_t *pSem) |
| donatien | 0:013f5d54248c | 241 | { |
| donatien | 0:013f5d54248c | 242 | SYS_ARCH_DECL_PROTECT(lev); |
| donatien | 0:013f5d54248c | 243 | DBG("Deleting semaphore"); |
| donatien | 0:013f5d54248c | 244 | SYS_ARCH_PROTECT(lev); //Protect this section |
| donatien | 0:013f5d54248c | 245 | delete ((Semaphore*)(pSem->pSphre)); |
| donatien | 0:013f5d54248c | 246 | SYS_ARCH_UNPROTECT(lev); |
| donatien | 0:013f5d54248c | 247 | |
| donatien | 0:013f5d54248c | 248 | } |
| donatien | 0:013f5d54248c | 249 | |
| donatien | 0:013f5d54248c | 250 | /* |
| donatien | 0:013f5d54248c | 251 | Signals (or releases) a semaphore. |
| donatien | 0:013f5d54248c | 252 | */ |
| donatien | 0:013f5d54248c | 253 | void sys_sem_signal(sys_sem_t* pSem) |
| donatien | 0:013f5d54248c | 254 | { |
| donatien | 0:013f5d54248c | 255 | |
| donatien | 0:013f5d54248c | 256 | ((Semaphore*)(pSem->pSphre))->release(); //Produce (i.e. release) a resource |
| donatien | 0:013f5d54248c | 257 | |
| donatien | 0:013f5d54248c | 258 | } |
| donatien | 0:013f5d54248c | 259 | |
| donatien | 0:013f5d54248c | 260 | /* |
| donatien | 0:013f5d54248c | 261 | Blocks the thread while waiting for the semaphore to be signaled. The |
| donatien | 0:013f5d54248c | 262 | timeout parameter specifies how many milliseconds the function should |
| donatien | 0:013f5d54248c | 263 | block before returning; if the function times out, it should return |
| donatien | 0:013f5d54248c | 264 | SYS_ARCH_TIMEOUT. If timeout=0, then the function should block |
| donatien | 0:013f5d54248c | 265 | indefinitely. If the function acquires the semaphore, it should return |
| donatien | 0:013f5d54248c | 266 | how many milliseconds expired while waiting for the semaphore. The |
| donatien | 0:013f5d54248c | 267 | function may return 0 if the semaphore was immediately available. |
| donatien | 0:013f5d54248c | 268 | */ |
| donatien | 0:013f5d54248c | 269 | u32_t sys_arch_sem_wait(sys_sem_t* pSem, u32_t timeout) |
| donatien | 0:013f5d54248c | 270 | { |
| donatien | 0:013f5d54248c | 271 | |
| donatien | 0:013f5d54248c | 272 | int ret; |
| donatien | 0:013f5d54248c | 273 | Timer t; |
| donatien | 0:013f5d54248c | 274 | uint32_t timeout_sphre; |
| donatien | 0:013f5d54248c | 275 | int time_spent; |
| donatien | 0:013f5d54248c | 276 | |
| donatien | 0:013f5d54248c | 277 | timeout_sphre = (timeout != 0) ? timeout : osWaitForever /*block indefinitely*/; |
| donatien | 0:013f5d54248c | 278 | |
| donatien | 0:013f5d54248c | 279 | t.start(); |
| donatien | 0:013f5d54248c | 280 | ret = ((Semaphore*)(pSem->pSphre))->wait(timeout_sphre); |
| donatien | 0:013f5d54248c | 281 | if(ret == 0) |
| donatien | 0:013f5d54248c | 282 | { |
| donatien | 0:013f5d54248c | 283 | |
| donatien | 0:013f5d54248c | 284 | return SYS_ARCH_TIMEOUT; |
| donatien | 0:013f5d54248c | 285 | } |
| donatien | 0:013f5d54248c | 286 | time_spent = t.read_ms(); |
| donatien | 0:013f5d54248c | 287 | |
| donatien | 0:013f5d54248c | 288 | return time_spent; |
| donatien | 0:013f5d54248c | 289 | } |
| donatien | 0:013f5d54248c | 290 | |
| donatien | 0:013f5d54248c | 291 | /** Check if a sempahore is valid/allocated: return 1 for valid, 0 for invalid */ |
| donatien | 0:013f5d54248c | 292 | int sys_sem_valid(sys_sem_t *pSem) |
| donatien | 0:013f5d54248c | 293 | { |
| donatien | 0:013f5d54248c | 294 | |
| donatien | 0:013f5d54248c | 295 | if (pSem->pSphre != NULL) |
| donatien | 0:013f5d54248c | 296 | { |
| donatien | 0:013f5d54248c | 297 | |
| donatien | 0:013f5d54248c | 298 | return (pSem->valid); |
| donatien | 0:013f5d54248c | 299 | } |
| donatien | 0:013f5d54248c | 300 | |
| donatien | 0:013f5d54248c | 301 | return false; |
| donatien | 0:013f5d54248c | 302 | } |
| donatien | 0:013f5d54248c | 303 | |
| donatien | 0:013f5d54248c | 304 | /** Set a semaphore invalid so that sys_sem_valid returns 0 */ |
| donatien | 0:013f5d54248c | 305 | void sys_sem_set_invalid(sys_sem_t *pSem) |
| donatien | 0:013f5d54248c | 306 | { |
| donatien | 0:013f5d54248c | 307 | |
| donatien | 0:013f5d54248c | 308 | pSem->valid = false; |
| donatien | 0:013f5d54248c | 309 | |
| donatien | 0:013f5d54248c | 310 | } |
| donatien | 0:013f5d54248c | 311 | |
| donatien | 0:013f5d54248c | 312 | //Mailboxes |
| donatien | 0:013f5d54248c | 313 | |
| donatien | 0:013f5d54248c | 314 | /* |
| donatien | 0:013f5d54248c | 315 | The implementation reserves a pool of mailboxes of generic (void*) type that can be used by |
| donatien | 0:013f5d54248c | 316 | LwIP in order to only use static allocation |
| donatien | 0:013f5d54248c | 317 | */ |
| donatien | 0:013f5d54248c | 318 | |
| donatien | 0:013f5d54248c | 319 | #define QUEUE_SIZE 8 |
| donatien | 0:013f5d54248c | 320 | #define SYS_MBOX_SIZE QUEUE_SIZE |
| donatien | 0:013f5d54248c | 321 | #if 0 |
| donatien | 0:013f5d54248c | 322 | /* |
| donatien | 0:013f5d54248c | 323 | Returns a new mailbox, or SYS_MBOX_NULL on error. |
| donatien | 0:013f5d54248c | 324 | */ |
| donatien | 0:013f5d54248c | 325 | err_t sys_mbox_new(sys_mbox_t *pMbox, int size) |
| donatien | 0:013f5d54248c | 326 | { |
| donatien | 0:013f5d54248c | 327 | |
| donatien | 0:013f5d54248c | 328 | SYS_ARCH_DECL_PROTECT(lev); |
| donatien | 0:013f5d54248c | 329 | ERR("Trying to create new queue of size %d", size); |
| donatien | 0:013f5d54248c | 330 | |
| donatien | 0:013f5d54248c | 331 | SYS_ARCH_PROTECT(lev); //Protect this section |
| donatien | 0:013f5d54248c | 332 | if(size > QUEUE_SIZE) |
| donatien | 0:013f5d54248c | 333 | { |
| donatien | 0:013f5d54248c | 334 | ERR("Queue size > QUEUE_SIZE"); |
| donatien | 0:013f5d54248c | 335 | } |
| donatien | 0:013f5d54248c | 336 | pMbox->pQueue = new Queue<void,QUEUE_SIZE>(); |
| donatien | 0:013f5d54248c | 337 | SYS_ARCH_UNPROTECT(lev); |
| donatien | 0:013f5d54248c | 338 | if(pMbox->pQueue == NULL) |
| donatien | 0:013f5d54248c | 339 | { |
| donatien | 0:013f5d54248c | 340 | |
| donatien | 0:013f5d54248c | 341 | return ERR_MEM; |
| donatien | 0:013f5d54248c | 342 | } |
| donatien | 0:013f5d54248c | 343 | pMbox->valid = true; |
| donatien | 0:013f5d54248c | 344 | |
| donatien | 0:013f5d54248c | 345 | |
| donatien | 0:013f5d54248c | 346 | return ERR_OK; //Return the mailbox |
| donatien | 0:013f5d54248c | 347 | } |
| donatien | 0:013f5d54248c | 348 | |
| donatien | 0:013f5d54248c | 349 | /* |
| donatien | 0:013f5d54248c | 350 | Deallocates a mailbox. If there are messages still present in the |
| donatien | 0:013f5d54248c | 351 | mailbox when the mailbox is deallocated, it is an indication of a |
| donatien | 0:013f5d54248c | 352 | programming error in lwIP and the developer should be notified. |
| donatien | 0:013f5d54248c | 353 | */ |
| donatien | 0:013f5d54248c | 354 | void sys_mbox_free(sys_mbox_t* pMbox) |
| donatien | 0:013f5d54248c | 355 | { |
| donatien | 0:013f5d54248c | 356 | ERR("WHY??"); |
| donatien | 0:013f5d54248c | 357 | |
| donatien | 0:013f5d54248c | 358 | SYS_ARCH_DECL_PROTECT(lev); |
| donatien | 0:013f5d54248c | 359 | SYS_ARCH_PROTECT(lev); //Protect this section |
| donatien | 0:013f5d54248c | 360 | delete ((Queue<void,QUEUE_SIZE>*)(pMbox->pQueue)); |
| donatien | 0:013f5d54248c | 361 | SYS_ARCH_UNPROTECT(lev); |
| donatien | 0:013f5d54248c | 362 | |
| donatien | 0:013f5d54248c | 363 | } |
| donatien | 0:013f5d54248c | 364 | |
| donatien | 0:013f5d54248c | 365 | /* |
| donatien | 0:013f5d54248c | 366 | Posts the "msg" to the mailbox. |
| donatien | 0:013f5d54248c | 367 | */ |
| donatien | 0:013f5d54248c | 368 | void sys_mbox_post(sys_mbox_t* pMbox, void *msg) |
| donatien | 0:013f5d54248c | 369 | { |
| donatien | 0:013f5d54248c | 370 | |
| donatien | 0:013f5d54248c | 371 | if(msg == NULL) |
| donatien | 0:013f5d54248c | 372 | { |
| donatien | 0:013f5d54248c | 373 | ERR("NULL"); |
| donatien | 0:013f5d54248c | 374 | Thread::wait(100); |
| donatien | 0:013f5d54248c | 375 | } |
| donatien | 0:013f5d54248c | 376 | |
| donatien | 0:013f5d54248c | 377 | ((Queue<void,QUEUE_SIZE>*)(pMbox->pQueue))->put(msg, osWaitForever); |
| donatien | 0:013f5d54248c | 378 | |
| donatien | 0:013f5d54248c | 379 | } |
| donatien | 0:013f5d54248c | 380 | |
| donatien | 0:013f5d54248c | 381 | /* |
| donatien | 0:013f5d54248c | 382 | Blocks the thread until a message arrives in the mailbox, but does not |
| donatien | 0:013f5d54248c | 383 | block the thread longer than timeout milliseconds (similar to the |
| donatien | 0:013f5d54248c | 384 | sys_arch_sem_wait() function). The msg argument is a pointer to the |
| donatien | 0:013f5d54248c | 385 | message in the mailbox and may be NULL to indicate that the message |
| donatien | 0:013f5d54248c | 386 | should be dropped. This should return either SYS_ARCH_TIMEOUT or the |
| donatien | 0:013f5d54248c | 387 | number of milliseconds elapsed waiting for a message. |
| donatien | 0:013f5d54248c | 388 | */ |
| donatien | 0:013f5d54248c | 389 | u32_t sys_arch_mbox_fetch(sys_mbox_t* pMbox, void **msg, u32_t timeout) |
| donatien | 0:013f5d54248c | 390 | { |
| donatien | 0:013f5d54248c | 391 | Timer t; |
| donatien | 0:013f5d54248c | 392 | uint32_t timeout_queue; |
| donatien | 0:013f5d54248c | 393 | int time_spent; |
| donatien | 0:013f5d54248c | 394 | osEvent evt; |
| donatien | 0:013f5d54248c | 395 | |
| donatien | 0:013f5d54248c | 396 | timeout_queue = (timeout != 0) ? timeout : osWaitForever /*block indefinitely*/; |
| donatien | 0:013f5d54248c | 397 | |
| donatien | 0:013f5d54248c | 398 | t.start(); |
| donatien | 0:013f5d54248c | 399 | evt = ((Queue<void,QUEUE_SIZE>*)(pMbox->pQueue))->get(timeout_queue); |
| donatien | 0:013f5d54248c | 400 | if(evt.status != osEventMessage) |
| donatien | 0:013f5d54248c | 401 | { |
| donatien | 0:013f5d54248c | 402 | return SYS_ARCH_TIMEOUT; |
| donatien | 0:013f5d54248c | 403 | } |
| donatien | 0:013f5d54248c | 404 | time_spent = t.read_ms(); |
| donatien | 0:013f5d54248c | 405 | if(msg!=NULL) |
| donatien | 0:013f5d54248c | 406 | { |
| donatien | 0:013f5d54248c | 407 | *msg = evt.value.p; |
| donatien | 0:013f5d54248c | 408 | } |
| donatien | 0:013f5d54248c | 409 | else |
| donatien | 0:013f5d54248c | 410 | { |
| donatien | 0:013f5d54248c | 411 | ERR("Dropped"); |
| donatien | 0:013f5d54248c | 412 | Thread::wait(100); |
| donatien | 0:013f5d54248c | 413 | } |
| donatien | 0:013f5d54248c | 414 | return time_spent; |
| donatien | 0:013f5d54248c | 415 | } |
| donatien | 0:013f5d54248c | 416 | |
| donatien | 0:013f5d54248c | 417 | /* |
| donatien | 0:013f5d54248c | 418 | This is similar to sys_arch_mbox_fetch, however if a message is not |
| donatien | 0:013f5d54248c | 419 | present in the mailbox, it immediately returns with the code |
| donatien | 0:013f5d54248c | 420 | SYS_MBOX_EMPTY. On success 0 is returned with msg pointing to the |
| donatien | 0:013f5d54248c | 421 | message retrieved from the mailbox. |
| donatien | 0:013f5d54248c | 422 | */ |
| donatien | 0:013f5d54248c | 423 | u32_t sys_arch_mbox_tryfetch(sys_mbox_t* pMbox, void **msg) |
| donatien | 0:013f5d54248c | 424 | { |
| donatien | 0:013f5d54248c | 425 | |
| donatien | 0:013f5d54248c | 426 | osEvent evt; |
| donatien | 0:013f5d54248c | 427 | |
| donatien | 0:013f5d54248c | 428 | evt = ((Queue<void,QUEUE_SIZE>*)(pMbox->pQueue))->get(0); |
| donatien | 0:013f5d54248c | 429 | if(evt.status != osEventMessage) |
| donatien | 0:013f5d54248c | 430 | { |
| donatien | 0:013f5d54248c | 431 | return SYS_MBOX_EMPTY; |
| donatien | 0:013f5d54248c | 432 | } |
| donatien | 0:013f5d54248c | 433 | if(msg!=NULL) |
| donatien | 0:013f5d54248c | 434 | { |
| donatien | 0:013f5d54248c | 435 | *msg = evt.value.p; |
| donatien | 0:013f5d54248c | 436 | } |
| donatien | 0:013f5d54248c | 437 | else |
| donatien | 0:013f5d54248c | 438 | { |
| donatien | 0:013f5d54248c | 439 | ERR("Dropped"); |
| donatien | 0:013f5d54248c | 440 | Thread::wait(100); |
| donatien | 0:013f5d54248c | 441 | } |
| donatien | 0:013f5d54248c | 442 | |
| donatien | 0:013f5d54248c | 443 | return ERR_OK; |
| donatien | 0:013f5d54248c | 444 | } |
| donatien | 0:013f5d54248c | 445 | |
| donatien | 0:013f5d54248c | 446 | /* |
| donatien | 0:013f5d54248c | 447 | Tries to post a message to mbox by polling (no timeout). |
| donatien | 0:013f5d54248c | 448 | */ |
| donatien | 0:013f5d54248c | 449 | #define X() do{(*((volatile uint32_t *)0x40024048))=__LINE__;}while(0) |
| donatien | 0:013f5d54248c | 450 | #define D(d) do{(*((volatile uint32_t *)0x4002404C))=d;}while(0) |
| donatien | 0:013f5d54248c | 451 | err_t sys_mbox_trypost(sys_mbox_t* pMbox, void *msg) |
| donatien | 0:013f5d54248c | 452 | { |
| donatien | 0:013f5d54248c | 453 | int ret; |
| donatien | 0:013f5d54248c | 454 | |
| donatien | 0:013f5d54248c | 455 | if(msg == NULL) |
| donatien | 0:013f5d54248c | 456 | { |
| donatien | 0:013f5d54248c | 457 | ERR("NULL"); |
| donatien | 0:013f5d54248c | 458 | Thread::wait(100); |
| donatien | 0:013f5d54248c | 459 | } |
| donatien | 0:013f5d54248c | 460 | |
| donatien | 0:013f5d54248c | 461 | ret = ((Queue<void,QUEUE_SIZE>*)(pMbox->pQueue))->put(msg,0); |
| donatien | 0:013f5d54248c | 462 | if(ret != osOK) |
| donatien | 0:013f5d54248c | 463 | { |
| donatien | 0:013f5d54248c | 464 | ERR("FULL"); |
| donatien | 0:013f5d54248c | 465 | return ERR_MEM; |
| donatien | 0:013f5d54248c | 466 | } |
| donatien | 0:013f5d54248c | 467 | |
| donatien | 0:013f5d54248c | 468 | return ERR_OK; |
| donatien | 0:013f5d54248c | 469 | } |
| donatien | 0:013f5d54248c | 470 | |
| donatien | 0:013f5d54248c | 471 | /** Check if an mbox is valid/allocated: return 1 for valid, 0 for invalid */ |
| donatien | 0:013f5d54248c | 472 | int sys_mbox_valid(sys_mbox_t *pMbox) |
| donatien | 0:013f5d54248c | 473 | { |
| donatien | 0:013f5d54248c | 474 | |
| donatien | 0:013f5d54248c | 475 | if (pMbox->pQueue != NULL) |
| donatien | 0:013f5d54248c | 476 | { |
| donatien | 0:013f5d54248c | 477 | |
| donatien | 0:013f5d54248c | 478 | return (pMbox->valid); |
| donatien | 0:013f5d54248c | 479 | } |
| donatien | 0:013f5d54248c | 480 | |
| donatien | 0:013f5d54248c | 481 | return false; |
| donatien | 0:013f5d54248c | 482 | } |
| donatien | 0:013f5d54248c | 483 | |
| donatien | 0:013f5d54248c | 484 | /** Set an mbox invalid so that sys_mbox_valid returns 0 */ |
| donatien | 0:013f5d54248c | 485 | void sys_mbox_set_invalid(sys_mbox_t *pMbox) |
| donatien | 0:013f5d54248c | 486 | { |
| donatien | 0:013f5d54248c | 487 | |
| donatien | 0:013f5d54248c | 488 | pMbox->valid = false; |
| donatien | 0:013f5d54248c | 489 | |
| donatien | 0:013f5d54248c | 490 | } |
| donatien | 0:013f5d54248c | 491 | #else |
| donatien | 0:013f5d54248c | 492 | |
| donatien | 0:013f5d54248c | 493 | /** Check if an mbox is valid/allocated: return 1 for valid, 0 for invalid */ |
| donatien | 0:013f5d54248c | 494 | int sys_mbox_valid(sys_mbox_t *pMbox) |
| donatien | 0:013f5d54248c | 495 | { |
| donatien | 0:013f5d54248c | 496 | return (pMbox->valid); |
| donatien | 0:013f5d54248c | 497 | } |
| donatien | 0:013f5d54248c | 498 | |
| donatien | 0:013f5d54248c | 499 | /** Set an mbox invalid so that sys_mbox_valid returns 0 */ |
| donatien | 0:013f5d54248c | 500 | void sys_mbox_set_invalid(sys_mbox_t *pMbox) |
| donatien | 0:013f5d54248c | 501 | { |
| donatien | 0:013f5d54248c | 502 | pMbox->valid = false; |
| donatien | 0:013f5d54248c | 503 | } |
| donatien | 0:013f5d54248c | 504 | |
| donatien | 0:013f5d54248c | 505 | /*-----------------------------------------------------------------------------------*/ |
| donatien | 0:013f5d54248c | 506 | err_t |
| donatien | 0:013f5d54248c | 507 | sys_mbox_new(sys_mbox_t* mb, int size) |
| donatien | 0:013f5d54248c | 508 | { |
| donatien | 0:013f5d54248c | 509 | LWIP_UNUSED_ARG(size); |
| donatien | 0:013f5d54248c | 510 | |
| donatien | 0:013f5d54248c | 511 | mb->first = mb->last = 0; |
| donatien | 0:013f5d54248c | 512 | sys_sem_new(&mb->not_empty, 0); |
| donatien | 0:013f5d54248c | 513 | sys_sem_new(&mb->not_full, 0); |
| donatien | 0:013f5d54248c | 514 | sys_sem_new(&mb->mutex, 1); |
| donatien | 0:013f5d54248c | 515 | mb->wait_send = 0; |
| donatien | 0:013f5d54248c | 516 | mb->valid=true; |
| donatien | 0:013f5d54248c | 517 | |
| donatien | 0:013f5d54248c | 518 | return ERR_OK; |
| donatien | 0:013f5d54248c | 519 | } |
| donatien | 0:013f5d54248c | 520 | /*-----------------------------------------------------------------------------------*/ |
| donatien | 0:013f5d54248c | 521 | void |
| donatien | 0:013f5d54248c | 522 | sys_mbox_free(sys_mbox_t* mb) |
| donatien | 0:013f5d54248c | 523 | { |
| donatien | 0:013f5d54248c | 524 | if ((mb != NULL)) { |
| donatien | 0:013f5d54248c | 525 | sys_arch_sem_wait(&mb->mutex, 0); |
| donatien | 0:013f5d54248c | 526 | |
| donatien | 0:013f5d54248c | 527 | sys_sem_free(&mb->not_empty); |
| donatien | 0:013f5d54248c | 528 | sys_sem_free(&mb->not_full); |
| donatien | 0:013f5d54248c | 529 | sys_sem_free(&mb->mutex); |
| donatien | 0:013f5d54248c | 530 | //mb->not_empty = mb->not_full = mb->mutex = NULL; |
| donatien | 0:013f5d54248c | 531 | mb->valid=false; |
| donatien | 0:013f5d54248c | 532 | /* LWIP_DEBUGF("sys_mbox_free: mbox 0x%lx\n", mbox); */ |
| donatien | 0:013f5d54248c | 533 | } |
| donatien | 0:013f5d54248c | 534 | } |
| donatien | 0:013f5d54248c | 535 | /*-----------------------------------------------------------------------------------*/ |
| donatien | 0:013f5d54248c | 536 | err_t |
| donatien | 0:013f5d54248c | 537 | sys_mbox_trypost(sys_mbox_t* mb, void *msg) |
| donatien | 0:013f5d54248c | 538 | { |
| donatien | 0:013f5d54248c | 539 | u8_t first; |
| donatien | 0:013f5d54248c | 540 | LWIP_ASSERT("invalid mbox", (mb != NULL) ); |
| donatien | 0:013f5d54248c | 541 | |
| donatien | 0:013f5d54248c | 542 | sys_arch_sem_wait(&mb->mutex, 0); |
| donatien | 0:013f5d54248c | 543 | |
| donatien | 0:013f5d54248c | 544 | LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_trypost: mbox %p msg %p\n", |
| donatien | 0:013f5d54248c | 545 | (void *)mb, (void *)msg)); |
| donatien | 0:013f5d54248c | 546 | |
| donatien | 0:013f5d54248c | 547 | if ((mb->last + 1) >= (mb->first + SYS_MBOX_SIZE)) { |
| donatien | 0:013f5d54248c | 548 | sys_sem_signal(&mb->mutex); |
| donatien | 0:013f5d54248c | 549 | return ERR_MEM; |
| donatien | 0:013f5d54248c | 550 | } |
| donatien | 0:013f5d54248c | 551 | |
| donatien | 0:013f5d54248c | 552 | mb->msgs[mb->last % SYS_MBOX_SIZE] = msg; |
| donatien | 0:013f5d54248c | 553 | |
| donatien | 0:013f5d54248c | 554 | if (mb->last == mb->first) { |
| donatien | 0:013f5d54248c | 555 | first = 1; |
| donatien | 0:013f5d54248c | 556 | } else { |
| donatien | 0:013f5d54248c | 557 | first = 0; |
| donatien | 0:013f5d54248c | 558 | } |
| donatien | 0:013f5d54248c | 559 | |
| donatien | 0:013f5d54248c | 560 | mb->last++; |
| donatien | 0:013f5d54248c | 561 | |
| donatien | 0:013f5d54248c | 562 | if (first) { |
| donatien | 0:013f5d54248c | 563 | sys_sem_signal(&mb->not_empty); |
| donatien | 0:013f5d54248c | 564 | } |
| donatien | 0:013f5d54248c | 565 | |
| donatien | 0:013f5d54248c | 566 | sys_sem_signal(&mb->mutex); |
| donatien | 0:013f5d54248c | 567 | |
| donatien | 0:013f5d54248c | 568 | return ERR_OK; |
| donatien | 0:013f5d54248c | 569 | } |
| donatien | 0:013f5d54248c | 570 | /*-----------------------------------------------------------------------------------*/ |
| donatien | 0:013f5d54248c | 571 | void |
| donatien | 0:013f5d54248c | 572 | sys_mbox_post(sys_mbox_t* mb, void *msg) |
| donatien | 0:013f5d54248c | 573 | { |
| donatien | 0:013f5d54248c | 574 | u8_t first; |
| donatien | 0:013f5d54248c | 575 | LWIP_ASSERT("invalid mbox", (mb != NULL)); |
| donatien | 0:013f5d54248c | 576 | |
| donatien | 0:013f5d54248c | 577 | sys_arch_sem_wait(&mb->mutex, 0); |
| donatien | 0:013f5d54248c | 578 | |
| donatien | 0:013f5d54248c | 579 | LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_post: mbox %p msg %p\n", (void *)mb, (void *)msg)); |
| donatien | 0:013f5d54248c | 580 | |
| donatien | 0:013f5d54248c | 581 | while ((mb->last + 1) >= (mb->first + SYS_MBOX_SIZE)) { |
| donatien | 0:013f5d54248c | 582 | mb->wait_send++; |
| donatien | 0:013f5d54248c | 583 | sys_sem_signal(&mb->mutex); |
| donatien | 0:013f5d54248c | 584 | sys_arch_sem_wait(&mb->not_full, 0); |
| donatien | 0:013f5d54248c | 585 | sys_arch_sem_wait(&mb->mutex, 0); |
| donatien | 0:013f5d54248c | 586 | mb->wait_send--; |
| donatien | 0:013f5d54248c | 587 | } |
| donatien | 0:013f5d54248c | 588 | |
| donatien | 0:013f5d54248c | 589 | mb->msgs[mb->last % SYS_MBOX_SIZE] = msg; |
| donatien | 0:013f5d54248c | 590 | |
| donatien | 0:013f5d54248c | 591 | if (mb->last == mb->first) { |
| donatien | 0:013f5d54248c | 592 | first = 1; |
| donatien | 0:013f5d54248c | 593 | } else { |
| donatien | 0:013f5d54248c | 594 | first = 0; |
| donatien | 0:013f5d54248c | 595 | } |
| donatien | 0:013f5d54248c | 596 | |
| donatien | 0:013f5d54248c | 597 | mb->last++; |
| donatien | 0:013f5d54248c | 598 | |
| donatien | 0:013f5d54248c | 599 | if (first) { |
| donatien | 0:013f5d54248c | 600 | sys_sem_signal(&mb->not_empty); |
| donatien | 0:013f5d54248c | 601 | } |
| donatien | 0:013f5d54248c | 602 | |
| donatien | 0:013f5d54248c | 603 | sys_sem_signal(&mb->mutex); |
| donatien | 0:013f5d54248c | 604 | } |
| donatien | 0:013f5d54248c | 605 | /*-----------------------------------------------------------------------------------*/ |
| donatien | 0:013f5d54248c | 606 | u32_t |
| donatien | 0:013f5d54248c | 607 | sys_arch_mbox_tryfetch(sys_mbox_t* mb, void **msg) |
| donatien | 0:013f5d54248c | 608 | { |
| donatien | 0:013f5d54248c | 609 | LWIP_ASSERT("invalid mbox", (mb != NULL)); |
| donatien | 0:013f5d54248c | 610 | |
| donatien | 0:013f5d54248c | 611 | sys_arch_sem_wait(&mb->mutex, 0); |
| donatien | 0:013f5d54248c | 612 | |
| donatien | 0:013f5d54248c | 613 | if (mb->first == mb->last) { |
| donatien | 0:013f5d54248c | 614 | sys_sem_signal(&mb->mutex); |
| donatien | 0:013f5d54248c | 615 | return SYS_MBOX_EMPTY; |
| donatien | 0:013f5d54248c | 616 | } |
| donatien | 0:013f5d54248c | 617 | |
| donatien | 0:013f5d54248c | 618 | if (msg != NULL) { |
| donatien | 0:013f5d54248c | 619 | LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_tryfetch: mbox %p msg %p\n", (void *)mb, *msg)); |
| donatien | 0:013f5d54248c | 620 | *msg = mb->msgs[mb->first % SYS_MBOX_SIZE]; |
| donatien | 0:013f5d54248c | 621 | } |
| donatien | 0:013f5d54248c | 622 | else{ |
| donatien | 0:013f5d54248c | 623 | LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_tryfetch: mbox %p, null msg\n", (void *)mb)); |
| donatien | 0:013f5d54248c | 624 | } |
| donatien | 0:013f5d54248c | 625 | |
| donatien | 0:013f5d54248c | 626 | mb->first++; |
| donatien | 0:013f5d54248c | 627 | |
| donatien | 0:013f5d54248c | 628 | if (mb->wait_send) { |
| donatien | 0:013f5d54248c | 629 | sys_sem_signal(&mb->not_full); |
| donatien | 0:013f5d54248c | 630 | } |
| donatien | 0:013f5d54248c | 631 | |
| donatien | 0:013f5d54248c | 632 | sys_sem_signal(&mb->mutex); |
| donatien | 0:013f5d54248c | 633 | |
| donatien | 0:013f5d54248c | 634 | return 0; |
| donatien | 0:013f5d54248c | 635 | } |
| donatien | 0:013f5d54248c | 636 | /*-----------------------------------------------------------------------------------*/ |
| donatien | 0:013f5d54248c | 637 | u32_t |
| donatien | 0:013f5d54248c | 638 | sys_arch_mbox_fetch(sys_mbox_t* mb, void **msg, u32_t timeout) |
| donatien | 0:013f5d54248c | 639 | { |
| donatien | 0:013f5d54248c | 640 | u32_t time_needed = 0; |
| donatien | 0:013f5d54248c | 641 | LWIP_ASSERT("invalid mbox", (mb != NULL)); |
| donatien | 0:013f5d54248c | 642 | |
| donatien | 0:013f5d54248c | 643 | /* The mutex lock is quick so we don't bother with the timeout |
| donatien | 0:013f5d54248c | 644 | stuff here. */ |
| donatien | 0:013f5d54248c | 645 | sys_arch_sem_wait(&mb->mutex, 0); |
| donatien | 0:013f5d54248c | 646 | |
| donatien | 0:013f5d54248c | 647 | while (mb->first == mb->last) { |
| donatien | 0:013f5d54248c | 648 | sys_sem_signal(&mb->mutex); |
| donatien | 0:013f5d54248c | 649 | |
| donatien | 0:013f5d54248c | 650 | /* We block while waiting for a mail to arrive in the mailbox. We |
| donatien | 0:013f5d54248c | 651 | must be prepared to timeout. */ |
| donatien | 0:013f5d54248c | 652 | if (timeout != 0) { |
| donatien | 0:013f5d54248c | 653 | time_needed = sys_arch_sem_wait(&mb->not_empty, timeout); |
| donatien | 0:013f5d54248c | 654 | |
| donatien | 0:013f5d54248c | 655 | if (time_needed == SYS_ARCH_TIMEOUT) { |
| donatien | 0:013f5d54248c | 656 | return SYS_ARCH_TIMEOUT; |
| donatien | 0:013f5d54248c | 657 | } |
| donatien | 0:013f5d54248c | 658 | } else { |
| donatien | 0:013f5d54248c | 659 | sys_arch_sem_wait(&mb->not_empty, 0); |
| donatien | 0:013f5d54248c | 660 | } |
| donatien | 0:013f5d54248c | 661 | sys_arch_sem_wait(&mb->mutex, 0); |
| donatien | 0:013f5d54248c | 662 | } |
| donatien | 0:013f5d54248c | 663 | |
| donatien | 0:013f5d54248c | 664 | if (msg != NULL) { |
| donatien | 0:013f5d54248c | 665 | LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_fetch: mbox %p msg %p\n", (void *)mb, *msg)); |
| donatien | 0:013f5d54248c | 666 | *msg = mb->msgs[mb->first % SYS_MBOX_SIZE]; |
| donatien | 0:013f5d54248c | 667 | } |
| donatien | 0:013f5d54248c | 668 | else{ |
| donatien | 0:013f5d54248c | 669 | LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_fetch: mbox %p, null msg\n", (void *)mb)); |
| donatien | 0:013f5d54248c | 670 | } |
| donatien | 0:013f5d54248c | 671 | |
| donatien | 0:013f5d54248c | 672 | mb->first++; |
| donatien | 0:013f5d54248c | 673 | |
| donatien | 0:013f5d54248c | 674 | if (mb->wait_send) { |
| donatien | 0:013f5d54248c | 675 | sys_sem_signal(&mb->not_full); |
| donatien | 0:013f5d54248c | 676 | } |
| donatien | 0:013f5d54248c | 677 | |
| donatien | 0:013f5d54248c | 678 | sys_sem_signal(&mb->mutex); |
| donatien | 0:013f5d54248c | 679 | |
| donatien | 0:013f5d54248c | 680 | return time_needed; |
| donatien | 0:013f5d54248c | 681 | } |
| donatien | 0:013f5d54248c | 682 | |
| donatien | 0:013f5d54248c | 683 | #endif |
| donatien | 0:013f5d54248c | 684 | |
| donatien | 0:013f5d54248c | 685 | //Threads and timeout lists |
| donatien | 0:013f5d54248c | 686 | |
| donatien | 0:013f5d54248c | 687 | /* |
| donatien | 0:013f5d54248c | 688 | The implementation reserves a pool of threads that can be used by |
| donatien | 0:013f5d54248c | 689 | LwIP in order to only use static allocation |
| donatien | 0:013f5d54248c | 690 | */ |
| donatien | 0:013f5d54248c | 691 | |
| donatien | 0:013f5d54248c | 692 | //sys_thread_t* lwip_system_threads = NULL; //Linked list of active threads |
| donatien | 0:013f5d54248c | 693 | //struct sys_timeo* lwip_system_timeouts = NULL; // Default timeouts list for lwIP |
| donatien | 0:013f5d54248c | 694 | |
| donatien | 0:013f5d54248c | 695 | /* |
| donatien | 0:013f5d54248c | 696 | Instanciates a thread for LwIP: |
| donatien | 0:013f5d54248c | 697 | name is the thread name. thread(arg) is the call made as the thread's |
| donatien | 0:013f5d54248c | 698 | entry point. stacksize is the recommended stack size for this thread. |
| donatien | 0:013f5d54248c | 699 | prio is the priority that lwIP asks for. Stack size(s) and priority(ies) |
| donatien | 0:013f5d54248c | 700 | are defined in lwipopts.h. |
| donatien | 0:013f5d54248c | 701 | */ |
| donatien | 0:013f5d54248c | 702 | |
| donatien | 0:013f5d54248c | 703 | struct sys_thread |
| donatien | 0:013f5d54248c | 704 | { |
| donatien | 0:013f5d54248c | 705 | Thread* pTask; |
| donatien | 0:013f5d54248c | 706 | //struct sys_timeo* timeouts; |
| donatien | 0:013f5d54248c | 707 | struct sys_thread* next; |
| donatien | 0:013f5d54248c | 708 | }; |
| donatien | 0:013f5d54248c | 709 | struct sys_thread* lwip_system_threads = NULL; //Linked list of active threads |
| donatien | 0:013f5d54248c | 710 | |
| donatien | 0:013f5d54248c | 711 | sys_thread_t sys_thread_new(const char *name, lwip_thread_fn fn, void *arg, int stacksize, int prio) |
| donatien | 0:013f5d54248c | 712 | { |
| donatien | 0:013f5d54248c | 713 | SYS_ARCH_DECL_PROTECT(lev); |
| donatien | 0:013f5d54248c | 714 | SYS_ARCH_PROTECT(lev); //Protect this section |
| donatien | 0:013f5d54248c | 715 | struct sys_thread* pT = new struct sys_thread; |
| donatien | 0:013f5d54248c | 716 | SYS_ARCH_UNPROTECT(lev); |
| donatien | 0:013f5d54248c | 717 | if(pT==NULL) |
| donatien | 0:013f5d54248c | 718 | { |
| donatien | 0:013f5d54248c | 719 | ERR("No mem"); |
| donatien | 0:013f5d54248c | 720 | return; |
| donatien | 0:013f5d54248c | 721 | } |
| donatien | 0:013f5d54248c | 722 | |
| donatien | 0:013f5d54248c | 723 | SYS_ARCH_PROTECT(lev); //Protect this section |
| donatien | 0:013f5d54248c | 724 | |
| donatien | 0:013f5d54248c | 725 | //Link in list |
| donatien | 0:013f5d54248c | 726 | pT->next = lwip_system_threads; |
| donatien | 0:013f5d54248c | 727 | lwip_system_threads = pT; |
| donatien | 0:013f5d54248c | 728 | |
| donatien | 0:013f5d54248c | 729 | |
| donatien | 0:013f5d54248c | 730 | SYS_ARCH_UNPROTECT(lev); //Protect this section |
| donatien | 0:013f5d54248c | 731 | |
| donatien | 0:013f5d54248c | 732 | DBG("Trying to create new thread of stacksize %d and prio %d", stacksize, prio); |
| donatien | 0:013f5d54248c | 733 | |
| donatien | 0:013f5d54248c | 734 | //Must be done at the very end because the task will start immediately |
| donatien | 0:013f5d54248c | 735 | SYS_ARCH_PROTECT(lev); |
| donatien | 0:013f5d54248c | 736 | pT->pTask = new Thread((void (*)(void const *argument))fn, arg, (osPriority)prio /*FIXME*/, stacksize); |
| donatien | 0:013f5d54248c | 737 | SYS_ARCH_UNPROTECT(lev); |
| donatien | 0:013f5d54248c | 738 | |
| donatien | 0:013f5d54248c | 739 | DBG("pT->pTask=%p", pT->pTask); |
| donatien | 0:013f5d54248c | 740 | if(pT->pTask == NULL) |
| donatien | 0:013f5d54248c | 741 | { |
| donatien | 0:013f5d54248c | 742 | SYS_ARCH_PROTECT(lev); |
| donatien | 0:013f5d54248c | 743 | |
| donatien | 0:013f5d54248c | 744 | //Unlink |
| donatien | 0:013f5d54248c | 745 | if(pT == lwip_system_threads) |
| donatien | 0:013f5d54248c | 746 | { |
| donatien | 0:013f5d54248c | 747 | lwip_system_threads = pT->next; |
| donatien | 0:013f5d54248c | 748 | } |
| donatien | 0:013f5d54248c | 749 | else |
| donatien | 0:013f5d54248c | 750 | { |
| donatien | 0:013f5d54248c | 751 | struct sys_thread* pLT = lwip_system_threads; |
| donatien | 0:013f5d54248c | 752 | while(pLT->next != pT) |
| donatien | 0:013f5d54248c | 753 | { |
| donatien | 0:013f5d54248c | 754 | pLT = pLT->next; |
| donatien | 0:013f5d54248c | 755 | } |
| donatien | 0:013f5d54248c | 756 | pLT->next = pT->next; |
| donatien | 0:013f5d54248c | 757 | } |
| donatien | 0:013f5d54248c | 758 | SYS_ARCH_UNPROTECT(lev); //Release protection |
| donatien | 0:013f5d54248c | 759 | SYS_ARCH_PROTECT(lev); |
| donatien | 0:013f5d54248c | 760 | delete pT; |
| donatien | 0:013f5d54248c | 761 | SYS_ARCH_UNPROTECT(lev); |
| donatien | 0:013f5d54248c | 762 | ERR("No mem"); |
| donatien | 0:013f5d54248c | 763 | return; |
| donatien | 0:013f5d54248c | 764 | } |
| donatien | 0:013f5d54248c | 765 | |
| donatien | 0:013f5d54248c | 766 | DBG("Thread OK"); |
| donatien | 0:013f5d54248c | 767 | return; //Return the thread |
| donatien | 0:013f5d54248c | 768 | } |
| donatien | 0:013f5d54248c | 769 | #if 0 |
| donatien | 0:013f5d54248c | 770 | struct sys_timeouts *sys_arch_timeouts(void) { |
| donatien | 0:013f5d54248c | 771 | struct sys_timeo* timeouts; |
| donatien | 0:013f5d54248c | 772 | |
| donatien | 0:013f5d54248c | 773 | SYS_ARCH_DECL_PROTECT(lev); |
| donatien | 0:013f5d54248c | 774 | |
| donatien | 0:013f5d54248c | 775 | timeouts = &lwip_system_timeouts; //If there is no match, just return the system-wide default version |
| donatien | 0:013f5d54248c | 776 | |
| donatien | 0:013f5d54248c | 777 | SYS_ARCH_PROTECT(lev); //Protect this section |
| donatien | 0:013f5d54248c | 778 | |
| donatien | 0:013f5d54248c | 779 | sys_thread_t pT = lwip_system_threads; |
| donatien | 0:013f5d54248c | 780 | |
| donatien | 0:013f5d54248c | 781 | // Search the threads list for the thread that is currently running |
| donatien | 0:013f5d54248c | 782 | for ( ; pT!=NULL; pT=pT->next) |
| donatien | 0:013f5d54248c | 783 | { |
| donatien | 0:013f5d54248c | 784 | if ( Task::isCurrent(pT->pTask) ) |
| donatien | 0:013f5d54248c | 785 | { |
| donatien | 0:013f5d54248c | 786 | timeouts = pT->timeouts; |
| donatien | 0:013f5d54248c | 787 | } |
| donatien | 0:013f5d54248c | 788 | } |
| donatien | 0:013f5d54248c | 789 | |
| donatien | 0:013f5d54248c | 790 | SYS_ARCH_UNPROTECT(lev); //Release protection |
| donatien | 0:013f5d54248c | 791 | |
| donatien | 0:013f5d54248c | 792 | return timeouts; |
| donatien | 0:013f5d54248c | 793 | } |
| donatien | 0:013f5d54248c | 794 | #endif |
| donatien | 0:013f5d54248c | 795 | /* |
| donatien | 0:013f5d54248c | 796 | Architecture-specific initialization. |
| donatien | 0:013f5d54248c | 797 | */ |
| donatien | 0:013f5d54248c | 798 | static Timer sys_jiffies_timer; |
| donatien | 0:013f5d54248c | 799 | |
| donatien | 0:013f5d54248c | 800 | void sys_init(void) |
| donatien | 0:013f5d54248c | 801 | { |
| donatien | 0:013f5d54248c | 802 | sys_jiffies_timer.start(); |
| donatien | 0:013f5d54248c | 803 | } |
| donatien | 0:013f5d54248c | 804 | |
| donatien | 0:013f5d54248c | 805 | /* |
| donatien | 0:013f5d54248c | 806 | Used by PPP as a timestamp-ish value. |
| donatien | 0:013f5d54248c | 807 | */ |
| donatien | 0:013f5d54248c | 808 | u32_t sys_jiffies(void) |
| donatien | 0:013f5d54248c | 809 | { |
| donatien | 0:013f5d54248c | 810 | static u32_t jiffies = 0; |
| donatien | 0:013f5d54248c | 811 | jiffies += 1 + sys_jiffies_timer.read_ms()/10; |
| donatien | 0:013f5d54248c | 812 | sys_jiffies_timer.reset(); |
| donatien | 0:013f5d54248c | 813 | |
| donatien | 0:013f5d54248c | 814 | return jiffies; |
| donatien | 0:013f5d54248c | 815 | } |
| donatien | 0:013f5d54248c | 816 | |
| donatien | 0:013f5d54248c | 817 | /** |
| donatien | 0:013f5d54248c | 818 | * Sleep for some ms. Timeouts are NOT processed while sleeping. |
| donatien | 0:013f5d54248c | 819 | * |
| donatien | 0:013f5d54248c | 820 | * @param ms number of milliseconds to sleep |
| donatien | 0:013f5d54248c | 821 | */ |
| donatien | 0:013f5d54248c | 822 | //Change DG, arch specific |
| donatien | 0:013f5d54248c | 823 | void |
| donatien | 0:013f5d54248c | 824 | sys_msleep(u32_t ms) |
| donatien | 0:013f5d54248c | 825 | { |
| donatien | 0:013f5d54248c | 826 | Thread::wait(ms); |
| donatien | 0:013f5d54248c | 827 | } |
| donatien | 0:013f5d54248c | 828 | |
| donatien | 0:013f5d54248c | 829 | } //extern "C" |
