mod_atcmd.c 7.0 KB

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