Entering a message buffer for CAN. The code has been restructured to import the library prototype into other projects

This commit is contained in:
Tenocha 2024-11-20 11:39:47 +03:00
parent 212d1cd684
commit 50035f4d34
2 changed files with 583 additions and 318 deletions

View File

@ -10,6 +10,8 @@
#define ONLINE 1;
#define OFFLINE 0;
// TIM defines
#define TIM_REQUESTER_Prescaler 64000
//CAN Filter
@ -22,19 +24,6 @@
#define CAN_SENSOR_TYPE_FILTER 0x00F80000
#define CAN_SENSOR_ID_FILTER 0x0007FF00
void REQUESTER_Init(void);
void REQUESTER_MainWhile(void);
void Boot_SystemClock_Config(void);
void REQUESTER_AnalogProcessing(void);
void REQUESTER_BroadcastProcessing(void);
void REQUESTER_DiscreticProcessing(void);
void REQUESTER_ModbusProcessing(void);
void REQUESTER_RTC_SYNC(uint8_t *data);
void REQUESTER_Pulse_TIM_Handler(void);
void Boot_SystemClock_Config(void);
void REQUESTER_CAN_FILTERS(void);
union Byte{
struct bitfield{
unsigned bit0:1;
@ -75,24 +64,26 @@ _Bool IsLeapYear(uint8_t year);
#define DATA_TYPE_PULSE 0b1111
//Sensor Types for DATA_TYPE_BROADCAST
#define SENSOR_TYPE_STATUS 0b00000
#define SENSOR_TYPE_ONOFF 0b00001
#define SENSOR_TYPE_RTCSETUP 0b00010
#define SENSOR_TYPE_BROADCAST_STATUS 0b00000
#define SENSOR_TYPE_BROADCAST_ONOFF 0b00001
#define SENSOR_TYPE_BROADCAST_RESTARTDEVICE 0b00010
#define SENSOR_TYPE_BROADCAST_RTCSETUP 0b00011
//Sensor Types for DATA_TYPE_DISCRETE
#define SENSOR_TYPE_ACCIDENT 0b00000
#define SENSOR_TYPE_WARNING 0b00001
#define SENSOR_TYPE_CONTROL_SIGNALS 0b00010
#define SENSOR_TYPE_FLAGS 0b00011
#define SENSOR_TYPE_RESET 0b00100
#define SENSOR_TYPE_CHANGE_MODE 0b00101
#define SENSOR_TYPE_REQUEST_LIST_OF_PARAMETERS 0b00110
#define SENSOR_TYPE_DISCRETE_ACCIDENT 0b00000
#define SENSOR_TYPE_DISCRETE_WARNING 0b00001
#define SENSOR_TYPE_DISCRETE_CONTROL_SIGNALS 0b00010
#define SENSOR_TYPE_DISCRETE_FLAGS 0b00011
#define SENSOR_TYPE_DISCRETE_RESET 0b00100
#define SENSOR_TYPE_DISCRETE_CHANGE_MODE 0b00101
#define SENSOR_TYPE_DISCRETE_REQUEST_LIST_OF_PARAMETERS 0b00110
//Sensor Types for DATA_TYPE_ANALOG
#define SENSOR_TYPE_UNIVERSAL 0b00000
#define SENSOR_TYPE_U 0b00001
#define SENSOR_TYPE_I 0b00010
#define SENSOR_TYPE_T 0b00011
#define SENSOR_TYPE_ANALOG_UNIVERSAL 0b00000
#define SENSOR_TYPE_ANALOG_USTAVKI 0b00001
#define SENSOR_TYPE_ANALOG_U 0b00010
#define SENSOR_TYPE_ANALOG_I 0b00011
#define SENSOR_TYPE_ANALOG_T 0b00100
//Error Code
#define NONEXISTENT_ELEMENT 0x01
@ -106,7 +97,7 @@ struct controlflags{
};
//Device settings
union ext_ID{
/*union ext_ID{
struct ext_ID_fields{
unsigned DeviceID:8;
unsigned SensorID:11;
@ -123,15 +114,33 @@ union ext_ID_Modbus{
unsigned CountReg:8;
unsigned StrAdr:8;
unsigned DataType:4;
unsigned Route;
unsigned Route:1;
}Fields;
unsigned int BitAll:29;
};
};*/
typedef union _extID{
struct extID_Standard_Fields{
unsigned DeviceID:8;
unsigned SensorID:11;
unsigned SensorType:5;
unsigned DataType:4;
unsigned Route:1;
}StandardFields;
struct extID_Modbus_Fields{
unsigned DeviceID:8;
unsigned CountReg:8;
unsigned StrAdr:8;
unsigned DataType:4;
unsigned Route:1;
}ModbusFields;
unsigned int BitAll:29;
}extID;
struct device
{
unsigned Status:1;
union ext_ID ExtID;
extID ExtID;
unsigned TimeFromLastPulse;
unsigned LastPulseStep;
};
@ -161,7 +170,7 @@ struct data
};
struct received_request{
union ext_ID RequestedExtID;
extID RequestedExtID;
unsigned RequestedDLC;
uint8_t RxData[8];
@ -221,4 +230,56 @@ struct received_request{
unsigned ModbusFlag:1;
};
#define CAN_RX_BUFFER_SIZE 128
struct RXMsg{
struct INFO{
unsigned EXT:1;
unsigned RTR:1;
}info;
extID eID;
uint16_t DLC;
uint8_t Data[8];
};
uint16_t AvailableCanRxMsg(void);
void REQUESTER_Init(void);
void REQUESTER_MainWhile(void);
void Boot_SystemClock_Config(void);
void REQUESTER_AnalogProcessing(struct RXMsg _rxMsg);
HAL_StatusTypeDef CanRequestToAnalogUniversal(struct RXMsg _rxMsg);
HAL_StatusTypeDef CanRequestToAnalogUSTAVKI(struct RXMsg _rxMsg);
HAL_StatusTypeDef CanRequestToAnalogUSens(struct RXMsg _rxMsg);
HAL_StatusTypeDef CanRequestToAnalogISens(struct RXMsg _rxMsg);
HAL_StatusTypeDef CanRequestToAnalogTSens(struct RXMsg _rxMsg);
void REQUESTER_BroadcastProcessing(struct RXMsg _rxMsg);
HAL_StatusTypeDef CanRequestToBroadcastStatus(struct RXMsg _rxMsg);
void CanRequestToBroadcastOnOff(struct RXMsg _rxMsg);
void CanRequestToBroadcastRestart(struct RXMsg _rxMsg);
void CanRequestToBroadcastRtcSetup(struct RXMsg _rxMsg);
void REQUESTER_DiscreticProcessing(struct RXMsg _rxMsg);
void CanRequestToDiscreteAccident(struct RXMsg _rxMsg);
void CanRequestToDiscreteWarning(struct RXMsg _rxMsg);
void CanRequestToDiscreteControlSignals(struct RXMsg _rxMsg);
void CanRequestToDiscreteFlags(struct RXMsg _rxMsg);
void CanRequestToDiscreteReset(struct RXMsg _rxMsg);
void CanRequestToDiscreteChangeMode(struct RXMsg _rxMsg);
void CanRequestToDiscreteRequestListOfParameters(struct RXMsg _rxMsg);
void REQUESTER_ModbusProcessing(struct RXMsg _rxMsg);
void CanRequestToModbusCoil(struct RXMsg _rxMsg);
void CanRequestToModbusDiscrete(struct RXMsg _rxMsg);
void CanRequestToModbusHolding(struct RXMsg _rxMsg);
void CanRequestToModbusInput(struct RXMsg _rxMsg);
void REQUESTER_RTC_SYNC(uint8_t *data);
void REQUESTER_Pulse_TIM_Handler(void);
void Boot_SystemClock_Config(void);
void REQUESTER_CAN_FILTERS(void);
#endif

View File

@ -13,6 +13,10 @@ struct data Data;
struct controlflags ControlFlags;
struct received_request ReceivedRequest;
uint8_t CurrentStep = 0;
uint8_t LastStep = 0;
struct RXMsg rxMsg[CAN_RX_BUFFER_SIZE];
#define MAX_NUM_OF_DEVICES_PER_LINE 4
#define MAX_NUM_OF_REGISTERS_IN_DEVICE 255
@ -26,6 +30,11 @@ _Bool IsLeapYear(uint8_t year)
return (year%400==0)||((year%4==0)&&(year%100!=0));
}
uint16_t AvailableCanRxMsg(void)
{
return ((uint16_t)(CAN_RX_BUFFER_SIZE + LastStep - CurrentStep))%CAN_RX_BUFFER_SIZE;
}
/**
* @brief Инициализация переферии
* @details Инициализация HAL, CAN, TIM7, RTC.
@ -77,45 +86,59 @@ void REQUESTER_MainWhile(void)
CAN_TxHeaderTypeDef TxHeader;
uint32_t TxMailBox = 0;
uint8_t data[8];
union ext_ID eID;
eID.Fields.DeviceID = 16;
eID.Fields.DataType = DATA_TYPE_ANALOG;
eID.Fields.SensorType = 0x1F;
eID.Fields.SensorID = 0;
eID.Fields.Route = ROUTE_SLAVE;
extID eID;
eID.BitAll = 0;
eID.StandardFields.DeviceID = 2;
eID.StandardFields.DataType = DATA_TYPE_ANALOG;
eID.StandardFields.SensorType = 0x1F;
eID.StandardFields.SensorID = 0;
eID.StandardFields.Route = ROUTE_SLAVE;
TxHeader.IDE = CAN_ID_EXT;
TxHeader.TransmitGlobalTime = DISABLE;
TxHeader.RTR = CAN_RTR_DATA;
TxHeader.ExtId = eID.BitAll;
int TxTest = 0;
HAL_TIM_Base_Start_IT(&htim7);
while(1)
{
if(ReceivedRequest.AnalogFlags.AllFlags)
if(AvailableCanRxMsg())
{
REQUESTER_AnalogProcessing();
}
if(ReceivedRequest.BroadcastFlags.AllFlags)
if(rxMsg[CurrentStep].eID.StandardFields.DataType == DATA_TYPE_ANALOG)
{
REQUESTER_BroadcastProcessing();
REQUESTER_AnalogProcessing(rxMsg[CurrentStep]);
}
if(ReceivedRequest.DiscreticFlags.AllFlags)
else if(rxMsg[CurrentStep].eID.StandardFields.DataType == DATA_TYPE_BROADCAST)
{
REQUESTER_DiscreticProcessing();
REQUESTER_BroadcastProcessing(rxMsg[CurrentStep]);
}
if(ReceivedRequest.ModbusFlags.AllFlags)
else if(rxMsg[CurrentStep].eID.StandardFields.DataType == DATA_TYPE_DISCRETE)
{
REQUESTER_ModbusProcessing();
REQUESTER_DiscreticProcessing(rxMsg[CurrentStep]);
}
else if(rxMsg[CurrentStep].eID.StandardFields.DataType == DATA_TYPE_MODBUS_COIL ||
rxMsg[CurrentStep].eID.StandardFields.DataType == DATA_TYPE_MODBUS_DISCRETE ||
rxMsg[CurrentStep].eID.StandardFields.DataType == DATA_TYPE_MODBUS_HOLDING ||
rxMsg[CurrentStep].eID.StandardFields.DataType == DATA_TYPE_MODBUS_INPUT)
{
REQUESTER_ModbusProcessing(rxMsg[CurrentStep]);
}
CurrentStep = (uint16_t)(CurrentStep + 1) % CAN_RX_BUFFER_SIZE;
}
/*
while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan) == 0);
TxHeader.ExtId = eID.BitAll;
if(HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox)!= HAL_OK)
{
ProverkaArbitors++;
}
eID.Fields.SensorID++;
if(eID.Fields.SensorID>100)
{
eID.Fields.SensorID = 0;
}
*/
// eID.Fields.SensorID++;
// if(eID.Fields.SensorID>10)
//{
// eID.Fields.SensorID = 0;
// }
}
}
@ -123,29 +146,98 @@ void REQUESTER_MainWhile(void)
* @brief Функция обработки аналоговых запросов.
* @details Функция, формирующая и отправляющая ответ на запросы. Типы запросов: Универсальный, Уставки, Напряжение, Ток, Температура
*/
void REQUESTER_AnalogProcessing()
void REQUESTER_AnalogProcessing(struct RXMsg _rxMsg)
{
switch (_rxMsg.eID.StandardFields.SensorType)
{
case SENSOR_TYPE_ANALOG_UNIVERSAL:
{
CanRequestToAnalogUniversal(_rxMsg);
break;
}
case SENSOR_TYPE_ANALOG_USTAVKI:
{
CanRequestToAnalogUSTAVKI(_rxMsg);
break;
}
case SENSOR_TYPE_ANALOG_U:
{
CanRequestToAnalogUSens(_rxMsg);
break;
}
case SENSOR_TYPE_ANALOG_I:
{
CanRequestToAnalogISens(_rxMsg);
break;
}
case SENSOR_TYPE_ANALOG_T:
{
CanRequestToAnalogTSens(_rxMsg);
break;
}
default:
//RESERVE SENSOR TYPE
break;
}
}
__weak HAL_StatusTypeDef CanRequestToAnalogUniversal(struct RXMsg _rxMsg)
{
CAN_TxHeaderTypeDef TxHeader;
uint32_t TxMailBox = 0;
uint8_t data[8];
if(ReceivedRequest.AnalogFlags.AnalogType.Request_Universal_Sens)
{
ReceivedRequest.AnalogFlags.AnalogType.Request_Universal_Sens = 0;
}
if(ReceivedRequest.AnalogFlags.AnalogType.Request_U_Sens)
{
//Запрос на данные датчика напряжения.
//В дальнейшем реализовать отправку настоящих данных.
//А пока - тестовое сообщение, нужное для отладки.
//Расширенный ID
TxHeader.IDE=CAN_ID_EXT;
//Ответ на запрос осуществляется по тому-же ID,
//с которым был отправлен запрос.
union ext_ID eID;
eID.BitAll = ReceivedRequest.RequestedExtID.BitAll;
eID.Fields.Route = ROUTE_SLAVE;
TxHeader.ExtId = eID.BitAll;
//Выставляется количество передаваемых байтов. (Макс. 8)
TxHeader.IDE = CAN_ID_EXT;
TxHeader.TransmitGlobalTime = DISABLE;
TxHeader.RTR = CAN_RTR_DATA;
extID tmp_eID;
tmp_eID.BitAll = _rxMsg.eID.BitAll;
tmp_eID.StandardFields.Route = ROUTE_SLAVE;
TxHeader.ExtId = tmp_eID.BitAll;
TxHeader.DLC = 6;
data[0] = 'U';
data[1] = 'N';
data[2] = 'I';
data[3] = 'V';
data[4] = 'E';
data[5] = 'R';
return HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
}
__weak HAL_StatusTypeDef CanRequestToAnalogUSTAVKI(struct RXMsg _rxMsg)
{
CAN_TxHeaderTypeDef TxHeader;
uint32_t TxMailBox = 0;
uint8_t data[8];
TxHeader.IDE = CAN_ID_EXT;
TxHeader.TransmitGlobalTime = DISABLE;
TxHeader.RTR = CAN_RTR_DATA;
extID tmp_eID;
tmp_eID.BitAll = _rxMsg.eID.BitAll;
tmp_eID.StandardFields.Route = ROUTE_SLAVE;
TxHeader.ExtId = tmp_eID.BitAll;
TxHeader.DLC = 7;
data[0] = 'U';
data[1] = 'S';
data[2] = 'T';
data[3] = 'A';
data[4] = 'V';
data[5] = 'K';
data[6] = 'I';
return HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
}
__weak HAL_StatusTypeDef CanRequestToAnalogUSens(struct RXMsg _rxMsg)
{
CAN_TxHeaderTypeDef TxHeader;
uint32_t TxMailBox = 0;
uint8_t data[8];
TxHeader.IDE = CAN_ID_EXT;
TxHeader.TransmitGlobalTime = DISABLE;
TxHeader.RTR = CAN_RTR_DATA;
extID tmp_eID;
tmp_eID.BitAll = _rxMsg.eID.BitAll;
tmp_eID.StandardFields.Route = ROUTE_SLAVE;
TxHeader.ExtId = tmp_eID.BitAll;
TxHeader.DLC = 6;
data[0] = 'U';
data[1] = ' ';
@ -153,16 +245,21 @@ void REQUESTER_AnalogProcessing()
data[3] = 'e';
data[4] = 'n';
data[5] = 's';
HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
ReceivedRequest.AnalogFlags.AnalogType.Request_U_Sens=0;
}
if(ReceivedRequest.AnalogFlags.AnalogType.Request_I_Sens)
{
TxHeader.IDE=CAN_ID_EXT;
union ext_ID eID;
eID.BitAll = ReceivedRequest.RequestedExtID.BitAll;
eID.Fields.Route = ROUTE_SLAVE;
TxHeader.ExtId = eID.BitAll;
return HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
}
__weak HAL_StatusTypeDef CanRequestToAnalogISens(struct RXMsg _rxMsg)
{
CAN_TxHeaderTypeDef TxHeader;
uint32_t TxMailBox = 0;
uint8_t data[8];
TxHeader.IDE = CAN_ID_EXT;
TxHeader.TransmitGlobalTime = DISABLE;
TxHeader.RTR = CAN_RTR_DATA;
extID tmp_eID;
tmp_eID.BitAll = _rxMsg.eID.BitAll;
tmp_eID.StandardFields.Route = ROUTE_SLAVE;
TxHeader.ExtId = tmp_eID.BitAll;
TxHeader.DLC = 6;
data[0] = 'I';
data[1] = ' ';
@ -170,16 +267,21 @@ void REQUESTER_AnalogProcessing()
data[3] = 'e';
data[4] = 'n';
data[5] = 's';
HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
ReceivedRequest.AnalogFlags.AnalogType.Request_I_Sens=0;
}
if(ReceivedRequest.AnalogFlags.AnalogType.Request_T_Sens)
{
TxHeader.IDE=CAN_ID_EXT;
union ext_ID eID;
eID.BitAll = ReceivedRequest.RequestedExtID.BitAll;
eID.Fields.Route = ROUTE_SLAVE;
TxHeader.ExtId = eID.BitAll;
return HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
}
__weak HAL_StatusTypeDef CanRequestToAnalogTSens(struct RXMsg _rxMsg)
{
CAN_TxHeaderTypeDef TxHeader;
uint32_t TxMailBox = 0;
uint8_t data[8];
TxHeader.IDE = CAN_ID_EXT;
TxHeader.TransmitGlobalTime = DISABLE;
TxHeader.RTR = CAN_RTR_DATA;
extID tmp_eID;
tmp_eID.BitAll = _rxMsg.eID.BitAll;
tmp_eID.StandardFields.Route = ROUTE_SLAVE;
TxHeader.ExtId = tmp_eID.BitAll;
TxHeader.DLC = 6;
data[0] = 'T';
data[1] = ' ';
@ -187,28 +289,107 @@ void REQUESTER_AnalogProcessing()
data[3] = 'e';
data[4] = 'n';
data[5] = 's';
HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
ReceivedRequest.AnalogFlags.AnalogType.Request_T_Sens=0;
}
return HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
}
/**
* @brief Функция обработки широковещательных запросов.
* @details Функция, выполняющая команды, переданные в широковещательном формате с головного (master) устройства. Типы команд: Запрос статуса, запрос на включение или выключение, рестарт устройств, установка времени.
*/
void REQUESTER_BroadcastProcessing()
void REQUESTER_BroadcastProcessing(struct RXMsg _rxMsg)
{
if(ReceivedRequest.BroadcastFlags.BroadcastType.Request_OnOff)
switch(_rxMsg.eID.StandardFields.SensorType)
{
case SENSOR_TYPE_BROADCAST_STATUS:
{
//Обработка запроса статуса устройства
CanRequestToBroadcastStatus(_rxMsg);
break;
}
case SENSOR_TYPE_BROADCAST_ONOFF:
{
//Обработка запроса на вкл/выкл
ControlFlags.IsPulse = !ControlFlags.IsPulse;
ReceivedRequest.BroadcastFlags.BroadcastType.Request_OnOff = 0;
CanRequestToBroadcastOnOff(_rxMsg);
break;
}
if(ReceivedRequest.BroadcastFlags.BroadcastType.Request_RTC_Setup)
case SENSOR_TYPE_BROADCAST_RESTARTDEVICE:
{
CanRequestToBroadcastRestart(_rxMsg);
break;
}
case SENSOR_TYPE_BROADCAST_RTCSETUP:
{
//Обработка запроса на синхронизацию времени
//С головным устройством
if(ReceivedRequest.RequestedDLC > 7)
CanRequestToBroadcastRtcSetup(_rxMsg);
break;;
}
default:
//RESERVE SENSOR TYPE.
break;
}
}
__weak HAL_StatusTypeDef CanRequestToBroadcastStatus(struct RXMsg _rxMsg)
{
CAN_TxHeaderTypeDef TxHeader;
uint32_t TxMailBox = 0;
uint8_t data[8];
TxHeader.IDE = CAN_ID_EXT;
TxHeader.DLC = 7;
TxHeader.TransmitGlobalTime = DISABLE;
TxHeader.RTR = CAN_RTR_DATA;
extID tmp_eID;
tmp_eID.BitAll = _rxMsg.eID.BitAll;
tmp_eID.StandardFields.Route = ROUTE_SLAVE;
tmp_eID.StandardFields.DeviceID = CURRENT_ID_DEVICE;
TxHeader.ExtId = tmp_eID.BitAll;
RTC_TimeTypeDef sTime = {0};
HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
data[0] = sTime.Hours;
data[1] = sTime.Minutes;
data[2] = sTime.Seconds;
RTC_DateTypeDef DateToUpdate = {0};
HAL_RTC_GetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BIN);
data[3] = DateToUpdate.Year;
data[4] = DateToUpdate.Month;
data[5] = DateToUpdate.Date;
data[6] = DateToUpdate.WeekDay;
return HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
}
__weak void CanRequestToBroadcastOnOff(struct RXMsg _rxMsg)
{
ControlFlags.IsPulse = !ControlFlags.IsPulse;
}
__weak void CanRequestToBroadcastRestart(struct RXMsg _rxMsg)
{
if(_rxMsg.DLC == 0)
{
return;
}
if(_rxMsg.eID.StandardFields.SensorID == (CURRENT_ID_DEVICE / (_rxMsg.DLC*8)))
{
uint64_t page = 0;
for(int i = 0; i < _rxMsg.DLC; i++)
{
page+=(_rxMsg.Data[i]<<(i*8));
}
if((page>>CURRENT_ID_DEVICE)&0b1)
{
NVIC_SystemReset();
}
}
return;
}
__weak void CanRequestToBroadcastRtcSetup(struct RXMsg _rxMsg)
{
if(_rxMsg.DLC > 7)
{
//ERROR
}
@ -216,52 +397,21 @@ void REQUESTER_BroadcastProcessing()
{
int DaysCount_Normal[2][12] = {{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
if (ReceivedRequest.RxData[0]>23 ||
ReceivedRequest.RxData[1]>59 ||
ReceivedRequest.RxData[2]>59 ||
ReceivedRequest.RxData[3]>99 ||
ReceivedRequest.RxData[4]>12 ||
ReceivedRequest.RxData[5] > DaysCount_Normal[IsLeapYear(ReceivedRequest.RxData[3])][ReceivedRequest.RxData[4]] ||
ReceivedRequest.RxData[6]>6)
if (_rxMsg.Data[0]>23 ||
_rxMsg.Data[1]>59 ||
_rxMsg.Data[2]>59 ||
_rxMsg.Data[3]>99 ||
_rxMsg.Data[4]>12 ||
_rxMsg.Data[5] > DaysCount_Normal[IsLeapYear(_rxMsg.Data[3])][_rxMsg.Data[4]] ||
_rxMsg.Data[6]>6)
{
//ERROR
}
else
{
REQUESTER_RTC_SYNC(ReceivedRequest.RxData);
REQUESTER_RTC_SYNC(_rxMsg.Data);
}
}
ReceivedRequest.BroadcastFlags.BroadcastType.Request_RTC_Setup = 0;
}
if(ReceivedRequest.BroadcastFlags.BroadcastType.Request_Status)
{
//Обработка запроса статуса устройства
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef DateToUpdate = {0};
HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
CAN_TxHeaderTypeDef TxHeader;
uint32_t TxMailBox = 0;
uint8_t data[8];
TxHeader.IDE=CAN_ID_EXT;
union ext_ID eID;
eID.BitAll = ReceivedRequest.RequestedExtID.BitAll;
eID.Fields.Route = ROUTE_SLAVE;
TxHeader.ExtId = eID.BitAll;
TxHeader.DLC = 7;
data[0] = sTime.Hours;
data[1] = sTime.Minutes;
data[2] = sTime.Seconds;
HAL_RTC_GetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BIN);
data[3] = DateToUpdate.Year;
data[4] = DateToUpdate.Month;
data[5] = DateToUpdate.Date;
data[6] = DateToUpdate.WeekDay;
HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
ReceivedRequest.AnalogFlags.AnalogType.Request_T_Sens=0;
ReceivedRequest.BroadcastFlags.BroadcastType.Request_Status = 0;
}
}
/**
@ -269,51 +419,136 @@ void REQUESTER_BroadcastProcessing()
* @details Функция, формирующая и отправляющая ответ на запросы. Типы запросов: Аварии, Предупреждения, Управляющие сигналы, Флаги, Рестарт устройства, Изменение режима работы устройства, Запрос на устройство.
* @note Запрос на устройство. Головное (master) устройство запрашивает некоторое колличество параметров. В Data - 64 битовых адресса параметров, тип которых задаётся в Sensor ID. Имеется возможность запрашивать непоследовательные параметры.
*/
void REQUESTER_DiscreticProcessing()
void REQUESTER_DiscreticProcessing(struct RXMsg _rxMsg)
{
if(ReceivedRequest.DiscreticFlags.DiscreticType.Request_Accident)
switch(_rxMsg.eID.StandardFields.SensorType){
case SENSOR_TYPE_DISCRETE_ACCIDENT:
{
ReceivedRequest.DiscreticFlags.DiscreticType.Request_Accident = 0;
CanRequestToDiscreteAccident(_rxMsg);
break;
}
if(ReceivedRequest.DiscreticFlags.DiscreticType.Request_Control_Signals)
case SENSOR_TYPE_DISCRETE_WARNING:
{
ReceivedRequest.DiscreticFlags.DiscreticType.Request_Control_Signals = 0;
CanRequestToDiscreteWarning(_rxMsg);
break;
}
if(ReceivedRequest.DiscreticFlags.DiscreticType.Request_Flags)
case SENSOR_TYPE_DISCRETE_CONTROL_SIGNALS:
{
ReceivedRequest.DiscreticFlags.DiscreticType.Request_Flags = 0;
CanRequestToDiscreteControlSignals(_rxMsg);
break;
}
if(ReceivedRequest.DiscreticFlags.DiscreticType.Request_Warning)
case SENSOR_TYPE_DISCRETE_FLAGS:
{
ReceivedRequest.DiscreticFlags.DiscreticType.Request_Warning = 0;
CanRequestToDiscreteFlags(_rxMsg);
break;
}
if(ReceivedRequest.DiscreticFlags.DiscreticType.Request_Reset)
case SENSOR_TYPE_DISCRETE_RESET:
{
ReceivedRequest.DiscreticFlags.DiscreticType.Request_Reset = 0;
CanRequestToDiscreteReset(_rxMsg);
break;
}
case SENSOR_TYPE_DISCRETE_CHANGE_MODE:
{
CanRequestToDiscreteChangeMode(_rxMsg);
break;
}
case SENSOR_TYPE_DISCRETE_REQUEST_LIST_OF_PARAMETERS:
{
CanRequestToDiscreteRequestListOfParameters(_rxMsg);
break;
}
default:
//RESERVE SENSOR TYPE.
break;
}
}
__weak void CanRequestToDiscreteAccident(struct RXMsg _rxMsg)
{
return;
}
__weak void CanRequestToDiscreteWarning(struct RXMsg _rxMsg)
{
return;
}
__weak void CanRequestToDiscreteControlSignals(struct RXMsg _rxMsg)
{
return;
}
__weak void CanRequestToDiscreteFlags(struct RXMsg _rxMsg)
{
return;
}
__weak void CanRequestToDiscreteReset(struct RXMsg _rxMsg)
{
NVIC_SystemReset();
}
if(ReceivedRequest.DiscreticFlags.DiscreticType.Request_List_of_Parameters)
{
ReceivedRequest.DiscreticFlags.DiscreticType.Request_List_of_Parameters = 0;
for(int Current_byte = 0; Current_byte < 8; Current_byte++)
{
for(int Current_bit = 0; Current_bit < 8; Current_bit++)
{
if((ReceivedRequest.RxData[Current_byte]>>Current_bit)&0b1)
{
_GET_MODBUS_ADR(ReceivedRequest.RequestedExtID.Fields.SensorID, Current_byte, Current_bit);
}
}
}
}
}
__weak void CanRequestToDiscreteChangeMode(struct RXMsg _rxMsg)
{
return;
}
__weak void CanRequestToDiscreteRequestListOfParameters(struct RXMsg _rxMsg)
{
return;
}
/**
* @brief Функция обработки Modbus запросов.
* @details Функция, формирующая и отправляющая ответ на запросы.
*/
void REQUESTER_ModbusProcessing()
void REQUESTER_ModbusProcessing(struct RXMsg _rxMsg)
{
switch(_rxMsg.eID.ModbusFields.DataType)
{
case DATA_TYPE_MODBUS_COIL:
{
CanRequestToModbusCoil(_rxMsg);
break;
}
case DATA_TYPE_MODBUS_DISCRETE:
{
CanRequestToModbusDiscrete(_rxMsg);
break;
}
case DATA_TYPE_MODBUS_HOLDING:
{
CanRequestToModbusHolding(_rxMsg);
break;
}
case DATA_TYPE_MODBUS_INPUT:
{
CanRequestToModbusInput(_rxMsg);
break;
}
default:
//ERROR
break;
}
}
__weak void CanRequestToModbusCoil(struct RXMsg _rxMsg)
{
return;
}
__weak void CanRequestToModbusDiscrete(struct RXMsg _rxMsg)
{
return;
}
__weak void CanRequestToModbusHolding(struct RXMsg _rxMsg)
{
return;
}
__weak void CanRequestToModbusInput(struct RXMsg _rxMsg)
{
return;
}
/* СТАРЫЙ УЖАСНЫЙ МОДБАС
if((ReceivedRequest.SensorToModbus.Modbus.StrAdr>=0) && (ReceivedRequest.SensorToModbus.Modbus.StrAdr<=127))
{
//Обращение к существующему в устройстве модбас регистру
@ -332,11 +567,11 @@ void REQUESTER_ModbusProcessing()
)
{
TxHeader.DLC = 0;
union ext_ID eID;
extID eID;
eID.BitAll = ReceivedRequest.RequestedExtID.BitAll;
eID.Fields.Route = ROUTE_SLAVE;
eID.Fields.SensorType = RequestFromDLC>>11;
eID.Fields.SensorID = RequestFromDLC;
eID.StandardFields.Route = ROUTE_SLAVE;
eID.StandardFields.SensorType = RequestFromDLC>>11;
eID.StandardFields.SensorID = RequestFromDLC;
TxHeader.ExtId = eID.BitAll;
for(int DataFor = 0; DataFor < 8; DataFor+=2)
{
@ -353,16 +588,17 @@ void REQUESTER_ModbusProcessing()
while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan) == 0);
HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
}
union ext_ID_Modbus extID;
extID.Fields.DeviceID = CURRENT_ID_DEVICE;
extID.Fields.DataType = DATA_TYPE_ERROR;
extID.Fields.CountReg = NONEXISTENT_ELEMENT;
extID.Fields.Route = ROUTE_SLAVE;
extID emID;
emID.BitAll = 0;
emID.ModbusFields.DeviceID = CURRENT_ID_DEVICE;
emID.ModbusFields.DataType = DATA_TYPE_ERROR;
emID.ModbusFields.CountReg = NONEXISTENT_ELEMENT;
emID.ModbusFields.Route = ROUTE_SLAVE;
TxHeader.DLC = 0;
for(;(RequestFromDLC-ReceivedRequest.SensorToModbus.Modbus.StrAdr)<ReceivedRequest.SensorToModbus.Modbus.Count; RequestFromDLC++)
{
extID.Fields.StrAdr = RequestFromDLC;
TxHeader.ExtId = extID.BitAll;
emID.ModbusFields.StrAdr = RequestFromDLC;
TxHeader.ExtId = emID.BitAll;
while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan) == 0);
HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
}
@ -370,8 +606,26 @@ void REQUESTER_ModbusProcessing()
return;
}
ReceivedRequest.ModbusFlags.AllFlags = 0;
*/
void TakeRxMsgToBuffer(extID tmp_eID, uint32_t tmp_IDE, uint32_t tmp_RTR, uint32_t tmp_DLC, uint8_t *tmp_DATA, uint16_t tmp_LastStep)
{
rxMsg[tmp_LastStep].eID.BitAll = tmp_eID.BitAll;
rxMsg[tmp_LastStep].info.EXT = tmp_IDE;
rxMsg[tmp_LastStep].info.RTR = tmp_RTR;
rxMsg[tmp_LastStep].DLC = tmp_DLC;
for(int i = 0; i < tmp_DLC; i++)
{
rxMsg[tmp_LastStep].Data[i] = tmp_DATA[i];
}
LastStep = tmp_LastStep;
}
/**
* @brief Callback-Функция обработки приёма.
* @details Функция, сигнализирующая через флаги в бесконечный цикл REQUESTER_MainWhile о приёме запроса.
*/
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
CAN_RxHeaderTypeDef RxHeader;
@ -380,131 +634,62 @@ void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
//Расширенный ID
if(RxHeader.IDE == CAN_ID_EXT)
{
union ext_ID ExtID_Of_RX_MSG;
uint16_t tmp_LastStep = (uint16_t)(LastStep + 1) % CAN_RX_BUFFER_SIZE;
if(tmp_LastStep == CurrentStep)
{
return;
}
extID ExtID_Of_RX_MSG;
ExtID_Of_RX_MSG.BitAll = RxHeader.ExtId;
//Полученное сообщение - широковещательное
if (ExtID_Of_RX_MSG.Fields.DeviceID == ID_MAIN_DEVICE)
if (ExtID_Of_RX_MSG.StandardFields.DeviceID == ID_MAIN_DEVICE)
{
if(ExtID_Of_RX_MSG.Fields.DataType == DATA_TYPE_BROADCAST)
if(ExtID_Of_RX_MSG.StandardFields.DataType == DATA_TYPE_BROADCAST)
{
switch(ExtID_Of_RX_MSG.Fields.SensorType)
{
case SENSOR_TYPE_STATUS:
ReceivedRequest.BroadcastFlags.BroadcastType.Request_Status = 1;
break;
case SENSOR_TYPE_ONOFF:
ReceivedRequest.BroadcastFlags.BroadcastType.Request_OnOff = 1;
break;
case SENSOR_TYPE_RTCSETUP:
ReceivedRequest.RequestedDLC = RxHeader.DLC;
ReceivedRequest.RxData[0] = RCAN_Data[0];
ReceivedRequest.RxData[1] = RCAN_Data[1];
ReceivedRequest.RxData[2] = RCAN_Data[2];
ReceivedRequest.RxData[3] = RCAN_Data[3];
ReceivedRequest.RxData[4] = RCAN_Data[4];
ReceivedRequest.RxData[5] = RCAN_Data[5];
ReceivedRequest.RxData[6] = RCAN_Data[6];
ReceivedRequest.BroadcastFlags.BroadcastType.Request_RTC_Setup = 1;
break;
default:
break;
}
TakeRxMsgToBuffer(ExtID_Of_RX_MSG, RxHeader.IDE, RxHeader.RTR, RxHeader.DLC, RCAN_Data, tmp_LastStep);
return;
}
}
//Если ID запроса соответствует ID устройства
if(ExtID_Of_RX_MSG.Fields.DeviceID == (CURRENT_ID_DEVICE))
if(ExtID_Of_RX_MSG.StandardFields.DeviceID == (CURRENT_ID_DEVICE))
{
if(ExtID_Of_RX_MSG.Fields.DataType == DATA_TYPE_DISCRETE)
if(ExtID_Of_RX_MSG.StandardFields.DataType == DATA_TYPE_DISCRETE)
{
switch(ExtID_Of_RX_MSG.Fields.SensorType)
{
case SENSOR_TYPE_ACCIDENT:
break;
case SENSOR_TYPE_WARNING:
break;
case SENSOR_TYPE_CONTROL_SIGNALS:
break;
case SENSOR_TYPE_FLAGS:
break;
case SENSOR_TYPE_RESET:
ReceivedRequest.DiscreticFlags.DiscreticType.Request_Reset = 1;
break;
case SENSOR_TYPE_CHANGE_MODE:
ControlFlags.IsRtrMode = !ControlFlags.IsRtrMode;
break;
case SENSOR_TYPE_REQUEST_LIST_OF_PARAMETERS:
ReceivedRequest.DiscreticFlags.DiscreticType.Request_List_of_Parameters = 1;
break;
TakeRxMsgToBuffer(ExtID_Of_RX_MSG, RxHeader.IDE, RxHeader.RTR, RxHeader.DLC, RCAN_Data, tmp_LastStep);
return;
}
}
if(ExtID_Of_RX_MSG.Fields.DataType == DATA_TYPE_ANALOG)
if(ExtID_Of_RX_MSG.StandardFields.DataType == DATA_TYPE_ANALOG)
{
//Является ли полученное сообщение - запросом
if(RxHeader.RTR)
{
ReceivedRequest.RequestedExtID.BitAll = ExtID_Of_RX_MSG.BitAll;
ReceivedRequest.RequestedDLC = RxHeader.DLC;
//Определяется запрашиваемая информация
switch(ExtID_Of_RX_MSG.Fields.SensorType)
TakeRxMsgToBuffer(ExtID_Of_RX_MSG, RxHeader.IDE, RxHeader.RTR, RxHeader.DLC, RCAN_Data, tmp_LastStep);
return;
}
}
if(ExtID_Of_RX_MSG.StandardFields.DataType == DATA_TYPE_MODBUS_COIL ||
ExtID_Of_RX_MSG.StandardFields.DataType == DATA_TYPE_MODBUS_DISCRETE ||
ExtID_Of_RX_MSG.StandardFields.DataType == DATA_TYPE_MODBUS_HOLDING ||
ExtID_Of_RX_MSG.StandardFields.DataType == DATA_TYPE_MODBUS_INPUT)
{
case SENSOR_TYPE_UNIVERSAL:
{
ReceivedRequest.AnalogFlags.AnalogType.Request_Universal_Sens=1;
break;
}
case SENSOR_TYPE_U:
{
ReceivedRequest.AnalogFlags.AnalogType.Request_U_Sens=1;
break;
}
case SENSOR_TYPE_I:
{
ReceivedRequest.AnalogFlags.AnalogType.Request_I_Sens=1;
break;
}
case SENSOR_TYPE_T:
{
ReceivedRequest.AnalogFlags.AnalogType.Request_T_Sens=1;
break;
}
}
}
}
if(ExtID_Of_RX_MSG.Fields.DataType == DATA_TYPE_MODBUS_COIL)
{
ReceivedRequest.RequestedExtID.BitAll = ExtID_Of_RX_MSG.BitAll;
ReceivedRequest.SensorToModbus.Sensor = SensorToModbusRegister(ReceivedRequest.RequestedExtID.Fields.SensorType, ReceivedRequest.RequestedExtID.Fields.SensorID);
ReceivedRequest.ModbusFlags.ModbusType.Coil = 1;
}
if(ExtID_Of_RX_MSG.Fields.DataType == DATA_TYPE_MODBUS_DISCRETE)
{
ReceivedRequest.RequestedExtID.BitAll = ExtID_Of_RX_MSG.BitAll;
ReceivedRequest.SensorToModbus.Sensor = SensorToModbusRegister(ReceivedRequest.RequestedExtID.Fields.SensorType, ReceivedRequest.RequestedExtID.Fields.SensorID);
ReceivedRequest.ModbusFlags.ModbusType.Discrete = 1;
}
if(ExtID_Of_RX_MSG.Fields.DataType == DATA_TYPE_MODBUS_HOLDING)
{
ReceivedRequest.RequestedExtID.BitAll = ExtID_Of_RX_MSG.BitAll;
ReceivedRequest.SensorToModbus.Sensor = SensorToModbusRegister(ReceivedRequest.RequestedExtID.Fields.SensorType, ReceivedRequest.RequestedExtID.Fields.SensorID);
ReceivedRequest.ModbusFlags.ModbusType.Holding = 1;
}
if(ExtID_Of_RX_MSG.Fields.DataType == DATA_TYPE_MODBUS_INPUT)
{
ReceivedRequest.RequestedExtID.BitAll = ExtID_Of_RX_MSG.BitAll;
ReceivedRequest.SensorToModbus.Sensor = SensorToModbusRegister(ReceivedRequest.RequestedExtID.Fields.SensorType, ReceivedRequest.RequestedExtID.Fields.SensorID);
ReceivedRequest.ModbusFlags.ModbusType.Input = 1;
TakeRxMsgToBuffer(ExtID_Of_RX_MSG, RxHeader.IDE, RxHeader.RTR, RxHeader.DLC, RCAN_Data, tmp_LastStep);
return;
}
}
//Полученное сообщение - пульс устройств в сети
if (ExtID_Of_RX_MSG.Fields.DataType == DATA_TYPE_PULSE)
if (ExtID_Of_RX_MSG.StandardFields.DataType == DATA_TYPE_PULSE)
{
Device_on_the_Network[ExtID_Of_RX_MSG.Fields.DeviceID].Status = ONLINE;
Device_on_the_Network[ExtID_Of_RX_MSG.Fields.DeviceID].TimeFromLastPulse = 0;
Device_on_the_Network[ExtID_Of_RX_MSG.StandardFields.DeviceID].Status = ONLINE;
Device_on_the_Network[ExtID_Of_RX_MSG.StandardFields.DeviceID].TimeFromLastPulse = 0;
}
}
}
/**
* @brief Функция отправки пульса устройства.
* @details Пульс устройства. Есть возможность отключить пульс по запросу.
*/
void REQUESTER_Pulse_TIM_Handler()
{
if(ControlFlags.IsPulse)
@ -512,11 +697,11 @@ void REQUESTER_Pulse_TIM_Handler()
static unsigned PulseStage = 0;
CAN_TxHeaderTypeDef TxHeader;
uint32_t TxMailBox = 0;
union ext_ID currentID;
extID currentID;
currentID.BitAll = 0;
currentID.Fields.DeviceID = CURRENT_ID_DEVICE;
currentID.Fields.DataType = DATA_TYPE_PULSE;
currentID.Fields.Route = ROUTE_SLAVE;
currentID.StandardFields.DeviceID = CURRENT_ID_DEVICE;
currentID.StandardFields.DataType = DATA_TYPE_PULSE;
currentID.StandardFields.Route = ROUTE_SLAVE;
TxHeader.ExtId = currentID.BitAll;
uint8_t data[8];
TxHeader.IDE = CAN_ID_EXT;
@ -532,6 +717,10 @@ void REQUESTER_Pulse_TIM_Handler()
}
}
/**
* @brief Функция установки в RTC полученной даты/время из запроса.
* @param Байтовый массив, 7 элементов. [0] - Часы. [1] - Минуты. [2] - Секунды. [3] - Год. [4] - Месяц. [5] - Дата. [6] - День недели.
*/
void REQUESTER_RTC_SYNC(uint8_t *data)
{
__HAL_RTC_WRITEPROTECTION_DISABLE(&hrtc);
@ -558,6 +747,10 @@ void REQUESTER_RTC_SYNC(uint8_t *data)
__HAL_RTC_WRITEPROTECTION_ENABLE(&hrtc);
}
/**
* @brief Функция настройки фильтров CAN.
* @details Настройка фильтров. Фильр для приёма сообщений с главного устройства. Фильтр для приёма на текущее устройство. Фильтр для приёма пульса других устройств в сети.
*/
void REQUESTER_CAN_FILTERS()
{
//MAIN DEVICE
@ -616,4 +809,15 @@ void REQUESTER_CAN_FILTERS()
{
Error_Handler();
}
//DEBUG FILTER "ALL IDE WELCOME"
canFilterConfig.FilterBank = 4;
canFilterConfig.FilterIdHigh = (uint16_t)(HighIdFilter(0)>>13);
canFilterConfig.FilterIdLow = (uint16_t)(HighIdFilter(0)<<5) | CAN_IDE_32;
canFilterConfig.FilterMaskIdHigh = (uint16_t)(0>>13); // we're checking only high 13 bits, that contained "key"
canFilterConfig.FilterMaskIdLow = (uint16_t)(0<<3) | CAN_IDE_32; // 1<<2 - set IDE bit
if(HAL_CAN_ConfigFilter(&hcan, &canFilterConfig) != HAL_OK)
{
Error_Handler();
}
}