Skip to content

Commit a519f94

Browse files
committed
Merge pull request #271 from bcostm/master
[NUCLEO_F030R8] Many improvements added
2 parents 14ad877 + 4ac1790 commit a519f94

File tree

12 files changed

+344
-94
lines changed

12 files changed

+344
-94
lines changed

libraries/mbed/targets/cmsis/TARGET_STM/TARGET_NUCLEO_F030R8/stm32f0xx.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
Timeout value
138138
*/
139139
#if !defined (HSE_STARTUP_TIMEOUT)
140-
#define HSE_STARTUP_TIMEOUT ((uint16_t)0x5000) /*!< Time out for HSE start up */
140+
#define HSE_STARTUP_TIMEOUT ((uint16_t)1000) /*!< Time out for HSE start up */
141141
#endif /* HSE_STARTUP_TIMEOUT */
142142

143143
/**

libraries/mbed/targets/cmsis/TARGET_STM/TARGET_NUCLEO_F030R8/system_stm32f0xx.c

Lines changed: 156 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,17 @@
4040
* value to your own configuration.
4141
*
4242
* 5. This file configures the system clock as follows:
43-
*=============================================================================
44-
*=============================================================================
45-
* System Clock source | HSI
4643
*-----------------------------------------------------------------------------
47-
* SYSCLK(Hz) | 8000000
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) |
4848
*-----------------------------------------------------------------------------
49-
* HCLK(Hz) | 8000000
49+
* SYSCLK(MHz) | 48 | 48
5050
*-----------------------------------------------------------------------------
51-
* AHB Prescaler | 1
51+
* AHBCLK (MHz) | 48 | 48
5252
*-----------------------------------------------------------------------------
53-
* APB Prescaler | 1
54-
*-----------------------------------------------------------------------------
55-
* HSE Frequency(Hz) | NA
56-
*----------------------------------------------------------------------------
57-
* PLLMUL | NA
58-
*-----------------------------------------------------------------------------
59-
* PREDIV | NA
60-
*-----------------------------------------------------------------------------
61-
* Flash Latency(WS) | 0
62-
*-----------------------------------------------------------------------------
63-
* Prefetch Buffer | ON
53+
* APBCLK (MHz) | 48 | 48
6454
*-----------------------------------------------------------------------------
6555
******************************************************************************
6656
* @attention
@@ -129,14 +119,19 @@
129119
* @{
130120
*/
131121

122+
/* Select the clock sources (other than HSI) to start with (0=OFF, 1=ON) */
123+
#define USE_PLL_HSE_EXTC (1) /* Use external clock */
124+
#define USE_PLL_HSE_XTAL (1) /* Use external xtal */
125+
132126
/**
133127
* @}
134128
*/
135129

136130
/** @addtogroup STM32F0xx_System_Private_Variables
137131
* @{
138132
*/
139-
uint32_t SystemCoreClock = 8000000;
133+
134+
uint32_t SystemCoreClock = 48000000;
140135
__I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
141136

142137
/**
@@ -147,7 +142,11 @@ __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}
147142
* @{
148143
*/
149144

150-
static void SetSysClock(void);
145+
#if (USE_PLL_HSE_XTAL != 0) || (USE_PLL_HSE_EXTC != 0)
146+
uint8_t SetSysClock_PLL_HSE(uint8_t bypass);
147+
#endif
148+
149+
uint8_t SetSysClock_PLL_HSI(void);
151150

152151
/**
153152
* @}
@@ -192,9 +191,6 @@ void SystemInit (void)
192191

193192
/* Disable all interrupts */
194193
RCC->CIR = 0x00000000;
195-
196-
/* Configure the System clock frequency, AHB/APBx prescalers and Flash settings */
197-
SetSysClock();
198194
}
199195

