Skip to content

Commit 5055463

Browse files
committed
change the SystemClock_Config from the mbed os
Signed-off-by: Francois Ramu <francois.ramu@st.com>
1 parent ab6e293 commit 5055463

File tree

2 files changed

+153
-66
lines changed

2 files changed

+153
-66
lines changed

variants/PNUCLEO_WB55RG/otp.h

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
******************************************************************************
3+
* @file otp.h
4+
* @author MCD Application Team
5+
* @brief OTP manager interface
6+
******************************************************************************
7+
* @attention
8+
*
9+
* <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
10+
* All rights reserved.</center></h2>
11+
*
12+
* This software component is licensed by ST under BSD 3-Clause license,
13+
* the "License"; You may not use this file except in compliance with the
14+
* License. You may obtain a copy of the License at:
15+
* opensource.org/licenses/BSD-3-Clause
16+
*
17+
******************************************************************************
18+
*/
19+
20+
21+
/* Define to prevent recursive inclusion -------------------------------------*/
22+
#ifndef __OTP_H
23+
#define __OTP_H
24+
25+
#ifdef __cplusplus
26+
extern "C" {
27+
#endif
28+
29+
/* Includes ------------------------------------------------------------------*/
30+
/* ----------------------------------- *
31+
* Packed usage (compiler dependent) *
32+
* ----------------------------------- */
33+
#undef PACKED__
34+
#undef PACKED_STRUCT
35+
36+
#if defined ( __CC_ARM )
37+
#if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050U)
38+
#define PACKED__ __attribute__((packed))
39+
#define PACKED_STRUCT struct PACKED__
40+
#else
41+
#define PACKED__(TYPE) __packed TYPE
42+
#define PACKED_STRUCT PACKED__(struct)
43+
#endif
44+
#elif defined ( __GNUC__ )
45+
#define PACKED__ __attribute__((packed))
46+
#define PACKED_STRUCT struct PACKED__
47+
#elif defined (__ICCARM__)
48+
#define PACKED_STRUCT __packed struct
49+
#elif
50+
#define PACKED_STRUCT __packed struct
51+
#endif
52+
53+
/* Exported types ------------------------------------------------------------*/
54+
typedef PACKED_STRUCT
55+
{
56+
uint8_t bd_address[6];
57+
uint8_t hse_tuning;
58+
uint8_t id;
59+
} OTP_ID0_t;
60+
61+
/* Exported constants --------------------------------------------------------*/
62+
/* External variables --------------------------------------------------------*/
63+
/* Exported macros -----------------------------------------------------------*/
64+
/* Exported functions ------------------------------------------------------- */
65+
#define CFG_OTP_BASE_ADDRESS OTP_AREA_BASE
66+
67+
#define CFG_OTP_END_ADDRESS OTP_AREA_END_ADDR
68+
69+
#ifdef __cplusplus
70+
}
71+
#endif
72+
73+
#endif /*__OTP_H */
74+
75+
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

variants/PNUCLEO_WB55RG/variant.cpp

+78-66
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
#include "pins_arduino.h"
20+
#include "otp.h"
2021

