test_ringbuf.c 46 KB

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