Events.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. #define MSGQUEUE_OBJECTS 16 // number of Message Queue Objects
  10. typedef struct { // object data type
  11. uint8_t Buf[32];
  12. uint8_t Idx;
  13. } MEM_BLOCK_t;
  14. typedef struct { // object data type
  15. uint8_t Buf[32];
  16. uint8_t Idx;
  17. } MSGQUEUE_OBJ_t;
  18. osEventFlagsId_t evt_id; // message queue id
  19. #define FLAGS_MSK1 0x00000001ul
  20. int Init_Events (void) {
  21. tid_Thread_EventSender = osThreadNew (Thread_EventSender, NULL, NULL);
  22. if (!tid_Thread_EventSender) return(-1);
  23. tid_Thread_EventReceiver = osThreadNew (Thread_EventReceiver, NULL, NULL);
  24. if (!tid_Thread_EventReceiver) return(-1);
  25. return(0);
  26. }
  27. void *Thread_EventSender (void *argument) {
  28. while (1) {
  29. evt_id = osEventFlagsNew(NULL);
  30. osEventFlagsSet(evt_id, FLAGS_MSK1);
  31. osThreadYield (); // suspend thread
  32. }
  33. }
  34. void *Thread_EventReceiver (void *argument) {
  35. uint32_t flags;
  36. while (1) {
  37. flags = osEventFlagsWait (evt_id,FLAGS_MSK1,osFlagsWaitAny, osWaitForever);
  38. //handle event
  39. }
  40. }