BSP.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. #include "BSP.h"
  2. #include "led.h"
  3. #include "../APP/dev_mgr/common/device.h"
  4. #include "../APP/network/downlink.h"
  5. #include "../APP/storage/AT24C128Opt.h"
  6. #include "../APP/globalDef.h"
  7. #include "../APP/command/uart_conf.h"
  8. #include "../APP/toolkit/CH455G.h"
  9. #include "adc.h"
  10. OS_TMR watch_tmr;
  11. uint32_t mcuID[3];
  12. uint8_t air_state = 0,lora_flag = 0;
  13. //uint8_t lora_rcv[128];
  14. uint16_t lora_rcv_len;
  15. uint32_t mcutypeID;
  16. //读取ChipID
  17. void cpuidGetId(void)
  18. {
  19. mcuID[0] = *(volatile uint32_t*)(GD32F10X_ID_ADDR);
  20. mcuID[1] = *(volatile uint32_t*)(GD32F10X_ID_ADDR+4);
  21. mcuID[2] = *(volatile uint32_t*)(GD32F10X_ID_ADDR+8);
  22. }
  23. /*------------------------------------------------------
  24. * 读取设备类型编码
  25. * -----------------------------------------------------*/
  26. void getcpuTypeIdcode(void)
  27. {
  28. mcutypeID = *(volatile uint32_t*)(0xE0042000);
  29. g_firmwareMsg.gatewayMsg.devicTypeID = (uint16_t)(mcutypeID&0xFFFF);
  30. }
  31. static void watchdog_feed_timer(void *p_tmr, void *p_arg)
  32. {
  33. static uint8_t led_flag = 0;
  34. #if 1
  35. IWDG_ReloadCounter();
  36. #endif
  37. #ifdef MSTS_C
  38. LED(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15, led_flag);
  39. LED(GPIOC, GPIO_Pin_6 | GPIO_Pin_7, led_flag);
  40. #else
  41. LED(GPIOB, GPIO_Pin_13 | GPIO_Pin_14, led_flag);
  42. #endif
  43. if(led_flag) {
  44. led_flag = 0;
  45. } else {
  46. led_flag = 1;
  47. }
  48. RTC_Get();
  49. }
  50. void watchdog_init(uint8_t pre,uint16_t rlr)
  51. {
  52. OS_ERR err;
  53. #if 0
  54. IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
  55. IWDG_SetPrescaler(pre);
  56. IWDG_SetReload(rlr);
  57. IWDG_ReloadCounter();
  58. IWDG_Enable();
  59. #endif
  60. OSTmrCreate(&watch_tmr, "watch_tmr", 1, 1, OS_OPT_TMR_PERIODIC, watchdog_feed_timer, NULL, &err);
  61. OSTmrStart(&watch_tmr, &err);
  62. }
  63. void system_first_powerup(void)
  64. {
  65. fram_init_collect_msg();
  66. fram_init_config_msg();
  67. fram_init_gateway_msg();
  68. fram_init_gateway_resetnum();
  69. fram_init_encoder_configMsg();
  70. fram_init_encoder_msg();
  71. // fram_init_gateway_version();
  72. }
  73. void gateway_info_get(void)
  74. {
  75. uint8_t i = 0;
  76. fram_read_config_msg();
  77. fram_read_gateway_hardware_msg();
  78. fram_init_gateway_version();
  79. fram_read_mqtt_msg();
  80. g_firmwareMsg.gatewayMsg.gate_resetType = system_reset_flag_get();
  81. // printf(" gateway_id:%010u\n",downlink_config.gateway_id);
  82. printf("\r\n软件版本:\r\n");
  83. printf("boot_ver = 0x%08x\r\n",g_firmwareMsg.gatewayMsg.verMsg.gate_bootloaderVr);
  84. printf("pt_ver = 0x%08x\r\n",g_firmwareMsg.gatewayMsg.verMsg.pt_version);
  85. printf("app1_ver = 0x%08x\r\n",g_firmwareMsg.gatewayMsg.verMsg.gate_appVr);
  86. printf("app2_ver = 0x%08x\r\n",g_firmwareMsg.gatewayMsg.verMsg.gate_appVr2);
  87. printf("app3_ver = 0x%08x\r\n",g_firmwareMsg.gatewayMsg.verMsg.gate_appVr3);
  88. printf("\r\n设备出厂信息:\r\n");
  89. printf("device_pt_init = 0x%02x\r\n",g_firmwareMsg.gatewayMsg.hardwareMsg.bInit);
  90. printf("device_type = 0x%04x\r\n",0x0101);
  91. printf("device_sn = %010u(0x%08x)\r\n",g_firmwareMsg.gatewayMsg.hardwareMsg.gateway_sn,g_firmwareMsg.gatewayMsg.hardwareMsg.gateway_sn);
  92. printf("uuid = ");
  93. for(i=0;i<12;i++){
  94. printf("%02x ",g_firmwareMsg.gatewayMsg.Uuid[i]);
  95. }
  96. printf("\r\n");
  97. printf("pcb_ver = 0x%02x\r\n",g_firmwareMsg.gatewayMsg.hardwareMsg.pcbVersion);
  98. printf("device_pd = 0x%08x\r\n",g_firmwareMsg.gatewayMsg.hardwareMsg.data);
  99. printf("device_bn = 0x%04x\r\n",g_firmwareMsg.gatewayMsg.hardwareMsg.seqNo);
  100. printf("device_mfrs = 0x%04x\r\n",g_firmwareMsg.gatewayMsg.hardwareMsg.factoryMsg);
  101. }
  102. void gateway_power_info_get(void)
  103. {
  104. if(g_ptTest.bTestStart==1) return;
  105. gateway_info_get();
  106. led_display_version();
  107. downlink_init();
  108. }
  109. void system_info_get(sys_eeprom_info_t *p_info)
  110. {
  111. return;
  112. }
  113. void system_info_update_pos(uint32_t pos, uint32_t value)
  114. {
  115. return;
  116. }
  117. void system_info_update(sys_eeprom_info_t *p_info)
  118. {
  119. return;
  120. }
  121. uint32_t system_reset_flag_get(void) //sys_eeprom_info_t *p_info
  122. {
  123. uint32_t RestFlag = 0;
  124. fram_read_gateway_resetnum();
  125. if(RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET) {//RST PIN复位标志
  126. RestFlag |= RESET_PINRST;
  127. // g_firmwareMsg.gatewayMsg.gate_resetnum++;
  128. }
  129. if(RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) {//上电复位标志
  130. RestFlag |= RESET_PORRSTF;
  131. // g_firmwareMsg.gatewayMsg.gate_resetnum++;
  132. }
  133. if(RCC_GetFlagStatus(RCC_FLAG_SFTRST) != RESET) {//软件复位标志
  134. RestFlag |= RESET_SFTRSTF;
  135. // g_firmwareMsg.gatewayMsg.gate_resetnum++;
  136. }
  137. if (RCC_GetFlagStatus(RCC_FLAG_IWDGRST) != RESET) {//独立看门狗复位标志
  138. RestFlag |= RESET_IWDGRSTF;//
  139. // g_firmwareMsg.gatewayMsg.gate_resetnum++;
  140. }
  141. if (RCC_GetFlagStatus(RCC_FLAG_WWDGRST) != RESET) {//窗口看门狗复位标志
  142. RestFlag |= RESET_IWDGRSTF;//
  143. // g_firmwareMsg.gatewayMsg.gate_resetnum++;
  144. }
  145. if (RCC_GetFlagStatus(RCC_FLAG_LPWRRST) != RESET) {//功耗复位
  146. RestFlag |= RESET_LPWRRSTF;//
  147. // g_firmwareMsg.gatewayMsg.gate_resetnum++;
  148. }
  149. RCC_ClearFlag();//清楚复位状态标志位
  150. fram_wirte_gateway_resetnum();
  151. return RestFlag;
  152. }
  153. void SysTick_init(void)
  154. {
  155. RCC_ClocksTypeDef RCC_Clocks;
  156. RCC_GetClocksFreq(&RCC_Clocks);
  157. SysTick_Config(RCC_Clocks.SYSCLK_Frequency / OS_CFG_TICK_RATE_HZ);
  158. //SystemCoreClock
  159. // SysTick->CTRL |= SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;
  160. // NVIC_EnableIRQ(SysTick_IRQn);
  161. }
  162. //LED IO初始化
  163. void LED_Init(u32 clk, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  164. {
  165. GPIO_InitTypeDef GPIO_InitStructure;
  166. RCC_APB2PeriphClockCmd(clk, ENABLE); //使能PB,PE端口时钟
  167. GPIO_InitStructure.GPIO_Pin = GPIO_Pin; //LED0-->PC.1 端口配置
  168. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
  169. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
  170. GPIO_Init(GPIOx, &GPIO_InitStructure); //根据设定参数初始化GPIOC.1
  171. GPIO_SetBits(GPIOx, GPIO_Pin); //PC.1 输出低
  172. }
  173. void KEY_Init(u32 clk, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  174. {
  175. GPIO_InitTypeDef GPIO_InitStructure;
  176. RCC_APB2PeriphClockCmd(clk, ENABLE); //使能PB,PE端口时钟
  177. GPIO_InitStructure.GPIO_Pin = GPIO_Pin; //LED0-->PC.1 端口配置
  178. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //推挽输出
  179. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
  180. GPIO_Init(GPIOx, &GPIO_InitStructure); //根据设定参数初始化GPIOC.1
  181. }
  182. void GPIO_COMM_Init(u32 clk, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIOMode_TypeDef mode, GPIOSpeed_TypeDef speed)
  183. {
  184. GPIO_InitTypeDef GPIO_InitStructure;
  185. RCC_APB2PeriphClockCmd(clk, ENABLE);
  186. GPIO_InitStructure.GPIO_Pin = GPIO_Pin;
  187. GPIO_InitStructure.GPIO_Mode = mode;
  188. GPIO_InitStructure.GPIO_Speed = speed;
  189. GPIO_Init(GPIOx, &GPIO_InitStructure);
  190. GPIO_SetBits(GPIOx, GPIO_Pin);
  191. }
  192. void LED_Set(uint8_t pos)
  193. {
  194. /*
  195. pos 12 11 10 9 8 7 6 5 4 3 2 1
  196. L11 L10 L9 L8 L7 L6 L5 L4 L3 L2 L1 L0
  197. bit3 1 0 1 0 1 0 ~ ~ ~ ~ ~ ~
  198. bit2 0 1 ~ ~ ~ ~ 1 0 1 0 ~ ~
  199. bit1 ~ ~ 0 1 ~ ~ 0 1 ~ ~ 1 0
  200. bit0 ~ ~ ~ ~ 0 1 ~ ~ 0 1 0 1
  201. */
  202. uint8_t i;
  203. GPIO_InitTypeDef GPIO_InitStructure;
  204. GPIOMode_TypeDef GPIO_Mode = GPIO_Mode_Out_PP;
  205. uint16_t led_pin = 0;
  206. uint8_t led_val[12][4] = {
  207. {0xff, 0xff, 0, 1},
  208. {0xff, 0xff, 1, 0},
  209. {0xff, 0, 0xff, 1},
  210. {0xff, 1, 0xff, 0},
  211. {0, 0xff, 0xff, 1},
  212. {1, 0xff, 0xff, 0},
  213. {0xff, 0, 1, 0xff},
  214. {0xff, 1, 0, 0xff},
  215. {0, 0xff, 1, 0xff},
  216. {1, 0xff, 0, 0xff},
  217. {0, 1, 0xff, 0xff},
  218. {1, 0, 0xff, 0xff},
  219. };
  220. for(i = 0;i < 4;i++) {
  221. led_pin = 1<<(2+i);
  222. if(led_val[pos][i] == 0xff) {
  223. GPIO_Mode = GPIO_Mode_IN_FLOATING;
  224. } else {
  225. GPIO_Mode = GPIO_Mode_Out_PP;
  226. }
  227. GPIO_InitStructure.GPIO_Pin = led_pin;
  228. GPIO_InitStructure.GPIO_Mode = GPIO_Mode;
  229. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  230. GPIO_Init(GPIOE, &GPIO_InitStructure);
  231. if(led_val[pos][i] == 1) {
  232. GPIO_SetBits(GPIOE, led_pin);
  233. } else if(led_val[pos][i] == 0){
  234. GPIO_ResetBits(GPIOE, led_pin);
  235. }
  236. }
  237. }
  238. void BSP_Init(void)
  239. {
  240. u8 i;
  241. OS_ERR err;
  242. // uint16_t flag = 0;
  243. SystemInit();
  244. SysTick_init();
  245. watchdog_init(IWDG_Prescaler_256, 4000);
  246. RTC_Init();
  247. OSSemCreate(&i2c_sem, "i2c_sem", 1, &err);
  248. I2C_init();
  249. // adc_init();
  250. // fram_init_config_msg();
  251. for(i = 0;i < UART_MAX;i++) {
  252. if((i == UART4_ID)||(i == UART5_ID)) {
  253. if(1) {
  254. /* 明文采集器 */
  255. //uart_init(i, 9600, USART_WordLength_9b, USART_Parity_Even);
  256. /* 密文采集器 */
  257. uart_init(i, 115200, USART_WordLength_8b, USART_Parity_No);
  258. } else {/* 液位仪 */
  259. uart_init(i, 9600, USART_WordLength_8b, USART_Parity_No);
  260. }
  261. } else if(i == UART2_ID) {
  262. uart_init(i, 9600, USART_WordLength_8b, USART_Parity_No);
  263. } else if(i == UART3_ID) {
  264. uart_init(i, 115200, USART_WordLength_8b, USART_Parity_No);
  265. } else {
  266. uart_init(i, 115200, USART_WordLength_8b, USART_Parity_No);
  267. }
  268. }
  269. fram_read_gateway_hardware_msg();
  270. adc_init();
  271. #ifdef GAS_STATION
  272. /*
  273. (1)GPIO_Mode_AIN 模拟输入
  274. (2)GPIO_Mode_IN_FLOATING 浮空输入
  275. (3)GPIO_Mode_IPD 下拉输入
  276. (4)GPIO_Mode_IPU 上拉输入
  277. (5)GPIO_Mode_Out_OD 开漏输出
  278. (6)GPIO_Mode_Out_PP 推挽输出
  279. (7)GPIO_Mode_AF_OD 复用开漏输出
  280. (8)GPIO_Mode_AF_PP 复用推挽输出
  281. GPIO_Speed_10MHz 最高输出速率10MHz
  282. GPIO_Speed_2MHz 最高输出速率2MHz
  283. GPIO_Speed_50MHz 最高输出速率50MHz
  284. */
  285. // //STM32_GUN_DET_EN_0
  286. // GPIO_COMM_Init(RCC_APB2Periph_GPIOE, GPIOE,
  287. // GPIO_Pin_2, GPIO_Mode_Out_PP, GPIO_Speed_50MHz);
  288. // //KEY,PE0, PE1
  289. // GPIO_COMM_Init(RCC_APB2Periph_GPIOE, GPIOE,
  290. // GPIO_Pin_0 | GPIO_Pin_1, GPIO_Mode_IN_FLOATING, GPIO_Speed_50MHz);
  291. // //STM32_LED_RUN(PD1)
  292. // GPIO_COMM_Init(RCC_APB2Periph_GPIOD, GPIOD,
  293. // GPIO_Pin_1 , GPIO_Mode_Out_PP, GPIO_Speed_50MHz);
  294. // GPIO_SetBits(GPIOD, GPIO_Pin_1);
  295. // //MINI_PCIE_LED_WANN_N(PD12), 由4G模块驱动
  296. // GPIO_COMM_Init(RCC_APB2Periph_GPIOD, GPIOD,
  297. // GPIO_Pin_12, GPIO_Mode_IN_FLOATING, GPIO_Speed_50MHz);
  298. //LED(PC1,PC2)
  299. GPIO_COMM_Init(RCC_APB2Periph_GPIOC, GPIOC,
  300. GPIO_Pin_1|GPIO_Pin_2, GPIO_Mode_Out_PP, GPIO_Speed_50MHz);
  301. //RS485_CTL(PA1)
  302. //GPIO_COMM_Init(RCC_APB2Periph_GPIOA, GPIOA,
  303. // GPIO_Pin_1, GPIO_Mode_Out_PP, GPIO_Speed_50MHz);
  304. //RS485_RX();
  305. //KEY,PE12,13,14
  306. GPIO_COMM_Init(RCC_APB2Periph_GPIOE, GPIOE,
  307. GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14, GPIO_Mode_IN_FLOATING, GPIO_Speed_50MHz);
  308. //开关切换 USIM0 PC9, USIM1 PA8
  309. GPIO_COMM_Init(RCC_APB2Periph_GPIOC, GPIOC,
  310. GPIO_Pin_9, GPIO_Mode_Out_PP, GPIO_Speed_50MHz);
  311. GPIO_COMM_Init(RCC_APB2Periph_GPIOA, GPIOA,
  312. GPIO_Pin_8, GPIO_Mode_Out_PP, GPIO_Speed_50MHz);
  313. //USIM检测 USIM_CD
  314. GPIO_COMM_Init(RCC_APB2Periph_GPIOD, GPIOD,
  315. GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5, GPIO_Mode_IN_FLOATING, GPIO_Speed_50MHz);
  316. //USIM卡使能
  317. GPIO_COMM_Init(RCC_APB2Periph_GPIOC, GPIOC,
  318. GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8, GPIO_Mode_Out_PP, GPIO_Speed_50MHz);
  319. //MINI_PCIE_LED_WANN_N(PD15), 由4G模块驱动
  320. GPIO_COMM_Init(RCC_APB2Periph_GPIOD, GPIOD,
  321. GPIO_Pin_15, GPIO_Mode_IN_FLOATING, GPIO_Speed_50MHz);
  322. //NET_PWR_EN
  323. GPIO_COMM_Init(RCC_APB2Periph_GPIOD, GPIOD,
  324. GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14, GPIO_Mode_Out_PP, GPIO_Speed_50MHz);
  325. GPIO_SetBits(GPIOD,GPIO_Pin_14);
  326. GPIO_COMM_Init(RCC_APB2Periph_GPIOC, GPIOC,
  327. GPIO_Pin_3, GPIO_Mode_Out_PP, GPIO_Speed_50MHz);
  328. GPIO_ResetBits(GPIOC, GPIO_Pin_3);
  329. GPIO_ResetBits(GPIOD, GPIO_Pin_14);
  330. //for sim 1
  331. // //开关
  332. // USIM1_L();
  333. // USIM0_H();
  334. //
  335. // //使能
  336. // USIM1_PWR_ON();
  337. // USIM2_PWR_OFF();
  338. // USIM3_PWR_OFF();
  339. #if 0
  340. //chip select
  341. //STM32_I2C2_EEPROM_WP
  342. GPIO_COMM_Init(RCC_APB2Periph_GPIOE, GPIOE,
  343. GPIO_Pin_15, GPIO_Mode_Out_PP, GPIO_Speed_50MHz);
  344. GPIO_ResetBits(GPIOE, GPIO_Pin_15);
  345. //FLASH_WP_N
  346. GPIO_COMM_Init(RCC_APB2Periph_GPIOD, GPIOD,
  347. GPIO_Pin_10, GPIO_Mode_Out_PP, GPIO_Speed_50MHz);
  348. GPIO_ResetBits(GPIOD, GPIO_Pin_10);
  349. //EEPROM_SPI2_NSS_N
  350. GPIO_COMM_Init(RCC_APB2Periph_GPIOB, GPIOB,
  351. GPIO_Pin_12, GPIO_Mode_Out_PP, GPIO_Speed_50MHz);
  352. GPIO_SetBits(GPIOB, GPIO_Pin_12);
  353. #endif
  354. // //NET_PWR_EN
  355. // GPIO_COMM_Init(RCC_APB2Periph_GPIOD, GPIOD,
  356. // GPIO_Pin_0|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15, GPIO_Mode_Out_PP, GPIO_Speed_50MHz);
  357. // GPIO_SetBits(GPIOD, GPIO_Pin_0);
  358. {//EN_USIM_PWR_1 - 3 //SW_USIM_0 - 1
  359. // GPIO_COMM_Init(RCC_APB2Periph_GPIOC, GPIOC,
  360. // GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9, GPIO_Mode_Out_PP, GPIO_Speed_50MHz);
  361. // GPIO_COMM_Init(RCC_APB2Periph_GPIOA, GPIOA,
  362. // GPIO_Pin_8, GPIO_Mode_Out_PP, GPIO_Speed_50MHz);
  363. #if 1
  364. // //for sim 1
  365. // GPIO_SetBits(GPIOC, GPIO_Pin_6);
  366. // GPIO_ResetBits(GPIOC, GPIO_Pin_7);
  367. // GPIO_ResetBits(GPIOC, GPIO_Pin_8);
  368. // GPIO_SetBits(GPIOC, GPIO_Pin_9);
  369. // GPIO_ResetBits(GPIOA, GPIO_Pin_8);
  370. #else
  371. //for sim 2
  372. GPIO_ResetBits(GPIOC, GPIO_Pin_6);
  373. GPIO_ResetBits(GPIOC, GPIO_Pin_7);
  374. GPIO_SetBits(GPIOC, GPIO_Pin_8);
  375. GPIO_ResetBits(GPIOC, GPIO_Pin_9);
  376. GPIO_ResetBits(GPIOA, GPIO_Pin_8);
  377. #endif
  378. }
  379. #else
  380. #ifdef MSTS_C
  381. LED_Init(RCC_APB2Periph_GPIOD, GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
  382. LED_Init(RCC_APB2Periph_GPIOC, GPIOC, GPIO_Pin_6 | GPIO_Pin_7);
  383. #else
  384. LED_Init(RCC_APB2Periph_GPIOB, GPIOB, GPIO_Pin_13 | GPIO_Pin_14);
  385. #endif
  386. #endif
  387. }
  388. /*-----------------------------------------------------------------
  389. * 检查usim卡的情况
  390. * 返回值 :0: 没有卡 ,非0: 已经插入卡
  391. * ------------------------------------------------------------------*/
  392. uint8_t check_usim_status(uint8_t type)
  393. {
  394. uint8_t usmflag = 0;
  395. static uint8_t lastusmflag = 0x00;
  396. // if(type==1){
  397. // lastusmflag = 0xFF;
  398. // }
  399. // printf("\r\n usim插入情况:\r\n");
  400. if(USIM1_CD){
  401. usmflag |= 0x01;
  402. // printf("usim1 on\n");
  403. }
  404. else{
  405. usmflag &= (~(0x01<<0));
  406. // printf("usim1 off\n");
  407. }
  408. if(USIM2_CD){
  409. usmflag |= 0x02;
  410. // printf("usim2 on\n");
  411. }
  412. else{
  413. usmflag &= (~(0x01<<1));
  414. // printf("usim2 off\n");
  415. }
  416. if(USIM3_CD){
  417. usmflag |= 0x04;
  418. // printf("usim3 on\n");
  419. }
  420. else{
  421. usmflag &= (~(0x01<<2));
  422. // printf("usim3 off\n");
  423. }
  424. if(usmflag == 0) {
  425. printf(" usim插入情况:no usim \r\n");
  426. lastusmflag = usmflag;
  427. return usmflag;
  428. }
  429. else {
  430. if(usmflag!= lastusmflag){
  431. if(usmflag&0x01) printf("usim1 on\n");
  432. else printf("usim1 off\n");
  433. if(usmflag&0x02) printf("usim2 on\n");
  434. else printf("usim2 off\n");
  435. if(usmflag&0x04) printf("usim3 on\n");
  436. else printf("usim3 off\n");
  437. lastusmflag = usmflag;
  438. timeout_stop(&g_usmTimeOut);
  439. g_data4G.flag = 0;
  440. g_data4G.errorTime = 0;
  441. sim_switch(usmflag);
  442. return usmflag;
  443. }
  444. }
  445. //if((usmflag!=0) && (usmflag == lastusmflag)) return 0;
  446. //lastusmflag = usmflag;
  447. return 0;
  448. }
  449. void sim_switch(uint8_t code)
  450. {
  451. printf("\r\n usim使用情况:\r\n");
  452. if((code&0x01)==0x01){
  453. USIM1_L();
  454. USIM0_H();
  455. //使能
  456. USIM1_PWR_ON();
  457. USIM2_PWR_OFF();
  458. USIM3_PWR_OFF();
  459. printf("use usim_01 \r\n");
  460. }
  461. else if((code&0x02) == 0x02) {
  462. USIM1_H();
  463. USIM0_L();
  464. //使能
  465. USIM1_PWR_OFF();
  466. USIM2_PWR_ON();
  467. USIM3_PWR_OFF();
  468. printf("use usim_02 \r\n");
  469. }
  470. else if((code&0x04) == 0x04) {
  471. USIM1_H();
  472. USIM0_H();
  473. //使能
  474. USIM1_PWR_OFF();
  475. USIM2_PWR_OFF();
  476. USIM3_PWR_ON();
  477. printf("use usim_03 \r\n");
  478. }
  479. else {
  480. printf("no usim \r\n");
  481. }
  482. }
  483. //按键处理函数
  484. //返回按键值
  485. //mode:0,不支持连续按;1,支持连续按;
  486. //0,没有任何按键按下
  487. //1,KEY0按下
  488. //2,KEY1按下
  489. //3,KEY2按下
  490. //4,KEY3按下 WK_UP
  491. //注意此函数有响应优先级,KEY0>KEY1>KEY2>KEY3!!
  492. uint8_t KEY_Scan(uint8_t mode)
  493. {
  494. static int8_t key0PressPreStater=0;
  495. static int8_t key1PressPreStater=0;
  496. static int8_t key2PressPreStater=0;
  497. uint8_t key0_p=1;
  498. uint8_t key1_p=1;
  499. uint8_t key2_p=1;
  500. uint8_t res=0;
  501. key0_p = KEY0;
  502. if((key0_p == 0)&&(key0PressPreStater==1)){
  503. key0PressPreStater = 0;
  504. res = KEY0_PRES;
  505. return res;
  506. }
  507. else key0PressPreStater = KEY0;
  508. key1_p = KEY2;
  509. if((key1_p == 0)&&(key1PressPreStater==1)){
  510. key1PressPreStater = 0;
  511. res = KEY1_PRES;
  512. // key_info.keyvalue = KEY1_PRES;
  513. return res;
  514. }
  515. else {
  516. key1PressPreStater = KEY2;
  517. // key_info.keyvalue = KEY1_PRES;
  518. }
  519. key2_p = KEY1;
  520. if((key2_p == 0)&&(key2PressPreStater==1)){
  521. key2PressPreStater = 0;
  522. res = KEY2_PRES;
  523. return res;
  524. }
  525. else key2PressPreStater = KEY1;
  526. return 0;// 无按键按下
  527. }
  528. /*
  529. *********************************************************************************************************
  530. * BSP_CPU_ClkFreq()
  531. *
  532. * Description : Read CPU registers to determine the CPU clock frequency of the chip.
  533. * 读取cpu寄存器确定时钟频率
  534. * Argument(s) : none.
  535. *
  536. * Return(s) : The CPU clock frequency, in Hz. 返回CPU始终频率
  537. *
  538. * Caller(s) : Application.
  539. *
  540. * Note(s) : none.
  541. *********************************************************************************************************
  542. */
  543. CPU_INT32U BSP_CPU_ClkFreq (void)
  544. {
  545. RCC_ClocksTypeDef rcc_clocks;
  546. RCC_GetClocksFreq(&rcc_clocks);
  547. return ((CPU_INT32U)rcc_clocks.HCLK_Frequency);
  548. }
  549. #if (CPU_CFG_TS_TMR_EN == DEF_ENABLED)
  550. void CPU_TS_TmrInit (void)
  551. {
  552. CPU_INT32U cpu_clk_freq_hz;
  553. cpu_clk_freq_hz = BSP_CPU_ClkFreq();
  554. CPU_TS_TmrFreqSet(cpu_clk_freq_hz);
  555. }
  556. #endif
  557. /*$PAGE*/
  558. /*
  559. *********************************************************************************************************
  560. * CPU_TS_TmrRd()
  561. *
  562. * Description : Get current CPU timestamp timer count value.
  563. *
  564. * Argument(s) : none.
  565. *
  566. * Return(s) : Timestamp timer count (see Notes #2a & #2b).
  567. *
  568. * Caller(s) : CPU_TS_Init(),
  569. * CPU_TS_Get32(),
  570. * CPU_TS_Get64(),
  571. * CPU_IntDisMeasStart(),
  572. * CPU_IntDisMeasStop().
  573. *
  574. * This function is an INTERNAL CPU module function & MUST be implemented by application/
  575. * BSP function(s) [see Note #1] but SHOULD NOT be called by application function(s).
  576. *
  577. * Note(s) : (1) CPU_TS_TmrRd() is an application/BSP function that MUST be defined by the developer
  578. * if either of the following CPU features is enabled :
  579. *
  580. * (a) CPU timestamps
  581. * (b) CPU interrupts disabled time measurements
  582. *
  583. * See 'cpu_cfg.h CPU TIMESTAMP CONFIGURATION Note #1'
  584. * & 'cpu_cfg.h CPU INTERRUPTS DISABLED TIME MEASUREMENT CONFIGURATION Note #1a'.
  585. *
  586. * (2) (a) Timer count values MUST be returned via word-size-configurable 'CPU_TS_TMR'
  587. * data type.
  588. *
  589. * (1) If timer has more bits, truncate timer values' higher-order bits greater
  590. * than the configured 'CPU_TS_TMR' timestamp timer data type word size.
  591. *
  592. * (2) Since the timer MUST NOT have less bits than the configured 'CPU_TS_TMR'
  593. * timestamp timer data type word size; 'CPU_CFG_TS_TMR_SIZE' MUST be
  594. * configured so that ALL bits in 'CPU_TS_TMR' data type are significant.
  595. *
  596. * In other words, if timer size is not a binary-multiple of 8-bit octets
  597. * (e.g. 20-bits or even 24-bits), then the next lower, binary-multiple
  598. * octet word size SHOULD be configured (e.g. to 16-bits). However, the
  599. * minimum supported word size for CPU timestamp timers is 8-bits.
  600. *
  601. * See also 'cpu_cfg.h CPU TIMESTAMP CONFIGURATION Note #2'
  602. * & 'cpu_core.h CPU TIMESTAMP DATA TYPES Note #1'.
  603. *
  604. * (b) Timer SHOULD be an 'up' counter whose values increase with each time count.
  605. *
  606. * (1) If timer is a 'down' counter whose values decrease with each time count,
  607. * then the returned timer value MUST be ones-complemented.
  608. *
  609. * (c) (1) When applicable, the amount of time measured by CPU timestamps is
  610. * calculated by either of the following equations :
  611. *
  612. * (A) Time measured = Number timer counts * Timer period
  613. *
  614. * where
  615. *
  616. * Number timer counts Number of timer counts measured
  617. * Timer period Timer's period in some units of
  618. * (fractional) seconds
  619. * Time measured Amount of time measured, in same
  620. * units of (fractional) seconds
  621. * as the Timer period
  622. *
  623. * Number timer counts
  624. * (B) Time measured = ---------------------
  625. * Timer frequency
  626. *
  627. * where
  628. *
  629. * Number timer counts Number of timer counts measured
  630. * Timer frequency Timer's frequency in some units
  631. * of counts per second
  632. * Time measured Amount of time measured, in seconds
  633. *
  634. * (2) Timer period SHOULD be less than the typical measured time but MUST be less
  635. * than the maximum measured time; otherwise, timer resolution inadequate to
  636. * measure desired times.
  637. *********************************************************************************************************
  638. */
  639. //#if (CPU_CFG_TS_TMR_EN == DEF_ENABLED)
  640. //CPU_TS_TMR CPU_TS_TmrRd (void)
  641. //{
  642. // return (CPU_TS_TMR)0;
  643. //}
  644. //#endif
  645. //#if (CPU_CFG_TS_TMR_EN == DEF_ENABLED)
  646. //void CPU_TS_TmrInit2 (void)
  647. //{
  648. // CPU_INT32U fclk_freq;
  649. //
  650. //
  651. // fclk_freq = BSP_CPU_ClkFreq();
  652. //
  653. // BSP_REG_DEM_CR |= (CPU_INT32U)BSP_BIT_DEM_CR_TRCENA;
  654. // BSP_REG_DWT_CYCCNT = (CPU_INT32U)0u;
  655. // BSP_REG_DWT_CR |= (CPU_INT32U)BSP_BIT_DWT_CR_CYCCNTENA;
  656. //
  657. // CPU_TS_TmrFreqSet((CPU_TS_TMR_FREQ)fclk_freq);
  658. //}
  659. //#endif
  660. #if (CPU_CFG_TS_TMR_EN == DEF_ENABLED)
  661. CPU_TS_TMR CPU_TS_TmrRd (void)
  662. {
  663. extern volatile uint32_t TickCounter;
  664. return (TickCounter);
  665. }
  666. #endif
  667. void board_info_get(uint16_t *flash_size, uint16_t *ram_size, uint8_t * chip_id)
  668. {
  669. //0x36393359_ffffff39_43013637
  670. // uint32_t *p_chip_id = (uint32_t *)chip_id;
  671. if(ram_size) {
  672. *ram_size = ( *( uint32_t* )0x1FFFF7E0 ) >> 16;
  673. // printf("STM32 Flash memory's ram size = %dkB\r\n", *ram_size);
  674. }
  675. if(flash_size) {
  676. *flash_size = ( *( uint32_t* )0x1FFFF7E0 ) & 0xffff;
  677. // printf("STM32 Flash memory's flash size = %dkB\r\n", *flash_size);
  678. }
  679. if(chip_id) {
  680. //memcpy(chip_id, ( ( uint8_t* )0x1FFFF7E8), 12);
  681. //p_chip_id = (uint32_t *)chip_id;
  682. memcpy(g_firmwareMsg.gatewayMsg.Uuid,( uint8_t* )(0x1FFFF7E8),12);
  683. // *p_chip_id = *(( uint32_t* )0x1FFFF7E8);
  684. // memcpy(g_firmwareMsg.gatewayMsg.Uuid,(uint8_t*)p_chip_id,4);
  685. // p_chip_id++;
  686. // *p_chip_id = *(( uint32_t* )0x1FFFF7EC);
  687. // memcpy(g_firmwareMsg.gatewayMsg.Uuid+4,(uint8_t*)p_chip_id,4);
  688. // p_chip_id++;
  689. // *p_chip_id = *(( uint32_t* )0x1FFFF7F0);
  690. // memcpy(g_firmwareMsg.gatewayMsg.Uuid+8,(uint8_t*)p_chip_id,4);
  691. // p_chip_id = (uint32_t *)chip_id;
  692. // printf("Unique device ID register (96 bits). ID(Hex) = 0x%08x_%08x_%08x\r\n", *p_chip_id++, *p_chip_id++, *p_chip_id);
  693. }
  694. }