| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- #include <stdint.h>
- #include "string.h"
- #include "stdbool.h"
- #include "stdlib.h"
- #include "sys_log.h"
- #include "sys_ver.h"
- #include "sys_common.h"
- #include "mod_atcmd.h"
- #include "mod_sys.h"
- #include "hal_can.h"
- #include "hal_spi.h"
- #include "hal_key.h"
- #include "hal_adc.h"
- #include "hal_ble.h"
- #include "hal_flash.h"
- #include "mod_spi.h"
- #define RX_CMD_MAX_LEN 32
- uint8_t g_s_at_buf[RX_CMD_MAX_LEN];
- static void Sys_Soft_Reset(void)
- {
- SCB->AIRCR = 0X05FA0000 | (u32)0x04;
- }
- void MOD_AtCmdDataProcess(void *data, uint16_t length)
- {
- const char *tmp_buf = NULL;
- memset(g_s_at_buf, 0, RX_CMD_MAX_LEN);
- memcpy(g_s_at_buf, data, length);
- tmp_buf = (char *)g_s_at_buf;
- if (!strncmp(tmp_buf, "AT+CANTX", strlen("AT+CANTX")))
- {
- uint32_t can_id = 0x6F1;
- uint8_t send_buf[8] = {0x60, 0x03, 0x22, 0xF1, 0x8B, 0xFF, 0xFF, 0xFF};
- DLOG_I("send data to can");
- HAL_CanTxData(0, can_id, send_buf, 8);
- }
- else if (!strncmp(tmp_buf, "AT+REBOOT", strlen("AT+REBOOT")))
- {
- Sys_Soft_Reset();
- }
- else if (!strncmp(tmp_buf, "AT+SPITX", strlen("AT+SPITX")))
- {
- uint8_t send_buf[32];
- int i;
- for (i = 0; i < 32; i++)
- {
- send_buf[i] = 0xF1 + i;
- }
- DLOG_I("spi send data");
- HAL_SpiTxData(send_buf, 32);
- }
- else if (!strncmp(tmp_buf, "AT+MCUVER", strlen("AT+MCUVER")))
- {
- int ver = get_ver();
- DLOG_I("soft_ver:0x%x", ver);
- }
- else if (!strncmp(tmp_buf, "AT+GETBLEVER", strlen("AT+GETBLEVER")))
- {
- MOD_SpiDataSend(BLE_CMD_TEST, NULL, 0);
- }
- else if (!strncmp(tmp_buf, "AT+KEYSTA", strlen("AT+KEYSTA")))
- {
- uint8_t status = HAL_KeyGetStatus();
- DLOG_I("key status:%d", status);
- }
- else if (!strncmp(tmp_buf, "AT+ADCVAL", strlen("AT+ADCVAL")))
- {
- int value = HAL_AdcGetVal();
- int mv = value * (3.3 / 4096) * 1000;
- int battery_mv = mv * 6;
- // 电池电压需要乘以6
- DLOG_I("adv_val:%d,battery_mv:%dmv", value, battery_mv);
- }
- else if (!strncmp(tmp_buf, "AT+SPITRANS", strlen("AT+SPITRANS")))
- {
- uint8_t buffer[] = {0x52, 0x12, 0x00, 0x00, 0x00, 0x06, 0xF1, 0x01, 0x11};
- MOD_SpiDataSend(BLE_CMD_TRANSFER, buffer, 9);
- }
- else if (!strncmp(tmp_buf, "AT+BLEON", strlen("AT+BLEON")))
- {
- HAL_SpiGpioInit();
- HAL_BleGpioEn(true);
- }
- else if (!strncmp(tmp_buf, "AT+BLEOFF", strlen("AT+BLEOFF")))
- {
- HAL_BleGpioEn(false);
- HAL_SpiClockPinReset();
- }
- else if (!strncmp(tmp_buf, "AT+BLEREBOOT", strlen("AT+BLEREBOOT")))
- {
- DLOG_I("reboot ble");
- HAL_BleReboot();
- }
- else if (!strncmp(tmp_buf, "AT+CHECKSECTOR=", strlen("AT+CHECKSECTOR=")))
- {
- uint8_t erase_index = atoi(&tmp_buf[strlen("AT+CHECKSECTOR=")]);
- DLOG_I("erase_index:%d", erase_index);
- if (1 == erase_index)
- {
- MOD_SysCheckSector(FLASH_MAIN_PARA_ADDR);
- }
- else if (2 == erase_index)
- {
- MOD_SysCheckSector(FLASH_BACKUP_PARA_ADDR);
- }
- }
- else if (!strncmp(tmp_buf, "AT+FERASE=", strlen("AT+FERASE=")))
- {
- uint8_t erase_index = atoi(&tmp_buf[strlen("AT+FERASE=")]);
- DLOG_I("erase_index:%d", erase_index);
- if (1 == erase_index)
- {
- HAL_FlashSectorErase(FLASH_MAIN_PARA_ADDR);
- }
- else if (2 == erase_index)
- {
- HAL_FlashSectorErase(FLASH_BACKUP_PARA_ADDR);
- }
- }
- else if (!strncmp(tmp_buf, "AT+FWRITE=", strlen("AT+FWRITE=")))
- {
- uint8_t write_buf[64];
- int i;
- uint8_t write_ret;
- uint8_t write_index = atoi(&tmp_buf[strlen("AT+FWRITE=")]);
- DLOG_I("write_index:%d", write_index);
- for (i = 0; i < 64; i++)
- {
- write_buf[i] = i;
- }
- if (1 == write_index)
- {
- write_ret = HAL_FlashWriteBytes(FLASH_MAIN_PARA_ADDR, write_buf, 64);
- }
- else if (2 == write_index)
- {
- write_ret = HAL_FlashWriteBytes(FLASH_BACKUP_PARA_ADDR, write_buf, 64);
- }
- DLOG_I("write flash ret:%d", write_ret);
- }
- else if (!strncmp(tmp_buf, "AT+FREAD=", strlen("AT+FREAD=")))
- {
- uint8_t read_buf[64];
- uint8_t read_index;
- DLOG_I("read_index:%d", read_index);
- memset(read_buf, 0, 64);
- read_index = atoi(&tmp_buf[strlen("AT+FREAD=")]);
- if (1 == read_index)
- {
- HAL_FlashReadBytes(FLASH_MAIN_PARA_ADDR, read_buf, 64);
- DLOG_I("read_tmp_buf:%s", SYS_Hex2str(read_buf, 64));
- }
- else if (2 == read_index)
- {
- HAL_FlashReadBytes(FLASH_BACKUP_PARA_ADDR, read_buf, 64);
- DLOG_I("read_tmp_buf:%s", SYS_Hex2str(read_buf, 64));
- }
- }
- else if (!strncmp(tmp_buf, "AT+SHOWPARA", strlen("AT+SHOWPARA")))
- {
- DLOG_I("show para");
- MOD_SysShowPara();
- }
- else if (!strncmp(tmp_buf, "AT+MAC=", strlen("AT+MAC=")))
- {
- uint8_t ble_mac[6];
- memset(ble_mac, 0, 6);
- if('?' == tmp_buf[strlen("AT+MAC=")])
- {
- memcpy(ble_mac, (uint8_t* )MOD_SysGetPara(PARA_BLE_MAC), 6);
- 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]);
- }
- else
- {
- if(length - 7 != 12)
- {
- DLOG_I("input mac len:%d error", length - strlen("AT+MAC="));
- }
- MOD_SysSetPara(PARA_BLE_MAC, 6, SYS_Str2hex((uint8_t*)&tmp_buf[7], 12));
- }
- }
- else if (!strncmp(tmp_buf, "AT+NAME=", strlen("AT+NAME=")))
- {
- uint8_t ble_name[8] = {0};
- uint8_t name_len = 0;
- uint8_t* p_name = NULL;
- if('?' == tmp_buf[strlen("AT+NAME=")])
- {
- p_name = (uint8_t* )MOD_SysGetPara(PARA_BLE_NAME);
- DLOG_I("ble_name:%s,len:%d", p_name, strlen(p_name));
- }
- else
- {
- name_len = length - strlen("AT+NAME=");
- memcpy(ble_name, (uint8_t* )&tmp_buf[strlen("AT+NAME=")], name_len);
- ble_name[name_len] = '\0';
- DLOG_I("set ble_name:%s", ble_name);
- MOD_SysSetPara(PARA_BLE_NAME, name_len, ble_name);
- }
- }
- else if (!strncmp(tmp_buf, "AT+INTERVAL=", strlen("AT+INTERVAL=")))
- {
- uint32_t ble_interval = 0;
- if('?' == tmp_buf[strlen("AT+INTERVAL=")])
- {
- DLOG_I("ble_adv_interval:%dms", *(uint32_t* )MOD_SysGetPara(PARA_BLE_ADV_INTERVAL));
- }
- else
- {
- ble_interval = atoi(&tmp_buf[strlen("AT+INTERVAL=")]);
- DLOG_I("set ble_interval:%d", ble_interval);
- MOD_SysSetPara(PARA_BLE_ADV_INTERVAL, 4, &ble_interval);
- }
- }
- }
|