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