2017-12-29 12:52 PM
Hello, I'm trying to bring up CAN on the STM32F303RE. After banging my head against the wall, and with much help from the forum, I'm getting closer. What I notice is that the CAN clock is not repeatable from power up to power up. I've copied the Config_System_Clock() function from the Can Networking example from the STM32373C_Eval. It is shown below. The line with the
HSEPredivValue does not compile since this platform doesn't define it. I suspect my random CAN bus timing is because the System Clock is not configured properly.
Has anyone experienced this? Does anyone have a known good clock configuration that will repeatably drive the CAN bus at a known rate? (I'd also like to know how to change the clock to vary the bus rate.)
Thanks and happy new year!
/** * @brief System Clock Configuration * The system Clock is configured as follow : * System Clock source = PLL (HSI) * SYSCLK(Hz) = 64000000 * HCLK(Hz) = 64000000 * AHB Prescaler = 1 * APB1 Prescaler = 2 * APB2 Prescaler = 1 * HSI Frequency(Hz) = 8000000 * PREDIV = RCC_PREDIV_DIV2 (2) * PLLMUL = RCC_PLL_MUL16 (16) * Flash Latency(WS) = 2 * @param None * @retval None */static void SystemClock_Config(void)
{ RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitTypeDef RCC_OscInitStruct;/* Enable HSE Oscillator and activate PLL with HSE as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON;// RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; This line does not compile on STM32f303 platform RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct)!= HAL_OK) { Error_Handler(); }/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
clocks dividers */ RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2)!= HAL_OK) { Error_Handler(); }}2017-12-29 02:38 PM
You should look at the SystemCoreClock value, and then have it compute the APB1/APB2 clocks.
To see what's going wrong decompose the RCC/PLL setting to determine sources.
printf('Core=%d, %d MHz\n', SystemCoreClock, SystemCoreClock / 1000000);
printf('APB1=%d\n', HAL_RCC_GetPCLK1Freq());printf('APB2=%d\n', HAL_RCC_GetPCLK2Freq());{
RCC_OscInitTypeDef RCC_OscInitStruct; HAL_RCC_GetOscConfig(&RCC_OscInitStruct);// Dump out Osc states
}One could use the APB to compute prescaler and bit-quanta on the fly, but you most probably want to get to the bottom of the issue you mention first.
2018-01-01 07:16 PM
I guess I'm wondering how the clock can be somewhat random. At this point I just want a repeatable CAN clock so I can work on send/receive between devices. As I mentioned, I copied the ConfigSystemClock from the eval to my STM32F303re project. Does anyone know the system clock differences between the STM32373_eval and the 303? I hope I can avoid a page by page comparison of the clock systems between the devices... I appreciate the ARB and printf() suggestions. Unfortunately, I don't have a serial output from the 303 currently and my knowledge of the clock subsystem is seriously lacking...
Anyone have a ConfigSystemClock function that works for the 303 handy?
Thanks
2018-01-01 07:26 PM
Sometimes the EVAL board use different crystals, and HSE_VALUE define (ie 8 MHz vs 25 MHz)
If you lack a serial port consider using the SWV (Serial Wire Viewer via the debugger's PB3-SWO connection)
I'd imagine the HAL examples for the assorted boards contain working clock configurations.
Map the internal clocks out the PA8-MCO pin so you can scope them.
You're really going to need to get some diagnostic information out to be able to trouble shoot this.
2018-01-02 05:36 AM
did you use the cube ? I setup the canbus inside the cube.
The CanDo unit made it very easy with its clock configurator.
2018-01-02 11:33 AM
I did not use the cube tool. I thought I'd just work off the example projects since what I'm trying to do should be pretty simple - monitor an ADC and GPIO and read/write from the CAN bus. I thought it would be easy. Just getting the CAN bus almost working has been anything but. But I'm close... I'm hoping I can use another 303 sample project's clock setup to get a stable CAN bus clock. I suspect that installing and coming up to speed on a new framework tool, the cube, will take a lot of time. I may end up there if the sample code approach doesn't work..