/*------------------------------------------------------------------------------ * main.c * Copyright 2019 WDKL Electronics Co.,Ltd. * Description: * - This file implements the follow functions: * - * modification history * -------------------- * 09:59am, 24.Sep.2019, written by dwd * -------------------- ------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------- * I N C L U D E *--------------------------------------------------------------------------*/ #include "stm32f0xx.h" #include "general.h" #include "mapp_ext.h" #include "mapp_gpio.h" #include "mapp_key.h" #include "mapp_pwr.h" #include "mapp_uart1.h" #include "main.h" #include "mapp_msg.h" #include "mapp_arm.h" #define OS_TIMER_1MS 1 /*------------------------------------------------------------------------------ * D E F I N E *------------------------------------------------------------------------------*/ #define WDKL_TYPE_FJI 0x2014100Bu /* 病床分机 */ #define IAP_PACKET_SIZE ((U08)128) #define FLASH_PAGE_SIZE ((U16)2048) #define AppStoreAddressStart ((U32)0x8000000) //起始位 #define AppStoreAddress ((U32)0x8008000) //32K位置 #define AppStoreEndAddress ((U32)0x8020000) //128K位置 /*------------------------------------------------------------------------------ * G L O B A L *------------------------------------------------------------------------------*/ //ddconst U08 DeviceID[] __attribute__((at(0X8008000)))="SZFJI221016001"; const U08 DeviceMacInfo[] __attribute__((at(0X8003300)))="F301"; //const U08 DeviceVisionInfo[] ={"HV20SV142103050"}; const U08 DeviceVisionInfo[] ={"SZD101012502091"}; uint16_t X_Addata=0,Y_Addata=0; /*---------------------------------------------------------------------------- * V A R I A B L E S *--------------------------------------------------------------------------*/ static VU08 s_vu8SysSched0Tmr = 0; static VU08 s_vu8SysSched1Tmr = 0; static VU08 s_vu8SysSched2Tmr = 0; static VU08 s_vu8SysSched3Tmr = 0; /*------------------------------------------------------------------------------ * P R O T O T Y P E *------------------------------------------------------------------------------*/ static void s_MainNvicCfg(void); static void s_MainSysTickCfg(void); static void s_MainPeripheralCfg(void); static void s_MainIwdgCfg(void); static void s_MainSysVarInit(void); static void EraseFlash(U08 eraseCnt); static void EraseFlashMultiple(U08 eraseCntStart,U08 eraseCntEnd); static void s_FlashUnlockFunc(void); //static U32 Get_ChipSerialNum(void); //static void WriteEncrypt(void) ; //-------------------------------------------------------- void strncpy_rom(U08* to,const U08 from[],U08 size) { while(size) { *to=*from; to++; from++; size--; } return; } void strncpy_u8(U08* to,U08* from,U08 size) { while(size) { *to=*from; to++; from++; size--; } return; } U08 MEMCPY(void * to,void * from,U08 size) { U08 *pTo = (U08 *)to; U08 *pFrom = (U08 *)from; if((NULL == pTo) || (NULL == pFrom)) { return 0; } if(!((pTo >= pFrom + size) || (pFrom >= pTo + size))) { return 0; } while(size-- > 0) { *pTo++=*pFrom++; } return 1; } void MEMSET(void *buff,U08 data,U16 length) { U16 i; U08 *p; p = (U08 *)buff; for(i=0;i*str2) return 1; else if(*str1<*str2) return 1; else { ++str1; ++str2; } } return 0; } /*---------------------------------------------------------------------- // Function uses : SysDelayUs // Input parameter : // Output parameter : // Use Function : delay 1us timer. // Reserve date : 11:10am, 17.Apr.2012, written by Sirius ----------------------------------------------------------------------*/ void SysDelayUs(U16 dlyus) { U16 i = 0; while (dlyus--) { i = 15; while (i--); } } /*---------------------------------------------------------------------- // Function uses : SysDelayMs // Input parameter : // Output parameter : // Use Function : delay 1ms timer. // Reserve date : 10:10am, 9.Sep.2019, written by dwd ----------------------------------------------------------------------*/ void SysDelayMs(U16 dlyms) { U16 i = 0; while (dlyms--) { i = 6200; while (i--); } } /*---------------------------------------------------------------------- // Function uses : SysTickISR // Input parameter : // Output parameter : // Use Function : Decrements the TimingDelay variable,tick every 5ms. // Reserve date : 10:10am, 9.Sep.2019, written by dwd //---------------------------------------------------------------------*/ void SysTickISR(void) { static U08 u8systimecnt =0; if (s_vu8SysSched0Tmr != 0xff) { s_vu8SysSched0Tmr++; } if (s_vu8SysSched1Tmr != 0xff) { s_vu8SysSched1Tmr++; } if (s_vu8SysSched2Tmr != 0xff) { s_vu8SysSched2Tmr++; } if (s_vu8SysSched3Tmr != 0xff) { s_vu8SysSched3Tmr++; } if (gtArm.uarttxcnt != 0xff) { gtArm.uarttxcnt++; } /*--- Power task timer ---*/ PwrTaskTmr(); ArmTaskTmr(); /*---I2C task timer---*/ u8systimecnt ++; } void SosLedState(U08 u8sta) { static U08 u8cnt =0; if (u8sta ==0) { LED_EMEGENCY_ON ; } else if (u8sta ==1) { LED_EMEGENCY_OFF ; } else if (u8sta ==2) { u8cnt ++; if (u8cnt <5) { LED_EMEGENCY_OFF ; } else if (u8cnt >=9) { LED_EMEGENCY_ON ; u8cnt =0; } else if (u8cnt >5) { LED_EMEGENCY_ON ; } } } void DoorLedState(void) { static U08 u8cnt[4] ={0}; if (gtArm.DoorLed[0] == 0) { LED_RED_OFF; } else if (gtArm.DoorLed[0] ==1) { LED_RED_ON ; } else if (gtArm.DoorLed[0] ==2) { u8cnt[0] ++; if (u8cnt[0] <5) { LED_RED_ON ; } else if (u8cnt[0] >=9) { u8cnt[0] =0; LED_RED_OFF; } else if (u8cnt[0] >5) { LED_RED_OFF; } } if (gtArm.DoorLed[1] == 0) { LED_BLUE_OFF ; } else if (gtArm.DoorLed[1] ==1) { LED_BLUE_ON; } else if (gtArm.DoorLed[1] ==2) { u8cnt[1] ++; if (u8cnt[1] <5) { LED_BLUE_ON; } else if (u8cnt[1] >=9) { u8cnt[1] =0; LED_BLUE_OFF ; } else if (u8cnt[1] >5) { LED_BLUE_OFF ; } } if (gtArm.DoorLed[2] == 0) { LED_GREEN_OFF; } else if (gtArm.DoorLed[2] ==1) { LED_GREEN_ON ; } else if (gtArm.DoorLed[2] ==2) { u8cnt[2] ++; if (u8cnt[2] <5) { LED_GREEN_ON ; } else if (u8cnt[2] >=9) { u8cnt[2] =0; LED_GREEN_OFF; } else if (u8cnt[2] >5) { LED_GREEN_OFF; } } } void CallLedState(void) { static U08 u8cnt = 0; static U08 u8cnt2 = 0; if (gtArm.callled_red == 1 ) { u8cnt ++; if (u8cnt <5) { LED_R_ON; } else if (u8cnt >=9) { u8cnt =0; LED_R_OFF; } else if (u8cnt > 5) { LED_R_OFF; } } else if (gtArm.callled_red == 2 ) { LED_R_ON; } else { LED_R_OFF; } if (gtArm.callled_yel == 1 ) { u8cnt2 ++; if (u8cnt2 <5) { LED_Y_ON; } else if (u8cnt2 >=9) { u8cnt2 =0; LED_Y_OFF; } else if (u8cnt2 > 5) { LED_Y_OFF; } } else if (gtArm.callled_yel == 2 ) { LED_Y_ON; } else { LED_Y_OFF; } } void LED_Task(void) { static U08 uLedKeyStae =0; static U08 u8time =0; static U08 u8cnt =0; static U08 u8cnt2 =0; #if 0 u8time ++; if (gtArm.LedKeyState != uLedKeyStae) { u8time =0; if (gtArm.LedKeyState == 13) { u8cnt ++; if (u8cnt > 10) { u8cnt2 ++; u8cnt =0; LED_GREEN_OFF; } else if (u8cnt > 5) { LED_GREEN_OFF; } else { LED_GREEN_ON; } if (u8cnt2 >2) { u8cnt2 =0; gtArm.LedKeyState =0; } } else if (gtArm.LedKeyState == 12) { u8cnt ++; if (u8cnt > 10) { u8cnt2 ++; u8cnt =0; LED_GREEN_OFF; } else if (u8cnt > 5) { LED_GREEN_OFF; } else { LED_GREEN_ON; } if (u8cnt2 >0) { u8cnt2 =0; gtArm.LedKeyState =0; } } else if (gtArm.LedKeyState == 11) { u8cnt ++; if (u8cnt > 10) { u8cnt2 ++; u8cnt =0; LED_GREEN_OFF; } else if (u8cnt > 5) { LED_GREEN_OFF; } else { LED_GREEN_ON; } if (u8cnt2 >0) { u8cnt2 =0; gtArm.LedKeyState =0; } } else { u8cnt2 =0; } uLedKeyStae = gtArm.LedKeyState ; } if (u8time >50) { //5秒后清除按键 u8time =0; gtArm.LedKeyState =0; uLedKeyStae = gtArm.LedKeyState ; } #endif SosLedState(gtArm.Uled); DoorLedState(); CallLedState(); } /*---------------------------------------------------------------------- // Function uses : main // Input parameter : // Output parameter : // Use Function : // Reserve date : 10:10am, 9.Sep.2019, written by dwd ----------------------------------------------------------------------*/ int main(void) { s_MainSysTickCfg(); /* Configure the systick */ s_MainPeripheralCfg(); /* Peripheral Configuration */ s_MainNvicCfg(); /* NVIC configuration */ s_MainSysVarInit(); s_MainIwdgCfg(); #if 0 // 使能GPIOA的时钟 RCC->AHBENR |= RCC_AHBENR_GPIOAEN; // 配置PA7为输出模式 GPIOA->MODER &= ~(GPIO_MODER_MODER7); // 清除MODER7位 GPIOA->MODER |= GPIO_MODER_MODER7_0; // 设置MODER7位为01,即输出模式 // 设置PA7输出高电平 GPIOA->BSRR = GPIO_BSRR_BS_7; if (s_vu8SysSched0Tmr >= OS_TIMER_5MS) { /* Schedule 5ms task with priority 0 */ s_vu8SysSched0Tmr = 0; ArmTxTask(); KeyPB0Check(); continue; } #endif while (1) { IWDG_ReloadCounter(); /* Reload IWDG counter */ if (s_vu8SysSched0Tmr >= OS_TIMER_5MS) { /* Schedule 5ms task with priority 0 */ s_vu8SysSched0Tmr = 0; ArmTxTask(); KeyPB0Check(); KeyPA8Check(); continue; } if (s_vu8SysSched1Tmr >= OS_TIMER_35MS) { /* Schedule 35ms task with priority 1 */ s_vu8SysSched1Tmr = 0; ArmRxTask(); KeyTask(); continue; } if (s_vu8SysSched2Tmr >= OS_TIMER_100MS) { /* Schedule 100ms task with priority 2*/ s_vu8SysSched2Tmr = 0; PwrTask(); ExtTask(); LED_Task(); continue; } if (s_vu8SysSched3Tmr >= OS_TIMER_35MS) { /* Schedule 100+ms task with priority 2*/ s_vu8SysSched3Tmr = 0; continue; } } } /*---------------------------------------------------------------------- // Function uses : s_MainNvicCfg // Input parameter : Configures Vector Table base location. // Output parameter : // Use Function : // Reserve date : 10:10am, 9.Sep.2019, written by dwd ----------------------------------------------------------------------*/ static void s_MainNvicCfg(void) { RCC_ClocksTypeDef get_rcc_clock;//??? NVIC_InitTypeDef NVIC_InitStructure; RCC_GetClocksFreq(&get_rcc_clock); /* Configure one bit for preemption priority */ /* Enable the CAN1 RX0 Interrupt */ //NVIC_InitStructure.NVIC_IRQChannel = CEC_CAN_IRQn; //NVIC_InitStructure.NVIC_IRQChannelPriority = 1; //NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //NVIC_Init(&NVIC_InitStructure); /* Enable DVD USART3 USART4 Interrupt */ NVIC_InitStructure.NVIC_IRQChannelPriority = 1; NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } /*---------------------------------------------------------------------- // Function uses : s_MainSysTickCfg // Input parameter : // Output parameter : // Use Function : // Reserve date : 10:10am, 9.Sep.2019, written by dwd ----------------------------------------------------------------------*/ static void s_MainSysTickCfg(void) { /* This example shows how to configure the SysTick to generate a time base equal to 5 ms. The system clock is set to 24 MHz on Value line devices and to 72 MHz on other devices, the SysTick is clocked by the AHB clock (HCLK). prm == (N.[ms] * SystemCoreClock / 1000). */ if (SysTick_Config(SystemCoreClock / OS_TICKS_PER_SEC)) { /* Capture error */ while (1); } } /*---------------------------------------------------------------------- // Function uses : s_MainPeripheralCfg // Input parameter : Configures the different peripheral. // Output parameter : // Use Function : // Reserve date : 10:10am, 9.Sep.2019, written by dwd ----------------------------------------------------------------------*/ static void s_MainPeripheralCfg(void) { U32 u32prm =0; /*--- GPIO ---*/ GpioCfgInit(); /*---UART1 ---*/ Uart1CfgInit(); /*---AD----*/ //AdcCfgInit(); u32prm =*(U32*)(0X8003300); if ((u32prm !=0) && (u32prm !=0xffffff) ) { //gtArm.SlaveFJZHmac = TO_WORD(LO_BYTE(u16prm), HI_BYTE(u16prm)); gtArm.FJKeymac = HexToInt(u32prm); // gtArm.SlaveFJZHmac = u16prm; } } /*---------------------------------------------------------------------- // Function uses : s_MainIwdgCfg // Input parameter : // Output parameter : // Use Function : // Reserve date : 10:10am, 9.Sep.2019, written by dwd ----------------------------------------------------------------------*/ static void s_MainIwdgCfg(void) { /* IWDG timeout equal to 2000 ms (the timeout may varies due to LSI frequency dispersion) */ /* Enable write access to IWDG_PR and IWDG_RLR registers */ IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); /* IWDG counter clock: 40KHz(LSI) / 32 = 1.25 KHz */ IWDG_SetPrescaler(IWDG_Prescaler_128); /* Set counter reload value to 499 */ IWDG_SetReload(1000); /* Reload IWDG counter */ IWDG_ReloadCounter(); /* Enable IWDG (the LSI oscillator will be enabled by hardware) */ IWDG_Enable(); } /*---------------------------------------------------------------------- // Function uses : s_MainSysVarInit // Input parameter : // Output parameter : // Use Function : // Reserve date : 10:10am, 9.Sep.2019, written by dwd //---------------------------------------------------------------------*/ static void s_MainSysVarInit(void) { s_vu8SysSched0Tmr = 0; s_vu8SysSched1Tmr = 0; s_vu8SysSched2Tmr = 0; s_vu8SysSched3Tmr = 0; /*--- POWER ---*/ PwrVarInit(); /*--- UART1 ---*/ /*--- KEY ---*/ KeyVarInit(); /*---dvd---*/ ArmValInit(); /*---ext---*/ ExtValInit(); /*---msg---*/ OSQInit(); } void FlashCheckWriteProtect(void) { unsigned int BlockNbr = 0; unsigned int UserMemoryMask = 0; BlockNbr = (AppStoreAddress - 0x08000000) >> 12; /* Compute the mask to test if the Flash memory, where the user program will be loaded, is write protected */ UserMemoryMask = ((unsigned int)~((1 << BlockNbr) - 1)); /* Test if any page of Flash memory where program user will be loaded is write protected */ if ((FLASH_GetWriteProtectionOptionByte() & UserMemoryMask) != UserMemoryMask) { FLASH_EraseOptionBytes(); } } static void EraseFlash(U08 eraseCnt) { FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR); FLASH_ErasePage(AppStoreAddress + (FLASH_PAGE_SIZE * eraseCnt)); } static void EraseFlashMultiple(U08 eraseCntStart,U08 eraseCntEnd) { U08 u8prm =0; U08 u8diff =0; FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR); u8diff = eraseCntEnd -eraseCntStart; for (u8prm =0;u8prm < u8diff;u8prm++) { FLASH_ErasePage( AppStoreAddressStart+(FLASH_PAGE_SIZE * (eraseCntStart +u8prm))); } } static void s_FlashUnlockFunc(void) { FLASH_Unlock(); FlashCheckWriteProtect(); EraseFlash(32); } //FLASH_ProgramHalfWord(AppStoreAddress, u16sncnt); //写FLASH值 //stIap.totalSN = *(u32*)(AppStoreAddress); //读FLASH位置 //读取指定地址的半字(16位数据) //也是按照半字读出,即每次读2个字节数据返回 U16 FLASH_ReadHalfWord(U32 address) { return *(__IO U16*)address; } //从指定地址开始读取多个数据 void FLASH_ReadMoreData(U32 startAddress,U16 *readData,U16 countToRead) { U16 dataIndex; for(dataIndex=0;dataIndex