200196
/**
@@ -277,30 +273,155 @@ void SystemCoreClockUpdate (void)
277273
}
278274

279275
/**
280-
* @brief Configures the System clock frequency, AHB/APBx prescalers and Flash
281-
* settings.
282-
* @note This function should be called only once the RCC clock configuration
283-
* is reset to the default reset state (done in SystemInit() function).
276+
* @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
284277
* @param None
285278
* @retval None
286279
*/
287-
static void SetSysClock(void)
280+
void SetSysClock(void)
281+
{
282+
/* 1- Try to start with HSE and external clock */
283+
#if USE_PLL_HSE_EXTC != 0
284+
if (SetSysClock_PLL_HSE(1) == 0)
285+
#endif
286+
{
287+
/* 2- If fail try to start with HSE and external xtal */
288+
#if USE_PLL_HSE_XTAL != 0
289+
if (SetSysClock_PLL_HSE(0) == 0)
290+
#endif
291+
{
292+
/* 3- If fail start with HSI clock */
293+
if (SetSysClock_PLL_HSI() == 0)
294+
{
295+
while(1)
296+
{
297+
// [TODO] Put something here to tell the user that a problem occured...
298+
}
299+
}
300+
}
301+
}
302+
303+
// Output clock on MCO pin (PA8) for debugging purpose
304+
/*
305+
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
306+
GPIO_InitTypeDef GPIO_InitStructure;
307+
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
308+
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
309+
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
310+
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
311+
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
312+
GPIO_Init(GPIOA, &GPIO_InitStructure);
313+
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_0);
314+
// Output clock on MCO pin
315+
// Warning: only RCC_MCOPrescaler_1 is available on STM32F030x8 devices
316+
RCC_MCOConfig(RCC_MCOSource_SYSCLK, RCC_MCOPrescaler_1);
317+
*/
318+
}
319+
320+
#if (USE_PLL_HSE_XTAL != 0) || (USE_PLL_HSE_EXTC != 0)
321+
/******************************************************************************/
322+
/* PLL (clocked by HSE) used as System clock source */
323+
/******************************************************************************/
324+
uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
288325
{
326+
__IO uint32_t StartUpCounter = 0;
327+
__IO uint32_t HSEStatus = 0;
328+
329+
/* Bypass HSE: can be done only if HSE is OFF */
330+
RCC->CR &= ((uint32_t)~RCC_CR_HSEON); /* To be sure HSE is OFF */
331+
if (bypass != 0)
332+
{
333+
RCC->CR |= ((uint32_t)RCC_CR_HSEBYP);
334+
}
335+
else
336+
{
337+
RCC->CR &= ((uint32_t)~RCC_CR_HSEBYP);
338+
}
339+
340+
/* Enable HSE */
341+
RCC->CR |= ((uint32_t)RCC_CR_HSEON);
342+
343+
/* Wait till HSE is ready */
344+
do
345+
{
346+
HSEStatus = RCC->CR & RCC_CR_HSERDY;
347+
StartUpCounter++;
348+
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
349+
350+
/* Check if HSE has started correctly */
351+
if ((RCC->CR & RCC_CR_HSERDY) != RESET)
352+
{
353+
/* Enable Prefetch Buffer */
354+
FLASH->ACR |= FLASH_ACR_PRFTBE;
355+
356+
/* Enable Prefetch Buffer and set Flash Latency */
357+
FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY;
358+
359+
/* PLL configuration
360+
PLLCLK = 48 MHz (xtal 8 MHz * 6) */
361+
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
362+
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL6
363+
| RCC_CFGR_HPRE_DIV1 /* HCLK = 48 MHz */
364+
| RCC_CFGR_PPRE_DIV1); /* PCLK = 48 MHz */
365+
366+
/* Enable PLL */
367+
RCC->CR |= RCC_CR_PLLON;
368+
369+
/* Wait till PLL is ready */
370+
while((RCC->CR & RCC_CR_PLLRDY) == 0)
371+
{
372+
}
373+
374+
/* Select PLL as system clock source */
375+
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
376+
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
377+
378+
/* Wait till PLL is used as system clock source */
379+
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
380+
{
381+
}
382+
383+
return 1; // OK
384+
}
385+
else
386+
{
387+
return 0; // FAIL
388+
}
389+
}
390+
#endif
391+
289392
/******************************************************************************/
290-
/* HSI used as System clock source */
393+
/* PLL (clocked by HSI) used as System clock source */
291394
/******************************************************************************/
395+
uint8_t SetSysClock_PLL_HSI(void)
396+
{
397+
/* Enable Prefetch Buffer and set Flash Latency */
398+
FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY;
292399

293-
/* At this stage the HSI is already enabled and used as System clock source */
400+
/* PLL configuration
401+
PLLCLK = 48 MHz ((HSI 8 MHz / 2) * 12) */
402+
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
403+
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSI_Div2 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL12
404+
| RCC_CFGR_HPRE_DIV1 /* HCLK = 48 MHz */
405+
| RCC_CFGR_PPRE_DIV1); /* PCLK = 48 MHz */
294406

295-
/* Enable Prefetch Buffer and Flash 0 wait state */
296-
FLASH->ACR = FLASH_ACR_PRFTBE;
407+
/* Enable PLL */
408+
RCC->CR |= RCC_CR_PLLON;
297409

