Skip to content
70 changes: 53 additions & 17 deletions firmware/boards/TOAD_G4/variant_TOAD_G4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,47 +103,83 @@ const uint32_t analogInputPin[] = {
* @param None
* @retval None
*/
WEAK void SystemClock_Config(void) {
void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = {};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {};
RCC_CRSInitTypeDef CRS_Init = {};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {};

/** Configure the main internal regulator output voltage
*/
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);
/* */
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
Comment thread
jacobh460 marked this conversation as resolved.
Resolved

/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSI48;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;

/* See RM[275] for clock tree */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48 | RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; /* Our HSE is 8MHz; we use a MEMS oscillator instead of crystal
oscillator, hence BYPASS (RM[277]) */
RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV4;
RCC_OscInitStruct.PLL.PLLN = 85;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV4;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; /* PLL Source is HSE */
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV2; /* Prescaler; scales down to 4MHz */
RCC_OscInitStruct.PLL.PLLN = 60; /* Multiplier; scales up to 240MHz */
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; /* PLLPCLK = 120MHz */
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; /* PLLQCLK = 120MHz */
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; /* PLLRCLK = 120MHz */
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}

/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; /* SYSCLK = PLLRCLK = 120MHz */
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; /* AHB = 120MHz */
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; /* APB1 = AHB = 120MHz */
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; /* APB2 = AHB = 120MHz */

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK) {
/* Note: from RM[75] stm32g474 is a category 3 device */
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) {
Error_Handler();
}
/** Initializes the peripherals clocks

/** Enable the SYSCFG APB clock
*/
__HAL_RCC_CRS_CLK_ENABLE();

/** Configures CRS
Comment thread
jacobh460 marked this conversation as resolved.
Resolved
*/
CRS_Init.Prescaler = RCC_CRS_SYNC_DIV1;
CRS_Init.Source = RCC_CRS_SYNC_SOURCE_USB;
CRS_Init.Polarity = RCC_CRS_SYNC_POLARITY_RISING;
CRS_Init.ReloadValue = __HAL_RCC_CRS_RELOADVALUE_CALCULATE(48000000, 1000);
CRS_Init.ErrorLimitValue = 34;
CRS_Init.HSI48CalibrationValue = 32;

HAL_RCCEx_CRSConfig(&CRS_Init);

PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting that this works on G4, while HSI48 had issues on H7...
(usb wouldn't work if we set the clock to HSI48 like we are doing here)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this have something to do with the fact that we have CRS disabled on the H7? HSI48 is not precise enough alone to be used for USB

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah that's almost certainly it - we could consider HSI48 + CRS on the H7 then?

Not sure what's actually better, but I assume the same thing is better for both. Anyhow, issue for future PRs.

if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
Error_Handler();
}

/* Peripheral clock enable */
__HAL_RCC_USB_CLK_ENABLE();

/* Select FDCAN Clock Source */
PeriphClkInit = {0};
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_FDCAN;
PeriphClkInit.FdcanClockSelection = RCC_FDCANCLKSOURCE_PLL;

if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
Error_Handler();
}

/* Enable FDCAN Peripheral Clock */
__HAL_RCC_FDCAN_CLK_ENABLE();
}
6 changes: 6 additions & 0 deletions firmware/boards/TOAD_G4/variant_TOAD_G4.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@
#define HAL_QSPI_MODULE_ENABLED
#endif

#if !defined(HAL_FDCAN_MODULE_DISABLED) // enable FDCAN HAL definitions
#define HAL_FDCAN_MODULE_ENABLED
#endif

#define HSE_VALUE (8000000UL) /*!< Value of the External oscillator in Hz */

/*----------------------------------------------------------------------------
* Arduino objects - C++ only
*----------------------------------------------------------------------------*/
Expand Down
6 changes: 3 additions & 3 deletions firmware/boards/TOAD_H7/variant_TOAD_H7.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ const uint32_t analogInputPin[] = {
140 // A31, PC3_C
};

void SystemClock_Config(void)
{
void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

Expand Down Expand Up @@ -231,7 +230,8 @@ void SystemClock_Config(void)

/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 | RCC_CLOCKTYPE_D3PCLK1 | RCC_CLOCKTYPE_D1PCLK1;
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 |
RCC_CLOCKTYPE_D3PCLK1 | RCC_CLOCKTYPE_D1PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
Expand Down
6 changes: 1 addition & 5 deletions firmware/boards/TOAD_H7/variant_TOAD_H7.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,9 @@
#endif

// added by jhillman - do compile FDCAN HAL
#if !defined (HAL_FDCAN_MODULE_ENABLED)
#if !defined(HAL_FDCAN_MODULE_ENABLED)
#define HAL_FDCAN_MODULE_ENABLED
#endif
#if !defined (USE_HAL_FDCAN_REGISTER_CALLBACKS)
#define USE_HAL_FDCAN_REGISTER_CALLBACKS 1
#endif


#if !defined(HSE_VALUE)
// our HSE is 8MHz
Expand Down
Loading
Loading