Skip to content

Commit 11bc6fd

Browse files
committed
Merge pull request #232 from bcostm/master
[NUCLEO_F302R8] Improvements in clock configuration, spi default pins, code formatting
2 parents d4c04e8 + c675516 commit 11bc6fd

File tree

20 files changed

+656
-569
lines changed

20 files changed

+656
-569
lines changed

libraries/mbed/targets/cmsis/TARGET_STM/TARGET_NUCLEO_F302R8/TOOLCHAIN_ARM_STD/startup_stm32f302x8.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
;******************** (C) COPYRIGHT 2014 STMicroelectronics ********************
22
;* File Name : startup_stm32f302x8.s
3-
; STM32F302x8 Devices vector table for MDK ARM_MICRO toolchain
3+
; STM32F302x8 Devices vector table for MDK ARM_STD toolchain
44
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
55
; Copyright (c) 2014, STMicroelectronics
66
; All rights reserved.

libraries/mbed/targets/cmsis/TARGET_STM/TARGET_NUCLEO_F302R8/stm32f30x.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
can define the HSE value in your toolchain compiler preprocessor.
113113
*/
114114
#if !defined (HSE_VALUE)
115-
#define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */
115+
#define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External xtal in Hz */
116116
#endif /* HSE_VALUE */
117117

