www.pudn.com > STM32F4_bootloader.zip > main.c, change:2017-05-09,size:3998b


#include "stm32f4xx.h" 
#include "usart.h" 
#include "delay.h" 
#include "exfuns.h" 
#include "iap.h" 
#include "sdio_sdcard.h" 
#include "24cxx.h" 
 
//LED端口定义 
#define RUN PBout(1)	  // DS0 
#define FAU PBout(0)	// DS1	  
//#define BEEP PEout(10) 
 
void LED_init(void) 
{ 
  GPIO_InitTypeDef  GPIO_InitStructure; 
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); 
 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; 
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; 
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 
  GPIO_Init(GPIOB, &GPIO_InitStructure); 
	 
//	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 
//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; 
//  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
//  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; 
//  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 
//  GPIO_Init(GPIOE, &GPIO_InitStructure);	 
	 
} 
 
int main(void) 
{ 
	u32 t=0; 
	u8 temp[5]={0}; 
	u16 version = 0; 
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组 
	delay_init(164);	   //延迟函数初始化 
	LED_init();          //初始化指示灯 
	RUN = 0; 
	FAU = 0; 
 
	uart2_init(115200);   //串口2初始化 
	AT24CXX_Init(); 
	while(SD_Init())//检测不到SD卡  1秒一次的双闪是没有SD卡 
	{ 
    RUN =!RUN;   //DS0闪烁 
		FAU =!FAU; 
		delay_ms(500); 
		t++; 
		if(t>10)break; 
	} 
//	SD_Init(); 
	delay_ms(500); 
	AT24CXX_Read(0,temp,4); //读取当前版本号 转换为4位数的10进制数 
	if((temp[0] >= '0')&&(temp[0] <= '9') && (temp[1] >= '0') && (temp[1] <= '9') && 
		 (temp[2] >= '0')&&(temp[2] <= '9') && (temp[3] >= '0') && (temp[3] <= '9')) 
	{ 
	 version = (temp[0]-'0')*1000 + (temp[1]-'0')*100 +(temp[2]-'0')*10 +(temp[3]-'0');	  
	} 
	 
	t = Check_for_Updates(version);  
	 
	if(t==1)  
	{ 
		printf("当前已是最新版本\r\n");  //1秒频率闪两下直接运行不更新程序 
    RUN =1;   //DS0闪烁 
		FAU =1; 
		delay_ms(500); 
		RUN =0;   //DS0闪烁 
		FAU =0; 
		delay_ms(500); 
		RUN =1;   //DS0闪烁 
		FAU =1; 
		delay_ms(500); 
		RUN =0;   //DS0闪烁 
		FAU =0; 
		delay_ms(500); 
		 
	} 
	else if(t==2)printf("文件打开失败\r\n"); 
	else if(t==3)printf("文件写入失败\r\n"); 
	else  
	{ 
		printf("程序更新成功\r\n");  //1秒频率闪三下更新程序成功 
		RUN = 1; 
		 
    RUN =1;   //DS0闪烁 
		FAU =1; 
		delay_ms(500); 
		RUN =0;   //DS0闪烁 
		FAU =0; 
		delay_ms(500); 
		RUN =1;   //DS0闪烁 
		FAU =1; 
		delay_ms(500); 
		RUN =0;   //DS0闪烁 
		FAU =0; 
		delay_ms(500); 
		RUN =1;   //DS0闪烁 
		FAU =1; 
		delay_ms(500); 
		RUN =0;   //DS0闪烁 
		FAU =0; 
		delay_ms(500); 
	} 
	if(((*(vu32*)(FLASH_APP_ADDR+4))&0xFF000000)==0x08000000)//判断是否为0X08XXXXXX. 
  {	  
    printf("准备跳转到App程序处开始执行\r\n"); 
		delay_ms(50); 
    iap_load_app(FLASH_APP_ADDR);//执行FLASH APP代码 
  } 
	else  
  { 
    printf("App程序运行失败!\r\n");	    
  } 
 
	 
  while(1){ 
    //printf("t:%d\r\n",t); 
		RUN =!RUN;   //DS0闪烁 
		FAU =!FAU; 
		delay_ms(200); 
		t++; 
	} 
} 
 
/* 
手册中讲解到步骤15的时候的main.c源码如下: 
#include "stm32f4xx.h" 
 
//ALIENTEK 探索者STM32F407开发板 实验0 
//STM32F4工程模板-库函数版本 
//技术支持:www.openedv.com 
//淘宝店铺:http://eboard.taobao.com 
//广州市星翼电子科技有限公司   
//作者:正点原子 @ALIENTEK 
   
void Delay(__IO uint32_t nCount); 
 
void Delay(__IO uint32_t nCount) 
{ 
  while(nCount--){} 
} 
 
int main(void) 
{ 
 
  GPIO_InitTypeDef  GPIO_InitStructure; 
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); 
 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; 
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; 
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 
  GPIO_Init(GPIOF, &GPIO_InitStructure); 
 
  while(1){ 
		GPIO_SetBits(GPIOF,GPIO_Pin_9|GPIO_Pin_10); 
		Delay(0x7FFFFF); 
		GPIO_ResetBits(GPIOF,GPIO_Pin_9|GPIO_Pin_10); 
		Delay(0x7FFFFF); 
	 
	} 
} 
*/