test_ringbuf.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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_APB,
  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_start(gptimer));
  655. //Wait for ISR to complete multiple iterations
  656. xSemaphoreTake(done_sem, portMAX_DELAY);
  657. //Cleanup
  658. TEST_ESP_OK(gptimer_stop(gptimer));
  659. TEST_ESP_OK(gptimer_del_timer(gptimer));
  660. vSemaphoreDelete(done_sem);
  661. for (int i = 0; i < NO_OF_RB_TYPES; i++) {
  662. vRingbufferDelete(buffer_handles[i]);
  663. }
  664. }
  665. /* ---------------------------- Test ring buffer SMP ---------------------------
  666. * The following test case tests each type of ring buffer in an SMP fashion. A
  667. * sending task and a receiving task is created. The sending task will split
  668. * a continuous piece of data into items of random length and send it to a ring
  669. * buffer. The receiving task will receive and check those items.
  670. * Every permutation of core pinning of the sending and receiving task will be
  671. * tested.
  672. */
  673. #define SRAND_SEED 3 //Arbitrarily chosen srand() seed
  674. #define SMP_TEST_ITERATIONS 4
  675. static const char continuous_data[] = {"A_very_long_string_that_will_be_split_into_"
  676. "items_of_random_lengths_and_sent_to_the_ring_"
  677. "buffer._The_maximum_random_length_will_also_"
  678. "be_increased_over_multiple_iterations_in_this"
  679. "_test"
  680. };
  681. #define CONT_DATA_LEN sizeof(continuous_data)
  682. //32-bit aligned size that guarantees a wrap around at some point
  683. #define CONT_DATA_TEST_BUFF_LEN (((CONT_DATA_LEN/2) + 0x03) & ~0x3)
  684. typedef struct {
  685. RingbufHandle_t buffer;
  686. RingbufferType_t type;
  687. } task_args_t;
  688. static SemaphoreHandle_t tasks_done;
  689. static SemaphoreHandle_t tx_done;
  690. static SemaphoreHandle_t rx_done;
  691. static void send_to_buffer(RingbufHandle_t buffer, size_t max_item_size)
  692. {
  693. for (int iter = 0; iter < SMP_TEST_ITERATIONS; iter++) {
  694. size_t bytes_sent = 0; //Number of data bytes sent in this iteration
  695. size_t next_item_size; //Size of next item to send
  696. while (bytes_sent < CONT_DATA_LEN) {
  697. //Get size of next item
  698. next_item_size = rand() % (max_item_size + 1);
  699. if (next_item_size + bytes_sent > CONT_DATA_LEN) {
  700. next_item_size = CONT_DATA_LEN - bytes_sent;
  701. }
  702. //Send item
  703. TEST_ASSERT_MESSAGE(xRingbufferSend(buffer, (void *) & (continuous_data[bytes_sent]), next_item_size, TIMEOUT_TICKS) == pdTRUE, "Failed to send an item");
  704. bytes_sent += next_item_size;
  705. }
  706. xSemaphoreGive(tx_done);
  707. xSemaphoreTake(rx_done, portMAX_DELAY);
  708. }
  709. }
  710. static void read_from_buffer(RingbufHandle_t buffer, RingbufferType_t buf_type, size_t max_rec_size)
  711. {
  712. for (int iter = 0; iter < SMP_TEST_ITERATIONS; iter++) {
  713. size_t bytes_rec = 0; //Number of data bytes received in this iteration
  714. while (bytes_rec < CONT_DATA_LEN) {
  715. size_t item_size, item_size2; //Possible for allow split buffers to receive two items
  716. char *item_data, *item_data2;
  717. //Select appropriate receive function for type of ring buffer
  718. if (buf_type == RINGBUF_TYPE_NOSPLIT) {
  719. item_data = (char *)xRingbufferReceive(buffer, &item_size, TIMEOUT_TICKS);
  720. } else if (buf_type == RINGBUF_TYPE_ALLOWSPLIT) {
  721. BaseType_t ret = xRingbufferReceiveSplit(buffer, (void **)&item_data, (void **)&item_data2, &item_size, &item_size2, TIMEOUT_TICKS);
  722. TEST_ASSERT_MESSAGE(ret == pdTRUE, "Failed to receive any item");
  723. } else {
  724. item_data = (char *)xRingbufferReceiveUpTo(buffer, &item_size, TIMEOUT_TICKS, max_rec_size);
  725. }
  726. //Check received item and return it
  727. TEST_ASSERT_MESSAGE(item_data != NULL, "Failed to receive an item");
  728. if (buf_type == RINGBUF_TYPE_BYTEBUF) {
  729. TEST_ASSERT_MESSAGE(item_size <= max_rec_size, "Received data exceeds max size");
  730. }
  731. for (int i = 0; i < item_size; i++) {
  732. //Check item_data is valid
  733. TEST_ASSERT_MESSAGE(item_data[i] == continuous_data[bytes_rec + i], "Received data is corrupted");
  734. }
  735. bytes_rec += item_size;
  736. vRingbufferReturnItem(buffer, item_data);
  737. if (buf_type == RINGBUF_TYPE_ALLOWSPLIT && item_data2 != NULL) {
  738. //Check item_data2 is valid
  739. for (int i = 0; i < item_size2; i++) {
  740. TEST_ASSERT_MESSAGE(item_data2[i] == continuous_data[bytes_rec + i], "Received split data is corrupted");
  741. }
  742. bytes_rec += item_size2;
  743. vRingbufferReturnItem(buffer, item_data2);
  744. }
  745. }
  746. TEST_ASSERT_MESSAGE(bytes_rec == CONT_DATA_LEN, "Total length of received data is incorrect");
  747. xSemaphoreGive(rx_done);
  748. xSemaphoreTake(tx_done, portMAX_DELAY);
  749. }
  750. }
  751. static void send_task(void *args)
  752. {
  753. RingbufHandle_t buffer = ((task_args_t *)args)->buffer;
  754. size_t max_item_len = xRingbufferGetMaxItemSize(buffer);
  755. //Test sending short length items
  756. send_to_buffer(buffer, 1);
  757. //Test sending mid length items
  758. send_to_buffer(buffer, max_item_len / 2);
  759. //Test sending long length items
  760. send_to_buffer(buffer, max_item_len);
  761. vTaskDelete(NULL);
  762. }
  763. static void rec_task(void *args)
  764. {
  765. RingbufHandle_t buffer = ((task_args_t *)args)->buffer;
  766. size_t max_rec_len = xRingbufferGetMaxItemSize(buffer);
  767. //Test receiving short length items
  768. read_from_buffer(buffer, ((task_args_t *)args)->type, 1);
  769. //Test receiving mid length items
  770. read_from_buffer(buffer, ((task_args_t *)args)->type, max_rec_len / 2);
  771. //Test receiving long length items
  772. read_from_buffer(buffer, ((task_args_t *)args)->type, max_rec_len);
  773. xSemaphoreGive(tasks_done);
  774. vTaskDelete(NULL);
  775. }
  776. static void setup(void)
  777. {
  778. esp_rom_printf("Size of test data: %d\n", CONT_DATA_LEN);
  779. tx_done = xSemaphoreCreateBinary(); //Semaphore to indicate send is done for a particular iteration
  780. rx_done = xSemaphoreCreateBinary(); //Semaphore to indicate receive is done for a particular iteration
  781. tasks_done = xSemaphoreCreateBinary(); //Semaphore used to to indicate send and receive tasks completed running
  782. srand(SRAND_SEED); //Seed RNG
  783. }
  784. static void cleanup(void)
  785. {
  786. //Cleanup
  787. vSemaphoreDelete(tx_done);
  788. vSemaphoreDelete(rx_done);
  789. vSemaphoreDelete(tasks_done);
  790. }
  791. TEST_CASE("Test ring buffer SMP", "[esp_ringbuf]")
  792. {
  793. setup();
  794. //Iterate through buffer types (No split, split, then byte buff)
  795. for (RingbufferType_t buf_type = 0; buf_type < RINGBUF_TYPE_MAX; buf_type++) {
  796. //Create buffer
  797. task_args_t task_args;
  798. task_args.buffer = xRingbufferCreate(CONT_DATA_TEST_BUFF_LEN, buf_type); //Create buffer of selected type
  799. task_args.type = buf_type;
  800. TEST_ASSERT_MESSAGE(task_args.buffer != NULL, "Failed to create ring buffer");
  801. for (int prior_mod = -1; prior_mod < 2; prior_mod++) { //Test different relative priorities
  802. //Test every permutation of core affinity
  803. for (int send_core = 0; send_core < portNUM_PROCESSORS; send_core++) {
  804. for (int rec_core = 0; rec_core < portNUM_PROCESSORS; rec_core ++) {
  805. esp_rom_printf("Type: %d, PM: %d, SC: %d, RC: %d\n", buf_type, prior_mod, send_core, rec_core);
  806. xTaskCreatePinnedToCore(send_task, "send tsk", 2048, (void *)&task_args, 10 + prior_mod, NULL, send_core);
  807. xTaskCreatePinnedToCore(rec_task, "rec tsk", 2048, (void *)&task_args, 10, NULL, rec_core);
  808. xSemaphoreTake(tasks_done, portMAX_DELAY);
  809. vTaskDelay(5); //Allow idle to clean up
  810. }
  811. }
  812. }
  813. //Delete ring buffer
  814. vRingbufferDelete(task_args.buffer);
  815. vTaskDelay(10);
  816. }
  817. cleanup();
  818. }
  819. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  820. TEST_CASE("Test static ring buffer SMP", "[esp_ringbuf]")
  821. {
  822. setup();
  823. //Iterate through buffer types (No split, split, then byte buff)
  824. for (RingbufferType_t buf_type = 0; buf_type < RINGBUF_TYPE_MAX; buf_type++) {
  825. StaticRingbuffer_t *buffer_struct;
  826. uint8_t *buffer_storage;
  827. //Allocate memory and create semaphores
  828. #if CONFIG_SPIRAM_USE_CAPS_ALLOC //When SPIRAM can only be allocated using heap_caps_malloc()
  829. buffer_struct = (StaticRingbuffer_t *)heap_caps_malloc(sizeof(StaticRingbuffer_t), MALLOC_CAP_SPIRAM);
  830. buffer_storage = (uint8_t *)heap_caps_malloc(sizeof(uint8_t) * CONT_DATA_TEST_BUFF_LEN, MALLOC_CAP_SPIRAM);
  831. #else //Case where SPIRAM is disabled or when SPIRAM is allocatable through malloc()
  832. buffer_struct = (StaticRingbuffer_t *)malloc(sizeof(StaticRingbuffer_t));
  833. buffer_storage = (uint8_t *)malloc(sizeof(uint8_t) * CONT_DATA_TEST_BUFF_LEN);
  834. #endif
  835. TEST_ASSERT(buffer_struct != NULL && buffer_storage != NULL);
  836. //Create buffer
  837. task_args_t task_args;
  838. task_args.buffer = xRingbufferCreateStatic(CONT_DATA_TEST_BUFF_LEN, buf_type, buffer_storage, buffer_struct); //Create buffer of selected type
  839. task_args.type = buf_type;
  840. TEST_ASSERT_MESSAGE(task_args.buffer != NULL, "Failed to create ring buffer");
  841. for (int prior_mod = -1; prior_mod < 2; prior_mod++) { //Test different relative priorities
  842. //Test every permutation of core affinity
  843. for (int send_core = 0; send_core < portNUM_PROCESSORS; send_core++) {
  844. for (int rec_core = 0; rec_core < portNUM_PROCESSORS; rec_core ++) {
  845. esp_rom_printf("Type: %d, PM: %d, SC: %d, RC: %d\n", buf_type, prior_mod, send_core, rec_core);
  846. xTaskCreatePinnedToCore(send_task, "send tsk", 2048, (void *)&task_args, 10 + prior_mod, NULL, send_core);
  847. xTaskCreatePinnedToCore(rec_task, "rec tsk", 2048, (void *)&task_args, 10, NULL, rec_core);
  848. xSemaphoreTake(tasks_done, portMAX_DELAY);
  849. vTaskDelay(5); //Allow idle to clean up
  850. }
  851. }
  852. }
  853. //Delete ring buffer
  854. vRingbufferDelete(task_args.buffer);
  855. //Deallocate memory
  856. free(buffer_storage);
  857. free(buffer_struct);
  858. vTaskDelay(10);
  859. }
  860. cleanup();
  861. }
  862. #endif
  863. /* -------------------------- Test ring buffer IRAM ------------------------- */
  864. static IRAM_ATTR __attribute__((noinline)) bool iram_ringbuf_test(void)
  865. {
  866. bool result = true;
  867. RingbufHandle_t handle = xRingbufferCreate(CONT_DATA_TEST_BUFF_LEN, RINGBUF_TYPE_NOSPLIT);
  868. result = result && (handle != NULL);
  869. spi_flash_guard_get()->start(); // Disables flash cache
  870. xRingbufferGetMaxItemSize(handle);
  871. spi_flash_guard_get()->end(); // Re-enables flash cache
  872. vRingbufferDelete(handle);
  873. return result;
  874. }
  875. TEST_CASE("Test ringbuffer functions work with flash cache disabled", "[esp_ringbuf]")
  876. {
  877. TEST_ASSERT( iram_ringbuf_test() );
  878. }