2122
#ifdef __cplusplus
2223
extern "C" {
@@ -88,94 +89,105 @@ const PinName digitalPin[] = {
8889
extern "C" {
8990
#endif
9091

92+
static uint8_t * OTP_Read( uint8_t id )
93+
{
94+
uint8_t *p_id;
95+
96+
p_id = (uint8_t*)(CFG_OTP_END_ADDRESS - 7) ;
97+
98+
while( ((*( p_id + 7 )) != id) && ( p_id != (uint8_t*)CFG_OTP_BASE_ADDRESS) )
99+
{
100+
p_id -= 8 ;
101+
}
102+
103+
if((*( p_id + 7 )) != id)
104+
{
105+
p_id = 0 ;
106+
}
107+
108+
return p_id ;
109+
}
110+
111+
static void Config_HSE(void)
112+
{
113+
OTP_ID0_t *p_otp;
114+
115+
/**
116+
* Read HSE_Tuning from OTP
117+
*/
118+
p_otp = (OTP_ID0_t *) OTP_Read(0);
119+
if (p_otp) {
120+
LL_RCC_HSE_SetCapacitorTuning(p_otp->hse_tuning);
121+
}
122+
123+
return;
124+
}
125+
91126
/**
92127
* @brief System Clock Configuration
93128
* @param None
94129
* @retval None
95130
*/
96131
WEAK void SystemClock_Config(void)
97132
{
98-
RCC_OscInitTypeDef RCC_OscInitStruct = {};
99-
RCC_ClkInitTypeDef RCC_ClkInitStruct = {};
100-
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {};
101-
102-
/* Configure LSE Drive Capability */
103-
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
104-
/* Configure the main internal regulator output voltage */
105-
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
106-
/* Initializes the CPU, AHB and APB busses clocks */
107-
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE
108-
| RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_MSI;
109-
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
110-
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
111-
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
112-
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
113-
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
114-
RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
115-
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
116-
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
117-
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
118-
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1;
119-
RCC_OscInitStruct.PLL.PLLN = 32;
120-
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
121-
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
122-
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
123-
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
124-
Error_Handler();
125-
}
126-
/* Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers */
127-
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK4 | RCC_CLOCKTYPE_HCLK2
128-
| RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
129-
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
130-
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
131-
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
132-
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
133-
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
134-
RCC_ClkInitStruct.AHBCLK2Divider = RCC_SYSCLK_DIV2;
135-
RCC_ClkInitStruct.AHBCLK4Divider = RCC_SYSCLK_DIV1;
136-
137-
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) {
138-
Error_Handler();
139-
}
140-
/* Initializes the peripherals clocks */
141-
/* from MBED:
142-
*
143-
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SMPS | RCC_PERIPHCLK_USB;
144-
PeriphClkInitStruct.PLLSAI1.PLLN = 24;
145-
PeriphClkInitStruct.PLLSAI1.PLLP = RCC_PLLP_DIV2;
146-
PeriphClkInitStruct.PLLSAI1.PLLQ = RCC_PLLQ_DIV2;
147-
PeriphClkInitStruct.PLLSAI1.PLLR = RCC_PLLR_DIV2;
148-
PeriphClkInitStruct.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_USBCLK;
149-
PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_PLLSAI1;
150-
PeriphClkInitStruct.SmpsClockSelection = RCC_SMPSCLKSOURCE_HSI;
151-
PeriphClkInitStruct.SmpsDivSelection = RCC_SMPSCLKDIV_RANGE0; */
152-
/* RNG needs to be configured like in M0 core, i.e. with HSI48 */
133+
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
134+
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
135+
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
136+
137+
Config_HSE();
138+
139+
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
140+
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
141+
142+
/* This prevents the CPU2 (M0+) to disable the HSI48 oscillator
143+
while (LL_HSEM_1StepLock(HSEM, CFG_HW_CLK48_CONFIG_SEMID)); */
144+
145+
/* Initializes the CPU, AHB and APB busses clocks */
146+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSI48 | RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_LSE;
147+
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
148+
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
149+
RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
150+
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
151+
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
152+
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
153+
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
154+
Error_Handler();
155+
}
156+
157+
/** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers
158+
*/
159+
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK4 | RCC_CLOCKTYPE_HCLK2
160+
| RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
161+
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
162+
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
163+
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
164+
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
165+
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
166+
RCC_ClkInitStruct.AHBCLK2Divider = RCC_SYSCLK_DIV1;
167+
RCC_ClkInitStruct.AHBCLK4Divider = RCC_SYSCLK_DIV1;
168+
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {
169+
Error_Handler();
170+
}
171+
172+
/* Initializes the peripherals clocks */
173+
/* RNG needs to be configured like in M0 core, i.e. with HSI48 */
153174
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SMPS | RCC_PERIPHCLK_RFWAKEUP | RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_USB;
154175
PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;
155176
PeriphClkInitStruct.RngClockSelection = RCC_RNGCLKSOURCE_HSI48;
156177
PeriphClkInitStruct.RFWakeUpClockSelection = RCC_RFWKPCLKSOURCE_LSE;
157178
PeriphClkInitStruct.SmpsClockSelection = RCC_SMPSCLKSOURCE_HSE;
158179
PeriphClkInitStruct.SmpsDivSelection = RCC_SMPSCLKDIV_RANGE1;
159180
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
160-
Error_Handler();
181+
Error_Handler();
161182
}
162183

163184
LL_PWR_SMPS_SetStartupCurrent(LL_PWR_SMPS_STARTUP_CURRENT_80MA);
164185
LL_PWR_SMPS_SetOutputVoltageLevel(LL_PWR_SMPS_OUTPUT_VOLTAGE_1V40);
165-
// LL_PWR_SMPS_Enable();
186+
LL_PWR_SMPS_Enable();
166187

167188
/* Select HSI as system clock source after Wake Up from Stop mode */
168189
LL_RCC_SetClkAfterWakeFromStop(LL_RCC_STOP_WAKEUPCLOCK_HSI);
169190

170-
/* Enable MSI Auto calibration */
171-
HAL_RCCEx_EnableMSIPLLMode();
172-
173-
/* */
174-
LL_PWR_SMPS_SetStartupCurrent(LL_PWR_SMPS_STARTUP_CURRENT_80MA);
175-
LL_PWR_SMPS_SetOutputVoltageLevel(LL_PWR_SMPS_OUTPUT_VOLTAGE_1V40);
176-
177-
LL_RCC_SetClkAfterWakeFromStop(LL_RCC_STOP_WAKEUPCLOCK_MSI);
178-
179191
}
180192

181193
#ifdef __cplusplus

0 commit comments

Comments
 (0)