gateway_mcu/CH32V303-FreeRTOS/App/application/Slave/Src/slaveMcuComm.c

321 lines
7.8 KiB
C
Raw Permalink Normal View History

#include "slaveMcuComm.h"
#include "slaveQueueUart.h"
#include "slaveParameter.h"
#include "uart_dev.h"
#include "FreeRTOS.h"
#include "task.h"
#include "mcu_common.h"
/* <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͱ<EFBFBD> */
enum {
/* (1<>ֽ<EFBFBD>)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3>ͨ<EFBFBD>Ų<EFBFBD><C5B2><EFBFBD><EFBFBD><EFBFBD>(00H<30><48><EFBFBD><EFBFBD>ʹ<EFBFBD>øô<C3B8><C3B4><EFBFBD>, 01H<31><48>4800;02H<32><48>9600;03H<33><48>19200;04H<34><48>38400;05H<35><48>57600;06H<36><48>115200) */
J1_485_Communication_Baud_Rate = 0x0001,
J3_485_Communication_Baud_Rate = 0x0002,
J5_485_Communication_Baud_Rate = 0x0003,
J7_485_Communication_Baud_Rate = 0x0004,
J8_485_Communication_Baud_Rate = 0x0005,
J9_485_Communication_Baud_Rate = 0x0006,
};
/* <20>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
static uint16_t ReadRegisterJ1Baud(void *pMsg);
static uint16_t ReadRegisterJ3Baud(void *pMsg);
static uint16_t ReadRegisterJ5Baud(void *pMsg);
static uint16_t ReadRegisterJ7Baud(void *pMsg);
static uint16_t ReadRegisterJ8Baud(void *pMsg);
static uint16_t ReadRegisterJ9Baud(void *pMsg);
static uint16_t WriteRegisterJ1Baud(void *pMsg);
static uint16_t WriteRegisterJ3Baud(void *pMsg);
static uint16_t WriteRegisterJ5Baud(void *pMsg);
static uint16_t WriteRegisterJ7Baud(void *pMsg);
static uint16_t WriteRegisterJ8Baud(void *pMsg);
static uint16_t WriteRegisterJ9Baud(void *pMsg);
/* <20>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
typedef uint16_t (*RegProcFunc)(void*);
typedef struct _SL_RegProcTable{
uint32_t regId;
RegProcFunc pRegProc;
}SL_RegProcTable;
/* <20><>ȡ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
SL_RegProcTable g_RegTblR[] =
{
{J1_485_Communication_Baud_Rate, ReadRegisterJ1Baud},
{J3_485_Communication_Baud_Rate, ReadRegisterJ3Baud},
{J5_485_Communication_Baud_Rate, ReadRegisterJ5Baud},
{J7_485_Communication_Baud_Rate, ReadRegisterJ7Baud},
{J8_485_Communication_Baud_Rate, ReadRegisterJ8Baud},
{J9_485_Communication_Baud_Rate, ReadRegisterJ9Baud},
};
/* д<><D0B4><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
SL_RegProcTable g_RegTblW[] =
{
{J1_485_Communication_Baud_Rate, WriteRegisterJ1Baud},
{J3_485_Communication_Baud_Rate, WriteRegisterJ3Baud},
{J5_485_Communication_Baud_Rate, WriteRegisterJ5Baud},
{J7_485_Communication_Baud_Rate, WriteRegisterJ7Baud},
{J8_485_Communication_Baud_Rate, WriteRegisterJ8Baud},
{J9_485_Communication_Baud_Rate, WriteRegisterJ9Baud},
};
static void SlaveMcuReadRegister(uint8_t *buff, uint8_t dataLen)
{
uint16_t Register_Start_Address = (buff[6] << 8) | buff[7];
uint16_t Register_Number = (buff[8] << 8) | buff[9];
/* <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD> */
//ϵͳ<CFB5>ڴ治<DAB4><EFBFBD><E3A3AC>ȥ<EFBFBD><C8A5>ǰ<EFBFBD><C7B0>
uint32_t tempLen = 13 + 2 * Register_Number + slaveQueueUartSendInfoSize;
if (xPortGetFreeHeapSize() < tempLen + 1024) {
return;
}
uint8_t *Buff = (uint8_t *)pvPortMalloc(tempLen);
slaveQueueUartSendInfo *sendBuff = (slaveQueueUartSendInfo *)Buff;
sendBuff->length = 13 + 2 * Register_Number;
sendBuff->data = Buff + slaveQueueUartSendInfoSize;
uint8_t *replay_pack = sendBuff->data;
/* <20><>ʼ<EFBFBD><CABC>־ */
*(replay_pack) = 'S';
*(replay_pack + 1) = 'L';
/* <20>˿ں<CBBF> */
replay_pack += 2;
*(replay_pack) = SlaveMcu;
/* <20>ظ<EFBFBD><D8B8>ֽڳ<D6BD><DAB3><EFBFBD> */
replay_pack += 1;
*(replay_pack) = (uint8_t)((sendBuff->length - 6) >> 8);
*(replay_pack + 1) = (uint8_t)(sendBuff->length - 6);
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
replay_pack += 2;
*(replay_pack) = SL_Function_Code_Read_Register;
/* <20><>ȡ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>ַ */
replay_pack += 1;
*(replay_pack) = buff[6];
*(replay_pack + 1) = buff[7];
/* <20>ظ<EFBFBD><D8B8>ֽڳ<D6BD><DAB3><EFBFBD> */
replay_pack += 2;
*(replay_pack) = (uint8_t)((sendBuff->length - 13) >> 8);
*(replay_pack + 1) = (uint8_t)(sendBuff->length - 13);
/* <20>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD> */
for ( uint16_t pos = 0; pos < Register_Number; pos++) {
for (uint16_t var = 0; var < sizeof(g_RegTblR) / sizeof(SL_RegProcTable); var++) {
if (g_RegTblR[var].regId == (Register_Start_Address + pos)) {
uint16_t temp = g_RegTblR[var].pRegProc(NULL);
replay_pack += 2;
*(replay_pack) = (uint8_t)(temp >> 8);
*(replay_pack + 1) = (uint8_t)(temp);
}
}
}
/* У<><D0A3>λ */
replay_pack += 2;
uint16_t crc_temp = modebusCrc(sendBuff->data, sendBuff->length - 3);
*replay_pack = (uint8_t)(crc_temp >> 8);
replay_pack += 1;
*replay_pack = (uint8_t)crc_temp;
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־ */
replay_pack += 1;
*replay_pack = 0x17;
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>пռ<D5BC>򽫷<EFBFBD><F2BDABB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
if (uxQueueSpacesAvailable(mcu_uart_Queue)) {
xQueueSend(mcu_uart_Queue, &Buff, 10);
}
else {
vPortFree(Buff);
}
}
static void SlaveMcuWriteRegister(uint8_t *buff, uint8_t dataLen)
{
uint16_t Register_Start_Address = (buff[6] << 8) | buff[7];
uint16_t Register_Number = (buff[8] << 8) | buff[9];
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD> */
for ( uint16_t pos = 0; pos < Register_Number; pos++) {
for (uint16_t i = 0; i < sizeof(g_RegTblW) / sizeof(SL_RegProcTable); i++) {
if (g_RegTblW[i].regId == (Register_Start_Address + pos)) {
uint16_t data = buff[10 + 2 * pos] << 8 | buff[10 + 2 * pos + 1];
g_RegTblW[i].pRegProc(&data);
}
}
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD> */
}
void slaveFrameDataAnalyze(uint8_t *buff, uint8_t dataLen)
{
uint16_t crc = (buff[dataLen - 4] << 8) | buff[dataLen - 3];
if (crc != modebusCrc(buff, dataLen - 3)) {
return;
}
if (buff[5] == SL_Function_Code_Read_Register) {
SlaveMcuReadRegister(buff, dataLen);
}
else if (buff[5] == SL_Function_Code_Write_Register) {
SlaveMcuWriteRegister(buff, dataLen);
}
}
/**
* @brief <EFBFBD><EFBFBD>ȡJ1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param
* @retval
*/
uint16_t ReadRegisterJ1Baud(void *pMsg)
{
return baudConversionConfig(getJ1_485_Baud());
}
/**
* @brief <EFBFBD><EFBFBD>ȡJ3<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param
* @retval
*/
uint16_t ReadRegisterJ3Baud(void *pMsg)
{
return baudConversionConfig(getJ3_485_Baud());
}
/**
* @brief <EFBFBD><EFBFBD>ȡJ5<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param
* @retval
*/
uint16_t ReadRegisterJ5Baud(void *pMsg)
{
return baudConversionConfig(getJ5_485_Baud());
}
/**
* @brief <EFBFBD><EFBFBD>ȡJ7<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param
* @retval
*/
uint16_t ReadRegisterJ7Baud(void *pMsg)
{
return baudConversionConfig(getJ7_485_Baud());
}
/**
* @brief <EFBFBD><EFBFBD>ȡJ8<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param
* @retval
*/
uint16_t ReadRegisterJ8Baud(void *pMsg)
{
return baudConversionConfig(getJ8_485_Baud());
}
/**
* @brief <EFBFBD><EFBFBD>ȡJ9<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param
* @retval
*/
uint16_t ReadRegisterJ9Baud(void *pMsg)
{
return baudConversionConfig(getJ9_485_Baud());
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>ö˿<EFBFBD>1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param
* @retval
*/
uint16_t WriteRegisterJ1Baud(void *pMsg)
{
setJ1_485_Baud(configConversionBaud(*(uint16_t *)pMsg));
Init_J1_485(getJ1_485_Baud());
return 0;
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>ö˿<EFBFBD>3<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param
* @retval
*/
uint16_t WriteRegisterJ3Baud(void *pMsg)
{
setJ3_485_Baud(configConversionBaud(*(uint16_t *)pMsg));
Init_J3_485(getJ3_485_Baud());
return 0;
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>ö˿<EFBFBD>1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param
* @retval
*/
uint16_t WriteRegisterJ5Baud(void *pMsg)
{
setJ5_485_Baud(configConversionBaud(*(uint16_t *)pMsg));
Init_J5_485(getJ5_485_Baud());
return 0;
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>ö˿<EFBFBD>7<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param
* @retval
*/
uint16_t WriteRegisterJ7Baud(void *pMsg)
{
setJ7_485_Baud(configConversionBaud(*(uint16_t *)pMsg));
Init_J7_485(getJ7_485_Baud());
return 0;
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>ö˿<EFBFBD>8<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param
* @retval
*/
uint16_t WriteRegisterJ8Baud(void *pMsg)
{
setJ8_485_Baud(configConversionBaud(*(uint16_t *)pMsg));
Init_J8_485(getJ8_485_Baud());
return 0;
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>ö˿<EFBFBD>9<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param
* @retval
*/
uint16_t WriteRegisterJ9Baud(void *pMsg)
{
setJ9_485_Baud(configConversionBaud(*(uint16_t *)pMsg));
Init_J9_485(getJ9_485_Baud());
return 0;
}