test_ringbuf.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "freertos/queue.h"
  11. #include "freertos/semphr.h"
  12. #include "freertos/ringbuf.h"
  13. #include "driver/gptimer.h"
  14. #include "esp_private/spi_flash_os.h"
  15. #include "esp_memory_utils.h"
  16. #include "esp_heap_caps.h"
  17. #include "spi_flash_mmap.h"
  18. #include "unity.h"
  19. #include "esp_rom_sys.h"
  20. //Definitions used in multiple test cases
  21. #define TIMEOUT_TICKS 10
  22. #define NO_OF_RB_TYPES 3
  23. #define ITEM_HDR_SIZE 8
  24. #define SMALL_ITEM_SIZE 8
  25. #define MEDIUM_ITEM_SIZE ((3 * SMALL_ITEM_SIZE) >> 1) //12 bytes
  26. #define LARGE_ITEM_SIZE (2 * SMALL_ITEM_SIZE) //16 bytes
  27. #define BUFFER_SIZE 160 //4Byte aligned size
  28. static const uint8_t small_item[SMALL_ITEM_SIZE] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  29. static const uint8_t large_item[LARGE_ITEM_SIZE] = { 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  30. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  31. };
  32. static RingbufHandle_t buffer_handles[NO_OF_RB_TYPES];
  33. static SemaphoreHandle_t done_sem;
  34. static void send_item_and_check(RingbufHandle_t handle, const uint8_t *item, size_t item_size, TickType_t ticks_to_wait, bool in_isr)
  35. {
  36. BaseType_t ret;
  37. if (in_isr) {
  38. ret = xRingbufferSendFromISR(handle, (void *)item, item_size, NULL);
  39. } else {
  40. ret = xRingbufferSend(handle, (void *)item, item_size, ticks_to_wait);
  41. }
  42. TEST_ASSERT_MESSAGE(ret == pdTRUE, "Failed to send item");
  43. }
  44. static void send_item_and_check_failure(RingbufHandle_t handle, const uint8_t *item, size_t item_size, TickType_t ticks_to_wait, bool in_isr)
  45. {
  46. BaseType_t ret;
  47. if (in_isr) {
  48. ret = xRingbufferSendFromISR(handle, (void *)item, item_size, NULL);
  49. } else {
  50. ret = xRingbufferSend(handle, (void *)item, item_size, ticks_to_wait);
  51. }
  52. TEST_ASSERT_MESSAGE(ret == pdFALSE, "Sent an item to a full buffer");
  53. }
  54. static void receive_check_and_return_item_no_split(RingbufHandle_t handle, const uint8_t *expected_data, size_t expected_size, TickType_t ticks_to_wait, bool in_isr)
  55. {
  56. //Receive item from no-split buffer
  57. size_t item_size;
  58. uint8_t *item;
  59. if (in_isr) {
  60. item = (uint8_t *)xRingbufferReceiveFromISR(handle, &item_size);
  61. } else {
  62. item = (uint8_t *)xRingbufferReceive(handle, &item_size, ticks_to_wait);
  63. }
  64. TEST_ASSERT_MESSAGE(item != NULL, "Failed to receive item");
  65. TEST_ASSERT_MESSAGE(item_size == expected_size, "Item size is incorrect");
  66. //Check data of received item
  67. for (int i = 0; i < item_size; i++) {
  68. TEST_ASSERT_MESSAGE(item[i] == expected_data[i], "Item data is invalid");
  69. }
  70. //Return item
  71. if (in_isr) {
  72. vRingbufferReturnItemFromISR(handle, (void *)item, NULL);
  73. } else {
  74. vRingbufferReturnItem(handle, (void *)item);
  75. }
  76. }
  77. static void receive_check_and_return_item_allow_split(RingbufHandle_t handle, const uint8_t *expected_data, size_t expected_size, TickType_t ticks_to_wait, bool in_isr)
  78. {
  79. //Receive item
  80. size_t item_size1, item_size2;
  81. uint8_t *item1, *item2;
  82. BaseType_t ret;
  83. if (in_isr) {
  84. ret = xRingbufferReceiveSplitFromISR(handle, (void **)&item1, (void **)&item2, &item_size1, &item_size2);
  85. } else {
  86. ret = xRingbufferReceiveSplit(handle, (void **)&item1, (void **)&item2, &item_size1, &item_size2, ticks_to_wait);
  87. }
  88. TEST_ASSERT_MESSAGE(ret == pdTRUE, "Failed to receive item");
  89. TEST_ASSERT_MESSAGE(item1 != NULL, "Failed to receive item");
  90. //Check data of received item(s) and return them
  91. if (item2 == NULL) {
  92. TEST_ASSERT_MESSAGE(item_size1 == expected_size, "Item size is incorrect");
  93. for (int i = 0; i < item_size1; i++) {
  94. TEST_ASSERT_MESSAGE(item1[i] == expected_data[i], "Item data is invalid");
  95. }
  96. //Return item
  97. if (in_isr) {
  98. vRingbufferReturnItemFromISR(handle, (void *)item1, NULL);
  99. } else {
  100. vRingbufferReturnItem(handle, (void *)item1);
  101. }
  102. } else {
  103. //Item was split
  104. TEST_ASSERT_MESSAGE(item_size1 + item_size2 == expected_size, "Total item size is incorrect");
  105. for (int i = 0; i < item_size1; i++) {
  106. TEST_ASSERT_MESSAGE(item1[i] == expected_data[i], "Head item data is invalid");
  107. }
  108. for (int i = 0; i < item_size2; i++) {
  109. TEST_ASSERT_MESSAGE(item2[i] == expected_data[item_size1 + i], "Head item data is invalid");
  110. }
  111. //Return Items
  112. if (in_isr) {
  113. vRingbufferReturnItemFromISR(handle, (void *)item1, NULL);
  114. vRingbufferReturnItemFromISR(handle, (void *)item2, NULL);
  115. } else {
  116. vRingbufferReturnItem(handle, (void *)item1);
  117. vRingbufferReturnItem(handle, (void *)item2);
  118. }
  119. }
  120. }
  121. static void receive_check_and_return_item_byte_buffer(RingbufHandle_t handle, const uint8_t *expected_data, size_t expected_size, TickType_t ticks_to_wait, bool in_isr)
  122. {
  123. //Receive item
  124. size_t item_size;
  125. uint8_t *item;
  126. if (in_isr) {
  127. item = (uint8_t *)xRingbufferReceiveUpToFromISR(handle, &item_size, expected_size);
  128. } else {
  129. item = (uint8_t *)xRingbufferReceiveUpTo(handle, &item_size, ticks_to_wait, expected_size); //Limit amount of bytes returned to the size of one item
  130. }
  131. TEST_ASSERT_MESSAGE(item != NULL, "Failed to receive item");
  132. //Check data of received item
  133. for (int i = 0; i < item_size; i++) {
  134. TEST_ASSERT_MESSAGE(item[i] == expected_data[i], "Item data is invalid");
  135. }
  136. //Return item
  137. if (in_isr) {
  138. vRingbufferReturnItemFromISR(handle, (void *)item, NULL);
  139. } else {
  140. vRingbufferReturnItem(handle, (void *)item);
  141. }
  142. //Check if item wrapped around
  143. if (item_size < expected_size) {
  144. //Item is wrapped, receive second portion
  145. size_t item_size2;
  146. uint8_t *item2;
  147. if (in_isr) {
  148. item2 = (uint8_t *)xRingbufferReceiveUpToFromISR(handle, &item_size2, expected_size - item_size);
  149. } else {
  150. item2 = (uint8_t *)xRingbufferReceiveUpTo(handle, &item_size2, ticks_to_wait, expected_size - item_size);
  151. }
  152. //= (uint8_t *)xRingbufferReceiveUpTo(handle, &item_size2, ticks_to_wait, expected_size - item_size);
  153. TEST_ASSERT_MESSAGE(item2 != NULL, "Failed to receive item");
  154. TEST_ASSERT_MESSAGE(item_size + item_size2 == expected_size, "Total item size is incorrect");
  155. for (int i = 0; i < item_size2; i++) {
  156. TEST_ASSERT_MESSAGE(item2[i] == expected_data[item_size + i], "Item data is invalid");
  157. }
  158. if (in_isr) {
  159. vRingbufferReturnItemFromISR(handle, (void *)item2, NULL);
  160. } else {
  161. vRingbufferReturnItem(handle, (void *)item2);
  162. }
  163. } else {
  164. TEST_ASSERT_MESSAGE(item_size == expected_size, "Item size is incorrect");
  165. }
  166. }
  167. /* ----------------- Basic ring buffer behavior tests cases --------------------
  168. * The following set of test cases will test basic send, receive, wrap around and buffer full
  169. * behavior of each type of ring buffer.
  170. * Test Case 1:
  171. * 1) Send multiple items (nearly fill the buffer)
  172. * 2) Receive and check the sent items (also prepares the buffer for a wrap around)
  173. * 3) Send a final item that causes a wrap around
  174. * 4) Receive and check the wrapped item
  175. *
  176. * Test Case 2:
  177. * 1) Send multiple items (completely fill the buffer)
  178. * 2) Send another item and verify that send failure occurs
  179. * 3) Receive one item and verify that space is freed up in the buffer
  180. * 4) Receive and check the remaining sent items
  181. *
  182. * Test Case 3:
  183. * 1) Send multiple items (nearly fill the buffer)
  184. * 2) Send another small sized item to fill the buffer without causing a wrap around (not needed in case of byte buffer)
  185. * 2) Send another item and verify that send failure occurs
  186. * 4) Receive and check the sent items
  187. */
  188. TEST_CASE("TC#1: No-Split", "[esp_ringbuf]")
  189. {
  190. //Create buffer
  191. RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_NOSPLIT);
  192. TEST_ASSERT_MESSAGE(buffer_handle != NULL, "Failed to create ring buffer");
  193. //Check buffer free size and max item size upon buffer creation. Should be BUFFER_SIZE/2 - ITEM_HDR_SIZE.
  194. TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == ((BUFFER_SIZE >> 1) - ITEM_HDR_SIZE), "Incorrect buffer free size received");
  195. TEST_ASSERT_MESSAGE(xRingbufferGetMaxItemSize(buffer_handle) == ((BUFFER_SIZE >> 1) - ITEM_HDR_SIZE), "Incorrect max item size received");
  196. //Calculate number of items to send. Aim to almost fill buffer to setup for wrap around
  197. int no_of_items = (BUFFER_SIZE - (ITEM_HDR_SIZE + SMALL_ITEM_SIZE)) / (ITEM_HDR_SIZE + SMALL_ITEM_SIZE);
  198. //Test sending items
  199. for (int i = 0; i < no_of_items; i++) {
  200. send_item_and_check(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  201. }
  202. //Verify items waiting matches with the number of items sent
  203. UBaseType_t items_waiting;
  204. vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
  205. TEST_ASSERT_MESSAGE(items_waiting == no_of_items, "Incorrect items waiting");
  206. //Test receiving items
  207. for (int i = 0; i < no_of_items; i++) {
  208. receive_check_and_return_item_no_split(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  209. }
  210. //Verify that no items are waiting
  211. vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
  212. TEST_ASSERT_MESSAGE(items_waiting == 0, "Incorrect items waiting");
  213. //Write pointer should be near the end, test wrap around
  214. UBaseType_t write_pos_before, write_pos_after;
  215. vRingbufferGetInfo(buffer_handle, NULL, NULL, &write_pos_before, NULL, NULL);
  216. //Send large item that causes wrap around
  217. send_item_and_check(buffer_handle, large_item, LARGE_ITEM_SIZE, TIMEOUT_TICKS, false);
  218. //Receive wrapped item
  219. receive_check_and_return_item_no_split(buffer_handle, large_item, LARGE_ITEM_SIZE, TIMEOUT_TICKS, false);
  220. vRingbufferGetInfo(buffer_handle, NULL, NULL, &write_pos_after, NULL, NULL);
  221. TEST_ASSERT_MESSAGE(write_pos_after < write_pos_before, "Failed to wrap around");
  222. //Cleanup
  223. vRingbufferDelete(buffer_handle);
  224. }
  225. TEST_CASE("TC#2: No-Split", "[esp_ringbuf]")
  226. {
  227. //Create buffer
  228. RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_NOSPLIT);
  229. TEST_ASSERT_MESSAGE(buffer_handle != NULL, "Failed to create ring buffer");
  230. //Check buffer free size and max item size upon buffer creation. Should be BUFFER_SIZE/2 - ITEM_HDR_SIZE.
  231. TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == ((BUFFER_SIZE >> 1) - ITEM_HDR_SIZE), "Incorrect buffer free size received");
  232. TEST_ASSERT_MESSAGE(xRingbufferGetMaxItemSize(buffer_handle) == ((BUFFER_SIZE >> 1) - ITEM_HDR_SIZE), "Incorrect max item size received");
  233. //Calculate number of items to send. Aim to fill the buffer
  234. int no_of_items = (BUFFER_SIZE) / (ITEM_HDR_SIZE + SMALL_ITEM_SIZE);
  235. //Test sending items
  236. for (int i = 0; i < no_of_items; i++) {
  237. send_item_and_check(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  238. }
  239. //Verify items waiting matches with the number of items sent
  240. UBaseType_t items_waiting;
  241. vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
  242. TEST_ASSERT_MESSAGE(items_waiting == no_of_items, "Incorrect items waiting");
  243. //At this point, the buffer should be full.
  244. TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == 0, "Buffer full not achieved");
  245. //Send an item. The item should not be sent to a full buffer.
  246. send_item_and_check_failure(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  247. //Receive one item
  248. receive_check_and_return_item_no_split(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  249. //At this point, the buffer should not be full any more
  250. TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) > 0, "Buffer should have free space");
  251. //Test receiving remaining items
  252. for (int i = 0; i < no_of_items - 1; i++) {
  253. receive_check_and_return_item_no_split(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  254. }
  255. //Verify that no items are waiting
  256. vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
  257. TEST_ASSERT_MESSAGE(items_waiting == 0, "Incorrect items waiting");
  258. //Cleanup
  259. vRingbufferDelete(buffer_handle);
  260. }
  261. TEST_CASE("TC#3: No-Split", "[esp_ringbuf]")
  262. {
  263. //Create buffer
  264. RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_NOSPLIT);
  265. TEST_ASSERT_MESSAGE(buffer_handle != NULL, "Failed to create ring buffer");
  266. //Check buffer free size and max item size upon buffer creation. Should be BUFFER_SIZE/2 - ITEM_HDR_SIZE.
  267. TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == ((BUFFER_SIZE >> 1) - ITEM_HDR_SIZE), "Incorrect buffer free size received");
  268. TEST_ASSERT_MESSAGE(xRingbufferGetMaxItemSize(buffer_handle) == ((BUFFER_SIZE >> 1) - ITEM_HDR_SIZE), "Incorrect max item size received");
  269. //Calculate number of medium items to send. Aim to almost fill the buffer
  270. int no_of_medium_items = (BUFFER_SIZE - (ITEM_HDR_SIZE + MEDIUM_ITEM_SIZE)) / (ITEM_HDR_SIZE + MEDIUM_ITEM_SIZE);
  271. //Test sending items
  272. for (int i = 0; i < no_of_medium_items; i++) {
  273. send_item_and_check(buffer_handle, large_item, MEDIUM_ITEM_SIZE, TIMEOUT_TICKS, false);
  274. }
  275. //Verify items waiting matches with the number of medium items sent
  276. UBaseType_t items_waiting;
  277. vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
  278. TEST_ASSERT_MESSAGE(items_waiting == no_of_medium_items, "Incorrect items waiting");
  279. //Send one small sized item. This will ensure that the item fits at the end of the buffer without causing the write pointer to wrap around.
  280. send_item_and_check(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  281. //The buffer should not have any free space as the number of bytes remaining should be < ITEM_HDR_SIZE.
  282. TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == 0, "Buffer full not achieved");
  283. //Send an item. The item should not be sent to a full buffer.
  284. send_item_and_check_failure(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  285. //Test receiving medium items
  286. for (int i = 0; i < no_of_medium_items; i++) {
  287. receive_check_and_return_item_no_split(buffer_handle, large_item, MEDIUM_ITEM_SIZE, TIMEOUT_TICKS, false);
  288. }
  289. //Test receiving small item
  290. receive_check_and_return_item_no_split(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  291. //Verify that no items are waiting
  292. vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
  293. TEST_ASSERT_MESSAGE(items_waiting == 0, "Incorrect items waiting");
  294. //Cleanup
  295. vRingbufferDelete(buffer_handle);
  296. }
  297. TEST_CASE("TC#1: Allow-Split", "[esp_ringbuf]")
  298. {
  299. //Create buffer
  300. RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_ALLOWSPLIT);
  301. TEST_ASSERT_MESSAGE(buffer_handle != NULL, "Failed to create ring buffer");
  302. //Check buffer free size and max item size upon buffer creation. Should be BUFFER_SIZE - (ITEM_HDR_SIZE * 2).
  303. TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == (BUFFER_SIZE - (ITEM_HDR_SIZE * 2)), "Incorrect buffer free size received");
  304. TEST_ASSERT_MESSAGE(xRingbufferGetMaxItemSize(buffer_handle) == (BUFFER_SIZE - (ITEM_HDR_SIZE * 2)), "Incorrect max item size received");
  305. //Calculate number of items to send. Aim to almost fill buffer to setup for wrap around
  306. int no_of_items = (BUFFER_SIZE - (ITEM_HDR_SIZE + SMALL_ITEM_SIZE)) / (ITEM_HDR_SIZE + SMALL_ITEM_SIZE);
  307. //Test sending items
  308. for (int i = 0; i < no_of_items; i++) {
  309. send_item_and_check(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  310. }
  311. //Verify items waiting matches with the number of items sent
  312. UBaseType_t items_waiting;
  313. vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
  314. TEST_ASSERT_MESSAGE(items_waiting == no_of_items, "Incorrect items waiting");
  315. //Test receiving items
  316. for (int i = 0; i < no_of_items; i++) {
  317. receive_check_and_return_item_allow_split(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  318. }
  319. //Verify that no items are waiting
  320. vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
  321. TEST_ASSERT_MESSAGE(items_waiting == 0, "Incorrect items waiting");
  322. //Write pointer should be near the end, test wrap around
  323. UBaseType_t write_pos_before, write_pos_after;
  324. vRingbufferGetInfo(buffer_handle, NULL, NULL, &write_pos_before, NULL, NULL);
  325. //Send large item that causes wrap around
  326. send_item_and_check(buffer_handle, large_item, LARGE_ITEM_SIZE, TIMEOUT_TICKS, false);
  327. //Receive wrapped item
  328. receive_check_and_return_item_allow_split(buffer_handle, large_item, LARGE_ITEM_SIZE, TIMEOUT_TICKS, false);
  329. vRingbufferGetInfo(buffer_handle, NULL, NULL, &write_pos_after, NULL, NULL);
  330. TEST_ASSERT_MESSAGE(write_pos_after < write_pos_before, "Failed to wrap around");
  331. //Cleanup
  332. vRingbufferDelete(buffer_handle);
  333. }
  334. TEST_CASE("TC#2: Allow-Split", "[esp_ringbuf]")
  335. {
  336. //Create buffer
  337. RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_ALLOWSPLIT);
  338. TEST_ASSERT_MESSAGE(buffer_handle != NULL, "Failed to create ring buffer");
  339. //Check buffer free size and max item size upon buffer creation. Should be BUFFER_SIZE - (ITEM_HDR_SIZE * 2).
  340. TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == (BUFFER_SIZE - (ITEM_HDR_SIZE * 2)), "Incorrect buffer free size received");
  341. TEST_ASSERT_MESSAGE(xRingbufferGetMaxItemSize(buffer_handle) == (BUFFER_SIZE - (ITEM_HDR_SIZE * 2)), "Incorrect max item size received");
  342. //Calculate number of items to send. Aim to fill the buffer
  343. int no_of_items = (BUFFER_SIZE) / (ITEM_HDR_SIZE + SMALL_ITEM_SIZE);
  344. //Test sending items
  345. for (int i = 0; i < no_of_items; i++) {
  346. send_item_and_check(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  347. }
  348. //Verify items waiting matches with the number of items sent
  349. UBaseType_t items_waiting;
  350. vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
  351. TEST_ASSERT_MESSAGE(items_waiting == no_of_items, "Incorrect items waiting");
  352. //At this point, the buffer should be full.
  353. TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == 0, "Buffer full not achieved");
  354. //Send an item. The item should not be sent to a full buffer.
  355. send_item_and_check_failure(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  356. //Receive one item
  357. receive_check_and_return_item_allow_split(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  358. //At this point, the buffer should not be full any more
  359. TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) > 0, "Buffer should have free space");
  360. //Test receiving remaining items
  361. for (int i = 0; i < no_of_items - 1; i++) {
  362. receive_check_and_return_item_allow_split(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  363. }
  364. //Verify that no items are waiting
  365. vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
  366. TEST_ASSERT_MESSAGE(items_waiting == 0, "Incorrect items waiting");
  367. //Cleanup
  368. vRingbufferDelete(buffer_handle);
  369. }
  370. TEST_CASE("TC#3: Allow-Split", "[esp_ringbuf]")
  371. {
  372. //Create buffer
  373. RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_ALLOWSPLIT);
  374. TEST_ASSERT_MESSAGE(buffer_handle != NULL, "Failed to create ring buffer");
  375. //Check buffer free size and max item size upon buffer creation. Should be BUFFER_SIZE - (ITEM_HDR_SIZE * 2).
  376. TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == (BUFFER_SIZE - (ITEM_HDR_SIZE * 2)), "Incorrect buffer free size received");
  377. TEST_ASSERT_MESSAGE(xRingbufferGetMaxItemSize(buffer_handle) == (BUFFER_SIZE - (ITEM_HDR_SIZE * 2)), "Incorrect max item size received");
  378. //Calculate number of medium items to send. Aim to almost fill the buffer
  379. int no_of_medium_items = (BUFFER_SIZE - (ITEM_HDR_SIZE + MEDIUM_ITEM_SIZE)) / (ITEM_HDR_SIZE + MEDIUM_ITEM_SIZE);
  380. //Test sending items
  381. for (int i = 0; i < no_of_medium_items; i++) {
  382. send_item_and_check(buffer_handle, large_item, MEDIUM_ITEM_SIZE, TIMEOUT_TICKS, false);
  383. }
  384. //Verify items waiting matches with the number of medium items sent
  385. UBaseType_t items_waiting;
  386. vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
  387. TEST_ASSERT_MESSAGE(items_waiting == no_of_medium_items, "Incorrect items waiting");
  388. //Send one small sized item. This will ensure that the item fits at the end of the buffer without causing the write pointer to wrap around.
  389. send_item_and_check(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  390. //The buffer should not have any free space as the number of bytes remaining should be < ITEM_HDR_SIZE.
  391. TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == 0, "Buffer full not achieved");
  392. //Send an item. The item should not be sent to a full buffer.
  393. send_item_and_check_failure(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  394. //Test receiving medium items
  395. for (int i = 0; i < no_of_medium_items; i++) {
  396. receive_check_and_return_item_allow_split(buffer_handle, large_item, MEDIUM_ITEM_SIZE, TIMEOUT_TICKS, false);
  397. }
  398. //Test receiving small item
  399. receive_check_and_return_item_allow_split(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  400. //Verify that no items are waiting
  401. vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
  402. TEST_ASSERT_MESSAGE(items_waiting == 0, "Incorrect items waiting");
  403. //Cleanup
  404. vRingbufferDelete(buffer_handle);
  405. }
  406. TEST_CASE("TC#1: Byte buffer", "[esp_ringbuf]")
  407. {
  408. //Create buffer
  409. RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_BYTEBUF);
  410. TEST_ASSERT_MESSAGE(buffer_handle != NULL, "Failed to create ring buffer");
  411. //Check buffer free size and max item size upon buffer creation. Should be BUFFER_SIZE
  412. TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == BUFFER_SIZE, "Incorrect buffer free size received");
  413. TEST_ASSERT_MESSAGE(xRingbufferGetMaxItemSize(buffer_handle) == BUFFER_SIZE, "Incorrect max item size received");
  414. //Calculate number of items to send. Aim to almost fill buffer to setup for wrap around
  415. int no_of_items = (BUFFER_SIZE - SMALL_ITEM_SIZE) / SMALL_ITEM_SIZE;
  416. //Test sending items
  417. for (int i = 0; i < no_of_items; i++) {
  418. send_item_and_check(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  419. }
  420. //Verify items waiting matches with the number of items sent
  421. UBaseType_t items_waiting;
  422. vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
  423. TEST_ASSERT_MESSAGE(items_waiting == no_of_items * SMALL_ITEM_SIZE, "Incorrect number of bytes waiting");
  424. //Test receiving items
  425. for (int i = 0; i < no_of_items; i++) {
  426. receive_check_and_return_item_byte_buffer(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  427. }
  428. //Verify that no items are waiting
  429. vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
  430. TEST_ASSERT_MESSAGE(items_waiting == 0, "Incorrect number of bytes waiting");
  431. //Write pointer should be near the end, test wrap around
  432. UBaseType_t write_pos_before, write_pos_after;
  433. vRingbufferGetInfo(buffer_handle, NULL, NULL, &write_pos_before, NULL, NULL);
  434. //Send large item that causes wrap around
  435. send_item_and_check(buffer_handle, large_item, LARGE_ITEM_SIZE, TIMEOUT_TICKS, false);
  436. //Receive wrapped item
  437. receive_check_and_return_item_byte_buffer(buffer_handle, large_item, LARGE_ITEM_SIZE, TIMEOUT_TICKS, false);
  438. vRingbufferGetInfo(buffer_handle, NULL, NULL, &write_pos_after, NULL, NULL);
  439. TEST_ASSERT_MESSAGE(write_pos_after < write_pos_before, "Failed to wrap around");
  440. //Cleanup
  441. vRingbufferDelete(buffer_handle);
  442. }
  443. TEST_CASE("TC#2: Byte buffer", "[esp_ringbuf]")
  444. {
  445. //Create buffer
  446. RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_BYTEBUF);
  447. TEST_ASSERT_MESSAGE(buffer_handle != NULL, "Failed to create ring buffer");
  448. //Check buffer free size and max item size upon buffer creation. Should be BUFFER_SIZE.
  449. TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == BUFFER_SIZE, "Incorrect buffer free size received");
  450. TEST_ASSERT_MESSAGE(xRingbufferGetMaxItemSize(buffer_handle) == BUFFER_SIZE, "Incorrect max item size received");
  451. //Calculate number of items to send. Aim to fill the buffer
  452. int no_of_items = BUFFER_SIZE / SMALL_ITEM_SIZE;
  453. //Test sending items
  454. for (int i = 0; i < no_of_items; i++) {
  455. send_item_and_check(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  456. }
  457. //Verify items waiting matches with the number of items sent
  458. UBaseType_t items_waiting;
  459. vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
  460. TEST_ASSERT_MESSAGE(items_waiting == no_of_items * SMALL_ITEM_SIZE, "Incorrect number of bytes waiting");
  461. //At this point, the buffer should be full.
  462. TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == 0, "Buffer full not achieved");
  463. //Send an item. The item should not be sent to a full buffer.
  464. send_item_and_check_failure(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  465. //Receive one item
  466. receive_check_and_return_item_byte_buffer(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  467. //At this point, the buffer should not be full any more
  468. TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) > 0, "Buffer should have free space");
  469. //Test receiving remaining items
  470. for (int i = 0; i < no_of_items - 1; i++) {
  471. receive_check_and_return_item_byte_buffer(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  472. }
  473. //Verify that no items are waiting
  474. vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
  475. TEST_ASSERT_MESSAGE(items_waiting == 0, "Incorrect items waiting");
  476. //Cleanup
  477. vRingbufferDelete(buffer_handle);
  478. }
  479. TEST_CASE("TC#3: Byte buffer", "[esp_ringbuf]")
  480. {
  481. //Create buffer
  482. RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_BYTEBUF);
  483. TEST_ASSERT_MESSAGE(buffer_handle != NULL, "Failed to create ring buffer");
  484. //Check buffer free size and max item size upon buffer creation. Should be BUFFER_SIZE.
  485. TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == BUFFER_SIZE, "Incorrect buffer free size received");
  486. TEST_ASSERT_MESSAGE(xRingbufferGetMaxItemSize(buffer_handle) == BUFFER_SIZE, "Incorrect max item size received");
  487. //Calculate number of medium items to send. Aim to almost fill the buffer
  488. int no_of_medium_items = BUFFER_SIZE / MEDIUM_ITEM_SIZE;
  489. //Test sending items
  490. for (int i = 0; i < no_of_medium_items; i++) {
  491. send_item_and_check(buffer_handle, large_item, MEDIUM_ITEM_SIZE, TIMEOUT_TICKS, false);
  492. }
  493. //Verify items waiting matches with the number of medium items sent
  494. UBaseType_t items_waiting;
  495. vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
  496. TEST_ASSERT_MESSAGE(items_waiting == no_of_medium_items * MEDIUM_ITEM_SIZE, "Incorrect number of bytes waiting");
  497. //The buffer should not have any free space for one small item.
  498. TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) < SMALL_ITEM_SIZE, "Buffer full not achieved");
  499. //Send an item. The item should not be sent to a full buffer.
  500. send_item_and_check_failure(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  501. //Test receiving medium items
  502. for (int i = 0; i < no_of_medium_items; i++) {
  503. receive_check_and_return_item_byte_buffer(buffer_handle, large_item, MEDIUM_ITEM_SIZE, TIMEOUT_TICKS, false);
  504. }
  505. //Verify that no items are waiting
  506. vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
  507. TEST_ASSERT_MESSAGE(items_waiting == 0, "Incorrect items waiting");
  508. //Cleanup
  509. vRingbufferDelete(buffer_handle);
  510. }
  511. /* ----------------------- Ring buffer queue sets test ------------------------
  512. * The following test case will test receiving from ring buffers that have been
  513. * added to a queue set. The test case will do the following...
  514. * 1) Ring buffer of each type is created and added to the queue set
  515. * 2) A receiving task is created to select from the queue set and read from the appropriate ring buffer
  516. */
  517. static void queue_set_receiving_task(void *queue_set_handle)
  518. {
  519. QueueSetHandle_t queue_set = (QueueSetHandle_t)queue_set_handle;
  520. //Receive multiple items via queue set
  521. BaseType_t done = pdFALSE;
  522. int no_of_items = BUFFER_SIZE / SMALL_ITEM_SIZE;
  523. int items_rec_count[NO_OF_RB_TYPES] = {0};
  524. while (done != pdTRUE) {
  525. QueueSetMemberHandle_t member = xQueueSelectFromSet(queue_set, TIMEOUT_TICKS);
  526. //Read from selected ring buffer
  527. if (xRingbufferCanRead(buffer_handles[0], member) == pdTRUE) {
  528. //No-split buffer
  529. receive_check_and_return_item_no_split(buffer_handles[0], small_item, SMALL_ITEM_SIZE, 0, false);
  530. items_rec_count[0] ++;
  531. } else if (xRingbufferCanRead(buffer_handles[1], member) == pdTRUE) {
  532. //Allow-split buffer
  533. receive_check_and_return_item_allow_split(buffer_handles[1], small_item, SMALL_ITEM_SIZE, 0, false);
  534. items_rec_count[1] ++;
  535. } else if (xRingbufferCanRead(buffer_handles[2], member) == pdTRUE) {
  536. //Byte buffer
  537. receive_check_and_return_item_byte_buffer(buffer_handles[2], small_item, SMALL_ITEM_SIZE, 0, false);
  538. items_rec_count[2] ++;
  539. } else {
  540. TEST_ASSERT_MESSAGE( false, "Error with queue set member");
  541. }
  542. //Check for completion
  543. if (items_rec_count[0] == no_of_items &&
  544. items_rec_count[1] == no_of_items &&
  545. items_rec_count[2] == no_of_items) {
  546. done = pdTRUE;
  547. }
  548. }
  549. xSemaphoreGive(done_sem);
  550. vTaskDelete(NULL);
  551. }
  552. TEST_CASE("Test ring buffer with queue sets", "[esp_ringbuf]")
  553. {
  554. QueueSetHandle_t queue_set = xQueueCreateSet(NO_OF_RB_TYPES);
  555. done_sem = xSemaphoreCreateBinary();
  556. //Create ring buffer of each type, then add them to a queue set
  557. for (int i = 0; i < NO_OF_RB_TYPES; i++) {
  558. buffer_handles[i] = xRingbufferCreate(BUFFER_SIZE, i);
  559. TEST_ASSERT_MESSAGE(buffer_handles[i] != NULL, "Failed to create ring buffer");
  560. TEST_ASSERT_MESSAGE(xRingbufferAddToQueueSetRead(buffer_handles[i], queue_set) == pdPASS, "Failed to add to read queue set");
  561. }
  562. //Create a task to send items to each ring buffer
  563. int no_of_items = BUFFER_SIZE / SMALL_ITEM_SIZE;
  564. xTaskCreatePinnedToCore(queue_set_receiving_task, "rec tsk", 2048, (void *)queue_set, 10, NULL, 0);
  565. //Send multiple items to each type of ring buffer
  566. for (int i = 0; i < no_of_items; i++) {
  567. for (int j = 0; j < NO_OF_RB_TYPES; j++) {
  568. send_item_and_check(buffer_handles[j], small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  569. }
  570. }
  571. xSemaphoreTake(done_sem, portMAX_DELAY);
  572. vSemaphoreDelete(done_sem);
  573. //Remove and delete ring buffers from queue sets
  574. for (int i = 0; i < NO_OF_RB_TYPES; i++) {
  575. TEST_ASSERT_MESSAGE(xRingbufferRemoveFromQueueSetRead(buffer_handles[i], queue_set) == pdTRUE, "Failed to remove from read queue set");
  576. vRingbufferDelete(buffer_handles[i]);
  577. }
  578. vQueueDelete(queue_set);
  579. vTaskDelay(1);
  580. }
  581. /* -------------------------- Test ring buffer ISR -----------------------------
  582. * The following test case tests ring buffer ISR API. A timer is used to trigger
  583. * the ISR. The test case will do the following
  584. * 1) ISR will be triggered periodically by timer
  585. * 2) The ISR will iterate through all ring buffer types where each iteration
  586. * will send then receive an item to a ring buffer.
  587. */
  588. #define ISR_ITERATIONS ((BUFFER_SIZE / SMALL_ITEM_SIZE) * 2)
  589. static int buf_type;
  590. static int iterations;
  591. static bool on_timer_alarm(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx)
  592. {
  593. bool need_yield = false;
  594. //Test sending to buffer from ISR from ISR
  595. if (buf_type < NO_OF_RB_TYPES) {
  596. send_item_and_check(buffer_handles[buf_type], (void *)small_item, SMALL_ITEM_SIZE, 0, true);
  597. }
  598. //Receive item from ISR
  599. if (buf_type == RINGBUF_TYPE_NOSPLIT) {
  600. //Test receive from ISR for no-split buffer
  601. receive_check_and_return_item_no_split(buffer_handles[buf_type], (void *)small_item, SMALL_ITEM_SIZE, 0, true);
  602. buf_type++;
  603. } else if (buf_type == RINGBUF_TYPE_ALLOWSPLIT) {
  604. //Test send from ISR to allow-split buffer
  605. receive_check_and_return_item_allow_split(buffer_handles[buf_type], (void *)small_item, SMALL_ITEM_SIZE, 0, true);
  606. buf_type++;
  607. } else if (buf_type == RINGBUF_TYPE_BYTEBUF) {
  608. //Test receive from ISR for byte buffer
  609. receive_check_and_return_item_byte_buffer(buffer_handles[buf_type], (void *)small_item, SMALL_ITEM_SIZE, 0, true);
  610. buf_type++;
  611. } else if (buf_type == NO_OF_RB_TYPES) {
  612. //Check if all iterations complete
  613. if (iterations < ISR_ITERATIONS) {
  614. iterations++;
  615. buf_type = 0; //Reset and iterate through each buffer type again
  616. goto out;
  617. } else {
  618. //Signal complete
  619. BaseType_t task_woken = pdFALSE;
  620. xSemaphoreGiveFromISR(done_sem, &task_woken);
  621. if (task_woken == pdTRUE) {
  622. buf_type++;
  623. need_yield = true;
  624. }
  625. }
  626. }
  627. out:
  628. return need_yield;
  629. }
  630. // IDF-6471 - test hangs up on QEMU
  631. TEST_CASE("Test ring buffer ISR", "[esp_ringbuf][qemu-ignore]")
  632. {
  633. gptimer_handle_t gptimer;
  634. for (int i = 0; i < NO_OF_RB_TYPES; i++) {
  635. buffer_handles[i] = xRingbufferCreate(BUFFER_SIZE, i);
  636. }
  637. done_sem = xSemaphoreCreateBinary();
  638. buf_type = 0;
  639. iterations = 0;
  640. //Setup timer for ISR
  641. gptimer_config_t config = {
  642. .clk_src = GPTIMER_CLK_SRC_DEFAULT,
  643. .direction = GPTIMER_COUNT_UP,
  644. .resolution_hz = 1000000,
  645. };
  646. TEST_ESP_OK(gptimer_new_timer(&config, &gptimer));
  647. gptimer_alarm_config_t alarm_config = {
  648. .reload_count = 0,
  649. .alarm_count = 2000,
  650. .flags.auto_reload_on_alarm = true,
  651. };
  652. gptimer_event_callbacks_t cbs = {
  653. .on_alarm = on_timer_alarm,
  654. };
  655. TEST_ESP_OK(gptimer_register_event_callbacks(gptimer, &cbs, NULL));
  656. TEST_ESP_OK(gptimer_set_alarm_action(gptimer, &alarm_config));
  657. TEST_ESP_OK(gptimer_enable(gptimer));
  658. TEST_ESP_OK(gptimer_start(gptimer));
  659. //Wait for ISR to complete multiple iterations
  660. xSemaphoreTake(done_sem, portMAX_DELAY);
  661. //Cleanup
  662. TEST_ESP_OK(gptimer_stop(gptimer));
  663. TEST_ESP_OK(gptimer_disable(gptimer));
  664. TEST_ESP_OK(gptimer_del_timer(gptimer));
  665. vSemaphoreDelete(done_sem);
  666. for (int i = 0; i < NO_OF_RB_TYPES; i++) {
  667. vRingbufferDelete(buffer_handles[i]);
  668. }
  669. }
  670. /* ---------------------------- Test ring buffer SMP ---------------------------
  671. * The following test case tests each type of ring buffer in an SMP fashion. A
  672. * sending task and a receiving task is created. The sending task will split
  673. * a continuous piece of data into items of random length and send it to a ring
  674. * buffer. The receiving task will receive and check those items.
  675. * Every permutation of core pinning of the sending and receiving task will be
  676. * tested.
  677. */
  678. #define SRAND_SEED 3 //Arbitrarily chosen srand() seed
  679. #define SMP_TEST_ITERATIONS 4
  680. static const char continuous_data[] = {"A_very_long_string_that_will_be_split_into_"
  681. "items_of_random_lengths_and_sent_to_the_ring_"
  682. "buffer._The_maximum_random_length_will_also_"
  683. "be_increased_over_multiple_iterations_in_this"
  684. "_test"
  685. };
  686. #define CONT_DATA_LEN sizeof(continuous_data)
  687. //32-bit aligned size that guarantees a wrap around at some point
  688. #define CONT_DATA_TEST_BUFF_LEN (((CONT_DATA_LEN/2) + 0x03) & ~0x3)
  689. typedef struct {
  690. RingbufHandle_t buffer;
  691. RingbufferType_t type;
  692. } task_args_t;
  693. static SemaphoreHandle_t tasks_done;
  694. static SemaphoreHandle_t tx_done;
  695. static SemaphoreHandle_t rx_done;
  696. static void send_to_buffer(RingbufHandle_t buffer, size_t max_item_size)
  697. {
  698. for (int iter = 0; iter < SMP_TEST_ITERATIONS; iter++) {
  699. size_t bytes_sent = 0; //Number of data bytes sent in this iteration
  700. size_t next_item_size; //Size of next item to send
  701. while (bytes_sent < CONT_DATA_LEN) {
  702. //Get size of next item
  703. next_item_size = rand() % (max_item_size + 1);
  704. if (next_item_size + bytes_sent > CONT_DATA_LEN) {
  705. next_item_size = CONT_DATA_LEN - bytes_sent;
  706. }
  707. //Send item
  708. TEST_ASSERT_MESSAGE(xRingbufferSend(buffer, (void *) & (continuous_data[bytes_sent]), next_item_size, TIMEOUT_TICKS) == pdTRUE, "Failed to send an item");
  709. bytes_sent += next_item_size;
  710. }
  711. xSemaphoreGive(tx_done);
  712. xSemaphoreTake(rx_done, portMAX_DELAY);
  713. }
  714. }
  715. static void read_from_buffer(RingbufHandle_t buffer, RingbufferType_t buf_type, size_t max_rec_size)
  716. {
  717. for (int iter = 0; iter < SMP_TEST_ITERATIONS; iter++) {
  718. size_t bytes_rec = 0; //Number of data bytes received in this iteration
  719. while (bytes_rec < CONT_DATA_LEN) {
  720. size_t item_size, item_size2; //Possible for allow split buffers to receive two items
  721. char *item_data, *item_data2;
  722. //Select appropriate receive function for type of ring buffer
  723. if (buf_type == RINGBUF_TYPE_NOSPLIT) {
  724. item_data = (char *)xRingbufferReceive(buffer, &item_size, TIMEOUT_TICKS);
  725. } else if (buf_type == RINGBUF_TYPE_ALLOWSPLIT) {
  726. BaseType_t ret = xRingbufferReceiveSplit(buffer, (void **)&item_data, (void **)&item_data2, &item_size, &item_size2, TIMEOUT_TICKS);
  727. TEST_ASSERT_MESSAGE(ret == pdTRUE, "Failed to receive any item");
  728. } else {
  729. item_data = (char *)xRingbufferReceiveUpTo(buffer, &item_size, TIMEOUT_TICKS, max_rec_size);
  730. }
  731. //Check received item and return it
  732. TEST_ASSERT_MESSAGE(item_data != NULL, "Failed to receive an item");
  733. if (buf_type == RINGBUF_TYPE_BYTEBUF) {
  734. TEST_ASSERT_MESSAGE(item_size <= max_rec_size, "Received data exceeds max size");
  735. }
  736. for (int i = 0; i < item_size; i++) {
  737. //Check item_data is valid
  738. TEST_ASSERT_MESSAGE(item_data[i] == continuous_data[bytes_rec + i], "Received data is corrupted");
  739. }
  740. bytes_rec += item_size;
  741. vRingbufferReturnItem(buffer, item_data);
  742. if (buf_type == RINGBUF_TYPE_ALLOWSPLIT && item_data2 != NULL) {
  743. //Check item_data2 is valid
  744. for (int i = 0; i < item_size2; i++) {
  745. TEST_ASSERT_MESSAGE(item_data2[i] == continuous_data[bytes_rec + i], "Received split data is corrupted");
  746. }
  747. bytes_rec += item_size2;
  748. vRingbufferReturnItem(buffer, item_data2);
  749. }
  750. }
  751. TEST_ASSERT_MESSAGE(bytes_rec == CONT_DATA_LEN, "Total length of received data is incorrect");
  752. xSemaphoreGive(rx_done);
  753. xSemaphoreTake(tx_done, portMAX_DELAY);
  754. }
  755. }
  756. static void send_task(void *args)
  757. {
  758. RingbufHandle_t buffer = ((task_args_t *)args)->buffer;
  759. size_t max_item_len = xRingbufferGetMaxItemSize(buffer);
  760. //Test sending short length items
  761. send_to_buffer(buffer, 1);
  762. //Test sending mid length items
  763. send_to_buffer(buffer, max_item_len / 2);
  764. //Test sending long length items
  765. send_to_buffer(buffer, max_item_len);
  766. vTaskDelete(NULL);
  767. }
  768. static void rec_task(void *args)
  769. {
  770. RingbufHandle_t buffer = ((task_args_t *)args)->buffer;
  771. size_t max_rec_len = xRingbufferGetMaxItemSize(buffer);
  772. //Test receiving short length items
  773. read_from_buffer(buffer, ((task_args_t *)args)->type, 1);
  774. //Test receiving mid length items
  775. read_from_buffer(buffer, ((task_args_t *)args)->type, max_rec_len / 2);
  776. //Test receiving long length items
  777. read_from_buffer(buffer, ((task_args_t *)args)->type, max_rec_len);
  778. xSemaphoreGive(tasks_done);
  779. vTaskDelete(NULL);
  780. }
  781. static void setup(void)
  782. {
  783. esp_rom_printf("Size of test data: %d\n", CONT_DATA_LEN);
  784. tx_done = xSemaphoreCreateBinary(); //Semaphore to indicate send is done for a particular iteration
  785. rx_done = xSemaphoreCreateBinary(); //Semaphore to indicate receive is done for a particular iteration
  786. tasks_done = xSemaphoreCreateBinary(); //Semaphore used to to indicate send and receive tasks completed running
  787. srand(SRAND_SEED); //Seed RNG
  788. }
  789. static void cleanup(void)
  790. {
  791. //Cleanup
  792. vSemaphoreDelete(tx_done);
  793. vSemaphoreDelete(rx_done);
  794. vSemaphoreDelete(tasks_done);
  795. }
  796. TEST_CASE("Test ring buffer SMP", "[esp_ringbuf]")
  797. {
  798. setup();
  799. //Iterate through buffer types (No split, split, then byte buff)
  800. for (RingbufferType_t buf_type = 0; buf_type < RINGBUF_TYPE_MAX; buf_type++) {
  801. //Create buffer
  802. task_args_t task_args;
  803. task_args.buffer = xRingbufferCreate(CONT_DATA_TEST_BUFF_LEN, buf_type); //Create buffer of selected type
  804. task_args.type = buf_type;
  805. TEST_ASSERT_MESSAGE(task_args.buffer != NULL, "Failed to create ring buffer");
  806. for (int prior_mod = -1; prior_mod < 2; prior_mod++) { //Test different relative priorities
  807. //Test every permutation of core affinity
  808. for (int send_core = 0; send_core < portNUM_PROCESSORS; send_core++) {
  809. for (int rec_core = 0; rec_core < portNUM_PROCESSORS; rec_core ++) {
  810. esp_rom_printf("Type: %d, PM: %d, SC: %d, RC: %d\n", buf_type, prior_mod, send_core, rec_core);
  811. xTaskCreatePinnedToCore(send_task, "send tsk", 2048, (void *)&task_args, 10 + prior_mod, NULL, send_core);
  812. xTaskCreatePinnedToCore(rec_task, "rec tsk", 2048, (void *)&task_args, 10, NULL, rec_core);
  813. xSemaphoreTake(tasks_done, portMAX_DELAY);
  814. vTaskDelay(5); //Allow idle to clean up
  815. }
  816. }
  817. }
  818. //Delete ring buffer
  819. vRingbufferDelete(task_args.buffer);
  820. vTaskDelay(10);
  821. }
  822. cleanup();
  823. }
  824. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  825. TEST_CASE("Test static ring buffer SMP", "[esp_ringbuf]")
  826. {
  827. setup();
  828. //Iterate through buffer types (No split, split, then byte buff)
  829. for (RingbufferType_t buf_type = 0; buf_type < RINGBUF_TYPE_MAX; buf_type++) {
  830. StaticRingbuffer_t *buffer_struct;
  831. uint8_t *buffer_storage;
  832. //Allocate memory and create semaphores
  833. #if CONFIG_SPIRAM_USE_CAPS_ALLOC //When SPIRAM can only be allocated using heap_caps_malloc()
  834. buffer_struct = (StaticRingbuffer_t *)heap_caps_malloc(sizeof(StaticRingbuffer_t), MALLOC_CAP_SPIRAM);
  835. buffer_storage = (uint8_t *)heap_caps_malloc(sizeof(uint8_t) * CONT_DATA_TEST_BUFF_LEN, MALLOC_CAP_SPIRAM);
  836. #else //Case where SPIRAM is disabled or when SPIRAM is allocatable through malloc()
  837. buffer_struct = (StaticRingbuffer_t *)malloc(sizeof(StaticRingbuffer_t));
  838. buffer_storage = (uint8_t *)malloc(sizeof(uint8_t) * CONT_DATA_TEST_BUFF_LEN);
  839. #endif
  840. TEST_ASSERT(buffer_struct != NULL && buffer_storage != NULL);
  841. //Create buffer
  842. task_args_t task_args;
  843. task_args.buffer = xRingbufferCreateStatic(CONT_DATA_TEST_BUFF_LEN, buf_type, buffer_storage, buffer_struct); //Create buffer of selected type
  844. task_args.type = buf_type;
  845. TEST_ASSERT_MESSAGE(task_args.buffer != NULL, "Failed to create ring buffer");
  846. for (int prior_mod = -1; prior_mod < 2; prior_mod++) { //Test different relative priorities
  847. //Test every permutation of core affinity
  848. for (int send_core = 0; send_core < portNUM_PROCESSORS; send_core++) {
  849. for (int rec_core = 0; rec_core < portNUM_PROCESSORS; rec_core ++) {
  850. esp_rom_printf("Type: %d, PM: %d, SC: %d, RC: %d\n", buf_type, prior_mod, send_core, rec_core);
  851. xTaskCreatePinnedToCore(send_task, "send tsk", 2048, (void *)&task_args, 10 + prior_mod, NULL, send_core);
  852. xTaskCreatePinnedToCore(rec_task, "rec tsk", 2048, (void *)&task_args, 10, NULL, rec_core);
  853. xSemaphoreTake(tasks_done, portMAX_DELAY);
  854. vTaskDelay(5); //Allow idle to clean up
  855. }
  856. }
  857. }
  858. //Delete ring buffer
  859. vRingbufferDelete(task_args.buffer);
  860. //Deallocate memory
  861. free(buffer_storage);
  862. free(buffer_struct);
  863. vTaskDelay(10);
  864. }
  865. cleanup();
  866. }
  867. #endif
  868. #if !CONFIG_RINGBUF_PLACE_FUNCTIONS_INTO_FLASH && !CONFIG_RINGBUF_PLACE_ISR_FUNCTIONS_INTO_FLASH
  869. /* -------------------------- Test ring buffer IRAM ------------------------- */
  870. static IRAM_ATTR __attribute__((noinline)) bool iram_ringbuf_test(void)
  871. {
  872. bool result = true;
  873. uint8_t item[4];
  874. size_t item_size;
  875. RingbufHandle_t handle = xRingbufferCreate(CONT_DATA_TEST_BUFF_LEN, RINGBUF_TYPE_NOSPLIT);
  876. result = result && (handle != NULL);
  877. spi_flash_guard_get()->start(); // Disables flash cache
  878. xRingbufferGetMaxItemSize(handle);
  879. xRingbufferSendFromISR(handle, (void *)item, sizeof(item), NULL);
  880. xRingbufferReceiveFromISR(handle, &item_size);
  881. spi_flash_guard_get()->end(); // Re-enables flash cache
  882. vRingbufferDelete(handle);
  883. return result;
  884. }
  885. TEST_CASE("Test ringbuffer functions work with flash cache disabled", "[esp_ringbuf]")
  886. {
  887. TEST_ASSERT( iram_ringbuf_test() );
  888. }
  889. #endif /* !CONFIG_RINGBUF_PLACE_FUNCTIONS_INTO_FLASH && !CONFIG_RINGBUF_PLACE_ISR_FUNCTIONS_INTO_FLASH */
  890. /* ------------------------ Test ring buffer 0 Item Size -----------------------
  891. * The following test case tests that sending/acquiring an item/bytes of 0 size
  892. * is permissible.
  893. */
  894. TEST_CASE("Test ringbuffer 0 item size", "[esp_ringbuf]")
  895. {
  896. RingbufHandle_t no_split_rb = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_NOSPLIT);
  897. RingbufHandle_t allow_split_rb = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_ALLOWSPLIT);
  898. RingbufHandle_t byte_rb = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_BYTEBUF);
  899. TEST_ASSERT_MESSAGE(no_split_rb && allow_split_rb && byte_rb, "Failed to create ring buffers");
  900. void *pvItem1;
  901. void *pvItem2;
  902. size_t xItemSize1;
  903. size_t xItemSize2;
  904. //Test that 0 item size on no split buffers should only send a header with no data
  905. TEST_ASSERT_EQUAL(pdTRUE, xRingbufferSend(no_split_rb, NULL, 0, 0));
  906. TEST_ASSERT_NOT_EQUAL(NULL, xRingbufferReceive(no_split_rb, &xItemSize1, 0));
  907. TEST_ASSERT_EQUAL(0, xItemSize1);
  908. //Test that acquiring 0 item size on no split buffers should only send a header without reserving a data buffer
  909. TEST_ASSERT_EQUAL(pdTRUE, xRingbufferSendAcquire(no_split_rb, &pvItem1, 0, 0));
  910. TEST_ASSERT_NOT_EQUAL(NULL, pvItem1);
  911. TEST_ASSERT_EQUAL(pdTRUE, xRingbufferSendComplete(no_split_rb, pvItem1));
  912. TEST_ASSERT_NOT_EQUAL(NULL, xRingbufferReceive(no_split_rb, &xItemSize1, 0));
  913. TEST_ASSERT_EQUAL(0, xItemSize1);
  914. //Test that 0 item size on allow split buffers should only send a header with no data
  915. TEST_ASSERT_EQUAL(pdTRUE, xRingbufferSend(allow_split_rb, NULL, 0, 0));
  916. TEST_ASSERT_EQUAL(pdTRUE, xRingbufferReceiveSplit(allow_split_rb, &pvItem1, &pvItem2, &xItemSize1, &xItemSize2, 0));
  917. TEST_ASSERT_NOT_EQUAL(NULL, pvItem1);
  918. TEST_ASSERT_EQUAL(NULL, pvItem2);
  919. TEST_ASSERT_EQUAL(0, xItemSize1);
  920. //Test that 0 item size on byte buffers should send nothing
  921. TEST_ASSERT_EQUAL(pdTRUE, xRingbufferSend(byte_rb, NULL, 0, 0));
  922. TEST_ASSERT_EQUAL(pdFALSE, xRingbufferReceiveUpTo(byte_rb, &xItemSize1, 0, BUFFER_SIZE));
  923. //Cleanup
  924. vRingbufferDelete(no_split_rb);
  925. vRingbufferDelete(allow_split_rb);
  926. vRingbufferDelete(byte_rb);
  927. }
  928. /* --------------------- Test ring buffer create with caps ---------------------
  929. * The following test case tests ring buffer creation with caps. Specifically
  930. * the following APIs:
  931. *
  932. * - xRingbufferCreateWithCaps()
  933. * - vRingbufferDeleteWithCaps()
  934. * - xRingbufferGetStaticBuffer()
  935. */
  936. TEST_CASE("Test ringbuffer with caps", "[esp_ringbuf]")
  937. {
  938. RingbufHandle_t rb_handle;
  939. uint8_t *rb_storage;
  940. StaticRingbuffer_t *rb_obj;
  941. // Create ring buffer with caps
  942. rb_handle = xRingbufferCreateWithCaps(BUFFER_SIZE, RINGBUF_TYPE_NOSPLIT, (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT));
  943. TEST_ASSERT_NOT_EQUAL(NULL, rb_handle);
  944. // Get the ring buffer's memory
  945. TEST_ASSERT_EQUAL(pdTRUE, xRingbufferGetStaticBuffer(rb_handle, &rb_storage, &rb_obj));
  946. TEST_ASSERT(esp_ptr_in_dram(rb_storage));
  947. TEST_ASSERT(esp_ptr_in_dram(rb_obj));
  948. // Free the ring buffer
  949. vRingbufferDeleteWithCaps(rb_handle);
  950. }