test_fastbus.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "sdkconfig.h"
  7. #if CONFIG_IDF_TARGET_ESP32
  8. #include <stdio.h>
  9. #include "esp_types.h"
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "freertos/semphr.h"
  13. #include "freertos/queue.h"
  14. #include "freertos/xtensa_api.h"
  15. #include "unity.h"
  16. #include "soc/uart_periph.h"
  17. #include "soc/dport_reg.h"
  18. #include "hal/gpio_hal.h"
  19. #include "driver/gpio.h"
  20. /*
  21. This test tests the 'fast' peripherial bus at 0x3ff40000. This bus is connected directly to the core, and as such
  22. can receive 'speculative' reads, that is, reads that may or may not actually be executed in the code flow. This
  23. may mess with any FIFOs mapped in the region: if a byte gets dropped due to a missed speculative read, the fifo
  24. may advance to the next byte anyway.
  25. This code tests reading/writing from the UART1 FIFO, using both cores. For this to work, it's required that the
  26. UARTs RX and TX lines are connected.
  27. */
  28. void test_fastbus_cp(int fifo_addr, unsigned char *buf, int len, int *dummy);
  29. static volatile int state = 0;
  30. static volatile int xor = 0;
  31. static unsigned char res[128];
  32. static void tskOne(void *pvParameters)
  33. {
  34. int run = 0, err = 0;
  35. int x;
  36. int ct[256];
  37. volatile int w;
  38. int dummy;
  39. while (1) {
  40. state = 1;
  41. for (x = 0; x < 64; x++) {
  42. WRITE_PERI_REG(UART_FIFO_REG(1), x ^ xor);
  43. }
  44. for (w = 0; w < (1 << 14); w++); //delay
  45. state = 2;
  46. test_fastbus_cp(UART_FIFO_REG(1), &res[0], 64, &dummy);
  47. for (w = 0; w < (1 << 10); w++); //delay
  48. for (x = 0; x < 255; x++) {
  49. ct[x] = 0; //zero ctrs
  50. }
  51. for (x = 0; x < 128; x++) {
  52. ct[(int)res[x]^xor]++; //count values
  53. }
  54. for (x = 0; x < 255; x++) { //check counts
  55. if (ct[x] != (x < 128 ? 1 : 0)) {
  56. //Disregard first few loops; there may be crap in the fifo.
  57. if (run > 2) {
  58. err++;
  59. printf("Error! Received value %d %d times!\n", x, ct[x]);
  60. }
  61. }
  62. }
  63. run++;
  64. if ((run & 255) == 0) {
  65. printf("Loop %d errct %d\n", run, err);
  66. }
  67. xor = (xor + 1) & 0xff;
  68. }
  69. }
  70. #define FB2ADDR 0x40098000
  71. static void tskTwo(void *pvParameters)
  72. {
  73. int x;
  74. int dummy;
  75. int *p = (int *)FB2ADDR;
  76. int *s = (int *)test_fastbus_cp;
  77. for (x = 0; x < 100; x++) {
  78. *p++ = *s++;
  79. }
  80. void (*test_fastbus_cp2)(int fifo_addr, unsigned char * buf, int len, int * dummy) = (void *)FB2ADDR;
  81. while (1) {
  82. while (state != 1) ;
  83. for (x = 64; x < 128; x++) {
  84. WRITE_PERI_REG(UART_FIFO_REG(1), x ^ xor);
  85. }
  86. while (state != 2);
  87. test_fastbus_cp2(UART_FIFO_REG(1), &res[64], 64, &dummy);
  88. }
  89. }
  90. // TODO: split this thing into separate orthogonal tests
  91. TEST_CASE("Fast I/O bus test", "[hw][ignore]")
  92. {
  93. int i;
  94. if ((REG_UART_BASE(0) >> 16) != 0x3ff4) {
  95. printf("Error! Uart base isn't on fast bus.\n");
  96. TEST_ASSERT(0);
  97. }
  98. gpio_pullup_dis(10);
  99. gpio_hal_iomux_func_sel(PERIPHS_IO_MUX_SD_DATA2_U, FUNC_SD_DATA2_U1RXD);
  100. gpio_hal_iomux_func_sel(PERIPHS_IO_MUX_SD_DATA3_U, FUNC_SD_DATA3_U1TXD);
  101. int reg_val = (1 << UART_RXFIFO_FULL_THRHD_S);
  102. WRITE_PERI_REG(UART_CONF1_REG(1), reg_val);
  103. WRITE_PERI_REG(UART_CLKDIV_REG(1), 0x30); //semi-random
  104. // CLEAR_PERI_REG_MASK(UART_INT_ENA_REG(1), UART_TXFIFO_EMPTY_INT_ENA|UART_RXFIFO_TOUT_INT_ENA);
  105. TaskHandle_t th[2];
  106. printf("Creating tasks\n");
  107. xTaskCreatePinnedToCore(tskOne , "tskone" , 2048, NULL, 3, &th[0], 0);
  108. xTaskCreatePinnedToCore(tskTwo , "tsktwo" , 2048, NULL, 3, &th[1], 1);
  109. // Let stuff run for 20s
  110. while (1) {
  111. vTaskDelay(20000 / portTICK_PERIOD_MS);
  112. }
  113. //Shut down all the tasks
  114. for (i = 0; i < 2; i++) {
  115. vTaskDelete(th[i]);
  116. }
  117. xt_ints_off(1 << ETS_UART0_INUM);
  118. }
  119. #endif // CONFIG_IDF_TARGET_ESP32