| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- #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);
- /* 刷新次数:64ms/8192行 = 7.8125us;按100MHz计算:781.25-20 = 761 */
- HAL_SDRAM_ProgramRefreshRate(&hsdram1, 761);
- }
- 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++;
- }
- }
- void Int_sdram_test(void)
- {
- uint32_t i;
- uint32_t *p = (uint32_t *)INT_SDRAM_BASE_ADDR;
- uint32_t errors = 0;
- printf("SDRAM Test Start...\r\n");
- for (i = 0; i < 1024; i++)
- {
- p[i] = 0x55AA55AA;
- }
- for (i = 0; i < 1024; i++)
- {
- if (p[i] != 0x55AA55AA)
- {
- errors++;
- }
- }
- printf("0x55AA55AA pattern: %lu errors\r\n", errors);
- errors = 0;
- for (i = 0; i < 1024; i++)
- {
- p[i] = 0xAA55AA55;
- }
- for (i = 0; i < 1024; i++)
- {
- if (p[i] != 0xAA55AA55)
- {
- errors++;
- }
- }
- printf("0xAA55AA55 pattern: %lu errors\r\n", errors);
- errors = 0;
- for (i = 0; i < 1024; i++)
- {
- p[i] = i;
- }
- for (i = 0; i < 1024; i++)
- {
- if (p[i] != i)
- {
- errors++;
- }
- }
- printf("Address pattern: %lu errors\r\n", errors);
- if (errors == 0)
- {
- printf("SDRAM Test PASSED!\r\n");
- }
- else
- {
- printf("SDRAM Test FAILED!\r\n");
- }
- }
|