|
| 1 | +/** |
| 2 | + * @file FEB_CAN_IVT.c |
| 3 | + * @brief IVT (Isabellenhutte) Current/Voltage sensor CAN reception for the PCU |
| 4 | + * @author Formula Electric @ Berkeley |
| 5 | + * |
| 6 | + * Decodes the IVT-S broadcast on CAN1 using the generated feb_can_ivt_*_unpack() |
| 7 | + * functions, exactly like the other PCU CAN packages. The measured pack voltage |
| 8 | + * and current feed the RMS torque/power limiter (FEB_RMS.c). |
| 9 | + */ |
| 10 | + |
| 11 | +#include "FEB_CAN_IVT.h" |
| 12 | +#include "feb_can_lib.h" |
| 13 | +#include "feb_can.h" |
| 14 | +#include "stm32f4xx_hal.h" |
| 15 | +#include "cmsis_compiler.h" |
| 16 | +#include <stddef.h> |
| 17 | + |
| 18 | +/* Note: float reads are atomic on ARM Cortex-M4, so no critical section needed. */ |
| 19 | + |
| 20 | +typedef struct |
| 21 | +{ |
| 22 | + volatile float current_mA; |
| 23 | + volatile float voltage_1_mV; |
| 24 | + volatile float voltage_2_mV; |
| 25 | + volatile float voltage_3_mV; |
| 26 | + volatile uint32_t last_rx_tick; /* 0 = never received */ |
| 27 | +} IVT_Data_t; |
| 28 | + |
| 29 | +static IVT_Data_t ivt_data = {0}; |
| 30 | + |
| 31 | +/* ISR-context callback — no logging or blocking work. */ |
| 32 | +static void FEB_CAN_IVT_Callback(FEB_CAN_Instance_t instance, uint32_t can_id, FEB_CAN_ID_Type_t id_type, |
| 33 | + const uint8_t *data, uint8_t length, void *user_data) |
| 34 | +{ |
| 35 | + (void)instance; |
| 36 | + (void)id_type; |
| 37 | + (void)user_data; |
| 38 | + |
| 39 | + switch (can_id) |
| 40 | + { |
| 41 | + case FEB_CAN_IVT_CURRENT_FRAME_ID: |
| 42 | + { |
| 43 | + struct feb_can_ivt_current_t msg; |
| 44 | + if (feb_can_ivt_current_unpack(&msg, data, length) < 0) |
| 45 | + { |
| 46 | + return; |
| 47 | + } |
| 48 | + /* Current in mA, negate for reversed direction (matches BMS convention) */ |
| 49 | + ivt_data.current_mA = (float)msg.current * (-0.001f) * 1000.0f; |
| 50 | + __DMB(); |
| 51 | + ivt_data.last_rx_tick = HAL_GetTick(); |
| 52 | + break; |
| 53 | + } |
| 54 | + |
| 55 | + case FEB_CAN_IVT_VOLTAGE1_FRAME_ID: |
| 56 | + { |
| 57 | + struct feb_can_ivt_voltage1_t msg; |
| 58 | + if (feb_can_ivt_voltage1_unpack(&msg, data, length) < 0) |
| 59 | + { |
| 60 | + return; |
| 61 | + } |
| 62 | + ivt_data.voltage_1_mV = (float)msg.voltage1; |
| 63 | + __DMB(); |
| 64 | + ivt_data.last_rx_tick = HAL_GetTick(); |
| 65 | + break; |
| 66 | + } |
| 67 | + |
| 68 | + case FEB_CAN_IVT_VOLTAGE2_FRAME_ID: |
| 69 | + { |
| 70 | + struct feb_can_ivt_voltage2_t msg; |
| 71 | + if (feb_can_ivt_voltage2_unpack(&msg, data, length) < 0) |
| 72 | + { |
| 73 | + return; |
| 74 | + } |
| 75 | + ivt_data.voltage_2_mV = (float)msg.voltage2; |
| 76 | + __DMB(); |
| 77 | + ivt_data.last_rx_tick = HAL_GetTick(); |
| 78 | + break; |
| 79 | + } |
| 80 | + |
| 81 | + case FEB_CAN_IVT_VOLTAGE3_FRAME_ID: |
| 82 | + { |
| 83 | + struct feb_can_ivt_voltage3_t msg; |
| 84 | + if (feb_can_ivt_voltage3_unpack(&msg, data, length) < 0) |
| 85 | + { |
| 86 | + return; |
| 87 | + } |
| 88 | + ivt_data.voltage_3_mV = (float)msg.voltage3; |
| 89 | + __DMB(); |
| 90 | + ivt_data.last_rx_tick = HAL_GetTick(); |
| 91 | + break; |
| 92 | + } |
| 93 | + |
| 94 | + default: |
| 95 | + break; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +void FEB_CAN_IVT_Init(void) |
| 100 | +{ |
| 101 | + ivt_data.current_mA = 0.0f; |
| 102 | + ivt_data.voltage_1_mV = 0.0f; |
| 103 | + ivt_data.voltage_2_mV = 0.0f; |
| 104 | + ivt_data.voltage_3_mV = 0.0f; |
| 105 | + ivt_data.last_rx_tick = 0; |
| 106 | + |
| 107 | + /* Single MASK registration covering 0x520-0x527 (IVT frames are 0x521-0x525). |
| 108 | + * One hardware filter bank; the callback ignores anything else in range. */ |
| 109 | + FEB_CAN_RX_Params_t rx_params = { |
| 110 | + .instance = FEB_CAN_INSTANCE_1, |
| 111 | + .can_id = 0x520, |
| 112 | + .id_type = FEB_CAN_ID_STD, |
| 113 | + .filter_type = FEB_CAN_FILTER_MASK, |
| 114 | + .mask = 0x7F8, |
| 115 | + .fifo = FEB_CAN_FIFO_0, |
| 116 | + .callback = FEB_CAN_IVT_Callback, |
| 117 | + .user_data = NULL, |
| 118 | + }; |
| 119 | + FEB_CAN_RX_Register(&rx_params); |
| 120 | +} |
| 121 | + |
| 122 | +float FEB_CAN_IVT_GetVoltage(void) |
| 123 | +{ |
| 124 | + if (!FEB_CAN_IVT_IsDataFresh(FEB_CAN_IVT_DATA_TIMEOUT_MS)) |
| 125 | + { |
| 126 | + return 0.0f; |
| 127 | + } |
| 128 | + |
| 129 | +#if FEB_CAN_IVT_PACK_VOLTAGE_CHANNEL == 1 |
| 130 | + return ivt_data.voltage_1_mV * 0.001f; |
| 131 | +#elif FEB_CAN_IVT_PACK_VOLTAGE_CHANNEL == 2 |
| 132 | + return ivt_data.voltage_2_mV * 0.001f; |
| 133 | +#elif FEB_CAN_IVT_PACK_VOLTAGE_CHANNEL == 3 |
| 134 | + return ivt_data.voltage_3_mV * 0.001f; |
| 135 | +#else |
| 136 | +#error "FEB_CAN_IVT_PACK_VOLTAGE_CHANNEL must be 1, 2, or 3" |
| 137 | +#endif |
| 138 | +} |
| 139 | + |
| 140 | +float FEB_CAN_IVT_GetCurrent(void) |
| 141 | +{ |
| 142 | + return ivt_data.current_mA * 0.001f; |
| 143 | +} |
| 144 | + |
| 145 | +bool FEB_CAN_IVT_IsDataFresh(uint32_t timeout_ms) |
| 146 | +{ |
| 147 | + if (ivt_data.last_rx_tick == 0) |
| 148 | + { |
| 149 | + return false; |
| 150 | + } |
| 151 | + return (HAL_GetTick() - ivt_data.last_rx_tick) < timeout_ms; |
| 152 | +} |
0 commit comments