test_ringbuf.c 46 KB

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