test_ringbuf.c 29 KB

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