Use FaSTTUBe CAN Abstraction Layer
This commit is contained in:
@ -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;
|
||||
|
||||
@ -6,6 +6,8 @@
|
||||
#include "ts_state_machine.h"
|
||||
#include "util.h"
|
||||
|
||||
#include "FT_CAN_AL.h"
|
||||
|
||||
ShuntData shunt_data;
|
||||
|
||||
void shunt_init() {
|
||||
@ -25,20 +27,11 @@ void shunt_check() {
|
||||
ts_sm_set_error_source(TS_ERROR_SOURCE_SHUNT, is_error);
|
||||
}
|
||||
|
||||
void shunt_handle_can_msg(CAN_RxHeaderTypeDef *header, uint8_t *data) {
|
||||
void shunt_handle_can_msg(uint16_t id, const uint8_t *data) {
|
||||
// All result messages contain a big-endian 6-byte integer
|
||||
data[7] = 0;
|
||||
data[8] = 0;
|
||||
uint64_t result;
|
||||
memcpy(&result, data, 8);
|
||||
result = ntohll(result);
|
||||
// Top two bytes should always be zero in our usecase (or 0xFF for a signed
|
||||
// number)
|
||||
if ((result >> 4) != 0 && (result >> 4) != 0xFFFF) {
|
||||
set_error_led();
|
||||
}
|
||||
uint64_t result = ftcan_unmarshal_unsigned(&data, 6);
|
||||
|
||||
switch (header->StdId) {
|
||||
switch (id) {
|
||||
case CAN_ID_SHUNT_CURRENT:
|
||||
shunt_data.current = result;
|
||||
break;
|
||||
|
||||
@ -3,6 +3,8 @@
|
||||
#include "main.h"
|
||||
#include "ts_state_machine.h"
|
||||
|
||||
#include "FT_CAN_AL.h"
|
||||
|
||||
#include "stm32f3xx_hal.h"
|
||||
#include "stm32f3xx_hal_gpio.h"
|
||||
|
||||
@ -70,9 +72,10 @@ void slaves_check() {
|
||||
}
|
||||
}
|
||||
|
||||
void slaves_handle_panic(uint8_t *data) {
|
||||
uint8_t slave_id = data[0];
|
||||
switch (data[1]) {
|
||||
void slaves_handle_panic(const uint8_t *data) {
|
||||
uint8_t slave_id = ftcan_unmarshal_unsigned(&data, 1);
|
||||
uint8_t error_kind = ftcan_unmarshal_unsigned(&data, 1);
|
||||
switch (error_kind) {
|
||||
case SLAVE_PANIC_OT:
|
||||
slaves[slave_id].error.kind = SLAVE_ERR_OT;
|
||||
break;
|
||||
@ -86,10 +89,10 @@ void slaves_handle_panic(uint8_t *data) {
|
||||
slaves[slave_id].error.kind = SLAVE_ERR_UV;
|
||||
break;
|
||||
}
|
||||
memcpy(&slaves[slave_id].error.data, &data[2], 4);
|
||||
slaves[slave_id].error.data = ftcan_unmarshal_unsigned(&data, 4);
|
||||
slaves[slave_id].last_message = HAL_GetTick();
|
||||
}
|
||||
|
||||
void slaves_handle_log(uint8_t *data) {
|
||||
void slaves_handle_log(const uint8_t *data) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
@ -194,7 +194,7 @@ void ts_sm_check_close_wait(int *is_closed, int should_close) {
|
||||
}
|
||||
}
|
||||
|
||||
void ts_sm_handle_ams_in(uint8_t *data) {
|
||||
void ts_sm_handle_ams_in(const uint8_t *data) {
|
||||
if (data[0] & 0x01) {
|
||||
ts_state.target_state = TS_ACTIVE;
|
||||
} else {
|
||||
|
||||
@ -5,22 +5,3 @@
|
||||
void set_error_led() {
|
||||
HAL_GPIO_WritePin(STATUS2_GPIO_Port, STATUS2_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
uint64_t ntohll(uint64_t netlonglong) {
|
||||
uint8_t *p = (uint8_t *)&netlonglong;
|
||||
return ((uint64_t)p[0] << 56) | ((uint64_t)p[1] << 48) |
|
||||
((uint64_t)p[2] << 40) | ((uint64_t)p[3] << 32) |
|
||||
((uint64_t)p[4] << 24) | ((uint64_t)p[5] << 16) |
|
||||
((uint64_t)p[6] << 8) | ((uint64_t)p[7] << 0);
|
||||
}
|
||||
|
||||
uint32_t ntohl(uint32_t netlong) {
|
||||
uint8_t *p = (uint8_t *)&netlong;
|
||||
return ((uint32_t)p[3] << 24) | ((uint32_t)p[2] << 16) |
|
||||
((uint32_t)p[1] << 8) | ((uint32_t)p[0] << 0);
|
||||
}
|
||||
|
||||
uint16_t ntohs(uint16_t netshort) {
|
||||
uint8_t *p = (uint8_t *)&netshort;
|
||||
return ((uint16_t)p[1] << 8) | ((uint16_t)p[0] << 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user