hello.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\n%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 A 0x45c11ba1 /* Arbitrary odd constant */
  33. #define B 0x78dacecb /* Arbitrary constant */
  34. static void test_sdram(void)
  35. {
  36. static unsigned int stride = 4;
  37. uint32_t start_time = rdtime();
  38. uint32_t w = 0;
  39. uint32_t n;
  40. con_printf("Testing SDRAM from 0x%08x to 0x%08x, stride 0x%08x...\r\n",
  41. SDRAM_ADDR, SDRAM_END, stride);
  42. n = 0;
  43. err_char = '-';
  44. rate_limit = ERROR_RATELIMIT;
  45. do {
  46. uint32_t a = w + SDRAM_ADDR;
  47. volatile uint32_t *p = (volatile uint32_t *)a;
  48. write_check(p, 0);
  49. write_check(p, ~0);
  50. write_check(p, ~w);
  51. write_check(p, w);
  52. write_check(p, A*w + B);
  53. if (!(++n & 0x3ffff)) {
  54. CONSOLE = err_char;
  55. err_char = '-';
  56. }
  57. w = (w + stride) & SDRAM_MASK;
  58. } while (w);
  59. con_puts("\r\nReading back to check for aliases...\r\n");
  60. rate_limit = ERROR_RATELIMIT;
  61. do {
  62. uint32_t a = w + SDRAM_ADDR;
  63. volatile uint32_t *p = (volatile uint32_t *)a;
  64. data_check(p, A*w + B);
  65. if (!(++n & 0x3ffff)) {
  66. CONSOLE = err_char;
  67. err_char = '-';
  68. }
  69. w = (w - stride) & SDRAM_MASK;
  70. } while (w);
  71. con_printf("\rSDRAM test complete, time = %u ms\r\n",
  72. (rdtime() - start_time)/(CPU_CLK_HZ/1000));
  73. stride *= 3;
  74. stride = ((stride & SDRAM_MASK) ^ (stride >> (SDRAM_ADDR_BITS-2))) & ~3;
  75. }
  76. void main(void)
  77. {
  78. static const char hello[] = "\f\033[2J\033[H"
  79. "*** Hello, World! ***\r\n"
  80. "Firmware compiled on: " __DATE__ " " __TIME__ "\r\n\n";
  81. uint8_t led = 0;
  82. uint8_t loops;
  83. con_set_baudrate(115200);
  84. set_led(led = 0);
  85. while ( 1 ) {
  86. con_puts(hello);
  87. con_printf("Loops: %u, errors = %u\r\n\n", loops++, errors);
  88. test_sdram();
  89. led = (led << 1) | ((~led >> 2) & 1);
  90. set_led(led);
  91. }
  92. }