118118
/**

libraries/mbed/targets/cmsis/TARGET_STM/TARGET_NUCLEO_F302R8/system_stm32f30x.c

Lines changed: 156 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,22 @@
4040
* value to your own configuration.
4141
*
4242
* 5. This file configures the system clock as follows:
43-
*=============================================================================
44-
* Supported STM32F30x device
4543
*-----------------------------------------------------------------------------
46-
* System Clock source | PLL(HSI)
44+
* System clock source | 1- PLL_HSE_EXTC | 3- PLL_HSI
45+
* | (external 8 MHz clock) | (internal 8 MHz)
46+
* | 2- PLL_HSE_XTAL |
47+
* | (external 8 MHz xtal) |
4748
*-----------------------------------------------------------------------------
48-
* SYSCLK(Hz) | 64000000
49+
* SYSCLK(MHz) | 72 | 64
4950
*-----------------------------------------------------------------------------
50-
* HCLK(Hz) | 64000000
51+
* AHBCLK (MHz) | 72 | 64
5152
*-----------------------------------------------------------------------------
52-
* AHB Prescaler | 1
53+
* APB1CLK (MHz) | 36 | 32
5354
*-----------------------------------------------------------------------------
54-
* APB2 Prescaler | 1
55+
* APB2CLK (MHz) | 72 | 64
5556
*-----------------------------------------------------------------------------
56-
* APB1 Prescaler (Max = 36MHz) | 2 (SPI, ...)
57-
*-----------------------------------------------------------------------------
58-
* HSE Frequency(Hz) | 8000000
59-
*----------------------------------------------------------------------------
60-
* PLLMUL | 16
61-
*-----------------------------------------------------------------------------
62-
* PREDIV | 2
63-
*-----------------------------------------------------------------------------
64-
* USB Clock | DISABLE
65-
*-----------------------------------------------------------------------------
66-
* Flash Latency(WS) | 2
67-
*-----------------------------------------------------------------------------
68-
* Prefetch Buffer | OFF
69-
*-----------------------------------------------------------------------------
70-
*=============================================================================
57+
* USB capable (48 MHz precise clock) | YES | NO
58+
*-----------------------------------------------------------------------------
7159
******************************************************************************
7260
* @attention
7361
*
@@ -97,6 +85,7 @@
9785
*
9886
******************************************************************************
9987
*/
88+
10089
/** @addtogroup CMSIS
10190
* @{
10291
*/
@@ -126,6 +115,7 @@
126115
/** @addtogroup STM32F30x_System_Private_Defines
127116
* @{
128117
*/
118+
129119
/*!< Uncomment the following line if you need to relocate your vector Table in
130120
Internal SRAM. */
131121
/* #define VECT_TAB_SRAM */
@@ -139,6 +129,10 @@
139129
* @{
140130
*/
141131

132+
/* Select the clock sources (other than HSI) to start with (0=OFF, 1=ON) */
133+
#define USE_PLL_HSE_EXTC (1) /* Use external clock */
134+
#define USE_PLL_HSE_XTAL (1) /* Use external xtal */
135+
142136
/**
143137
* @}
144138
*/
@@ -147,9 +141,9 @@
147141
* @{
148142
*/
149143

150-
uint32_t SystemCoreClock = 64000000;
144+
uint32_t SystemCoreClock = 64000000; /* Default with HSI. Will be updated if HSE is used */
151145

152-
__I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
146+
__I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
153147

154148
/**
155149
* @}
@@ -161,6 +155,12 @@
161155

162156
void SetSysClock(void);
163157

158+
#if (USE_PLL_HSE_XTAL != 0) || (USE_PLL_HSE_EXTC != 0)
159+
uint8_t SetSysClock_PLL_HSE(uint8_t bypass);
160+
#endif
161+
162+
uint8_t SetSysClock_PLL_HSI(void);
163+
164164
/**
165165
* @}
166166
*/
@@ -208,31 +208,16 @@ void SystemInit(void)
208208
/* Disable all interrupts */
209209
RCC->CIR = 0x00000000;
210210

211-
/* Configure the System clock source, PLL Multiplier and Divider factors,
212-
AHB/APBx prescalers and Flash settings ----------------------------------*/
213-
SetSysClock();
214-
211+
/* Configure the Vector Table location add offset address ------------------*/
215212
#ifdef VECT_TAB_SRAM
216-
SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
213+
SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
217214
#else
218-
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
215+
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
219216
#endif
220217

221-
// ADDED FOR MBED DEBUGGING PURPOSE
222-
/*
223-
// Enable GPIOA clock
224-
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
225-
// Configure MCO pin (PA8)
226-
GPIO_InitTypeDef GPIO_InitStructure;
227-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
228-
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
229-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
230-
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
231-
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
232-
GPIO_Init(GPIOA, &GPIO_InitStructure);
233-
// Select the clock to output
234-
RCC_MCOConfig(RCC_MCOSource_SYSCLK, RCC_MCOPrescaler_1);
235-
*/
218+
/* Configure the System clock source, PLL Multiplier and Divider factors,
219+
AHB/APBx prescalers and Flash settings */
220+
SetSysClock();
236221
}
237222

238223
/**
@@ -325,31 +310,137 @@ void SystemCoreClockUpdate (void)
325310
*/
326311
void SetSysClock(void)
327312
{
313+
/* 1- Try to start with HSE and external clock */
314+
#if USE_PLL_HSE_EXTC != 0
315+
if (SetSysClock_PLL_HSE(1) == 0)
316+
#endif
317+
{
318+
/* 2- If fail try to start with HSE and external xtal */
319+
#if USE_PLL_HSE_XTAL != 0
320+
if (SetSysClock_PLL_HSE(0) == 0)
321+
#endif
322+
{
323+
/* 3- If fail start with HSI clock */
324+
if (SetSysClock_PLL_HSI() == 0)
325+
{
326+
while(1)
327+
{
328+
// [TODO] Put something here to tell the user that a problem occured...
329+
}
330+
}
331+
}
332+
}
333+
334+
/* Update SystemCoreClock variable */
335+
SystemCoreClockUpdate();
336+
337+
/* Output SYSCLK on MCO pin(PA8) for debugging purpose */
338+
/*
339+
// Enable GPIOA clock
340+
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
341+
// Configure MCO pin (PA8)
342+
GPIO_InitTypeDef GPIO_InitStructure;
343+
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
344+
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
345+
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
346+
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
347+
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
348+
GPIO_Init(GPIOA, &GPIO_InitStructure);
349+
// Select the clock to output
350+
RCC_MCOConfig(RCC_MCOSource_SYSCLK, RCC_MCOPrescaler_1);
351+
*/
352+
}
353+
354+
#if (USE_PLL_HSE_XTAL != 0) || (USE_PLL_HSE_EXTC != 0)
328355
/******************************************************************************/
329-
/* PLL (clocked by HSI) used as System clock source */
356+
/* PLL (clocked by HSE) used as System clock source */
330357
/******************************************************************************/
358+
uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
359+
{
360+
__IO uint32_t StartUpCounter = 0;
361+
__IO uint32_t HSEStatus = 0;
331362

332-
/* At this stage the HSI is already enabled and used as System clock source */
363+
/* Bypass HSE: can be done only if HSE is OFF */
364+
if (bypass != 0)
365+
{
366+
RCC->CR &= ((uint32_t)~RCC_CR_HSEON); /* To be sure HSE is OFF */
367+
RCC->CR |= ((uint32_t)RCC_CR_HSEBYP);
368+
}
333369

334-
/* SYSCLK, HCLK, PCLK configuration ----------------------------------------*/
335-
336-
/* Disable Prefetch Buffer and set Flash Latency */
337-
FLASH->ACR = (uint32_t)FLASH_ACR_LATENCY_1;
338-
339-
/* HCLK = 64 MHz */
340-
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
341-
342-
/* PCLK2 = 64 MHz */
343-
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
370+
/* Enable HSE */
371+
RCC->CR |= ((uint32_t)RCC_CR_HSEON);
372+
373+
/* Wait till HSE is ready */
374+
do
375+
{
376+
HSEStatus = RCC->CR & RCC_CR_HSERDY;
377+
StartUpCounter++;
378+
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
344379

345-
/* PCLK1 = 32 MHz (SPI, ...) */
346-
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
380+
/* Check if HSE has started correctly */
381+
if ((RCC->CR & RCC_CR_HSERDY) != RESET)
382+
{
383+
/* Enable prefetch buffer and set flash latency
384+
0WS for 0 < SYSCLK <= 24 MHz
385+
1WS for 24 < SYSCLK <= 48 MHz
386+
2WS for 48 < SYSCLK <= 72 MHz */
387+
FLASH->ACR = FLASH_ACR_PRFTBE | (uint32_t)FLASH_ACR_LATENCY_1; /* 2 WS */
388+
389+
/* Warning: values are obtained with external xtal or clock = 8 MHz */
390+
/* SYSCLK = 72 MHz (8 MHz * 9) */
391+
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
392+
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL9
393+
| RCC_CFGR_HPRE_DIV1 /* HCLK = 72 MHz */
394+
| RCC_CFGR_PPRE2_DIV1 /* PCLK2 = 72 MHz */
395+
| RCC_CFGR_PPRE1_DIV2); /* PCLK1 = 36 MHz */
396+
/* USBCLK = 48 MHz (72 MHz / 1.5) --> USB OK */
397+
398+
/* Enable PLL */
399+
RCC->CR |= RCC_CR_PLLON;
400+
401+
/* Wait till PLL is ready */
402+
while((RCC->CR & RCC_CR_PLLRDY) == 0)
403+
{
404+
}
405+
406+
/* Select PLL as system clock source */
407+
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
408+
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
409+
410+
/* Wait till PLL is used as system clock source */
411+
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
412+
{
413+
}
414+
415+
return 1; // OK
416+
}
417+
else
418+
{
419+
return 0; // FAIL
420+
}
421+
}
422+
#endif
347423

348-
/* PLL configuration
349-
SYSCLK = 4 MHz * 16 = 64 MHz
350-
*/
424+
/******************************************************************************/
425+
/* PLL (clocked by HSI) used as System clock source */
426+
/******************************************************************************/
427+
uint8_t SetSysClock_PLL_HSI(void)
428+
{
429+
/* At this stage the HSI is already enabled and used as System clock source */
430+
431+
/* Enable prefetch buffer and set flash latency
432+
0WS for 0 < SYSCLK <= 24 MHz
433+
1WS for 24 < SYSCLK <= 48 MHz
434+
2WS for 48 < SYSCLK <= 72 MHz */
435+
FLASH->ACR = FLASH_ACR_PRFTBE | (uint32_t)FLASH_ACR_LATENCY_1; /* 2 WS */
436+
437+
/* SYSCLK = 64 MHz (8 MHz / 2 * 16) */
351438
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
352-
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSI_Div2 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL16);
439+
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSI_Div2 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL16
440+
| RCC_CFGR_HPRE_DIV1 /* HCLK = 64 MHz */
441+
| RCC_CFGR_PPRE2_DIV1 /* PCLK2 = 64 MHz */
442+
| RCC_CFGR_PPRE1_DIV2); /* PCLK1 = 32 MHz */
443+
/* USBCLK = 42.667 MHz (64 MHz / 1.5) --> USB NOT POSSIBLE */
353444

354445
/* Enable PLL */
355446
RCC->CR |= RCC_CR_PLLON;
@@ -367,6 +458,8 @@ void SetSysClock(void)
367458
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
368459
{
369460
}
461+
462+
return 1; // OK
370463
}
371464

372465
/**
@@ -382,4 +475,3 @@ void SetSysClock(void)
382475
*/
383476

384477
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
385-

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/PeripheralNames.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ typedef enum {
4545
} DACName;
4646

4747
typedef enum {
48-
UART_1 = (int)USART1_BASE,
48+
UART_1 = (int)USART1_BASE,
4949
UART_2 = (int)USART2_BASE,
5050
UART_3 = (int)USART3_BASE
5151
} UARTName;

0 commit comments

Comments
 (0)