test_fastbus.c 3.7 KB

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