test_ringbuf.c 26 KB

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