Int_sdram.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "Int_sdram.h"
  2. #include "fmc.h"
  3. #include <stdio.h>
  4. #include <string.h>
  5. extern SDRAM_HandleTypeDef hsdram1;
  6. static uint8_t Int_sdram_send_cmd(uint8_t bank, uint8_t cmd, uint8_t refresh, uint16_t regval)
  7. {
  8. FMC_SDRAM_CommandTypeDef command = {0};
  9. command.CommandMode = cmd;
  10. command.CommandTarget = (bank == 0) ? FMC_SDRAM_CMD_TARGET_BANK1 : FMC_SDRAM_CMD_TARGET_BANK2;
  11. command.AutoRefreshNumber = refresh;
  12. command.ModeRegisterDefinition = regval;
  13. if (HAL_SDRAM_SendCommand(&hsdram1, &command, 0xFFFF) == HAL_OK)
  14. {
  15. return 0;
  16. }
  17. return 1;
  18. }
  19. void Int_sdram_init(void)
  20. {
  21. uint32_t mode_register;
  22. Int_sdram_send_cmd(0, FMC_SDRAM_CMD_CLK_ENABLE, 1, 0);
  23. HAL_Delay(10);
  24. Int_sdram_send_cmd(0, FMC_SDRAM_CMD_PALL, 1, 0);
  25. Int_sdram_send_cmd(0, FMC_SDRAM_CMD_AUTOREFRESH_MODE, 8, 0);
  26. /* 模式寄存器:BL=1(000), CAS=3(011), 顺序突发, 单次写入(1<<9) */
  27. mode_register = (uint32_t)(0x0000 |
  28. 0x0030 |
  29. 0x0000 |
  30. 0x0200);
  31. Int_sdram_send_cmd(0, FMC_SDRAM_CMD_LOAD_MODE, 1, mode_register);
  32. /* 刷新次数:64ms/8192行 = 7.8125us;按100MHz计算:781.25-20 = 761 */
  33. HAL_SDRAM_ProgramRefreshRate(&hsdram1, 761);
  34. }
  35. void Int_sdram_write_buffer(uint8_t *pbuf, uint32_t addr, uint32_t len)
  36. {
  37. uint32_t i;
  38. volatile uint8_t *p = (volatile uint8_t *)(INT_SDRAM_BASE_ADDR + addr);
  39. for (i = 0; i < len; i++)
  40. {
  41. *p++ = *pbuf++;
  42. }
  43. }
  44. void Int_sdram_read_buffer(uint8_t *pbuf, uint32_t addr, uint32_t len)
  45. {
  46. uint32_t i;
  47. volatile uint8_t *p = (volatile uint8_t *)(INT_SDRAM_BASE_ADDR + addr);
  48. for (i = 0; i < len; i++)
  49. {
  50. *pbuf++ = *p++;
  51. }
  52. }
  53. void Int_sdram_test(void)
  54. {
  55. uint32_t i;
  56. uint32_t *p = (uint32_t *)INT_SDRAM_BASE_ADDR;
  57. uint32_t errors = 0;
  58. printf("SDRAM Test Start...\r\n");
  59. for (i = 0; i < 1024; i++)
  60. {
  61. p[i] = 0x55AA55AA;
  62. }
  63. for (i = 0; i < 1024; i++)
  64. {
  65. if (p[i] != 0x55AA55AA)
  66. {
  67. errors++;
  68. }
  69. }
  70. printf("0x55AA55AA pattern: %lu errors\r\n", errors);
  71. errors = 0;
  72. for (i = 0; i < 1024; i++)
  73. {
  74. p[i] = 0xAA55AA55;
  75. }
  76. for (i = 0; i < 1024; i++)
  77. {
  78. if (p[i] != 0xAA55AA55)
  79. {
  80. errors++;
  81. }
  82. }
  83. printf("0xAA55AA55 pattern: %lu errors\r\n", errors);
  84. errors = 0;
  85. for (i = 0; i < 1024; i++)
  86. {
  87. p[i] = i;
  88. }
  89. for (i = 0; i < 1024; i++)
  90. {
  91. if (p[i] != i)
  92. {
  93. errors++;
  94. }
  95. }
  96. printf("Address pattern: %lu errors\r\n", errors);
  97. if (errors == 0)
  98. {
  99. printf("SDRAM Test PASSED!\r\n");
  100. }
  101. else
  102. {
  103. printf("SDRAM Test FAILED!\r\n");
  104. }
  105. }