hello.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <stddef.h>
  2. #include <stdint.h>
  3. #include "io.h"
  4. #include "console.h"
  5. #define SDRAM_ADDR 0x40000000
  6. #define SDRAM_ADDR_BITS 25
  7. #define SDRAM_SIZE (1U << SDRAM_ADDR_BITS)
  8. #define SDRAM_MASK (SDRAM_SIZE - 1)
  9. #define SDRAM_END (SDRAM_ADDR + SDRAM_SIZE)
  10. static unsigned int errors = 0;
  11. static unsigned int rate_limit;
  12. static char err_char;
  13. static void data_check(volatile uint32_t *p, uint32_t expect)
  14. {
  15. uint32_t readback = *p;
  16. if (readback != expect) {
  17. if (rate_limit) {
  18. con_printf("\r%p : read %08x expected %08x\r\n",
  19. p, readback, expect);
  20. rate_limit--;
  21. }
  22. err_char = 'X';
  23. errors++;
  24. }
  25. }
  26. static inline void write_check(volatile uint32_t *p, uint32_t v)
  27. {
  28. *p = v;
  29. data_check(p, v);
  30. }
  31. #define ERROR_RATELIMIT 8
  32. #define SOME_NUMBER 0x78dacecb
  33. static void test_sdram(void)
  34. {
  35. static unsigned int stride = 4;
  36. uint32_t start_time = rdtime();
  37. uint32_t w = 0;
  38. uint32_t n;
  39. con_printf("Testing SDRAM from 0x%08x to 0x%08x, stride 0x%08x...\r\n",
  40. SDRAM_ADDR, SDRAM_END, stride);
  41. n = 0;
  42. err_char = '-';
  43. rate_limit = ERROR_RATELIMIT;
  44. do {
  45. uint32_t a = w + SDRAM_ADDR;
  46. volatile uint32_t *p = (volatile uint32_t *)a;
  47. write_check(p, 0);
  48. write_check(p, ~0);
  49. write_check(p, ~w);
  50. write_check(p, w);
  51. write_check(p, w + SOME_NUMBER);
  52. if (!(++n & 0x3ffff)) {
  53. CONSOLE = err_char;
  54. err_char = '-';
  55. }
  56. w = (w + stride) & SDRAM_MASK;
  57. } while (w);
  58. con_puts("\r\nReading back to check for aliases...\r\n");
  59. rate_limit = ERROR_RATELIMIT;
  60. do {
  61. uint32_t a = w + SDRAM_ADDR;
  62. volatile uint32_t *p = (volatile uint32_t *)a;
  63. data_check(p, w + SOME_NUMBER);
  64. if (!(++n & 0x3ffff)) {
  65. CONSOLE = err_char;
  66. err_char = '-';
  67. }
  68. w = (w - stride) & SDRAM_MASK;
  69. } while (w);
  70. con_printf("\rSDRAM test complete, time = %u ms\r\n",
  71. (rdtime() - start_time)/(CPU_CLK_HZ/1000));
  72. stride *= 3;
  73. stride = ((stride & SDRAM_MASK) ^ (stride >> (SDRAM_ADDR_BITS-2))) & ~3;
  74. }
  75. void main(void)
  76. {
  77. static const char hello[] = "\f\033[2J\033[H"
  78. "*** Hello, World! ***\r\n"
  79. "Firmware compiled on: " __DATE__ " " __TIME__ "\r\n\n";
  80. uint8_t led = 0;
  81. uint8_t loops;
  82. con_set_baudrate(115200);
  83. set_led(led = 0);
  84. while ( 1 ) {
  85. con_puts(hello);
  86. con_printf("Loops: %u, errors = %u\r\n\n", loops++, errors);
  87. test_sdram();
  88. led = (led << 1) | ((~led >> 2) & 1);
  89. set_led(led);
  90. }
  91. }