/**********主程序*******/ int main() { #ifdef DEBUG debug(); #endif /* System Clocks Configuration */ SystemInit(); /* Enable the FSMC Clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE); /* Configure FSMC */ FSMC_LCD_Init(); /* Init for LCD */ LCD_Setup(); EXTI_Config(); SPI_Flash_Init(); NVIC_Configuration(); /*变量初始化*/ GlobalStateFlag=0; MenuFlag=1; Pic_Addr=gImage_picture1; HomePage_Disp(); /* Infinite loop */ while (1) { } }
/**************************************************************************** * 名 称:FS_Init * 功 能:初始化系统配置,小车配置等信息 * 参 数:无 * 反 回:无 * 说 明: ****************************************************************************/ void FS_Init(void) { #if SPI_FLASH_DEBUG u16 ChipID = 0; #endif SPI_Flash_Init(); #if SPI_FLASH_DEBUG ChipID = SPI_Flash_ReadID(); if((ChipID & FLASH_ID) == FLASH_ID) printf("Read SPI Flash Chip ID Success\r\n"); else printf("Read SPI Flash Chip ID Error\r\n"); #endif SPI_Flash_Read( CAR_CFG_OFFSET, (u8*)&Car_Save_Info, sizeof(Car_Save_Info)); if( strcmp( Car_Save_Info.CheckStr , CAR_CHECK_STR) != NULL ) { #if SPI_FLASH_DEBUG printf("Read CarPara Error,Write to Default Info\r\n"); #endif memset( (char*)&Car_Save_Info, 0x00, sizeof(Car_Save_Info)); sprintf(Car_Save_Info.CheckStr,"%s",CAR_CHECK_STR); Car_Save_Info.CarPara.RunMode = CAR_MODE_MANUAL; Car_Save_Info.CarPara.SetSpeed = 15; SPI_Flash_Write( CAR_CFG_OFFSET, (unsigned char *)&Car_Save_Info, sizeof(Car_Save_Info)); } #if SPI_FLASH_DEBUG printf("Read CarPara Success\r\n"); #endif SPI_Flash_Read( ROUTE_CFG_OFFSET, (u8*)&Route_Save_Info, sizeof(Route_Save_Info)); if( strcmp( Route_Save_Info.CheckStr , ROUTE_CHECK_STR) != NULL ) { #if SPI_FLASH_DEBUG printf("Read Route Error,Write to Default Info\r\n"); #endif memset( (char*)&Route_Save_Info, 0x00, sizeof(Route_Save_Info)); sprintf(Route_Save_Info.CheckStr,"%s",ROUTE_CHECK_STR); SPI_Flash_Write( ROUTE_CFG_OFFSET, (unsigned char *)&Route_Save_Info, sizeof(Route_Save_Info)); } #if SPI_FLASH_DEBUG printf("Read Route Success\r\n"); #endif }
/** * @brief Main routine for MSC class application * @param None * @retval int */ int main(void) { NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0xC000);//初始化启动项 gpio_init();//初始化所有定义的gpio寄存器 cpu_run(); flash_stop(); // rs485_recv();//使能485接受数据 FLASH_ON(); remote_init(); LOOP_ON();//使能环流 // uart_init(9600);//设置波特率为9600 串口初始化 delay_ms(1);//定时器 SPI_Flash_Init();//初始化spi flash外围驱动 get_cfg_from_flash();//得到配置信息 (获取cfg文件的配置) get_line_from_flash(); message_state |= MESSAGE_LINE_NEW; message_state |= MESSAGE_LEFT_NEW; message_state |= MESSAGE_RIGHT_NEW; message_state |= MESSAGE_BRAKE_NEW; alloc_memory(); //初始化缓存 wwdg_init(0x7F, 0x5F, WWDG_Prescaler_8); //计数器值为7f,窗口寄存器为5f,分频数为8 (使用窗口看门狗的原因是他可以被中断) while (1) { if(uart_get_flag) { uart_get_flag = 0; data_process(); LED_OK = 1; } get_disp_state(); //左转右转还有刹车的判断报站; if(bflag) { LED_DispString(1, 1, disp_buff); //固定显示 } else { LED_ScrollDispHZ(1, 1, disp_buff);//滚动显示 } } }
DSTATUS disk_initialize ( BYTE drv /* Physical drive nmuber (0..) */ ) { u8 res=0; switch(drv) { case SD_CARD://SD卡 res = SD_Init();//SD_Initialize() break; case EX_FLASH://外部flash SPI_Flash_Init(); if(SPI_FLASH_TYPE==W25Q64)FLASH_SECTOR_COUNT=2048*6;//W25Q64 else FLASH_SECTOR_COUNT=2048*2; //其他 break; default: res=1; } if(res)return STA_NOINIT; else return 0; //初始化成功 }
int main(void) { u8 offline_cnt=0; u8 tct=0; u8 USB_STA; u8 Divece_STA; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//中断分组设置 delay_init(); //延时函数初始化 uart_init(9600); //串口初始化为9600 LCD_Init(); //初始化液晶 LED_Init(); //LED初始化 KEY_Init(); //按键初始化 POINT_COLOR=RED; //设置字体为红色 LCD_ShowString(60,50,200,16,16,"Mini STM32"); LCD_ShowString(60,70,200,16,16,"USB Card Reader TEST"); LCD_ShowString(60,90,200,16,16,"ATOM@ALIENTEK"); LCD_ShowString(60,110,200,16,16,"2014/3/15"); SPI_Flash_Init(); if(SD_Initialize())LCD_ShowString(60,130,200,16,16,"SD Card Error!"); //检测SD卡错误 else //SD 卡正常 { LCD_ShowString(60,130,200,16,16,"SD Card Size: MB"); Mass_Memory_Size[0]=(long long)SD_GetSectorCount()*512;//得到SD卡容量(字节),当SD卡容量超过4G的时候,需要用到两个u32来表示 Mass_Block_Size[0] =512;//因为我们在Init里面设置了SD卡的操作字节为512个,所以这里一定是512个字节. Mass_Block_Count[0]=Mass_Memory_Size[0]/Mass_Block_Size[0]; LCD_ShowNum(164,130,Mass_Memory_Size[0]>>20,5,16); //显示SD卡容量 } if(SPI_FLASH_TYPE!=W25Q64)LCD_ShowString(60,130,200,16,16,"W25Q64 Error!"); //检测SD卡错误 else //SPI FLASH 正常 { Mass_Memory_Size[1]=4916*1024;//前4.8M字节 Mass_Block_Size[1] =512;//因为我们在Init里面设置了SD卡的操作字节为512个,所以这里一定是512个字节. Mass_Block_Count[1]=Mass_Memory_Size[1]/Mass_Block_Size[1]; LCD_ShowString(60,150,200,16,16,"SPI FLASH Size:4916KB"); } delay_ms(1800); USB_Port_Set(0); //USB先断开 delay_ms(300); USB_Port_Set(1); //USB再次连接 LCD_ShowString(60,170,200,16,16,"USB Connecting...");//提示SD卡已经准备了 Data_Buffer=mymalloc(BULK_MAX_PACKET_SIZE*2*4); //为USB数据缓存区申请内存 Bulk_Data_Buff=mymalloc(BULK_MAX_PACKET_SIZE); //申请内存 //USB配置 USB_Interrupts_Config(); Set_USBClock(); USB_Init(); delay_ms(1800); while(1) { delay_ms(1); if(USB_STA!=USB_STATUS_REG)//状态改变了 { LCD_Fill(60,190,240,190+16,WHITE);//清除显示 if(USB_STATUS_REG&0x01)//正在写 { LCD_ShowString(60,190,200,16,16,"USB Writing...");//提示USB正在写入数据 } if(USB_STATUS_REG&0x02)//正在读 { LCD_ShowString(60,190,200,16,16,"USB Reading...");//提示USB正在读出数据 } if(USB_STATUS_REG&0x04)LCD_ShowString(60,210,200,16,16,"USB Write Err ");//提示写入错误 else LCD_Fill(60,210,240,210+16,WHITE);//清除显示 if(USB_STATUS_REG&0x08)LCD_ShowString(60,230,200,16,16,"USB Read Err ");//提示读出错误 else LCD_Fill(60,230,240,230+16,WHITE);//清除显示 USB_STA=USB_STATUS_REG;//记录最后的状态 } if(Divece_STA!=bDeviceState) { if(bDeviceState==CONFIGURED)LCD_ShowString(60,170,200,16,16,"USB Connected ");//提示USB连接已经建立 else LCD_ShowString(60,170,200,16,16,"USB DisConnected ");//提示USB被拔出了 Divece_STA=bDeviceState; } tct++; if(tct==200) { tct=0; LED0=!LED0;//提示系统在运行 if(USB_STATUS_REG&0x10) { offline_cnt=0;//USB连接了,则清除offline计数器 bDeviceState=CONFIGURED; }else//没有得到轮询 { offline_cnt++; if(offline_cnt>10)bDeviceState=UNCONNECTED;//2s内没收到在线标记,代表USB被拔出了 } USB_STATUS_REG=0; } }; }
int main(void) { u8 Data_PC[SIZE]; //从Flash中读取数据到Data_PC,并上传至上位机 u16 a,b=0; delay_init(); //延时函数初始化 NVIC_Configuration();//设置NVIC中断分组2:2位抢占优先级,2位响应优先级 uart_init(9600); //串口初始化为9600 LED_Init(); //LED端口初始化 LCD_Init(); KEY_Init(); WORD_Init(); TIM2_Int_Init(999,7199); TIM3_Int_Init(9,7199); EXTIX_Init(); //外部中断初始化 Adc_Init(); //ADC初始化 SPI_Flash_Init(); //SPI FLASH 初始化 while(1) { //功能:开机状态显示 //说明:1.开机,绿灯亮(DS1) // 2.使用temp是为了避免重复执行该if语句 if(flag2==1&&temp==0) { while(SPI_Flash_ReadID()!=W25Q64) //检测不到W25Q64 { LCD_ShowString(40,150,200,16,16,"W25Q64 Check Failed!"); delay_ms(500); LCD_ShowString(40,150,200,16,16,"Please Check! "); delay_ms(500); LED0=!LED0;//DS0闪烁 } LCD_ShowString(60,130,200,16,16,"W25Q64 Ready!"); LED1=0; temp=1; } //功能:数据采集并保存至Flash中 //说明:1.使用temp1是为了避免重复使能TIM3 // 2.使用temp2是为了避免在数据上传至上位机的过程中,与终止上传功能相冲突 // 3.flag1=1代表按键按下,并开始采集 // 4.flag2=1代表开机,flag2=0代表未开机 // 5.采集过程中,红灯(DS0)以一定频率闪烁 if(flag2==1&&temp2==0&&flag1==1) { if(temp1==0) { LCD_ShowString(20,150,200,16,16,"Data collection began..."); TIM_Cmd(TIM3, ENABLE); //使能TIM3中断 LED0=0; } temp1=1; if(flag_cycle==0&&flag_cycle_1==0) { SPI_Flash_Write((u8*)TEXT_Buffer2,FLASH_SIZE+counter*SIZE,SIZE); counter++; flag_cycle_1=1; } if(flag_cycle==1&&flag_cycle_1==0) { SPI_Flash_Write((u8*)TEXT_Buffer1,FLASH_SIZE+counter*SIZE,SIZE); counter++; flag_cycle_1=1; } } //功能:将Flash中采集到的数据上传至上位机 //说明:1.使用temp1是为了保证上传是在采集之后进行 // 2.使用temp3是为了避免由于按键误操作使得该程序重复被执行 // 3.flag1=0代表按键再次被按下,上传开始 ;若再次被按下,则上传被终止;之后再按就没有作用了 // 4.上传过程中,红灯(DS0)以一定频率闪烁 if(temp1==1&&temp3==0&&flag1==0) { TIM_Cmd(TIM3, DISABLE); //失能TIM3中断 //用于解决当采集时间不是42ms的倍数时,部分采集数据未写入FLash的问题 if((time_collect-1)%42!=0) { data_residue=(time_collect-1)%42*6; if(flag_cycle==0) { SPI_Flash_Write((u8*)TEXT_Buffer2,FLASH_SIZE+(counter+1)*SIZE,SIZE); } if(flag_cycle==1) { SPI_Flash_Write((u8*)TEXT_Buffer1,FLASH_SIZE+(counter+1)*SIZE,SIZE); } } LCD_ShowString(20,170,200,16,16,"Data collection complete!"); LCD_ShowString(20,190,200,16,16,"Data is being uploaded..."); temp2=1; for(a=1;a<counter;a++) { LED0=!LED0; SPI_Flash_Read(Data_PC,FLASH_SIZE+a*SIZE,SIZE); for(b=0;b<SIZE;b++) { if(flag1==1) //上传中途使用按键停止传送 { break; } USART_GetFlagStatus(USART1, USART_FLAG_TC); USART_SendData(USART1,Data_PC[b]); while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET); } if(flag1==1) //上传中途使用按键停止传送 { LCD_ShowString(20,210,200,16,16,"Data upload is terminated!"); break; } } //用于解决当采集时间不是42ms的倍数时,部分采集数据未写入FLash的问题 if(flag1!=1&&(time_collect-1)%42!=0) { LED0=!LED0; SPI_Flash_Read(Data_PC,FLASH_SIZE+(a+1)*SIZE,SIZE); for(b=0;b<data_residue;b++) { if(flag1==1) //上传中途使用按键停止传送 { break; } USART_GetFlagStatus(USART1, USART_FLAG_TC); USART_SendData(USART1,Data_PC[b]); while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET); } if(flag1==1) //上传中途使用按键停止传送 { LCD_ShowString(20,210,200,16,16,"Data upload is terminated!"); break; } } if(flag1!=1) //要么显示数据上传完成,要么显示数据上传完成 { LCD_ShowString(20,210,200,16,16,"Data upload is completed!"); } temp3=1; } //功能:关机并擦除Flash //说明:1.flag2=0代表关机,与上temp1,temp2是要保证前面的已被执行 // 2.关机过程中擦除Flash,完成之后红绿灯同时亮一秒后灭 if(temp1==1&&temp2==1&&flag2==0) //关机机状态显示 { flag1=0; flag2=0; temp1=0; temp2=0; LED0=1; LED1=1; SPI_Flash_Erase_Chip(); LED0=0; LED1=0; delay_ms(1000); LED0=1; LED1=1; LCD_ShowString(80,230,200,16,16,"Shutdown!"); } } }
DSTATUS disk_initialize ( BYTE drv /* Physical drive nmuber (0..) */ ) { SD_CardInfo mSDCardInfo; /* 定义SD卡状态信息结构体 */ uint32_t DeviceSizeMul = 0, NumberOfBlocks = 0; uint16_t Status; switch(drv) { case SD_CARD://SD卡 //SD卡初始化-------------------------------------------------------------------------- Status = SD_Init(); if (Status != SD_OK) { fs_printf("SD_Init() fail (%d) : file %s on line %d\r\n", Status, __FILE__, __LINE__); goto retfail; } SD_GetCardInfo(&mSDCardInfo); /* 读取SD卡的信息 */ SD_SelectDeselect((uint32_t) (mSDCardInfo.RCA << 16)); DeviceSizeMul = (mSDCardInfo.SD_csd.DeviceSizeMul + 2); if (mSDCardInfo.CardType == SDIO_HIGH_CAPACITY_SD_CARD) /* 高容量SD卡 SDHC */ { Mass_Block_Count = (mSDCardInfo.SD_csd.DeviceSize + 1) * 1024; } else /* 普通SD卡, 最大4G */ { NumberOfBlocks = ((1 << (mSDCardInfo.SD_csd.RdBlockLen)) / 512); Mass_Block_Count = ((mSDCardInfo.SD_csd.DeviceSize + 1) * (1 << DeviceSizeMul) << (NumberOfBlocks/2)); } Status = SD_SelectDeselect((uint32_t) (mSDCardInfo.RCA << 16)); Status = SD_EnableWideBusOperation(SDIO_BusWide_4b); if (Status != SD_OK) { fs_printf("SD_EnableWideBusOperation(SDIO_BusWide_4b) Fail (%d)\r\n", Status); goto retfail; } Status = SD_SetDeviceMode(SD_DMA_MODE); /* 设置SD卡工作模式为DMA, 其它模式由中断、轮询 */ if (Status != SD_OK) { fs_printf("SD_SetDeviceMode(SD_DMA_MODE) Fail (%d)\r\n", Status); goto retfail; } /* 显示容量 */ Mass_Block_Size = 512; if (mSDCardInfo.CardType == SDIO_HIGH_CAPACITY_SD_CARD) /* 高容量SD卡 SDHC */ { fs_printf("SDHC Card, Memory Size = %uMB\r\n", Mass_Block_Count / (1024 * 4)); } else { fs_printf("Normal Card, Memory Size = %uMB\r\n", (Mass_Block_Count * Mass_Block_Size) /(1024*1024)); } //SD卡初始化结束-------------------------------------------------------------------------- break; case EX_FLASH://外部flash SPI_Flash_Init(); if(SPI_FLASH_TYPE==W25Q64)FLASH_SECTOR_COUNT=2048*6;//W25Q64 else FLASH_SECTOR_COUNT=2048*2; //其他 break; default: goto retfail; } return RES_OK; retfail: return RES_ERROR; }