找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2657|回复: 1
打印 上一主题 下一主题
收起左侧

环形缓存区ring_buffer源程序

[复制链接]
跳转到指定楼层
楼主
ID:423034 发表于 2018-11-13 17:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
串口数据接收 及发送处理 节省空间 高效率

单片机源程序如下:
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "ring_buffer.h"

  4. /*****************************************************************************
  5. * Private types/enumerations/variables
  6. ****************************************************************************/

  7. #define RB_INDH(rb)                ((rb)->head & ((rb)->count - 1))
  8. #define RB_INDT(rb)                ((rb)->tail & ((rb)->count - 1))

  9. #if !defined(MAX)
  10. #define MAX(a, b) (((a) > (b)) ? (a) : (b))
  11. #endif
  12. #if !defined(MIN)
  13. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  14. #endif
  15. /*****************************************************************************
  16. * Public types/enumerations/variables
  17. ****************************************************************************/

  18. /*****************************************************************************
  19. * Private functions
  20. ****************************************************************************/

  21. /*****************************************************************************
  22. * Public functions
  23. ****************************************************************************/

  24. /**
  25. * @brief        Resets the ring buffer to empty
  26. * @param        RingBuff        : Pointer to ring buffer
  27. * @return        Nothing
  28. */
  29. void RingBuffer_Flush(RINGBUFF_T *RingBuff)
  30. {
  31.     RingBuff->head = RingBuff->tail = 0;
  32. }

  33. /**
  34. * @brief        Return size the ring buffer
  35. * @param        RingBuff        : Pointer to ring buffer
  36. * @return        Size of the ring buffer in bytes
  37. */
  38. int RingBuffer_GetSize(RINGBUFF_T *RingBuff)
  39. {
  40.     return RingBuff->count;
  41. }

  42. /**
  43. * @brief        Return number of items in the ring buffer
  44. * @param        RingBuff        : Pointer to ring buffer
  45. * @return        Number of items in the ring buffer
  46. */
  47. static int RingBuffer_GetCount(RINGBUFF_T *RingBuff)
  48. {
  49.     return RB_VHEAD(RingBuff) - RB_VTAIL(RingBuff);
  50. }

  51. /**
  52. * @brief        Return number of free items in the ring buffer
  53. * @param        RingBuff        : Pointer to ring buffer
  54. * @return        Number of free items in the ring buffer
  55. */
  56. static int RingBuffer_GetFree(RINGBUFF_T *RingBuff)
  57. {
  58.     return RingBuff->count - RingBuffer_GetCount(RingBuff);
  59. }

  60. /**
  61. * @brief        Return number of items in the ring buffer
  62. * @param        RingBuff        : Pointer to ring buffer
  63. * @return        1 if the ring buffer is full, otherwise 0
  64. */
  65. int RingBuffer_IsFull(RINGBUFF_T *RingBuff)
  66. {
  67.     return (RingBuffer_GetCount(RingBuff) >= RingBuff->count);
  68. }

  69. /**
  70. * @brief        Return empty status of ring buffer
  71. * @param        RingBuff        : Pointer to ring buffer
  72. * @return        1 if the ring buffer is empty, otherwise 0
  73. */
  74. int RingBuffer_IsEmpty(RINGBUFF_T *RingBuff)
  75. {
  76.     return RB_VHEAD(RingBuff) == RB_VTAIL(RingBuff);
  77. }
  78. /* Initialize ring buffer */
  79. int RingBuffer_Init(RINGBUFF_T *RingBuff, void *buffer, int itemSize, int count)
  80. {
  81.     RingBuff->data = buffer;
  82.     RingBuff->count = count;
  83.     RingBuff->itemSz = itemSize;
  84.     RingBuff->head = RingBuff->tail = 0;

  85.     return 1;
  86. }

  87. /* Insert a single item into Ring Buffer */
  88. int RingBuffer_Insert(RINGBUFF_T *RingBuff, const void *data)
  89. {
  90.     u8 *ptr = RingBuff->data;

  91.     /* We cannot insert when queue is full */
  92.     if (RingBuffer_IsFull(RingBuff))
  93.         return 0;
  94.     ptr += RB_INDH(RingBuff) * RingBuff->itemSz;
  95.     memcpy(ptr, data, RingBuff->itemSz);
  96.     RingBuff->head++;
  97.     return 1;
  98. }

  99. /* Insert multiple items into Ring Buffer */
  100. int RingBuffer_InsertMult(RINGBUFF_T *RingBuff, const void *data, int num)
  101. {
  102.     u8 *ptr = RingBuff->data;
  103.     int cnt1, cnt2;

  104.     /* We cannot insert when queue is full */
  105.     if (RingBuffer_IsFull(RingBuff))
  106.         return 0;

  107.     /* Calculate the segment lengths */
  108.     cnt1 = cnt2 = RingBuffer_GetFree(RingBuff);
  109.     if (RB_INDH(RingBuff) + cnt1 >= RingBuff->count)
  110.         cnt1 = RingBuff->count - RB_INDH(RingBuff);
  111.     cnt2 -= cnt1;

  112.     cnt1 = MIN(cnt1, num);
  113.     num -= cnt1;

  114.     cnt2 = MIN(cnt2, num);
  115.     num -= cnt2;

  116.     /* Write segment 1 */
  117.     ptr += RB_INDH(RingBuff) * RingBuff->itemSz;
  118.     memcpy(ptr, data, cnt1 * RingBuff->itemSz);
  119.     RingBuff->head += cnt1;

  120.     /* Write segment 2 */
  121.     ptr = (u8 *) RingBuff->data + RB_INDH(RingBuff) * RingBuff->itemSz;
  122.     data = (const u8 *) data + cnt1 * RingBuff->itemSz;
  123.     memcpy(ptr, data, cnt2 * RingBuff->itemSz);
  124.     RingBuff->head += cnt2;

  125.     return cnt1 + cnt2;
  126. }

  127. /* Pop single item from Ring Buffer */
  128. int RingBuffer_Pop(RINGBUFF_T *RingBuff, void *data)
  129. {
  130.     u8 *ptr = RingBuff->data;

  131.     /* We cannot pop when queue is empty */
  132.     if (RingBuffer_IsEmpty(RingBuff))
  133.         return 0;

  134.     ptr += RB_INDT(RingBuff) * RingBuff->itemSz;
  135.     memcpy(data, ptr, RingBuff->itemSz);
  136.     RingBuff->tail++;

  137.     return 1;
  138. }

  139. /* Pop multiple items from Ring buffer */
  140. int RingBuffer_PopMult(RINGBUFF_T *RingBuff, void *data, int num)
  141. {
  142.     u8 *ptr = RingBuff->data;
  143.     int cnt1, cnt2;

  144.     /* We cannot insert when queue is empty */
  145.     if (RingBuffer_IsEmpty(RingBuff))
  146.         return 0;

  147.     /* Calculate the segment lengths */
  148.     cnt1 = cnt2 = RingBuffer_GetCount(RingBuff);
  149.     if (RB_INDT(RingBuff) + cnt1 >= RingBuff->count)
  150.         cnt1 = RingBuff->count - RB_INDT(RingBuff);
  151.     cnt2 -= cnt1;

  152.     cnt1 = MIN(cnt1, num);
  153.     num -= cnt1;

  154.     cnt2 = MIN(cnt2, num);
  155.     num -= cnt2;

  156.     /* Write segment 1 */
  157.     ptr += RB_INDT(RingBuff) * RingBuff->itemSz;
  158.     memcpy(data, ptr, cnt1 * RingBuff->itemSz);
  159.     RingBuff->tail += cnt1;

  160.     /* Write segment 2 */
  161.     ptr = (u8 *) RingBuff->data + RB_INDT(RingBuff) * RingBuff->itemSz;
  162.     data = (u8 *) data + cnt1 * RingBuff->itemSz;
  163.     memcpy(data, ptr, cnt2 * RingBuff->itemSz);
  164.     RingBuff->tail += cnt2;

  165.     return cnt1 + cnt2;
  166. }
复制代码

所有资料51hei提供下载:
ring_buffer.zip (3.77 KB, 下载次数: 30)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:92520 发表于 2020-1-18 10:56 | 只看该作者
正是需要的,感谢分享!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表