Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BSP_DISCO_F746NG_patch mbed-rtos mbed
patch/LwIP/system/OS/sys_arch.c@0:c00e6c923941, 2015-11-02 (annotated)
- Committer:
- NirT
- Date:
- Mon Nov 02 23:38:08 2015 +0000
- Revision:
- 0:c00e6c923941
Error: Incomplete type is not allowed in "patch/LwIP/src/include/lwip/dhcp.h", Line: 83, Col: 4; ; and more like this.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| NirT | 0:c00e6c923941 | 1 | /* |
| NirT | 0:c00e6c923941 | 2 | * Copyright (c) 2001-2003 Swedish Institute of Computer Science. |
| NirT | 0:c00e6c923941 | 3 | * All rights reserved. |
| NirT | 0:c00e6c923941 | 4 | * |
| NirT | 0:c00e6c923941 | 5 | * Redistribution and use in source and binary forms, with or without modification, |
| NirT | 0:c00e6c923941 | 6 | * are permitted provided that the following conditions are met: |
| NirT | 0:c00e6c923941 | 7 | * |
| NirT | 0:c00e6c923941 | 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| NirT | 0:c00e6c923941 | 9 | * this list of conditions and the following disclaimer. |
| NirT | 0:c00e6c923941 | 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| NirT | 0:c00e6c923941 | 11 | * this list of conditions and the following disclaimer in the documentation |
| NirT | 0:c00e6c923941 | 12 | * and/or other materials provided with the distribution. |
| NirT | 0:c00e6c923941 | 13 | * 3. The name of the author may not be used to endorse or promote products |
| NirT | 0:c00e6c923941 | 14 | * derived from this software without specific prior written permission. |
| NirT | 0:c00e6c923941 | 15 | * |
| NirT | 0:c00e6c923941 | 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| NirT | 0:c00e6c923941 | 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| NirT | 0:c00e6c923941 | 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| NirT | 0:c00e6c923941 | 19 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| NirT | 0:c00e6c923941 | 20 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| NirT | 0:c00e6c923941 | 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| NirT | 0:c00e6c923941 | 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| NirT | 0:c00e6c923941 | 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| NirT | 0:c00e6c923941 | 24 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
| NirT | 0:c00e6c923941 | 25 | * OF SUCH DAMAGE. |
| NirT | 0:c00e6c923941 | 26 | * |
| NirT | 0:c00e6c923941 | 27 | * This file is part of the lwIP TCP/IP stack. |
| NirT | 0:c00e6c923941 | 28 | * |
| NirT | 0:c00e6c923941 | 29 | * Author: Adam Dunkels <adam@sics.se> |
| NirT | 0:c00e6c923941 | 30 | * |
| NirT | 0:c00e6c923941 | 31 | */ |
| NirT | 0:c00e6c923941 | 32 | |
| NirT | 0:c00e6c923941 | 33 | /* lwIP includes. */ |
| NirT | 0:c00e6c923941 | 34 | #include "lwip/debug.h" |
| NirT | 0:c00e6c923941 | 35 | #include "lwip/def.h" |
| NirT | 0:c00e6c923941 | 36 | #include "lwip/sys.h" |
| NirT | 0:c00e6c923941 | 37 | #include "lwip/mem.h" |
| NirT | 0:c00e6c923941 | 38 | #include "lwip/stats.h" |
| NirT | 0:c00e6c923941 | 39 | #include "FreeRTOS.h" |
| NirT | 0:c00e6c923941 | 40 | #include "task.h" |
| NirT | 0:c00e6c923941 | 41 | |
| NirT | 0:c00e6c923941 | 42 | |
| NirT | 0:c00e6c923941 | 43 | xTaskHandle xTaskGetCurrentTaskHandle( void ) PRIVILEGED_FUNCTION; |
| NirT | 0:c00e6c923941 | 44 | |
| NirT | 0:c00e6c923941 | 45 | /* This is the number of threads that can be started with sys_thread_new() */ |
| NirT | 0:c00e6c923941 | 46 | #define SYS_THREAD_MAX 6 |
| NirT | 0:c00e6c923941 | 47 | |
| NirT | 0:c00e6c923941 | 48 | static u16_t s_nextthread = 0; |
| NirT | 0:c00e6c923941 | 49 | |
| NirT | 0:c00e6c923941 | 50 | |
| NirT | 0:c00e6c923941 | 51 | /*-----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 52 | // Creates an empty mailbox. |
| NirT | 0:c00e6c923941 | 53 | err_t sys_mbox_new(sys_mbox_t *mbox, int size) |
| NirT | 0:c00e6c923941 | 54 | { |
| NirT | 0:c00e6c923941 | 55 | (void ) size; |
| NirT | 0:c00e6c923941 | 56 | |
| NirT | 0:c00e6c923941 | 57 | *mbox = xQueueCreate( archMESG_QUEUE_LENGTH, sizeof( void * ) ); |
| NirT | 0:c00e6c923941 | 58 | |
| NirT | 0:c00e6c923941 | 59 | #if SYS_STATS |
| NirT | 0:c00e6c923941 | 60 | ++lwip_stats.sys.mbox.used; |
| NirT | 0:c00e6c923941 | 61 | if (lwip_stats.sys.mbox.max < lwip_stats.sys.mbox.used) { |
| NirT | 0:c00e6c923941 | 62 | lwip_stats.sys.mbox.max = lwip_stats.sys.mbox.used; |
| NirT | 0:c00e6c923941 | 63 | } |
| NirT | 0:c00e6c923941 | 64 | #endif /* SYS_STATS */ |
| NirT | 0:c00e6c923941 | 65 | if (*mbox == NULL) |
| NirT | 0:c00e6c923941 | 66 | return ERR_MEM; |
| NirT | 0:c00e6c923941 | 67 | |
| NirT | 0:c00e6c923941 | 68 | return ERR_OK; |
| NirT | 0:c00e6c923941 | 69 | } |
| NirT | 0:c00e6c923941 | 70 | |
| NirT | 0:c00e6c923941 | 71 | /*-----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 72 | /* |
| NirT | 0:c00e6c923941 | 73 | Deallocates a mailbox. If there are messages still present in the |
| NirT | 0:c00e6c923941 | 74 | mailbox when the mailbox is deallocated, it is an indication of a |
| NirT | 0:c00e6c923941 | 75 | programming error in lwIP and the developer should be notified. |
| NirT | 0:c00e6c923941 | 76 | */ |
| NirT | 0:c00e6c923941 | 77 | void sys_mbox_free(sys_mbox_t *mbox) |
| NirT | 0:c00e6c923941 | 78 | { |
| NirT | 0:c00e6c923941 | 79 | if( uxQueueMessagesWaiting( *mbox ) ) |
| NirT | 0:c00e6c923941 | 80 | { |
| NirT | 0:c00e6c923941 | 81 | /* Line for breakpoint. Should never break here! */ |
| NirT | 0:c00e6c923941 | 82 | portNOP(); |
| NirT | 0:c00e6c923941 | 83 | #if SYS_STATS |
| NirT | 0:c00e6c923941 | 84 | lwip_stats.sys.mbox.err++; |
| NirT | 0:c00e6c923941 | 85 | #endif /* SYS_STATS */ |
| NirT | 0:c00e6c923941 | 86 | |
| NirT | 0:c00e6c923941 | 87 | // TODO notify the user of failure. |
| NirT | 0:c00e6c923941 | 88 | } |
| NirT | 0:c00e6c923941 | 89 | |
| NirT | 0:c00e6c923941 | 90 | vQueueDelete( *mbox ); |
| NirT | 0:c00e6c923941 | 91 | |
| NirT | 0:c00e6c923941 | 92 | #if SYS_STATS |
| NirT | 0:c00e6c923941 | 93 | --lwip_stats.sys.mbox.used; |
| NirT | 0:c00e6c923941 | 94 | #endif /* SYS_STATS */ |
| NirT | 0:c00e6c923941 | 95 | } |
| NirT | 0:c00e6c923941 | 96 | |
| NirT | 0:c00e6c923941 | 97 | /*-----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 98 | // Posts the "msg" to the mailbox. |
| NirT | 0:c00e6c923941 | 99 | void sys_mbox_post(sys_mbox_t *mbox, void *data) |
| NirT | 0:c00e6c923941 | 100 | { |
| NirT | 0:c00e6c923941 | 101 | while ( xQueueSendToBack(*mbox, &data, portMAX_DELAY ) != pdTRUE ){} |
| NirT | 0:c00e6c923941 | 102 | } |
| NirT | 0:c00e6c923941 | 103 | |
| NirT | 0:c00e6c923941 | 104 | |
| NirT | 0:c00e6c923941 | 105 | /*-----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 106 | // Try to post the "msg" to the mailbox. |
| NirT | 0:c00e6c923941 | 107 | err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg) |
| NirT | 0:c00e6c923941 | 108 | { |
| NirT | 0:c00e6c923941 | 109 | err_t result; |
| NirT | 0:c00e6c923941 | 110 | |
| NirT | 0:c00e6c923941 | 111 | if ( xQueueSend( *mbox, &msg, 0 ) == pdPASS ) |
| NirT | 0:c00e6c923941 | 112 | { |
| NirT | 0:c00e6c923941 | 113 | result = ERR_OK; |
| NirT | 0:c00e6c923941 | 114 | } |
| NirT | 0:c00e6c923941 | 115 | else { |
| NirT | 0:c00e6c923941 | 116 | // could not post, queue must be full |
| NirT | 0:c00e6c923941 | 117 | result = ERR_MEM; |
| NirT | 0:c00e6c923941 | 118 | |
| NirT | 0:c00e6c923941 | 119 | #if SYS_STATS |
| NirT | 0:c00e6c923941 | 120 | lwip_stats.sys.mbox.err++; |
| NirT | 0:c00e6c923941 | 121 | #endif /* SYS_STATS */ |
| NirT | 0:c00e6c923941 | 122 | |
| NirT | 0:c00e6c923941 | 123 | } |
| NirT | 0:c00e6c923941 | 124 | |
| NirT | 0:c00e6c923941 | 125 | return result; |
| NirT | 0:c00e6c923941 | 126 | } |
| NirT | 0:c00e6c923941 | 127 | |
| NirT | 0:c00e6c923941 | 128 | /*-----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 129 | /* |
| NirT | 0:c00e6c923941 | 130 | Blocks the thread until a message arrives in the mailbox, but does |
| NirT | 0:c00e6c923941 | 131 | not block the thread longer than "timeout" milliseconds (similar to |
| NirT | 0:c00e6c923941 | 132 | the sys_arch_sem_wait() function). The "msg" argument is a result |
| NirT | 0:c00e6c923941 | 133 | parameter that is set by the function (i.e., by doing "*msg = |
| NirT | 0:c00e6c923941 | 134 | ptr"). The "msg" parameter maybe NULL to indicate that the message |
| NirT | 0:c00e6c923941 | 135 | should be dropped. |
| NirT | 0:c00e6c923941 | 136 | |
| NirT | 0:c00e6c923941 | 137 | The return values are the same as for the sys_arch_sem_wait() function: |
| NirT | 0:c00e6c923941 | 138 | Number of milliseconds spent waiting or SYS_ARCH_TIMEOUT if there was a |
| NirT | 0:c00e6c923941 | 139 | timeout. |
| NirT | 0:c00e6c923941 | 140 | |
| NirT | 0:c00e6c923941 | 141 | Note that a function with a similar name, sys_mbox_fetch(), is |
| NirT | 0:c00e6c923941 | 142 | implemented by lwIP. |
| NirT | 0:c00e6c923941 | 143 | */ |
| NirT | 0:c00e6c923941 | 144 | u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout) |
| NirT | 0:c00e6c923941 | 145 | { |
| NirT | 0:c00e6c923941 | 146 | void *dummyptr; |
| NirT | 0:c00e6c923941 | 147 | portTickType StartTime, EndTime, Elapsed; |
| NirT | 0:c00e6c923941 | 148 | |
| NirT | 0:c00e6c923941 | 149 | StartTime = xTaskGetTickCount(); |
| NirT | 0:c00e6c923941 | 150 | |
| NirT | 0:c00e6c923941 | 151 | if ( msg == NULL ) |
| NirT | 0:c00e6c923941 | 152 | { |
| NirT | 0:c00e6c923941 | 153 | msg = &dummyptr; |
| NirT | 0:c00e6c923941 | 154 | } |
| NirT | 0:c00e6c923941 | 155 | |
| NirT | 0:c00e6c923941 | 156 | if ( timeout != 0 ) |
| NirT | 0:c00e6c923941 | 157 | { |
| NirT | 0:c00e6c923941 | 158 | if ( pdTRUE == xQueueReceive( *mbox, &(*msg), timeout / portTICK_RATE_MS ) ) |
| NirT | 0:c00e6c923941 | 159 | { |
| NirT | 0:c00e6c923941 | 160 | EndTime = xTaskGetTickCount(); |
| NirT | 0:c00e6c923941 | 161 | Elapsed = (EndTime - StartTime) * portTICK_RATE_MS; |
| NirT | 0:c00e6c923941 | 162 | |
| NirT | 0:c00e6c923941 | 163 | return ( Elapsed ); |
| NirT | 0:c00e6c923941 | 164 | } |
| NirT | 0:c00e6c923941 | 165 | else // timed out blocking for message |
| NirT | 0:c00e6c923941 | 166 | { |
| NirT | 0:c00e6c923941 | 167 | *msg = NULL; |
| NirT | 0:c00e6c923941 | 168 | |
| NirT | 0:c00e6c923941 | 169 | return SYS_ARCH_TIMEOUT; |
| NirT | 0:c00e6c923941 | 170 | } |
| NirT | 0:c00e6c923941 | 171 | } |
| NirT | 0:c00e6c923941 | 172 | else // block forever for a message. |
| NirT | 0:c00e6c923941 | 173 | { |
| NirT | 0:c00e6c923941 | 174 | while( pdTRUE != xQueueReceive( *mbox, &(*msg), portMAX_DELAY ) ){} // time is arbitrary |
| NirT | 0:c00e6c923941 | 175 | EndTime = xTaskGetTickCount(); |
| NirT | 0:c00e6c923941 | 176 | Elapsed = (EndTime - StartTime) * portTICK_RATE_MS; |
| NirT | 0:c00e6c923941 | 177 | |
| NirT | 0:c00e6c923941 | 178 | return ( Elapsed ); // return time blocked TODO test |
| NirT | 0:c00e6c923941 | 179 | } |
| NirT | 0:c00e6c923941 | 180 | } |
| NirT | 0:c00e6c923941 | 181 | |
| NirT | 0:c00e6c923941 | 182 | /*-----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 183 | /* |
| NirT | 0:c00e6c923941 | 184 | Similar to sys_arch_mbox_fetch, but if message is not ready immediately, we'll |
| NirT | 0:c00e6c923941 | 185 | return with SYS_MBOX_EMPTY. On success, 0 is returned. |
| NirT | 0:c00e6c923941 | 186 | */ |
| NirT | 0:c00e6c923941 | 187 | u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg) |
| NirT | 0:c00e6c923941 | 188 | { |
| NirT | 0:c00e6c923941 | 189 | void *dummyptr; |
| NirT | 0:c00e6c923941 | 190 | |
| NirT | 0:c00e6c923941 | 191 | if ( msg == NULL ) |
| NirT | 0:c00e6c923941 | 192 | { |
| NirT | 0:c00e6c923941 | 193 | msg = &dummyptr; |
| NirT | 0:c00e6c923941 | 194 | } |
| NirT | 0:c00e6c923941 | 195 | |
| NirT | 0:c00e6c923941 | 196 | if ( pdTRUE == xQueueReceive( *mbox, &(*msg), 0 ) ) |
| NirT | 0:c00e6c923941 | 197 | { |
| NirT | 0:c00e6c923941 | 198 | return ERR_OK; |
| NirT | 0:c00e6c923941 | 199 | } |
| NirT | 0:c00e6c923941 | 200 | else |
| NirT | 0:c00e6c923941 | 201 | { |
| NirT | 0:c00e6c923941 | 202 | return SYS_MBOX_EMPTY; |
| NirT | 0:c00e6c923941 | 203 | } |
| NirT | 0:c00e6c923941 | 204 | } |
| NirT | 0:c00e6c923941 | 205 | /*----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 206 | int sys_mbox_valid(sys_mbox_t *mbox) |
| NirT | 0:c00e6c923941 | 207 | { |
| NirT | 0:c00e6c923941 | 208 | if (*mbox == SYS_MBOX_NULL) |
| NirT | 0:c00e6c923941 | 209 | return 0; |
| NirT | 0:c00e6c923941 | 210 | else |
| NirT | 0:c00e6c923941 | 211 | return 1; |
| NirT | 0:c00e6c923941 | 212 | } |
| NirT | 0:c00e6c923941 | 213 | /*-----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 214 | void sys_mbox_set_invalid(sys_mbox_t *mbox) |
| NirT | 0:c00e6c923941 | 215 | { |
| NirT | 0:c00e6c923941 | 216 | *mbox = SYS_MBOX_NULL; |
| NirT | 0:c00e6c923941 | 217 | } |
| NirT | 0:c00e6c923941 | 218 | |
| NirT | 0:c00e6c923941 | 219 | /*-----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 220 | // Creates a new semaphore. The "count" argument specifies |
| NirT | 0:c00e6c923941 | 221 | // the initial state of the semaphore. |
| NirT | 0:c00e6c923941 | 222 | err_t sys_sem_new(sys_sem_t *sem, u8_t count) |
| NirT | 0:c00e6c923941 | 223 | { |
| NirT | 0:c00e6c923941 | 224 | vSemaphoreCreateBinary(*sem ); |
| NirT | 0:c00e6c923941 | 225 | if(*sem == NULL) |
| NirT | 0:c00e6c923941 | 226 | { |
| NirT | 0:c00e6c923941 | 227 | #if SYS_STATS |
| NirT | 0:c00e6c923941 | 228 | ++lwip_stats.sys.sem.err; |
| NirT | 0:c00e6c923941 | 229 | #endif /* SYS_STATS */ |
| NirT | 0:c00e6c923941 | 230 | return ERR_MEM; |
| NirT | 0:c00e6c923941 | 231 | } |
| NirT | 0:c00e6c923941 | 232 | |
| NirT | 0:c00e6c923941 | 233 | if(count == 0) // Means it can't be taken |
| NirT | 0:c00e6c923941 | 234 | { |
| NirT | 0:c00e6c923941 | 235 | xSemaphoreTake(*sem,1); |
| NirT | 0:c00e6c923941 | 236 | } |
| NirT | 0:c00e6c923941 | 237 | |
| NirT | 0:c00e6c923941 | 238 | #if SYS_STATS |
| NirT | 0:c00e6c923941 | 239 | ++lwip_stats.sys.sem.used; |
| NirT | 0:c00e6c923941 | 240 | if (lwip_stats.sys.sem.max < lwip_stats.sys.sem.used) { |
| NirT | 0:c00e6c923941 | 241 | lwip_stats.sys.sem.max = lwip_stats.sys.sem.used; |
| NirT | 0:c00e6c923941 | 242 | } |
| NirT | 0:c00e6c923941 | 243 | #endif /* SYS_STATS */ |
| NirT | 0:c00e6c923941 | 244 | |
| NirT | 0:c00e6c923941 | 245 | return ERR_OK; |
| NirT | 0:c00e6c923941 | 246 | } |
| NirT | 0:c00e6c923941 | 247 | |
| NirT | 0:c00e6c923941 | 248 | /*-----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 249 | /* |
| NirT | 0:c00e6c923941 | 250 | Blocks the thread while waiting for the semaphore to be |
| NirT | 0:c00e6c923941 | 251 | signaled. If the "timeout" argument is non-zero, the thread should |
| NirT | 0:c00e6c923941 | 252 | only be blocked for the specified time (measured in |
| NirT | 0:c00e6c923941 | 253 | milliseconds). |
| NirT | 0:c00e6c923941 | 254 | |
| NirT | 0:c00e6c923941 | 255 | If the timeout argument is non-zero, the return value is the number of |
| NirT | 0:c00e6c923941 | 256 | milliseconds spent waiting for the semaphore to be signaled. If the |
| NirT | 0:c00e6c923941 | 257 | semaphore wasn't signaled within the specified time, the return value is |
| NirT | 0:c00e6c923941 | 258 | SYS_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore |
| NirT | 0:c00e6c923941 | 259 | (i.e., it was already signaled), the function may return zero. |
| NirT | 0:c00e6c923941 | 260 | |
| NirT | 0:c00e6c923941 | 261 | Notice that lwIP implements a function with a similar name, |
| NirT | 0:c00e6c923941 | 262 | sys_sem_wait(), that uses the sys_arch_sem_wait() function. |
| NirT | 0:c00e6c923941 | 263 | */ |
| NirT | 0:c00e6c923941 | 264 | u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout) |
| NirT | 0:c00e6c923941 | 265 | { |
| NirT | 0:c00e6c923941 | 266 | portTickType StartTime, EndTime, Elapsed; |
| NirT | 0:c00e6c923941 | 267 | |
| NirT | 0:c00e6c923941 | 268 | StartTime = xTaskGetTickCount(); |
| NirT | 0:c00e6c923941 | 269 | |
| NirT | 0:c00e6c923941 | 270 | if( timeout != 0) |
| NirT | 0:c00e6c923941 | 271 | { |
| NirT | 0:c00e6c923941 | 272 | if( xSemaphoreTake( *sem, timeout / portTICK_RATE_MS ) == pdTRUE ) |
| NirT | 0:c00e6c923941 | 273 | { |
| NirT | 0:c00e6c923941 | 274 | EndTime = xTaskGetTickCount(); |
| NirT | 0:c00e6c923941 | 275 | Elapsed = (EndTime - StartTime) * portTICK_RATE_MS; |
| NirT | 0:c00e6c923941 | 276 | |
| NirT | 0:c00e6c923941 | 277 | return (Elapsed); // return time blocked TODO test |
| NirT | 0:c00e6c923941 | 278 | } |
| NirT | 0:c00e6c923941 | 279 | else |
| NirT | 0:c00e6c923941 | 280 | { |
| NirT | 0:c00e6c923941 | 281 | return SYS_ARCH_TIMEOUT; |
| NirT | 0:c00e6c923941 | 282 | } |
| NirT | 0:c00e6c923941 | 283 | } |
| NirT | 0:c00e6c923941 | 284 | else // must block without a timeout |
| NirT | 0:c00e6c923941 | 285 | { |
| NirT | 0:c00e6c923941 | 286 | while( xSemaphoreTake(*sem, portMAX_DELAY) != pdTRUE){} |
| NirT | 0:c00e6c923941 | 287 | EndTime = xTaskGetTickCount(); |
| NirT | 0:c00e6c923941 | 288 | Elapsed = (EndTime - StartTime) * portTICK_RATE_MS; |
| NirT | 0:c00e6c923941 | 289 | |
| NirT | 0:c00e6c923941 | 290 | return ( Elapsed ); // return time blocked |
| NirT | 0:c00e6c923941 | 291 | |
| NirT | 0:c00e6c923941 | 292 | } |
| NirT | 0:c00e6c923941 | 293 | } |
| NirT | 0:c00e6c923941 | 294 | |
| NirT | 0:c00e6c923941 | 295 | /*-----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 296 | // Signals a semaphore |
| NirT | 0:c00e6c923941 | 297 | void sys_sem_signal(sys_sem_t *sem) |
| NirT | 0:c00e6c923941 | 298 | { |
| NirT | 0:c00e6c923941 | 299 | xSemaphoreGive(*sem); |
| NirT | 0:c00e6c923941 | 300 | } |
| NirT | 0:c00e6c923941 | 301 | |
| NirT | 0:c00e6c923941 | 302 | /*-----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 303 | // Deallocates a semaphore |
| NirT | 0:c00e6c923941 | 304 | void sys_sem_free(sys_sem_t *sem) |
| NirT | 0:c00e6c923941 | 305 | { |
| NirT | 0:c00e6c923941 | 306 | #if SYS_STATS |
| NirT | 0:c00e6c923941 | 307 | --lwip_stats.sys.sem.used; |
| NirT | 0:c00e6c923941 | 308 | #endif /* SYS_STATS */ |
| NirT | 0:c00e6c923941 | 309 | |
| NirT | 0:c00e6c923941 | 310 | vQueueDelete(*sem); |
| NirT | 0:c00e6c923941 | 311 | } |
| NirT | 0:c00e6c923941 | 312 | /*-----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 313 | int sys_sem_valid(sys_sem_t *sem) |
| NirT | 0:c00e6c923941 | 314 | { |
| NirT | 0:c00e6c923941 | 315 | if (*sem == SYS_SEM_NULL) |
| NirT | 0:c00e6c923941 | 316 | return 0; |
| NirT | 0:c00e6c923941 | 317 | else |
| NirT | 0:c00e6c923941 | 318 | return 1; |
| NirT | 0:c00e6c923941 | 319 | } |
| NirT | 0:c00e6c923941 | 320 | |
| NirT | 0:c00e6c923941 | 321 | /*-----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 322 | void sys_sem_set_invalid(sys_sem_t *sem) |
| NirT | 0:c00e6c923941 | 323 | { |
| NirT | 0:c00e6c923941 | 324 | *sem = SYS_SEM_NULL; |
| NirT | 0:c00e6c923941 | 325 | } |
| NirT | 0:c00e6c923941 | 326 | |
| NirT | 0:c00e6c923941 | 327 | /*-----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 328 | // Initialize sys arch |
| NirT | 0:c00e6c923941 | 329 | void sys_init(void) |
| NirT | 0:c00e6c923941 | 330 | { |
| NirT | 0:c00e6c923941 | 331 | // keep track of how many threads have been created |
| NirT | 0:c00e6c923941 | 332 | s_nextthread = 0; |
| NirT | 0:c00e6c923941 | 333 | } |
| NirT | 0:c00e6c923941 | 334 | /*-----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 335 | /* Mutexes*/ |
| NirT | 0:c00e6c923941 | 336 | /*-----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 337 | /*-----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 338 | #if LWIP_COMPAT_MUTEX == 0 |
| NirT | 0:c00e6c923941 | 339 | /* Create a new mutex*/ |
| NirT | 0:c00e6c923941 | 340 | err_t sys_mutex_new(sys_mutex_t *mutex) { |
| NirT | 0:c00e6c923941 | 341 | |
| NirT | 0:c00e6c923941 | 342 | *mutex = xSemaphoreCreateMutex(); |
| NirT | 0:c00e6c923941 | 343 | if(*mutex == NULL) |
| NirT | 0:c00e6c923941 | 344 | { |
| NirT | 0:c00e6c923941 | 345 | #if SYS_STATS |
| NirT | 0:c00e6c923941 | 346 | ++lwip_stats.sys.mutex.err; |
| NirT | 0:c00e6c923941 | 347 | #endif /* SYS_STATS */ |
| NirT | 0:c00e6c923941 | 348 | return ERR_MEM; |
| NirT | 0:c00e6c923941 | 349 | } |
| NirT | 0:c00e6c923941 | 350 | |
| NirT | 0:c00e6c923941 | 351 | #if SYS_STATS |
| NirT | 0:c00e6c923941 | 352 | ++lwip_stats.sys.mutex.used; |
| NirT | 0:c00e6c923941 | 353 | if (lwip_stats.sys.mutex.max < lwip_stats.sys.mutex.used) { |
| NirT | 0:c00e6c923941 | 354 | lwip_stats.sys.mutex.max = lwip_stats.sys.mutex.used; |
| NirT | 0:c00e6c923941 | 355 | } |
| NirT | 0:c00e6c923941 | 356 | #endif /* SYS_STATS */ |
| NirT | 0:c00e6c923941 | 357 | return ERR_OK; |
| NirT | 0:c00e6c923941 | 358 | } |
| NirT | 0:c00e6c923941 | 359 | /*-----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 360 | /* Deallocate a mutex*/ |
| NirT | 0:c00e6c923941 | 361 | void sys_mutex_free(sys_mutex_t *mutex) |
| NirT | 0:c00e6c923941 | 362 | { |
| NirT | 0:c00e6c923941 | 363 | #if SYS_STATS |
| NirT | 0:c00e6c923941 | 364 | --lwip_stats.sys.mutex.used; |
| NirT | 0:c00e6c923941 | 365 | #endif /* SYS_STATS */ |
| NirT | 0:c00e6c923941 | 366 | |
| NirT | 0:c00e6c923941 | 367 | vQueueDelete(*mutex); |
| NirT | 0:c00e6c923941 | 368 | } |
| NirT | 0:c00e6c923941 | 369 | /*-----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 370 | /* Lock a mutex*/ |
| NirT | 0:c00e6c923941 | 371 | void sys_mutex_lock(sys_mutex_t *mutex) |
| NirT | 0:c00e6c923941 | 372 | { |
| NirT | 0:c00e6c923941 | 373 | sys_arch_sem_wait(mutex, 0); |
| NirT | 0:c00e6c923941 | 374 | } |
| NirT | 0:c00e6c923941 | 375 | |
| NirT | 0:c00e6c923941 | 376 | /*-----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 377 | /* Unlock a mutex*/ |
| NirT | 0:c00e6c923941 | 378 | void sys_mutex_unlock(sys_mutex_t *mutex) |
| NirT | 0:c00e6c923941 | 379 | { |
| NirT | 0:c00e6c923941 | 380 | xSemaphoreGive(*mutex); |
| NirT | 0:c00e6c923941 | 381 | } |
| NirT | 0:c00e6c923941 | 382 | #endif /*LWIP_COMPAT_MUTEX*/ |
| NirT | 0:c00e6c923941 | 383 | /*-----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 384 | // TODO |
| NirT | 0:c00e6c923941 | 385 | /*-----------------------------------------------------------------------------------*/ |
| NirT | 0:c00e6c923941 | 386 | /* |
| NirT | 0:c00e6c923941 | 387 | Starts a new thread with priority "prio" that will begin its execution in the |
| NirT | 0:c00e6c923941 | 388 | function "thread()". The "arg" argument will be passed as an argument to the |
| NirT | 0:c00e6c923941 | 389 | thread() function. The id of the new thread is returned. Both the id and |
| NirT | 0:c00e6c923941 | 390 | the priority are system dependent. |
| NirT | 0:c00e6c923941 | 391 | */ |
| NirT | 0:c00e6c923941 | 392 | sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread , void *arg, int stacksize, int prio) |
| NirT | 0:c00e6c923941 | 393 | { |
| NirT | 0:c00e6c923941 | 394 | xTaskHandle CreatedTask; |
| NirT | 0:c00e6c923941 | 395 | int result; |
| NirT | 0:c00e6c923941 | 396 | |
| NirT | 0:c00e6c923941 | 397 | if ( s_nextthread < SYS_THREAD_MAX ) |
| NirT | 0:c00e6c923941 | 398 | { |
| NirT | 0:c00e6c923941 | 399 | result = xTaskCreate( thread, ( signed portCHAR * ) name, stacksize, arg, prio, &CreatedTask ); |
| NirT | 0:c00e6c923941 | 400 | |
| NirT | 0:c00e6c923941 | 401 | // For each task created, store the task handle (pid) in the timers array. |
| NirT | 0:c00e6c923941 | 402 | // This scheme doesn't allow for threads to be deleted |
| NirT | 0:c00e6c923941 | 403 | //s_timeoutlist[s_nextthread++].pid = CreatedTask; |
| NirT | 0:c00e6c923941 | 404 | |
| NirT | 0:c00e6c923941 | 405 | if(result == pdPASS) |
| NirT | 0:c00e6c923941 | 406 | { |
| NirT | 0:c00e6c923941 | 407 | return CreatedTask; |
| NirT | 0:c00e6c923941 | 408 | } |
| NirT | 0:c00e6c923941 | 409 | else |
| NirT | 0:c00e6c923941 | 410 | { |
| NirT | 0:c00e6c923941 | 411 | return NULL; |
| NirT | 0:c00e6c923941 | 412 | } |
| NirT | 0:c00e6c923941 | 413 | } |
| NirT | 0:c00e6c923941 | 414 | else |
| NirT | 0:c00e6c923941 | 415 | { |
| NirT | 0:c00e6c923941 | 416 | return NULL; |
| NirT | 0:c00e6c923941 | 417 | } |
| NirT | 0:c00e6c923941 | 418 | } |
| NirT | 0:c00e6c923941 | 419 | |
| NirT | 0:c00e6c923941 | 420 | /* |
| NirT | 0:c00e6c923941 | 421 | This optional function does a "fast" critical region protection and returns |
| NirT | 0:c00e6c923941 | 422 | the previous protection level. This function is only called during very short |
| NirT | 0:c00e6c923941 | 423 | critical regions. An embedded system which supports ISR-based drivers might |
| NirT | 0:c00e6c923941 | 424 | want to implement this function by disabling interrupts. Task-based systems |
| NirT | 0:c00e6c923941 | 425 | might want to implement this by using a mutex or disabling tasking. This |
| NirT | 0:c00e6c923941 | 426 | function should support recursive calls from the same task or interrupt. In |
| NirT | 0:c00e6c923941 | 427 | other words, sys_arch_protect() could be called while already protected. In |
| NirT | 0:c00e6c923941 | 428 | that case the return value indicates that it is already protected. |
| NirT | 0:c00e6c923941 | 429 | |
| NirT | 0:c00e6c923941 | 430 | sys_arch_protect() is only required if your port is supporting an operating |
| NirT | 0:c00e6c923941 | 431 | system. |
| NirT | 0:c00e6c923941 | 432 | */ |
| NirT | 0:c00e6c923941 | 433 | sys_prot_t sys_arch_protect(void) |
| NirT | 0:c00e6c923941 | 434 | { |
| NirT | 0:c00e6c923941 | 435 | vPortEnterCritical(); |
| NirT | 0:c00e6c923941 | 436 | return 1; |
| NirT | 0:c00e6c923941 | 437 | } |
| NirT | 0:c00e6c923941 | 438 | |
| NirT | 0:c00e6c923941 | 439 | /* |
| NirT | 0:c00e6c923941 | 440 | This optional function does a "fast" set of critical region protection to the |
| NirT | 0:c00e6c923941 | 441 | value specified by pval. See the documentation for sys_arch_protect() for |
| NirT | 0:c00e6c923941 | 442 | more information. This function is only required if your port is supporting |
| NirT | 0:c00e6c923941 | 443 | an operating system. |
| NirT | 0:c00e6c923941 | 444 | */ |
| NirT | 0:c00e6c923941 | 445 | void sys_arch_unprotect(sys_prot_t pval) |
| NirT | 0:c00e6c923941 | 446 | { |
| NirT | 0:c00e6c923941 | 447 | ( void ) pval; |
| NirT | 0:c00e6c923941 | 448 | vPortExitCritical(); |
| NirT | 0:c00e6c923941 | 449 | } |
| NirT | 0:c00e6c923941 | 450 | |
| NirT | 0:c00e6c923941 | 451 | /* |
| NirT | 0:c00e6c923941 | 452 | * Prints an assertion messages and aborts execution. |
| NirT | 0:c00e6c923941 | 453 | */ |
| NirT | 0:c00e6c923941 | 454 | void sys_assert( const char *msg ) |
| NirT | 0:c00e6c923941 | 455 | { |
| NirT | 0:c00e6c923941 | 456 | ( void ) msg; |
| NirT | 0:c00e6c923941 | 457 | /*FSL:only needed for debugging |
| NirT | 0:c00e6c923941 | 458 | printf(msg); |
| NirT | 0:c00e6c923941 | 459 | printf("\n\r"); |
| NirT | 0:c00e6c923941 | 460 | */ |
| NirT | 0:c00e6c923941 | 461 | vPortEnterCritical( ); |
| NirT | 0:c00e6c923941 | 462 | for(;;) |
| NirT | 0:c00e6c923941 | 463 | ; |
| NirT | 0:c00e6c923941 | 464 | } |