ringframe.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #ifndef __RINGFRAME_H
  2. #define __RINGFRAME_H
  3. /**@brief A FIFO instance structure.
  4. * @details Keeps track of which bytes to read and write next.
  5. * Also, it keeps the information about which memory is allocated for the buffer
  6. * and its size. This structure must be initialized by app_fifo_init() before use.
  7. */
  8. typedef enum
  9. {
  10. ringframe_size_4 = 0x0004U,
  11. ringframe_size_8 = 0x0008U,
  12. ringframe_size_16 = 0x0010U,
  13. ringframe_size_32 = 0x0020U,
  14. ringframe_size_64 = 0x0040U,
  15. ringframe_size_128 = 0x0080U,
  16. ringframe_size_256 = 0x0100U,
  17. ringframe_size_512 = 0x0200U,
  18. ringframe_size_1024 = 0x0400U,
  19. ringframe_size_2048 = 0x0800U,
  20. ringframe_size_4096 = 0x1000U,
  21. ringframe_size_8192 = 0x2000U,
  22. ringframe_size_16384 = 0x4000U,
  23. ringframe_size_32768 = 0x8000U,
  24. }ringframe_size_t;
  25. typedef struct
  26. {
  27. volatile unsigned short front; //!< ringframe front index.
  28. volatile unsigned short back; //!< ringframe back index.
  29. void * p_buffer; //!< Pointer to the memory that is used as storage.
  30. ringframe_size_t size; //单位为字节
  31. unsigned short element_count;
  32. unsigned short freespace;
  33. } ringframe_t;
  34. /******************************API 接口*************************************/
  35. #define RINGFRAME_DEF( _name, _size) \
  36. unsigned char _name##_ringframe_buffer[(_size)]; \
  37. ringframe_t _name= \
  38. { \
  39. .front = 0, \
  40. .back = 0, \
  41. .p_buffer = _name##_ringframe_buffer, \
  42. .size = (_size), \
  43. .element_count = 0, \
  44. .freespace = (_size)-1, \
  45. }
  46. //返回0表示操作成功,否则失败
  47. unsigned short ringframe_in(ringframe_t * p_ringframe, void * p_data,unsigned char data_size);
  48. //返回0表示操作成功,否则失败
  49. unsigned short ringframe_out(ringframe_t * p_ringframe, void * p_data,unsigned char * data_size);
  50. void ringframe_reset(ringframe_t * p_ringframe);
  51. //返回0表示操作成功,否则失败
  52. unsigned short ringframe_peek(ringframe_t * p_ringframe, void * p_data,unsigned char * data_size);
  53. //返回0表示操作成功,否则失败
  54. unsigned short ringframe_throw(ringframe_t * p_ringframe);
  55. #endif