|
| 1 | +/** |
| 2 | + ****************************************************************************** |
| 3 | + * @file : FEB_CAN_SensorNodes.c |
| 4 | + * @brief : CAN SensorNodes Receiving Module Implementation |
| 5 | + * @author : Formula Electric @ Berkeley |
| 6 | + ****************************************************************************** |
| 7 | + */ |
| 8 | + |
| 9 | +#include "FEB_CAN_LVPDB.h" |
| 10 | +#include "feb_can.h" |
| 11 | +#include "feb_can_lib.h" |
| 12 | +#include "feb_log.h" |
| 13 | +#include "stm32f4xx_hal.h" |
| 14 | +#include <stdbool.h> |
| 15 | +#include <stdint.h> |
| 16 | + |
| 17 | +static int16_t rear_wheel_speed = 0; |
| 18 | +static uint32_t last_rx_tick; |
| 19 | + |
| 20 | +/* ============================================================================ |
| 21 | + * RX Callback Handlers |
| 22 | + * ============================================================================ */ |
| 23 | + |
| 24 | +static void rx_callback_rear_wss(FEB_CAN_Instance_t instance, uint32_t can_id, FEB_CAN_ID_Type_t id_type, |
| 25 | + const uint8_t *data, uint8_t length, void *user_data) |
| 26 | +{ |
| 27 | + struct feb_can_wss_rear_data_t msg; |
| 28 | + if (feb_can_wss_rear_data_unpack(&msg, data, length) == 0) |
| 29 | + { |
| 30 | + rear_wheel_speed = msg.wss_right_rear; |
| 31 | + __DMB(); |
| 32 | + last_rx_tick = HAL_GetTick(); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/* ============================================================================ |
| 37 | + * API Implementation |
| 38 | + * ============================================================================ */ |
| 39 | + |
| 40 | +void FEB_CAN_SensorNodes_Init(void) |
| 41 | +{ |
| 42 | + FEB_CAN_RX_Params_t rx_params = { |
| 43 | + .instance = FEB_CAN_INSTANCE_1, |
| 44 | + .can_id = FEB_CAN_WSS_REAR_DATA_FRAME_ID, |
| 45 | + .id_type = FEB_CAN_ID_STD, |
| 46 | + .filter_type = FEB_CAN_FILTER_EXACT, |
| 47 | + .fifo = FEB_CAN_FIFO_0, |
| 48 | + .callback = rx_callback_rear_wss, |
| 49 | + .user_data = NULL, |
| 50 | + }; |
| 51 | + |
| 52 | + FEB_CAN_RX_Register(&rx_params); |
| 53 | +} |
| 54 | + |
| 55 | +uint16_t FEB_CAN_SensorNodes_GetLastRearWheelSpeed(void) |
| 56 | +{ |
| 57 | + return rear_wheel_speed; |
| 58 | +} |
| 59 | + |
| 60 | +bool FEB_CAN_SensorNodes_IsDataFresh(uint32_t timeout_ms) |
| 61 | +{ |
| 62 | + uint32_t last = last_rx_tick; |
| 63 | + if (last == 0) |
| 64 | + return false; |
| 65 | + return (HAL_GetTick() - last) < timeout_ms; |
| 66 | +} |
0 commit comments