Events.c 1.4 KB

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