Events.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "cmsis_os2.h" // CMSIS RTOS header file
  2. /*----------------------------------------------------------------------------
  3. * Event Flags creation & usage
  4. *---------------------------------------------------------------------------*/
  5. #define FLAGS_MSK1 0x00000001U
  6. osEventFlagsId_t evt_id; // event flags id
  7. osThreadId_t tid_Thread_EventSender; // thread id 1
  8. osThreadId_t tid_Thread_EventReceiver; // thread id 2
  9. void Thread_EventSender (void *argument); // thread function 1
  10. void Thread_EventReceiver (void *argument); // thread function 2
  11. int Init_Events (void) {
  12. evt_id = osEventFlagsNew(NULL);
  13. if (evt_id == NULL) {
  14. ; // Event Flags object not created, handle failure
  15. }
  16. tid_Thread_EventSender = osThreadNew(Thread_EventSender, NULL, NULL);
  17. if (tid_Thread_EventSender == NULL) {
  18. return(-1);
  19. }
  20. tid_Thread_EventReceiver = osThreadNew(Thread_EventReceiver, NULL, NULL);
  21. if (tid_Thread_EventReceiver == NULL) {
  22. return(-1);
  23. }
  24. return(0);
  25. }
  26. void Thread_EventSender (void *argument) {
  27. while (1) {
  28. osEventFlagsSet(evt_id, FLAGS_MSK1);
  29. osThreadYield(); // suspend thread
  30. }
  31. }
  32. void Thread_EventReceiver (void *argument) {
  33. uint32_t flags;
  34. while (1) {
  35. flags = osEventFlagsWait(evt_id, FLAGS_MSK1, osFlagsWaitAny, osWaitForever);
  36. //handle event
  37. }
  38. }