自己做了一块STM32F427IIG的板子,上面有一块ISSI SDRAM IS42S32160F, SDRAM接的是FMC_SDNE1和FMC_SDCKE1引脚. 自己写了一个简单的SDRAM读写程序,但只要读SDRAM中的内容就进入到BusFault_handler中断里面去。一开始用STM32Cube自动产生文件,也不行。现在自己就直接用寄存器配置还是不行,请做过这个的大侠帮忙看看程序有什么问题,谢谢!
#include "stm32f4xx_conf.h"
#define SDRAM_BASE_ADDR ((uint8_t *)(0xD0000000))
/************************* PLL Parameters *************************************/
void SystemInit(void)
{
/* Reset the RCC clock configuration to the default reset state ------------*/
RCC->CR |= 0x00000001; // Set HSION bit
RCC->CFGR = 0x00000000; // Reset CFGR register
RCC->CR &= 0xFEF6FFFF; // Reset HSEON, CSSON and PLLON bits
RCC->PLLCFGR = 0x24003010; // Reset PLLCFGR register
RCC->CR &= 0xFFFBFFFF; // Reset HSEBYP bit
RCC->CIR = 0x00000000; // Disable all interrupts
// Configure the System clock source, PLL Multiplier and Divider factors, AHB/APBx prescalers and Flash settings
RCC->CR |= 0x00010000; // Enable HSE
while(!(RCC->CR & 0x00020000)); // Wait till HSE is ready and if Time out is reached exit
RCC->APB1ENR |= 0x10000000; // Select regulator voltage output Scale 1 mode
PWR->CR |= PWR_CR_VOS;
RCC->CFGR = 0x00009400; // HCLK = SYSCLK / 1; PCLK2 = HCLK / 2; PCLK1 = HCLK / 4
RCC->PLLCFGR = 0x27405A19; // M = 25; N = 360; Q = 7; P = 2; PLLSRC = HSE
RCC->CR |= 0x01000000; // Enable the main PLL
while(!(RCC->CR & 0x02000000)) ; // Wait till the main PLL is ready
PWR->CR |= PWR_CR_ODEN; // Enable the Over-drive to extend the clock frequency to 180 Mhz
while((PWR->CSR & PWR_CSR_ODRDY) == 0);
PWR->CR |= PWR_CR_ODSWEN;
while((PWR->CSR & PWR_CSR_ODSWRDY) == 0);
FLASH->ACR = 0x00000705; // Enable Flash prefetch, Instruction cache, Data cache and 5 wait state
RCC->CFGR |= 0x00000002; // Select the main PLL as system clock source
while (RCC->CFGR & 0x0000000C != 0x00000008); // Wait till the main PLL is used as system clock source
RCC->AHB1ENR |= 0x0E0007FF; // Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH, GPIOI, GPIOK and GPIOJ interface clock
// Enable Ethernet Reception clock and Ethernet Transmission clock
RCC->AHB3ENR |= 0x00000001; // Enable the FSMC interface clock
}
void SystemInit_ExtMemCtl(void)
{;
register uint32_t index;
/*-- FMC Configuration ------------------------------------------------------*/
/* Configure and enable SDRAM bank2 */
FMC_Bank5_6->SDCR[1] = 0x00000969; // 9-bit column addr; 13-bit raw addr; CL = 2; SDCLK = 1/2 HCLK; WP disable; 4 banks, 32-bit
FMC_Bank5_6->SDTR[1] = 0x02227472; // TMRD = 2; TSXR = 7; TRAS = 4; TRC= 7; TWR = 2; TRP = 2; TRCD = 2;
/* SDRAM initialization sequence */
FMC_Bank5_6->SDCMR = 0x00000009; // Clock enable command
while(FMC_Bank5_6->SDSR & 0x00000020);
for (index = 0; index<10000; index++); // Delay at least 100us
FMC_Bank5_6->SDCMR = 0x0000000A; // PALL command
while(FMC_Bank5_6->SDSR & 0x00000020); //
FMC_Bank5_6->SDCMR = 0x0000000B; // Auto refresh command N = 1
while(FMC_Bank5_6->SDSR & 0x00000020);
FMC_Bank5_6->SDCMR = 0x0004400C; // MRD register program
while(FMC_Bank5_6->SDSR & 0x00000020);
FMC_Bank5_6->SDRTR = 0x00000381; // Set refresh count
}
void GPIO_Init1(void)
{
/* Connect PCx pins to FMC Alternate function */
GPIOC->AFR[0] = 0x0000000C;
// GPIOC->AFR[1] = 0x00007700;
/* Configure PCx pins in Alternate function mode */
GPIOC->MODER = 0x50000002;
/* Configure PCx pins speed to 50 MHz */
GPIOC->OSPEEDR = 0x00000002;
/* Configure PCx pins Output type to push-pull */
GPIOC->OTYPER = 0x00000000;
/* No pull-up, No pull-down for PCx pins */
GPIOC->PUPDR = 0x00000000;
/* Connect PDx pins to FMC Alternate function */
GPIOD->AFR[0] = 0x000000CC;
GPIOD->AFR[1] = 0xCC000CCC;
/* Configure PDx pins in Alternate function mode */
GPIOD->MODER = 0xA02A000A;
/* Configure PDx pins speed to 50 MHz */
GPIOD->OSPEEDR = 0xA02A000A;
/* Configure PDx pins Output type to push-pull */
GPIOD->OTYPER = 0x00000000;
/* No pull-up, pull-down for PDx pins */
GPIOD->PUPDR = 0x00000000;
/* Connect PEx pins to FMC Alternate function */
GPIOE->AFR[0] = 0xC00000CC;
GPIOE->AFR[1] = 0xCCCCCCCC;
/* Configure PEx pins in Alternate function mode */
GPIOE->MODER = 0xAAAA800A;
/* Configure PEx pins speed to 50 MHz */
GPIOE->OSPEEDR = 0xAAAA800A;
/* Configure PEx pins Output type to push-pull */
GPIOE->OTYPER = 0x00000000;
/* No pull-up, pull-down for PEx pins */
GPIOE->PUPDR = 0x00000000;
/* Connect PFx pins to FMC Alternate function */
GPIOF->AFR[0] = 0x00CCCCCC;
GPIOF->AFR[1] = 0xCCCCC000;
/* Configure PFx pins in Alternate function mode */
GPIOF->MODER = 0xAA800AAA;
/* Configure PFx pins speed to 50 MHz */
GPIOF->OSPEEDR = 0xAA800AAA;
/* Configure PFx pins Output type to push-pull */
GPIOF->OTYPER = 0x00000000;
/* No pull-up, pull-down for PFx pins */
GPIOF->PUPDR = 0x00000000;
/* Connect PGx pins to FMC Alternate function */
GPIOG->AFR[0] = 0x00CC0CCC;
GPIOG->AFR[1] = 0xC000000C;
/* Configure PGx pins in Alternate function mode */
GPIOG->MODER = 0x80020A2A;
/* Configure PGx pins speed to 50 MHz */
GPIOG->OSPEEDR = 0x80020A2A;
/* Configure PGx pins Output type to push-pull */
GPIOG->OTYPER = 0x00000000;
/* No pull-up, pull-down for PGx pins */
GPIOG->PUPDR = 0x00000000;
/* Connect PHx pins to FMC Alternate function */
GPIOH->AFR[0] = 0xCC000000;
GPIOH->AFR[1] = 0xCCCCCCCC;
/* Configure PHx pins in Alternate function mode */
GPIOH->MODER = 0xAAAAA000;
/* Configure PHx pins speed to 50 MHz */
GPIOH->OSPEEDR = 0xAAAAA000;
/* Configure PHx pins Output type to push-pull */
GPIOH->OTYPER = 0x00000000;
/* No pull-up, pull-down for PHx pins */
GPIOH->PUPDR = 0x00000000;
/* Connect PIx pins to FMC Alternate function */
GPIOI->AFR[0] = 0xCCCCCCCC;
GPIOI->AFR[1] = 0x00000CC0;
/* Configure PIx pins in Alternate function mode */
GPIOI->MODER = 0x0028AAAA;
/* Configure PIx pins speed to 50 MHz */
GPIOI->OSPEEDR = 0x0028AAAA;
/* Configure PIx pins Output type to push-pull */
GPIOI->OTYPER = 0x00000000;
/* No pull-up, pull-down for PIx pins */
GPIOI->PUPDR = 0x00000000;
}
int main()
{
uint32_t tmpreg;
uint8_t *addr, i, temp;
SystemInit();
GPIO_Init1();
SystemInit_ExtMemCtl();
GPIOC->ODR = 0x00008000;
addr = SDRAM_BASE_ADDR;
*addr = 0xAA;
temp = *addr; // 只要执行到这句就进入到busfault中断里去出不来了
while(1)
{
for(tmpreg = 0; tmpreg < 8000000; tmpreg++);
if(temp == 0xAA)
GPIOC->ODR = GPIOC->ODR ^ 0x0000C000;
else
GPIOC->ODR = GPIOC->ODR ^ 0x00004000;
};
}
main.rar
(1.96 KB, 下载次数: 2)
2018-8-28 19:21 上传
点击文件名下载附件
程序
一周热门 更多>