gateway_mcu/CH32V303-FreeRTOS/App/tools/mcu_common.c

176 lines
3.4 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "mcu_common.h"
#include "uart_dev.h"
/* 储存解析时的数据 */
uint8_t *mcuUartRxBuffer;
uint16_t mcuUartRxBufferIndex = 0;
uint8_t *J0_485RxBuffer;
uint16_t J0_485RxBufferIndex = 0;
uint8_t *J1_485RxBuffer;
uint16_t J1_485RxBufferIndex = 0;
uint8_t *J2_485RxBuffer;
uint16_t J2_485RxBufferIndex = 0;
uint8_t *J3_485RxBuffer;
uint16_t J3_485RxBufferIndex = 0;
uint8_t *J4_485RxBuffer;
uint16_t J4_485RxBufferIndex = 0;
uint8_t *J5_485RxBuffer;
uint16_t J5_485RxBufferIndex = 0;
uint8_t *J6_485RxBuffer;
uint16_t J6_485RxBufferIndex = 0;
uint8_t *J7_485RxBuffer;
uint16_t J7_485RxBufferIndex = 0;
uint8_t *J8_485RxBuffer;
uint16_t J8_485RxBufferIndex = 0;
uint8_t *J9_485RxBuffer;
uint16_t J9_485RxBufferIndex = 0;
uint8_t *su806RxBuffer;
uint16_t su806RxBufferIndex = 0;
uint8_t *loraRxBuffer;
uint16_t loraRxBufferIndex = 0;
/* 计时参数2秒后没解析出一帧数据丢掉当前数据 */
uint32_t mcuUartRxTime = 0;
uint32_t su806UartRxTime = 0;
/* 状态机状态机变量 */
uartStateMachine state = wait;
/* 帧长度 */
uint16_t frameLength = 0;
#define hostMcuUartBufferLen 280
#define slaveMcuUartBufferLen 1100
#define su806BufferLen 1100
#define rs485BufferLen 280
#define loraBufferLen 280
uint8_t applyDataBuffer(void)
{
/* 从mcu */
if (getMCU()) {
mcuUartRxBuffer = pvPortMalloc(slaveMcuUartBufferLen);
J1_485RxBuffer = pvPortMalloc(rs485BufferLen);
J3_485RxBuffer = pvPortMalloc(rs485BufferLen);
J5_485RxBuffer = pvPortMalloc(rs485BufferLen);
J7_485RxBuffer = pvPortMalloc(rs485BufferLen);
J8_485RxBuffer = pvPortMalloc(rs485BufferLen);
J9_485RxBuffer = pvPortMalloc(rs485BufferLen);
}
/* 主mcu */
else {
mcuUartRxBuffer = pvPortMalloc(hostMcuUartBufferLen);
su806RxBuffer = pvPortMalloc(su806BufferLen);
J0_485RxBuffer = pvPortMalloc(rs485BufferLen);
J2_485RxBuffer = pvPortMalloc(rs485BufferLen);
J4_485RxBuffer = pvPortMalloc(rs485BufferLen);
J6_485RxBuffer = pvPortMalloc(rs485BufferLen);
loraRxBuffer = pvPortMalloc(loraBufferLen);
}
return 0;
}
/**
* @brief 得到两次获取节拍的差值
* @param lastTick 上次获取的节拍值
* @retval
*/
uint32_t getTickDiff(uint32_t lastTick)
{
int64_t temp;
temp = xTaskGetTickCount() - lastTick;
/* 节拍值超过最大值后重新计数 */
if (temp < 0) {
temp = portMAX_DELAY - lastTick + xTaskGetTickCount();
}
return temp;
}
/**
* @brief 用于波特率转化为传递的值
* @param
* @retval
*/
int baudConversionConfig(int baud)
{
if (baud == 4800) {
return 0x0001;
}
else if (baud == 9600) {
return 0x0002;
}
else if (baud == 19200) {
return 0x0003;
}
else if (baud == 38400) {
return 0x0004;
}
else if (baud == 57600) {
return 0x0005;
}
else if (baud == 115200) {
return 0x0006;
}
return 0x0002;
}
/**
* @brief 用于传递的值转化为波特率
* @param
* @retval
*/
int configConversionBaud(int config)
{
if (config == 0x0001) {
return 4800;
}
else if (config == 0x0002) {
return 9600;
}
else if (config == 0x0003) {
return 19200;
}
else if (config == 0x0004) {
return 38400;
}
else if (config == 0x0005) {
return 57600;
}
else if (config == 0x0006) {
return 115200;
}
return 9600;
}
/**
* @brief 用于modebus16crc校验
* @param
* @retval
*/
uint16_t modebusCrc(uint8_t *arr_buff, uint8_t len)
{
unsigned short crc = 0xFFFF;
unsigned char i, j;
for ( j = 0; j < len; j++){
crc=crc ^ *arr_buff++;
for (i=0; i<8; i++){
if((crc&0x0001) >0){
crc=crc>>1;
crc=crc^ 0xa001;
}else{
crc=crc>>1;
}
}
}
return crc;
}