Ethernet test for ECE 4180 and others to find your IP address and do a simple HTTP GET request over port 80.

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Committer:
mkersh3
Date:
Thu Apr 04 05:26:09 2013 +0000
Revision:
0:e7ca326e76ee
Ethernet Test for ECE4180 and others to find their IP Address and do a simple HTTP GET request over port 80.

Who changed what in which revision?

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