/**
******************************************************************************
* @file main.c
* @author xufujing
* @version V1.0.0
* @date 2015.4.22
* @brief Main program body
******************************************************************************
* @attention
*
* 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 FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"
#include "fsmc_nand.h"
#include "usart.h"
#include "24C02.h"
#include "sdcard.h"
#include "timer.h"
#include "gpio.h"
#include "stm32f4xx_syscfg.h"
#include "stm32f4xx_exti.h"
#include "flash.h"
#include "timer.h"
#include "stm32f4xx_pwr.h"
#include "delay.h"
#include "rtc.h"
#define BlockSize 512 /* Block Size in Bytes */
#define Operate_Block 0
SD_CardInfo SDCardInfo;
extern unsigned char add_begin[3]; //Target1地址 列1 列2 page1 block2 lun 3
extern unsigned char add_end[3]; //Target1地址 列1 列2 page1 block2 lun 3
extern void SystemInitt(void);
/* Private typedef -----------------------------------------------------------*/
void Sensor_Init(void);
void SDCard_Init(void);
void SDCard_NVIC_Confign(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void Delay(__IO uint32_t nCount);
void Flash_Init(void);
void Sys_Standby(void);
void Sys_Enter_Standby(void);
void Sys_Init(void);
SD_Error Status = SD_OK;
unsigned char buf[6]={0X0D,0X00};
uint8_t UART_Count=0;
uint8_t USER_OVER=1;
unsigned char test[8];
unsigned char sample_par=1;
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
if (PWR_GetFlagStatus(PWR_FLAG_WU) == RESET)
{
//时钟初始化为168MHz
Sys_Init();
//RTC初始化
My_RTC_Init();
// RTC_Set_WakeUp(RTC_WakeUpClock_CK_SPRE_16bits,0);
RTC_Set_AlarmA(4,12,0,0); //设置闹钟
RTC_Set_Tamper();
//串口1初始化
USART_Configuration();
//定时器初始化
RTC_WriteBackupRegister(RTC_BKP_DR1,0x01);
Timer3_Init(0x55,0x55); //采样频率为1KHz
Timer4_Init(0x55,0x55); //采样1s
//管脚初始化
GPIO_Configuration();
//I2C初始化
I2C_Configuration();
//FSMC初始化
// FSMC_NANDDeInit(FSMC_Bank2_NAND);
// FSMC_NAND_Init();
//flash初始化
Flash_Init();
//SDCard初始化
SDCard_Init();
//传感器初始化
Sensor_Init();
//中断初始化
NVIC_Configuration();
//进入低功耗
//Sys_Standby();
Sys_Enter_Standby();
}
else
{
RCC_ClearFlag();
//时钟初始化为168MHz
Sys_Init();
//串口1初始化
USART_Configuration();
//定时器初始化
sample_par=RTC_ReadBackupRegister(RTC_BKP_DR1);
Timer3_Init(0x55,0x55); //采样频率为1KHz
Timer4_Init(0x55,0x55); //采样1s
//管脚初始化
GPIO_Configuration();
//I2C初始化
I2C_Configuration();
//flash初始化
Flash_Init();
//SDCard初始化
SDCard_Init();
//传感器初始化
Sensor_Init();
if(RTC_GetFlagStatus(RTC_FLAG_TAMP1F)==SET)
{ //危险唤醒
EXTI_ClearITPendingBit(EXTI_Line21);
GPIO_SetBits(GPIOA, GPIO_Pin_2); //调试
TIM_Cmd(TIM4, ENABLE);
TIM_Cmd(TIM3, ENABLE);
}
else if(RTC_GetFlagStatus(RTC_FLAG_ALRAF)==SET)//ALARM A中断
{ //闹钟唤醒
Read_Sensor();
GPIO_ToggleBits(GPIOA, GPIO_Pin_1); //调试
EXTI_ClearITPendingBit(EXTI_Line17); //清除中断线17的中断标志
//进入低功耗
Sys_Enter_Standby();
}
else if(GPIO_ReadInputDataBit(GPIOC, TEST))
{ //电源监测
GPIO_SetBits(GPIOA, GPIO_Pin_3); //调试
Read_Sensor();
while(1)
{
GPIO_SetBits(GPIOB, BUZZER);
Delay(0xB5E);
GPIO_ResetBits(GPIOB, BUZZER);
}
}
else if(GPIO_ReadInputDataBit(GPIOB, USER_WAKEUP))
{//人工唤醒
while(USER_OVER)
{
;
}
//进入低功耗
Sys_Enter_Standby();
}
}
while(1)
{
;
}
//结束
// FSMC_NAND_Test();
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
//LED1 (PA1) LED2 (PA2) LED3 (PA3)
GPIO_InitStructure.GPIO_Pin = LED;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA, GPIO_Pin_1);
GPIO_ResetBits(GPIOA, GPIO_Pin_2);
GPIO_ResetBits(GPIOA, GPIO_Pin_3);
//BUZZER PB0
GPIO_InitStructure.GPIO_Pin = BUZZER;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB, BUZZER);
// L_INT1(PE0) H_INT1(PE1) CLKINT(PE12)
GPIO_InitStructure.GPIO_Pin = L_INT1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = H_INT1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = CLKINT;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOE, &GPIO_InitStructure);
//TEST(PC2) 上升沿
GPIO_InitStructure.GPIO_Pin = TEST;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//USER_WAKEUP(PB5)
GPIO_InitStructure.GPIO_Pin = USER_WAKEUP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/**
* 延时函数
*/
void Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
}
void Sys_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//ST PC13
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENAB