Onenet

Dependents:   K64F_eCompass_OneNET_JW

Committer:
robert_jw
Date:
Mon Jun 20 01:40:20 2016 +0000
Revision:
0:b2805b6888dc
ADS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
robert_jw 0:b2805b6888dc 1 /* Copyright (C) 2012 mbed.org, MIT License
robert_jw 0:b2805b6888dc 2 *
robert_jw 0:b2805b6888dc 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
robert_jw 0:b2805b6888dc 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
robert_jw 0:b2805b6888dc 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
robert_jw 0:b2805b6888dc 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
robert_jw 0:b2805b6888dc 7 * furnished to do so, subject to the following conditions:
robert_jw 0:b2805b6888dc 8 *
robert_jw 0:b2805b6888dc 9 * The above copyright notice and this permission notice shall be included in all copies or
robert_jw 0:b2805b6888dc 10 * substantial portions of the Software.
robert_jw 0:b2805b6888dc 11 *
robert_jw 0:b2805b6888dc 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
robert_jw 0:b2805b6888dc 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
robert_jw 0:b2805b6888dc 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
robert_jw 0:b2805b6888dc 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
robert_jw 0:b2805b6888dc 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
robert_jw 0:b2805b6888dc 17 */
robert_jw 0:b2805b6888dc 18 #include <string.h>
robert_jw 0:b2805b6888dc 19
robert_jw 0:b2805b6888dc 20 /* mbed includes */
robert_jw 0:b2805b6888dc 21 #include "mbed_error.h"
robert_jw 0:b2805b6888dc 22 #include "mbed_interface.h"
robert_jw 0:b2805b6888dc 23 #include "us_ticker_api.h"
robert_jw 0:b2805b6888dc 24
robert_jw 0:b2805b6888dc 25 /* lwIP includes. */
robert_jw 0:b2805b6888dc 26 #include "lwip/opt.h"
robert_jw 0:b2805b6888dc 27 #include "lwip/debug.h"
robert_jw 0:b2805b6888dc 28 #include "lwip/def.h"
robert_jw 0:b2805b6888dc 29 #include "lwip/sys.h"
robert_jw 0:b2805b6888dc 30 #include "lwip/mem.h"
robert_jw 0:b2805b6888dc 31
robert_jw 0:b2805b6888dc 32 #if NO_SYS==1
robert_jw 0:b2805b6888dc 33 #include "cmsis.h"
robert_jw 0:b2805b6888dc 34
robert_jw 0:b2805b6888dc 35 /* Saved total time in ms since timer was enabled */
robert_jw 0:b2805b6888dc 36 static volatile u32_t systick_timems;
robert_jw 0:b2805b6888dc 37
robert_jw 0:b2805b6888dc 38 /* Enable systick rate and interrupt */
robert_jw 0:b2805b6888dc 39 void SysTick_Init(void) {
robert_jw 0:b2805b6888dc 40 if (SysTick_Config(SystemCoreClock / 1000)) {
robert_jw 0:b2805b6888dc 41 while (1); /* Capture error */
robert_jw 0:b2805b6888dc 42 }
robert_jw 0:b2805b6888dc 43 }
robert_jw 0:b2805b6888dc 44
robert_jw 0:b2805b6888dc 45 /** \brief SysTick IRQ handler and timebase management
robert_jw 0:b2805b6888dc 46 *
robert_jw 0:b2805b6888dc 47 * This function keeps a timebase for the sysTick that can be
robert_jw 0:b2805b6888dc 48 * used for other functions. It also calls an external function
robert_jw 0:b2805b6888dc 49 * (SysTick_User) that must be defined outside this handler.
robert_jw 0:b2805b6888dc 50 */
robert_jw 0:b2805b6888dc 51 void SysTick_Handler(void) {
robert_jw 0:b2805b6888dc 52 systick_timems++;
robert_jw 0:b2805b6888dc 53 }
robert_jw 0:b2805b6888dc 54
robert_jw 0:b2805b6888dc 55 /* Delay for the specified number of milliSeconds */
robert_jw 0:b2805b6888dc 56 void osDelay(uint32_t ms) {
robert_jw 0:b2805b6888dc 57 uint32_t to = ms + systick_timems;
robert_jw 0:b2805b6888dc 58 while (to > systick_timems);
robert_jw 0:b2805b6888dc 59 }
robert_jw 0:b2805b6888dc 60
robert_jw 0:b2805b6888dc 61 /* Returns the current time in mS. This is needed for the LWIP timers */
robert_jw 0:b2805b6888dc 62 u32_t sys_now(void) {
robert_jw 0:b2805b6888dc 63 return (u32_t) systick_timems;
robert_jw 0:b2805b6888dc 64 }
robert_jw 0:b2805b6888dc 65
robert_jw 0:b2805b6888dc 66 #else
robert_jw 0:b2805b6888dc 67 /* CMSIS-RTOS implementation of the lwip operating system abstraction */
robert_jw 0:b2805b6888dc 68 #include "arch/sys_arch.h"
robert_jw 0:b2805b6888dc 69
robert_jw 0:b2805b6888dc 70 /*---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 71 * Routine: sys_mbox_new
robert_jw 0:b2805b6888dc 72 *---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 73 * Description:
robert_jw 0:b2805b6888dc 74 * Creates a new mailbox
robert_jw 0:b2805b6888dc 75 * Inputs:
robert_jw 0:b2805b6888dc 76 * sys_mbox_t mbox -- Handle of mailbox
robert_jw 0:b2805b6888dc 77 * int queue_sz -- Size of elements in the mailbox
robert_jw 0:b2805b6888dc 78 * Outputs:
robert_jw 0:b2805b6888dc 79 * err_t -- ERR_OK if message posted, else ERR_MEM
robert_jw 0:b2805b6888dc 80 *---------------------------------------------------------------------------*/
robert_jw 0:b2805b6888dc 81 err_t sys_mbox_new(sys_mbox_t *mbox, int queue_sz) {
robert_jw 0:b2805b6888dc 82 if (queue_sz > MB_SIZE)
robert_jw 0:b2805b6888dc 83 error("sys_mbox_new size error\n");
robert_jw 0:b2805b6888dc 84
robert_jw 0:b2805b6888dc 85 #ifdef CMSIS_OS_RTX
robert_jw 0:b2805b6888dc 86 memset(mbox->queue, 0, sizeof(mbox->queue));
robert_jw 0:b2805b6888dc 87 mbox->def.pool = mbox->queue;
robert_jw 0:b2805b6888dc 88 mbox->def.queue_sz = queue_sz;
robert_jw 0:b2805b6888dc 89 #endif
robert_jw 0:b2805b6888dc 90 mbox->id = osMessageCreate(&mbox->def, NULL);
robert_jw 0:b2805b6888dc 91 return (mbox->id == NULL) ? (ERR_MEM) : (ERR_OK);
robert_jw 0:b2805b6888dc 92 }
robert_jw 0:b2805b6888dc 93
robert_jw 0:b2805b6888dc 94 /*---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 95 * Routine: sys_mbox_free
robert_jw 0:b2805b6888dc 96 *---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 97 * Description:
robert_jw 0:b2805b6888dc 98 * Deallocates a mailbox. If there are messages still present in the
robert_jw 0:b2805b6888dc 99 * mailbox when the mailbox is deallocated, it is an indication of a
robert_jw 0:b2805b6888dc 100 * programming error in lwIP and the developer should be notified.
robert_jw 0:b2805b6888dc 101 * Inputs:
robert_jw 0:b2805b6888dc 102 * sys_mbox_t *mbox -- Handle of mailbox
robert_jw 0:b2805b6888dc 103 *---------------------------------------------------------------------------*/
robert_jw 0:b2805b6888dc 104 void sys_mbox_free(sys_mbox_t *mbox) {
robert_jw 0:b2805b6888dc 105 osEvent event = osMessageGet(mbox->id, 0);
robert_jw 0:b2805b6888dc 106 if (event.status == osEventMessage)
robert_jw 0:b2805b6888dc 107 error("sys_mbox_free error\n");
robert_jw 0:b2805b6888dc 108 }
robert_jw 0:b2805b6888dc 109
robert_jw 0:b2805b6888dc 110 /*---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 111 * Routine: sys_mbox_post
robert_jw 0:b2805b6888dc 112 *---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 113 * Description:
robert_jw 0:b2805b6888dc 114 * Post the "msg" to the mailbox.
robert_jw 0:b2805b6888dc 115 * Inputs:
robert_jw 0:b2805b6888dc 116 * sys_mbox_t mbox -- Handle of mailbox
robert_jw 0:b2805b6888dc 117 * void *msg -- Pointer to data to post
robert_jw 0:b2805b6888dc 118 *---------------------------------------------------------------------------*/
robert_jw 0:b2805b6888dc 119 void sys_mbox_post(sys_mbox_t *mbox, void *msg) {
robert_jw 0:b2805b6888dc 120 if (osMessagePut(mbox->id, (uint32_t)msg, osWaitForever) != osOK)
robert_jw 0:b2805b6888dc 121 error("sys_mbox_post error\n");
robert_jw 0:b2805b6888dc 122 }
robert_jw 0:b2805b6888dc 123
robert_jw 0:b2805b6888dc 124 /*---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 125 * Routine: sys_mbox_trypost
robert_jw 0:b2805b6888dc 126 *---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 127 * Description:
robert_jw 0:b2805b6888dc 128 * Try to post the "msg" to the mailbox. Returns immediately with
robert_jw 0:b2805b6888dc 129 * error if cannot.
robert_jw 0:b2805b6888dc 130 * Inputs:
robert_jw 0:b2805b6888dc 131 * sys_mbox_t mbox -- Handle of mailbox
robert_jw 0:b2805b6888dc 132 * void *msg -- Pointer to data to post
robert_jw 0:b2805b6888dc 133 * Outputs:
robert_jw 0:b2805b6888dc 134 * err_t -- ERR_OK if message posted, else ERR_MEM
robert_jw 0:b2805b6888dc 135 * if not.
robert_jw 0:b2805b6888dc 136 *---------------------------------------------------------------------------*/
robert_jw 0:b2805b6888dc 137 err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg) {
robert_jw 0:b2805b6888dc 138 osStatus status = osMessagePut(mbox->id, (uint32_t)msg, 0);
robert_jw 0:b2805b6888dc 139 return (status == osOK) ? (ERR_OK) : (ERR_MEM);
robert_jw 0:b2805b6888dc 140 }
robert_jw 0:b2805b6888dc 141
robert_jw 0:b2805b6888dc 142 /*---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 143 * Routine: sys_arch_mbox_fetch
robert_jw 0:b2805b6888dc 144 *---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 145 * Description:
robert_jw 0:b2805b6888dc 146 * Blocks the thread until a message arrives in the mailbox, but does
robert_jw 0:b2805b6888dc 147 * not block the thread longer than "timeout" milliseconds (similar to
robert_jw 0:b2805b6888dc 148 * the sys_arch_sem_wait() function). The "msg" argument is a result
robert_jw 0:b2805b6888dc 149 * parameter that is set by the function (i.e., by doing "*msg =
robert_jw 0:b2805b6888dc 150 * ptr"). The "msg" parameter maybe NULL to indicate that the message
robert_jw 0:b2805b6888dc 151 * should be dropped.
robert_jw 0:b2805b6888dc 152 *
robert_jw 0:b2805b6888dc 153 * The return values are the same as for the sys_arch_sem_wait() function:
robert_jw 0:b2805b6888dc 154 * Number of milliseconds spent waiting or SYS_ARCH_TIMEOUT if there was a
robert_jw 0:b2805b6888dc 155 * timeout.
robert_jw 0:b2805b6888dc 156 *
robert_jw 0:b2805b6888dc 157 * Note that a function with a similar name, sys_mbox_fetch(), is
robert_jw 0:b2805b6888dc 158 * implemented by lwIP.
robert_jw 0:b2805b6888dc 159 * Inputs:
robert_jw 0:b2805b6888dc 160 * sys_mbox_t mbox -- Handle of mailbox
robert_jw 0:b2805b6888dc 161 * void **msg -- Pointer to pointer to msg received
robert_jw 0:b2805b6888dc 162 * u32_t timeout -- Number of milliseconds until timeout
robert_jw 0:b2805b6888dc 163 * Outputs:
robert_jw 0:b2805b6888dc 164 * u32_t -- SYS_ARCH_TIMEOUT if timeout, else number
robert_jw 0:b2805b6888dc 165 * of milliseconds until received.
robert_jw 0:b2805b6888dc 166 *---------------------------------------------------------------------------*/
robert_jw 0:b2805b6888dc 167 u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout) {
robert_jw 0:b2805b6888dc 168 u32_t start = us_ticker_read();
robert_jw 0:b2805b6888dc 169
robert_jw 0:b2805b6888dc 170 osEvent event = osMessageGet(mbox->id, (timeout != 0)?(timeout):(osWaitForever));
robert_jw 0:b2805b6888dc 171 if (event.status != osEventMessage)
robert_jw 0:b2805b6888dc 172 return SYS_ARCH_TIMEOUT;
robert_jw 0:b2805b6888dc 173
robert_jw 0:b2805b6888dc 174 *msg = (void *)event.value.v;
robert_jw 0:b2805b6888dc 175
robert_jw 0:b2805b6888dc 176 return (us_ticker_read() - start) / 1000;
robert_jw 0:b2805b6888dc 177 }
robert_jw 0:b2805b6888dc 178
robert_jw 0:b2805b6888dc 179 /*---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 180 * Routine: sys_arch_mbox_tryfetch
robert_jw 0:b2805b6888dc 181 *---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 182 * Description:
robert_jw 0:b2805b6888dc 183 * Similar to sys_arch_mbox_fetch, but if message is not ready
robert_jw 0:b2805b6888dc 184 * immediately, we'll return with SYS_MBOX_EMPTY. On success, 0 is
robert_jw 0:b2805b6888dc 185 * returned.
robert_jw 0:b2805b6888dc 186 * Inputs:
robert_jw 0:b2805b6888dc 187 * sys_mbox_t mbox -- Handle of mailbox
robert_jw 0:b2805b6888dc 188 * void **msg -- Pointer to pointer to msg received
robert_jw 0:b2805b6888dc 189 * Outputs:
robert_jw 0:b2805b6888dc 190 * u32_t -- SYS_MBOX_EMPTY if no messages. Otherwise,
robert_jw 0:b2805b6888dc 191 * return ERR_OK.
robert_jw 0:b2805b6888dc 192 *---------------------------------------------------------------------------*/
robert_jw 0:b2805b6888dc 193 u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg) {
robert_jw 0:b2805b6888dc 194 osEvent event = osMessageGet(mbox->id, 0);
robert_jw 0:b2805b6888dc 195 if (event.status != osEventMessage)
robert_jw 0:b2805b6888dc 196 return SYS_MBOX_EMPTY;
robert_jw 0:b2805b6888dc 197
robert_jw 0:b2805b6888dc 198 *msg = (void *)event.value.v;
robert_jw 0:b2805b6888dc 199
robert_jw 0:b2805b6888dc 200 return ERR_OK;
robert_jw 0:b2805b6888dc 201 }
robert_jw 0:b2805b6888dc 202
robert_jw 0:b2805b6888dc 203 /*---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 204 * Routine: sys_sem_new
robert_jw 0:b2805b6888dc 205 *---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 206 * Description:
robert_jw 0:b2805b6888dc 207 * Creates and returns a new semaphore. The "ucCount" argument specifies
robert_jw 0:b2805b6888dc 208 * the initial state of the semaphore.
robert_jw 0:b2805b6888dc 209 * NOTE: Currently this routine only creates counts of 1 or 0
robert_jw 0:b2805b6888dc 210 * Inputs:
robert_jw 0:b2805b6888dc 211 * sys_sem_t sem -- Handle of semaphore
robert_jw 0:b2805b6888dc 212 * u8_t count -- Initial count of semaphore
robert_jw 0:b2805b6888dc 213 * Outputs:
robert_jw 0:b2805b6888dc 214 * err_t -- ERR_OK if semaphore created
robert_jw 0:b2805b6888dc 215 *---------------------------------------------------------------------------*/
robert_jw 0:b2805b6888dc 216 err_t sys_sem_new(sys_sem_t *sem, u8_t count) {
robert_jw 0:b2805b6888dc 217 #ifdef CMSIS_OS_RTX
robert_jw 0:b2805b6888dc 218 memset(sem->data, 0, sizeof(uint32_t)*2);
robert_jw 0:b2805b6888dc 219 sem->def.semaphore = sem->data;
robert_jw 0:b2805b6888dc 220 #endif
robert_jw 0:b2805b6888dc 221 sem->id = osSemaphoreCreate(&sem->def, count);
robert_jw 0:b2805b6888dc 222 if (sem->id == NULL)
robert_jw 0:b2805b6888dc 223 error("sys_sem_new create error\n");
robert_jw 0:b2805b6888dc 224
robert_jw 0:b2805b6888dc 225 return ERR_OK;
robert_jw 0:b2805b6888dc 226 }
robert_jw 0:b2805b6888dc 227
robert_jw 0:b2805b6888dc 228 /*---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 229 * Routine: sys_arch_sem_wait
robert_jw 0:b2805b6888dc 230 *---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 231 * Description:
robert_jw 0:b2805b6888dc 232 * Blocks the thread while waiting for the semaphore to be
robert_jw 0:b2805b6888dc 233 * signaled. If the "timeout" argument is non-zero, the thread should
robert_jw 0:b2805b6888dc 234 * only be blocked for the specified time (measured in
robert_jw 0:b2805b6888dc 235 * milliseconds).
robert_jw 0:b2805b6888dc 236 *
robert_jw 0:b2805b6888dc 237 * If the timeout argument is non-zero, the return value is the number of
robert_jw 0:b2805b6888dc 238 * milliseconds spent waiting for the semaphore to be signaled. If the
robert_jw 0:b2805b6888dc 239 * semaphore wasn't signaled within the specified time, the return value is
robert_jw 0:b2805b6888dc 240 * SYS_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore
robert_jw 0:b2805b6888dc 241 * (i.e., it was already signaled), the function may return zero.
robert_jw 0:b2805b6888dc 242 *
robert_jw 0:b2805b6888dc 243 * Notice that lwIP implements a function with a similar name,
robert_jw 0:b2805b6888dc 244 * sys_sem_wait(), that uses the sys_arch_sem_wait() function.
robert_jw 0:b2805b6888dc 245 * Inputs:
robert_jw 0:b2805b6888dc 246 * sys_sem_t sem -- Semaphore to wait on
robert_jw 0:b2805b6888dc 247 * u32_t timeout -- Number of milliseconds until timeout
robert_jw 0:b2805b6888dc 248 * Outputs:
robert_jw 0:b2805b6888dc 249 * u32_t -- Time elapsed or SYS_ARCH_TIMEOUT.
robert_jw 0:b2805b6888dc 250 *---------------------------------------------------------------------------*/
robert_jw 0:b2805b6888dc 251 u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout) {
robert_jw 0:b2805b6888dc 252 u32_t start = us_ticker_read();
robert_jw 0:b2805b6888dc 253
robert_jw 0:b2805b6888dc 254 if (osSemaphoreWait(sem->id, (timeout != 0)?(timeout):(osWaitForever)) < 1)
robert_jw 0:b2805b6888dc 255 return SYS_ARCH_TIMEOUT;
robert_jw 0:b2805b6888dc 256
robert_jw 0:b2805b6888dc 257 return (us_ticker_read() - start) / 1000;
robert_jw 0:b2805b6888dc 258 }
robert_jw 0:b2805b6888dc 259
robert_jw 0:b2805b6888dc 260 /*---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 261 * Routine: sys_sem_signal
robert_jw 0:b2805b6888dc 262 *---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 263 * Description:
robert_jw 0:b2805b6888dc 264 * Signals (releases) a semaphore
robert_jw 0:b2805b6888dc 265 * Inputs:
robert_jw 0:b2805b6888dc 266 * sys_sem_t sem -- Semaphore to signal
robert_jw 0:b2805b6888dc 267 *---------------------------------------------------------------------------*/
robert_jw 0:b2805b6888dc 268 void sys_sem_signal(sys_sem_t *data) {
robert_jw 0:b2805b6888dc 269 if (osSemaphoreRelease(data->id) != osOK)
robert_jw 0:b2805b6888dc 270 mbed_die(); /* Can be called by ISR do not use printf */
robert_jw 0:b2805b6888dc 271 }
robert_jw 0:b2805b6888dc 272
robert_jw 0:b2805b6888dc 273 /*---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 274 * Routine: sys_sem_free
robert_jw 0:b2805b6888dc 275 *---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 276 * Description:
robert_jw 0:b2805b6888dc 277 * Deallocates a semaphore
robert_jw 0:b2805b6888dc 278 * Inputs:
robert_jw 0:b2805b6888dc 279 * sys_sem_t sem -- Semaphore to free
robert_jw 0:b2805b6888dc 280 *---------------------------------------------------------------------------*/
robert_jw 0:b2805b6888dc 281 void sys_sem_free(sys_sem_t *sem) {}
robert_jw 0:b2805b6888dc 282
robert_jw 0:b2805b6888dc 283 /** Create a new mutex
robert_jw 0:b2805b6888dc 284 * @param mutex pointer to the mutex to create
robert_jw 0:b2805b6888dc 285 * @return a new mutex */
robert_jw 0:b2805b6888dc 286 err_t sys_mutex_new(sys_mutex_t *mutex) {
robert_jw 0:b2805b6888dc 287 #ifdef CMSIS_OS_RTX
robert_jw 0:b2805b6888dc 288 #ifdef __MBED_CMSIS_RTOS_CA9
robert_jw 0:b2805b6888dc 289 memset(mutex->data, 0, sizeof(int32_t)*4);
robert_jw 0:b2805b6888dc 290 #else
robert_jw 0:b2805b6888dc 291 memset(mutex->data, 0, sizeof(int32_t)*3);
robert_jw 0:b2805b6888dc 292 #endif
robert_jw 0:b2805b6888dc 293 mutex->def.mutex = mutex->data;
robert_jw 0:b2805b6888dc 294 #endif
robert_jw 0:b2805b6888dc 295 mutex->id = osMutexCreate(&mutex->def);
robert_jw 0:b2805b6888dc 296 if (mutex->id == NULL)
robert_jw 0:b2805b6888dc 297 return ERR_MEM;
robert_jw 0:b2805b6888dc 298
robert_jw 0:b2805b6888dc 299 return ERR_OK;
robert_jw 0:b2805b6888dc 300 }
robert_jw 0:b2805b6888dc 301
robert_jw 0:b2805b6888dc 302 /** Lock a mutex
robert_jw 0:b2805b6888dc 303 * @param mutex the mutex to lock */
robert_jw 0:b2805b6888dc 304 void sys_mutex_lock(sys_mutex_t *mutex) {
robert_jw 0:b2805b6888dc 305 if (osMutexWait(mutex->id, osWaitForever) != osOK)
robert_jw 0:b2805b6888dc 306 error("sys_mutex_lock error\n");
robert_jw 0:b2805b6888dc 307 }
robert_jw 0:b2805b6888dc 308
robert_jw 0:b2805b6888dc 309 /** Unlock a mutex
robert_jw 0:b2805b6888dc 310 * @param mutex the mutex to unlock */
robert_jw 0:b2805b6888dc 311 void sys_mutex_unlock(sys_mutex_t *mutex) {
robert_jw 0:b2805b6888dc 312 if (osMutexRelease(mutex->id) != osOK)
robert_jw 0:b2805b6888dc 313 error("sys_mutex_unlock error\n");
robert_jw 0:b2805b6888dc 314 }
robert_jw 0:b2805b6888dc 315
robert_jw 0:b2805b6888dc 316 /** Delete a mutex
robert_jw 0:b2805b6888dc 317 * @param mutex the mutex to delete */
robert_jw 0:b2805b6888dc 318 void sys_mutex_free(sys_mutex_t *mutex) {}
robert_jw 0:b2805b6888dc 319
robert_jw 0:b2805b6888dc 320 /*---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 321 * Routine: sys_init
robert_jw 0:b2805b6888dc 322 *---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 323 * Description:
robert_jw 0:b2805b6888dc 324 * Initialize sys arch
robert_jw 0:b2805b6888dc 325 *---------------------------------------------------------------------------*/
robert_jw 0:b2805b6888dc 326 osMutexId lwip_sys_mutex;
robert_jw 0:b2805b6888dc 327 osMutexDef(lwip_sys_mutex);
robert_jw 0:b2805b6888dc 328
robert_jw 0:b2805b6888dc 329 void sys_init(void) {
robert_jw 0:b2805b6888dc 330 us_ticker_read(); // Init sys tick
robert_jw 0:b2805b6888dc 331 lwip_sys_mutex = osMutexCreate(osMutex(lwip_sys_mutex));
robert_jw 0:b2805b6888dc 332 if (lwip_sys_mutex == NULL)
robert_jw 0:b2805b6888dc 333 error("sys_init error\n");
robert_jw 0:b2805b6888dc 334 }
robert_jw 0:b2805b6888dc 335
robert_jw 0:b2805b6888dc 336 /*---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 337 * Routine: sys_jiffies
robert_jw 0:b2805b6888dc 338 *---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 339 * Description:
robert_jw 0:b2805b6888dc 340 * Used by PPP as a timestamp-ish value
robert_jw 0:b2805b6888dc 341 *---------------------------------------------------------------------------*/
robert_jw 0:b2805b6888dc 342 u32_t sys_jiffies(void) {
robert_jw 0:b2805b6888dc 343 static u32_t jiffies = 0;
robert_jw 0:b2805b6888dc 344 jiffies += 1 + (us_ticker_read()/10000);
robert_jw 0:b2805b6888dc 345 return jiffies;
robert_jw 0:b2805b6888dc 346 }
robert_jw 0:b2805b6888dc 347
robert_jw 0:b2805b6888dc 348 /*---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 349 * Routine: sys_arch_protect
robert_jw 0:b2805b6888dc 350 *---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 351 * Description:
robert_jw 0:b2805b6888dc 352 * This optional function does a "fast" critical region protection and
robert_jw 0:b2805b6888dc 353 * returns the previous protection level. This function is only called
robert_jw 0:b2805b6888dc 354 * during very short critical regions. An embedded system which supports
robert_jw 0:b2805b6888dc 355 * ISR-based drivers might want to implement this function by disabling
robert_jw 0:b2805b6888dc 356 * interrupts. Task-based systems might want to implement this by using
robert_jw 0:b2805b6888dc 357 * a mutex or disabling tasking. This function should support recursive
robert_jw 0:b2805b6888dc 358 * calls from the same task or interrupt. In other words,
robert_jw 0:b2805b6888dc 359 * sys_arch_protect() could be called while already protected. In
robert_jw 0:b2805b6888dc 360 * that case the return value indicates that it is already protected.
robert_jw 0:b2805b6888dc 361 *
robert_jw 0:b2805b6888dc 362 * sys_arch_protect() is only required if your port is supporting an
robert_jw 0:b2805b6888dc 363 * operating system.
robert_jw 0:b2805b6888dc 364 * Outputs:
robert_jw 0:b2805b6888dc 365 * sys_prot_t -- Previous protection level (not used here)
robert_jw 0:b2805b6888dc 366 *---------------------------------------------------------------------------*/
robert_jw 0:b2805b6888dc 367 sys_prot_t sys_arch_protect(void) {
robert_jw 0:b2805b6888dc 368 if (osMutexWait(lwip_sys_mutex, osWaitForever) != osOK)
robert_jw 0:b2805b6888dc 369 error("sys_arch_protect error\n");
robert_jw 0:b2805b6888dc 370 return (sys_prot_t) 1;
robert_jw 0:b2805b6888dc 371 }
robert_jw 0:b2805b6888dc 372
robert_jw 0:b2805b6888dc 373 /*---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 374 * Routine: sys_arch_unprotect
robert_jw 0:b2805b6888dc 375 *---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 376 * Description:
robert_jw 0:b2805b6888dc 377 * This optional function does a "fast" set of critical region
robert_jw 0:b2805b6888dc 378 * protection to the value specified by pval. See the documentation for
robert_jw 0:b2805b6888dc 379 * sys_arch_protect() for more information. This function is only
robert_jw 0:b2805b6888dc 380 * required if your port is supporting an operating system.
robert_jw 0:b2805b6888dc 381 * Inputs:
robert_jw 0:b2805b6888dc 382 * sys_prot_t -- Previous protection level (not used here)
robert_jw 0:b2805b6888dc 383 *---------------------------------------------------------------------------*/
robert_jw 0:b2805b6888dc 384 void sys_arch_unprotect(sys_prot_t p) {
robert_jw 0:b2805b6888dc 385 if (osMutexRelease(lwip_sys_mutex) != osOK)
robert_jw 0:b2805b6888dc 386 error("sys_arch_unprotect error\n");
robert_jw 0:b2805b6888dc 387 }
robert_jw 0:b2805b6888dc 388
robert_jw 0:b2805b6888dc 389 u32_t sys_now(void) {
robert_jw 0:b2805b6888dc 390 return us_ticker_read() / 1000;
robert_jw 0:b2805b6888dc 391 }
robert_jw 0:b2805b6888dc 392
robert_jw 0:b2805b6888dc 393 void sys_msleep(u32_t ms) {
robert_jw 0:b2805b6888dc 394 osDelay(ms);
robert_jw 0:b2805b6888dc 395 }
robert_jw 0:b2805b6888dc 396
robert_jw 0:b2805b6888dc 397 // Keep a pool of thread structures
robert_jw 0:b2805b6888dc 398 static int thread_pool_index = 0;
robert_jw 0:b2805b6888dc 399 static sys_thread_data_t thread_pool[SYS_THREAD_POOL_N];
robert_jw 0:b2805b6888dc 400
robert_jw 0:b2805b6888dc 401 /*---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 402 * Routine: sys_thread_new
robert_jw 0:b2805b6888dc 403 *---------------------------------------------------------------------------*
robert_jw 0:b2805b6888dc 404 * Description:
robert_jw 0:b2805b6888dc 405 * Starts a new thread with priority "prio" that will begin its
robert_jw 0:b2805b6888dc 406 * execution in the function "thread()". The "arg" argument will be
robert_jw 0:b2805b6888dc 407 * passed as an argument to the thread() function. The id of the new
robert_jw 0:b2805b6888dc 408 * thread is returned. Both the id and the priority are system
robert_jw 0:b2805b6888dc 409 * dependent.
robert_jw 0:b2805b6888dc 410 * Inputs:
robert_jw 0:b2805b6888dc 411 * char *name -- Name of thread
robert_jw 0:b2805b6888dc 412 * void (*thread)(void *arg) -- Pointer to function to run.
robert_jw 0:b2805b6888dc 413 * void *arg -- Argument passed into function
robert_jw 0:b2805b6888dc 414 * int stacksize -- Required stack amount in bytes
robert_jw 0:b2805b6888dc 415 * int priority -- Thread priority
robert_jw 0:b2805b6888dc 416 * Outputs:
robert_jw 0:b2805b6888dc 417 * sys_thread_t -- Pointer to thread handle.
robert_jw 0:b2805b6888dc 418 *---------------------------------------------------------------------------*/
robert_jw 0:b2805b6888dc 419 sys_thread_t sys_thread_new(const char *pcName,
robert_jw 0:b2805b6888dc 420 void (*thread)(void *arg),
robert_jw 0:b2805b6888dc 421 void *arg, int stacksize, int priority) {
robert_jw 0:b2805b6888dc 422 LWIP_DEBUGF(SYS_DEBUG, ("New Thread: %s\n", pcName));
robert_jw 0:b2805b6888dc 423
robert_jw 0:b2805b6888dc 424 if (thread_pool_index >= SYS_THREAD_POOL_N)
robert_jw 0:b2805b6888dc 425 error("sys_thread_new number error\n");
robert_jw 0:b2805b6888dc 426 sys_thread_t t = (sys_thread_t)&thread_pool[thread_pool_index];
robert_jw 0:b2805b6888dc 427 thread_pool_index++;
robert_jw 0:b2805b6888dc 428
robert_jw 0:b2805b6888dc 429 #ifdef CMSIS_OS_RTX
robert_jw 0:b2805b6888dc 430 t->def.pthread = (os_pthread)thread;
robert_jw 0:b2805b6888dc 431 t->def.tpriority = (osPriority)priority;
robert_jw 0:b2805b6888dc 432 t->def.stacksize = stacksize;
robert_jw 0:b2805b6888dc 433 t->def.stack_pointer = (uint32_t*)malloc(stacksize);
robert_jw 0:b2805b6888dc 434 if (t->def.stack_pointer == NULL) {
robert_jw 0:b2805b6888dc 435 error("Error allocating the stack memory");
robert_jw 0:b2805b6888dc 436 }
robert_jw 0:b2805b6888dc 437 #endif
robert_jw 0:b2805b6888dc 438 t->id = osThreadCreate(&t->def, arg);
robert_jw 0:b2805b6888dc 439 if (t->id == NULL)
robert_jw 0:b2805b6888dc 440 error("sys_thread_new create error\n");
robert_jw 0:b2805b6888dc 441
robert_jw 0:b2805b6888dc 442 return t;
robert_jw 0:b2805b6888dc 443 }
robert_jw 0:b2805b6888dc 444
robert_jw 0:b2805b6888dc 445 #endif
robert_jw 0:b2805b6888dc 446
robert_jw 0:b2805b6888dc 447 #ifdef LWIP_DEBUG
robert_jw 0:b2805b6888dc 448
robert_jw 0:b2805b6888dc 449 /** \brief Displays an error message on assertion
robert_jw 0:b2805b6888dc 450
robert_jw 0:b2805b6888dc 451 This function will display an error message on an assertion
robert_jw 0:b2805b6888dc 452 to the debug output.
robert_jw 0:b2805b6888dc 453
robert_jw 0:b2805b6888dc 454 \param[in] msg Error message to display
robert_jw 0:b2805b6888dc 455 \param[in] line Line number in file with error
robert_jw 0:b2805b6888dc 456 \param[in] file Filename with error
robert_jw 0:b2805b6888dc 457 */
robert_jw 0:b2805b6888dc 458 void assert_printf(char *msg, int line, char *file) {
robert_jw 0:b2805b6888dc 459 if (msg)
robert_jw 0:b2805b6888dc 460 error("%s:%d in file %s\n", msg, line, file);
robert_jw 0:b2805b6888dc 461 else
robert_jw 0:b2805b6888dc 462 error("LWIP ASSERT\n");
robert_jw 0:b2805b6888dc 463 }
robert_jw 0:b2805b6888dc 464
robert_jw 0:b2805b6888dc 465 #endif /* LWIP_DEBUG */