Mutex.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "cmsis_os2.h" // CMSIS RTOS header file
  2. /*----------------------------------------------------------------------------
  3. * Mutex creation & usage
  4. *---------------------------------------------------------------------------*/
  5. void *Thread_Mutex (void *argument); // thread function
  6. osThreadId_t tid_Thread_Mutex; // thread id
  7. osMutexId_t mid_Thread_Mutex; // mutex id
  8. int Init_Mutex (void) {
  9. mid_Thread_Mutex = osMutexNew (NULL);
  10. if (!tid_Thread_Mutex) {
  11. ; // Mutex object not created, handle failure
  12. }
  13. tid_Thread_Mutex = osThreadNew (Thread_Mutex, NULL, NULL);
  14. if (!tid_Thread_Mutex) return(-1);
  15. return(0);
  16. }
  17. void *Thread_Mutex (void *argument) {
  18. osStatus_t status;
  19. while (1) {
  20. ; // Insert thread code here...
  21. status = osMutexAcquire (mid_Thread_Mutex, NULL);
  22. switch (status) {
  23. case osOK:
  24. ; // Use protected code here...
  25. osMutexRelease (mid_Thread_Mutex);
  26. break;
  27. case osErrorResource:
  28. break;
  29. case osErrorParameter:
  30. break;
  31. case osErrorISR:
  32. break;
  33. default:
  34. break;
  35. }
  36. osThreadYield (); // suspend thread
  37. }
  38. }