queue.h111 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #ifndef __QUEUE_H
  2. #define __QUEUE_H
  3. #include "stdio.h"
  4. #define MAX_QSIZE (60u) /* 最大队列长度 */
  5. typedef unsigned long long u64;
  6. typedef struct
  7. {
  8. struct
  9. {
  10. uint8_t ide;
  11. uint8_t rtr;
  12. uint8_t dlc;
  13. } reg;
  14. union
  15. {
  16. uint32_t r;
  17. struct
  18. {
  19. uint8_t sa : 8;
  20. uint8_t ps : 8;
  21. uint8_t pf : 8;
  22. uint8_t dp : 1;
  23. uint8_t r : 1;
  24. uint8_t p : 3;
  25. } b;
  26. } id;
  27. union
  28. {
  29. uint8_t u8_buf[8];
  30. uint16_t u16_buf[4];
  31. uint32_t u32_buf[2];
  32. u64 u64_buf;
  33. } data;
  34. } pdu_tag, *p_pdu_tag;
  35. typedef struct
  36. {
  37. uint8_t buf[8];
  38. uint32_t can_id;
  39. } CanData_TypeDef;
  40. typedef struct
  41. {
  42. uint16_t head;
  43. uint16_t tail;
  44. uint16_t count;
  45. pdu_tag can_message[MAX_QSIZE];
  46. } can_queue_tag, *p_can_queue_tag;
  47. typedef enum
  48. {
  49. Q_OK,
  50. Q_ERR,
  51. Q_FULL,
  52. Q_EMPTY,
  53. } QUEUE_STATUS;
  54. #define QUEUE_ENTRY(type, size) \
  55. struct \
  56. { \
  57. uint8_t head; \
  58. uint8_t tail; \
  59. uint8_t count; \
  60. type message[size]; \
  61. }
  62. #define QUEUE_INIT(name) \
  63. { \
  64. (name)->head = (name)->tail = 0; \
  65. (name)->count = 0; \
  66. }
  67. /* List functions. */
  68. #define QUEUE_EMPTY(name) ((name)->count == 0)
  69. #define QUEUE_MAX(name) ((sizeof((name)->message)) / (sizeof((name)->message[0])))
  70. #define QUEUE_FULL(name) ((name)->count == ((sizeof((name)->message)) / (sizeof((name)->message[0]))))
  71. #define EN_QUEUE(name, field, ret) \
  72. do \
  73. { \
  74. if (QUEUE_FULL(name)) \
  75. { \
  76. ret = Q_FULL; \
  77. } \
  78. else \
  79. { \
  80. (name)->count++; \
  81. (name)->message[(name)->head] = field; \
  82. (name)->head = ((name)->head + 1) % QUEUE_MAX(name); \
  83. ret = Q_OK; \
  84. } \
  85. } while (0)
  86. #define DE_QUEUE(name, field, ret) \
  87. do \
  88. { \
  89. if (QUEUE_EMPTY(name)) \
  90. { \
  91. ret = Q_EMPTY; \
  92. } \
  93. else \
  94. { \
  95. field = (name)->message[(name)->tail]; \
  96. (name)->tail = ((name)->tail + 1) % QUEUE_MAX(name); \
  97. (name)->count--; \
  98. ret = Q_OK; \
  99. } \
  100. } while (0)
  101. void queue_init(p_can_queue_tag); // 初始化队列
  102. uint8_t queue_empty(p_can_queue_tag); // 查询队列是否为空
  103. uint8_t queue_full(p_can_queue_tag);
  104. uint8_t get_head(can_queue_tag *p_queue, pdu_tag *data); // 获取对头数据
  105. QUEUE_STATUS en_queue(p_can_queue_tag, pdu_tag); // 队列插入数据
  106. QUEUE_STATUS de_queue(p_can_queue_tag, pdu_tag);
  107. #endif