初始提交
This commit is contained in:
@@ -0,0 +1,417 @@
|
||||
#include "main.h"
|
||||
#include "cmsis_os.h"
|
||||
#include "cpy_prot.h"
|
||||
#include "Param/param.h"
|
||||
#include "ADS8321/ads8321.h"
|
||||
#include "IAP/iap.h"
|
||||
|
||||
char gacVersionBuf[32];
|
||||
|
||||
int giCpyProtThreadStatus= 0;
|
||||
|
||||
extern UART_HandleTypeDef huart4;
|
||||
|
||||
static uint16_t gusMaxDataLen = 16 * 1024;
|
||||
|
||||
|
||||
static daq_packet_t *tx_packet_buf = NULL;
|
||||
static daq_packet_t *rx_packet_buf = NULL;
|
||||
static int rx_packet_ready = 0;
|
||||
static int tx_packet_busy = 0;
|
||||
|
||||
//函数声明
|
||||
static int daq_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(daq_packet_t *packet) {
|
||||
if (packet->header != DAQ_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(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 daq_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 daq_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 daq_send_packet(uint8_t cmd, const uint8_t *data, uint16_t len) {
|
||||
uint16_t packet_len = DATA_LEN_2_PACKET_LEN(len);
|
||||
daq_packet_t *packet = tx_packet_buf;
|
||||
packet->header = DAQ_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 = daq_send((const char *)packet, packet_len);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static int daq_recv_packet(uint16_t data_len) {
|
||||
daq_packet_t *rx_packet = rx_packet_buf;
|
||||
|
||||
return daq_recv((char *)rx_packet, DATA_LEN_2_PACKET_LEN(data_len));
|
||||
}
|
||||
|
||||
|
||||
static int makeDataPacket(uint8_t *data)
|
||||
{
|
||||
uint16_t ulPos = 0;
|
||||
uint16_t i = 0;
|
||||
uint16_t ulLen = adcDataQueueLen();
|
||||
|
||||
if (ulLen < 5)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
adcDataUnitDef *ptRetData = (adcDataUnitDef *)(data + 4);
|
||||
|
||||
for (i = 0; i < ulLen; ++i)
|
||||
{
|
||||
ulPos = gtAdcDataQueue.ulReadPos + i;
|
||||
if (ulPos >= ADC_QUEUE_LEN)
|
||||
{
|
||||
ulPos -= ADC_QUEUE_LEN;
|
||||
}
|
||||
volatile adcDataUnitDef *ptData = >AdcDataQueue.atQueue[ulPos];
|
||||
memcpy(&ptRetData[i], (uint8_t *)ptData, sizeof(adcDataUnitDef));
|
||||
}
|
||||
|
||||
uint16_t usDataLen = ulLen * sizeof(adcDataUnitDef);
|
||||
|
||||
*((uint16_t *)(data + 0)) = usDataLen; ///< 长度
|
||||
*((uint16_t *)(data + 2)) = crc16(data + 4, usDataLen); ///< 校验
|
||||
|
||||
return usDataLen + 4;
|
||||
}
|
||||
|
||||
|
||||
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[10];
|
||||
*((uint32_t *)&aucRspData[0]) = osKernelGetTickCount();
|
||||
*((uint16_t *)&aucRspData[4]) = gusTempVal;
|
||||
daq_send_packet(cmd | CPY_PROT_CMD_ACK, aucRspData, sizeof(aucRspData));
|
||||
}
|
||||
break;
|
||||
|
||||
case CPY_PROT_CMD_VERSION:
|
||||
daq_send_packet(cmd | CPY_PROT_CMD_ACK, (const uint8_t *)gacVersionBuf, strlen(gacVersionBuf));
|
||||
break;
|
||||
|
||||
case CPY_PROT_CMD_PARAM_READ:
|
||||
|
||||
daq_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";
|
||||
daq_send_packet(cmd | CPY_PROT_CMD_ACK, (const uint8_t *)err_msg, strlen(err_msg));
|
||||
break;
|
||||
}
|
||||
memcpy(gptSysParam, data, sizeof(sysParamDef));
|
||||
saveSysParam();
|
||||
daq_send_packet(cmd | CPY_PROT_CMD_ACK, (const uint8_t *)gptSysParam, sizeof(sysParamDef));
|
||||
break;
|
||||
|
||||
case CPY_PROT_CMD_SAMPLE_LEN:
|
||||
{
|
||||
uint16_t usLen = adcDataQueueLen();
|
||||
|
||||
daq_send_packet(cmd | CPY_PROT_CMD_ACK, (const uint8_t *)&usLen, sizeof(uint16_t));
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case CPY_PROT_CMD_SAMPLE_DATA:
|
||||
{
|
||||
uint16_t usLen = adcDataQueueLen();
|
||||
if (usLen == 0) {
|
||||
const char *err_msg = "no sample data";
|
||||
daq_send_packet(cmd | CPY_PROT_CMD_ACK, (const uint8_t *)err_msg, strlen(err_msg));
|
||||
break;
|
||||
}
|
||||
|
||||
static uint8_t data[PACKET_DATA_UNIT_NUM * sizeof(adcDataUnitDef)];
|
||||
usLen = makeDataPacket(data);
|
||||
if (usLen == 0) {
|
||||
const char *err_msg = "make data packet failed";
|
||||
daq_send_packet(cmd | CPY_PROT_CMD_ACK, (const uint8_t *)err_msg, strlen(err_msg));
|
||||
break;
|
||||
}
|
||||
daq_send_packet(cmd | CPY_PROT_CMD_ACK, (const uint8_t *)data, usLen);
|
||||
}
|
||||
break;
|
||||
case CPY_PROT_CMD_IAP:
|
||||
iapPacketHandler(data);
|
||||
break;
|
||||
#ifdef BOOTLOADER
|
||||
case CPY_PROT_CMD_BOOT_BLOCKING:
|
||||
blockInBootloader(data[0]);
|
||||
daq_send_packet(cmd | CPY_PROT_CMD_ACK, NULL, 0);
|
||||
printf("CPY_PROT_CMD_BOOT_BLOCKING\r\n");
|
||||
break;
|
||||
case CPY_PROT_CMD_JUMP_APP:
|
||||
daq_send_packet(cmd | CPY_PROT_CMD_ACK, NULL, 0);
|
||||
osDelay(100);
|
||||
blockInBootloader(0);
|
||||
break;
|
||||
#endif
|
||||
case CPY_PROT_CMD_RESET:
|
||||
daq_send_packet(cmd | CPY_PROT_CMD_ACK, NULL, 0);
|
||||
osDelay(100);
|
||||
systemReboot();
|
||||
break;
|
||||
case CPY_PROT_CMD_OTP_READ:
|
||||
{
|
||||
const sysOtpInfoDef *otpInfo = loadOtpInfo();
|
||||
daq_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";
|
||||
daq_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);
|
||||
daq_send_packet(cmd | CPY_PROT_CMD_ERROR, (const uint8_t *)err_msg, strlen(err_msg));
|
||||
break;
|
||||
}
|
||||
otpInfo = loadOtpInfo();
|
||||
daq_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";
|
||||
daq_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);
|
||||
daq_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 int ioRelay = 0;
|
||||
static int ioSample = 0;
|
||||
|
||||
if (DI_REALY_ASSERTED()) {
|
||||
if (ioRelay < 0) {
|
||||
ioRelay = 0;
|
||||
}
|
||||
ioRelay++;
|
||||
if (ioRelay >= 3) {
|
||||
ioRelay = 3;
|
||||
DO_RELAY_ASSERT();
|
||||
}
|
||||
} else {
|
||||
if (ioRelay > 0) {
|
||||
ioRelay = 0;
|
||||
}
|
||||
ioRelay--;
|
||||
if (ioRelay <= -3) {
|
||||
ioRelay = -3;
|
||||
DO_RELAY_ASSERT();
|
||||
}
|
||||
}
|
||||
|
||||
if (DI_SIMPLE_ASSERTED()) {
|
||||
if (ioSample < 0) {
|
||||
ioSample = 0;
|
||||
}
|
||||
ioSample++;
|
||||
if (ioSample >= 3) {
|
||||
ioSample = 3;
|
||||
adcSampleStart();
|
||||
}
|
||||
} else {
|
||||
if (ioSample > 0) {
|
||||
ioSample = 0;
|
||||
}
|
||||
ioSample--;
|
||||
if (ioSample <= -3) {
|
||||
ioSample = -3;
|
||||
adcSampleStop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void cpyProtThread(void *arg)
|
||||
{
|
||||
int iCnt = 0;
|
||||
daq_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;
|
||||
daq_recv_packet(gusMaxDataLen);
|
||||
} else if (rx_packet_ready == -1) {
|
||||
rx_packet_ready = 0;
|
||||
daq_recv_packet(gusMaxDataLen);
|
||||
}
|
||||
|
||||
ioCheck();
|
||||
|
||||
osDelay(1);
|
||||
if (iCnt > 1000) {
|
||||
iCnt = 0;
|
||||
// daq_send("hello", 5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user