Webserver+3d print
Embed:
(wiki syntax)
Show/hide line numbers
os_port_windows.c
Go to the documentation of this file.
00001 /** 00002 * @file os_port_windows.c 00003 * @brief RTOS abstraction layer (Windows) 00004 * 00005 * @section License 00006 * 00007 * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved. 00008 * 00009 * This program is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU General Public License 00011 * as published by the Free Software Foundation; either version 2 00012 * of the License, or (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software Foundation, 00021 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00022 * 00023 * @author Oryx Embedded SARL (www.oryx-embedded.com) 00024 * @version 1.7.6 00025 **/ 00026 00027 //Switch to the appropriate trace level 00028 #define TRACE_LEVEL TRACE_LEVEL_OFF 00029 00030 //Memory leaks detection 00031 #if (defined(_WIN32) && defined(_DEBUG)) 00032 #define _CRTDBG_MAP_ALLOC 00033 #include <stdlib.h> 00034 #include <crtdbg.h> 00035 #endif 00036 00037 //Dependencies 00038 #include <stdio.h> 00039 #include <stdlib.h> 00040 #include <windows.h> 00041 #include "os_port.h" 00042 #include "os_port_windows.h" 00043 #include "debug.h" 00044 00045 00046 /** 00047 * @brief Kernel initialization 00048 **/ 00049 00050 void osInitKernel(void) 00051 { 00052 //Not implemented 00053 } 00054 00055 00056 /** 00057 * @brief Start kernel 00058 **/ 00059 00060 void osStartKernel(void) 00061 { 00062 //Not implemented 00063 } 00064 00065 00066 /** 00067 * @brief Create a new task 00068 * @param[in] name A name identifying the task 00069 * @param[in] taskCode Pointer to the task entry function 00070 * @param[in] params A pointer to a variable to be passed to the task 00071 * @param[in] stackSize The initial size of the stack, in words 00072 * @param[in] priority The priority at which the task should run 00073 * @return If the function succeeds, the return value is a pointer to the 00074 * new task. If the function fails, the return value is NULL 00075 **/ 00076 00077 OsTask *osCreateTask(const char_t *name, OsTaskCode taskCode, 00078 void *params, size_t stackSize, int_t priority) 00079 { 00080 void *handle; 00081 00082 //Create a new thread 00083 handle = CreateThread(NULL, 0, 00084 (LPTHREAD_START_ROUTINE) taskCode, params, 0, NULL); 00085 00086 //Return a pointer to the newly created thread 00087 return handle; 00088 } 00089 00090 00091 /** 00092 * @brief Delete a task 00093 * @param[in] task Pointer to the task to be deleted 00094 **/ 00095 00096 void osDeleteTask(OsTask *task) 00097 { 00098 //Delete the calling thread? 00099 if(task == NULL) 00100 { 00101 //Kill ourselves 00102 ExitThread(0); 00103 } 00104 else 00105 { 00106 //Delete the specified thread 00107 TerminateThread(task, 0); 00108 } 00109 } 00110 00111 00112 /** 00113 * @brief Delay routine 00114 * @param[in] delay Amount of time for which the calling task should block 00115 **/ 00116 00117 void osDelayTask(systime_t delay) 00118 { 00119 //Delay the task for the specified duration 00120 Sleep(delay); 00121 } 00122 00123 00124 /** 00125 * @brief Yield control to the next task 00126 **/ 00127 00128 void osSwitchTask(void) 00129 { 00130 //Not implemented 00131 } 00132 00133 00134 /** 00135 * @brief Suspend scheduler activity 00136 **/ 00137 00138 void osSuspendAllTasks(void) 00139 { 00140 //Not implemented 00141 } 00142 00143 00144 /** 00145 * @brief Resume scheduler activity 00146 **/ 00147 00148 void osResumeAllTasks(void) 00149 { 00150 //Not implemented 00151 } 00152 00153 00154 /** 00155 * @brief Create an event object 00156 * @param[in] event Pointer to the event object 00157 * @return The function returns TRUE if the event object was successfully 00158 * created. Otherwise, FALSE is returned 00159 **/ 00160 00161 bool_t osCreateEvent(OsEvent *event) 00162 { 00163 //Create an event object 00164 event->handle = CreateEvent(NULL, FALSE, FALSE, NULL); 00165 00166 //Check whether the returned handle is valid 00167 if(event->handle != NULL) 00168 return TRUE; 00169 else 00170 return FALSE; 00171 } 00172 00173 00174 /** 00175 * @brief Delete an event object 00176 * @param[in] event Pointer to the event object 00177 **/ 00178 00179 void osDeleteEvent(OsEvent *event) 00180 { 00181 //Make sure the handle is valid 00182 if(event->handle != NULL) 00183 { 00184 //Properly dispose the event object 00185 CloseHandle(event->handle); 00186 } 00187 } 00188 00189 00190 /** 00191 * @brief Set the specified event object to the signaled state 00192 * @param[in] event Pointer to the event object 00193 **/ 00194 00195 void osSetEvent(OsEvent *event) 00196 { 00197 //Set the specified event to the signaled state 00198 SetEvent(event->handle); 00199 } 00200 00201 00202 /** 00203 * @brief Set the specified event object to the nonsignaled state 00204 * @param[in] event Pointer to the event object 00205 **/ 00206 00207 void osResetEvent(OsEvent *event) 00208 { 00209 //Force the specified event to the nonsignaled state 00210 ResetEvent(event->handle); 00211 } 00212 00213 00214 /** 00215 * @brief Wait until the specified event is in the signaled state 00216 * @param[in] event Pointer to the event object 00217 * @param[in] timeout Timeout interval 00218 * @return The function returns TRUE if the state of the specified object is 00219 * signaled. FALSE is returned if the timeout interval elapsed 00220 **/ 00221 00222 bool_t osWaitForEvent(OsEvent *event, systime_t timeout) 00223 { 00224 //Wait until the specified event is in the signaled state 00225 if(WaitForSingleObject(event->handle, timeout) == WAIT_OBJECT_0) 00226 return TRUE; 00227 else 00228 return FALSE; 00229 } 00230 00231 00232 /** 00233 * @brief Set an event object to the signaled state from an interrupt service routine 00234 * @param[in] event Pointer to the event object 00235 * @return TRUE if setting the event to signaled state caused a task to unblock 00236 * and the unblocked task has a priority higher than the currently running task 00237 **/ 00238 00239 bool_t osSetEventFromIsr(OsEvent *event) 00240 { 00241 //Not implemented 00242 return FALSE; 00243 } 00244 00245 00246 /** 00247 * @brief Create a semaphore object 00248 * @param[in] semaphore Pointer to the semaphore object 00249 * @param[in] count The maximum count for the semaphore object. This value 00250 * must be greater than zero 00251 * @return The function returns TRUE if the semaphore was successfully 00252 * created. Otherwise, FALSE is returned 00253 **/ 00254 00255 bool_t osCreateSemaphore(OsSemaphore *semaphore, uint_t count) 00256 { 00257 //Create a semaphore object 00258 semaphore->handle = CreateSemaphore(NULL, count, count, NULL); 00259 00260 //Check whether the returned handle is valid 00261 if(semaphore->handle != NULL) 00262 return TRUE; 00263 else 00264 return FALSE; 00265 } 00266 00267 00268 /** 00269 * @brief Delete a semaphore object 00270 * @param[in] semaphore Pointer to the semaphore object 00271 **/ 00272 00273 void osDeleteSemaphore(OsSemaphore *semaphore) 00274 { 00275 //Make sure the handle is valid 00276 if(semaphore->handle != NULL) 00277 { 00278 //Properly dispose the semaphore object 00279 CloseHandle(semaphore->handle); 00280 } 00281 } 00282 00283 00284 /** 00285 * @brief Wait for the specified semaphore to be available 00286 * @param[in] semaphore Pointer to the semaphore object 00287 * @param[in] timeout Timeout interval 00288 * @return The function returns TRUE if the semaphore is available. FALSE is 00289 * returned if the timeout interval elapsed 00290 **/ 00291 00292 bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout) 00293 { 00294 //Wait until the specified semaphore becomes available 00295 if(WaitForSingleObject(semaphore->handle, timeout) == WAIT_OBJECT_0) 00296 return TRUE; 00297 else 00298 return FALSE; 00299 } 00300 00301 00302 /** 00303 * @brief Release the specified semaphore object 00304 * @param[in] semaphore Pointer to the semaphore object 00305 **/ 00306 00307 void osReleaseSemaphore(OsSemaphore *semaphore) 00308 { 00309 //Release the semaphore 00310 ReleaseSemaphore(semaphore->handle, 1, NULL); 00311 } 00312 00313 00314 /** 00315 * @brief Create a mutex object 00316 * @param[in] mutex Pointer to the mutex object 00317 * @return The function returns TRUE if the mutex was successfully 00318 * created. Otherwise, FALSE is returned 00319 **/ 00320 00321 bool_t osCreateMutex(OsMutex *mutex) 00322 { 00323 //Create a mutex object 00324 mutex->handle = CreateMutex(NULL, FALSE, NULL); 00325 00326 //Check whether the returned handle is valid 00327 if(mutex->handle != NULL) 00328 return TRUE; 00329 else 00330 return FALSE; 00331 } 00332 00333 00334 /** 00335 * @brief Delete a mutex object 00336 * @param[in] mutex Pointer to the mutex object 00337 **/ 00338 00339 void osDeleteMutex(OsMutex *mutex) 00340 { 00341 //Make sure the handle is valid 00342 if(mutex->handle != NULL) 00343 { 00344 //Properly dispose the mutex object 00345 CloseHandle(mutex->handle); 00346 } 00347 } 00348 00349 00350 /** 00351 * @brief Acquire ownership of the specified mutex object 00352 * @param[in] mutex A handle to the mutex object 00353 **/ 00354 00355 void osAcquireMutex(OsMutex *mutex) 00356 { 00357 //Obtain ownership of the mutex object 00358 WaitForSingleObject(mutex->handle, INFINITE); 00359 } 00360 00361 00362 /** 00363 * @brief Release ownership of the specified mutex object 00364 * @param[in] mutex Pointer to the mutex object 00365 **/ 00366 00367 void osReleaseMutex(OsMutex *mutex) 00368 { 00369 //Release ownership of the mutex object 00370 ReleaseMutex(mutex->handle); 00371 } 00372 00373 00374 /** 00375 * @brief Retrieve system time 00376 * @return Number of milliseconds elapsed since the system was last started 00377 **/ 00378 00379 systime_t osGetSystemTime(void) 00380 { 00381 //Get current tick count 00382 return GetTickCount(); 00383 } 00384 00385 00386 /** 00387 * @brief Allocate a memory block 00388 * @param[in] size Bytes to allocate 00389 * @return A pointer to the allocated memory block or NULL if 00390 * there is insufficient memory available 00391 **/ 00392 00393 void *osAllocMem(size_t size) 00394 { 00395 //Allocate a memory block 00396 return malloc(size); 00397 } 00398 00399 00400 /** 00401 * @brief Release a previously allocated memory block 00402 * @param[in] p Previously allocated memory block to be freed 00403 **/ 00404 00405 void osFreeMem(void *p) 00406 { 00407 //Free memory block 00408 free(p); 00409 } 00410
Generated on Tue Jul 12 2022 17:10:15 by
