BSP.c 23 KB

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