app_config.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "app_config.h"
  2. #include "nrf_gpio.h"
  3. #include "usr_config.h"
  4. #include "bsp_time.h"
  5. #include "system.h"
  6. #include "hal_mt.h"
  7. #include "hal_battery.h"
  8. #include "app_charge.h"
  9. #include "hal_ble_client.h"
  10. #include "hal_led.h"
  11. #include "hal_flash.h"
  12. #include "ble_gap.h"
  13. #include "hal_ble_uart0.h"
  14. #include "ble_comm.h"
  15. static ble_gap_addr_t mAddr;
  16. static uint8_t isConfig = 0;
  17. void UART0_Config_Send(uint8_t cmd)
  18. {
  19. uint8_t buf[32];
  20. uint8_t L = 0;
  21. //MAC 地址
  22. for(int i=0;i<6;i++){
  23. buf[L++] = mAddr.addr[5-i];
  24. }
  25. //硬件版本
  26. buf[L++] = (uint8_t)((uint16_t)HARDWARE_VERSION>>8);
  27. buf[L++] = (uint8_t)((uint16_t)HARDWARE_VERSION>>0);
  28. //软件版本
  29. buf[L++] = (uint8_t)((uint16_t)SOFTWARE_VERSION>>8);
  30. buf[L++] = (uint8_t)((uint16_t)SOFTWARE_VERSION>>0);
  31. UART0_Tx_Send(0,cmd,buf,L);
  32. }
  33. void app_config_Process(void)
  34. {
  35. static uint8_t flag = 0;
  36. if(app_charge_Getstate()!=BLE_Client_T_CHARGE_PULLOUT){
  37. if(flag != 1)
  38. {
  39. // UART0_Initialize();
  40. flag = 1;
  41. }
  42. else if(isConfig<50){ isConfig++;
  43. if(mFlash.isHost){ //充电模式下主机主机申请配对
  44. // UART0_Config_Send(UART0_T_CONFIG_ASK);
  45. }
  46. }
  47. }else{
  48. if(flag != 2)
  49. {
  50. // UART0_unInit();
  51. flag = 2;
  52. }
  53. else if(isConfig>0) isConfig = 0;
  54. }
  55. }
  56. void app_config_LED(void)
  57. {
  58. static uint8_t state = 0;
  59. switch(state){
  60. case 0:
  61. Process_SetHoldOn(app_config_LED,1);
  62. LED_SetColor(LED_CONFIG,COLOR_BLUE);
  63. LED_Start(LED_CONFIG);
  64. state = 1;
  65. Process_UpdatePeroid(app_config_LED,2000);
  66. break;
  67. case 1:
  68. Process_UpdatePeroid(app_config_LED,0);
  69. LED_Stop(LED_CONFIG);
  70. state = 0;
  71. Process_SetHoldOn(app_config_LED,0);
  72. Process_Stop(app_config_LED);
  73. break;
  74. default:state=0;Process_UpdatePeroid(app_config_LED,0);break;
  75. }
  76. }
  77. void cb_UART0_R_CONFIG_ASK(void* handle)
  78. { //从机接收到配对申请
  79. UART0_Rx_t* target = handle;
  80. uint8_t mac[6];
  81. char buf[16];
  82. uint8_t ret = 0;
  83. if(app_charge_Getstate()==BLE_Client_T_CHARGE_PULLOUT) return; //非充电模式不匹配
  84. if(mFlash.isHost) return; //主机自发自收
  85. for(int i=0;i<6;i++) mac[i] = target->pDat[i];
  86. uint16_t hv = ((uint16_t)target->pDat[6]<<8)| ((uint16_t)target->pDat[7]<<0);
  87. uint16_t sv = ((uint16_t)target->pDat[8]<<8)| ((uint16_t)target->pDat[9]<<0);
  88. if(slave_isconnect()) slave_disconnect();
  89. else advertising_stop();
  90. UART0_Config_Send(UART0_T_CONFIG_ACK);//应答主机申请
  91. mFlash.mClient.isConfig = 'C';
  92. if(mBackup.isConfig != mFlash.mClient.isConfig)ret = 1;
  93. for(int i=0;i<6;i++){
  94. mFlash.mClient.macAddr[i] = mAddr.addr[5-i]; //从机自身mac地址
  95. mFlash.macHost[i] = mac[i]; //主机发过来的mac地址
  96. if(mBackup.macAddr_L[i]!=mFlash.macHost[i]) ret = 1;
  97. if(mBackup.macAddr_R[i]!=mFlash.mClient.macAddr[i]) ret = 1;
  98. }
  99. mFlash.mClient.hardVersion = hv;
  100. mFlash.mClient.sotfVersion = sv;
  101. if( mBackup.hardVersion != mFlash.mClient.hardVersion) ret = 1;
  102. if( mBackup.sotfVersion != mFlash.mClient.sotfVersion) ret = 1;
  103. DEBUG_LOG("mFlash.mClient.hardVersion:%X\n",mFlash.mClient.hardVersion);
  104. DEBUG_LOG("mFlash.mClient.sotfVersion:%X\n",mFlash.mClient.sotfVersion);
  105. DEBUG_LOG("mFlash.mClient.macAddr:%02X %02X %02X %02X %02X %02X\n",mFlash.mClient.macAddr[0],mFlash.mClient.macAddr[1],mFlash.mClient.macAddr[2],mFlash.mClient.macAddr[3],mFlash.mClient.macAddr[4],mFlash.mClient.macAddr[5]);
  106. DEBUG_LOG("mFlash.macHost:%02X %02X %02X %02X %02X %02X\n",mFlash.macHost[0],mFlash.macHost[1],mFlash.macHost[2],mFlash.macHost[3],mFlash.macHost[4],mFlash.macHost[5]);
  107. Process_Start(0,"config_LED",app_config_LED);
  108. memset(buf,0,16);
  109. sprintf(buf,"%02X%02X%02X%02X%02X%02X",mFlash.macHost[0],mFlash.macHost[1],mFlash.macHost[2],mFlash.mClient.macAddr[3],mFlash.mClient.macAddr[4],mFlash.mClient.macAddr[5]);
  110. DEBUG_LOG("advName(%d):%s\n",strlen(buf),buf);
  111. slave_set_adv_name(buf,strlen(buf));
  112. slave_adv_init();
  113. if(ret >=1){
  114. for(int i=0;i<6;i++){
  115. mBackup.macAddr_L[i] = mFlash.macHost[i]; //主机地址
  116. mBackup.macAddr_R[i] = mFlash.mClient.macAddr[i];//从机地址
  117. }
  118. mBackup.hardVersion = mFlash.mClient.hardVersion;
  119. mBackup.sotfVersion = mFlash.mClient.sotfVersion;
  120. mBackup.isConfig = mFlash.mClient.isConfig;
  121. //DEBUG_LOG("AAAAAbackup config:%d,hardVersion:%d,sotfVersion:%d\n",mBackup.isConfig,mBackup.hardVersion,mBackup.sotfVersion);
  122. Flash_SaveInfomation();
  123. Flash_SaveBackup();
  124. }
  125. while(slave_isconnect());
  126. advertising_start();
  127. // nrf_gpio_cfg_output(PIN_LED_RUN);
  128. // nrf_gpio_pin_write(PIN_LED_RUN,0);
  129. }
  130. void cb_UART0_R_CONFIG_ACK(void* handle)
  131. { //主机接收到配对应答
  132. UART0_Rx_t* target = handle;
  133. uint8_t mac[6];
  134. char buf[16];
  135. uint8_t ret = 0;
  136. if(app_charge_Getstate()==BLE_Client_T_CHARGE_PULLOUT) return; //非充电模式不匹配
  137. if(!mFlash.isHost) return; //从机自发自收
  138. for(int i=0;i<6;i++) mac[i] = target->pDat[i];
  139. uint16_t hv = ((uint16_t)target->pDat[6]<<8)| ((uint16_t)target->pDat[7]<<0);
  140. uint16_t sv = ((uint16_t)target->pDat[8]<<8)| ((uint16_t)target->pDat[9]<<0);
  141. isConfig = 100; //已配对,停止配对发送
  142. mFlash.mClient.isConfig = 'C';
  143. if(mBackup.isConfig != mFlash.mClient.isConfig)ret = 1;
  144. for(int i=0;i<6;i++){
  145. mFlash.mClient.macAddr[i] = mac[i]; //收到的从机mac地址
  146. mFlash.macHost[i] = mAddr.addr[5-i]; //主机本身mac地址
  147. if(mBackup.macAddr_L[i]!=mFlash.macHost[i]) ret = 1;
  148. if(mBackup.macAddr_R[i]!=mFlash.mClient.macAddr[i]) ret = 1;
  149. }
  150. mFlash.mClient.hardVersion = hv;
  151. mFlash.mClient.sotfVersion = sv;
  152. if( mBackup.hardVersion != mFlash.mClient.hardVersion) ret = 1;
  153. if( mBackup.sotfVersion != mFlash.mClient.sotfVersion) ret = 1;
  154. DEBUG_LOG("mFlash.mClient.hardVersion:%X\n",mFlash.mClient.hardVersion);
  155. DEBUG_LOG("mFlash.mClient.sotfVersion:%X\n",mFlash.mClient.sotfVersion);
  156. DEBUG_LOG("mFlash.mClient.macAddr:%02X %02X %02X %02X %02X %02X\n",mFlash.mClient.macAddr[0],mFlash.mClient.macAddr[1],mFlash.mClient.macAddr[2],mFlash.mClient.macAddr[3],mFlash.mClient.macAddr[4],mFlash.mClient.macAddr[5]);
  157. DEBUG_LOG("mFlash.macHost:%02X %02X %02X %02X %02X %02X\n",mFlash.macHost[0],mFlash.macHost[1],mFlash.macHost[2],mFlash.macHost[3],mFlash.macHost[4],mFlash.macHost[5]);
  158. Process_Start(0,"config_LED",app_config_LED);
  159. memset(buf,0,16);
  160. sprintf(buf,"%02X%02X%02X%02X%02X%02X",mFlash.macHost[0],mFlash.macHost[1],mFlash.macHost[2],mFlash.mClient.macAddr[3],mFlash.mClient.macAddr[4],mFlash.mClient.macAddr[5]);
  161. DEBUG_LOG("scanName(%d):%s\n",strlen(buf),buf);
  162. host_set_scan_name(buf,strlen(buf));
  163. if(ret >=1){
  164. for(int i=0;i<6;i++){
  165. mBackup.macAddr_L[i] = mFlash.macHost[i]; //主机地址
  166. mBackup.macAddr_R[i] = mFlash.mClient.macAddr[i];//从机地址
  167. }
  168. mBackup.hardVersion = mFlash.mClient.hardVersion;
  169. mBackup.sotfVersion = mFlash.mClient.sotfVersion;
  170. mBackup.isConfig = mFlash.mClient.isConfig;
  171. //DEBUG_LOG("AAAAAbackup config:%d,hardVersion:%d,sotfVersion:%d\n",mBackup.isConfig,mBackup.hardVersion,mBackup.sotfVersion);
  172. Flash_SaveInfomation();
  173. Flash_SaveBackup();
  174. }
  175. //nrf_gpio_cfg_output(PIN_LED_RUN); nrf_gpio_pin_write(PIN_LED_RUN,0);
  176. }
  177. void app_config_Init(void)
  178. {
  179. uint32_t err_code = sd_ble_gap_addr_get(&mAddr); APP_ERROR_CHECK(err_code);
  180. Process_Start(100,"app_config",app_config_Process);
  181. UART0_Rx_Regist(UART0_R_CONFIG_ASK,cb_UART0_R_CONFIG_ASK);
  182. UART0_Rx_Regist(UART0_R_CONFIG_ACK,cb_UART0_R_CONFIG_ACK);
  183. }