test_ringbuf.c 26 KB

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