📄 main.c
字号:
/*****************************************************************************
* 文件名 : main.c
*功能描述 :主程序
*******************************************************************************/
/* 头文件定义 ------------------------------------------------------------------*/
#include "fsmc_nor.h"
#include"stdio.h"
#include "main.h"
#define BUFFER_SIZE 0x400 /*缓存大小定义*/
#define WRITE_READ_ADDR 0x8000 /*读/写地址*/
extern void usart1(void); /*串口输出调试信息见usart1.c文件程序*/
GPIO_InitTypeDef GPIO_InitStructure;
ErrorStatus HSEStartUpStatus;
static vu32 TimingDelay = 0;
u16 TxBuffer[BUFFER_SIZE];
u16 RxBuffer[BUFFER_SIZE];
u32 WriteReadStatus = 0, Index = 0;
NOR_IDTypeDef NOR_ID; /*NOR闪存ID*/
/* 功能函数 -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void Fill_Buffer(u16 *pBuffer, u16 BufferLenght, u32 Offset);
int main(void)
{
#ifdef DEBUG
debug();
#endif
/* 系统时钟配置 */
RCC_Configuration();
/* 向量表配置 */
NVIC_Configuration();
/* systick配置 */
SysTick_Config();
/* LCD初始化 */
STM3210E_LCD_Init();
/* 清屏 */
LCD_Clear(White);
LCD_SetBackColor(Blue);
/* 设定LCD字体色=白色 */
LCD_SetTextColor(White);
LCD_DisplayStringLine(Line0, " EM_STM32E Demo ");
LCD_DisplayStringLine(Line1, " NorFlash_Test ");
LCD_DisplayStringLine(Line2, " welcome to Embest ");
LCD_DisplayStringLine(Line3, " www.embedinfo.com ");
LCD_SetBackColor(White); // 底色=白色 */
LCD_SetTextColor(Blue); // 字体色=蓝色 */
/* PF06和PF.07驱动D1和D2发光二极管 *****************************/
/* 使能GPIOF时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);
/*配置PF06,PF07复用推挽输出*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
usart1();
/* 配置FSMC Bank1 NOR/SRAM2 */
FSMC_NOR_Init();
FSMC_NOR_ReturnToReadMode();
printf("nor flash is erasing...\n");
LCD_DisplayStringLine(Line4, "nor flash erasing....");
/* 擦除NOR闪存块*/
FSMC_NOR_EraseBlock(WRITE_READ_ADDR);
/* 写NOR闪存 */
/* 缓存区数据填充,写缓存 */
Fill_Buffer(TxBuffer, BUFFER_SIZE, 0x3210);
printf("nor flash writing...\n");
LCD_DisplayStringLine(Line5, "nor flash writing...");
FSMC_NOR_WriteBuffer(TxBuffer, WRITE_READ_ADDR, BUFFER_SIZE);
printf("nor flash reading...\n");
LCD_DisplayStringLine(Line6, "nor flash reading...");
/* 从NOR闪存读数据 */
FSMC_NOR_ReadBuffer(RxBuffer, WRITE_READ_ADDR, BUFFER_SIZE);
/* 校验是正确性 */
for (Index = 0x00; (Index < BUFFER_SIZE) && (WriteReadStatus == 0); Index++)
{
if (RxBuffer[Index] != TxBuffer[Index])
{
WriteReadStatus = Index + 1;
}
}
if (WriteReadStatus == 0) /*如果正确*/
{
printf("nor flash operation success\n");/*输出操作成功提示*/
LCD_DisplayStringLine(Line7, " operation success ");/*LCD输出操作成功提示*/
GPIO_SetBits(GPIOF, GPIO_Pin_6); /* 点亮发光二极管D1=PF6 */
}
else /*如果不正确*/
{
/* 点亮D2 */
printf("nor flash operation error\n");/*输出操作失败提示*/
LCD_DisplayStringLine(Line7, "operation error");/*LCD输出操作失败提示*/
GPIO_SetBits(GPIOF, GPIO_Pin_7);/* 点亮发光二极管D2=PF7 */
}
while (1)
{
}
}
/*******************************************************************************
* 函数名 : RCC_Configuration
* 功能描述 : 配置系统时钟.
* 输入 : 无
* 输出 : 无
* 返回 : 无
*******************************************************************************/
void RCC_Configuration(void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
/* 使能FSMC时钟,AHB时钟是FSMC的参考时钟 */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
}
/*******************************************************************************
* 函数名 : NVIC_Configurationmain
* 功能描述 : 向量表基址配置.
* 输入 : 无
* 输出 : 无
* 返回 : 无
*******************************************************************************/
void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}
/*******************************************************************************
* 函数名 : Fill_Buffer
* 功能描述 : 全局缓存变量.
* 输入 : - pBuffer: 缓存指针
- BufferSize: 缓存空间大小
- Offset: 缓存基址
* 输出 : 无
* 返回 : 无
*******************************************************************************/
void Fill_Buffer(u16 *pBuffer, u16 BufferLenght, u32 Offset)
{
u16 IndexTmp = 0;
for (IndexTmp = 0; IndexTmp < BufferLenght; IndexTmp++ )
{
pBuffer[IndexTmp] = IndexTmp + Offset;
}
}
#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input : - file: pointer to the source file name
* - line: assert_param error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/*******************************************************************************
* 函数名 : SysTick_Config
* 功能描述 : 配置systick时钟10ms.
* 输入 : 无
* 输出 : 无
* 返回 : 无
*******************************************************************************/
void SysTick_Config(void)
{
/* 配置HCLK时钟做为SysTick时钟源 */
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
SysTick_SetReload(720000);
/* 使能SysTick 中断 */
SysTick_ITConfig(ENABLE);
}
/*******************************************************************************
* 函数名 : Delay
* 功能描述 : 延时.
* 输入 : nCount: 延时长度.
* 输出 : 无
* 返回 : 无
*******************************************************************************/
void Delay(u32 nCount)
{
TimingDelay = nCount;
/* 使能 SysTick 计数 */
SysTick_CounterCmd(SysTick_Counter_Enable);
while(TimingDelay != 0)
{
}
/* 禁止 SysTick 计数 */
SysTick_CounterCmd(SysTick_Counter_Disable);
/* 计数清零 */
SysTick_CounterCmd(SysTick_Counter_Clear);
}
/*******************************************************************************
* 函数名 : Decrement_TimingDelay
* 功能描述 : 延时递减.
* 输入 : 无
* 输出 : 延时量
* 返回 : 无
*******************************************************************************/
void Decrement_TimingDelay(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -