test_fastbus.c 3.8 KB

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