pdu/Software/Code/Core/Src/can_communication.c

115 lines
3.5 KiB
C

/*
* can_communication.c
*
* Created on: Mar 12, 2025
* Author: janek
*/
#include "can_communication.h"
#include "channel_control.h"
#include "current_monitoring.h"
#include "plausibility_check.h"
rx_status_frame rxstate = {};
volatile uint8_t canmsg_received = 0;
extern enable_gpios update_ports;
extern current_measurements current_measurements_adc_val;
extern err_states error;
extern uint32_t lastheartbeat;
extern int inhibit_SDC;
void can_init(CAN_HandleTypeDef* hcan){
ftcan_init(hcan);
ftcan_add_filter(0x00, 0x00); // no filter
}
void can_sendloop(){
static uint8_t additionaltxcounter = 0;
uint8_t status_data[3];
status_data[0] = update_ports.porta.porta;
status_data[1] = update_ports.portb.portb;
status_data[2] = !inhibit_SDC;
ftcan_transmit(TX_STATUS_MSG_ID, status_data, 3);
uint8_t data[8];
switch (additionaltxcounter) {
case 0:
data[0] = current_measurements_adc_val.alwayson >> 8;
data[1] = current_measurements_adc_val.alwayson & 0xFF;
data[2] = current_measurements_adc_val.misc >> 8;
data[3] = current_measurements_adc_val.misc & 0xFF;
data[4] = current_measurements_adc_val.inverter >> 8;
data[5] = current_measurements_adc_val.inverter & 0xFF;
data[6] = current_measurements_adc_val.sdc >> 8;
data[7] = current_measurements_adc_val.sdc & 0xFF;
ftcan_transmit(CUR_CHANNELS_1_ID, data, 8);
break;
case 1:
data[0] = current_measurements_adc_val.acc_cooling >> 8;
data[1] = current_measurements_adc_val.acc_cooling & 0xFF;
data[2] = current_measurements_adc_val.ts_cooling >> 8;
data[3] = current_measurements_adc_val.ts_cooling & 0xFF;
data[4] = current_measurements_adc_val.acu >> 8;
data[5] = current_measurements_adc_val.acu & 0xFF;
data[6] = current_measurements_adc_val.epsc >> 8;
data[7] = current_measurements_adc_val.epsc & 0xFF;
ftcan_transmit(CUR_CHANNELS_2_ID, data, 8);
break;
case 2:
data[0] = current_measurements_adc_val.ebs1 >> 8;
data[1] = current_measurements_adc_val.ebs1 & 0xFF;
data[2] = current_measurements_adc_val.ebs2 >> 8;
data[3] = current_measurements_adc_val.ebs2 & 0xFF;
data[4] = current_measurements_adc_val.ebs3 >> 8;
data[5] = current_measurements_adc_val.ebs3 & 0xFF;
data[6] = current_measurements_adc_val.drs >> 8;
data[7] = current_measurements_adc_val.drs & 0xFF;
ftcan_transmit(CUR_CHANNELS_3_ID, data, 8);
break;
case 3:
data[0] = current_measurements_adc_val.lidar >> 8;
data[1] = current_measurements_adc_val.lidar & 0xFF;
data[2] = current_measurements_adc_val.lvms_v >> 8;
data[3] = current_measurements_adc_val.lvms_v & 0xFF;
data[4] = current_measurements_adc_val.asms_v >> 8;
data[5] = current_measurements_adc_val.asms_v & 0xFF;
data[6] = current_measurements_adc_val.epsc_precharge >> 8; // not used (transmits 313)
data[7] = current_measurements_adc_val.epsc_precharge & 0xFF; // not used (transmits 313)
ftcan_transmit(CUR_CHANNELS_4_ID, data, 8);
break;
default:
break;
}
additionaltxcounter = (additionaltxcounter + 1) % 4;
}
void can_error_report(){
uint8_t error_data[2];
error_data[0] = error.group1.group1;
error_data[1] = error.group2.group2;
ftcan_transmit(ERROR_ID, error_data, 2);
}
void ftcan_msg_received_cb(uint16_t id, size_t datalen, const uint8_t* data){
canmsg_received = 1;
if((id == RX_STATUS_MSG_ID) && (datalen == 3)){
rxstate.iostatus.porta.porta = data[0];
rxstate.iostatus.portb.portb = data[1];
rxstate.checksum = data[2];
}
if (id == RX_STATUS_HEARTBEAT){
lastheartbeat = HAL_GetTick();
inhibit_SDC = 0;
}
}