mod_atcmd.c 6.7 KB

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