Add CAN Filter and Watchdog functionality, theoretically complete
This commit is contained in:
@ -57,7 +57,7 @@ typedef enum {
|
||||
*/
|
||||
|
||||
typedef union {
|
||||
uint8_t raw[1];
|
||||
uint8_t raw[8]; // Must be 8 bytes because HAL always writes 8 bytes
|
||||
struct {
|
||||
// BITFIELDS ARE LSB FIRST!
|
||||
bool as_close_sdc : 1;
|
||||
@ -118,6 +118,9 @@ typedef union {
|
||||
|
||||
#define AMI_GPIO_Port GPIOB
|
||||
|
||||
//#define WATCHDOG_UCC
|
||||
#define WATCHDOG_STM
|
||||
|
||||
/* USER CODE END PD */
|
||||
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
@ -128,6 +131,8 @@ typedef union {
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
CAN_HandleTypeDef hcan;
|
||||
|
||||
IWDG_HandleTypeDef hiwdg;
|
||||
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
// Mission Maps: NONE ACCEL SKIDPAD TRACKDRIVE EBSTEST INSPECTION AUTOX MANUAL
|
||||
@ -136,12 +141,18 @@ const mission_t mission2next[] = {M_MANUAL , M_SKIDPAD , M_AUTOX , M_EBSTEST
|
||||
|
||||
mission_t mission = M_NONE;
|
||||
|
||||
#ifdef WATCHDOG_STM
|
||||
bool pHeartbeat = false;
|
||||
bool WD_OK = false;
|
||||
#endif
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_CAN_Init(void);
|
||||
static void MX_IWDG_Init(void);
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
@ -184,20 +195,23 @@ int main(void)
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_CAN_Init();
|
||||
MX_IWDG_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
// Ensure we start with SDC disabled
|
||||
HAL_GPIO_WritePin(Watchdog_GPIO_Port, Watchdog_Pin, GPIO_PIN_RESET);
|
||||
|
||||
if (HAL_CAN_Start(&hcan) != HAL_OK)
|
||||
Error_Handler();
|
||||
|
||||
// TODO: Use Filter
|
||||
CAN_FilterTypeDef canfilterconfig;
|
||||
|
||||
canfilterconfig.FilterActivation = CAN_FILTER_ENABLE;
|
||||
canfilterconfig.FilterBank = 0;
|
||||
canfilterconfig.FilterFIFOAssignment = CAN_FILTER_FIFO0;
|
||||
canfilterconfig.FilterIdHigh = 0;
|
||||
canfilterconfig.FilterIdHigh = 0 << (16 - 11);
|
||||
canfilterconfig.FilterIdLow = 0;
|
||||
canfilterconfig.FilterMaskIdHigh = 0;
|
||||
canfilterconfig.FilterMaskIdHigh = 0x7FF << (16 - 11);
|
||||
canfilterconfig.FilterMaskIdLow = 0;
|
||||
canfilterconfig.FilterMode = CAN_FILTERMODE_IDMASK;
|
||||
canfilterconfig.FilterScale = CAN_FILTERSCALE_32BIT;
|
||||
@ -230,13 +244,16 @@ int main(void)
|
||||
bool pAMC = false;
|
||||
mission_t new_mission = mission; // By default, don't change mission
|
||||
|
||||
// Important to prevent bus error state while ABX is starting up
|
||||
HAL_Delay(1000);
|
||||
|
||||
while (true) {
|
||||
|
||||
bool TS_activate_MUXed = HAL_GPIO_ReadPin(TS_activate_MUXed_GPIO_Port, TS_activate_MUXed_Pin) == GPIO_PIN_RESET;
|
||||
bool ASMS = HAL_GPIO_ReadPin(ASMS_GPIO_Port, ASMS_Pin) == GPIO_PIN_RESET;
|
||||
//bool WD_OK = HAL_GPIO_ReadPin(WD_OK_GPIO_Port, WD_OK_Pin) == GPIO_PIN_RESET;
|
||||
#ifdef WATCHDOG_UCC
|
||||
bool WD_OK = HAL_GPIO_ReadPin(WD_OK_GPIO_Port, WD_OK_Pin) == GPIO_PIN_RESET;
|
||||
#endif
|
||||
bool SDC_is_ready = HAL_GPIO_ReadPin(SDC_is_ready_GPIO_Port, SDC_is_ready_Pin) == GPIO_PIN_SET;
|
||||
bool SDC_in_3V3 = HAL_GPIO_ReadPin(SDC_in_3V3_GPIO_Port, SDC_in_3V3_Pin) == GPIO_PIN_SET;
|
||||
bool LV_SENSE_1 = HAL_GPIO_ReadPin(LV_SENSE_1_GPIO_Port, LV_SENSE_1_Pin) == GPIO_PIN_SET;
|
||||
@ -258,9 +275,6 @@ int main(void)
|
||||
|
||||
}
|
||||
|
||||
// TODO: Depends on STM watchdog
|
||||
bool WD_OK = true;
|
||||
|
||||
txData = (tx_data_t) {
|
||||
.signals = {
|
||||
.asms_state = ASMS,
|
||||
@ -304,9 +318,10 @@ void SystemClock_Config(void)
|
||||
/** Initializes the RCC Oscillators according to the specified parameters
|
||||
* in the RCC_OscInitTypeDef structure.
|
||||
*/
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
|
||||
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
||||
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
|
||||
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
|
||||
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
||||
{
|
||||
@ -365,6 +380,35 @@ static void MX_CAN_Init(void)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief IWDG Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_IWDG_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN IWDG_Init 0 */
|
||||
|
||||
/* USER CODE END IWDG_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN IWDG_Init 1 */
|
||||
|
||||
/* USER CODE END IWDG_Init 1 */
|
||||
hiwdg.Instance = IWDG;
|
||||
hiwdg.Init.Prescaler = IWDG_PRESCALER_4;
|
||||
hiwdg.Init.Window = 1000;
|
||||
hiwdg.Init.Reload = 1000;
|
||||
if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN IWDG_Init 2 */
|
||||
|
||||
/* USER CODE END IWDG_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief GPIO Initialization Function
|
||||
* @param None
|
||||
@ -425,32 +469,33 @@ static void MX_GPIO_Init(void)
|
||||
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) {
|
||||
|
||||
CAN_RxHeaderTypeDef rxHeader;
|
||||
// TODO: Remove buffer once filter is implemented?
|
||||
uint8_t rxBuffer[8];
|
||||
rx_data_t rxData;
|
||||
|
||||
// Read frame from HW into buffer
|
||||
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &rxHeader, rxBuffer) != HAL_OK)
|
||||
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &rxHeader, rxData.raw) != HAL_OK)
|
||||
Error_Handler();
|
||||
|
||||
// Discard if it's not for us
|
||||
// Discard if it's not for us (shouldn't happen thanks to filter, but just to be sure)
|
||||
if (rxHeader.StdId != CAN_ID_RX)
|
||||
return;
|
||||
|
||||
// copy to bitfield
|
||||
rxData.raw[0] = rxBuffer[0];
|
||||
|
||||
HAL_GPIO_WritePin(ASB_Error_GPIO_Port, ASB_Error_Pin, rxData.signals.asb_error);
|
||||
HAL_GPIO_WritePin(AS_close_SDC_GPIO_Port, AS_close_SDC_Pin, rxData.signals.as_close_sdc);
|
||||
|
||||
/*
|
||||
* TODO: Heartbeat -> Watchdog via STM Watchdog
|
||||
* Also alternative implementation for UCC? With ifdefs?
|
||||
* rxData.signals.heartbeat
|
||||
*/
|
||||
#ifdef WATCHDOG_STM
|
||||
|
||||
// TODO: TEMP!
|
||||
HAL_GPIO_WritePin(Watchdog_GPIO_Port, Watchdog_Pin, true);
|
||||
if (rxData.signals.heartbeat != pHeartbeat) {
|
||||
HAL_IWDG_Refresh(&hiwdg);
|
||||
WD_OK = true;
|
||||
HAL_GPIO_WritePin(Watchdog_GPIO_Port, Watchdog_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
pHeartbeat = rxData.signals.heartbeat;
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef WATCHDOG_UCC
|
||||
HAL_GPIO_WritePin(Watchdog_GPIO_Port, Watchdog_Pin, rxData.signals.heartbeat);
|
||||
#endif
|
||||
|
||||
// Reset old mission LED
|
||||
setMissionLED(mission, GPIO_PIN_RESET);
|
||||
|
||||
Reference in New Issue
Block a user