circle_buffer.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #ifndef _Cirule_Buffer_h__
  2. #define _Cirule_Buffer_h__
  3. #include <stdint.h>
  4. #ifndef min
  5. #define min(x,y)(x<y)?x:y;
  6. #endif
  7. #define CBUFF_SUCCESS 0
  8. #define CBUFF_FULL -1
  9. #define CBUFF_EMPTY -2
  10. typedef struct {
  11. uint8_t * buffer;
  12. int16_t buffer_len;
  13. int16_t w_pos;
  14. int16_t r_pos;
  15. }c_buffer_t;
  16. //used by dma
  17. static __inline__ void circle_update_write_position(c_buffer_t *cbuff, int pos){
  18. cbuff->w_pos = pos;
  19. }
  20. static __inline__ int circle_get_read_position(c_buffer_t *cbuff){
  21. return cbuff->r_pos;
  22. }
  23. static __inline__ int circle_get_write_space(c_buffer_t *cbuff){
  24. int16_t size = (cbuff->w_pos >= cbuff->r_pos)?(cbuff->buffer_len - cbuff->w_pos + cbuff->r_pos):(cbuff->r_pos - cbuff->w_pos);
  25. size -= 1;
  26. if (size <= 0){
  27. return CBUFF_FULL;
  28. }
  29. return size;
  30. }
  31. void circle_buffer_init(c_buffer_t *cbuff, uint8_t *buffer, int16_t max_len);
  32. void circle_reset(c_buffer_t *cbuff);
  33. int circle_put_one_data(c_buffer_t *cbuff, uint8_t data);
  34. int circle_put_data(c_buffer_t *cbuff, uint8_t *data, int16_t len);
  35. int circle_get_one_data(c_buffer_t *cbuff, uint8_t *data);
  36. int circle_get_data(c_buffer_t *cbuffer, uint8_t *data, int16_t len);
  37. #endif /* _Cirule_Buffer_h__ */