| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- #include "Int_sdram.h"
- #include "fmc.h"
- #include <stdio.h>
- #include <string.h>
- extern SDRAM_HandleTypeDef hsdram1;
- static uint8_t Int_sdram_send_cmd(uint8_t bank, uint8_t cmd, uint8_t refresh, uint16_t regval)
- {
- FMC_SDRAM_CommandTypeDef command = {0};
- command.CommandMode = cmd;
- command.CommandTarget = (bank == 0) ? FMC_SDRAM_CMD_TARGET_BANK1 : FMC_SDRAM_CMD_TARGET_BANK2;
- command.AutoRefreshNumber = refresh;
- command.ModeRegisterDefinition = regval;
- if (HAL_SDRAM_SendCommand(&hsdram1, &command, 0xFFFF) == HAL_OK)
- {
- return 0;
- }
- return 1;
- }
- void Int_sdram_init(void)
- {
- uint32_t mode_register;
- Int_sdram_send_cmd(0, FMC_SDRAM_CMD_CLK_ENABLE, 1, 0);
- HAL_Delay(10);
- Int_sdram_send_cmd(0, FMC_SDRAM_CMD_PALL, 1, 0);
- Int_sdram_send_cmd(0, FMC_SDRAM_CMD_AUTOREFRESH_MODE, 8, 0);
- /* 模式寄存器:BL=1(000), CAS=3(011), 顺序突发, 单次写入(1<<9) */
- mode_register = (uint32_t)(0x0000 |
- 0x0030 |
- 0x0000 |
- 0x0200);
- Int_sdram_send_cmd(0, FMC_SDRAM_CMD_LOAD_MODE, 1, mode_register);
- /* 刷新间隔 (COUNT+20)/SDCLK, SDCLK = PLL2 166/2 = 83MHz。
- * 304 → 3.90us ⇒ 8192 行全覆盖 32ms(上限 64ms, 留一倍); 带宽 ~2%。 */
- HAL_SDRAM_ProgramRefreshRate(&hsdram1, 304);
- }
- void Int_sdram_write_buffer(uint8_t *pbuf, uint32_t addr, uint32_t len)
- {
- uint32_t i;
- volatile uint8_t *p = (volatile uint8_t *)(INT_SDRAM_BASE_ADDR + addr);
- for (i = 0; i < len; i++)
- {
- *p++ = *pbuf++;
- }
- }
- void Int_sdram_read_buffer(uint8_t *pbuf, uint32_t addr, uint32_t len)
- {
- uint32_t i;
- volatile uint8_t *p = (volatile uint8_t *)(INT_SDRAM_BASE_ADDR + addr);
- for (i = 0; i < len; i++)
- {
- *pbuf++ = *p++;
- }
- }
|