Int_sdram.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. /* 刷新间隔 (COUNT+20)/SDCLK, SDCLK = PLL2 166/2 = 83MHz。
  33. * 304 → 3.90us ⇒ 8192 行全覆盖 32ms(上限 64ms, 留一倍); 带宽 ~2%。 */
  34. HAL_SDRAM_ProgramRefreshRate(&hsdram1, 304);
  35. }
  36. void Int_sdram_write_buffer(uint8_t *pbuf, uint32_t addr, uint32_t len)
  37. {
  38. uint32_t i;
  39. volatile uint8_t *p = (volatile uint8_t *)(INT_SDRAM_BASE_ADDR + addr);
  40. for (i = 0; i < len; i++)
  41. {
  42. *p++ = *pbuf++;
  43. }
  44. }
  45. void Int_sdram_read_buffer(uint8_t *pbuf, uint32_t addr, uint32_t len)
  46. {
  47. uint32_t i;
  48. volatile uint8_t *p = (volatile uint8_t *)(INT_SDRAM_BASE_ADDR + addr);
  49. for (i = 0; i < len; i++)
  50. {
  51. *pbuf++ = *p++;
  52. }
  53. }