test_ringbuf.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. #include "freertos/queue.h"
  6. #include "freertos/semphr.h"
  7. #include "freertos/ringbuf.h"
  8. #include "driver/timer.h"
  9. #include "esp_heap_caps.h"
  10. #include "esp_spi_flash.h"
  11. #include "unity.h"
  12. #include "test_utils.h"
  13. #include "esp_rom_sys.h"
  14. //Definitions used in multiple test cases
  15. #define TIMEOUT_TICKS 10
  16. #define NO_OF_RB_TYPES 3
  17. #define ITEM_HDR_SIZE 8
  18. #define SMALL_ITEM_SIZE 8
  19. #define LARGE_ITEM_SIZE (2 * SMALL_ITEM_SIZE)
  20. #define BUFFER_SIZE 160 //4Byte aligned size
  21. static const uint8_t small_item[SMALL_ITEM_SIZE] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  22. static const uint8_t large_item[LARGE_ITEM_SIZE] = { 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  23. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17};
  24. static RingbufHandle_t buffer_handles[NO_OF_RB_TYPES];
  25. static SemaphoreHandle_t done_sem;
  26. 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)
  27. {
  28. BaseType_t ret;
  29. if (in_isr) {
  30. ret = xRingbufferSendFromISR(handle, (void *)item, item_size, NULL);
  31. } else {
  32. ret = xRingbufferSend(handle, (void *)item, item_size, ticks_to_wait);
  33. }
  34. TEST_ASSERT_MESSAGE(ret == pdTRUE, "Failed to send item");
  35. }
  36. 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)
  37. {
  38. //Receive item from no-split buffer
  39. size_t item_size;
  40. uint8_t *item;
  41. if (in_isr) {
  42. item = (uint8_t *)xRingbufferReceiveFromISR(handle, &item_size);
  43. } else {
  44. item = (uint8_t *)xRingbufferReceive(handle, &item_size, ticks_to_wait);
  45. }
  46. TEST_ASSERT_MESSAGE(item != NULL, "Failed to receive item");
  47. TEST_ASSERT_MESSAGE(item_size == expected_size, "Item size is incorrect");
  48. //Check data of received item
  49. for (int i = 0; i < item_size; i++) {
  50. TEST_ASSERT_MESSAGE(item[i] == expected_data[i], "Item data is invalid");
  51. }
  52. //Return item
  53. if (in_isr) {
  54. vRingbufferReturnItemFromISR(handle, (void *)item, NULL);
  55. } else {
  56. vRingbufferReturnItem(handle, (void *)item);
  57. }
  58. }
  59. 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)
  60. {
  61. //Receive item
  62. size_t item_size1, item_size2;
  63. uint8_t *item1, *item2;
  64. BaseType_t ret;
  65. if (in_isr) {
  66. ret = xRingbufferReceiveSplitFromISR(handle, (void**)&item1, (void **)&item2, &item_size1, &item_size2);
  67. } else {
  68. ret = xRingbufferReceiveSplit(handle, (void**)&item1, (void **)&item2, &item_size1, &item_size2, ticks_to_wait);
  69. }
  70. //= xRingbufferReceiveSplit(handle, (void**)&item1, (void **)&item2, &item_size1, &item_size2, ticks_to_wait);
  71. TEST_ASSERT_MESSAGE(ret == pdTRUE, "Failed to receive item");
  72. TEST_ASSERT_MESSAGE(item1 != NULL, "Failed to receive item");
  73. //Check data of received item(s) and return them
  74. if (item2 == NULL) {
  75. TEST_ASSERT_MESSAGE(item_size1 == expected_size, "Item size is incorrect");
  76. for (int i = 0; i < item_size1; i++) {
  77. TEST_ASSERT_MESSAGE(item1[i] == expected_data[i], "Item data is invalid");
  78. }
  79. //Return item
  80. if (in_isr) {
  81. vRingbufferReturnItemFromISR(handle, (void *)item1, NULL);
  82. } else {
  83. vRingbufferReturnItem(handle, (void *)item1);
  84. }
  85. } else {
  86. //Item was split
  87. TEST_ASSERT_MESSAGE(item_size1 + item_size2 == expected_size, "Total item size is incorrect");
  88. for (int i = 0; i < item_size1; i++) {
  89. TEST_ASSERT_MESSAGE(item1[i] == expected_data[i], "Head item data is invalid");
  90. }
  91. for (int i = 0; i < item_size2; i++) {
  92. TEST_ASSERT_MESSAGE(item2[i] == expected_data[item_size1 + i], "Head item data is invalid");
  93. }
  94. //Return Items
  95. if (in_isr) {
  96. vRingbufferReturnItemFromISR(handle, (void *)item1, NULL);
  97. vRingbufferReturnItemFromISR(handle, (void *)item2, NULL);
  98. } else {
  99. vRingbufferReturnItem(handle, (void *)item1);
  100. vRingbufferReturnItem(handle, (void *)item2);
  101. }
  102. }
  103. }
  104. 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)
  105. {
  106. //Receive item
  107. size_t item_size;
  108. uint8_t *item;
  109. if (in_isr) {
  110. item = (uint8_t *)xRingbufferReceiveUpToFromISR(handle, &item_size, expected_size);
  111. } else {
  112. item = (uint8_t *)xRingbufferReceiveUpTo(handle, &item_size, ticks_to_wait, expected_size); //Limit amount of bytes returned to the size of one item
  113. }
  114. TEST_ASSERT_MESSAGE(item != NULL, "Failed to receive item");
  115. //Check data of received item
  116. for (int i = 0; i < item_size; i++) {
  117. TEST_ASSERT_MESSAGE(item[i] == expected_data[i], "Item data is invalid");
  118. }
  119. //Return item
  120. if (in_isr) {
  121. vRingbufferReturnItemFromISR(handle, (void *)item, NULL);
  122. } else {
  123. vRingbufferReturnItem(handle, (void *)item);
  124. }
  125. //Check if item wrapped around
  126. if (item_size < expected_size) {
  127. //Item is wrapped, receive second portion
  128. size_t item_size2;
  129. uint8_t *item2;
  130. if (in_isr) {
  131. item2 = (uint8_t *)xRingbufferReceiveUpToFromISR(handle, &item_size2, expected_size - item_size);
  132. } else {
  133. item2 = (uint8_t *)xRingbufferReceiveUpTo(handle, &item_size2, ticks_to_wait, expected_size - item_size);
  134. }
  135. //= (uint8_t *)xRingbufferReceiveUpTo(handle, &item_size2, ticks_to_wait, expected_size - item_size);
  136. TEST_ASSERT_MESSAGE(item2 != NULL, "Failed to receive item");
  137. TEST_ASSERT_MESSAGE(item_size + item_size2 == expected_size, "Total item size is incorrect");
  138. for (int i = 0; i < item_size2; i++) {
  139. TEST_ASSERT_MESSAGE(item2[i] == expected_data[item_size + i], "Item data is invalid");
  140. }
  141. if (in_isr) {
  142. vRingbufferReturnItemFromISR(handle, (void *)item2, NULL);
  143. } else {
  144. vRingbufferReturnItem(handle, (void *)item2);
  145. }
  146. } else {
  147. TEST_ASSERT_MESSAGE(item_size == expected_size, "Item size is incorrect");
  148. }
  149. }
  150. /* ----------------- Basic ring buffer behavior tests cases --------------------
  151. * The following test cases will test basic send, receive, and wrap around
  152. * behavior of each type of ring buffer. Each test case will do the following
  153. * 1) Send multiple items (nearly fill the buffer)
  154. * 2) Receive and check the sent items (also prepares the buffer for a wrap around
  155. * 3) Send a final item that causes a wrap around
  156. * 4) Receive and check the wrapped item
  157. */
  158. TEST_CASE("Test ring buffer No-Split", "[esp_ringbuf]")
  159. {
  160. //Create buffer
  161. RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_NOSPLIT);
  162. TEST_ASSERT_MESSAGE(buffer_handle != NULL, "Failed to create ring buffer");
  163. //Calculate number of items to send. Aim to almost fill buffer to setup for wrap around
  164. int no_of_items = (BUFFER_SIZE - (ITEM_HDR_SIZE + SMALL_ITEM_SIZE)) / (ITEM_HDR_SIZE + SMALL_ITEM_SIZE);
  165. //Test sending items
  166. for (int i = 0; i < no_of_items; i++) {
  167. send_item_and_check(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  168. }
  169. //Test receiving items
  170. for (int i = 0; i < no_of_items; i++) {
  171. receive_check_and_return_item_no_split(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  172. }
  173. //Write pointer should be near the end, test wrap around
  174. UBaseType_t write_pos_before, write_pos_after;
  175. vRingbufferGetInfo(buffer_handle, NULL, NULL, &write_pos_before, NULL, NULL);
  176. //Send large item that causes wrap around
  177. send_item_and_check(buffer_handle, large_item, LARGE_ITEM_SIZE, TIMEOUT_TICKS, false);
  178. //Receive wrapped item
  179. receive_check_and_return_item_no_split(buffer_handle, large_item, LARGE_ITEM_SIZE, TIMEOUT_TICKS, false);
  180. vRingbufferGetInfo(buffer_handle, NULL, NULL, &write_pos_after, NULL, NULL);
  181. TEST_ASSERT_MESSAGE(write_pos_after < write_pos_before, "Failed to wrap around");
  182. //Cleanup
  183. vRingbufferDelete(buffer_handle);
  184. }
  185. TEST_CASE("Test ring buffer Allow-Split", "[esp_ringbuf]")
  186. {
  187. //Create buffer
  188. RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_ALLOWSPLIT);
  189. TEST_ASSERT_MESSAGE(buffer_handle != NULL, "Failed to create ring buffer");
  190. //Calculate number of items to send. Aim to almost fill buffer to setup for wrap around
  191. int no_of_items = (BUFFER_SIZE - (ITEM_HDR_SIZE + SMALL_ITEM_SIZE)) / (ITEM_HDR_SIZE + SMALL_ITEM_SIZE);
  192. //Test sending items
  193. for (int i = 0; i < no_of_items; i++) {
  194. send_item_and_check(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  195. }
  196. //Test receiving items
  197. for (int i = 0; i < no_of_items; i++) {
  198. receive_check_and_return_item_allow_split(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  199. }
  200. //Write pointer should be near the end, test wrap around
  201. UBaseType_t write_pos_before, write_pos_after;
  202. vRingbufferGetInfo(buffer_handle, NULL, NULL, &write_pos_before, NULL, NULL);
  203. //Send large item that causes wrap around
  204. send_item_and_check(buffer_handle, large_item, LARGE_ITEM_SIZE, TIMEOUT_TICKS, false);
  205. //Receive wrapped item
  206. receive_check_and_return_item_allow_split(buffer_handle, large_item, LARGE_ITEM_SIZE, TIMEOUT_TICKS, false);
  207. vRingbufferGetInfo(buffer_handle, NULL, NULL, &write_pos_after, NULL, NULL);
  208. TEST_ASSERT_MESSAGE(write_pos_after < write_pos_before, "Failed to wrap around");
  209. //Cleanup
  210. vRingbufferDelete(buffer_handle);
  211. }
  212. TEST_CASE("Test ring buffer Byte Buffer", "[esp_ringbuf]")
  213. {
  214. //Create buffer
  215. RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_BYTEBUF);
  216. TEST_ASSERT_MESSAGE(buffer_handle != NULL, "Failed to create ring buffer");
  217. //Calculate number of items to send. Aim to almost fill buffer to setup for wrap around
  218. int no_of_items = (BUFFER_SIZE - SMALL_ITEM_SIZE) / SMALL_ITEM_SIZE;
  219. //Test sending items
  220. for (int i = 0; i < no_of_items; i++) {
  221. send_item_and_check(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  222. }
  223. //Test receiving items
  224. for (int i = 0; i < no_of_items; i++) {
  225. receive_check_and_return_item_byte_buffer(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  226. }
  227. //Write pointer should be near the end, test wrap around
  228. UBaseType_t write_pos_before, write_pos_after;
  229. vRingbufferGetInfo(buffer_handle, NULL, NULL, &write_pos_before, NULL, NULL);
  230. //Send large item that causes wrap around
  231. send_item_and_check(buffer_handle, large_item, LARGE_ITEM_SIZE, TIMEOUT_TICKS, false);
  232. //Receive wrapped item
  233. receive_check_and_return_item_byte_buffer(buffer_handle, large_item, LARGE_ITEM_SIZE, TIMEOUT_TICKS, false);
  234. vRingbufferGetInfo(buffer_handle, NULL, NULL, &write_pos_after, NULL, NULL);
  235. TEST_ASSERT_MESSAGE(write_pos_after < write_pos_before, "Failed to wrap around");
  236. //Cleanup
  237. vRingbufferDelete(buffer_handle);
  238. }
  239. /* ----------------------- Ring buffer queue sets test ------------------------
  240. * The following test case will test receiving from ring buffers that have been
  241. * added to a queue set. The test case will do the following...
  242. * 1) Ring buffer of each type is created and added to the queue set
  243. * 2) A receiving task is created to select from the queue set and read from the appropriate ring buffer
  244. */
  245. static void queue_set_receiving_task(void *queue_set_handle)
  246. {
  247. QueueSetHandle_t queue_set = (QueueSetHandle_t)queue_set_handle;
  248. //Receive multiple items via queue set
  249. BaseType_t done = pdFALSE;
  250. int no_of_items = BUFFER_SIZE / SMALL_ITEM_SIZE;
  251. int items_rec_count[NO_OF_RB_TYPES] = {0};
  252. while (done != pdTRUE) {
  253. xQueueSetMemberHandle member = xQueueSelectFromSet(queue_set, TIMEOUT_TICKS);
  254. //Read from selected ring buffer
  255. if (xRingbufferCanRead(buffer_handles[0], member) == pdTRUE) {
  256. //No-split buffer
  257. receive_check_and_return_item_no_split(buffer_handles[0], small_item, SMALL_ITEM_SIZE, 0, false);
  258. items_rec_count[0] ++;
  259. } else if (xRingbufferCanRead(buffer_handles[1], member) == pdTRUE) {
  260. //Allow-split buffer
  261. receive_check_and_return_item_allow_split(buffer_handles[1], small_item, SMALL_ITEM_SIZE, 0, false);
  262. items_rec_count[1] ++;
  263. } else if (xRingbufferCanRead(buffer_handles[2], member) == pdTRUE){
  264. //Byte buffer
  265. receive_check_and_return_item_byte_buffer(buffer_handles[2], small_item, SMALL_ITEM_SIZE, 0, false);
  266. items_rec_count[2] ++;
  267. } else {
  268. TEST_ASSERT_MESSAGE( false, "Error with queue set member");
  269. }
  270. //Check for completion
  271. if (items_rec_count[0] == no_of_items &&
  272. items_rec_count[1] == no_of_items &&
  273. items_rec_count[2] == no_of_items) {
  274. done = pdTRUE;
  275. }
  276. }
  277. xSemaphoreGive(done_sem);
  278. vTaskDelete(NULL);
  279. }
  280. TEST_CASE("Test ring buffer with queue sets", "[esp_ringbuf]")
  281. {
  282. QueueSetHandle_t queue_set = xQueueCreateSet(NO_OF_RB_TYPES);
  283. done_sem = xSemaphoreCreateBinary();
  284. //Create ring buffer of each type, then add them to a queue set
  285. for (int i = 0; i < NO_OF_RB_TYPES; i++) {
  286. buffer_handles[i] = xRingbufferCreate(BUFFER_SIZE, i);
  287. TEST_ASSERT_MESSAGE(buffer_handles[i] != NULL, "Failed to create ring buffer");
  288. TEST_ASSERT_MESSAGE(xRingbufferAddToQueueSetRead(buffer_handles[i], queue_set) == pdPASS, "Failed to add to read queue set");
  289. }
  290. //Create a task to send items to each ring buffer
  291. int no_of_items = BUFFER_SIZE / SMALL_ITEM_SIZE;
  292. xTaskCreatePinnedToCore(queue_set_receiving_task, "rec tsk", 2048, (void *)queue_set, UNITY_FREERTOS_PRIORITY + 1 , NULL, 0);
  293. //Send multiple items to each type of ring buffer
  294. for (int i = 0; i < no_of_items; i++) {
  295. for (int j = 0; j < NO_OF_RB_TYPES; j++) {
  296. send_item_and_check(buffer_handles[j], small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
  297. }
  298. }
  299. xSemaphoreTake(done_sem, portMAX_DELAY);
  300. vSemaphoreDelete(done_sem);
  301. //Remove and delete ring buffers from queue sets
  302. for (int i = 0; i < NO_OF_RB_TYPES; i++) {
  303. TEST_ASSERT_MESSAGE(xRingbufferRemoveFromQueueSetRead(buffer_handles[i], queue_set) == pdTRUE, "Failed to remove from read queue set");
  304. vRingbufferDelete(buffer_handles[i]);
  305. }
  306. vQueueDelete(queue_set);
  307. }
  308. /* -------------------------- Test ring buffer ISR -----------------------------
  309. * The following test case tests ring buffer ISR API. A timer is used to trigger
  310. * the ISR. The test case will do the following
  311. * 1) ISR will be triggered periodically by timer
  312. * 2) The ISR will iterate through all ring buffer types where each iteration
  313. * will send then receive an item to a ring buffer.
  314. */
  315. #define TIMER_GROUP 0
  316. #define TIMER_NUMBER 0
  317. #define ISR_ITERATIONS ((BUFFER_SIZE / SMALL_ITEM_SIZE) * 2)
  318. intr_handle_t ringbuffer_isr_handle;
  319. static int buf_type;
  320. static int iterations;
  321. static void ringbuffer_isr(void *arg)
  322. {
  323. //Clear timer interrupt
  324. timer_group_clr_intr_status_in_isr(TIMER_GROUP_0, TIMER_0);
  325. timer_group_enable_alarm_in_isr(TIMER_GROUP_0, xPortGetCoreID());
  326. //Test sending to buffer from ISR from ISR
  327. if (buf_type < NO_OF_RB_TYPES) {
  328. send_item_and_check(buffer_handles[buf_type], (void *)small_item, SMALL_ITEM_SIZE, 0, true);
  329. }
  330. //Receive item from ISR
  331. if (buf_type == RINGBUF_TYPE_NOSPLIT) {
  332. //Test receive from ISR for no-split buffer
  333. receive_check_and_return_item_no_split(buffer_handles[buf_type], (void *)small_item, SMALL_ITEM_SIZE, 0, true);
  334. buf_type++;
  335. } else if (buf_type == RINGBUF_TYPE_ALLOWSPLIT) {
  336. //Test send from ISR to allow-split buffer
  337. receive_check_and_return_item_allow_split(buffer_handles[buf_type], (void *)small_item, SMALL_ITEM_SIZE, 0, true);
  338. buf_type++;
  339. } else if (buf_type == RINGBUF_TYPE_BYTEBUF) {
  340. //Test receive from ISR for byte buffer
  341. receive_check_and_return_item_byte_buffer(buffer_handles[buf_type], (void *)small_item, SMALL_ITEM_SIZE, 0, true);
  342. buf_type++;
  343. } else if (buf_type == NO_OF_RB_TYPES) {
  344. //Check if all iterations complete
  345. if (iterations < ISR_ITERATIONS) {
  346. iterations++;
  347. buf_type = 0; //Reset and iterate through each buffer type again
  348. return;
  349. } else {
  350. //Signal complete
  351. BaseType_t task_woken = pdFALSE;
  352. xSemaphoreGiveFromISR(done_sem, &task_woken);
  353. if (task_woken == pdTRUE) {
  354. buf_type++;
  355. portYIELD_FROM_ISR();
  356. }
  357. }
  358. }
  359. }
  360. static void setup_timer(void)
  361. {
  362. //Setup timer for ISR
  363. int timer_group = TIMER_GROUP;
  364. int timer_idx = TIMER_NUMBER;
  365. timer_config_t config;
  366. config.alarm_en = 1;
  367. config.auto_reload = 1;
  368. config.counter_dir = TIMER_COUNT_UP;
  369. config.divider = 10000;
  370. config.intr_type = TIMER_INTR_LEVEL;
  371. config.counter_en = TIMER_PAUSE;
  372. timer_init(timer_group, timer_idx, &config); //Configure timer
  373. timer_pause(timer_group, timer_idx); //Stop timer counter
  374. timer_set_counter_value(timer_group, timer_idx, 0x00000000ULL); //Load counter value
  375. timer_set_alarm_value(timer_group, timer_idx, 20); //Set alarm value
  376. timer_enable_intr(timer_group, timer_idx); //Enable timer interrupt
  377. timer_set_auto_reload(timer_group, timer_idx, 1); //Auto Reload
  378. timer_isr_register(timer_group, timer_idx, ringbuffer_isr, NULL, 0, &ringbuffer_isr_handle); //Set ISR handler
  379. }
  380. static void cleanup_timer(void)
  381. {
  382. timer_disable_intr(TIMER_GROUP, TIMER_NUMBER);
  383. esp_intr_free(ringbuffer_isr_handle);
  384. }
  385. TEST_CASE("Test ring buffer ISR", "[esp_ringbuf]")
  386. {
  387. for (int i = 0; i < NO_OF_RB_TYPES; i++) {
  388. buffer_handles[i] = xRingbufferCreate(BUFFER_SIZE, i);
  389. }
  390. done_sem = xSemaphoreCreateBinary();
  391. buf_type = 0;
  392. iterations = 0;
  393. setup_timer();
  394. //Start timer to trigger ISR
  395. timer_start(TIMER_GROUP, TIMER_NUMBER);
  396. //Wait for ISR to complete multiple iterations
  397. xSemaphoreTake(done_sem, portMAX_DELAY);
  398. //Cleanup
  399. cleanup_timer();
  400. vSemaphoreDelete(done_sem);
  401. for (int i = 0; i < NO_OF_RB_TYPES; i++) {
  402. vRingbufferDelete(buffer_handles[i]);
  403. }
  404. }
  405. /* ---------------------------- Test ring buffer SMP ---------------------------
  406. * The following test case tests each type of ring buffer in an SMP fashion. A
  407. * sending task and a receiving task is created. The sending task will split
  408. * a continuous piece of data into items of random length and send it to a ring
  409. * buffer. The receiving task will receive and check those items.
  410. * Every permutation of core pinning of the sending and receiving task will be
  411. * tested.
  412. */
  413. #define SRAND_SEED 3 //Arbitrarily chosen srand() seed
  414. #define SMP_TEST_ITERATIONS 4
  415. static const char continuous_data[] = {"A_very_long_string_that_will_be_split_into_"
  416. "items_of_random_lengths_and_sent_to_the_ring_"
  417. "buffer._The_maximum_random_length_will_also_"
  418. "be_increased_over_multiple_iterations_in_this"
  419. "_test"};
  420. #define CONT_DATA_LEN sizeof(continuous_data)
  421. //32-bit aligned size that guarantees a wrap around at some point
  422. #define CONT_DATA_TEST_BUFF_LEN (((CONT_DATA_LEN/2) + 0x03) & ~0x3)
  423. typedef struct {
  424. RingbufHandle_t buffer;
  425. RingbufferType_t type;
  426. } task_args_t;
  427. static SemaphoreHandle_t tasks_done;
  428. static SemaphoreHandle_t tx_done;
  429. static SemaphoreHandle_t rx_done;
  430. static void send_to_buffer(RingbufHandle_t buffer, size_t max_item_size)
  431. {
  432. for (int iter = 0; iter < SMP_TEST_ITERATIONS; iter++) {
  433. size_t bytes_sent = 0; //Number of data bytes sent in this iteration
  434. size_t next_item_size; //Size of next item to send
  435. while (bytes_sent < CONT_DATA_LEN) {
  436. //Get size of next item
  437. next_item_size = rand() % (max_item_size + 1);
  438. if (next_item_size + bytes_sent > CONT_DATA_LEN) {
  439. next_item_size = CONT_DATA_LEN - bytes_sent;
  440. }
  441. //Send item
  442. TEST_ASSERT_MESSAGE(xRingbufferSend(buffer, (void *)&(continuous_data[bytes_sent]), next_item_size, TIMEOUT_TICKS) == pdTRUE, "Failed to send an item");
  443. bytes_sent += next_item_size;
  444. }
  445. xSemaphoreGive(tx_done);
  446. xSemaphoreTake(rx_done, portMAX_DELAY);
  447. }
  448. }
  449. static void read_from_buffer(RingbufHandle_t buffer, RingbufferType_t buf_type, size_t max_rec_size)
  450. {
  451. for (int iter = 0; iter < SMP_TEST_ITERATIONS; iter++) {
  452. size_t bytes_rec = 0; //Number of data bytes received in this iteration
  453. while (bytes_rec < CONT_DATA_LEN) {
  454. size_t item_size, item_size2; //Possible for allow split buffers to receive two items
  455. char *item_data, *item_data2;
  456. //Select appropriate receive function for type of ring buffer
  457. if (buf_type == RINGBUF_TYPE_NOSPLIT) {
  458. item_data = (char *)xRingbufferReceive(buffer, &item_size, TIMEOUT_TICKS);
  459. } else if (buf_type == RINGBUF_TYPE_ALLOWSPLIT) {
  460. BaseType_t ret = xRingbufferReceiveSplit(buffer, (void **)&item_data, (void **)&item_data2, &item_size, &item_size2, TIMEOUT_TICKS);
  461. TEST_ASSERT_MESSAGE(ret == pdTRUE, "Failed to receive any item");
  462. } else {
  463. item_data = (char *)xRingbufferReceiveUpTo(buffer, &item_size, TIMEOUT_TICKS, max_rec_size);
  464. }
  465. //Check received item and return it
  466. TEST_ASSERT_MESSAGE(item_data != NULL, "Failed to receive an item");
  467. if (buf_type == RINGBUF_TYPE_BYTEBUF) {
  468. TEST_ASSERT_MESSAGE(item_size <= max_rec_size, "Received data exceeds max size");
  469. }
  470. for (int i = 0; i < item_size; i++) {
  471. //Check item_data is valid
  472. TEST_ASSERT_MESSAGE(item_data[i] == continuous_data[bytes_rec + i], "Received data is corrupted");
  473. }
  474. bytes_rec += item_size;
  475. vRingbufferReturnItem(buffer, item_data);
  476. if (buf_type == RINGBUF_TYPE_ALLOWSPLIT && item_data2 != NULL) {
  477. //Check item_data2 is valid
  478. for (int i = 0; i < item_size2; i++) {
  479. TEST_ASSERT_MESSAGE(item_data2[i] == continuous_data[bytes_rec + i], "Received split data is corrupted");
  480. }
  481. bytes_rec += item_size2;
  482. vRingbufferReturnItem(buffer, item_data2);
  483. }
  484. }
  485. TEST_ASSERT_MESSAGE(bytes_rec == CONT_DATA_LEN, "Total length of received data is incorrect");
  486. xSemaphoreGive(rx_done);
  487. xSemaphoreTake(tx_done, portMAX_DELAY);
  488. }
  489. }
  490. static void send_task(void *args)
  491. {
  492. RingbufHandle_t buffer = ((task_args_t *)args)->buffer;
  493. size_t max_item_len = xRingbufferGetMaxItemSize(buffer);
  494. //Test sending short length items
  495. send_to_buffer(buffer, 1);
  496. //Test sending mid length items
  497. send_to_buffer(buffer, max_item_len/2);
  498. //Test sending long length items
  499. send_to_buffer(buffer, max_item_len);
  500. vTaskDelete(NULL);
  501. }
  502. static void rec_task(void *args)
  503. {
  504. RingbufHandle_t buffer = ((task_args_t *)args)->buffer;
  505. size_t max_rec_len = xRingbufferGetMaxItemSize(buffer);
  506. //Test receiving short length items
  507. read_from_buffer(buffer, ((task_args_t *)args)->type, 1);
  508. //Test receiving mid length items
  509. read_from_buffer(buffer, ((task_args_t *)args)->type, max_rec_len/2);
  510. //Test receiving long length items
  511. read_from_buffer(buffer, ((task_args_t *)args)->type, max_rec_len);
  512. xSemaphoreGive(tasks_done);
  513. vTaskDelete(NULL);
  514. }
  515. static void setup(void)
  516. {
  517. esp_rom_printf("Size of test data: %d\n", CONT_DATA_LEN);
  518. tx_done = xSemaphoreCreateBinary(); //Semaphore to indicate send is done for a particular iteration
  519. rx_done = xSemaphoreCreateBinary(); //Semaphore to indicate receive is done for a particular iteration
  520. tasks_done = xSemaphoreCreateBinary(); //Semaphore used to to indicate send and receive tasks completed running
  521. srand(SRAND_SEED); //Seed RNG
  522. }
  523. static void cleanup(void)
  524. {
  525. //Cleanup
  526. vSemaphoreDelete(tx_done);
  527. vSemaphoreDelete(rx_done);
  528. vSemaphoreDelete(tasks_done);
  529. }
  530. TEST_CASE("Test ring buffer SMP", "[esp_ringbuf]")
  531. {
  532. setup();
  533. //Iterate through buffer types (No split, split, then byte buff)
  534. for (RingbufferType_t buf_type = 0; buf_type < RINGBUF_TYPE_MAX; buf_type++) {
  535. //Create buffer
  536. task_args_t task_args;
  537. task_args.buffer = xRingbufferCreate(CONT_DATA_TEST_BUFF_LEN, buf_type); //Create buffer of selected type
  538. task_args.type = buf_type;
  539. TEST_ASSERT_MESSAGE(task_args.buffer != NULL, "Failed to create ring buffer");
  540. for (int prior_mod = -1; prior_mod < 2; prior_mod++) { //Test different relative priorities
  541. //Test every permutation of core affinity
  542. for (int send_core = 0; send_core < portNUM_PROCESSORS; send_core++) {
  543. for (int rec_core = 0; rec_core < portNUM_PROCESSORS; rec_core ++) {
  544. esp_rom_printf("Type: %d, PM: %d, SC: %d, RC: %d\n", buf_type, prior_mod, send_core, rec_core);
  545. xTaskCreatePinnedToCore(send_task, "send tsk", 2048, (void *)&task_args, 10 + prior_mod, NULL, send_core);
  546. xTaskCreatePinnedToCore(rec_task, "rec tsk", 2048, (void *)&task_args, 10, NULL, rec_core);
  547. xSemaphoreTake(tasks_done, portMAX_DELAY);
  548. vTaskDelay(5); //Allow idle to clean up
  549. }
  550. }
  551. }
  552. //Delete ring buffer
  553. vRingbufferDelete(task_args.buffer);
  554. vTaskDelay(10);
  555. }
  556. cleanup();
  557. }
  558. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  559. TEST_CASE("Test static ring buffer SMP", "[esp_ringbuf]")
  560. {
  561. setup();
  562. //Iterate through buffer types (No split, split, then byte buff)
  563. for (RingbufferType_t buf_type = 0; buf_type < RINGBUF_TYPE_MAX; buf_type++) {
  564. StaticRingbuffer_t *buffer_struct;
  565. uint8_t *buffer_storage;
  566. //Allocate memory and create semaphores
  567. #if CONFIG_SPIRAM_USE_CAPS_ALLOC //When SPIRAM can only be allocated using heap_caps_malloc()
  568. buffer_struct = (StaticRingbuffer_t *)heap_caps_malloc(sizeof(StaticRingbuffer_t), MALLOC_CAP_SPIRAM);
  569. buffer_storage = (uint8_t *)heap_caps_malloc(sizeof(uint8_t)*CONT_DATA_TEST_BUFF_LEN, MALLOC_CAP_SPIRAM);
  570. #else //Case where SPIRAM is disabled or when SPIRAM is allocatable through malloc()
  571. buffer_struct = (StaticRingbuffer_t *)malloc(sizeof(StaticRingbuffer_t));
  572. buffer_storage = (uint8_t *)malloc(sizeof(uint8_t)*CONT_DATA_TEST_BUFF_LEN);
  573. #endif
  574. TEST_ASSERT(buffer_struct != NULL && buffer_storage != NULL);
  575. //Create buffer
  576. task_args_t task_args;
  577. task_args.buffer = xRingbufferCreateStatic(CONT_DATA_TEST_BUFF_LEN, buf_type, buffer_storage, buffer_struct); //Create buffer of selected type
  578. task_args.type = buf_type;
  579. TEST_ASSERT_MESSAGE(task_args.buffer != NULL, "Failed to create ring buffer");
  580. for (int prior_mod = -1; prior_mod < 2; prior_mod++) { //Test different relative priorities
  581. //Test every permutation of core affinity
  582. for (int send_core = 0; send_core < portNUM_PROCESSORS; send_core++) {
  583. for (int rec_core = 0; rec_core < portNUM_PROCESSORS; rec_core ++) {
  584. esp_rom_printf("Type: %d, PM: %d, SC: %d, RC: %d\n", buf_type, prior_mod, send_core, rec_core);
  585. xTaskCreatePinnedToCore(send_task, "send tsk", 2048, (void *)&task_args, 10 + prior_mod, NULL, send_core);
  586. xTaskCreatePinnedToCore(rec_task, "rec tsk", 2048, (void *)&task_args, 10, NULL, rec_core);
  587. xSemaphoreTake(tasks_done, portMAX_DELAY);
  588. vTaskDelay(5); //Allow idle to clean up
  589. }
  590. }
  591. }
  592. //Delete ring buffer
  593. vRingbufferDelete(task_args.buffer);
  594. //Deallocate memory
  595. free(buffer_storage);
  596. free(buffer_struct);
  597. vTaskDelay(10);
  598. }
  599. cleanup();
  600. }
  601. #endif
  602. /* -------------------------- Test ring buffer IRAM ------------------------- */
  603. static IRAM_ATTR __attribute__((noinline)) bool iram_ringbuf_test(void)
  604. {
  605. bool result = true;
  606. RingbufHandle_t handle = xRingbufferCreate(CONT_DATA_TEST_BUFF_LEN, RINGBUF_TYPE_NOSPLIT);
  607. result = result && (handle != NULL);
  608. spi_flash_guard_get()->start(); // Disables flash cache
  609. xRingbufferGetMaxItemSize(handle);
  610. spi_flash_guard_get()->end(); // Re-enables flash cache
  611. vRingbufferDelete(handle);
  612. return result;
  613. }
  614. TEST_CASE("Test ringbuffer functions work with flash cache disabled", "[esp_ringbuf]")
  615. {
  616. TEST_ASSERT( iram_ringbuf_test() );
  617. }