332 lines
9.5 KiB
C
332 lines
9.5 KiB
C
#include "main.h"
|
|
#include "cmsis_os.h"
|
|
#include "cpy_prot.h"
|
|
#include "Param/param.h"
|
|
#include "Filament/filament.h"
|
|
#include "IAP/iap.h"
|
|
|
|
char gacVersionBuf[32];
|
|
|
|
int giCpyProtThreadStatus= 0;
|
|
|
|
extern UART_HandleTypeDef huart4;
|
|
|
|
static uint16_t gusMaxDataLen = 16 * 1024;
|
|
|
|
|
|
static cpy_prot_packet_t *tx_packet_buf = NULL;
|
|
static cpy_prot_packet_t *rx_packet_buf = NULL;
|
|
static int rx_packet_ready = 0;
|
|
static int tx_packet_busy = 0;
|
|
|
|
//函数声明
|
|
static int cpy_prot_recv_packet(uint16_t data_len);
|
|
|
|
static uint16_t crc16(const uint8_t *data, uint16_t len) {
|
|
uint16_t crc = 0xFFFF;
|
|
for (uint16_t i = 0; i < len; i++) {
|
|
crc ^= data[i];
|
|
for (uint8_t j = 0; j < 8; j++) {
|
|
if (crc & 0x0001) {
|
|
crc >>= 1;
|
|
crc ^= 0xA001;
|
|
} else {
|
|
crc >>= 1;
|
|
}
|
|
}
|
|
}
|
|
return crc;
|
|
}
|
|
|
|
static int check_packet(cpy_prot_packet_t *packet) {
|
|
if (packet->header != CPY_PROT_HEADER) {
|
|
printf("header error, 0x%04X\r\n", packet->header);
|
|
return 0;
|
|
}
|
|
|
|
if (packet->len > gusMaxDataLen) {
|
|
printf("len error, %d > %d\r\n", packet->len, gusMaxDataLen);
|
|
return 0;
|
|
}
|
|
|
|
int packet_len = DATA_LEN_2_PACKET_LEN(packet->len);
|
|
|
|
uint16_t crc = crc16((const uint8_t *)packet, packet_len - CRC_LEN);
|
|
uint16_t *pcrc = (uint16_t *)((uint8_t *)packet + packet_len - CRC_LEN);
|
|
|
|
if (crc != *pcrc) {
|
|
printf("crc error, 0x%04X != 0x%04X\r\n", crc, *pcrc);
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
|
|
{
|
|
if (huart->Instance == huart4.Instance) {
|
|
rx_packet_ready = -1;
|
|
}
|
|
}
|
|
|
|
void HAL_UARTEx_RxEventCallback_UART4(UART_HandleTypeDef *huart, uint16_t Size)
|
|
{
|
|
if ((huart->Instance == huart4.Instance) && (huart->RxEventType == HAL_UART_RXEVENT_IDLE)) {
|
|
//SCB_InvalidateDCache_by_Addr((volatile void *)rx_packet_buf, Size);
|
|
printf("rx packet %d\r\n", Size);
|
|
// for (int i = 0; i < Size; i++) {
|
|
// printf("%02X ", ((uint8_t *)rx_packet_buf)[i]);
|
|
// }
|
|
// printf("\r\n");
|
|
if (check_packet(rx_packet_buf)) {
|
|
rx_packet_ready = 1;
|
|
} else {
|
|
// printf("check packet failed, restart to recv\r\n");
|
|
rx_packet_ready = -1;
|
|
}
|
|
}
|
|
}
|
|
|
|
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
|
|
{
|
|
if (huart->Instance == huart4.Instance) {
|
|
tx_packet_busy = 0;
|
|
}
|
|
}
|
|
|
|
static int start_send(const char *data, size_t len) {
|
|
//SCB_CleanDCache_by_Addr((volatile void *)data, len);
|
|
return HAL_UART_Transmit_DMA(&huart4, (uint8_t *)data, len);
|
|
}
|
|
|
|
static int start_recv(char *data, size_t len) {
|
|
if (huart4.gState != HAL_UART_STATE_READY) {
|
|
HAL_UART_AbortReceive(&huart4);
|
|
}
|
|
|
|
return HAL_UARTEx_ReceiveToIdle_DMA(&huart4, (uint8_t *)data, len);
|
|
}
|
|
|
|
|
|
int cpy_prot_send_packet(uint8_t cmd, const uint8_t *data, uint16_t len) {
|
|
uint16_t packet_len = DATA_LEN_2_PACKET_LEN(len);
|
|
cpy_prot_packet_t *packet = tx_packet_buf;
|
|
packet->header = CPY_PROT_HEADER;
|
|
packet->cmd = cmd;
|
|
packet->len = len;
|
|
if (data) {
|
|
memcpy(packet->data, data, len);
|
|
}
|
|
|
|
uint16_t crc = crc16((const uint8_t *)packet, packet_len - CRC_LEN);
|
|
|
|
uint16_t *pcrc = (uint16_t *)((uint8_t *)packet + packet_len - CRC_LEN);
|
|
*pcrc = crc;
|
|
|
|
int ret = start_send((const char *)packet, packet_len);
|
|
return ret;
|
|
}
|
|
|
|
static int cpy_prot_recv_packet(uint16_t data_len) {
|
|
cpy_prot_packet_t *rx_packet = rx_packet_buf;
|
|
|
|
return start_recv((char *)rx_packet, DATA_LEN_2_PACKET_LEN(data_len));
|
|
}
|
|
|
|
int cpyProtPacketHandle(uint8_t cmd, const uint8_t *data, uint16_t len) {
|
|
// 处理接收到的数据包
|
|
// printf("recv packet: cmd=0x%02X, len=%d\r\n", cmd, len);
|
|
|
|
switch (cmd)
|
|
{
|
|
case CPY_PROT_CMD_HEARTBEAT:
|
|
{
|
|
extern uint16_t gusTempVal;
|
|
uint8_t aucRspData[13];
|
|
*((uint32_t *)&aucRspData[0]) = osKernelGetTickCount();
|
|
*((uint16_t *)&aucRspData[4]) = gusTempVal;
|
|
*((uint16_t *)&aucRspData[6]) = gusFilamentCurrent;
|
|
*((uint16_t *)&aucRspData[8]) = gusFilamentVoltage;
|
|
*((uint16_t *)&aucRspData[10]) = gusXrayCurrent;
|
|
aucRspData[12] = gucWorkState;
|
|
cpy_prot_send_packet(cmd | CPY_PROT_CMD_ACK, aucRspData, sizeof(aucRspData));
|
|
}
|
|
break;
|
|
|
|
case CPY_PROT_CMD_VERSION:
|
|
cpy_prot_send_packet(cmd | CPY_PROT_CMD_ACK, (const uint8_t *)gacVersionBuf, strlen(gacVersionBuf));
|
|
break;
|
|
|
|
case CPY_PROT_CMD_PARAM_READ:
|
|
|
|
cpy_prot_send_packet(cmd | CPY_PROT_CMD_ACK, (const uint8_t *)gptSysParam, sizeof(sysParamDef));
|
|
break;
|
|
|
|
case CPY_PROT_CMD_PARAM_WRITE:
|
|
if (len != sizeof(sysParamDef)) {
|
|
const char *err_msg = "param write len error";
|
|
cpy_prot_send_packet(cmd | CPY_PROT_CMD_ACK, (const uint8_t *)err_msg, strlen(err_msg));
|
|
break;
|
|
}
|
|
memcpy(gptSysParam, data, sizeof(sysParamDef));
|
|
saveSysParam();
|
|
cpy_prot_send_packet(cmd | CPY_PROT_CMD_ACK, (const uint8_t *)gptSysParam, sizeof(sysParamDef));
|
|
break;
|
|
|
|
case CPY_PROT_CMD_IAP:
|
|
iapPacketHandler(data);
|
|
break;
|
|
#ifdef BOOTLOADER
|
|
case CPY_PROT_CMD_BOOT_BLOCKING:
|
|
blockInBootloader(data[0]);
|
|
cpy_prot_send_packet(cmd | CPY_PROT_CMD_ACK, NULL, 0);
|
|
printf("CPY_PROT_CMD_BOOT_BLOCKING\r\n");
|
|
break;
|
|
case CPY_PROT_CMD_JUMP_APP:
|
|
cpy_prot_send_packet(cmd | CPY_PROT_CMD_ACK, NULL, 0);
|
|
osDelay(100);
|
|
blockInBootloader(0);
|
|
break;
|
|
#endif
|
|
case CPY_PROT_CMD_RESET:
|
|
cpy_prot_send_packet(cmd | CPY_PROT_CMD_ACK, NULL, 0);
|
|
osDelay(100);
|
|
systemReboot();
|
|
break;
|
|
case CPY_PROT_CMD_OTP_READ:
|
|
{
|
|
const sysOtpInfoDef *otpInfo = loadOtpInfo();
|
|
cpy_prot_send_packet(cmd | CPY_PROT_CMD_ACK, (const uint8_t *)otpInfo, sizeof(sysOtpInfoDef));
|
|
}
|
|
break;
|
|
case CPY_PROT_CMD_OTP_WRITE:
|
|
{
|
|
if (len != sizeof(sysOtpInfoDef)) {
|
|
const char *err_msg = "otp write len error";
|
|
cpy_prot_send_packet(cmd | CPY_PROT_CMD_ERROR, (const uint8_t *)err_msg, strlen(err_msg));
|
|
break;
|
|
}
|
|
const sysOtpInfoDef *otpInfo = (const sysOtpInfoDef *)data;
|
|
int ret = saveOtpInfo(otpInfo);
|
|
if (ret < 0) {
|
|
char err_msg[64];
|
|
sprintf(err_msg, "otp write failed: %d", ret);
|
|
cpy_prot_send_packet(cmd | CPY_PROT_CMD_ERROR, (const uint8_t *)err_msg, strlen(err_msg));
|
|
break;
|
|
}
|
|
otpInfo = loadOtpInfo();
|
|
cpy_prot_send_packet(cmd | CPY_PROT_CMD_ACK, (const uint8_t *)otpInfo, sizeof(sysOtpInfoDef));
|
|
}
|
|
break;
|
|
case CPY_PROT_CMD_RESET_FACTORY:
|
|
{
|
|
systemResetFactory();
|
|
const char *err_msg = "reset factory ok";
|
|
cpy_prot_send_packet(cmd | CPY_PROT_CMD_ACK, (const uint8_t *)err_msg, strlen(err_msg));
|
|
}
|
|
break;
|
|
case CPY_PROT_CMD_START:
|
|
{
|
|
if (gucWorkState == 0) {
|
|
gucWorkState = 1;
|
|
}
|
|
const char *err_msg = "start ok";
|
|
cpy_prot_send_packet(cmd | CPY_PROT_CMD_ACK, (const uint8_t *)err_msg, strlen(err_msg));
|
|
}
|
|
break;
|
|
case CPY_PROT_CMD_STOP:
|
|
{
|
|
gucWorkState = 0;
|
|
const char *err_msg = "stop ok";
|
|
cpy_prot_send_packet(cmd | CPY_PROT_CMD_ACK, (const uint8_t *)err_msg, strlen(err_msg));
|
|
}
|
|
break;
|
|
default:
|
|
{
|
|
const char *err_msg = "unknown cmd";
|
|
printf("%s\n", err_msg);
|
|
cpy_prot_send_packet(cmd | CPY_PROT_CMD_ACK, (const uint8_t *)err_msg, strlen(err_msg));
|
|
}
|
|
break;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int waitTxComplete(int timeout_ms)
|
|
{
|
|
while (tx_packet_busy)
|
|
{
|
|
osDelay(1);
|
|
timeout_ms--;
|
|
if (timeout_ms <= 0) {
|
|
return -1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static void ioCheck(void)
|
|
{
|
|
|
|
}
|
|
|
|
static void cpyProtThread(void *arg)
|
|
{
|
|
int iCnt = 0;
|
|
cpy_prot_recv_packet(gusMaxDataLen);
|
|
|
|
while (1) {
|
|
iCnt++;
|
|
if (rx_packet_ready == 1) {
|
|
rx_packet_ready = 0;
|
|
// 处理接收的数据包
|
|
cpyProtPacketHandle(rx_packet_buf->cmd, rx_packet_buf->data, rx_packet_buf->len);
|
|
|
|
// int ret = waitTxComplete(100);
|
|
// if (ret < 0) {
|
|
// printf("wait tx complete timeout\n");
|
|
// }
|
|
// tx_packet_busy = 0;
|
|
cpy_prot_recv_packet(gusMaxDataLen);
|
|
} else if (rx_packet_ready == -1) {
|
|
rx_packet_ready = 0;
|
|
cpy_prot_recv_packet(gusMaxDataLen);
|
|
}
|
|
|
|
ioCheck();
|
|
|
|
osDelay(1);
|
|
if (iCnt > 1000) {
|
|
iCnt = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
void cpyProtInit(void)
|
|
{
|
|
sprintf(gacVersionBuf, "%s@%s_%s", SW_VER, __TIME__, __DATE__);
|
|
|
|
tx_packet_buf = pvPortMalloc(DATA_LEN_2_PACKET_LEN(gusMaxDataLen));
|
|
rx_packet_buf = pvPortMalloc(DATA_LEN_2_PACKET_LEN(gusMaxDataLen));
|
|
|
|
if (tx_packet_buf == NULL || rx_packet_buf == NULL) {
|
|
printf("failed to malloc packet buf\n");
|
|
return;
|
|
}
|
|
|
|
// 创建任务
|
|
const osThreadAttr_t cpyProt_task_attributes = {
|
|
.name = "cpyProt_task",
|
|
.stack_size = 4096 * 2,
|
|
.priority = (osPriority_t) osPriorityNormal,
|
|
};
|
|
if (osThreadNew(cpyProtThread, NULL, &cpyProt_task_attributes) == NULL)
|
|
{
|
|
printf("failed to create cpyProt_task\n");
|
|
}
|
|
else
|
|
{
|
|
printf("create cpyProt_task success\n");
|
|
}
|
|
}
|