mod_atcmd.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include <stdint.h>
  2. #include "string.h"
  3. #include "stdbool.h"
  4. #include "stdlib.h"
  5. #include "sys_log.h"
  6. #include "sys_ver.h"
  7. #include "sys_common.h"
  8. #include "mod_atcmd.h"
  9. #include "mod_sys.h"
  10. #include "hal_can.h"
  11. #include "hal_spi.h"
  12. #include "hal_key.h"
  13. #include "hal_adc.h"
  14. #include "hal_ble.h"
  15. #include "hal_flash.h"
  16. #include "mod_spi.h"
  17. #define RX_CMD_MAX_LEN 32
  18. uint8_t g_s_at_buf[RX_CMD_MAX_LEN];
  19. static void Sys_Soft_Reset(void)
  20. {
  21. SCB->AIRCR = 0X05FA0000 | (u32)0x04;
  22. }
  23. void MOD_AtCmdDataProcess(void *data, uint16_t length)
  24. {
  25. const char *tmp_buf = NULL;
  26. memset(g_s_at_buf, 0, RX_CMD_MAX_LEN);
  27. memcpy(g_s_at_buf, data, length);
  28. tmp_buf = (char *)g_s_at_buf;
  29. if (!strncmp(tmp_buf, "AT+CANTX", strlen("AT+CANTX")))
  30. {
  31. uint32_t can_id = 0x6F1;
  32. uint8_t send_buf[8] = {0x60, 0x03, 0x22, 0xF1, 0x8B, 0xFF, 0xFF, 0xFF};
  33. DLOG_I("send data to can");
  34. HAL_CanTxData(0, can_id, send_buf, 8);
  35. }
  36. else if (!strncmp(tmp_buf, "AT+ISOTX", strlen("AT+ISOTX")))
  37. {
  38. uint32_t can_id = 0x6F1;
  39. // uint8_t send_buf[] = {0x6a,0x68,0x6f,0x47,0x4b,0x43,0x67,0x64,0x6e,0x45,0x6f,0x71};
  40. uint8_t send_buf[] = {0x62,0xf1,0x01,0x01,0x01,
  41. 0x00,0x04,0x21,0x06,0x22,0x8f,
  42. 0x04,0xd2,0x01,0x00,0x00,0x10,
  43. 0x00,0x00,0x00,0x01,0x00,0x00,
  44. 0x3c,0xfb,0x04,0x00,0x00,0x06,
  45. 0x00,0x00,0x3c,0xfc,0x04,0x00,
  46. 0x00,0x08,0x00,0x00,0x34,0xec,
  47. 0x0d,0x00,0x00,0x08,0x00,0x00,
  48. 0x43,0x4e,0x0d,0x00,0x01};
  49. DLOG_I("iso15765 tx");
  50. MOD_CanTransferData(send_buf, sizeof(send_buf));
  51. }
  52. else if (!strncmp(tmp_buf, "AT+REBOOT", strlen("AT+REBOOT")))
  53. {
  54. Sys_Soft_Reset();
  55. }
  56. else if (!strncmp(tmp_buf, "AT+SPITX", strlen("AT+SPITX")))
  57. {
  58. uint8_t send_buf[32];
  59. int i;
  60. for (i = 0; i < 32; i++)
  61. {
  62. send_buf[i] = 0xF1 + i;
  63. }
  64. DLOG_I("spi send data");
  65. HAL_SpiTxData(send_buf, 32);
  66. }
  67. else if (!strncmp(tmp_buf, "AT+MCUVER", strlen("AT+MCUVER")))
  68. {
  69. int ver = get_ver();
  70. DLOG_I("soft_ver:0x%x", ver);
  71. }
  72. else if (!strncmp(tmp_buf, "AT+GETBLEVER", strlen("AT+GETBLEVER")))
  73. {
  74. MOD_SpiDataSend(BLE_CMD_TEST, NULL, 0);
  75. }
  76. else if (!strncmp(tmp_buf, "AT+KEYSTA", strlen("AT+KEYSTA")))
  77. {
  78. uint8_t status = HAL_KeyGetStatus();
  79. DLOG_I("key status:%d", status);
  80. }
  81. else if (!strncmp(tmp_buf, "AT+ADCVAL", strlen("AT+ADCVAL")))
  82. {
  83. int value = HAL_AdcGetVal();
  84. int mv = value * (3.3 / 4096) * 1000;
  85. int battery_mv = mv * 6;
  86. // 电池电压需要乘以6
  87. DLOG_I("adv_val:%d,battery_mv:%dmv", value, battery_mv);
  88. }
  89. else if (!strncmp(tmp_buf, "AT+SPITRANS", strlen("AT+SPITRANS")))
  90. {
  91. uint8_t buffer[] = {0x52, 0x12, 0x00, 0x00, 0x00, 0x06, 0xF1, 0x01, 0x11};
  92. MOD_SpiDataSend(BLE_CMD_TRANSFER, buffer, 9);
  93. }
  94. else if (!strncmp(tmp_buf, "AT+BLEON", strlen("AT+BLEON")))
  95. {
  96. HAL_SpiGpioInit();
  97. HAL_BleGpioEn(true);
  98. }
  99. else if (!strncmp(tmp_buf, "AT+BLEOFF", strlen("AT+BLEOFF")))
  100. {
  101. HAL_BleGpioEn(false);
  102. HAL_SpiClockPinReset();
  103. }
  104. else if (!strncmp(tmp_buf, "AT+BLEREBOOT", strlen("AT+BLEREBOOT")))
  105. {
  106. DLOG_I("reboot ble");
  107. HAL_BleReboot();
  108. }
  109. else if (!strncmp(tmp_buf, "AT+CHECKSECTOR=", strlen("AT+CHECKSECTOR=")))
  110. {
  111. uint8_t erase_index = atoi(&tmp_buf[strlen("AT+CHECKSECTOR=")]);
  112. DLOG_I("erase_index:%d", erase_index);
  113. if (1 == erase_index)
  114. {
  115. MOD_SysCheckSector(FLASH_MAIN_PARA_ADDR);
  116. }
  117. else if (2 == erase_index)
  118. {
  119. MOD_SysCheckSector(FLASH_BACKUP_PARA_ADDR);
  120. }
  121. }
  122. else if (!strncmp(tmp_buf, "AT+FERASE=", strlen("AT+FERASE=")))
  123. {
  124. uint8_t erase_index = atoi(&tmp_buf[strlen("AT+FERASE=")]);
  125. DLOG_I("erase_index:%d", erase_index);
  126. if (1 == erase_index)
  127. {
  128. HAL_FlashSectorErase(FLASH_MAIN_PARA_ADDR);
  129. }
  130. else if (2 == erase_index)
  131. {
  132. HAL_FlashSectorErase(FLASH_BACKUP_PARA_ADDR);
  133. }
  134. }
  135. else if (!strncmp(tmp_buf, "AT+FWRITE=", strlen("AT+FWRITE=")))
  136. {
  137. uint8_t write_buf[64];
  138. int i;
  139. uint8_t write_ret;
  140. uint8_t write_index = atoi(&tmp_buf[strlen("AT+FWRITE=")]);
  141. DLOG_I("write_index:%d", write_index);
  142. for (i = 0; i < 64; i++)
  143. {
  144. write_buf[i] = i;
  145. }
  146. if (1 == write_index)
  147. {
  148. write_ret = HAL_FlashWriteBytes(FLASH_MAIN_PARA_ADDR, write_buf, 64);
  149. }
  150. else if (2 == write_index)
  151. {
  152. write_ret = HAL_FlashWriteBytes(FLASH_BACKUP_PARA_ADDR, write_buf, 64);
  153. }
  154. DLOG_I("write flash ret:%d", write_ret);
  155. }
  156. else if (!strncmp(tmp_buf, "AT+FREAD=", strlen("AT+FREAD=")))
  157. {
  158. uint8_t read_buf[64];
  159. uint8_t read_index;
  160. DLOG_I("read_index:%d", read_index);
  161. memset(read_buf, 0, 64);
  162. read_index = atoi(&tmp_buf[strlen("AT+FREAD=")]);
  163. if (1 == read_index)
  164. {
  165. HAL_FlashReadBytes(FLASH_MAIN_PARA_ADDR, read_buf, 64);
  166. DLOG_I("read_tmp_buf:%s", SYS_Hex2str(read_buf, 64));
  167. }
  168. else if (2 == read_index)
  169. {
  170. HAL_FlashReadBytes(FLASH_BACKUP_PARA_ADDR, read_buf, 64);
  171. DLOG_I("read_tmp_buf:%s", SYS_Hex2str(read_buf, 64));
  172. }
  173. }
  174. else if (!strncmp(tmp_buf, "AT+SHOWPARA", strlen("AT+SHOWPARA")))
  175. {
  176. DLOG_I("show para");
  177. MOD_SysShowPara();
  178. }
  179. else if (!strncmp(tmp_buf, "AT+MAC=", strlen("AT+MAC=")))
  180. {
  181. uint8_t ble_mac[6];
  182. memset(ble_mac, 0, 6);
  183. if('?' == tmp_buf[strlen("AT+MAC=")])
  184. {
  185. memcpy(ble_mac, (uint8_t* )MOD_SysGetPara(PARA_BLE_MAC), 6);
  186. DLOG_I("ble_mac:%X%X%X%X%X%X", ble_mac[0],ble_mac[1],ble_mac[2],ble_mac[3],ble_mac[4],ble_mac[5]);
  187. }
  188. else
  189. {
  190. if(length - 7 != 12)
  191. {
  192. DLOG_I("input mac len:%d error", length - strlen("AT+MAC="));
  193. }
  194. MOD_SysSetPara(PARA_BLE_MAC, 6, SYS_Str2hex((uint8_t*)&tmp_buf[7], 12));
  195. }
  196. }
  197. else if (!strncmp(tmp_buf, "AT+NAME=", strlen("AT+NAME=")))
  198. {
  199. uint8_t ble_name[8] = {0};
  200. uint8_t name_len = 0;
  201. uint8_t* p_name = NULL;
  202. if('?' == tmp_buf[strlen("AT+NAME=")])
  203. {
  204. p_name = (uint8_t* )MOD_SysGetPara(PARA_BLE_NAME);
  205. DLOG_I("ble_name:%s,len:%d", p_name, strlen(p_name));
  206. }
  207. else
  208. {
  209. name_len = length - strlen("AT+NAME=");
  210. memcpy(ble_name, (uint8_t* )&tmp_buf[strlen("AT+NAME=")], name_len);
  211. ble_name[name_len] = '\0';
  212. DLOG_I("set ble_name:%s", ble_name);
  213. MOD_SysSetPara(PARA_BLE_NAME, name_len, ble_name);
  214. }
  215. }
  216. else if (!strncmp(tmp_buf, "AT+INTERVAL=", strlen("AT+INTERVAL=")))
  217. {
  218. uint32_t ble_interval = 0;
  219. if('?' == tmp_buf[strlen("AT+INTERVAL=")])
  220. {
  221. DLOG_I("ble_adv_interval:%dms", *(uint32_t* )MOD_SysGetPara(PARA_BLE_ADV_INTERVAL));
  222. }
  223. else
  224. {
  225. ble_interval = atoi(&tmp_buf[strlen("AT+INTERVAL=")]);
  226. DLOG_I("set ble_interval:%d", ble_interval);
  227. MOD_SysSetPara(PARA_BLE_ADV_INTERVAL, 4, &ble_interval);
  228. }
  229. }
  230. }