added channel control and can communication (both are working)
This commit is contained in:
33
Software/Code/Core/Inc/can_communication.h
Normal file
33
Software/Code/Core/Inc/can_communication.h
Normal file
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* can_communication.h
|
||||
*
|
||||
* Created on: Mar 12, 2025
|
||||
* Author: janek
|
||||
*/
|
||||
|
||||
#ifndef INC_CAN_COMMUNICATION_H_
|
||||
#define INC_CAN_COMMUNICATION_H_
|
||||
|
||||
#include "channel_control.h"
|
||||
#include "can_halal.h"
|
||||
//#include "current_monitoring.h"
|
||||
|
||||
#define RX_STATUS_HEARTBEAT 0xC7 // IDs of all CAN-packages (may be other IDs next year)
|
||||
#define RX_STATUS_MSG_ID 0xC8 // TODO: check new IDs
|
||||
#define TX_STATUS_MSG_ID 0xC9
|
||||
#define CUR_CHANNELS_1_ID 0xCA
|
||||
#define CUR_CHANNELS_2_ID 0xCB
|
||||
#define CUR_CHANNELS_3_ID 0xCC
|
||||
#define CUR_CHANNELS_4_ID 0xCD
|
||||
#define LV_SENS_ID 0xCE
|
||||
|
||||
typedef struct { // TODO: add error-codes
|
||||
enable_gpios iostatus;
|
||||
uint8_t checksum;
|
||||
} rx_status_frame;
|
||||
|
||||
void can_init(CAN_HandleTypeDef* hcan);
|
||||
void can_rxupdateFrame();
|
||||
void can_sendloop();
|
||||
|
||||
#endif /* INC_CAN_COMMUNICATION_H_ */
|
||||
72
Software/Code/Core/Inc/can_halal.h
Normal file
72
Software/Code/Core/Inc/can_halal.h
Normal file
@ -0,0 +1,72 @@
|
||||
#ifndef CAN_HALAL_H
|
||||
#define CAN_HALAL_H
|
||||
|
||||
// Define family macros if none are defined and we recognize a chip macro
|
||||
#if !defined(STM32F3) && !defined(STM32H7) && !defined(STM32F0)
|
||||
#if defined(STM32F302x6) || defined(STM32F302x8) || defined(STM32F302xB) || \
|
||||
defined(STM32F302xC)
|
||||
#define STM32F3
|
||||
#endif
|
||||
#if defined(STM32H7A3xx) || defined(STM32H723xx)
|
||||
#define STM32H7
|
||||
#endif
|
||||
#if defined(STM32F042x6)
|
||||
#define STM32F0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(STM32F3)
|
||||
#include "stm32f3xx_hal.h"
|
||||
#define FTCAN_IS_BXCAN
|
||||
#define FTCAN_NUM_FILTERS 13
|
||||
#elif defined(STM32H7)
|
||||
#include "stm32h7xx_hal.h"
|
||||
#define FTCAN_IS_FDCAN
|
||||
#ifndef FTCAN_NUM_FILTERS
|
||||
#error "Please configure the number of filters in CubeMX, and then add a compiler define for FTCAN_NUM_FILTERS"
|
||||
#endif
|
||||
#elif defined(STM32F0)
|
||||
#include "stm32f0xx_hal.h"
|
||||
#define FTCAN_IS_BXCAN
|
||||
#define FTCAN_NUM_FILTERS 13
|
||||
#else
|
||||
#error "Couldn't detect STM family"
|
||||
#endif
|
||||
|
||||
#if defined(FTCAN_IS_BXCAN)
|
||||
HAL_StatusTypeDef ftcan_init(CAN_HandleTypeDef *handle);
|
||||
#elif defined(FTCAN_IS_FDCAN)
|
||||
HAL_StatusTypeDef ftcan_init(FDCAN_HandleTypeDef *handle);
|
||||
#else
|
||||
#error "Unknown CAN peripheral"
|
||||
#endif
|
||||
|
||||
HAL_StatusTypeDef ftcan_transmit(uint16_t id, const uint8_t *data,
|
||||
size_t datalen);
|
||||
|
||||
HAL_StatusTypeDef ftcan_add_filter(uint16_t id, uint16_t mask);
|
||||
|
||||
/**
|
||||
* Define this function to be notified of incoming CAN messages
|
||||
*/
|
||||
void ftcan_msg_received_cb(uint16_t id, size_t datalen, const uint8_t *data);
|
||||
|
||||
/**
|
||||
* Read num_bytes bytes from a message (unmarshalled network byte order). The
|
||||
* msg pointer is advanced by the corresponding number of bytes.
|
||||
*
|
||||
* Both methods return a 64-bit integer, but you can safely cast it to a smaller
|
||||
* integer type.
|
||||
*/
|
||||
uint64_t ftcan_unmarshal_unsigned(const uint8_t **data, size_t num_bytes);
|
||||
int64_t ftcan_unmarshal_signed(const uint8_t **data, size_t num_bytes);
|
||||
|
||||
/**
|
||||
* Write num_bytes to a message (marshalled in network byte order). The pointer
|
||||
* is advanced by the corresponding number of bytes and returned.
|
||||
*/
|
||||
uint8_t *ftcan_marshal_unsigned(uint8_t *data, uint64_t val, size_t num_bytes);
|
||||
uint8_t *ftcan_marshal_signed(uint8_t *data, int64_t val, size_t num_bytes);
|
||||
|
||||
#endif // CAN_HALAL_H
|
||||
|
||||
53
Software/Code/Core/Inc/channel_control.h
Normal file
53
Software/Code/Core/Inc/channel_control.h
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* channel_control.h
|
||||
*
|
||||
* Created on: Mar 10, 2025
|
||||
* Author: janek
|
||||
*/
|
||||
|
||||
#ifndef INC_CHANNEL_CONTROL_H_
|
||||
#define INC_CHANNEL_CONTROL_H_
|
||||
|
||||
#include "stm32f3xx_hal.h"
|
||||
|
||||
typedef union {
|
||||
struct {
|
||||
uint8_t acc_cooling : 1;
|
||||
uint8_t ts_cooling : 1;
|
||||
uint8_t drs : 1;
|
||||
uint8_t acu : 1;
|
||||
uint8_t epsc : 1;
|
||||
uint8_t inverter : 1;
|
||||
uint8_t lidar : 1;
|
||||
uint8_t misc : 1;
|
||||
};
|
||||
uint8_t porta;
|
||||
} gpio_port_a;
|
||||
|
||||
typedef union {
|
||||
struct {
|
||||
uint8_t alwayson : 1;
|
||||
uint8_t sdc : 1;
|
||||
uint8_t ebs1 : 1;
|
||||
uint8_t ebs2 : 1;
|
||||
uint8_t ebs3 : 1;
|
||||
uint8_t NC1 : 1; // not used
|
||||
uint8_t NC2 : 1; // not used
|
||||
uint8_t NC3 : 1; // not used
|
||||
};
|
||||
uint8_t portb;
|
||||
} gpio_port_b;
|
||||
|
||||
typedef union {
|
||||
struct {
|
||||
gpio_port_a porta;
|
||||
gpio_port_b portb;
|
||||
};
|
||||
uint16_t gpios_bitmask;
|
||||
} enable_gpios;
|
||||
|
||||
void ChannelControl_init();
|
||||
|
||||
void ChannelControl_UpdateGPIOs(enable_gpios);
|
||||
|
||||
#endif /* INC_CHANNEL_CONTROL_H_ */
|
||||
@ -85,10 +85,10 @@ void Error_Handler(void);
|
||||
#define IS5_GPIO_Port GPIOA
|
||||
#define PC_Read_Pin GPIO_PIN_4
|
||||
#define PC_Read_GPIO_Port GPIOC
|
||||
#define IN11_Pin GPIO_PIN_0
|
||||
#define IN11_GPIO_Port GPIOB
|
||||
#define IN12_Pin GPIO_PIN_1
|
||||
#define IN12_GPIO_Port GPIOB
|
||||
#define IN11_Pin GPIO_PIN_2
|
||||
#define IN11_GPIO_Port GPIOB
|
||||
#define IN13_Pin GPIO_PIN_10
|
||||
#define IN13_GPIO_Port GPIOB
|
||||
#define IN9_Pin GPIO_PIN_11
|
||||
|
||||
@ -57,6 +57,8 @@ void PendSV_Handler(void);
|
||||
void SysTick_Handler(void);
|
||||
void DMA1_Channel1_IRQHandler(void);
|
||||
void ADC1_2_IRQHandler(void);
|
||||
void USB_LP_CAN_RX0_IRQHandler(void);
|
||||
void CAN_RX1_IRQHandler(void);
|
||||
void TIM6_DAC_IRQHandler(void);
|
||||
void DMA2_Channel1_IRQHandler(void);
|
||||
/* USER CODE BEGIN EFP */
|
||||
|
||||
48
Software/Code/Core/Src/can_communication.c
Normal file
48
Software/Code/Core/Src/can_communication.c
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* can_communication.c
|
||||
*
|
||||
* Created on: Mar 12, 2025
|
||||
* Author: janek
|
||||
*/
|
||||
|
||||
#include "can_communication.h"
|
||||
#include "channel_control.h"
|
||||
//#include "current_monitoring.h"
|
||||
|
||||
rx_status_frame rxstate = {};
|
||||
volatile uint8_t canmsg_received = 0;
|
||||
extern enable_gpios update_ports;
|
||||
|
||||
|
||||
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);
|
||||
// TODO: implement transmission of current and voltage measurements
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
273
Software/Code/Core/Src/can_halal.c
Normal file
273
Software/Code/Core/Src/can_halal.c
Normal file
@ -0,0 +1,273 @@
|
||||
#include "can_halal.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined(FTCAN_IS_BXCAN)
|
||||
static CAN_HandleTypeDef *hcan;
|
||||
|
||||
HAL_StatusTypeDef ftcan_init(CAN_HandleTypeDef *handle) {
|
||||
hcan = handle;
|
||||
|
||||
HAL_StatusTypeDef status =
|
||||
HAL_CAN_ActivateNotification(hcan, CAN_IT_RX_FIFO0_MSG_PENDING);
|
||||
if (status != HAL_OK) {
|
||||
return status;
|
||||
}
|
||||
|
||||
return HAL_CAN_Start(hcan);
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef ftcan_transmit(uint16_t id, const uint8_t *data,
|
||||
size_t datalen) {
|
||||
static CAN_TxHeaderTypeDef header;
|
||||
header.StdId = id;
|
||||
header.IDE = CAN_ID_STD;
|
||||
header.RTR = CAN_RTR_DATA;
|
||||
header.DLC = datalen;
|
||||
uint32_t mailbox;
|
||||
return HAL_CAN_AddTxMessage(hcan, &header, data, &mailbox);
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef ftcan_add_filter(uint16_t id, uint16_t mask) {
|
||||
static uint32_t next_filter_no = 0;
|
||||
static CAN_FilterTypeDef filter;
|
||||
if (next_filter_no % 2 == 0) {
|
||||
filter.FilterIdHigh = id << 5;
|
||||
filter.FilterMaskIdHigh = mask << 5;
|
||||
filter.FilterIdLow = id << 5;
|
||||
filter.FilterMaskIdLow = mask << 5;
|
||||
} else {
|
||||
// Leave high filter untouched from the last configuration
|
||||
filter.FilterIdLow = id << 5;
|
||||
filter.FilterMaskIdLow = mask << 5;
|
||||
}
|
||||
filter.FilterFIFOAssignment = CAN_FILTER_FIFO0;
|
||||
filter.FilterBank = next_filter_no / 2;
|
||||
if (filter.FilterBank > FTCAN_NUM_FILTERS + 1) {
|
||||
return HAL_ERROR;
|
||||
}
|
||||
filter.FilterMode = CAN_FILTERMODE_IDMASK;
|
||||
filter.FilterScale = CAN_FILTERSCALE_16BIT;
|
||||
filter.FilterActivation = CAN_FILTER_ENABLE;
|
||||
|
||||
// Disable slave filters
|
||||
// TODO: Some STM32 have multiple CAN peripherals, and one uses the slave
|
||||
// filter bank
|
||||
filter.SlaveStartFilterBank = FTCAN_NUM_FILTERS;
|
||||
|
||||
HAL_StatusTypeDef status = HAL_CAN_ConfigFilter(hcan, &filter);
|
||||
if (status == HAL_OK) {
|
||||
next_filter_no++;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *handle) {
|
||||
if (handle != hcan) {
|
||||
return;
|
||||
}
|
||||
CAN_RxHeaderTypeDef header;
|
||||
uint8_t data[8];
|
||||
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &header, data) != HAL_OK) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (header.IDE != CAN_ID_STD) {
|
||||
return;
|
||||
}
|
||||
|
||||
ftcan_msg_received_cb(header.StdId, header.DLC, data);
|
||||
}
|
||||
#elif defined(FTCAN_IS_FDCAN)
|
||||
static FDCAN_HandleTypeDef *hcan;
|
||||
|
||||
HAL_StatusTypeDef ftcan_init(FDCAN_HandleTypeDef *handle) {
|
||||
hcan = handle;
|
||||
|
||||
HAL_StatusTypeDef status =
|
||||
HAL_FDCAN_ActivateNotification(hcan, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0);
|
||||
if (status != HAL_OK) {
|
||||
return status;
|
||||
}
|
||||
// Reject non-matching messages
|
||||
status =
|
||||
HAL_FDCAN_ConfigGlobalFilter(hcan, FDCAN_REJECT, FDCAN_REJECT,
|
||||
FDCAN_REJECT_REMOTE, FDCAN_REJECT_REMOTE);
|
||||
if (status != HAL_OK) {
|
||||
return status;
|
||||
}
|
||||
|
||||
return HAL_FDCAN_Start(hcan);
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef ftcan_transmit(uint16_t id, const uint8_t *data,
|
||||
size_t datalen) {
|
||||
static FDCAN_TxHeaderTypeDef header;
|
||||
header.Identifier = id;
|
||||
header.IdType = FDCAN_STANDARD_ID;
|
||||
header.TxFrameType = FDCAN_DATA_FRAME;
|
||||
switch (datalen) {
|
||||
case 0:
|
||||
header.DataLength = FDCAN_DLC_BYTES_0;
|
||||
break;
|
||||
case 1:
|
||||
header.DataLength = FDCAN_DLC_BYTES_1;
|
||||
break;
|
||||
case 2:
|
||||
header.DataLength = FDCAN_DLC_BYTES_2;
|
||||
break;
|
||||
case 3:
|
||||
header.DataLength = FDCAN_DLC_BYTES_3;
|
||||
break;
|
||||
case 4:
|
||||
header.DataLength = FDCAN_DLC_BYTES_4;
|
||||
break;
|
||||
case 5:
|
||||
header.DataLength = FDCAN_DLC_BYTES_5;
|
||||
break;
|
||||
case 6:
|
||||
header.DataLength = FDCAN_DLC_BYTES_6;
|
||||
break;
|
||||
case 7:
|
||||
header.DataLength = FDCAN_DLC_BYTES_7;
|
||||
break;
|
||||
case 8:
|
||||
default:
|
||||
header.DataLength = FDCAN_DLC_BYTES_8;
|
||||
break;
|
||||
}
|
||||
header.ErrorStateIndicator = FDCAN_ESI_PASSIVE;
|
||||
header.BitRateSwitch = FDCAN_BRS_OFF;
|
||||
header.FDFormat = FDCAN_CLASSIC_CAN;
|
||||
header.TxEventFifoControl = FDCAN_NO_TX_EVENTS;
|
||||
|
||||
// HAL_FDCAN_AddMessageToTxFifoQ doesn't modify the data, but it's not marked
|
||||
// as const for some reason.
|
||||
uint8_t *data_nonconst = (uint8_t *)data;
|
||||
return HAL_FDCAN_AddMessageToTxFifoQ(hcan, &header, data_nonconst);
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef ftcan_add_filter(uint16_t id, uint16_t mask) {
|
||||
static uint32_t next_filter_no = 0;
|
||||
static FDCAN_FilterTypeDef filter;
|
||||
filter.IdType = FDCAN_STANDARD_ID;
|
||||
filter.FilterIndex = next_filter_no;
|
||||
if (filter.FilterIndex > FTCAN_NUM_FILTERS + 1) {
|
||||
return HAL_ERROR;
|
||||
}
|
||||
filter.FilterType = FDCAN_FILTER_MASK;
|
||||
filter.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
|
||||
filter.FilterID1 = id;
|
||||
filter.FilterID2 = mask;
|
||||
|
||||
HAL_StatusTypeDef status = HAL_FDCAN_ConfigFilter(hcan, &filter);
|
||||
if (status == HAL_OK) {
|
||||
next_filter_no++;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *handle,
|
||||
uint32_t RxFifo0ITs) {
|
||||
if (handle != hcan || (RxFifo0ITs & FDCAN_IT_RX_FIFO0_NEW_MESSAGE) == RESET) {
|
||||
return;
|
||||
}
|
||||
|
||||
static FDCAN_RxHeaderTypeDef header;
|
||||
static uint8_t data[8];
|
||||
if (HAL_FDCAN_GetRxMessage(hcan, FDCAN_RX_FIFO0, &header, data) != HAL_OK) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (header.FDFormat != FDCAN_CLASSIC_CAN ||
|
||||
header.RxFrameType != FDCAN_DATA_FRAME ||
|
||||
header.IdType != FDCAN_STANDARD_ID) {
|
||||
return;
|
||||
}
|
||||
|
||||
size_t datalen;
|
||||
switch (header.DataLength) {
|
||||
case FDCAN_DLC_BYTES_0:
|
||||
datalen = 0;
|
||||
break;
|
||||
case FDCAN_DLC_BYTES_1:
|
||||
datalen = 1;
|
||||
break;
|
||||
case FDCAN_DLC_BYTES_2:
|
||||
datalen = 2;
|
||||
break;
|
||||
case FDCAN_DLC_BYTES_3:
|
||||
datalen = 3;
|
||||
break;
|
||||
case FDCAN_DLC_BYTES_4:
|
||||
datalen = 4;
|
||||
break;
|
||||
case FDCAN_DLC_BYTES_5:
|
||||
datalen = 5;
|
||||
break;
|
||||
case FDCAN_DLC_BYTES_6:
|
||||
datalen = 6;
|
||||
break;
|
||||
case FDCAN_DLC_BYTES_7:
|
||||
datalen = 7;
|
||||
break;
|
||||
case FDCAN_DLC_BYTES_8:
|
||||
datalen = 8;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
ftcan_msg_received_cb(header.Identifier, datalen, data);
|
||||
}
|
||||
#endif
|
||||
|
||||
__weak void ftcan_msg_received_cb(uint16_t id, size_t datalen,
|
||||
const uint8_t *data) {}
|
||||
|
||||
uint64_t ftcan_unmarshal_unsigned(const uint8_t **data_ptr, size_t num_bytes) {
|
||||
if (num_bytes > 8) {
|
||||
num_bytes = 8;
|
||||
}
|
||||
|
||||
const uint8_t *data = *data_ptr;
|
||||
uint64_t result = 0;
|
||||
for (size_t i = 0; i < num_bytes; i++) {
|
||||
result <<= 8;
|
||||
result |= data[i];
|
||||
}
|
||||
*data_ptr += num_bytes;
|
||||
return result;
|
||||
}
|
||||
|
||||
int64_t ftcan_unmarshal_signed(const uint8_t **data_ptr, size_t num_bytes) {
|
||||
if (num_bytes > 8) {
|
||||
num_bytes = 8;
|
||||
}
|
||||
|
||||
uint64_t result_unsigned = ftcan_unmarshal_unsigned(data_ptr, num_bytes);
|
||||
// Sign extend by shifting left, then copying to a signed int and shifting
|
||||
// back to the right
|
||||
size_t diff_to_64 = 64 - num_bytes * 8;
|
||||
result_unsigned <<= diff_to_64;
|
||||
int64_t result;
|
||||
memcpy(&result, &result_unsigned, 8);
|
||||
return result >> diff_to_64;
|
||||
}
|
||||
|
||||
uint8_t *ftcan_marshal_unsigned(uint8_t *data, uint64_t val, size_t num_bytes) {
|
||||
if (num_bytes > 8) {
|
||||
num_bytes = 8;
|
||||
}
|
||||
|
||||
for (int i = num_bytes - 1; i >= 0; i--) {
|
||||
data[i] = val & 0xFF;
|
||||
val >>= 8;
|
||||
}
|
||||
|
||||
return data + num_bytes;
|
||||
}
|
||||
|
||||
uint8_t *ftcan_marshal_signed(uint8_t *data, int64_t val, size_t num_bytes) {
|
||||
return ftcan_marshal_unsigned(data, val, num_bytes);
|
||||
}
|
||||
53
Software/Code/Core/Src/channel_control.c
Normal file
53
Software/Code/Core/Src/channel_control.c
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* channel_control.c
|
||||
*
|
||||
* Created on: Mar 10, 2025
|
||||
* Author: janek
|
||||
*/
|
||||
|
||||
#include "channel_control.h"
|
||||
#include "main.h"
|
||||
|
||||
volatile enable_gpios enable;
|
||||
|
||||
extern int inhibit_SDC;
|
||||
extern int prev_epsc_state;
|
||||
|
||||
void ChannelControl_init(){
|
||||
enable.porta.porta = 0;
|
||||
enable.portb.portb = 0;
|
||||
enable.portb.alwayson = 1;
|
||||
ChannelControl_UpdateGPIOs(enable);
|
||||
}
|
||||
|
||||
void ChannelControl_UpdateGPIOs(enable_gpios UpdatePorts){
|
||||
UpdatePorts.portb.alwayson = 1;
|
||||
if (inhibit_SDC == 1){
|
||||
UpdatePorts.portb.sdc = 0;
|
||||
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, 1);
|
||||
}
|
||||
HAL_GPIO_WritePin(IN1_GPIO_Port, IN1_Pin, (GPIO_PinState)UpdatePorts.porta.acc_cooling); // Acc-Cooling
|
||||
HAL_GPIO_WritePin(IN2_GPIO_Port, IN2_Pin, (GPIO_PinState)UpdatePorts.porta.ts_cooling); // TS-Cooling
|
||||
HAL_GPIO_WritePin(IN3_GPIO_Port, IN3_Pin, (GPIO_PinState)UpdatePorts.porta.drs); // DRS
|
||||
HAL_GPIO_WritePin(IN4_GPIO_Port, IN4_Pin, (GPIO_PinState)UpdatePorts.porta.acu); // ACU
|
||||
if (prev_epsc_state == 0 && ((UpdatePorts.porta.porta >> 4) & 1) == 1){ // will be replaced by precharge
|
||||
HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, 1); // precharge activate
|
||||
HAL_Delay(2000); // contiuosly read precharge voltage
|
||||
HAL_GPIO_WritePin(IN5_GPIO_Port, IN5_Pin, (GPIO_PinState)UpdatePorts.porta.epsc); // if precharge voltage > 95% 24V enable PROFET
|
||||
HAL_Delay(100); // after few ms disengage precharge
|
||||
HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, 0);
|
||||
prev_epsc_state = UpdatePorts.porta.epsc;
|
||||
}
|
||||
else {
|
||||
HAL_GPIO_WritePin(IN5_GPIO_Port, IN5_Pin, (GPIO_PinState)UpdatePorts.porta.epsc);
|
||||
prev_epsc_state = UpdatePorts.porta.epsc;
|
||||
}
|
||||
HAL_GPIO_WritePin(IN6_GPIO_Port, IN6_Pin, (GPIO_PinState)UpdatePorts.porta.inverter); // inverter
|
||||
HAL_GPIO_WritePin(IN7_GPIO_Port, IN7_Pin, (GPIO_PinState)UpdatePorts.porta.lidar); // lidar
|
||||
HAL_GPIO_WritePin(IN8_GPIO_Port, IN8_Pin, (GPIO_PinState)UpdatePorts.porta.misc); // MISC
|
||||
HAL_GPIO_WritePin(IN9_GPIO_Port, IN9_Pin, (GPIO_PinState)UpdatePorts.portb.alwayson); // always on -> standardmäßig auf HIGH forcen
|
||||
HAL_GPIO_WritePin(IN10_GPIO_Port, IN10_Pin, (GPIO_PinState)UpdatePorts.portb.sdc); // SDC -> muss anders controlled werden
|
||||
HAL_GPIO_WritePin(IN11_GPIO_Port, IN11_Pin, (GPIO_PinState)UpdatePorts.portb.ebs1); // EBS 1
|
||||
HAL_GPIO_WritePin(IN12_GPIO_Port, IN12_Pin, (GPIO_PinState)UpdatePorts.portb.ebs2); // EBS 2
|
||||
HAL_GPIO_WritePin(IN13_GPIO_Port, IN13_Pin, (GPIO_PinState)UpdatePorts.portb.ebs3); // EBS 3
|
||||
}
|
||||
@ -21,7 +21,8 @@
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
#include "can_communication.h"
|
||||
#include "channel_control.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
@ -70,7 +71,15 @@ static void MX_TIM6_Init(void);
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
// adc buffer
|
||||
|
||||
extern rx_status_frame rxstate;
|
||||
extern volatile uint8_t canmsg_received;
|
||||
|
||||
volatile enable_gpios update_ports;
|
||||
uint32_t lastheartbeat;
|
||||
int inhibit_SDC; // wenn =1 ist es unmoeglich den SDC zu schliessen
|
||||
volatile int prev_epsc_state; // used for precharge
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
@ -109,6 +118,38 @@ int main(void)
|
||||
MX_UART4_Init();
|
||||
MX_TIM6_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
// begin start-up animation
|
||||
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
|
||||
HAL_Delay(100);
|
||||
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET);
|
||||
HAL_Delay(100);
|
||||
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, GPIO_PIN_SET);
|
||||
HAL_Delay(100);
|
||||
HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(LED4_GPIO_Port, LED4_Pin, GPIO_PIN_SET);
|
||||
HAL_Delay(100);
|
||||
HAL_GPIO_WritePin(LED4_GPIO_Port, LED3_Pin, GPIO_PIN_SET);
|
||||
HAL_Delay(100);
|
||||
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET);
|
||||
HAL_Delay(100);
|
||||
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
|
||||
HAL_Delay(100);
|
||||
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(LED4_GPIO_Port, LED4_Pin, GPIO_PIN_RESET);
|
||||
// end start-up animation
|
||||
HAL_GPIO_WritePin(LED4_GPIO_Port, LED4_Pin, GPIO_PIN_SET); // indicates running STM
|
||||
|
||||
ChannelControl_init();
|
||||
can_init(&hcan);
|
||||
// currentMonitor initialisieren
|
||||
|
||||
uint32_t lasttick = HAL_GetTick(); // Zeit in ms seit Start
|
||||
|
||||
inhibit_SDC = 0;
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
@ -119,6 +160,18 @@ int main(void)
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
if (canmsg_received){
|
||||
canmsg_received = 0;
|
||||
update_ports = rxstate.iostatus;
|
||||
}
|
||||
if ((HAL_GetTick() - lasttick) > 100u){
|
||||
lasttick = HAL_GetTick();
|
||||
can_sendloop();
|
||||
}
|
||||
//watchdog (auch Status-LED an schalten)
|
||||
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, (GPIO_PinState)!update_ports.portb.sdc); // indicates open SDC
|
||||
// overcurrent check (wenn funktioniert, LED schalten)
|
||||
ChannelControl_UpdateGPIOs(update_ports);
|
||||
}
|
||||
/* USER CODE END 3 */
|
||||
}
|
||||
@ -406,7 +459,7 @@ static void MX_CAN_Init(void)
|
||||
|
||||
/* USER CODE END CAN_Init 1 */
|
||||
hcan.Instance = CAN;
|
||||
hcan.Init.Prescaler = 1;
|
||||
hcan.Init.Prescaler = 2;
|
||||
hcan.Init.Mode = CAN_MODE_NORMAL;
|
||||
hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
|
||||
hcan.Init.TimeSeg1 = CAN_BS1_13TQ;
|
||||
@ -538,7 +591,7 @@ static void MX_GPIO_Init(void)
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOB, IN11_Pin|IN12_Pin|IN13_Pin|IN9_Pin
|
||||
HAL_GPIO_WritePin(GPIOB, IN12_Pin|IN11_Pin|IN13_Pin|IN9_Pin
|
||||
|IN3_Pin|IN8_Pin|IN5_Pin|IN4_Pin
|
||||
|DSEL0_Pin|DSEL1_Pin|PC_EN_Pin|IN7_Pin
|
||||
|IN10_Pin, GPIO_PIN_RESET);
|
||||
@ -549,11 +602,11 @@ static void MX_GPIO_Init(void)
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOA, IN2_Pin|IN1_Pin|IN6_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pins : IN11_Pin IN12_Pin IN13_Pin IN9_Pin
|
||||
/*Configure GPIO pins : IN12_Pin IN11_Pin IN13_Pin IN9_Pin
|
||||
IN3_Pin IN8_Pin IN5_Pin IN4_Pin
|
||||
DSEL0_Pin DSEL1_Pin PC_EN_Pin IN7_Pin
|
||||
IN10_Pin */
|
||||
GPIO_InitStruct.Pin = IN11_Pin|IN12_Pin|IN13_Pin|IN9_Pin
|
||||
GPIO_InitStruct.Pin = IN12_Pin|IN11_Pin|IN13_Pin|IN9_Pin
|
||||
|IN3_Pin|IN8_Pin|IN5_Pin|IN4_Pin
|
||||
|DSEL0_Pin|DSEL1_Pin|PC_EN_Pin|IN7_Pin
|
||||
|IN10_Pin;
|
||||
|
||||
@ -334,6 +334,11 @@ void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
|
||||
GPIO_InitStruct.Alternate = GPIO_AF9_CAN;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/* CAN interrupt Init */
|
||||
HAL_NVIC_SetPriority(USB_LP_CAN_RX0_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(USB_LP_CAN_RX0_IRQn);
|
||||
HAL_NVIC_SetPriority(CAN_RX1_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(CAN_RX1_IRQn);
|
||||
/* USER CODE BEGIN CAN_MspInit 1 */
|
||||
|
||||
/* USER CODE END CAN_MspInit 1 */
|
||||
@ -363,6 +368,9 @@ void HAL_CAN_MspDeInit(CAN_HandleTypeDef* hcan)
|
||||
*/
|
||||
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);
|
||||
|
||||
/* CAN interrupt DeInit */
|
||||
HAL_NVIC_DisableIRQ(USB_LP_CAN_RX0_IRQn);
|
||||
HAL_NVIC_DisableIRQ(CAN_RX1_IRQn);
|
||||
/* USER CODE BEGIN CAN_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END CAN_MspDeInit 1 */
|
||||
|
||||
@ -59,6 +59,7 @@ extern DMA_HandleTypeDef hdma_adc1;
|
||||
extern DMA_HandleTypeDef hdma_adc2;
|
||||
extern ADC_HandleTypeDef hadc1;
|
||||
extern ADC_HandleTypeDef hadc2;
|
||||
extern CAN_HandleTypeDef hcan;
|
||||
extern TIM_HandleTypeDef htim6;
|
||||
/* USER CODE BEGIN EV */
|
||||
|
||||
@ -231,6 +232,34 @@ void ADC1_2_IRQHandler(void)
|
||||
/* USER CODE END ADC1_2_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles USB low priority or CAN_RX0 interrupts.
|
||||
*/
|
||||
void USB_LP_CAN_RX0_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN USB_LP_CAN_RX0_IRQn 0 */
|
||||
|
||||
/* USER CODE END USB_LP_CAN_RX0_IRQn 0 */
|
||||
HAL_CAN_IRQHandler(&hcan);
|
||||
/* USER CODE BEGIN USB_LP_CAN_RX0_IRQn 1 */
|
||||
|
||||
/* USER CODE END USB_LP_CAN_RX0_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles CAN RX1 interrupt.
|
||||
*/
|
||||
void CAN_RX1_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN CAN_RX1_IRQn 0 */
|
||||
|
||||
/* USER CODE END CAN_RX1_IRQn 0 */
|
||||
HAL_CAN_IRQHandler(&hcan);
|
||||
/* USER CODE BEGIN CAN_RX1_IRQn 1 */
|
||||
|
||||
/* USER CODE END CAN_RX1_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles Timer 6 interrupt and DAC underrun interrupts.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user