app_drv_fifo.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : app_drv_fifo.h
  3. * Author : WCH
  4. * Version : V1.1
  5. * Date : 2022/01/19
  6. * Description :
  7. *********************************************************************************
  8. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  9. * Attention: This software (modified or not) and binary are used for
  10. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  11. *******************************************************************************/
  12. #ifndef __APP_DRV_FIFO_H__
  13. #define __APP_DRV_FIFO_H__
  14. #include <stdbool.h>
  15. #include <stdint.h>
  16. #ifndef BV
  17. #define BV(n) (1 << (n))
  18. #endif
  19. #ifndef BF
  20. #define BF(x, b, s) (((x) & (b)) >> (s))
  21. #endif
  22. #ifndef MIN
  23. #define MIN(n, m) (((n) < (m)) ? (n) : (m))
  24. #endif
  25. #ifndef MAX
  26. #define MAX(n, m) (((n) < (m)) ? (m) : (n))
  27. #endif
  28. #ifndef ABS
  29. #define ABS(n) (((n) < 0) ? -(n) : (n))
  30. #endif
  31. typedef enum
  32. {
  33. APP_DRV_FIFO_RESULT_SUCCESS = 0,
  34. APP_DRV_FIFO_RESULT_LENGTH_ERROR,
  35. APP_DRV_FIFO_RESULT_NOT_FOUND,
  36. APP_DRV_FIFO_RESULT_NOT_MEM,
  37. APP_DRV_FIFO_RESULT_NULL,
  38. } app_drv_fifo_result_t;
  39. #ifndef NULL
  40. #define NULL 0
  41. #endif
  42. /*!
  43. * FIFO structure
  44. */
  45. typedef struct Fifo_s
  46. {
  47. uint16_t begin;
  48. uint16_t end;
  49. uint8_t *data;
  50. uint16_t size;
  51. uint16_t size_mask;
  52. } app_drv_fifo_t;
  53. //__inline uint16_t app_drv_fifo_length(app_drv_fifo_t *fifo);
  54. uint16_t app_drv_fifo_length(app_drv_fifo_t *fifo);
  55. /*!
  56. * Initializes the FIFO structure
  57. *
  58. * \param [IN] fifo Pointer to the FIFO object
  59. * \param [IN] buffer Buffer to be used as FIFO
  60. * \param [IN] size size of the buffer
  61. */
  62. app_drv_fifo_result_t
  63. app_drv_fifo_init(app_drv_fifo_t *fifo, uint8_t *buffer, uint16_t buffer_size);
  64. /*!
  65. * Pushes data to the FIFO
  66. *
  67. * \param [IN] fifo Pointer to the FIFO object
  68. * \param [IN] data data to be pushed into the FIFO
  69. */
  70. void app_drv_fifo_push(app_drv_fifo_t *fifo, uint8_t data);
  71. /*!
  72. * Pops data from the FIFO
  73. *
  74. * \param [IN] fifo Pointer to the FIFO object
  75. * \retval data data popped from the FIFO
  76. */
  77. uint8_t app_drv_fifo_pop(app_drv_fifo_t *fifo);
  78. /*!
  79. * Flushes the FIFO
  80. *
  81. * \param [IN] fifo Pointer to the FIFO object
  82. */
  83. void app_drv_fifo_flush(app_drv_fifo_t *fifo);
  84. /*!
  85. * Checks if the FIFO is empty
  86. *
  87. * \param [IN] fifo Pointer to the FIFO object
  88. * \retval isEmpty true: FIFO is empty, false FIFO is not empty
  89. */
  90. bool app_drv_fifo_is_empty(app_drv_fifo_t *fifo);
  91. /*!
  92. * Checks if the FIFO is full
  93. *
  94. * \param [IN] fifo Pointer to the FIFO object
  95. * \retval isFull true: FIFO is full, false FIFO is not full
  96. */
  97. bool app_drv_fifo_is_full(app_drv_fifo_t *fifo);
  98. app_drv_fifo_result_t
  99. app_drv_fifo_write(app_drv_fifo_t *fifo, uint8_t *data,
  100. uint16_t *p_write_length);
  101. app_drv_fifo_result_t
  102. app_drv_fifo_write_from_same_addr(app_drv_fifo_t *fifo, uint8_t *data,
  103. uint16_t write_length);
  104. app_drv_fifo_result_t
  105. app_drv_fifo_read(app_drv_fifo_t *fifo, uint8_t *data, uint16_t *p_read_length);
  106. app_drv_fifo_result_t
  107. app_drv_fifo_read_to_same_addr(app_drv_fifo_t *fifo, uint8_t *data,
  108. uint16_t read_length);
  109. #endif // __APP_DRV_FIFO_H__