test_ringbuf.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. Test for multicore FreeRTOS ringbuffer.
  3. */
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include "rom/ets_sys.h"
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "freertos/semphr.h"
  10. #include "freertos/queue.h"
  11. #include "freertos/ringbuf.h"
  12. #include "freertos/xtensa_api.h"
  13. #include "unity.h"
  14. #include "soc/uart_reg.h"
  15. #include "soc/dport_reg.h"
  16. #include "soc/io_mux_reg.h"
  17. #include "esp_intr_alloc.h"
  18. static RingbufHandle_t rb;
  19. typedef enum {
  20. TST_MOSTLYFILLED,
  21. TST_MOSTLYEMPTY,
  22. TST_INTTOTASK,
  23. TST_TASKTOINT,
  24. } testtype_t;
  25. static volatile testtype_t testtype;
  26. intr_handle_t s_intr_handle;
  27. static void task1(void *arg)
  28. {
  29. testtype_t oldtest;
  30. char buf[100];
  31. int i = 0;
  32. int x, r;
  33. while (1) {
  34. oldtest = testtype;
  35. if (testtype == TST_MOSTLYFILLED || testtype == TST_MOSTLYEMPTY) {
  36. for (x = 0; x < 10; x++) {
  37. sprintf(buf, "This is test %d item %d.", (int)testtype, i++);
  38. ets_printf("TSK w");
  39. xRingbufferPrintInfo(rb);
  40. r = xRingbufferSend(rb, buf, strlen(buf) + 1, 2000 / portTICK_PERIOD_MS);
  41. if (!r) {
  42. printf("Test %d: Timeout on send!\n", (int)testtype);
  43. }
  44. if (testtype == TST_MOSTLYEMPTY) {
  45. vTaskDelay(300 / portTICK_PERIOD_MS);
  46. }
  47. }
  48. //Send NULL event to stop other side.
  49. r = xRingbufferSend(rb, NULL, 0, 10000 / portTICK_PERIOD_MS);
  50. }
  51. while (oldtest == testtype) {
  52. vTaskDelay(300 / portTICK_PERIOD_MS);
  53. }
  54. }
  55. }
  56. static void task2(void *arg)
  57. {
  58. testtype_t oldtest;
  59. char *buf;
  60. size_t len;
  61. while (1) {
  62. oldtest = testtype;
  63. if (testtype == TST_MOSTLYFILLED || testtype == TST_MOSTLYEMPTY) {
  64. while (1) {
  65. ets_printf("TSK r");
  66. xRingbufferPrintInfo(rb);
  67. buf = xRingbufferReceive(rb, &len, 2000 / portTICK_PERIOD_MS);
  68. if (buf == NULL) {
  69. printf("Test %d: Timeout on recv!\n", (int)testtype);
  70. } else if (len == 0) {
  71. printf("End packet received.\n");
  72. vRingbufferReturnItem(rb, buf);
  73. break;
  74. } else {
  75. printf("Received: %s (%d bytes, %p)\n", buf, len, buf);
  76. vRingbufferReturnItem(rb, buf);
  77. }
  78. if (testtype == TST_MOSTLYFILLED) {
  79. vTaskDelay(300 / portTICK_PERIOD_MS);
  80. }
  81. }
  82. }
  83. while (oldtest == testtype) {
  84. vTaskDelay(300 / portTICK_PERIOD_MS);
  85. }
  86. }
  87. }
  88. static void uartIsrHdl(void *arg)
  89. {
  90. char c;
  91. char buf[50];
  92. char *item;
  93. int r;
  94. size_t len;
  95. BaseType_t xHigherPriorityTaskWoken;
  96. SET_PERI_REG_MASK(UART_INT_CLR_REG(0), UART_RXFIFO_FULL_INT_CLR);
  97. while (READ_PERI_REG(UART_STATUS_REG(0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
  98. c = READ_PERI_REG(UART_FIFO_REG(0));
  99. if (c == 'r') {
  100. ets_printf("ISR r");
  101. xRingbufferPrintInfo(rb);
  102. item = xRingbufferReceiveFromISR(rb, &len);
  103. if (item == NULL) {
  104. ets_printf("ISR recv fail!\n");
  105. } else if (len == 0) {
  106. ets_printf("ISR recv NULL!\n");
  107. vRingbufferReturnItemFromISR(rb, item, &xHigherPriorityTaskWoken);
  108. } else {
  109. ets_printf("ISR recv '%s' (%d bytes, %p)\n", buf, len, buf);
  110. vRingbufferReturnItemFromISR(rb, item, &xHigherPriorityTaskWoken);
  111. }
  112. } else {
  113. sprintf(buf, "UART: %c", c);
  114. ets_printf("ISR w");
  115. xRingbufferPrintInfo(rb);
  116. r = xRingbufferSendFromISR(rb, buf, strlen(buf) + 1, &xHigherPriorityTaskWoken);
  117. if (!r) {
  118. ets_printf("ISR send fail\n");
  119. }
  120. }
  121. }
  122. if (xHigherPriorityTaskWoken) {
  123. portYIELD_FROM_ISR();
  124. }
  125. }
  126. static void uartRxInit()
  127. {
  128. WRITE_PERI_REG(UART_CONF1_REG(0), 1 << UART_RXFIFO_FULL_THRHD_S);
  129. CLEAR_PERI_REG_MASK(UART_INT_ENA_REG(0), UART_TXFIFO_EMPTY_INT_ENA | UART_RXFIFO_TOUT_INT_ENA);
  130. SET_PERI_REG_MASK(UART_INT_ENA_REG(0), UART_RXFIFO_FULL_INT_ENA);
  131. ESP_ERROR_CHECK(esp_intr_alloc(ETS_UART0_INTR_SOURCE, 0, &uartIsrHdl, NULL, &s_intr_handle));
  132. }
  133. static void uartRxDeinit()
  134. {
  135. esp_intr_free(s_intr_handle);
  136. }
  137. static void testRingbuffer(int type, bool arbitrary)
  138. {
  139. TaskHandle_t th[2];
  140. int i;
  141. /* Arbitrary Length means buffer length which is not a multiple of 4 */
  142. if (arbitrary) {
  143. rb = xRingbufferCreate(31 * 3, type);
  144. } else {
  145. rb = xRingbufferCreate(32 * 3, type);
  146. }
  147. testtype = TST_MOSTLYFILLED;
  148. xTaskCreatePinnedToCore(task1, "tskone", 2048, NULL, 3, &th[0], 0);
  149. xTaskCreatePinnedToCore(task2, "tsktwo", 2048, NULL, 3, &th[1], 0);
  150. uartRxInit();
  151. printf("Press 'r' to read an event in isr, any other key to write one.\n");
  152. printf("Test: mostlyfilled; putting 10 items in ringbuff ASAP, reading 1 a second\n");
  153. vTaskDelay(5000 / portTICK_PERIOD_MS);
  154. printf("Test: mostlyempty; putting 10 items in ringbuff @ 1/sec, reading as fast as possible\n");
  155. testtype = TST_MOSTLYEMPTY;
  156. vTaskDelay(5000 / portTICK_PERIOD_MS);
  157. //Shut down all the tasks
  158. for (i = 0; i < 2; i++) {
  159. vTaskDelete(th[i]);
  160. }
  161. vRingbufferDelete(rb);
  162. uartRxDeinit();
  163. }
  164. // TODO: split this thing into separate orthogonal tests
  165. TEST_CASE("FreeRTOS ringbuffer test, no splitting items", "[freertos]")
  166. {
  167. testRingbuffer(0, false);
  168. }
  169. TEST_CASE("FreeRTOS ringbuffer test, w/ splitting items", "[freertos]")
  170. {
  171. testRingbuffer(1, false);
  172. }
  173. TEST_CASE("FreeRTOS ringbuffer test, no splitting items, arbitrary length buffer", "[freertos]")
  174. {
  175. testRingbuffer(0, true);
  176. }
  177. TEST_CASE("FreeRTOS ringbuffer test, w/ splitting items, arbitrary length buffer", "[freertos]")
  178. {
  179. testRingbuffer(1, true);
  180. }
  181. TEST_CASE("FreeRTOS ringbuffer test, check if zero-length items are handled correctly", "[freertos]")
  182. {
  183. rb = xRingbufferCreate(32, 0);
  184. int r;
  185. void *v;
  186. size_t sz;
  187. for (int x=0; x<128; x++) {
  188. if (x!=127) {
  189. //Send an item
  190. r = xRingbufferSend(rb, NULL, 0, 10000 / portTICK_PERIOD_MS);
  191. assert(r==pdTRUE);
  192. }
  193. if (x!=0) {
  194. //Receive an item
  195. v=xRingbufferReceive(rb, &sz, 10000 / portTICK_PERIOD_MS);
  196. assert(sz==0);
  197. vRingbufferReturnItem(rb, v); //actually not needed for NULL data...
  198. }
  199. }
  200. vRingbufferDelete(rb);
  201. }