CAN_Request-Response_protocol/Core/Inc/requester.h

286 lines
6.7 KiB
C
Raw 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.

#ifndef _requester
#define _requester
#include "main.h"
#include "can.h"
#include "rtc.h"
#include "tim.h"
#define _DEMO
#define ONLINE 1;
#define OFFLINE 0;
// TIM defines
#define TIM_REQUESTER_Prescaler 64000
//CAN Filter
#define ID_MAIN_DEVICE 0x000
#define CURRENT_ID_DEVICE 0x002
#define CAN_IDE_32 0b00000100 // Для 32-х битного масштаба
#define CAN_DATA_TYPE_FILTER 0x1F000000
#define CAN_DEVICE_ID_FILTER 0x000000FF
#define CAN_SENSOR_TYPE_FILTER 0x00F80000
#define CAN_SENSOR_ID_FILTER 0x0007FF00
union Byte{
struct bitfield{
unsigned bit0:1;
unsigned bit1:1;
unsigned bit2:1;
unsigned bit3:1;
unsigned bit4:1;
unsigned bit5:1;
unsigned bit6:1;
unsigned bit7:1;
}Bitfield;
char AllBit;
};
_Bool IsLeapYear(uint8_t year);
#define COUNT_OF_MODBUS_SECTIONS 1
#define MODBUS_SECTION_MASK 0
#define MODBUS_SECTION_FILTER 1
#define SensorToModbusRegister(SensorType, SensorID) SensorType << 11 | SensorID
#define HighByteOfWord(WORD) (WORD>>8)&0xFF
#define LowByteOfWord(WORD) WORD&0xFF
//Route mode
#define ROUTE_MASTER 0
#define ROUTE_SLAVE 1
//Data Types
#define DATA_TYPE_BROADCAST 0b0000
#define DATA_TYPE_DISCRETE 0b0001
#define DATA_TYPE_ANALOG 0b0010
#define DATA_TYPE_MODBUS_COIL 0b0011
#define DATA_TYPE_MODBUS_DISCRETE 0b0100
#define DATA_TYPE_MODBUS_HOLDING 0b0101
#define DATA_TYPE_MODBUS_INPUT 0b0110
#define DATA_TYPE_ERROR 0b0111
#define DATA_TYPE_PULSE 0b1111
//Sensor Types for DATA_TYPE_BROADCAST
#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_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_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
#define HighIdFilter(x) x<<24
struct controlflags{
unsigned IsPulse:1;
unsigned IsRtrMode:1;
};
//Device settings
/*union ext_ID{
struct ext_ID_fields{
unsigned DeviceID:8;
unsigned SensorID:11;
unsigned SensorType:5;
unsigned DataType:4;
unsigned Route:1;
}Fields;
unsigned int BitAll:29;
};
union ext_ID_Modbus{
struct ext_ID_Modbus_fields{
unsigned DeviceID:8;
unsigned CountReg:8;
unsigned StrAdr:8;
unsigned DataType:4;
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;
extID ExtID;
unsigned TimeFromLastPulse;
unsigned LastPulseStep;
};
struct data
{
struct T_sens{
char info[256];
unsigned SensID;
unsigned AvrgValue;
unsigned LastTenValues[10];
}TS;
struct U_sens{
char info[256];
unsigned SensID;
unsigned AvrgValue;
unsigned LastTenValues[10];
}Uu, Uv, Uw;
struct I_sens{
char info[256];
unsigned SensID;
unsigned AvrgValue;
unsigned LastTenValues[10];
}Iu, Iv, Iw;
};
struct received_request{
extID RequestedExtID;
unsigned RequestedDLC;
uint8_t RxData[8];
//DATA_TYPE_DISCRETIC
union discreticflags{
struct discretictype{
unsigned Request_Accident:1;
unsigned Request_Warning:1;
unsigned Request_Control_Signals:1;
unsigned Request_Flags:1;
unsigned Request_Reset:1;
unsigned Request_List_of_Parameters:1;
}DiscreticType;
unsigned AllFlags:5;
}DiscreticFlags;
//DATA_TYPE_BROADCAST
union broadcastflags{
struct broadcasttype{
unsigned Request_Status:1;
unsigned Request_OnOff:1;
unsigned Request_RTC_Setup:1;
}BroadcastType;
unsigned AllFlags:3;
}BroadcastFlags;
//DATA_TYPE_ANALOG
union analogflags{
struct analogtype{
unsigned Request_Universal_Sens:1;
unsigned Request_U_Sens:1;
unsigned Request_I_Sens:1;
unsigned Request_T_Sens:1;
}AnalogType;
unsigned AllFlags:4;
}AnalogFlags;
//DATA_TYPE_MODBUS
union modbusflags{
struct modbustype{
unsigned Coil:1;
unsigned Discrete:1;
unsigned Holding:1;
unsigned Input:1;
}ModbusType;
unsigned AllFlags:4;
}ModbusFlags;
union sensor_To_Modbus{
struct modbus{
unsigned Count:8;
unsigned StrAdr:8;
}Modbus;
unsigned Sensor:16;
}SensorToModbus;
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