circle_buffer.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. if (pos != 0) {
  19. if (pos == cbuff->buffer_len) {
  20. cbuff->w_pos = 0;
  21. }else {
  22. cbuff->w_pos = pos;
  23. }
  24. }
  25. }
  26. static __inline__ int circle_get_read_position(c_buffer_t *cbuff){
  27. return cbuff->r_pos;
  28. }
  29. static __inline__ int circle_get_write_space(c_buffer_t *cbuff){
  30. int16_t size = (cbuff->w_pos >= cbuff->r_pos)?(cbuff->buffer_len - cbuff->w_pos + cbuff->r_pos):(cbuff->r_pos - cbuff->w_pos);
  31. size -= 1;
  32. if (size <= 0){
  33. return CBUFF_FULL;
  34. }
  35. return size;
  36. }
  37. void circle_buffer_init(c_buffer_t *cbuff, uint8_t *buffer, int16_t max_len);
  38. void circle_reset(c_buffer_t *cbuff);
  39. int circle_put_one_data(c_buffer_t *cbuff, uint8_t data);
  40. int circle_put_data(c_buffer_t *cbuff, uint8_t *data, int16_t len);
  41. int circle_get_one_data(c_buffer_t *cbuff, uint8_t *data);
  42. int circle_get_data(c_buffer_t *cbuffer, uint8_t *data, int16_t len);
  43. #endif /* _Cirule_Buffer_h__ */