298-
/* HCLK = SYSCLK / 1 */
299-
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
410+
/* Wait till PLL is ready */
411+
while((RCC->CR & RCC_CR_PLLRDY) == 0)
412+
{
413+
}
414+
415+
/* Select PLL as system clock source */
416+
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
417+
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
300418

301-
/* PCLK = HCLK / 1 */
302-
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE_DIV1;
419+
/* Wait till PLL is used as system clock source */
420+
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
421+
{
422+
}
303423

424+
return 1; // OK
304425
}
305426

306427
/**

libraries/mbed/targets/cmsis/TARGET_STM/TARGET_NUCLEO_F030R8/system_stm32f0xx.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ extern uint32_t SystemCoreClock; /*!< System Clock Frequency (Core Cloc
9494

9595
extern void SystemInit(void);
9696
extern void SystemCoreClockUpdate(void);
97+
extern void SetSysClock(void);
98+
9799
/**
98100
* @}
99101
*/

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/analogin_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2727
*/
2828
#include "analogin_api.h"
29-
#include "wait_api.h"
3029

3130
#if DEVICE_ANALOGIN
3231

3332
#include "cmsis.h"
3433
#include "pinmap.h"
3534
#include "error.h"
35+
#include "wait_api.h"
3636

3737
static const PinMap PinMap_ADC[] = {
3838
{PA_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN0

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/i2c_api.c

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,31 +86,59 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
8686
void i2c_frequency(i2c_t *obj, int hz) {
8787
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
8888
I2C_InitTypeDef I2C_InitStructure;
89+
uint32_t tim = 0;
90+
91+
// Disable the Fast Mode Plus capability
92+
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); // Enable SYSCFG clock
93+
SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C1, DISABLE);
94+
SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C2, DISABLE);
8995

90-
// Values calculated with I2C_Timing_Configuration_V1.0.1.xls file (see AN4235)
91-
// with Rise time = 100ns and Fall time = 10ns
96+
/*
97+
Values calculated with I2C_Timing_Configuration_V1.0.1.xls file (see AN4235)
98+
* Standard mode (up to 100 kHz)
99+
* Fast Mode (up to 400 kHz)
100+
* Fast Mode Plus (up to 1 MHz)
101+
Below values obtained with:
102+
- I2C clock source = 8 MHz (HSI clock per default)
103+
- Analog filter delay = ON
104+
- Digital filter coefficient = 0
105+
- Rise time = 100 ns
106+
- Fall time = 10ns
107+
*/
92108
switch (hz) {
93109
case 100000:
94-
I2C_InitStructure.I2C_Timing = 0x00201D2B; // Standard mode
110+
tim = 0x00201D2B; // Standard mode
95111
break;
96112
case 200000:
97-
I2C_InitStructure.I2C_Timing = 0x0010021E; // Fast mode
113+
tim = 0x0010021E; // Fast Mode
98114
break;
99115
case 400000:
100-
I2C_InitStructure.I2C_Timing = 0x0010020A; // Fast mode
116+
tim = 0x0010020A; // Fast Mode
117+
break;
118+
case 1000000:
119+
tim = 0x00100001; // Fast Mode Plus
120+
// Enable the Fast Mode Plus capability
121+
if (obj->i2c == I2C_1) {
122+
SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C1, ENABLE);
123+
}
124+
if (obj->i2c == I2C_2) {
125+
SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C2, ENABLE);
126+
}
101127
break;
102128
default:
103-
error("Only 100kHz, 200kHz and 400kHz I2C frequencies are supported.");
129+
error("Only 100kHz, 200kHz, 400kHz and 1MHz I2C frequencies are supported.");
104130
break;
105131
}
106132

107133
// I2C configuration
134+
I2C_DeInit(i2c);
108135
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
109136
I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
110137
I2C_InitStructure.I2C_DigitalFilter = 0x00;
111138
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
112139
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
113140
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
141+
I2C_InitStructure.I2C_Timing = tim;
114142
I2C_Init(i2c, &I2C_InitStructure);
115143

116144
I2C_Cmd(i2c, ENABLE);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2014, STMicroelectronics
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
14+
* may be used to endorse or promote products derived from this software
15+
* without specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
#include "stm32f0xx.h"
29+
30+
// This function is called after RAM initialization and before main.
31+
void mbed_sdk_init() {
32+
/* Configure the System clock source, PLL Multiplier and Divider factors,
33+
AHB/APBx prescalers and Flash settings */
34+
SetSysClock();
35+
36+
// Update the SystemCoreClock variable.
37+
SystemCoreClockUpdate();
38+
}

0 commit comments

Comments
 (0)