test_ringbuf.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. .alarm_en = 1,
  367. .auto_reload = 1,
  368. .counter_dir = TIMER_COUNT_UP,
  369. .divider = 10000,
  370. .intr_type = TIMER_INTR_LEVEL,
  371. .counter_en = TIMER_PAUSE,
  372. };
  373. timer_init(timer_group, timer_idx, &config); //Configure timer
  374. timer_pause(timer_group, timer_idx); //Stop timer counter
  375. timer_set_counter_value(timer_group, timer_idx, 0x00000000ULL); //Load counter value
  376. timer_set_alarm_value(timer_group, timer_idx, 20); //Set alarm value
  377. timer_enable_intr(timer_group, timer_idx); //Enable timer interrupt
  378. timer_set_auto_reload(timer_group, timer_idx, 1); //Auto Reload
  379. timer_isr_register(timer_group, timer_idx, ringbuffer_isr, NULL, 0, &ringbuffer_isr_handle); //Set ISR handler
  380. }
  381. static void cleanup_timer(void)
  382. {
  383. timer_disable_intr(TIMER_GROUP, TIMER_NUMBER);
  384. esp_intr_free(ringbuffer_isr_handle);
  385. }
  386. TEST_CASE("Test ring buffer ISR", "[esp_ringbuf]")
  387. {
  388. for (int i = 0; i < NO_OF_RB_TYPES; i++) {
  389. buffer_handles[i] = xRingbufferCreate(BUFFER_SIZE, i);
  390. }
  391. done_sem = xSemaphoreCreateBinary();
  392. buf_type = 0;
  393. iterations = 0;
  394. setup_timer();
  395. //Start timer to trigger ISR
  396. timer_start(TIMER_GROUP, TIMER_NUMBER);
  397. //Wait for ISR to complete multiple iterations
  398. xSemaphoreTake(done_sem, portMAX_DELAY);
  399. //Cleanup
  400. cleanup_timer();
  401. vSemaphoreDelete(done_sem);
  402. for (int i = 0; i < NO_OF_RB_TYPES; i++) {
  403. vRingbufferDelete(buffer_handles[i]);
  404. }
  405. }
  406. /* ---------------------------- Test ring buffer SMP ---------------------------
  407. * The following test case tests each type of ring buffer in an SMP fashion. A
  408. * sending task and a receiving task is created. The sending task will split
  409. * a continuous piece of data into items of random length and send it to a ring
  410. * buffer. The receiving task will receive and check those items.
  411. * Every permutation of core pinning of the sending and receiving task will be
  412. * tested.
  413. */
  414. #define SRAND_SEED 3 //Arbitrarily chosen srand() seed
  415. #define SMP_TEST_ITERATIONS 4
  416. static const char continuous_data[] = {"A_very_long_string_that_will_be_split_into_"
  417. "items_of_random_lengths_and_sent_to_the_ring_"
  418. "buffer._The_maximum_random_length_will_also_"
  419. "be_increased_over_multiple_iterations_in_this"
  420. "_test"};
  421. #define CONT_DATA_LEN sizeof(continuous_data)
  422. //32-bit aligned size that guarantees a wrap around at some point
  423. #define CONT_DATA_TEST_BUFF_LEN (((CONT_DATA_LEN/2) + 0x03) & ~0x3)
  424. typedef struct {
  425. RingbufHandle_t buffer;
  426. RingbufferType_t type;
  427. } task_args_t;
  428. static SemaphoreHandle_t tasks_done;
  429. static SemaphoreHandle_t tx_done;
  430. static SemaphoreHandle_t rx_done;
  431. static void send_to_buffer(RingbufHandle_t buffer, size_t max_item_size)
  432. {
  433. for (int iter = 0; iter < SMP_TEST_ITERATIONS; iter++) {
  434. size_t bytes_sent = 0; //Number of data bytes sent in this iteration
  435. size_t next_item_size; //Size of next item to send
  436. while (bytes_sent < CONT_DATA_LEN) {
  437. //Get size of next item
  438. next_item_size = rand() % (max_item_size + 1);
  439. if (next_item_size + bytes_sent > CONT_DATA_LEN) {
  440. next_item_size = CONT_DATA_LEN - bytes_sent;
  441. }
  442. //Send item
  443. TEST_ASSERT_MESSAGE(xRingbufferSend(buffer, (void *)&(continuous_data[bytes_sent]), next_item_size, TIMEOUT_TICKS) == pdTRUE, "Failed to send an item");
  444. bytes_sent += next_item_size;
  445. }
  446. xSemaphoreGive(tx_done);
  447. xSemaphoreTake(rx_done, portMAX_DELAY);
  448. }
  449. }
  450. static void read_from_buffer(RingbufHandle_t buffer, RingbufferType_t buf_type, size_t max_rec_size)
  451. {
  452. for (int iter = 0; iter < SMP_TEST_ITERATIONS; iter++) {
  453. size_t bytes_rec = 0; //Number of data bytes received in this iteration
  454. while (bytes_rec < CONT_DATA_LEN) {
  455. size_t item_size, item_size2; //Possible for allow split buffers to receive two items
  456. char *item_data, *item_data2;
  457. //Select appropriate receive function for type of ring buffer
  458. if (buf_type == RINGBUF_TYPE_NOSPLIT) {
  459. item_data = (char *)xRingbufferReceive(buffer, &item_size, TIMEOUT_TICKS);
  460. } else if (buf_type == RINGBUF_TYPE_ALLOWSPLIT) {
  461. BaseType_t ret = xRingbufferReceiveSplit(buffer, (void **)&item_data, (void **)&item_data2, &item_size, &item_size2, TIMEOUT_TICKS);
  462. TEST_ASSERT_MESSAGE(ret == pdTRUE, "Failed to receive any item");
  463. } else {
  464. item_data = (char *)xRingbufferReceiveUpTo(buffer, &item_size, TIMEOUT_TICKS, max_rec_size);
  465. }
  466. //Check received item and return it
  467. TEST_ASSERT_MESSAGE(item_data != NULL, "Failed to receive an item");
  468. if (buf_type == RINGBUF_TYPE_BYTEBUF) {
  469. TEST_ASSERT_MESSAGE(item_size <= max_rec_size, "Received data exceeds max size");
  470. }
  471. for (int i = 0; i < item_size; i++) {
  472. //Check item_data is valid
  473. TEST_ASSERT_MESSAGE(item_data[i] == continuous_data[bytes_rec + i], "Received data is corrupted");
  474. }
  475. bytes_rec += item_size;
  476. vRingbufferReturnItem(buffer, item_data);
  477. if (buf_type == RINGBUF_TYPE_ALLOWSPLIT && item_data2 != NULL) {
  478. //Check item_data2 is valid
  479. for (int i = 0; i < item_size2; i++) {
  480. TEST_ASSERT_MESSAGE(item_data2[i] == continuous_data[bytes_rec + i], "Received split data is corrupted");
  481. }
  482. bytes_rec += item_size2;
  483. vRingbufferReturnItem(buffer, item_data2);
  484. }
  485. }
  486. TEST_ASSERT_MESSAGE(bytes_rec == CONT_DATA_LEN, "Total length of received data is incorrect");
  487. xSemaphoreGive(rx_done);
  488. xSemaphoreTake(tx_done, portMAX_DELAY);
  489. }
  490. }
  491. static void send_task(void *args)
  492. {
  493. RingbufHandle_t buffer = ((task_args_t *)args)->buffer;
  494. size_t max_item_len = xRingbufferGetMaxItemSize(buffer);
  495. //Test sending short length items
  496. send_to_buffer(buffer, 1);
  497. //Test sending mid length items
  498. send_to_buffer(buffer, max_item_len/2);
  499. //Test sending long length items
  500. send_to_buffer(buffer, max_item_len);
  501. vTaskDelete(NULL);
  502. }
  503. static void rec_task(void *args)
  504. {
  505. RingbufHandle_t buffer = ((task_args_t *)args)->buffer;
  506. size_t max_rec_len = xRingbufferGetMaxItemSize(buffer);
  507. //Test receiving short length items
  508. read_from_buffer(buffer, ((task_args_t *)args)->type, 1);
  509. //Test receiving mid length items
  510. read_from_buffer(buffer, ((task_args_t *)args)->type, max_rec_len/2);
  511. //Test receiving long length items
  512. read_from_buffer(buffer, ((task_args_t *)args)->type, max_rec_len);
  513. xSemaphoreGive(tasks_done);
  514. vTaskDelete(NULL);
  515. }
  516. static void setup(void)
  517. {
  518. esp_rom_printf("Size of test data: %d\n", CONT_DATA_LEN);
  519. tx_done = xSemaphoreCreateBinary(); //Semaphore to indicate send is done for a particular iteration
  520. rx_done = xSemaphoreCreateBinary(); //Semaphore to indicate receive is done for a particular iteration
  521. tasks_done = xSemaphoreCreateBinary(); //Semaphore used to to indicate send and receive tasks completed running
  522. srand(SRAND_SEED); //Seed RNG
  523. }
  524. static void cleanup(void)
  525. {
  526. //Cleanup
  527. vSemaphoreDelete(tx_done);
  528. vSemaphoreDelete(rx_done);
  529. vSemaphoreDelete(tasks_done);
  530. }
  531. TEST_CASE("Test ring buffer SMP", "[esp_ringbuf]")
  532. {
  533. setup();
  534. //Iterate through buffer types (No split, split, then byte buff)
  535. for (RingbufferType_t buf_type = 0; buf_type < RINGBUF_TYPE_MAX; buf_type++) {
  536. //Create buffer
  537. task_args_t task_args;
  538. task_args.buffer = xRingbufferCreate(CONT_DATA_TEST_BUFF_LEN, buf_type); //Create buffer of selected type
  539. task_args.type = buf_type;
  540. TEST_ASSERT_MESSAGE(task_args.buffer != NULL, "Failed to create ring buffer");
  541. for (int prior_mod = -1; prior_mod < 2; prior_mod++) { //Test different relative priorities
  542. //Test every permutation of core affinity
  543. for (int send_core = 0; send_core < portNUM_PROCESSORS; send_core++) {
  544. for (int rec_core = 0; rec_core < portNUM_PROCESSORS; rec_core ++) {
  545. esp_rom_printf("Type: %d, PM: %d, SC: %d, RC: %d\n", buf_type, prior_mod, send_core, rec_core);
  546. xTaskCreatePinnedToCore(send_task, "send tsk", 2048, (void *)&task_args, 10 + prior_mod, NULL, send_core);
  547. xTaskCreatePinnedToCore(rec_task, "rec tsk", 2048, (void *)&task_args, 10, NULL, rec_core);
  548. xSemaphoreTake(tasks_done, portMAX_DELAY);
  549. vTaskDelay(5); //Allow idle to clean up
  550. }
  551. }
  552. }
  553. //Delete ring buffer
  554. vRingbufferDelete(task_args.buffer);
  555. vTaskDelay(10);
  556. }
  557. cleanup();
  558. }
  559. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  560. TEST_CASE("Test static ring buffer SMP", "[esp_ringbuf]")
  561. {
  562. setup();
  563. //Iterate through buffer types (No split, split, then byte buff)
  564. for (RingbufferType_t buf_type = 0; buf_type < RINGBUF_TYPE_MAX; buf_type++) {
  565. StaticRingbuffer_t *buffer_struct;
  566. uint8_t *buffer_storage;
  567. //Allocate memory and create semaphores
  568. #if CONFIG_SPIRAM_USE_CAPS_ALLOC //When SPIRAM can only be allocated using heap_caps_malloc()
  569. buffer_struct = (StaticRingbuffer_t *)heap_caps_malloc(sizeof(StaticRingbuffer_t), MALLOC_CAP_SPIRAM);
  570. buffer_storage = (uint8_t *)heap_caps_malloc(sizeof(uint8_t)*CONT_DATA_TEST_BUFF_LEN, MALLOC_CAP_SPIRAM);
  571. #else //Case where SPIRAM is disabled or when SPIRAM is allocatable through malloc()
  572. buffer_struct = (StaticRingbuffer_t *)malloc(sizeof(StaticRingbuffer_t));
  573. buffer_storage = (uint8_t *)malloc(sizeof(uint8_t)*CONT_DATA_TEST_BUFF_LEN);
  574. #endif
  575. TEST_ASSERT(buffer_struct != NULL && buffer_storage != NULL);
  576. //Create buffer
  577. task_args_t task_args;
  578. task_args.buffer = xRingbufferCreateStatic(CONT_DATA_TEST_BUFF_LEN, buf_type, buffer_storage, buffer_struct); //Create buffer of selected type
  579. task_args.type = buf_type;
  580. TEST_ASSERT_MESSAGE(task_args.buffer != NULL, "Failed to create ring buffer");
  581. for (int prior_mod = -1; prior_mod < 2; prior_mod++) { //Test different relative priorities
  582. //Test every permutation of core affinity
  583. for (int send_core = 0; send_core < portNUM_PROCESSORS; send_core++) {
  584. for (int rec_core = 0; rec_core < portNUM_PROCESSORS; rec_core ++) {
  585. esp_rom_printf("Type: %d, PM: %d, SC: %d, RC: %d\n", buf_type, prior_mod, send_core, rec_core);
  586. xTaskCreatePinnedToCore(send_task, "send tsk", 2048, (void *)&task_args, 10 + prior_mod, NULL, send_core);
  587. xTaskCreatePinnedToCore(rec_task, "rec tsk", 2048, (void *)&task_args, 10, NULL, rec_core);
  588. xSemaphoreTake(tasks_done, portMAX_DELAY);
  589. vTaskDelay(5); //Allow idle to clean up
  590. }
  591. }
  592. }
  593. //Delete ring buffer
  594. vRingbufferDelete(task_args.buffer);
  595. //Deallocate memory
  596. free(buffer_storage);
  597. free(buffer_struct);
  598. vTaskDelay(10);
  599. }
  600. cleanup();
  601. }
  602. #endif
  603. /* -------------------------- Test ring buffer IRAM ------------------------- */
  604. static IRAM_ATTR __attribute__((noinline)) bool iram_ringbuf_test(void)
  605. {
  606. bool result = true;
  607. RingbufHandle_t handle = xRingbufferCreate(CONT_DATA_TEST_BUFF_LEN, RINGBUF_TYPE_NOSPLIT);
  608. result = result && (handle != NULL);
  609. spi_flash_guard_get()->start(); // Disables flash cache
  610. xRingbufferGetMaxItemSize(handle);
  611. spi_flash_guard_get()->end(); // Re-enables flash cache
  612. vRingbufferDelete(handle);
  613. return result;
  614. }
  615. TEST_CASE("Test ringbuffer functions work with flash cache disabled", "[esp_ringbuf]")
  616. {
  617. TEST_ASSERT( iram_ringbuf_test() );
  618. }