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 ONLINE 1;
#define OFFLINE 0; #define OFFLINE 0;
// TIM defines
#define TIM_REQUESTER_Prescaler 64000 #define TIM_REQUESTER_Prescaler 64000
//CAN Filter //CAN Filter
@ -22,19 +24,6 @@
#define CAN_SENSOR_TYPE_FILTER 0x00F80000 #define CAN_SENSOR_TYPE_FILTER 0x00F80000
#define CAN_SENSOR_ID_FILTER 0x0007FF00 #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{ union Byte{
struct bitfield{ struct bitfield{
unsigned bit0:1; unsigned bit0:1;
@ -75,24 +64,26 @@ _Bool IsLeapYear(uint8_t year);
#define DATA_TYPE_PULSE 0b1111 #define DATA_TYPE_PULSE 0b1111
//Sensor Types for DATA_TYPE_BROADCAST //Sensor Types for DATA_TYPE_BROADCAST
#define SENSOR_TYPE_STATUS 0b00000 #define SENSOR_TYPE_BROADCAST_STATUS 0b00000
#define SENSOR_TYPE_ONOFF 0b00001 #define SENSOR_TYPE_BROADCAST_ONOFF 0b00001
#define SENSOR_TYPE_RTCSETUP 0b00010 #define SENSOR_TYPE_BROADCAST_RESTARTDEVICE 0b00010
#define SENSOR_TYPE_BROADCAST_RTCSETUP 0b00011
//Sensor Types for DATA_TYPE_DISCRETE //Sensor Types for DATA_TYPE_DISCRETE
#define SENSOR_TYPE_ACCIDENT 0b00000 #define SENSOR_TYPE_DISCRETE_ACCIDENT 0b00000
#define SENSOR_TYPE_WARNING 0b00001 #define SENSOR_TYPE_DISCRETE_WARNING 0b00001
#define SENSOR_TYPE_CONTROL_SIGNALS 0b00010 #define SENSOR_TYPE_DISCRETE_CONTROL_SIGNALS 0b00010
#define SENSOR_TYPE_FLAGS 0b00011 #define SENSOR_TYPE_DISCRETE_FLAGS 0b00011
#define SENSOR_TYPE_RESET 0b00100 #define SENSOR_TYPE_DISCRETE_RESET 0b00100
#define SENSOR_TYPE_CHANGE_MODE 0b00101 #define SENSOR_TYPE_DISCRETE_CHANGE_MODE 0b00101
#define SENSOR_TYPE_REQUEST_LIST_OF_PARAMETERS 0b00110 #define SENSOR_TYPE_DISCRETE_REQUEST_LIST_OF_PARAMETERS 0b00110
//Sensor Types for DATA_TYPE_ANALOG //Sensor Types for DATA_TYPE_ANALOG
#define SENSOR_TYPE_UNIVERSAL 0b00000 #define SENSOR_TYPE_ANALOG_UNIVERSAL 0b00000
#define SENSOR_TYPE_U 0b00001 #define SENSOR_TYPE_ANALOG_USTAVKI 0b00001
#define SENSOR_TYPE_I 0b00010 #define SENSOR_TYPE_ANALOG_U 0b00010
#define SENSOR_TYPE_T 0b00011 #define SENSOR_TYPE_ANALOG_I 0b00011
#define SENSOR_TYPE_ANALOG_T 0b00100
//Error Code //Error Code
#define NONEXISTENT_ELEMENT 0x01 #define NONEXISTENT_ELEMENT 0x01
@ -106,7 +97,7 @@ struct controlflags{
}; };
//Device settings //Device settings
union ext_ID{ /*union ext_ID{
struct ext_ID_fields{ struct ext_ID_fields{
unsigned DeviceID:8; unsigned DeviceID:8;
unsigned SensorID:11; unsigned SensorID:11;
@ -123,15 +114,33 @@ union ext_ID_Modbus{
unsigned CountReg:8; unsigned CountReg:8;
unsigned StrAdr:8; unsigned StrAdr:8;
unsigned DataType:4; unsigned DataType:4;
unsigned Route; unsigned Route:1;
}Fields; }Fields;
unsigned int BitAll:29; 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 struct device
{ {
unsigned Status:1; unsigned Status:1;
union ext_ID ExtID; extID ExtID;
unsigned TimeFromLastPulse; unsigned TimeFromLastPulse;
unsigned LastPulseStep; unsigned LastPulseStep;
}; };
@ -161,7 +170,7 @@ struct data
}; };
struct received_request{ struct received_request{
union ext_ID RequestedExtID; extID RequestedExtID;
unsigned RequestedDLC; unsigned RequestedDLC;
uint8_t RxData[8]; uint8_t RxData[8];
@ -221,4 +230,56 @@ struct received_request{
unsigned ModbusFlag:1; 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 #endif

View File

@ -13,6 +13,10 @@ struct data Data;
struct controlflags ControlFlags; struct controlflags ControlFlags;
struct received_request ReceivedRequest; 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_DEVICES_PER_LINE 4
#define MAX_NUM_OF_REGISTERS_IN_DEVICE 255 #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)); 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 Инициализация переферии * @brief Инициализация переферии
* @details Инициализация HAL, CAN, TIM7, RTC. * @details Инициализация HAL, CAN, TIM7, RTC.
@ -77,45 +86,59 @@ void REQUESTER_MainWhile(void)
CAN_TxHeaderTypeDef TxHeader; CAN_TxHeaderTypeDef TxHeader;
uint32_t TxMailBox = 0; uint32_t TxMailBox = 0;
uint8_t data[8]; uint8_t data[8];
union ext_ID eID; extID eID;
eID.Fields.DeviceID = 16; eID.BitAll = 0;
eID.Fields.DataType = DATA_TYPE_ANALOG; eID.StandardFields.DeviceID = 2;
eID.Fields.SensorType = 0x1F; eID.StandardFields.DataType = DATA_TYPE_ANALOG;
eID.Fields.SensorID = 0; eID.StandardFields.SensorType = 0x1F;
eID.Fields.Route = ROUTE_SLAVE; eID.StandardFields.SensorID = 0;
eID.StandardFields.Route = ROUTE_SLAVE;
TxHeader.IDE = CAN_ID_EXT;
TxHeader.TransmitGlobalTime = DISABLE; TxHeader.TransmitGlobalTime = DISABLE;
TxHeader.RTR = CAN_RTR_DATA; TxHeader.RTR = CAN_RTR_DATA;
TxHeader.ExtId = eID.BitAll;
int TxTest = 0; int TxTest = 0;
HAL_TIM_Base_Start_IT(&htim7); HAL_TIM_Base_Start_IT(&htim7);
while(1) while(1)
{ {
if(ReceivedRequest.AnalogFlags.AllFlags) if(AvailableCanRxMsg())
{ {
REQUESTER_AnalogProcessing(); if(rxMsg[CurrentStep].eID.StandardFields.DataType == DATA_TYPE_ANALOG)
} {
if(ReceivedRequest.BroadcastFlags.AllFlags) REQUESTER_AnalogProcessing(rxMsg[CurrentStep]);
{ }
REQUESTER_BroadcastProcessing(); else if(rxMsg[CurrentStep].eID.StandardFields.DataType == DATA_TYPE_BROADCAST)
} {
if(ReceivedRequest.DiscreticFlags.AllFlags) REQUESTER_BroadcastProcessing(rxMsg[CurrentStep]);
{ }
REQUESTER_DiscreticProcessing(); else if(rxMsg[CurrentStep].eID.StandardFields.DataType == DATA_TYPE_DISCRETE)
} {
if(ReceivedRequest.ModbusFlags.AllFlags) REQUESTER_DiscreticProcessing(rxMsg[CurrentStep]);
{ }
REQUESTER_ModbusProcessing(); 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); while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan) == 0);
TxHeader.ExtId = eID.BitAll;
if(HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox)!= HAL_OK) if(HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox)!= HAL_OK)
{ {
ProverkaArbitors++; ProverkaArbitors++;
} }
eID.Fields.SensorID++; */
if(eID.Fields.SensorID>100) // eID.Fields.SensorID++;
{ // if(eID.Fields.SensorID>10)
eID.Fields.SensorID = 0; //{
} // eID.Fields.SensorID = 0;
// }
} }
} }
@ -123,92 +146,250 @@ void REQUESTER_MainWhile(void)
* @brief Функция обработки аналоговых запросов. * @brief Функция обработки аналоговых запросов.
* @details Функция, формирующая и отправляющая ответ на запросы. Типы запросов: Универсальный, Уставки, Напряжение, Ток, Температура * @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; CAN_TxHeaderTypeDef TxHeader;
uint32_t TxMailBox = 0; uint32_t TxMailBox = 0;
uint8_t data[8]; uint8_t data[8];
if(ReceivedRequest.AnalogFlags.AnalogType.Request_Universal_Sens) TxHeader.IDE = CAN_ID_EXT;
{ TxHeader.TransmitGlobalTime = DISABLE;
ReceivedRequest.AnalogFlags.AnalogType.Request_Universal_Sens = 0; TxHeader.RTR = CAN_RTR_DATA;
} extID tmp_eID;
if(ReceivedRequest.AnalogFlags.AnalogType.Request_U_Sens) tmp_eID.BitAll = _rxMsg.eID.BitAll;
{ tmp_eID.StandardFields.Route = ROUTE_SLAVE;
//Запрос на данные датчика напряжения. TxHeader.ExtId = tmp_eID.BitAll;
//В дальнейшем реализовать отправку настоящих данных. TxHeader.DLC = 6;
//А пока - тестовое сообщение, нужное для отладки. data[0] = 'U';
//Расширенный ID data[1] = 'N';
TxHeader.IDE=CAN_ID_EXT; data[2] = 'I';
//Ответ на запрос осуществляется по тому-же ID, data[3] = 'V';
//с которым был отправлен запрос. data[4] = 'E';
union ext_ID eID; data[5] = 'R';
eID.BitAll = ReceivedRequest.RequestedExtID.BitAll; return HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
eID.Fields.Route = ROUTE_SLAVE; }
TxHeader.ExtId = eID.BitAll;
//Выставляется количество передаваемых байтов. (Макс. 8) __weak HAL_StatusTypeDef CanRequestToAnalogUSTAVKI(struct RXMsg _rxMsg)
TxHeader.DLC = 6; {
data[0] = 'U'; CAN_TxHeaderTypeDef TxHeader;
data[1] = ' '; uint32_t TxMailBox = 0;
data[2] = 's'; uint8_t data[8];
data[3] = 'e'; TxHeader.IDE = CAN_ID_EXT;
data[4] = 'n'; TxHeader.TransmitGlobalTime = DISABLE;
data[5] = 's'; TxHeader.RTR = CAN_RTR_DATA;
HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox); extID tmp_eID;
ReceivedRequest.AnalogFlags.AnalogType.Request_U_Sens=0; tmp_eID.BitAll = _rxMsg.eID.BitAll;
} tmp_eID.StandardFields.Route = ROUTE_SLAVE;
if(ReceivedRequest.AnalogFlags.AnalogType.Request_I_Sens) TxHeader.ExtId = tmp_eID.BitAll;
{ TxHeader.DLC = 7;
TxHeader.IDE=CAN_ID_EXT; data[0] = 'U';
union ext_ID eID; data[1] = 'S';
eID.BitAll = ReceivedRequest.RequestedExtID.BitAll; data[2] = 'T';
eID.Fields.Route = ROUTE_SLAVE; data[3] = 'A';
TxHeader.ExtId = eID.BitAll; data[4] = 'V';
TxHeader.DLC = 6; data[5] = 'K';
data[0] = 'I'; data[6] = 'I';
data[1] = ' '; return HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
data[2] = 's'; }
data[3] = 'e';
data[4] = 'n'; __weak HAL_StatusTypeDef CanRequestToAnalogUSens(struct RXMsg _rxMsg)
data[5] = 's'; {
HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox); CAN_TxHeaderTypeDef TxHeader;
ReceivedRequest.AnalogFlags.AnalogType.Request_I_Sens=0; uint32_t TxMailBox = 0;
} uint8_t data[8];
if(ReceivedRequest.AnalogFlags.AnalogType.Request_T_Sens) TxHeader.IDE = CAN_ID_EXT;
{ TxHeader.TransmitGlobalTime = DISABLE;
TxHeader.IDE=CAN_ID_EXT; TxHeader.RTR = CAN_RTR_DATA;
union ext_ID eID; extID tmp_eID;
eID.BitAll = ReceivedRequest.RequestedExtID.BitAll; tmp_eID.BitAll = _rxMsg.eID.BitAll;
eID.Fields.Route = ROUTE_SLAVE; tmp_eID.StandardFields.Route = ROUTE_SLAVE;
TxHeader.ExtId = eID.BitAll; TxHeader.ExtId = tmp_eID.BitAll;
TxHeader.DLC = 6; TxHeader.DLC = 6;
data[0] = 'T'; data[0] = 'U';
data[1] = ' '; data[1] = ' ';
data[2] = 's'; data[2] = 's';
data[3] = 'e'; data[3] = 'e';
data[4] = 'n'; data[4] = 'n';
data[5] = 's'; data[5] = 's';
HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox); return HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
ReceivedRequest.AnalogFlags.AnalogType.Request_T_Sens=0; }
}
__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] = ' ';
data[2] = 's';
data[3] = 'e';
data[4] = 'n';
data[5] = 's';
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] = ' ';
data[2] = 's';
data[3] = 'e';
data[4] = 'n';
data[5] = 's';
return HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
} }
/** /**
* @brief Функция обработки широковещательных запросов. * @brief Функция обработки широковещательных запросов.
* @details Функция, выполняющая команды, переданные в широковещательном формате с головного (master) устройства. Типы команд: Запрос статуса, запрос на включение или выключение, рестарт устройств, установка времени. * @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:
ControlFlags.IsPulse = !ControlFlags.IsPulse; {
ReceivedRequest.BroadcastFlags.BroadcastType.Request_OnOff = 0; //Обработка запроса статуса устройства
CanRequestToBroadcastStatus(_rxMsg);
break;
}
case SENSOR_TYPE_BROADCAST_ONOFF:
{
//Обработка запроса на вкл/выкл
CanRequestToBroadcastOnOff(_rxMsg);
break;
}
case SENSOR_TYPE_BROADCAST_RESTARTDEVICE:
{
CanRequestToBroadcastRestart(_rxMsg);
break;
}
case SENSOR_TYPE_BROADCAST_RTCSETUP:
{
//Обработка запроса на синхронизацию времени
//С головным устройством
CanRequestToBroadcastRtcSetup(_rxMsg);
break;;
}
default:
//RESERVE SENSOR TYPE.
break;
} }
if(ReceivedRequest.BroadcastFlags.BroadcastType.Request_RTC_Setup) }
__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(ReceivedRequest.RequestedDLC > 7) 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 //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}, 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}}; {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
if (ReceivedRequest.RxData[0]>23 || if (_rxMsg.Data[0]>23 ||
ReceivedRequest.RxData[1]>59 || _rxMsg.Data[1]>59 ||
ReceivedRequest.RxData[2]>59 || _rxMsg.Data[2]>59 ||
ReceivedRequest.RxData[3]>99 || _rxMsg.Data[3]>99 ||
ReceivedRequest.RxData[4]>12 || _rxMsg.Data[4]>12 ||
ReceivedRequest.RxData[5] > DaysCount_Normal[IsLeapYear(ReceivedRequest.RxData[3])][ReceivedRequest.RxData[4]] || _rxMsg.Data[5] > DaysCount_Normal[IsLeapYear(_rxMsg.Data[3])][_rxMsg.Data[4]] ||
ReceivedRequest.RxData[6]>6) _rxMsg.Data[6]>6)
{ {
//ERROR //ERROR
} }
else 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 Функция, формирующая и отправляющая ответ на запросы. Типы запросов: Аварии, Предупреждения, Управляющие сигналы, Флаги, Рестарт устройства, Изменение режима работы устройства, Запрос на устройство. * @details Функция, формирующая и отправляющая ответ на запросы. Типы запросов: Аварии, Предупреждения, Управляющие сигналы, Флаги, Рестарт устройства, Изменение режима работы устройства, Запрос на устройство.
* @note Запрос на устройство. Головное (master) устройство запрашивает некоторое колличество параметров. В Data - 64 битовых адресса параметров, тип которых задаётся в Sensor ID. Имеется возможность запрашивать непоследовательные параметры. * @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;
}
if(ReceivedRequest.DiscreticFlags.DiscreticType.Request_Control_Signals)
{
ReceivedRequest.DiscreticFlags.DiscreticType.Request_Control_Signals = 0;
}
if(ReceivedRequest.DiscreticFlags.DiscreticType.Request_Flags)
{
ReceivedRequest.DiscreticFlags.DiscreticType.Request_Flags = 0;
}
if(ReceivedRequest.DiscreticFlags.DiscreticType.Request_Warning)
{
ReceivedRequest.DiscreticFlags.DiscreticType.Request_Warning = 0;
}
if(ReceivedRequest.DiscreticFlags.DiscreticType.Request_Reset)
{
ReceivedRequest.DiscreticFlags.DiscreticType.Request_Reset = 0;
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++) CanRequestToDiscreteAccident(_rxMsg);
{ break;
if((ReceivedRequest.RxData[Current_byte]>>Current_bit)&0b1)
{
_GET_MODBUS_ADR(ReceivedRequest.RequestedExtID.Fields.SensorID, Current_byte, Current_bit);
}
}
} }
case SENSOR_TYPE_DISCRETE_WARNING:
{
CanRequestToDiscreteWarning(_rxMsg);
break;
}
case SENSOR_TYPE_DISCRETE_CONTROL_SIGNALS:
{
CanRequestToDiscreteControlSignals(_rxMsg);
break;
}
case SENSOR_TYPE_DISCRETE_FLAGS:
{
CanRequestToDiscreteFlags(_rxMsg);
break;
}
case SENSOR_TYPE_DISCRETE_RESET:
{
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();
}
__weak void CanRequestToDiscreteChangeMode(struct RXMsg _rxMsg)
{
return;
}
__weak void CanRequestToDiscreteRequestListOfParameters(struct RXMsg _rxMsg)
{
return;
}
/** /**
* @brief Функция обработки Modbus запросов. * @brief Функция обработки Modbus запросов.
* @details Функция, формирующая и отправляющая ответ на запросы. * @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)) if((ReceivedRequest.SensorToModbus.Modbus.StrAdr>=0) && (ReceivedRequest.SensorToModbus.Modbus.StrAdr<=127))
{ {
//Обращение к существующему в устройстве модбас регистру //Обращение к существующему в устройстве модбас регистру
@ -332,11 +567,11 @@ void REQUESTER_ModbusProcessing()
) )
{ {
TxHeader.DLC = 0; TxHeader.DLC = 0;
union ext_ID eID; extID eID;
eID.BitAll = ReceivedRequest.RequestedExtID.BitAll; eID.BitAll = ReceivedRequest.RequestedExtID.BitAll;
eID.Fields.Route = ROUTE_SLAVE; eID.StandardFields.Route = ROUTE_SLAVE;
eID.Fields.SensorType = RequestFromDLC>>11; eID.StandardFields.SensorType = RequestFromDLC>>11;
eID.Fields.SensorID = RequestFromDLC; eID.StandardFields.SensorID = RequestFromDLC;
TxHeader.ExtId = eID.BitAll; TxHeader.ExtId = eID.BitAll;
for(int DataFor = 0; DataFor < 8; DataFor+=2) for(int DataFor = 0; DataFor < 8; DataFor+=2)
{ {
@ -353,16 +588,17 @@ void REQUESTER_ModbusProcessing()
while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan) == 0); while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan) == 0);
HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox); HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
} }
union ext_ID_Modbus extID; extID emID;
extID.Fields.DeviceID = CURRENT_ID_DEVICE; emID.BitAll = 0;
extID.Fields.DataType = DATA_TYPE_ERROR; emID.ModbusFields.DeviceID = CURRENT_ID_DEVICE;
extID.Fields.CountReg = NONEXISTENT_ELEMENT; emID.ModbusFields.DataType = DATA_TYPE_ERROR;
extID.Fields.Route = ROUTE_SLAVE; emID.ModbusFields.CountReg = NONEXISTENT_ELEMENT;
emID.ModbusFields.Route = ROUTE_SLAVE;
TxHeader.DLC = 0; TxHeader.DLC = 0;
for(;(RequestFromDLC-ReceivedRequest.SensorToModbus.Modbus.StrAdr)<ReceivedRequest.SensorToModbus.Modbus.Count; RequestFromDLC++) for(;(RequestFromDLC-ReceivedRequest.SensorToModbus.Modbus.StrAdr)<ReceivedRequest.SensorToModbus.Modbus.Count; RequestFromDLC++)
{ {
extID.Fields.StrAdr = RequestFromDLC; emID.ModbusFields.StrAdr = RequestFromDLC;
TxHeader.ExtId = extID.BitAll; TxHeader.ExtId = emID.BitAll;
while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan) == 0); while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan) == 0);
HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox); HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
} }
@ -370,8 +606,26 @@ void REQUESTER_ModbusProcessing()
return; return;
} }
ReceivedRequest.ModbusFlags.AllFlags = 0; 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) void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{ {
CAN_RxHeaderTypeDef RxHeader; CAN_RxHeaderTypeDef RxHeader;
@ -380,131 +634,62 @@ void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
//Расширенный ID //Расширенный ID
if(RxHeader.IDE == CAN_ID_EXT) 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; 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) TakeRxMsgToBuffer(ExtID_Of_RX_MSG, RxHeader.IDE, RxHeader.RTR, RxHeader.DLC, RCAN_Data, tmp_LastStep);
{
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;
}
return; return;
} }
} }
//Если ID запроса соответствует ID устройства //Если 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) TakeRxMsgToBuffer(ExtID_Of_RX_MSG, RxHeader.IDE, RxHeader.RTR, RxHeader.DLC, RCAN_Data, tmp_LastStep);
{ return;
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;
}
} }
if(ExtID_Of_RX_MSG.Fields.DataType == DATA_TYPE_ANALOG) if(ExtID_Of_RX_MSG.StandardFields.DataType == DATA_TYPE_ANALOG)
{ {
//Является ли полученное сообщение - запросом //Является ли полученное сообщение - запросом
if(RxHeader.RTR) if(RxHeader.RTR)
{ {
ReceivedRequest.RequestedExtID.BitAll = ExtID_Of_RX_MSG.BitAll; TakeRxMsgToBuffer(ExtID_Of_RX_MSG, RxHeader.IDE, RxHeader.RTR, RxHeader.DLC, RCAN_Data, tmp_LastStep);
ReceivedRequest.RequestedDLC = RxHeader.DLC; return;
//Определяется запрашиваемая информация
switch(ExtID_Of_RX_MSG.Fields.SensorType)
{
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) 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)
{ {
ReceivedRequest.RequestedExtID.BitAll = ExtID_Of_RX_MSG.BitAll; TakeRxMsgToBuffer(ExtID_Of_RX_MSG, RxHeader.IDE, RxHeader.RTR, RxHeader.DLC, RCAN_Data, tmp_LastStep);
ReceivedRequest.SensorToModbus.Sensor = SensorToModbusRegister(ReceivedRequest.RequestedExtID.Fields.SensorType, ReceivedRequest.RequestedExtID.Fields.SensorID); return;
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;
} }
} }
//Полученное сообщение - пульс устройств в сети //Полученное сообщение - пульс устройств в сети
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.StandardFields.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].TimeFromLastPulse = 0;
} }
} }
} }
/**
* @brief Функция отправки пульса устройства.
* @details Пульс устройства. Есть возможность отключить пульс по запросу.
*/
void REQUESTER_Pulse_TIM_Handler() void REQUESTER_Pulse_TIM_Handler()
{ {
if(ControlFlags.IsPulse) if(ControlFlags.IsPulse)
@ -512,11 +697,11 @@ void REQUESTER_Pulse_TIM_Handler()
static unsigned PulseStage = 0; static unsigned PulseStage = 0;
CAN_TxHeaderTypeDef TxHeader; CAN_TxHeaderTypeDef TxHeader;
uint32_t TxMailBox = 0; uint32_t TxMailBox = 0;
union ext_ID currentID; extID currentID;
currentID.BitAll = 0; currentID.BitAll = 0;
currentID.Fields.DeviceID = CURRENT_ID_DEVICE; currentID.StandardFields.DeviceID = CURRENT_ID_DEVICE;
currentID.Fields.DataType = DATA_TYPE_PULSE; currentID.StandardFields.DataType = DATA_TYPE_PULSE;
currentID.Fields.Route = ROUTE_SLAVE; currentID.StandardFields.Route = ROUTE_SLAVE;
TxHeader.ExtId = currentID.BitAll; TxHeader.ExtId = currentID.BitAll;
uint8_t data[8]; uint8_t data[8];
TxHeader.IDE = CAN_ID_EXT; 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) void REQUESTER_RTC_SYNC(uint8_t *data)
{ {
__HAL_RTC_WRITEPROTECTION_DISABLE(&hrtc); __HAL_RTC_WRITEPROTECTION_DISABLE(&hrtc);
@ -558,6 +747,10 @@ void REQUESTER_RTC_SYNC(uint8_t *data)
__HAL_RTC_WRITEPROTECTION_ENABLE(&hrtc); __HAL_RTC_WRITEPROTECTION_ENABLE(&hrtc);
} }
/**
* @brief Функция настройки фильтров CAN.
* @details Настройка фильтров. Фильр для приёма сообщений с главного устройства. Фильтр для приёма на текущее устройство. Фильтр для приёма пульса других устройств в сети.
*/
void REQUESTER_CAN_FILTERS() void REQUESTER_CAN_FILTERS()
{ {
//MAIN DEVICE //MAIN DEVICE
@ -616,4 +809,15 @@ void REQUESTER_CAN_FILTERS()
{ {
Error_Handler(); 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();
}
} }