Integrating the ublox LISA C200 modem

Fork of SprintUSBModemHTTPClientTest by Donatien Garnier

Committer:
sam_grove
Date:
Thu Sep 26 00:44:20 2013 -0500
Revision:
5:3f93dd1d4cb3
Exported program and replaced contents of the repo with the source
to build and debug using keil mdk. Libs NOT upto date are lwip, lwip-sys
and socket. these have newer versions under mbed_official but were starting
from a know working point

Who changed what in which revision?

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