3232#include "u_queues.h"
3333#include "u_debug.h"
3434#include "u_lightning.h"
35+ #include "u_tx_debug.h"
3536/* USER CODE END Includes */
3637
3738/* Private typedef -----------------------------------------------------------*/
@@ -85,6 +86,7 @@ SPI_HandleTypeDef hspi2;
8586
8687/* Private function prototypes -----------------------------------------------*/
8788void SystemClock_Config (void );
89+ void PeriphCommonClock_Config (void );
8890static void MX_GPDMA1_Init (void );
8991static void MX_GPIO_Init (void );
9092static void MX_ICACHE_Init (void );
@@ -158,7 +160,36 @@ void HAL_FDCAN_ErrorStatusCallback(FDCAN_HandleTypeDef *hfdcan, uint32_t ErrorSt
158160
159161 if (protocol_status .BusOff != 0 ) // If Bus-Off error occurred
160162 {
163+ PRINTLN_ERROR ("CAN ENCOUNTERED BUS OFF STATE" );
161164 CLEAR_BIT (hfdcan -> Instance -> CCCR , FDCAN_CCCR_INIT ); // Clear INIT bit to recover from Bus-Off
165+ /* get the FDCAN hal errors. */
166+ uint8_t status = HAL_FDCAN_GetError (can1 .hcan );
167+ printf ("can - HAL_FDCAN_GetError() status: %d\n" , status );
168+
169+ FDCAN_ProtocolStatusTypeDef statt = { 0 };
170+ status = HAL_FDCAN_GetProtocolStatus (can1 .hcan , & statt );
171+ printf ("can - HAL_FDCAN_GetProtocolStatus() status: %d\n" , status );
172+
173+ /* Big statt */
174+ printf ("can - HAL_FDCAN_GetProtocolStatus() - LastErrorCode=%ld\n" , statt .LastErrorCode );
175+ printf ("can - HAL_FDCAN_GetProtocolStatus() - DataLastErrorCode=%ld\n" , statt .DataLastErrorCode );
176+ printf ("can - HAL_FDCAN_GetProtocolStatus() - Activity=%ld\n" , statt .Activity );
177+ printf ("can - HAL_FDCAN_GetProtocolStatus() - ErrorPassive=%ld\n" , statt .ErrorPassive );
178+ printf ("can - HAL_FDCAN_GetProtocolStatus() - Warning=%ld\n" , statt .Warning );
179+ printf ("can - HAL_FDCAN_GetProtocolStatus() - BusOff=%ld\n" , statt .BusOff );
180+ printf ("can - HAL_FDCAN_GetProtocolStatus() - RxESIflag=%ld\n" , statt .RxESIflag );
181+ printf ("can - HAL_FDCAN_GetProtocolStatus() - RxBRSflag=%ld\n" , statt .RxBRSflag );
182+ printf ("can - HAL_FDCAN_GetProtocolStatus() - RxFDFflag=%ld\n" , statt .RxFDFflag );
183+ printf ("can - HAL_FDCAN_GetProtocolStatus() - ProtocolException=%ld\n" , statt .ProtocolException );
184+ printf ("can - HAL_FDCAN_GetProtocolStatus() - TDCvalue=%ld\n" , statt .TDCvalue );
185+
186+ FDCAN_ErrorCountersTypeDef statt2 = { 0 };
187+ status = HAL_FDCAN_GetErrorCounters (can1 .hcan , & statt2 );
188+ printf ("can - HAL_FDCAN_GetErrorCounters() status: %d\n" , status );
189+ printf ("can - HAL_FDCAN_GetErrorCounters() - TxErrorCnt: %ld\n" , statt2 .TxErrorCnt );
190+ printf ("can - HAL_FDCAN_GetErrorCounters() - RxErrorCnt: %ld\n" , statt2 .RxErrorCnt );
191+ printf ("can - HAL_FDCAN_GetErrorCounters() - RxErrorPassive: %ld\n" , statt2 .RxErrorPassive );
192+ printf ("can - HAL_FDCAN_GetErrorCounters() - ErrorLogging: %ld\n" , statt2 .ErrorLogging );
162193 }
163194}
164195
@@ -187,6 +218,9 @@ int main(void)
187218 /* Configure the system clock */
188219 SystemClock_Config ();
189220
221+ /* Configure the peripherals common clocks */
222+ PeriphCommonClock_Config ();
223+
190224 /* USER CODE BEGIN SysInit */
191225 //HAL_Delay(10000);
192226 /* USER CODE END SysInit */
@@ -285,6 +319,36 @@ void SystemClock_Config(void)
285319 __HAL_FLASH_SET_PROGRAM_DELAY (FLASH_PROGRAMMING_DELAY_2 );
286320}
287321
322+ /**
323+ * @brief Peripherals Common Clock Configuration
324+ * @retval None
325+ */
326+ void PeriphCommonClock_Config (void )
327+ {
328+ RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0 };
329+
330+ /** Initializes the peripherals clock
331+ */
332+ PeriphClkInitStruct .PeriphClockSelection = RCC_PERIPHCLK_ADCDAC |RCC_PERIPHCLK_FDCAN
333+ |RCC_PERIPHCLK_SPI2 ;
334+ PeriphClkInitStruct .PLL2 .PLL2Source = RCC_PLL2_SOURCE_HSE ;
335+ PeriphClkInitStruct .PLL2 .PLL2M = 5 ;
336+ PeriphClkInitStruct .PLL2 .PLL2N = 64 ;
337+ PeriphClkInitStruct .PLL2 .PLL2P = 5 ;
338+ PeriphClkInitStruct .PLL2 .PLL2Q = 5 ;
339+ PeriphClkInitStruct .PLL2 .PLL2R = 2 ;
340+ PeriphClkInitStruct .PLL2 .PLL2RGE = RCC_PLL2_VCIRANGE_2 ;
341+ PeriphClkInitStruct .PLL2 .PLL2VCOSEL = RCC_PLL2_VCORANGE_WIDE ;
342+ PeriphClkInitStruct .PLL2 .PLL2FRACN = 0 ;
343+ PeriphClkInitStruct .PLL2 .PLL2ClockOut = RCC_PLL2_DIVP |RCC_PLL2_DIVQ ;
344+ PeriphClkInitStruct .FdcanClockSelection = RCC_FDCANCLKSOURCE_PLL2Q ;
345+ PeriphClkInitStruct .Spi2ClockSelection = RCC_SPI2CLKSOURCE_PLL2P ;
346+ if (HAL_RCCEx_PeriphCLKConfig (& PeriphClkInitStruct ) != HAL_OK )
347+ {
348+ Error_Handler ();
349+ }
350+ }
351+
288352/**
289353 * @brief ADC1 Initialization Function
290354 * @param None
@@ -596,10 +660,10 @@ static void MX_FDCAN2_Init(void)
596660 hfdcan2 .Init .AutoRetransmission = DISABLE ;
597661 hfdcan2 .Init .TransmitPause = DISABLE ;
598662 hfdcan2 .Init .ProtocolException = DISABLE ;
599- hfdcan2 .Init .NominalPrescaler = 5 ;
663+ hfdcan2 .Init .NominalPrescaler = 8 ;
600664 hfdcan2 .Init .NominalSyncJumpWidth = 1 ;
601- hfdcan2 .Init .NominalTimeSeg1 = 1 ;
602- hfdcan2 .Init .NominalTimeSeg2 = 8 ;
665+ hfdcan2 .Init .NominalTimeSeg1 = 11 ;
666+ hfdcan2 .Init .NominalTimeSeg2 = 4 ;
603667 hfdcan2 .Init .DataPrescaler = 1 ;
604668 hfdcan2 .Init .DataSyncJumpWidth = 1 ;
605669 hfdcan2 .Init .DataTimeSeg1 = 1 ;
@@ -928,7 +992,10 @@ static void MX_GPIO_Init(void)
928992 HAL_GPIO_WritePin (GPIOE , RED_LED_Pin |GREEN_LED_Pin |PHY_RESET_Pin , GPIO_PIN_RESET );
929993
930994 /*Configure GPIO pin Output Level */
931- HAL_GPIO_WritePin (GPIOF , EF_BATTBOX_EN_Pin |EF_MC_EN_Pin , GPIO_PIN_RESET );
995+ HAL_GPIO_WritePin (EF_BATTBOX_EN_GPIO_Port , EF_BATTBOX_EN_Pin , GPIO_PIN_SET );
996+
997+ /*Configure GPIO pin Output Level */
998+ HAL_GPIO_WritePin (EF_MC_EN_GPIO_Port , EF_MC_EN_Pin , GPIO_PIN_RESET );
932999
9331000 /*Configure GPIO pin Output Level */
9341001 HAL_GPIO_WritePin (GPIOD , EF_BREAK_EN_Pin |EF_FANBATT_EN_Pin |EF_PUMP1_EN_Pin |EF_PUMP2_EN_Pin
@@ -937,11 +1004,14 @@ static void MX_GPIO_Init(void)
9371004 /*Configure GPIO pin Output Level */
9381005 HAL_GPIO_WritePin (GPIOG , EF_RADFAN_EN_Pin |EF_SPARE_EN_Pin |WATCHDOG_Pin , GPIO_PIN_RESET );
9391006
1007+ /*Configure GPIO pin Output Level */
1008+ HAL_GPIO_WritePin (GPIOG , EF_SHUTDOWN_EN_Pin |FAULT_MCU_Pin , GPIO_PIN_SET );
1009+
9401010 /*Configure GPIO pin Output Level */
9411011 HAL_GPIO_WritePin (GPIOC , MUX_SEL1_Pin |MUX_SEL2_Pin |MUX_SEL3_Pin |MUX_SEL4_Pin , GPIO_PIN_RESET );
9421012
9431013 /*Configure GPIO pin Output Level */
944- HAL_GPIO_WritePin (FAULT_MCU_GPIO_Port , FAULT_MCU_Pin , GPIO_PIN_SET );
1014+ HAL_GPIO_WritePin (EF_LV_EN_GPIO_Port , EF_LV_EN_Pin , GPIO_PIN_SET );
9451015
9461016 /*Configure GPIO pin Output Level */
9471017 HAL_GPIO_WritePin (IMU_CS_GPIO_Port , IMU_CS_Pin , GPIO_PIN_SET );
@@ -959,19 +1029,26 @@ static void MX_GPIO_Init(void)
9591029 GPIO_InitStruct .Speed = GPIO_SPEED_FREQ_LOW ;
9601030 HAL_GPIO_Init (GPIOE , & GPIO_InitStruct );
9611031
962- /*Configure GPIO pins : EF_BATTBOX_EN_Pin EF_MC_EN_Pin */
963- GPIO_InitStruct .Pin = EF_BATTBOX_EN_Pin | EF_MC_EN_Pin ;
1032+ /*Configure GPIO pin : EF_BATTBOX_EN_Pin */
1033+ GPIO_InitStruct .Pin = EF_BATTBOX_EN_Pin ;
9641034 GPIO_InitStruct .Mode = GPIO_MODE_OUTPUT_PP ;
965- GPIO_InitStruct .Pull = GPIO_NOPULL ;
1035+ GPIO_InitStruct .Pull = GPIO_PULLUP ;
9661036 GPIO_InitStruct .Speed = GPIO_SPEED_FREQ_LOW ;
967- HAL_GPIO_Init (GPIOF , & GPIO_InitStruct );
1037+ HAL_GPIO_Init (EF_BATTBOX_EN_GPIO_Port , & GPIO_InitStruct );
9681038
9691039 /*Configure GPIO pins : EF_BATTBOX_ER_Pin EF_MC_ER_Pin */
9701040 GPIO_InitStruct .Pin = EF_BATTBOX_ER_Pin |EF_MC_ER_Pin ;
9711041 GPIO_InitStruct .Mode = GPIO_MODE_INPUT ;
9721042 GPIO_InitStruct .Pull = GPIO_PULLUP ;
9731043 HAL_GPIO_Init (GPIOF , & GPIO_InitStruct );
9741044
1045+ /*Configure GPIO pin : EF_MC_EN_Pin */
1046+ GPIO_InitStruct .Pin = EF_MC_EN_Pin ;
1047+ GPIO_InitStruct .Mode = GPIO_MODE_OUTPUT_PP ;
1048+ GPIO_InitStruct .Pull = GPIO_NOPULL ;
1049+ GPIO_InitStruct .Speed = GPIO_SPEED_FREQ_LOW ;
1050+ HAL_GPIO_Init (EF_MC_EN_GPIO_Port , & GPIO_InitStruct );
1051+
9751052 /*Configure GPIO pins : BMS_GPIO_Pin BOTS_GPIO_Pin SPARE_GPIO_Pin BSPD_GPIO_Pin
9761053 HV_C_GPIO_Pin HVD_GPIO_Pin */
9771054 GPIO_InitStruct .Pin = BMS_GPIO_Pin |BOTS_GPIO_Pin |SPARE_GPIO_Pin |BSPD_GPIO_Pin
@@ -1008,9 +1085,9 @@ static void MX_GPIO_Init(void)
10081085 HAL_GPIO_Init (GPIOD , & GPIO_InitStruct );
10091086
10101087 /*Configure GPIO pins : EF_BREAK_ER_Pin EF_FANBATT_ER_Pin EF_PUMP1_ER_Pin EF_PUMP2_ER_Pin
1011- EF_DASH_ER_Pin EF_LV_EN_Pin EF_LV_ER_Pin */
1088+ EF_DASH_ER_Pin EF_LV_ER_Pin */
10121089 GPIO_InitStruct .Pin = EF_BREAK_ER_Pin |EF_FANBATT_ER_Pin |EF_PUMP1_ER_Pin |EF_PUMP2_ER_Pin
1013- |EF_DASH_ER_Pin |EF_LV_EN_Pin | EF_LV_ER_Pin ;
1090+ |EF_DASH_ER_Pin |EF_LV_ER_Pin ;
10141091 GPIO_InitStruct .Mode = GPIO_MODE_INPUT ;
10151092 GPIO_InitStruct .Pull = GPIO_PULLUP ;
10161093 HAL_GPIO_Init (GPIOD , & GPIO_InitStruct );
@@ -1028,12 +1105,19 @@ static void MX_GPIO_Init(void)
10281105 GPIO_InitStruct .Speed = GPIO_SPEED_FREQ_LOW ;
10291106 HAL_GPIO_Init (GPIOG , & GPIO_InitStruct );
10301107
1031- /*Configure GPIO pins : EF_RADFAN_ER_Pin EF_SHUTDOWN_EN_Pin EF_SHUTDOWN_ER_Pin EF_SPARE_ER_Pin */
1032- GPIO_InitStruct .Pin = EF_RADFAN_ER_Pin |EF_SHUTDOWN_EN_Pin | EF_SHUTDOWN_ER_Pin |EF_SPARE_ER_Pin ;
1108+ /*Configure GPIO pins : EF_RADFAN_ER_Pin EF_SHUTDOWN_ER_Pin EF_SPARE_ER_Pin */
1109+ GPIO_InitStruct .Pin = EF_RADFAN_ER_Pin |EF_SHUTDOWN_ER_Pin |EF_SPARE_ER_Pin ;
10331110 GPIO_InitStruct .Mode = GPIO_MODE_INPUT ;
10341111 GPIO_InitStruct .Pull = GPIO_PULLUP ;
10351112 HAL_GPIO_Init (GPIOG , & GPIO_InitStruct );
10361113
1114+ /*Configure GPIO pin : EF_SHUTDOWN_EN_Pin */
1115+ GPIO_InitStruct .Pin = EF_SHUTDOWN_EN_Pin ;
1116+ GPIO_InitStruct .Mode = GPIO_MODE_OUTPUT_PP ;
1117+ GPIO_InitStruct .Pull = GPIO_PULLUP ;
1118+ GPIO_InitStruct .Speed = GPIO_SPEED_FREQ_LOW ;
1119+ HAL_GPIO_Init (EF_SHUTDOWN_EN_GPIO_Port , & GPIO_InitStruct );
1120+
10371121 /*Configure GPIO pins : MUX_SEL1_Pin MUX_SEL2_Pin MUX_SEL3_Pin MUX_SEL4_Pin */
10381122 GPIO_InitStruct .Pin = MUX_SEL1_Pin |MUX_SEL2_Pin |MUX_SEL3_Pin |MUX_SEL4_Pin ;
10391123 GPIO_InitStruct .Mode = GPIO_MODE_OUTPUT_PP ;
@@ -1047,6 +1131,13 @@ static void MX_GPIO_Init(void)
10471131 GPIO_InitStruct .Pull = GPIO_NOPULL ;
10481132 HAL_GPIO_Init (GPIOA , & GPIO_InitStruct );
10491133
1134+ /*Configure GPIO pin : EF_LV_EN_Pin */
1135+ GPIO_InitStruct .Pin = EF_LV_EN_Pin ;
1136+ GPIO_InitStruct .Mode = GPIO_MODE_OUTPUT_PP ;
1137+ GPIO_InitStruct .Pull = GPIO_PULLUP ;
1138+ GPIO_InitStruct .Speed = GPIO_SPEED_FREQ_LOW ;
1139+ HAL_GPIO_Init (EF_LV_EN_GPIO_Port , & GPIO_InitStruct );
1140+
10501141 /*Configure GPIO pin : IMU_CS_Pin */
10511142 GPIO_InitStruct .Pin = IMU_CS_Pin ;
10521143 GPIO_InitStruct .Mode = GPIO_MODE_OUTPUT_PP ;
0 commit comments