123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628 |
- /*------------------------------------------------------------------------------
- * 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"
- #include "mapp_ami.h"
- #include "mapp_time.h"
- #include "mapp_bus.h"
- #include "mapp_gpio.h"
- /*------------------------------------------------------------------------------
- * 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[] ={"SZD101012409280"};
- 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<length;i++)
- {
- *p = data;
- p++;
- }
- }
- U08 StrnCmp(U08 *str1,U08 *str2,U08 size)
- {
- U08 i;
- if(str1==str2) return 0;
- for(i=0;i<size;++i)
- {
- if(*str1>*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++; }
- /*--- Power task timer ---*/
- PwrTaskTmr();
- ArmTaskTmr();
- AmiTaskTmr();
- /*---I2C task timer---*/
- u8systimecnt ++;
- LocalTime++;
-
- }
- 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;
-
- 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_MainSysVarInit();
- s_MainSysTickCfg(); /* Configure the systick */
- s_MainPeripheralCfg(); /* Peripheral Configuration */
- s_MainNvicCfg(); /* NVIC configuration */
- s_MainIwdgCfg();
-
- VOICE_PF7_ON;
- //PA6_ON; //打开pA6
- while (1) {
- //12345689
- IWDG_ReloadCounter(); /* Reload IWDG counter */
-
- gtArm.PA4_Status|=KEY_PA4_HANDLE_DET;
- if(gtArm.PA4_Status){
- gtArm.PA4_Status|=2;
- }
- if (s_vu8SysSched0Tmr >= OS_TIMER_15MS) { /* Schedule 15ms task with priority 0 */
- s_vu8SysSched0Tmr = 0;
- ArmTxTask();
- AmiTxTask();
-
- if(gtArm.PA4_Status==0){
- gtArm.PA4_Value=0;
- }else {
- gtArm.PA4_Value=1;
- }
- gtArm.PA4_Status=0;
- continue;
- }
- if (s_vu8SysSched1Tmr >= OS_TIMER_35MS) { /* Schedule 35ms task with priority 1 */
- s_vu8SysSched1Tmr = 0;
- ArmRxTask();
- AmiRxTask();
-
- KeyTask();
- // PA6_OFF;
- 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_10MS) { /* Schedule 100+ms task with priority 2*/
- s_vu8SysSched3Tmr = 0;
- BUSTask();
- 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);
- NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannel = USART2_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();
- Uart2CfgInit();
- TIM3_PWM_Init();
- TIM6_init();
- TIM1_Init();
- /*---AD----*/
- //AdcCfgInit();
-
- u32prm =*(U32*)(0X8003300);
- if ((u32prm !=0) && (u32prm !=0xffffff) ) {
- //gtArm.SlaveFJZHmac = TO_WORD(LO_BYTE(u16prm), HI_BYTE(u16prm));
- gtArm.FJKeymac = HexToInt(u32prm);
- Addrp[0] = HI_BYTE(gtArm.FJKeymac);
- Addrp[1] = LO_BYTE(gtArm.FJKeymac);
- // 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<countToRead;dataIndex++)
- {
- readData[dataIndex]=FLASH_ReadHalfWord(startAddress+dataIndex*2);
- }
- }
|