test_ringbuf.c 46 KB

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