main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*------------------------------------------------------------------------------
  2. * main.c
  3. * Copyright 2019 WDKL Electronics Co.,Ltd.
  4. * Description:
  5. * - This file implements the follow functions:
  6. * -
  7. * modification history
  8. * --------------------
  9. * 09:59am, 24.Sep.2019, written by dwd
  10. * --------------------
  11. ------------------------------------------------------------------------------*/
  12. /*----------------------------------------------------------------------------
  13. * I N C L U D E
  14. *--------------------------------------------------------------------------*/
  15. #include "stm32f0xx.h"
  16. #include "general.h"
  17. #include "mapp_ext.h"
  18. #include "mapp_gpio.h"
  19. #include "mapp_key.h"
  20. #include "mapp_pwr.h"
  21. #include "mapp_uart1.h"
  22. #include "main.h"
  23. #include "mapp_msg.h"
  24. #include "mapp_arm.h"
  25. #include "mapp_ami.h"
  26. #include "mapp_time.h"
  27. #include "mapp_bus.h"
  28. #include "mapp_gpio.h"
  29. /*------------------------------------------------------------------------------
  30. * D E F I N E
  31. *------------------------------------------------------------------------------*/
  32. #define WDKL_TYPE_FJI 0x2014100Bu /* 病床分机 */
  33. #define IAP_PACKET_SIZE ((U08)128)
  34. #define FLASH_PAGE_SIZE ((U16)2048)
  35. #define AppStoreAddressStart ((U32)0x8000000) //起始位
  36. #define AppStoreAddress ((U32)0x8008000) //32K位置
  37. #define AppStoreEndAddress ((U32)0x8020000) //128K位置
  38. /*------------------------------------------------------------------------------
  39. * G L O B A L
  40. *------------------------------------------------------------------------------*/
  41. //ddconst U08 DeviceID[] __attribute__((at(0X8008000)))="SZFJI221016001";
  42. const U08 DeviceMacInfo[] __attribute__((at(0X8003300)))="F301";
  43. //const U08 DeviceVisionInfo[] ={"HV20SV142103050"};
  44. const U08 DeviceVisionInfo[] ={"SZD101012409280"};
  45. uint16_t X_Addata=0,Y_Addata=0;
  46. /*----------------------------------------------------------------------------
  47. * V A R I A B L E S
  48. *--------------------------------------------------------------------------*/
  49. static VU08 s_vu8SysSched0Tmr = 0;
  50. static VU08 s_vu8SysSched1Tmr = 0;
  51. static VU08 s_vu8SysSched2Tmr = 0;
  52. static VU08 s_vu8SysSched3Tmr = 0;
  53. /*------------------------------------------------------------------------------
  54. * P R O T O T Y P E
  55. *------------------------------------------------------------------------------*/
  56. static void s_MainNvicCfg(void);
  57. static void s_MainSysTickCfg(void);
  58. static void s_MainPeripheralCfg(void);
  59. static void s_MainIwdgCfg(void);
  60. static void s_MainSysVarInit(void);
  61. static void EraseFlash(U08 eraseCnt);
  62. static void EraseFlashMultiple(U08 eraseCntStart,U08 eraseCntEnd);
  63. static void s_FlashUnlockFunc(void);
  64. //static U32 Get_ChipSerialNum(void);
  65. //static void WriteEncrypt(void) ;
  66. //--------------------------------------------------------
  67. void strncpy_rom(U08* to,const U08 from[],U08 size)
  68. {
  69. while(size)
  70. {
  71. *to=*from;
  72. to++;
  73. from++;
  74. size--;
  75. }
  76. return;
  77. }
  78. void strncpy_u8(U08* to,U08* from,U08 size)
  79. {
  80. while(size)
  81. {
  82. *to=*from;
  83. to++;
  84. from++;
  85. size--;
  86. }
  87. return;
  88. }
  89. U08 MEMCPY(void * to,void * from,U08 size)
  90. {
  91. U08 *pTo = (U08 *)to;
  92. U08 *pFrom = (U08 *)from;
  93. if((NULL == pTo) || (NULL == pFrom))
  94. {
  95. return 0;
  96. }
  97. if(!((pTo >= pFrom + size) || (pFrom >= pTo + size)))
  98. {
  99. return 0;
  100. }
  101. while(size-- > 0)
  102. {
  103. *pTo++=*pFrom++;
  104. }
  105. return 1;
  106. }
  107. void MEMSET(void *buff,U08 data,U16 length)
  108. {
  109. U16 i;
  110. U08 *p;
  111. p = (U08 *)buff;
  112. for(i=0;i<length;i++)
  113. {
  114. *p = data;
  115. p++;
  116. }
  117. }
  118. U08 StrnCmp(U08 *str1,U08 *str2,U08 size)
  119. {
  120. U08 i;
  121. if(str1==str2) return 0;
  122. for(i=0;i<size;++i)
  123. {
  124. if(*str1>*str2) return 1;
  125. else if(*str1<*str2) return 1;
  126. else
  127. {
  128. ++str1;
  129. ++str2;
  130. }
  131. }
  132. return 0;
  133. }
  134. /*----------------------------------------------------------------------
  135. // Function uses : SysDelayUs
  136. // Input parameter :
  137. // Output parameter :
  138. // Use Function : delay 1us timer.
  139. // Reserve date : 11:10am, 17.Apr.2012, written by Sirius
  140. ----------------------------------------------------------------------*/
  141. void SysDelayUs(U16 dlyus)
  142. {
  143. U16 i = 0;
  144. while (dlyus--) {
  145. i = 15;
  146. while (i--);
  147. }
  148. }
  149. /*----------------------------------------------------------------------
  150. // Function uses : SysDelayMs
  151. // Input parameter :
  152. // Output parameter :
  153. // Use Function : delay 1ms timer.
  154. // Reserve date : 10:10am, 9.Sep.2019, written by dwd
  155. ----------------------------------------------------------------------*/
  156. void SysDelayMs(U16 dlyms)
  157. {
  158. U16 i = 0;
  159. while (dlyms--) {
  160. i = 6200;
  161. while (i--);
  162. }
  163. }
  164. /*----------------------------------------------------------------------
  165. // Function uses : SysTickISR
  166. // Input parameter :
  167. // Output parameter :
  168. // Use Function : Decrements the TimingDelay variable,tick every 5ms.
  169. // Reserve date : 10:10am, 9.Sep.2019, written by dwd
  170. //---------------------------------------------------------------------*/
  171. void SysTickISR(void)
  172. {
  173. static U08 u8systimecnt =0;
  174. if (s_vu8SysSched0Tmr != 0xff) { s_vu8SysSched0Tmr++; }
  175. if (s_vu8SysSched1Tmr != 0xff) { s_vu8SysSched1Tmr++; }
  176. if (s_vu8SysSched2Tmr != 0xff) { s_vu8SysSched2Tmr++; }
  177. if (s_vu8SysSched3Tmr != 0xff) { s_vu8SysSched3Tmr++; }
  178. /*--- Power task timer ---*/
  179. PwrTaskTmr();
  180. ArmTaskTmr();
  181. AmiTaskTmr();
  182. /*---I2C task timer---*/
  183. u8systimecnt ++;
  184. LocalTime++;
  185. }
  186. void SosLedState(U08 u8sta)
  187. {
  188. static U08 u8cnt =0;
  189. if (u8sta ==0) {
  190. LED_EMEGENCY_ON ;
  191. } else if (u8sta ==1) {
  192. LED_EMEGENCY_OFF ;
  193. } else if (u8sta ==2) {
  194. u8cnt ++;
  195. if (u8cnt <5) {
  196. LED_EMEGENCY_OFF ;
  197. } else if (u8cnt >=9) {
  198. LED_EMEGENCY_ON ;
  199. u8cnt =0;
  200. } else if (u8cnt >5) {
  201. LED_EMEGENCY_ON ;
  202. }
  203. }
  204. }
  205. void DoorLedState(void)
  206. {
  207. static U08 u8cnt[4] ={0};
  208. if (gtArm.DoorLed[0] == 0) {
  209. LED_RED_OFF;
  210. } else if (gtArm.DoorLed[0] ==1) {
  211. LED_RED_ON ;
  212. } else if (gtArm.DoorLed[0] ==2) {
  213. u8cnt[0] ++;
  214. if (u8cnt[0] <5) {
  215. LED_RED_ON ;
  216. } else if (u8cnt[0] >=9) {
  217. u8cnt[0] =0;
  218. LED_RED_OFF;
  219. } else if (u8cnt[0] >5) {
  220. LED_RED_OFF;
  221. }
  222. }
  223. if (gtArm.DoorLed[1] == 0) {
  224. LED_BLUE_OFF ;
  225. } else if (gtArm.DoorLed[1] ==1) {
  226. LED_BLUE_ON;
  227. } else if (gtArm.DoorLed[1] ==2) {
  228. u8cnt[1] ++;
  229. if (u8cnt[1] <5) {
  230. LED_BLUE_ON;
  231. } else if (u8cnt[1] >=9) {
  232. u8cnt[1] =0;
  233. LED_BLUE_OFF ;
  234. } else if (u8cnt[1] >5) {
  235. LED_BLUE_OFF ;
  236. }
  237. }
  238. if (gtArm.DoorLed[2] == 0) {
  239. LED_GREEN_OFF;
  240. } else if (gtArm.DoorLed[2] ==1) {
  241. LED_GREEN_ON ;
  242. } else if (gtArm.DoorLed[2] ==2) {
  243. u8cnt[2] ++;
  244. if (u8cnt[2] <5) {
  245. LED_GREEN_ON ;
  246. } else if (u8cnt[2] >=9) {
  247. u8cnt[2] =0;
  248. LED_GREEN_OFF;
  249. } else if (u8cnt[2] >5) {
  250. LED_GREEN_OFF;
  251. }
  252. }
  253. }
  254. void CallLedState(void)
  255. {
  256. static U08 u8cnt = 0;
  257. static U08 u8cnt2 = 0;
  258. if (gtArm.callled_red == 1 ) {
  259. u8cnt ++;
  260. if (u8cnt <5) {
  261. LED_R_ON;
  262. } else if (u8cnt >=9) {
  263. u8cnt =0;
  264. LED_R_OFF;
  265. } else if (u8cnt > 5) {
  266. LED_R_OFF;
  267. }
  268. } else if (gtArm.callled_red == 2 ) {
  269. LED_R_ON;
  270. } else {
  271. LED_R_OFF;
  272. }
  273. if (gtArm.callled_yel == 1 ) {
  274. u8cnt2 ++;
  275. if (u8cnt2 <5) {
  276. LED_Y_ON;
  277. } else if (u8cnt2 >=9) {
  278. u8cnt2 =0;
  279. LED_Y_OFF;
  280. } else if (u8cnt2 > 5) {
  281. LED_Y_OFF;
  282. }
  283. } else if (gtArm.callled_yel == 2 ) {
  284. LED_Y_ON;
  285. } else {
  286. LED_Y_OFF;
  287. }
  288. }
  289. void LED_Task(void)
  290. {
  291. static U08 uLedKeyStae =0;
  292. static U08 u8time =0;
  293. static U08 u8cnt =0;
  294. static U08 u8cnt2 =0;
  295. SosLedState(gtArm.Uled);
  296. DoorLedState();
  297. CallLedState();
  298. }
  299. /*----------------------------------------------------------------------
  300. // Function uses : main
  301. // Input parameter :
  302. // Output parameter :
  303. // Use Function :
  304. // Reserve date : 10:10am, 9.Sep.2019, written by dwd
  305. ----------------------------------------------------------------------*/
  306. int main(void)
  307. {
  308. s_MainSysVarInit();
  309. s_MainSysTickCfg(); /* Configure the systick */
  310. s_MainPeripheralCfg(); /* Peripheral Configuration */
  311. s_MainNvicCfg(); /* NVIC configuration */
  312. s_MainIwdgCfg();
  313. VOICE_PF7_ON;
  314. //PA6_ON; //打开pA6
  315. while (1) {
  316. //12345689
  317. IWDG_ReloadCounter(); /* Reload IWDG counter */
  318. gtArm.PA4_Status|=KEY_PA4_HANDLE_DET;
  319. if(gtArm.PA4_Status){
  320. gtArm.PA4_Status|=2;
  321. }
  322. if (s_vu8SysSched0Tmr >= OS_TIMER_15MS) { /* Schedule 15ms task with priority 0 */
  323. s_vu8SysSched0Tmr = 0;
  324. ArmTxTask();
  325. AmiTxTask();
  326. if(gtArm.PA4_Status==0){
  327. gtArm.PA4_Value=0;
  328. }else {
  329. gtArm.PA4_Value=1;
  330. }
  331. gtArm.PA4_Status=0;
  332. continue;
  333. }
  334. if (s_vu8SysSched1Tmr >= OS_TIMER_35MS) { /* Schedule 35ms task with priority 1 */
  335. s_vu8SysSched1Tmr = 0;
  336. ArmRxTask();
  337. AmiRxTask();
  338. KeyTask();
  339. // PA6_OFF;
  340. continue;
  341. }
  342. if (s_vu8SysSched2Tmr >= OS_TIMER_100MS) { /* Schedule 100ms task with priority 2*/
  343. s_vu8SysSched2Tmr = 0;
  344. PwrTask();
  345. ExtTask();
  346. LED_Task();
  347. continue;
  348. }
  349. if (s_vu8SysSched3Tmr >= OS_TIMER_10MS) { /* Schedule 100+ms task with priority 2*/
  350. s_vu8SysSched3Tmr = 0;
  351. BUSTask();
  352. continue;
  353. }
  354. }
  355. }
  356. /*----------------------------------------------------------------------
  357. // Function uses : s_MainNvicCfg
  358. // Input parameter : Configures Vector Table base location.
  359. // Output parameter :
  360. // Use Function :
  361. // Reserve date : 10:10am, 9.Sep.2019, written by dwd
  362. ----------------------------------------------------------------------*/
  363. static void s_MainNvicCfg(void)
  364. {
  365. RCC_ClocksTypeDef get_rcc_clock;//???
  366. NVIC_InitTypeDef NVIC_InitStructure;
  367. RCC_GetClocksFreq(&get_rcc_clock);
  368. /* Configure one bit for preemption priority */
  369. /* Enable the CAN1 RX0 Interrupt */
  370. //NVIC_InitStructure.NVIC_IRQChannel = CEC_CAN_IRQn;
  371. //NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
  372. //NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  373. //NVIC_Init(&NVIC_InitStructure);
  374. /* Enable DVD USART3 USART4 Interrupt */
  375. NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
  376. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  377. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  378. NVIC_Init(&NVIC_InitStructure);
  379. NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
  380. NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  381. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  382. NVIC_Init(&NVIC_InitStructure);
  383. }
  384. /*----------------------------------------------------------------------
  385. // Function uses : s_MainSysTickCfg
  386. // Input parameter :
  387. // Output parameter :
  388. // Use Function :
  389. // Reserve date : 10:10am, 9.Sep.2019, written by dwd
  390. ----------------------------------------------------------------------*/
  391. static void s_MainSysTickCfg(void)
  392. {
  393. /* This example shows how to configure the SysTick to generate a time base equal to
  394. 5 ms. The system clock is set to 24 MHz on Value line devices and to 72 MHz on
  395. other devices, the SysTick is clocked by the AHB clock (HCLK).
  396. prm == (N.[ms] * SystemCoreClock / 1000).
  397. */
  398. if (SysTick_Config(SystemCoreClock / OS_TICKS_PER_SEC))
  399. {
  400. /* Capture error */
  401. while (1);
  402. }
  403. }
  404. /*----------------------------------------------------------------------
  405. // Function uses : s_MainPeripheralCfg
  406. // Input parameter : Configures the different peripheral.
  407. // Output parameter :
  408. // Use Function :
  409. // Reserve date : 10:10am, 9.Sep.2019, written by dwd
  410. ----------------------------------------------------------------------*/
  411. static void s_MainPeripheralCfg(void)
  412. {
  413. U32 u32prm =0;
  414. /*--- GPIO ---*/
  415. GpioCfgInit();
  416. /*---UART1 ---*/
  417. Uart1CfgInit();
  418. Uart2CfgInit();
  419. TIM3_PWM_Init();
  420. TIM6_init();
  421. TIM1_Init();
  422. /*---AD----*/
  423. //AdcCfgInit();
  424. u32prm =*(U32*)(0X8003300);
  425. if ((u32prm !=0) && (u32prm !=0xffffff) ) {
  426. //gtArm.SlaveFJZHmac = TO_WORD(LO_BYTE(u16prm), HI_BYTE(u16prm));
  427. gtArm.FJKeymac = HexToInt(u32prm);
  428. Addrp[0] = HI_BYTE(gtArm.FJKeymac);
  429. Addrp[1] = LO_BYTE(gtArm.FJKeymac);
  430. // gtArm.SlaveFJZHmac = u16prm;
  431. }
  432. }
  433. /*----------------------------------------------------------------------
  434. // Function uses : s_MainIwdgCfg
  435. // Input parameter :
  436. // Output parameter :
  437. // Use Function :
  438. // Reserve date : 10:10am, 9.Sep.2019, written by dwd
  439. ----------------------------------------------------------------------*/
  440. static void s_MainIwdgCfg(void)
  441. {
  442. /* IWDG timeout equal to 2000 ms (the timeout may varies due to LSI frequency
  443. dispersion) */
  444. /* Enable write access to IWDG_PR and IWDG_RLR registers */
  445. IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
  446. /* IWDG counter clock: 40KHz(LSI) / 32 = 1.25 KHz */
  447. IWDG_SetPrescaler(IWDG_Prescaler_128);
  448. /* Set counter reload value to 499 */
  449. IWDG_SetReload(1000);
  450. /* Reload IWDG counter */
  451. IWDG_ReloadCounter();
  452. /* Enable IWDG (the LSI oscillator will be enabled by hardware) */
  453. IWDG_Enable();
  454. }
  455. /*----------------------------------------------------------------------
  456. // Function uses : s_MainSysVarInit
  457. // Input parameter :
  458. // Output parameter :
  459. // Use Function :
  460. // Reserve date : 10:10am, 9.Sep.2019, written by dwd
  461. //---------------------------------------------------------------------*/
  462. static void s_MainSysVarInit(void)
  463. {
  464. s_vu8SysSched0Tmr = 0;
  465. s_vu8SysSched1Tmr = 0;
  466. s_vu8SysSched2Tmr = 0;
  467. s_vu8SysSched3Tmr = 0;
  468. /*--- POWER ---*/
  469. PwrVarInit();
  470. /*--- UART1 ---*/
  471. /*--- KEY ---*/
  472. KeyVarInit();
  473. /*---dvd---*/
  474. ArmValInit();
  475. /*---ext---*/
  476. ExtValInit();
  477. /*---msg---*/
  478. OSQInit();
  479. }
  480. void FlashCheckWriteProtect(void)
  481. {
  482. unsigned int BlockNbr = 0;
  483. unsigned int UserMemoryMask = 0;
  484. BlockNbr = (AppStoreAddress - 0x08000000) >> 12;
  485. /* Compute the mask to test if the Flash memory, where the user program will be
  486. loaded, is write protected */
  487. UserMemoryMask = ((unsigned int)~((1 << BlockNbr) - 1));
  488. /* Test if any page of Flash memory where program user will be loaded is write protected */
  489. if ((FLASH_GetWriteProtectionOptionByte() & UserMemoryMask) != UserMemoryMask) {
  490. FLASH_EraseOptionBytes();
  491. }
  492. }
  493. static void EraseFlash(U08 eraseCnt)
  494. {
  495. FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
  496. FLASH_ErasePage(AppStoreAddress + (FLASH_PAGE_SIZE * eraseCnt));
  497. }
  498. static void EraseFlashMultiple(U08 eraseCntStart,U08 eraseCntEnd)
  499. {
  500. U08 u8prm =0;
  501. U08 u8diff =0;
  502. FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
  503. u8diff = eraseCntEnd -eraseCntStart;
  504. for (u8prm =0;u8prm < u8diff;u8prm++) {
  505. FLASH_ErasePage( AppStoreAddressStart+(FLASH_PAGE_SIZE * (eraseCntStart +u8prm)));
  506. }
  507. }
  508. static void s_FlashUnlockFunc(void)
  509. {
  510. FLASH_Unlock();
  511. FlashCheckWriteProtect();
  512. EraseFlash(32);
  513. }
  514. //FLASH_ProgramHalfWord(AppStoreAddress, u16sncnt); //写FLASH值
  515. //stIap.totalSN = *(u32*)(AppStoreAddress); //读FLASH位置
  516. //读取指定地址的半字(16位数据)
  517. //也是按照半字读出,即每次读2个字节数据返回
  518. U16 FLASH_ReadHalfWord(U32 address)
  519. {
  520. return *(__IO U16*)address;
  521. }
  522. //从指定地址开始读取多个数据
  523. void FLASH_ReadMoreData(U32 startAddress,U16 *readData,U16 countToRead)
  524. {
  525. U16 dataIndex;
  526. for(dataIndex=0;dataIndex<countToRead;dataIndex++)
  527. {
  528. readData[dataIndex]=FLASH_ReadHalfWord(startAddress+dataIndex*2);
  529. }
  530. }