mod_atcmd.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. MOD_CanIsoParseTest();
  39. }
  40. else if (!strncmp(tmp_buf, "AT+REBOOT", strlen("AT+REBOOT")))
  41. {
  42. Sys_Soft_Reset();
  43. }
  44. else if (!strncmp(tmp_buf, "AT+SPITX", strlen("AT+SPITX")))
  45. {
  46. uint8_t send_buf[32];
  47. int i;
  48. for (i = 0; i < 32; i++)
  49. {
  50. send_buf[i] = 0xF1 + i;
  51. }
  52. DLOG_I("spi send data");
  53. HAL_SpiTxData(send_buf, 32);
  54. }
  55. else if (!strncmp(tmp_buf, "AT+MCUVER", strlen("AT+MCUVER")))
  56. {
  57. int ver = get_ver();
  58. DLOG_I("soft_ver:0x%x", ver);
  59. }
  60. else if (!strncmp(tmp_buf, "AT+GETBLEVER", strlen("AT+GETBLEVER")))
  61. {
  62. MOD_SpiDataSend(BLE_CMD_TEST, NULL, 0);
  63. }
  64. else if (!strncmp(tmp_buf, "AT+KEYSTA", strlen("AT+KEYSTA")))
  65. {
  66. uint8_t status = HAL_KeyGetStatus();
  67. DLOG_I("key status:%d", status);
  68. }
  69. else if (!strncmp(tmp_buf, "AT+ADCVAL", strlen("AT+ADCVAL")))
  70. {
  71. int value = HAL_AdcGetVal();
  72. int mv = value * (3.3 / 4096) * 1000;
  73. int battery_mv = mv * 6;
  74. // 电池电压需要乘以6
  75. DLOG_I("adv_val:%d,battery_mv:%dmv", value, battery_mv);
  76. }
  77. else if (!strncmp(tmp_buf, "AT+SPITRANS", strlen("AT+SPITRANS")))
  78. {
  79. uint8_t buffer[] = {0x52, 0x12, 0x00, 0x00, 0x00, 0x06, 0xF1, 0x01, 0x11};
  80. MOD_SpiDataSend(BLE_CMD_TRANSFER, buffer, 9);
  81. }
  82. else if (!strncmp(tmp_buf, "AT+BLEON", strlen("AT+BLEON")))
  83. {
  84. HAL_SpiGpioInit();
  85. HAL_BleGpioEn(true);
  86. }
  87. else if (!strncmp(tmp_buf, "AT+BLEOFF", strlen("AT+BLEOFF")))
  88. {
  89. HAL_BleGpioEn(false);
  90. HAL_SpiClockPinReset();
  91. }
  92. else if (!strncmp(tmp_buf, "AT+BLEREBOOT", strlen("AT+BLEREBOOT")))
  93. {
  94. DLOG_I("reboot ble");
  95. HAL_BleReboot();
  96. }
  97. else if (!strncmp(tmp_buf, "AT+CHECKSECTOR=", strlen("AT+CHECKSECTOR=")))
  98. {
  99. uint8_t erase_index = atoi(&tmp_buf[strlen("AT+CHECKSECTOR=")]);
  100. DLOG_I("erase_index:%d", erase_index);
  101. if (1 == erase_index)
  102. {
  103. MOD_SysCheckSector(FLASH_MAIN_PARA_ADDR);
  104. }
  105. else if (2 == erase_index)
  106. {
  107. MOD_SysCheckSector(FLASH_BACKUP_PARA_ADDR);
  108. }
  109. }
  110. else if (!strncmp(tmp_buf, "AT+FERASE=", strlen("AT+FERASE=")))
  111. {
  112. uint8_t erase_index = atoi(&tmp_buf[strlen("AT+FERASE=")]);
  113. DLOG_I("erase_index:%d", erase_index);
  114. if (1 == erase_index)
  115. {
  116. HAL_FlashSectorErase(FLASH_MAIN_PARA_ADDR);
  117. }
  118. else if (2 == erase_index)
  119. {
  120. HAL_FlashSectorErase(FLASH_BACKUP_PARA_ADDR);
  121. }
  122. }
  123. else if (!strncmp(tmp_buf, "AT+FWRITE=", strlen("AT+FWRITE=")))
  124. {
  125. uint8_t write_buf[64];
  126. int i;
  127. uint8_t write_ret;
  128. uint8_t write_index = atoi(&tmp_buf[strlen("AT+FWRITE=")]);
  129. DLOG_I("write_index:%d", write_index);
  130. for (i = 0; i < 64; i++)
  131. {
  132. write_buf[i] = i;
  133. }
  134. if (1 == write_index)
  135. {
  136. write_ret = HAL_FlashWriteBytes(FLASH_MAIN_PARA_ADDR, write_buf, 64);
  137. }
  138. else if (2 == write_index)
  139. {
  140. write_ret = HAL_FlashWriteBytes(FLASH_BACKUP_PARA_ADDR, write_buf, 64);
  141. }
  142. DLOG_I("write flash ret:%d", write_ret);
  143. }
  144. else if (!strncmp(tmp_buf, "AT+FREAD=", strlen("AT+FREAD=")))
  145. {
  146. uint8_t read_buf[64];
  147. uint8_t read_index;
  148. DLOG_I("read_index:%d", read_index);
  149. memset(read_buf, 0, 64);
  150. read_index = atoi(&tmp_buf[strlen("AT+FREAD=")]);
  151. if (1 == read_index)
  152. {
  153. HAL_FlashReadBytes(FLASH_MAIN_PARA_ADDR, read_buf, 64);
  154. DLOG_I("read_tmp_buf:%s", SYS_Hex2str(read_buf, 64));
  155. }
  156. else if (2 == read_index)
  157. {
  158. HAL_FlashReadBytes(FLASH_BACKUP_PARA_ADDR, read_buf, 64);
  159. DLOG_I("read_tmp_buf:%s", SYS_Hex2str(read_buf, 64));
  160. }
  161. }
  162. else if (!strncmp(tmp_buf, "AT+SHOWPARA", strlen("AT+SHOWPARA")))
  163. {
  164. DLOG_I("show para");
  165. MOD_SysShowPara();
  166. }
  167. else if (!strncmp(tmp_buf, "AT+MAC=", strlen("AT+MAC=")))
  168. {
  169. uint8_t ble_mac[6];
  170. memset(ble_mac, 0, 6);
  171. if('?' == tmp_buf[strlen("AT+MAC=")])
  172. {
  173. memcpy(ble_mac, (uint8_t* )MOD_SysGetPara(PARA_BLE_MAC), 6);
  174. 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]);
  175. }
  176. else
  177. {
  178. if(length - 7 != 12)
  179. {
  180. DLOG_I("input mac len:%d error", length - strlen("AT+MAC="));
  181. }
  182. MOD_SysSetPara(PARA_BLE_MAC, 6, SYS_Str2hex((uint8_t*)&tmp_buf[7], 12));
  183. }
  184. }
  185. else if (!strncmp(tmp_buf, "AT+NAME=", strlen("AT+NAME=")))
  186. {
  187. uint8_t ble_name[8] = {0};
  188. uint8_t name_len = 0;
  189. uint8_t* p_name = NULL;
  190. if('?' == tmp_buf[strlen("AT+NAME=")])
  191. {
  192. p_name = (uint8_t* )MOD_SysGetPara(PARA_BLE_NAME);
  193. DLOG_I("ble_name:%s,len:%d", p_name, strlen(p_name));
  194. }
  195. else
  196. {
  197. name_len = length - strlen("AT+NAME=");
  198. memcpy(ble_name, (uint8_t* )&tmp_buf[strlen("AT+NAME=")], name_len);
  199. ble_name[name_len] = '\0';
  200. DLOG_I("set ble_name:%s", ble_name);
  201. MOD_SysSetPara(PARA_BLE_NAME, name_len, ble_name);
  202. }
  203. }
  204. else if (!strncmp(tmp_buf, "AT+INTERVAL=", strlen("AT+INTERVAL=")))
  205. {
  206. uint32_t ble_interval = 0;
  207. if('?' == tmp_buf[strlen("AT+INTERVAL=")])
  208. {
  209. DLOG_I("ble_adv_interval:%dms", *(uint32_t* )MOD_SysGetPara(PARA_BLE_ADV_INTERVAL));
  210. }
  211. else
  212. {
  213. ble_interval = atoi(&tmp_buf[strlen("AT+INTERVAL=")]);
  214. DLOG_I("set ble_interval:%d", ble_interval);
  215. MOD_SysSetPara(PARA_BLE_ADV_INTERVAL, 4, &ble_interval);
  216. }
  217. }
  218. }