📄 main.c
字号:
/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
* File Name : main.c
* Author : MCD Application Team
* Version : V2.0
* Date : 05/23/2008
* DescriptWHY8W-QT9DJ-P0VNX-L15PE-W4DCR-VP3FMion : Main program body
********************************************************************************
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
* FOR MORE INFORMATION PLEASE CAREFULLY READ THE LICENSE AGREEMENT FILE LOCATED
* IN THE ROOT DIRECTORY OF THIS FIRMWARE PACKAGE.
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "config.h"
#include "dataflash.h"
#include "string.h"
#include "stm32f10x_lib.h"
/* Private functions ---------------------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void USART_Configuration(void);
void Time_Adjust(void);
void Time_Show(void);
void Time_Display();
u8 USART_Scanf(u32 value);
void RTC_Config(void);
void Update(void);
void CheckSD(void);
void DataFlashInit(void);
FRESULT scan_files(char* path);
FRESULT RemoveFile(void);
void CopyFiles(void);
void TIM_TRIGGER_INIT(void);
void TIM_Configuration(void);
u16 filter(void);
void write_file_tmp(void);
#define RTCClockOutput_Enable /* RTC Clock/64 is output on tamper pin(PC.13) */
#define ADC1_DR_Address ((u32)0x4001244C)
#define FILEAMOUNT 11
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
ErrorStatus HSEStartUpStatus;
FATFS fs[2]; // Work area (file system object) for logical drive
FIL fsrc, fdst, fap; // file objects
FRESULT res, re; // FatFs function common result code
UINT br, bw, bt;
; // File R/W count
FILINFO finfo, fno;
DIR dirs;
time_t current_time;
struct tm time_now;
u8 passwordflag;
u8 timedata[30];
u8 password[6] = 0;
u8 timeFirstSet = 0;
char fileNames[30][30];
char machine[20] = "machine1";
char shellEnable = FALSE;
char sdFlag = 0;
int main(void) {
#ifdef DEBUG
debug();
#endif
RCC_Configuration();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_Configuration();
USART_Configuration();
/* NVIC configuration */
NVIC_Configuration();
//初始化延时函数
delay_init(72);
/* GPIO configuration */
// SPI_Configuration();
MSD_SPIConfig();
// bw = SD_Init();
MSD_Init();
SPI_FLASH_Init();
f_mount(0, &fs[0]);
f_mount(1, &fs[1]);
RTC_Config();
DataFlashInit();
res=f_mkfs(0,1,0);
//写入配置文件
write_file_tmp();
Time_Show();
/* Display time in infinite loop */
}
#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
void TIM_TRIGGER_INIT(void) {
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0x00;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); // Time base configuration
TIM_ETRClockMode2Config(TIM1, TIM_ExtTRGPSC_OFF,
TIM_ExtTRGPolarity_NonInverted, 0);
TIM_SetCounter(TIM1, 0);
TIM_Cmd(TIM1, ENABLE);
}
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
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) {
}
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB
| RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD
| RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF
| RCC_APB2Periph_GPIOG, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); /* TIM1 clock enable */
/* TIM2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
}
void TIM_Configuration(void) {
// TIM_OCInitTypeDef TIM_OCInitStructure ;
TIM_DeInit( TIM2);//复位TIM2定时器
/* TIM2 configuration */
TIM_TimeBaseStructure.TIM_Period = 1000; //最大计数值0xffff
TIM_TimeBaseStructure.TIM_Prescaler = 35999;//分频0x36
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; // 时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数方向向上计数
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Clear TIM2 update pending flag[清除TIM2溢出中断标志] */
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
/* Enable TIM2 Update interrupt [TIM2溢出中断允许]*/
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
/* TIM2 enable counter [允许tim2计数]*/
TIM_Cmd(TIM2, ENABLE);
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures Vector Table base location.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(void) {
NVIC_InitTypeDef NVIC_InitStructure;
#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
NVIC_PriorityGroupConfig( NVIC_PriorityGroup_1);
/* Enable the RTC Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART_Configuration() {
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* Configure USARTx_Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USARTx_Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl
= USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
void GPIO_Configuration(void) {
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure IO connected to LD1, LD2, LD3 and LD4 leds *********************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* user button ******************************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* SD SIGNAL ******************************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
////////////////////////////////////////////////////////////////////////////////
// RTC时钟初始化!
////////////////////////////////////////////////////////////////////////////////
/*******************************************************************************
* Function Name : RTC_Configuration
* Description : 来重新配置RTC和BKP,仅在检测到后备寄存器数据丢失时使用
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RTC_Configuration(void) {
//启用PWR和BKP的时钟(from APB1)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
//后备域解锁
PWR_BackupAccessCmd( ENABLE);
//备份寄存器模块复位
BKP_DeInit();
//外部32.768K其哟偶那个
RCC_LSEConfig( RCC_LSE_ON);
//等待稳定
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
;
//RTC时钟源配置成LSE(外部32.768K)
RCC_RTCCLKConfig( RCC_RTCCLKSource_LSE);
//RTC开启
RCC_RTCCLKCmd(ENABLE);
//开启后需要等待APB1时钟与RTC时钟同步,才能读写寄存器
RTC_WaitForSynchro();
//读写寄存器前,要确定上一个操作已经结束
RTC_WaitForLastTask();
//设置RTC分频器,使RTC时钟为1Hz
//RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1)
RTC_SetPrescaler(32767);
//等待寄存器写入完成
RTC_WaitForLastTask();
//使能秒中断
RTC_ITConfig(RTC_IT_SEC, ENABLE);
//等待写入完成
RTC_WaitForLastTask();
return;
}
void RTC_Config(void) {
//我们在BKP的后备寄存器1中,存了一个特殊字符0xA5A5
//第一次上电或后备电源掉电后,该寄存器数据丢失,
//表明RTC数据丢失,需要重新配置
if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5) {
printf("\r\n\n RTC not yet configured....");
//重新配置RTC
printf("\r\n请输入时间,如: !t2010 10 10 10 10 10!");
RTC_Configuration();
//死循环等待输入首次时间
while (timeFirstSet != 1) {
ProcessProtocol( SERI0);
}
// Time_Adjust();
//配置完成后,向后备寄存器中写特殊字符0xA5A5
BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
} else {
//若后备寄存器没有掉电,则无需重新配置RTC
//这里我们可以利用RCC_GetFlagStatus()函数查看本次复位类型
if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) {
//这是上电复位
} else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -