Ethernetwebsoc

Dependencies:   C12832_lcd LM75B WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Committer:
GordonSin
Date:
Fri May 31 04:09:54 2013 +0000
Revision:
0:0ed2a7c7190c
31/5/2013;

Who changed what in which revision?

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