Use FaSTTUBe CAN Abstraction Layer

This commit is contained in:
2023-03-15 18:07:38 +01:00
parent 9355595b73
commit e18135558d
14 changed files with 40 additions and 113 deletions

View File

@ -4,57 +4,19 @@
#include "shunt_monitoring.h"
#include "slave_monitoring.h"
#include "soc_estimation.h"
#include "stm32f3xx_hal_can.h"
#include "stm32f3xx_hal_gpio.h"
#include "ts_state_machine.h"
#include "util.h"
#include "FT_CAN_AL.h"
#include <stdint.h>
static CAN_HandleTypeDef *hcan;
void can_init(CAN_HandleTypeDef *handle) {
hcan = handle;
CAN_FilterTypeDef filter;
filter.FilterIdHigh = CAN_ID_SHUNT_BASE & 0xFF0;
filter.FilterMaskIdHigh = 0xFF0;
filter.FilterIdLow = CAN_ID_AMS_IN;
filter.FilterMaskIdLow = 0xFFF;
filter.FilterFIFOAssignment = CAN_FILTER_FIFO0;
filter.FilterBank = 0;
filter.FilterMode = CAN_FILTERMODE_IDMASK;
filter.FilterScale = CAN_FILTERSCALE_16BIT;
if (HAL_CAN_ConfigFilter(hcan, &filter) != HAL_OK) {
Error_Handler();
}
ftcan_init(handle);
ftcan_add_filter(CAN_ID_SHUNT_BASE, 0xFF0);
ftcan_add_filter(CAN_ID_AMS_IN, 0xFFF);
ftcan_add_filter(CAN_ID_SLAVE_PANIC, 0xFFF);
ftcan_add_filter(CAN_ID_SLAVE_LOG, 0xFFF);
// TODO: Slave status?
filter.FilterIdHigh = CAN_ID_SLAVE_PANIC;
filter.FilterMaskIdHigh = 0xFFF;
filter.FilterIdLow = CAN_ID_SLAVE_LOG;
filter.FilterMaskIdLow = 0xFFF;
filter.FilterBank = 1;
if (HAL_CAN_ConfigFilter(hcan, &filter) != HAL_OK) {
Error_Handler();
}
if (HAL_CAN_ActivateNotification(hcan, CAN_IT_RX_FIFO0_MSG_PENDING) !=
HAL_OK) {
Error_Handler();
}
if (HAL_CAN_Start(hcan) != HAL_OK) {
Error_Handler();
}
}
HAL_StatusTypeDef can_transmit(uint8_t id, 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 can_send_status() {
@ -63,30 +25,15 @@ HAL_StatusTypeDef can_send_status() {
data[1] = current_soc;
data[2] = min_voltage >> 8;
data[3] = max_temp >> 8;
return can_transmit(CAN_ID_AMS_STATUS, data, 4);
return ftcan_transmit(CAN_ID_AMS_STATUS, data, 4);
}
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *handle) {
if (handle != hcan) {
void ftcan_msg_received_cb(uint16_t id, size_t datalen, const uint8_t *data) {
if ((id & 0xFF0) == CAN_ID_SHUNT_BASE) {
shunt_handle_can_msg(id, data);
return;
}
CAN_RxHeaderTypeDef header;
uint8_t data[8];
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &header, data) != HAL_OK) {
set_error_led();
return;
}
if (header.IDE != CAN_ID_STD) {
return;
}
if ((header.StdId & 0xFF0) == CAN_ID_SHUNT_BASE) {
shunt_handle_can_msg(&header, data);
return;
}
switch (header.StdId) {
switch (id) {
case CAN_ID_SLAVE_PANIC:
slaves_handle_panic(data);
break;