test_ringbuf.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 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_heap_caps.h"
  16. #include "spi_flash_mmap.h"
  17. #include "unity.h"
  18. #include "test_utils.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, UNITY_FREERTOS_PRIORITY + 1, 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. }
  580. /* -------------------------- Test ring buffer ISR -----------------------------
  581. * The following test case tests ring buffer ISR API. A timer is used to trigger
  582. * the ISR. The test case will do the following
  583. * 1) ISR will be triggered periodically by timer
  584. * 2) The ISR will iterate through all ring buffer types where each iteration
  585. * will send then receive an item to a ring buffer.
  586. */
  587. #define ISR_ITERATIONS ((BUFFER_SIZE / SMALL_ITEM_SIZE) * 2)
  588. static int buf_type;
  589. static int iterations;
  590. static bool on_timer_alarm(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx)
  591. {
  592. bool need_yield = false;
  593. //Test sending to buffer from ISR from ISR
  594. if (buf_type < NO_OF_RB_TYPES) {
  595. send_item_and_check(buffer_handles[buf_type], (void *)small_item, SMALL_ITEM_SIZE, 0, true);
  596. }
  597. //Receive item from ISR
  598. if (buf_type == RINGBUF_TYPE_NOSPLIT) {
  599. //Test receive from ISR for no-split buffer
  600. receive_check_and_return_item_no_split(buffer_handles[buf_type], (void *)small_item, SMALL_ITEM_SIZE, 0, true);
  601. buf_type++;
  602. } else if (buf_type == RINGBUF_TYPE_ALLOWSPLIT) {
  603. //Test send from ISR to allow-split buffer
  604. receive_check_and_return_item_allow_split(buffer_handles[buf_type], (void *)small_item, SMALL_ITEM_SIZE, 0, true);
  605. buf_type++;
  606. } else if (buf_type == RINGBUF_TYPE_BYTEBUF) {
  607. //Test receive from ISR for byte buffer
  608. receive_check_and_return_item_byte_buffer(buffer_handles[buf_type], (void *)small_item, SMALL_ITEM_SIZE, 0, true);
  609. buf_type++;
  610. } else if (buf_type == NO_OF_RB_TYPES) {
  611. //Check if all iterations complete
  612. if (iterations < ISR_ITERATIONS) {
  613. iterations++;
  614. buf_type = 0; //Reset and iterate through each buffer type again
  615. goto out;
  616. } else {
  617. //Signal complete
  618. BaseType_t task_woken = pdFALSE;
  619. xSemaphoreGiveFromISR(done_sem, &task_woken);
  620. if (task_woken == pdTRUE) {
  621. buf_type++;
  622. need_yield = true;
  623. }
  624. }
  625. }
  626. out:
  627. return need_yield;
  628. }
  629. TEST_CASE("Test ring buffer ISR", "[esp_ringbuf]")
  630. {
  631. gptimer_handle_t gptimer;
  632. for (int i = 0; i < NO_OF_RB_TYPES; i++) {
  633. buffer_handles[i] = xRingbufferCreate(BUFFER_SIZE, i);
  634. }
  635. done_sem = xSemaphoreCreateBinary();
  636. buf_type = 0;
  637. iterations = 0;
  638. //Setup timer for ISR
  639. gptimer_config_t config = {
  640. .clk_src = GPTIMER_CLK_SRC_DEFAULT,
  641. .direction = GPTIMER_COUNT_UP,
  642. .resolution_hz = 1000000,
  643. };
  644. TEST_ESP_OK(gptimer_new_timer(&config, &gptimer));
  645. gptimer_alarm_config_t alarm_config = {
  646. .reload_count = 0,
  647. .alarm_count = 2000,
  648. .flags.auto_reload_on_alarm = true,
  649. };
  650. gptimer_event_callbacks_t cbs = {
  651. .on_alarm = on_timer_alarm,
  652. };
  653. TEST_ESP_OK(gptimer_register_event_callbacks(gptimer, &cbs, NULL));
  654. TEST_ESP_OK(gptimer_set_alarm_action(gptimer, &alarm_config));
  655. TEST_ESP_OK(gptimer_enable(gptimer));
  656. TEST_ESP_OK(gptimer_start(gptimer));
  657. //Wait for ISR to complete multiple iterations
  658. xSemaphoreTake(done_sem, portMAX_DELAY);
  659. //Cleanup
  660. TEST_ESP_OK(gptimer_stop(gptimer));
  661. TEST_ESP_OK(gptimer_disable(gptimer));
  662. TEST_ESP_OK(gptimer_del_timer(gptimer));
  663. vSemaphoreDelete(done_sem);
  664. for (int i = 0; i < NO_OF_RB_TYPES; i++) {
  665. vRingbufferDelete(buffer_handles[i]);
  666. }
  667. }
  668. /* ---------------------------- Test ring buffer SMP ---------------------------
  669. * The following test case tests each type of ring buffer in an SMP fashion. A
  670. * sending task and a receiving task is created. The sending task will split
  671. * a continuous piece of data into items of random length and send it to a ring
  672. * buffer. The receiving task will receive and check those items.
  673. * Every permutation of core pinning of the sending and receiving task will be
  674. * tested.
  675. */
  676. #define SRAND_SEED 3 //Arbitrarily chosen srand() seed
  677. #define SMP_TEST_ITERATIONS 4
  678. static const char continuous_data[] = {"A_very_long_string_that_will_be_split_into_"
  679. "items_of_random_lengths_and_sent_to_the_ring_"
  680. "buffer._The_maximum_random_length_will_also_"
  681. "be_increased_over_multiple_iterations_in_this"
  682. "_test"
  683. };
  684. #define CONT_DATA_LEN sizeof(continuous_data)
  685. //32-bit aligned size that guarantees a wrap around at some point
  686. #define CONT_DATA_TEST_BUFF_LEN (((CONT_DATA_LEN/2) + 0x03) & ~0x3)
  687. typedef struct {
  688. RingbufHandle_t buffer;
  689. RingbufferType_t type;
  690. } task_args_t;
  691. static SemaphoreHandle_t tasks_done;
  692. static SemaphoreHandle_t tx_done;
  693. static SemaphoreHandle_t rx_done;
  694. static void send_to_buffer(RingbufHandle_t buffer, size_t max_item_size)
  695. {
  696. for (int iter = 0; iter < SMP_TEST_ITERATIONS; iter++) {
  697. size_t bytes_sent = 0; //Number of data bytes sent in this iteration
  698. size_t next_item_size; //Size of next item to send
  699. while (bytes_sent < CONT_DATA_LEN) {
  700. //Get size of next item
  701. next_item_size = rand() % (max_item_size + 1);
  702. if (next_item_size + bytes_sent > CONT_DATA_LEN) {
  703. next_item_size = CONT_DATA_LEN - bytes_sent;
  704. }
  705. //Send item
  706. TEST_ASSERT_MESSAGE(xRingbufferSend(buffer, (void *) & (continuous_data[bytes_sent]), next_item_size, TIMEOUT_TICKS) == pdTRUE, "Failed to send an item");
  707. bytes_sent += next_item_size;
  708. }
  709. xSemaphoreGive(tx_done);
  710. xSemaphoreTake(rx_done, portMAX_DELAY);
  711. }
  712. }
  713. static void read_from_buffer(RingbufHandle_t buffer, RingbufferType_t buf_type, size_t max_rec_size)
  714. {
  715. for (int iter = 0; iter < SMP_TEST_ITERATIONS; iter++) {
  716. size_t bytes_rec = 0; //Number of data bytes received in this iteration
  717. while (bytes_rec < CONT_DATA_LEN) {
  718. size_t item_size, item_size2; //Possible for allow split buffers to receive two items
  719. char *item_data, *item_data2;
  720. //Select appropriate receive function for type of ring buffer
  721. if (buf_type == RINGBUF_TYPE_NOSPLIT) {
  722. item_data = (char *)xRingbufferReceive(buffer, &item_size, TIMEOUT_TICKS);
  723. } else if (buf_type == RINGBUF_TYPE_ALLOWSPLIT) {
  724. BaseType_t ret = xRingbufferReceiveSplit(buffer, (void **)&item_data, (void **)&item_data2, &item_size, &item_size2, TIMEOUT_TICKS);
  725. TEST_ASSERT_MESSAGE(ret == pdTRUE, "Failed to receive any item");
  726. } else {
  727. item_data = (char *)xRingbufferReceiveUpTo(buffer, &item_size, TIMEOUT_TICKS, max_rec_size);
  728. }
  729. //Check received item and return it
  730. TEST_ASSERT_MESSAGE(item_data != NULL, "Failed to receive an item");
  731. if (buf_type == RINGBUF_TYPE_BYTEBUF) {
  732. TEST_ASSERT_MESSAGE(item_size <= max_rec_size, "Received data exceeds max size");
  733. }
  734. for (int i = 0; i < item_size; i++) {
  735. //Check item_data is valid
  736. TEST_ASSERT_MESSAGE(item_data[i] == continuous_data[bytes_rec + i], "Received data is corrupted");
  737. }
  738. bytes_rec += item_size;
  739. vRingbufferReturnItem(buffer, item_data);
  740. if (buf_type == RINGBUF_TYPE_ALLOWSPLIT && item_data2 != NULL) {
  741. //Check item_data2 is valid
  742. for (int i = 0; i < item_size2; i++) {
  743. TEST_ASSERT_MESSAGE(item_data2[i] == continuous_data[bytes_rec + i], "Received split data is corrupted");
  744. }
  745. bytes_rec += item_size2;
  746. vRingbufferReturnItem(buffer, item_data2);
  747. }
  748. }
  749. TEST_ASSERT_MESSAGE(bytes_rec == CONT_DATA_LEN, "Total length of received data is incorrect");
  750. xSemaphoreGive(rx_done);
  751. xSemaphoreTake(tx_done, portMAX_DELAY);
  752. }
  753. }
  754. static void send_task(void *args)
  755. {
  756. RingbufHandle_t buffer = ((task_args_t *)args)->buffer;
  757. size_t max_item_len = xRingbufferGetMaxItemSize(buffer);
  758. //Test sending short length items
  759. send_to_buffer(buffer, 1);
  760. //Test sending mid length items
  761. send_to_buffer(buffer, max_item_len / 2);
  762. //Test sending long length items
  763. send_to_buffer(buffer, max_item_len);
  764. vTaskDelete(NULL);
  765. }
  766. static void rec_task(void *args)
  767. {
  768. RingbufHandle_t buffer = ((task_args_t *)args)->buffer;
  769. size_t max_rec_len = xRingbufferGetMaxItemSize(buffer);
  770. //Test receiving short length items
  771. read_from_buffer(buffer, ((task_args_t *)args)->type, 1);
  772. //Test receiving mid length items
  773. read_from_buffer(buffer, ((task_args_t *)args)->type, max_rec_len / 2);
  774. //Test receiving long length items
  775. read_from_buffer(buffer, ((task_args_t *)args)->type, max_rec_len);
  776. xSemaphoreGive(tasks_done);
  777. vTaskDelete(NULL);
  778. }
  779. static void setup(void)
  780. {
  781. esp_rom_printf("Size of test data: %d\n", CONT_DATA_LEN);
  782. tx_done = xSemaphoreCreateBinary(); //Semaphore to indicate send is done for a particular iteration
  783. rx_done = xSemaphoreCreateBinary(); //Semaphore to indicate receive is done for a particular iteration
  784. tasks_done = xSemaphoreCreateBinary(); //Semaphore used to to indicate send and receive tasks completed running
  785. srand(SRAND_SEED); //Seed RNG
  786. }
  787. static void cleanup(void)
  788. {
  789. //Cleanup
  790. vSemaphoreDelete(tx_done);
  791. vSemaphoreDelete(rx_done);
  792. vSemaphoreDelete(tasks_done);
  793. }
  794. TEST_CASE("Test ring buffer SMP", "[esp_ringbuf]")
  795. {
  796. setup();
  797. //Iterate through buffer types (No split, split, then byte buff)
  798. for (RingbufferType_t buf_type = 0; buf_type < RINGBUF_TYPE_MAX; buf_type++) {
  799. //Create buffer
  800. task_args_t task_args;
  801. task_args.buffer = xRingbufferCreate(CONT_DATA_TEST_BUFF_LEN, buf_type); //Create buffer of selected type
  802. task_args.type = buf_type;
  803. TEST_ASSERT_MESSAGE(task_args.buffer != NULL, "Failed to create ring buffer");
  804. for (int prior_mod = -1; prior_mod < 2; prior_mod++) { //Test different relative priorities
  805. //Test every permutation of core affinity
  806. for (int send_core = 0; send_core < portNUM_PROCESSORS; send_core++) {
  807. for (int rec_core = 0; rec_core < portNUM_PROCESSORS; rec_core ++) {
  808. esp_rom_printf("Type: %d, PM: %d, SC: %d, RC: %d\n", buf_type, prior_mod, send_core, rec_core);
  809. xTaskCreatePinnedToCore(send_task, "send tsk", 2048, (void *)&task_args, 10 + prior_mod, NULL, send_core);
  810. xTaskCreatePinnedToCore(rec_task, "rec tsk", 2048, (void *)&task_args, 10, NULL, rec_core);
  811. xSemaphoreTake(tasks_done, portMAX_DELAY);
  812. vTaskDelay(5); //Allow idle to clean up
  813. }
  814. }
  815. }
  816. //Delete ring buffer
  817. vRingbufferDelete(task_args.buffer);
  818. vTaskDelay(10);
  819. }
  820. cleanup();
  821. }
  822. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  823. TEST_CASE("Test static ring buffer SMP", "[esp_ringbuf]")
  824. {
  825. setup();
  826. //Iterate through buffer types (No split, split, then byte buff)
  827. for (RingbufferType_t buf_type = 0; buf_type < RINGBUF_TYPE_MAX; buf_type++) {
  828. StaticRingbuffer_t *buffer_struct;
  829. uint8_t *buffer_storage;
  830. //Allocate memory and create semaphores
  831. #if CONFIG_SPIRAM_USE_CAPS_ALLOC //When SPIRAM can only be allocated using heap_caps_malloc()
  832. buffer_struct = (StaticRingbuffer_t *)heap_caps_malloc(sizeof(StaticRingbuffer_t), MALLOC_CAP_SPIRAM);
  833. buffer_storage = (uint8_t *)heap_caps_malloc(sizeof(uint8_t) * CONT_DATA_TEST_BUFF_LEN, MALLOC_CAP_SPIRAM);
  834. #else //Case where SPIRAM is disabled or when SPIRAM is allocatable through malloc()
  835. buffer_struct = (StaticRingbuffer_t *)malloc(sizeof(StaticRingbuffer_t));
  836. buffer_storage = (uint8_t *)malloc(sizeof(uint8_t) * CONT_DATA_TEST_BUFF_LEN);
  837. #endif
  838. TEST_ASSERT(buffer_struct != NULL && buffer_storage != NULL);
  839. //Create buffer
  840. task_args_t task_args;
  841. task_args.buffer = xRingbufferCreateStatic(CONT_DATA_TEST_BUFF_LEN, buf_type, buffer_storage, buffer_struct); //Create buffer of selected type
  842. task_args.type = buf_type;
  843. TEST_ASSERT_MESSAGE(task_args.buffer != NULL, "Failed to create ring buffer");
  844. for (int prior_mod = -1; prior_mod < 2; prior_mod++) { //Test different relative priorities
  845. //Test every permutation of core affinity
  846. for (int send_core = 0; send_core < portNUM_PROCESSORS; send_core++) {
  847. for (int rec_core = 0; rec_core < portNUM_PROCESSORS; rec_core ++) {
  848. esp_rom_printf("Type: %d, PM: %d, SC: %d, RC: %d\n", buf_type, prior_mod, send_core, rec_core);
  849. xTaskCreatePinnedToCore(send_task, "send tsk", 2048, (void *)&task_args, 10 + prior_mod, NULL, send_core);
  850. xTaskCreatePinnedToCore(rec_task, "rec tsk", 2048, (void *)&task_args, 10, NULL, rec_core);
  851. xSemaphoreTake(tasks_done, portMAX_DELAY);
  852. vTaskDelay(5); //Allow idle to clean up
  853. }
  854. }
  855. }
  856. //Delete ring buffer
  857. vRingbufferDelete(task_args.buffer);
  858. //Deallocate memory
  859. free(buffer_storage);
  860. free(buffer_struct);
  861. vTaskDelay(10);
  862. }
  863. cleanup();
  864. }
  865. #endif
  866. /* -------------------------- Test ring buffer IRAM ------------------------- */
  867. static IRAM_ATTR __attribute__((noinline)) bool iram_ringbuf_test(void)
  868. {
  869. bool result = true;
  870. uint8_t item[4];
  871. size_t item_size;
  872. RingbufHandle_t handle = xRingbufferCreate(CONT_DATA_TEST_BUFF_LEN, RINGBUF_TYPE_NOSPLIT);
  873. result = result && (handle != NULL);
  874. spi_flash_guard_get()->start(); // Disables flash cache
  875. xRingbufferGetMaxItemSize(handle);
  876. xRingbufferSendFromISR(handle, (void *)item, sizeof(item), NULL);
  877. xRingbufferReceiveFromISR(handle, &item_size);
  878. spi_flash_guard_get()->end(); // Re-enables flash cache
  879. vRingbufferDelete(handle);
  880. return result;
  881. }
  882. TEST_CASE("Test ringbuffer functions work with flash cache disabled", "[esp_ringbuf]")
  883. {
  884. TEST_ASSERT( iram_ringbuf_test() );
  885. }