Events.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. {
  22. tid_Thread_EventSender = osThreadNew (Thread_EventSender, NULL, NULL);
  23. if (tid_Thread_EventSender == NULL) {
  24. return(-1);
  25. }
  26. tid_Thread_EventReceiver = osThreadNew (Thread_EventReceiver, NULL, NULL);
  27. if (tid_Thread_EventReceiver == NULL) {
  28. return(-1);
  29. }
  30. return(0);
  31. }
  32. void Thread_EventSender (void *argument)
  33. {
  34. evt_id = osEventFlagsNew(NULL);
  35. while (1) {
  36. osEventFlagsSet(evt_id, FLAGS_MSK1);
  37. osThreadYield (); // suspend thread
  38. }
  39. }
  40. void Thread_EventReceiver (void *argument)
  41. {
  42. uint32_t flags;
  43. while (1) {
  44. flags = osEventFlagsWait (evt_id,FLAGS_MSK1,osFlagsWaitAny, osWaitForever);
  45. //handle event
  46. }
  47. }