y_ringbuf.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /// ------------------------------------------------------------------------------------------------------------------------------------
  2. ///
  3. /// MIT License
  4. ///
  5. /// Permission is hereby granted, free of charge, to any person obtaining a copy
  6. /// of this software and associated documentation files (the "Software"), to deal
  7. /// in the Software without restriction, including without limitation the rights
  8. /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. /// copies of the Software, and to permit persons to whom the Software is
  10. /// furnished to do so, subject to the following conditions:
  11. ///
  12. /// The above copyright notice and this permission notice shall be included in all
  13. /// copies or substantial portions of the Software.
  14. ///
  15. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. /// SOFTWARE.
  22. ///
  23. /// Copyright (c) 2022 ycz. All rights reserved.
  24. ///
  25. /// Created by ycz on 2022/1/12.
  26. ///
  27. /// @brief
  28. /// y_ringbuf 具体功能实现
  29. ///
  30. /// ------------------------------------------------------------------------------------------------------------------------------------
  31. /// ------------------------------------------------------------------------------------------------------------------------------------
  32. /// 头文件
  33. /// ------------------------------------------------------------------------------------------------------------------------------------
  34. #include "y_ringbuf.h"
  35. #include "freertos/FreeRTOS.h"
  36. #include "freertos/task.h"
  37. #include "esp_system.h"
  38. #include "esp_log.h"
  39. #include "driver/uart.h"
  40. #include "string.h"
  41. #include "driver/gpio.h"
  42. #include "freertos/timers.h"
  43. extern SemaphoreHandle_t debug_Mutex;
  44. /// ------------------------------------------------------------------------------------------------------------------------------------
  45. /// 结构体
  46. /// ------------------------------------------------------------------------------------------------------------------------------------
  47. /// ringbuf 实例结构类型
  48. struct ringbuf {
  49. uint8_t *buf; ///< ringbuf 指针
  50. uint16_t length; ///< ringbuf 大小 最大64k
  51. uint16_t head; ///< ringbuf 头所在位置
  52. uint16_t tail; ///< ringbuf 尾所在位置
  53. uint16_t used_size; ///< ringbuf 已使用大小
  54. uint16_t free_size; ///< ringbuf 空闲大小
  55. #if Y_RINGBUF_USE_MUTEX_LOCK == 1
  56. int32_t mutex_lock; ///< 互斥锁句柄
  57. #endif
  58. };
  59. /// ------------------------------------------------------------------------------------------------------------------------------------
  60. /// 私有函数
  61. /// ------------------------------------------------------------------------------------------------------------------------------------
  62. #if Y_RINGBUF_USE_MUTEX_LOCK == 1
  63. /// @brief ringbuf 上锁
  64. /// @param [in] ringbuf ringbuf 句柄指针
  65. /// @retval true 成功
  66. /// @retval false 失败
  67. static bool _y_ringbuf_lock(RINGBUF_st *ringbuf) {
  68. // todo : 添加代码
  69. xSemaphoreTake( debug_Mutex, portMAX_DELAY );
  70. return true;
  71. }
  72. /// @brief ringbuf 解锁
  73. /// @param [in] ringbuf ringbuf 句柄指针
  74. /// @retval true 成功
  75. /// @retval false 失败
  76. static bool _y_ringbuf_unlock(RINGBUF_st *ringbuf) {
  77. // todo : 添加代码
  78. xSemaphoreGive( debug_Mutex );
  79. return true;
  80. }
  81. /// @brief 销毁互斥锁
  82. /// @param [in] ringbuf ringbuf 句柄指针
  83. /// @retval true 成功
  84. /// @retval false 失败
  85. static bool _y_ringbuf_destroy_mutex_lock(RINGBUF_st *ringbuf) {
  86. // todo : 添加代码
  87. vSemaphoreDelete(debug_Mutex);
  88. return true;
  89. }
  90. /// @brief 创建互斥锁
  91. /// @param [in] ringbuf ringbuf 句柄指针
  92. /// @retval true 成功
  93. /// @retval false 失败
  94. static bool _y_ringbuf_create_mutex_lock(RINGBUF_st *ringbuf) {
  95. // todo : 添加代码
  96. debug_Mutex = xSemaphoreCreateMutex();
  97. if(debug_Mutex == NULL)
  98. {
  99. return false;
  100. }
  101. return true;
  102. }
  103. #endif // Y_RINGBUF_USE_MUTEX_LOCK
  104. /// @brief ringbuf read 子函数
  105. /// @param [in] ringbuf ringbuf 句柄指针
  106. /// @param [out] data 读到数据存放的地址
  107. /// @param [in] size 想要读取的字节数
  108. /// @param [in] clear_data 读取后是否清除 buf 中的数据 true:清除 false:不清除
  109. /// @return 实际读到的字节数(\<=size)
  110. static uint16_t _y_ringbuf_read_sub(RINGBUF_st *ringbuf, uint8_t *data, uint16_t size, bool clear_data) {
  111. // 断言
  112. if (ringbuf == NULL) {
  113. printf("ringbuf is NULL");
  114. return 0;
  115. }
  116. if (data == NULL && size != 0) {
  117. printf("data pointer is NULL ,but size != 0 is %d", size);
  118. return 0;
  119. }
  120. #if Y_RINGBUF_USE_MUTEX_LOCK == 1
  121. // 上锁
  122. _y_ringbuf_lock(ringbuf);
  123. #endif
  124. uint16_t read_size = 0;
  125. // 判断是否有数据可读
  126. if (ringbuf->used_size != 0 && size != 0) {
  127. // 判断需要可读的大小
  128. if (ringbuf->used_size >= size) {
  129. read_size = size;
  130. } else {
  131. read_size = ringbuf->used_size;
  132. }
  133. // 判断头尾位置 确定读取方式
  134. if (ringbuf->tail <= ringbuf->head) {
  135. // 尾在头前面 或 在同一位置(全满)
  136. uint16_t end_size = ringbuf->length - ringbuf->head; // 尾部可读的空间
  137. if (end_size > read_size) {
  138. // 尾部数据足够读 可以顺序读取
  139. memcpy(data, ringbuf->buf + ringbuf->head, read_size);
  140. if (clear_data == true) {
  141. ringbuf->head += read_size;
  142. }
  143. } else if (end_size == read_size) {
  144. // 尾部数据刚好够读 可以顺序读取 但头位置移到 buf 开头
  145. memcpy(data, ringbuf->buf + ringbuf->head, read_size);
  146. if (clear_data == true) {
  147. ringbuf->head = 0;
  148. }
  149. } else {
  150. // 尾部数据不够读 需要分段读取
  151. memcpy(data, ringbuf->buf + ringbuf->head, end_size);
  152. memcpy(data + end_size, ringbuf->buf, read_size - end_size);
  153. if (clear_data == true) {
  154. ringbuf->head = read_size - end_size;
  155. }
  156. }
  157. } else {
  158. // 尾在头后面
  159. memcpy(data, ringbuf->buf + ringbuf->head, read_size);
  160. if (clear_data == true) {
  161. ringbuf->head += read_size;
  162. }
  163. }
  164. }
  165. // 更新句柄相关状态
  166. if (clear_data == true) {
  167. ringbuf->used_size -= read_size;
  168. ringbuf->free_size += read_size;
  169. }
  170. #if Y_RINGBUF_USE_MUTEX_LOCK == 1
  171. // 解锁
  172. _y_ringbuf_unlock(ringbuf);
  173. #endif
  174. //printf("ringbuf read %d byte succeed", read_size);
  175. return read_size;
  176. }
  177. /// ------------------------------------------------------------------------------------------------------------------------------------
  178. /// 公有函数
  179. /// ------------------------------------------------------------------------------------------------------------------------------------
  180. /// @brief 打印 y_ringbuf 版本号
  181. void y_ringbuf_print_version() {
  182. printf("y_ringbuf version : V%d.%d.%d", Y_RINGBUF_VERSION_MAJOR, Y_RINGBUF_VERSION_MINOR, Y_RINGBUF_VERSION_PATCH);
  183. }
  184. /// @brief 获取 ringbuf 中已使用的字节数
  185. /// @param [in] ringbuf ringbuf 句柄指针
  186. /// @return 已使用的字节数
  187. uint16_t y_ringbuf_get_used_size(RINGBUF_st *ringbuf) {
  188. // 断言
  189. if (ringbuf == NULL) {
  190. printf("ringbuf is NULL");
  191. return 0;
  192. }
  193. return ringbuf->used_size;
  194. }
  195. /// @brief 获取 ringbuf 中剩余可用的字节数
  196. /// @param [in] ringbuf ringbuf 句柄指针
  197. /// @return 剩余可用的字节数
  198. uint16_t y_ringbuf_get_free_size(RINGBUF_st *ringbuf) {
  199. // 断言
  200. if (ringbuf == NULL) {
  201. printf("ringbuf is NULL");
  202. return 0;
  203. }
  204. return ringbuf->free_size;
  205. }
  206. /// @brief 从 ringbuf 中读取数据后并清除
  207. /// @param [in] ringbuf ringbuf 句柄指针
  208. /// @param [out] data 读到数据存放的地址
  209. /// @param [in] size 想要读取的字节数
  210. /// @return 实际读到的字节数(\<=size)
  211. uint16_t y_ringbuf_read_clear(RINGBUF_st *ringbuf, uint8_t *data, uint16_t size) {
  212. return _y_ringbuf_read_sub(ringbuf, data, size, true);
  213. }
  214. /// @brief 从 ringbuf 中读取数据但不清除数据
  215. /// @param [in] ringbuf ringbuf 句柄指针
  216. /// @param [out] data 读到数据存放的地址
  217. /// @param [in] size 想要读取的字节数
  218. /// @return 实际读到的字节数(\<=size)
  219. uint16_t y_ringbuf_read_only(RINGBUF_st *ringbuf, uint8_t *data, uint16_t size) {
  220. return _y_ringbuf_read_sub(ringbuf, data, size, false);
  221. }
  222. /// @brief 写入数据到 ringbuf 长度大于空间时立即分配内存
  223. /// @param [in] ringbuf ringbuf 句柄指针
  224. /// @param [in] data 待写入数据指针
  225. /// @param [in] size 待写入数据大小
  226. /// @retval true 成功
  227. /// @retval false 失败
  228. bool y_ringbuf_write_remalloc_memory(RINGBUF_st *ringbuf, uint8_t *data, uint16_t size) {
  229. // 断言
  230. if (ringbuf == NULL) {
  231. printf("ringbuf is NULL");
  232. return false;
  233. }
  234. if (data == NULL && size != 0) {
  235. printf("data pointer is NULL ,but size != 0 is %d", size);
  236. return false;
  237. }
  238. #if Y_RINGBUF_USE_MUTEX_LOCK == 1
  239. // 上锁
  240. _y_ringbuf_lock(ringbuf);
  241. #endif
  242. // 判断空闲空间是否满足写入要求
  243. if (ringbuf->free_size < size) {
  244. printf("ringbuf free size %d ,but need write size %d", ringbuf->free_size, size);
  245. //重新设置内存
  246. //ringbuf->buf = (uint8_t *) heap_caps_malloc(length); // 开辟 ringbuf 所需要的空间
  247. ringbuf->length += ringbuf->length ;
  248. ringbuf->free_size += ringbuf->length;
  249. ringbuf->buf = (RINGBUF_st *) heap_caps_realloc( ringbuf->buf, ringbuf->length,MALLOC_CAP_8BIT|MALLOC_CAP_SPIRAM);
  250. if( ringbuf->buf!=NULL)
  251. {
  252. printf("remalloc ringbuf ok contiune write\r\n");
  253. }else
  254. {
  255. #if Y_RINGBUF_USE_MUTEX_LOCK == 1
  256. // 解锁
  257. _y_ringbuf_unlock(ringbuf);
  258. #endif
  259. printf("remalloc ringbuf fail not contiune write\r\n");
  260. return false;
  261. }
  262. //ringbuf = (RINGBUF_st *) heap_caps_malloc(sizeof(RINGBUF_st)); // 开辟 结构体句柄 所需要的空间
  263. //return false;
  264. }
  265. // 判断头尾位置 确定写入方式
  266. if (ringbuf->tail < ringbuf->head) {
  267. // 尾在头前面 可以直接顺序写入
  268. memcpy(ringbuf->buf + ringbuf->tail, data, size);
  269. ringbuf->tail += size;
  270. } else {
  271. // 尾在头后面 或 在同一位置(全空)
  272. uint16_t end_size = ringbuf->length - ringbuf->tail; // 尾部剩余可用的空间
  273. if (end_size > size) {
  274. // 尾部可用空间足够 可以直接顺序写入
  275. memcpy(ringbuf->buf + ringbuf->tail, data, size);
  276. ringbuf->tail += size;
  277. } else if (end_size == size) {
  278. // 尾部可用空间刚好够 可以直接顺序写入 但尾位置移到 buf 开头
  279. memcpy(ringbuf->buf + ringbuf->tail, data, size);
  280. ringbuf->tail = 0;
  281. } else {
  282. // 尾部可用空间不足 需要分段写入
  283. memcpy(ringbuf->buf + ringbuf->tail, data, end_size);
  284. memcpy(ringbuf->buf, data + end_size, size - end_size);
  285. ringbuf->tail = size - end_size;
  286. }
  287. }
  288. // 更新句柄相关状态
  289. ringbuf->used_size += size;
  290. ringbuf->free_size -= size;
  291. #if Y_RINGBUF_USE_MUTEX_LOCK == 1
  292. // 解锁
  293. _y_ringbuf_unlock(ringbuf);
  294. #endif
  295. //printf("ringbuf write %d byte succeed", size);
  296. return true;
  297. }
  298. /// @brief 写入数据到 ringbuf
  299. /// @param [in] ringbuf ringbuf 句柄指针
  300. /// @param [in] data 待写入数据指针
  301. /// @param [in] size 待写入数据大小
  302. /// @retval true 成功
  303. /// @retval false 失败
  304. bool y_ringbuf_write(RINGBUF_st *ringbuf, uint8_t *data, uint16_t size) {
  305. // 断言
  306. if (ringbuf == NULL) {
  307. printf("ringbuf is NULL");
  308. return false;
  309. }
  310. if (data == NULL && size != 0) {
  311. printf("data pointer is NULL ,but size != 0 is %d", size);
  312. return false;
  313. }
  314. #if Y_RINGBUF_USE_MUTEX_LOCK == 1
  315. // 上锁
  316. _y_ringbuf_lock(ringbuf);
  317. #endif
  318. // 判断空闲空间是否满足写入要求
  319. if (ringbuf->free_size < size) {
  320. printf("ringbuf free size %d ,but need write size %d", ringbuf->free_size, size);
  321. #if Y_RINGBUF_USE_MUTEX_LOCK == 1
  322. // 解锁
  323. _y_ringbuf_unlock(ringbuf);
  324. #endif
  325. return false;
  326. }
  327. // 判断头尾位置 确定写入方式
  328. if (ringbuf->tail < ringbuf->head) {
  329. // 尾在头前面 可以直接顺序写入
  330. memcpy(ringbuf->buf + ringbuf->tail, data, size);
  331. ringbuf->tail += size;
  332. } else {
  333. // 尾在头后面 或 在同一位置(全空)
  334. uint16_t end_size = ringbuf->length - ringbuf->tail; // 尾部剩余可用的空间
  335. if (end_size > size) {
  336. // 尾部可用空间足够 可以直接顺序写入
  337. memcpy(ringbuf->buf + ringbuf->tail, data, size);
  338. ringbuf->tail += size;
  339. } else if (end_size == size) {
  340. // 尾部可用空间刚好够 可以直接顺序写入 但尾位置移到 buf 开头
  341. memcpy(ringbuf->buf + ringbuf->tail, data, size);
  342. ringbuf->tail = 0;
  343. } else {
  344. // 尾部可用空间不足 需要分段写入
  345. memcpy(ringbuf->buf + ringbuf->tail, data, end_size);
  346. memcpy(ringbuf->buf, data + end_size, size - end_size);
  347. ringbuf->tail = size - end_size;
  348. }
  349. }
  350. // 更新句柄相关状态
  351. ringbuf->used_size += size;
  352. ringbuf->free_size -= size;
  353. #if Y_RINGBUF_USE_MUTEX_LOCK == 1
  354. // 解锁
  355. _y_ringbuf_unlock(ringbuf);
  356. #endif
  357. //printf("ringbuf write %d byte succeed", size);
  358. return true;
  359. }
  360. /// @brief 删除 ringbuf 中指定长度的数据
  361. /// @param [in] ringbuf ringbuf 句柄指针
  362. /// @param [in] size 要删除的数据长度
  363. /// @retval true 成功
  364. /// @retval false 失败
  365. bool y_ringbuf_delete_data(RINGBUF_st *ringbuf, uint16_t size) {
  366. // 断言
  367. if (ringbuf == NULL) {
  368. printf("ringbuf is NULL");
  369. return false;
  370. }
  371. // 判断是否有足够的数据可删
  372. if (ringbuf->used_size < size) {
  373. printf("not enough data, need delete %d data,but only used size %d", size, ringbuf->used_size);
  374. return false;
  375. }
  376. #if Y_RINGBUF_USE_MUTEX_LOCK == 1
  377. // 上锁
  378. _y_ringbuf_lock(ringbuf);
  379. #endif
  380. // 判断头尾位置 确定删除方式
  381. if (ringbuf->tail <= ringbuf->head) {
  382. // 尾在头前面 或 在同一位置(全满)
  383. uint16_t end_size = ringbuf->length - ringbuf->head; // 尾部删除的空间
  384. if (end_size > size) {
  385. // 尾部数据足够 可以直接删除
  386. ringbuf->head += size;
  387. } else if (end_size == size) {
  388. // 尾部数据刚好 可以直接删除 但头位置移到 buf 开头
  389. ringbuf->head = 0;
  390. } else {
  391. // 尾部数据不够 需要分段删除
  392. ringbuf->head = size - end_size;
  393. }
  394. } else {
  395. // 尾在头后面 可以直接删除
  396. ringbuf->head += size;
  397. }
  398. // 更新句柄相关状态
  399. ringbuf->used_size -= size;
  400. ringbuf->free_size += size;
  401. #if Y_RINGBUF_USE_MUTEX_LOCK == 1
  402. // 解锁
  403. _y_ringbuf_unlock(ringbuf);
  404. #endif
  405. printf("ringbuf delete %d byte succeed", size);
  406. return true;
  407. }
  408. /// @brief 判断 ringbuf 是否为满
  409. /// @param [in] ringbuf ringbuf 句柄指针
  410. /// @retval true 成功
  411. /// @retval false 失败
  412. bool y_ringbuf_is_full(RINGBUF_st *ringbuf) {
  413. // 断言
  414. if (ringbuf == NULL) {
  415. printf("ringbuf is NULL");
  416. return false;
  417. }
  418. if (ringbuf->free_size == 0) {
  419. return true;
  420. } else {
  421. return false;
  422. }
  423. }
  424. /// @brief 判断 ringbuf 是否为空
  425. /// @param [in] ringbuf ringbuf 句柄指针
  426. /// @retval true 成功
  427. /// @retval false 失败
  428. bool y_ringbuf_is_empty(RINGBUF_st *ringbuf) {
  429. // 断言
  430. if (ringbuf == NULL) {
  431. printf("ringbuf is NULL");
  432. return false;
  433. }
  434. if (ringbuf->used_size == 0) {
  435. return true;
  436. } else {
  437. return false;
  438. }
  439. }
  440. /// @brief 重置 ringbuf
  441. /// @param [in] ringbuf ringbuf 句柄指针
  442. /// @retval true 成功
  443. /// @retval false 失败
  444. bool y_ringbuf_reset(RINGBUF_st *ringbuf) {
  445. // 断言
  446. if (ringbuf == NULL) {
  447. printf("ringbuf is NULL, ringbuf reset fail");
  448. return false;
  449. }
  450. #if Y_RINGBUF_USE_MUTEX_LOCK == 1
  451. // 上锁
  452. _y_ringbuf_lock(ringbuf);
  453. #endif
  454. // 句柄状态初始化
  455. ringbuf->head = 0; // ringbuf 头所在位置
  456. ringbuf->tail = 0; // ringbuf 尾所在位置
  457. ringbuf->used_size = 0; // ringbuf 已使用大小
  458. ringbuf->free_size = ringbuf->length; // ringbuf 空闲大小
  459. #if Y_RINGBUF_USE_MUTEX_LOCK == 1
  460. // 解锁
  461. _y_ringbuf_unlock(ringbuf);
  462. #endif
  463. printf("ringbuf %p reset succeed", ringbuf);
  464. return true;
  465. }
  466. /// @brief 销毁一个 ringbuf
  467. /// @param [in] ringbuf ringbuf 句柄指针
  468. /// @retval true 成功
  469. /// @retval false 失败
  470. bool y_ringbuf_destroy(RINGBUF_st *ringbuf) {
  471. // 断言
  472. if (ringbuf == NULL) {
  473. printf("ringbuf is NULL, no need to delete");
  474. return false;
  475. }
  476. #if Y_RINGBUF_USE_MUTEX_LOCK == 1
  477. // 销毁互斥锁
  478. if (_y_ringbuf_destroy_mutex_lock(ringbuf) == false) {
  479. printf("ringbuf destroy mutex lock error, destroy ringbuf fail");
  480. return false;
  481. }
  482. #endif
  483. // 释放内存空间
  484. heap_caps_free(ringbuf->buf);
  485. heap_caps_free(ringbuf);
  486. printf("ringbuf %p destroy succeed", ringbuf);
  487. ringbuf = NULL;
  488. return true;
  489. }
  490. /// @brief 创建一个 ringbuf
  491. /// @param [in] length ringbuf 长度 (max 65535)
  492. /// @return ringbuf 句柄指针
  493. RINGBUF_st *y_ringbuf_create(uint16_t length) {
  494. // 断言
  495. if (length == 0) {
  496. printf("ringbuf length must > 0, ringbuf create fail");
  497. return NULL;
  498. }
  499. // 创建 ringbuf 句柄
  500. RINGBUF_st *tmp_ringbuf = (RINGBUF_st *) heap_caps_malloc(sizeof(RINGBUF_st),MALLOC_CAP_8BIT|MALLOC_CAP_SPIRAM); // 开辟 结构体句柄 所需要的空间
  501. if (tmp_ringbuf == NULL) {
  502. printf("heap_caps_malloc RINGBUF_st error");
  503. return NULL;
  504. } else {
  505. memset(tmp_ringbuf, 0, sizeof(RINGBUF_st)); // 清零
  506. }
  507. tmp_ringbuf->buf = (uint8_t *) heap_caps_malloc(length,MALLOC_CAP_8BIT|MALLOC_CAP_SPIRAM); // 开辟 ringbuf 所需要的空间
  508. if (tmp_ringbuf->buf == NULL) {
  509. printf("heap_caps_malloc ringbuf->buf error");
  510. heap_caps_free(tmp_ringbuf);
  511. return NULL;
  512. } else {
  513. memset(tmp_ringbuf->buf, 0, length); // 清零
  514. }
  515. // 初始化 ringbuf 句柄
  516. tmp_ringbuf->length = length;
  517. tmp_ringbuf->free_size = length;
  518. #if Y_RINGBUF_USE_MUTEX_LOCK == 1
  519. // 创建互斥锁
  520. if (_y_ringbuf_create_mutex_lock(tmp_ringbuf) == false) {
  521. heap_caps_free(tmp_ringbuf->buf);
  522. heap_caps_free(tmp_ringbuf);
  523. tmp_ringbuf = NULL;
  524. printf("ringbuf create mutex lock error, create ringbuf fail");
  525. return NULL;
  526. }
  527. #endif
  528. printf("ringbuf %p create succeed\n", tmp_ringbuf);
  529. return tmp_ringbuf;
  530. }