mirror of https://github.com/ARMmbed/mbed-os.git
[NUCLEO_F103RB] Add automatic HSE/HSI clock configuration
And: - restart PLL in deepsleep function - add more pins for SPI (the mbed pinout will be updated soon)pull/235/head
parent
66791e645f
commit
8299b1e6a0
|
@ -35,7 +35,23 @@
|
||||||
* When HSE is used as system clock source, directly or through PLL, and you
|
* When HSE is used as system clock source, directly or through PLL, and you
|
||||||
* are using different crystal you have to adapt the HSE value to your own
|
* are using different crystal you have to adapt the HSE value to your own
|
||||||
* configuration.
|
* configuration.
|
||||||
*
|
* 5. This file configures the system clock as follows:
|
||||||
|
*-----------------------------------------------------------------------------
|
||||||
|
* System clock source | 1- PLL_HSE_EXTC | 3- PLL_HSI
|
||||||
|
* | (external 8 MHz clock) | (internal 8 MHz)
|
||||||
|
* | 2- PLL_HSE_XTAL |
|
||||||
|
* | (external 8 MHz xtal) |
|
||||||
|
*-----------------------------------------------------------------------------
|
||||||
|
* SYSCLK(MHz) | 72 | 64
|
||||||
|
*-----------------------------------------------------------------------------
|
||||||
|
* AHBCLK (MHz) | 72 | 64
|
||||||
|
*-----------------------------------------------------------------------------
|
||||||
|
* APB1CLK (MHz) | 36 | 32
|
||||||
|
*-----------------------------------------------------------------------------
|
||||||
|
* APB2CLK (MHz) | 72 | 64
|
||||||
|
*-----------------------------------------------------------------------------
|
||||||
|
* USB capable (48 MHz precise clock) | YES | NO
|
||||||
|
*-----------------------------------------------------------------------------
|
||||||
*******************************************************************************
|
*******************************************************************************
|
||||||
* Copyright (c) 2014, STMicroelectronics
|
* Copyright (c) 2014, STMicroelectronics
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
|
@ -95,55 +111,11 @@
|
||||||
* @{
|
* @{
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*!< Uncomment the line corresponding to the desired System clock (SYSCLK)
|
|
||||||
frequency (after reset the HSI is used as SYSCLK source)
|
|
||||||
|
|
||||||
IMPORTANT NOTE:
|
|
||||||
==============
|
|
||||||
1. After each device reset the HSI is used as System clock source.
|
|
||||||
|
|
||||||
2. Please make sure that the selected System clock doesn't exceed your device's
|
|
||||||
maximum frequency.
|
|
||||||
|
|
||||||
3. If none of the define below is enabled, the HSI is used as System clock
|
|
||||||
source.
|
|
||||||
|
|
||||||
4. The System clock configuration functions provided within this file assume that:
|
|
||||||
- For Low, Medium and High density Value line devices an external 8MHz
|
|
||||||
crystal is used to drive the System clock.
|
|
||||||
- For Low, Medium and High density devices an external 8MHz crystal is
|
|
||||||
used to drive the System clock.
|
|
||||||
- For Connectivity line devices an external 25MHz crystal is used to drive
|
|
||||||
the System clock.
|
|
||||||
If you are using different crystal you have to adapt those functions accordingly.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
|
|
||||||
/* #define SYSCLK_FREQ_HSE HSE_VALUE */
|
|
||||||
/* #define SYSCLK_FREQ_24MHz 24000000 */
|
|
||||||
#else
|
|
||||||
/* #define SYSCLK_FREQ_HSE HSE_VALUE */
|
|
||||||
/* #define SYSCLK_FREQ_24MHz 24000000 */
|
|
||||||
/* #define SYSCLK_FREQ_36MHz 36000000 */
|
|
||||||
/* #define SYSCLK_FREQ_48MHz 48000000 */
|
|
||||||
/* #define SYSCLK_FREQ_56MHz 56000000 */
|
|
||||||
/* #define SYSCLK_FREQ_72MHz 72000000 */
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*!< Uncomment the following line if you need to use external SRAM mounted
|
|
||||||
on STM3210E-EVAL board (STM32 High density and XL-density devices) or on
|
|
||||||
STM32100E-EVAL board (STM32 High-density value line devices) as data memory */
|
|
||||||
#if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
|
|
||||||
/* #define DATA_IN_ExtSRAM */
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*!< Uncomment the following line if you need to relocate your vector Table in
|
/*!< Uncomment the following line if you need to relocate your vector Table in
|
||||||
Internal SRAM. */
|
Internal SRAM. */
|
||||||
/* #define VECT_TAB_SRAM */
|
/* #define VECT_TAB_SRAM */
|
||||||
#define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field.
|
#define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field.
|
||||||
This value must be a multiple of 0x200. */
|
This value must be a multiple of 0x200. */
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @}
|
* @}
|
||||||
*/
|
*/
|
||||||
|
@ -152,6 +124,10 @@
|
||||||
* @{
|
* @{
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/* Select the clock sources (other than HSI) to start with (0=OFF, 1=ON) */
|
||||||
|
#define USE_PLL_HSE_EXTC (1) /* Use external clock */
|
||||||
|
#define USE_PLL_HSE_XTAL (1) /* Use external xtal */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @}
|
* @}
|
||||||
*/
|
*/
|
||||||
|
@ -160,24 +136,7 @@
|
||||||
* @{
|
* @{
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*******************************************************************************
|
uint32_t SystemCoreClock = 64000000; /* Default with HSI. Will be updated if HSE is used */
|
||||||
* Clock Definitions
|
|
||||||
*******************************************************************************/
|
|
||||||
#ifdef SYSCLK_FREQ_HSE
|
|
||||||
uint32_t SystemCoreClock = SYSCLK_FREQ_HSE; /*!< System Clock Frequency (Core Clock) */
|
|
||||||
#elif defined SYSCLK_FREQ_24MHz
|
|
||||||
uint32_t SystemCoreClock = SYSCLK_FREQ_24MHz; /*!< System Clock Frequency (Core Clock) */
|
|
||||||
#elif defined SYSCLK_FREQ_36MHz
|
|
||||||
uint32_t SystemCoreClock = SYSCLK_FREQ_36MHz; /*!< System Clock Frequency (Core Clock) */
|
|
||||||
#elif defined SYSCLK_FREQ_48MHz
|
|
||||||
uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz; /*!< System Clock Frequency (Core Clock) */
|
|
||||||
#elif defined SYSCLK_FREQ_56MHz
|
|
||||||
uint32_t SystemCoreClock = SYSCLK_FREQ_56MHz; /*!< System Clock Frequency (Core Clock) */
|
|
||||||
#elif defined SYSCLK_FREQ_72MHz
|
|
||||||
uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz; /*!< System Clock Frequency (Core Clock) */
|
|
||||||
#else /*!< HSI Selected as System Clock source */
|
|
||||||
uint32_t SystemCoreClock = HSI_VALUE; /*!< System Clock Frequency (Core Clock) */
|
|
||||||
#endif
|
|
||||||
|
|
||||||
__I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
|
__I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
|
||||||
/**
|
/**
|
||||||
|
@ -188,22 +147,14 @@ __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}
|
||||||
* @{
|
* @{
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static void SetSysClock(void);
|
void SetSysClock(void);
|
||||||
|
|
||||||
#ifdef SYSCLK_FREQ_HSE
|
#if (USE_PLL_HSE_XTAL != 0) || (USE_PLL_HSE_EXTC != 0)
|
||||||
static void SetSysClockToHSE(void);
|
uint8_t SetSysClock_PLL_HSE(uint8_t bypass);
|
||||||
#elif defined SYSCLK_FREQ_24MHz
|
|
||||||
static void SetSysClockTo24(void);
|
|
||||||
#elif defined SYSCLK_FREQ_36MHz
|
|
||||||
static void SetSysClockTo36(void);
|
|
||||||
#elif defined SYSCLK_FREQ_48MHz
|
|
||||||
static void SetSysClockTo48(void);
|
|
||||||
#elif defined SYSCLK_FREQ_56MHz
|
|
||||||
static void SetSysClockTo56(void);
|
|
||||||
#elif defined SYSCLK_FREQ_72MHz
|
|
||||||
static void SetSysClockTo72(void);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
uint8_t SetSysClock_PLL_HSI(void);
|
||||||
|
|
||||||
#ifdef DATA_IN_ExtSRAM
|
#ifdef DATA_IN_ExtSRAM
|
||||||
static void SystemInit_ExtMemCtl(void);
|
static void SystemInit_ExtMemCtl(void);
|
||||||
#endif /* DATA_IN_ExtSRAM */
|
#endif /* DATA_IN_ExtSRAM */
|
||||||
|
@ -431,24 +382,41 @@ void SystemCoreClockUpdate (void)
|
||||||
* @param None
|
* @param None
|
||||||
* @retval None
|
* @retval None
|
||||||
*/
|
*/
|
||||||
static void SetSysClock(void)
|
void SetSysClock(void)
|
||||||
{
|
{
|
||||||
#ifdef SYSCLK_FREQ_HSE
|
/* 1- Try to start with HSE and external clock */
|
||||||
SetSysClockToHSE();
|
#if USE_PLL_HSE_EXTC != 0
|
||||||
#elif defined SYSCLK_FREQ_24MHz
|
if (SetSysClock_PLL_HSE(1) == 0)
|
||||||
SetSysClockTo24();
|
|
||||||
#elif defined SYSCLK_FREQ_36MHz
|
|
||||||
SetSysClockTo36();
|
|
||||||
#elif defined SYSCLK_FREQ_48MHz
|
|
||||||
SetSysClockTo48();
|
|
||||||
#elif defined SYSCLK_FREQ_56MHz
|
|
||||||
SetSysClockTo56();
|
|
||||||
#elif defined SYSCLK_FREQ_72MHz
|
|
||||||
SetSysClockTo72();
|
|
||||||
#endif
|
#endif
|
||||||
|
{
|
||||||
|
/* 2- If fail try to start with HSE and external xtal */
|
||||||
|
#if USE_PLL_HSE_XTAL != 0
|
||||||
|
if (SetSysClock_PLL_HSE(0) == 0)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
/* 3- If fail start with HSI clock */
|
||||||
|
if (SetSysClock_PLL_HSI() == 0)
|
||||||
|
{
|
||||||
|
while(1)
|
||||||
|
{
|
||||||
|
// [TODO] Put something here to tell the user that a problem occured...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* If none of the define above is enabled, the HSI is used as System clock
|
/* Output SYSCLK on MCO pin(PA8) for debugging purpose */
|
||||||
source (default after reset) */
|
/*
|
||||||
|
// Enable GPIOA clock
|
||||||
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
||||||
|
GPIO_InitTypeDef GPIO_InitStructure;
|
||||||
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
|
||||||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||||||
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||||
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||||||
|
// Select the clock to output
|
||||||
|
RCC_MCOConfig(RCC_MCO_SYSCLK);
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -504,469 +472,57 @@ void SystemInit_ExtMemCtl(void)
|
||||||
}
|
}
|
||||||
#endif /* DATA_IN_ExtSRAM */
|
#endif /* DATA_IN_ExtSRAM */
|
||||||
|
|
||||||
#ifdef SYSCLK_FREQ_HSE
|
#if (USE_PLL_HSE_XTAL != 0) || (USE_PLL_HSE_EXTC != 0)
|
||||||
/**
|
/******************************************************************************/
|
||||||
* @brief Selects HSE as System clock source and configure HCLK, PCLK2
|
/* PLL (clocked by HSE) used as System clock source */
|
||||||
* and PCLK1 prescalers.
|
/******************************************************************************/
|
||||||
* @note This function should be used only after reset.
|
uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
|
||||||
* @param None
|
|
||||||
* @retval None
|
|
||||||
*/
|
|
||||||
static void SetSysClockToHSE(void)
|
|
||||||
{
|
{
|
||||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
__IO uint32_t StartUpCounter = 0;
|
||||||
|
__IO uint32_t HSEStatus = 0;
|
||||||
|
|
||||||
|
/* Bypass HSE: can be done only if HSE is OFF */
|
||||||
|
RCC->CR &= ((uint32_t)~RCC_CR_HSEON); /* To be sure HSE is OFF */
|
||||||
|
if (bypass != 0)
|
||||||
|
{
|
||||||
|
RCC->CR |= ((uint32_t)RCC_CR_HSEBYP);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
RCC->CR &= ((uint32_t)~RCC_CR_HSEBYP);
|
||||||
|
}
|
||||||
|
|
||||||
/* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
|
|
||||||
/* Enable HSE */
|
/* Enable HSE */
|
||||||
RCC->CR |= ((uint32_t)RCC_CR_HSEON);
|
RCC->CR |= ((uint32_t)RCC_CR_HSEON);
|
||||||
|
|
||||||
/* Wait till HSE is ready and if Time out is reached exit */
|
/* Wait till HSE is ready */
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
HSEStatus = RCC->CR & RCC_CR_HSERDY;
|
HSEStatus = RCC->CR & RCC_CR_HSERDY;
|
||||||
StartUpCounter++;
|
StartUpCounter++;
|
||||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
||||||
|
|
||||||
|
/* Check if HSE has started correctly */
|
||||||
if ((RCC->CR & RCC_CR_HSERDY) != RESET)
|
if ((RCC->CR & RCC_CR_HSERDY) != RESET)
|
||||||
{
|
|
||||||
HSEStatus = (uint32_t)0x01;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
HSEStatus = (uint32_t)0x00;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (HSEStatus == (uint32_t)0x01)
|
|
||||||
{
|
|
||||||
|
|
||||||
#if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL && !defined STM32F10X_HD_VL
|
|
||||||
/* Enable Prefetch Buffer */
|
|
||||||
FLASH->ACR |= FLASH_ACR_PRFTBE;
|
|
||||||
|
|
||||||
/* Flash 0 wait state */
|
|
||||||
FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
|
|
||||||
|
|
||||||
#ifndef STM32F10X_CL
|
|
||||||
FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
|
|
||||||
#else
|
|
||||||
if (HSE_VALUE <= 24000000)
|
|
||||||
{
|
|
||||||
FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
|
|
||||||
}
|
|
||||||
#endif /* STM32F10X_CL */
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* HCLK = SYSCLK */
|
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
|
|
||||||
|
|
||||||
/* PCLK2 = HCLK */
|
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
|
|
||||||
|
|
||||||
/* PCLK1 = HCLK */
|
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
|
|
||||||
|
|
||||||
/* Select HSE as system clock source */
|
|
||||||
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
|
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSE;
|
|
||||||
|
|
||||||
/* Wait till HSE is used as system clock source */
|
|
||||||
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x04)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{ /* If HSE fails to start-up, the application will have wrong clock
|
|
||||||
configuration. User can add here some code to deal with this error */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#elif defined SYSCLK_FREQ_24MHz
|
|
||||||
/**
|
|
||||||
* @brief Sets System clock frequency to 24MHz and configure HCLK, PCLK2
|
|
||||||
* and PCLK1 prescalers.
|
|
||||||
* @note This function should be used only after reset.
|
|
||||||
* @param None
|
|
||||||
* @retval None
|
|
||||||
*/
|
|
||||||
static void SetSysClockTo24(void)
|
|
||||||
{
|
|
||||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
|
||||||
|
|
||||||
/* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
|
|
||||||
/* Enable HSE */
|
|
||||||
RCC->CR |= ((uint32_t)RCC_CR_HSEON);
|
|
||||||
|
|
||||||
/* Wait till HSE is ready and if Time out is reached exit */
|
|
||||||
do
|
|
||||||
{
|
|
||||||
HSEStatus = RCC->CR & RCC_CR_HSERDY;
|
|
||||||
StartUpCounter++;
|
|
||||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
|
||||||
|
|
||||||
if ((RCC->CR & RCC_CR_HSERDY) != RESET)
|
|
||||||
{
|
|
||||||
HSEStatus = (uint32_t)0x01;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
HSEStatus = (uint32_t)0x00;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (HSEStatus == (uint32_t)0x01)
|
|
||||||
{
|
|
||||||
#if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL && !defined STM32F10X_HD_VL
|
|
||||||
/* Enable Prefetch Buffer */
|
|
||||||
FLASH->ACR |= FLASH_ACR_PRFTBE;
|
|
||||||
|
|
||||||
/* Flash 0 wait state */
|
|
||||||
FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
|
|
||||||
FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* HCLK = SYSCLK */
|
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
|
|
||||||
|
|
||||||
/* PCLK2 = HCLK */
|
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
|
|
||||||
|
|
||||||
/* PCLK1 = HCLK */
|
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
|
|
||||||
|
|
||||||
#ifdef STM32F10X_CL
|
|
||||||
/* Configure PLLs ------------------------------------------------------*/
|
|
||||||
/* PLL configuration: PLLCLK = PREDIV1 * 6 = 24 MHz */
|
|
||||||
RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
|
|
||||||
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
|
|
||||||
RCC_CFGR_PLLMULL6);
|
|
||||||
|
|
||||||
/* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
|
|
||||||
/* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */
|
|
||||||
RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
|
|
||||||
RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
|
|
||||||
RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
|
|
||||||
RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV10);
|
|
||||||
|
|
||||||
/* Enable PLL2 */
|
|
||||||
RCC->CR |= RCC_CR_PLL2ON;
|
|
||||||
/* Wait till PLL2 is ready */
|
|
||||||
while((RCC->CR & RCC_CR_PLL2RDY) == 0)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
#elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
|
|
||||||
/* PLL configuration: = (HSE / 2) * 6 = 24 MHz */
|
|
||||||
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
|
|
||||||
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1_Div2 | RCC_CFGR_PLLMULL6);
|
|
||||||
#else
|
|
||||||
/* PLL configuration: = (HSE / 2) * 6 = 24 MHz */
|
|
||||||
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
|
|
||||||
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL6);
|
|
||||||
#endif /* STM32F10X_CL */
|
|
||||||
|
|
||||||
/* Enable PLL */
|
|
||||||
RCC->CR |= RCC_CR_PLLON;
|
|
||||||
|
|
||||||
/* Wait till PLL is ready */
|
|
||||||
while((RCC->CR & RCC_CR_PLLRDY) == 0)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Select PLL as system clock source */
|
|
||||||
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
|
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
|
|
||||||
|
|
||||||
/* Wait till PLL is used as system clock source */
|
|
||||||
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{ /* If HSE fails to start-up, the application will have wrong clock
|
|
||||||
configuration. User can add here some code to deal with this error */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#elif defined SYSCLK_FREQ_36MHz
|
|
||||||
/**
|
|
||||||
* @brief Sets System clock frequency to 36MHz and configure HCLK, PCLK2
|
|
||||||
* and PCLK1 prescalers.
|
|
||||||
* @note This function should be used only after reset.
|
|
||||||
* @param None
|
|
||||||
* @retval None
|
|
||||||
*/
|
|
||||||
static void SetSysClockTo36(void)
|
|
||||||
{
|
|
||||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
|
||||||
|
|
||||||
/* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
|
|
||||||
/* Enable HSE */
|
|
||||||
RCC->CR |= ((uint32_t)RCC_CR_HSEON);
|
|
||||||
|
|
||||||
/* Wait till HSE is ready and if Time out is reached exit */
|
|
||||||
do
|
|
||||||
{
|
|
||||||
HSEStatus = RCC->CR & RCC_CR_HSERDY;
|
|
||||||
StartUpCounter++;
|
|
||||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
|
||||||
|
|
||||||
if ((RCC->CR & RCC_CR_HSERDY) != RESET)
|
|
||||||
{
|
|
||||||
HSEStatus = (uint32_t)0x01;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
HSEStatus = (uint32_t)0x00;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (HSEStatus == (uint32_t)0x01)
|
|
||||||
{
|
{
|
||||||
/* Enable Prefetch Buffer */
|
/* Enable Prefetch Buffer */
|
||||||
FLASH->ACR |= FLASH_ACR_PRFTBE;
|
FLASH->ACR |= FLASH_ACR_PRFTBE;
|
||||||
|
|
||||||
/* Flash 1 wait state */
|
/* Flash wait states
|
||||||
FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
|
0WS for 0 < SYSCLK <= 24 MHz
|
||||||
FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
|
1WS for 24 < SYSCLK <= 48 MHz
|
||||||
|
2WS for 48 < SYSCLK <= 72 MHz */
|
||||||
/* HCLK = SYSCLK */
|
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
|
|
||||||
|
|
||||||
/* PCLK2 = HCLK */
|
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
|
|
||||||
|
|
||||||
/* PCLK1 = HCLK */
|
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
|
|
||||||
|
|
||||||
#ifdef STM32F10X_CL
|
|
||||||
/* Configure PLLs ------------------------------------------------------*/
|
|
||||||
|
|
||||||
/* PLL configuration: PLLCLK = PREDIV1 * 9 = 36 MHz */
|
|
||||||
RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
|
|
||||||
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
|
|
||||||
RCC_CFGR_PLLMULL9);
|
|
||||||
|
|
||||||
/*!< PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
|
|
||||||
/* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */
|
|
||||||
|
|
||||||
RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
|
|
||||||
RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
|
|
||||||
RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
|
|
||||||
RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV10);
|
|
||||||
|
|
||||||
/* Enable PLL2 */
|
|
||||||
RCC->CR |= RCC_CR_PLL2ON;
|
|
||||||
/* Wait till PLL2 is ready */
|
|
||||||
while((RCC->CR & RCC_CR_PLL2RDY) == 0)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
/* PLL configuration: PLLCLK = (HSE / 2) * 9 = 36 MHz */
|
|
||||||
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
|
|
||||||
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL9);
|
|
||||||
#endif /* STM32F10X_CL */
|
|
||||||
|
|
||||||
/* Enable PLL */
|
|
||||||
RCC->CR |= RCC_CR_PLLON;
|
|
||||||
|
|
||||||
/* Wait till PLL is ready */
|
|
||||||
while((RCC->CR & RCC_CR_PLLRDY) == 0)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Select PLL as system clock source */
|
|
||||||
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
|
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
|
|
||||||
|
|
||||||
/* Wait till PLL is used as system clock source */
|
|
||||||
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{ /* If HSE fails to start-up, the application will have wrong clock
|
|
||||||
configuration. User can add here some code to deal with this error */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#elif defined SYSCLK_FREQ_48MHz
|
|
||||||
/**
|
|
||||||
* @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2
|
|
||||||
* and PCLK1 prescalers.
|
|
||||||
* @note This function should be used only after reset.
|
|
||||||
* @param None
|
|
||||||
* @retval None
|
|
||||||
*/
|
|
||||||
static void SetSysClockTo48(void)
|
|
||||||
{
|
|
||||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
|
||||||
|
|
||||||
/* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
|
|
||||||
/* Enable HSE */
|
|
||||||
RCC->CR |= ((uint32_t)RCC_CR_HSEON);
|
|
||||||
|
|
||||||
/* Wait till HSE is ready and if Time out is reached exit */
|
|
||||||
do
|
|
||||||
{
|
|
||||||
HSEStatus = RCC->CR & RCC_CR_HSERDY;
|
|
||||||
StartUpCounter++;
|
|
||||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
|
||||||
|
|
||||||
if ((RCC->CR & RCC_CR_HSERDY) != RESET)
|
|
||||||
{
|
|
||||||
HSEStatus = (uint32_t)0x01;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
HSEStatus = (uint32_t)0x00;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (HSEStatus == (uint32_t)0x01)
|
|
||||||
{
|
|
||||||
/* Enable Prefetch Buffer */
|
|
||||||
FLASH->ACR |= FLASH_ACR_PRFTBE;
|
|
||||||
|
|
||||||
/* Flash 1 wait state */
|
|
||||||
FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
|
|
||||||
FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
|
|
||||||
|
|
||||||
/* HCLK = SYSCLK */
|
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
|
|
||||||
|
|
||||||
/* PCLK2 = HCLK */
|
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
|
|
||||||
|
|
||||||
/* PCLK1 = HCLK */
|
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
|
|
||||||
|
|
||||||
#ifdef STM32F10X_CL
|
|
||||||
/* Configure PLLs ------------------------------------------------------*/
|
|
||||||
/* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
|
|
||||||
/* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
|
|
||||||
|
|
||||||
RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
|
|
||||||
RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
|
|
||||||
RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
|
|
||||||
RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
|
|
||||||
|
|
||||||
/* Enable PLL2 */
|
|
||||||
RCC->CR |= RCC_CR_PLL2ON;
|
|
||||||
/* Wait till PLL2 is ready */
|
|
||||||
while((RCC->CR & RCC_CR_PLL2RDY) == 0)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* PLL configuration: PLLCLK = PREDIV1 * 6 = 48 MHz */
|
|
||||||
RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
|
|
||||||
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
|
|
||||||
RCC_CFGR_PLLMULL6);
|
|
||||||
#else
|
|
||||||
/* PLL configuration: PLLCLK = HSE * 6 = 48 MHz */
|
|
||||||
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
|
|
||||||
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL6);
|
|
||||||
#endif /* STM32F10X_CL */
|
|
||||||
|
|
||||||
/* Enable PLL */
|
|
||||||
RCC->CR |= RCC_CR_PLLON;
|
|
||||||
|
|
||||||
/* Wait till PLL is ready */
|
|
||||||
while((RCC->CR & RCC_CR_PLLRDY) == 0)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Select PLL as system clock source */
|
|
||||||
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
|
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
|
|
||||||
|
|
||||||
/* Wait till PLL is used as system clock source */
|
|
||||||
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{ /* If HSE fails to start-up, the application will have wrong clock
|
|
||||||
configuration. User can add here some code to deal with this error */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#elif defined SYSCLK_FREQ_56MHz
|
|
||||||
/**
|
|
||||||
* @brief Sets System clock frequency to 56MHz and configure HCLK, PCLK2
|
|
||||||
* and PCLK1 prescalers.
|
|
||||||
* @note This function should be used only after reset.
|
|
||||||
* @param None
|
|
||||||
* @retval None
|
|
||||||
*/
|
|
||||||
static void SetSysClockTo56(void)
|
|
||||||
{
|
|
||||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
|
||||||
|
|
||||||
/* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
|
|
||||||
/* Enable HSE */
|
|
||||||
RCC->CR |= ((uint32_t)RCC_CR_HSEON);
|
|
||||||
|
|
||||||
/* Wait till HSE is ready and if Time out is reached exit */
|
|
||||||
do
|
|
||||||
{
|
|
||||||
HSEStatus = RCC->CR & RCC_CR_HSERDY;
|
|
||||||
StartUpCounter++;
|
|
||||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
|
||||||
|
|
||||||
if ((RCC->CR & RCC_CR_HSERDY) != RESET)
|
|
||||||
{
|
|
||||||
HSEStatus = (uint32_t)0x01;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
HSEStatus = (uint32_t)0x00;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (HSEStatus == (uint32_t)0x01)
|
|
||||||
{
|
|
||||||
/* Enable Prefetch Buffer */
|
|
||||||
FLASH->ACR |= FLASH_ACR_PRFTBE;
|
|
||||||
|
|
||||||
/* Flash 2 wait state */
|
|
||||||
FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
|
FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
|
||||||
FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
|
FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
|
||||||
|
|
||||||
/* HCLK = SYSCLK */
|
/* PLL configuration */
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
|
/* SYSCLK = 72 MHz (8 MHz * 9) */
|
||||||
|
|
||||||
/* PCLK2 = HCLK */
|
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
|
|
||||||
|
|
||||||
/* PCLK1 = HCLK */
|
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
|
|
||||||
|
|
||||||
#ifdef STM32F10X_CL
|
|
||||||
/* Configure PLLs ------------------------------------------------------*/
|
|
||||||
/* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
|
|
||||||
/* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
|
|
||||||
|
|
||||||
RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
|
|
||||||
RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
|
|
||||||
RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
|
|
||||||
RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
|
|
||||||
|
|
||||||
/* Enable PLL2 */
|
|
||||||
RCC->CR |= RCC_CR_PLL2ON;
|
|
||||||
/* Wait till PLL2 is ready */
|
|
||||||
while((RCC->CR & RCC_CR_PLL2RDY) == 0)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* PLL configuration: PLLCLK = PREDIV1 * 7 = 56 MHz */
|
|
||||||
RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
|
|
||||||
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
|
|
||||||
RCC_CFGR_PLLMULL7);
|
|
||||||
#else
|
|
||||||
/* PLL configuration: PLLCLK = HSE * 7 = 56 MHz */
|
|
||||||
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
|
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
|
||||||
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL7);
|
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9
|
||||||
|
| RCC_CFGR_HPRE_DIV1 /* HCLK = 72 MHz */
|
||||||
#endif /* STM32F10X_CL */
|
| RCC_CFGR_PPRE2_DIV1 /* PCLK2 = 72 MHz */
|
||||||
|
| RCC_CFGR_PPRE1_DIV2); /* PCLK1 = 36 MHz */
|
||||||
|
/* USBCLK = 48 MHz (72 MHz / 1.5) --> USB OK */
|
||||||
|
|
||||||
/* Enable PLL */
|
/* Enable PLL */
|
||||||
RCC->CR |= RCC_CR_PLLON;
|
RCC->CR |= RCC_CR_PLLON;
|
||||||
|
@ -981,123 +537,64 @@ static void SetSysClockTo56(void)
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
|
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
|
||||||
|
|
||||||
/* Wait till PLL is used as system clock source */
|
/* Wait till PLL is used as system clock source */
|
||||||
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
|
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{ /* If HSE fails to start-up, the application will have wrong clock
|
|
||||||
configuration. User can add here some code to deal with this error */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#elif defined SYSCLK_FREQ_72MHz
|
return 1; // OK
|
||||||
/**
|
|
||||||
* @brief Sets System clock frequency to 72MHz and configure HCLK, PCLK2
|
|
||||||
* and PCLK1 prescalers.
|
|
||||||
* @note This function should be used only after reset.
|
|
||||||
* @param None
|
|
||||||
* @retval None
|
|
||||||
*/
|
|
||||||
static void SetSysClockTo72(void)
|
|
||||||
{
|
|
||||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
|
||||||
|
|
||||||
/* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
|
|
||||||
/* Enable HSE */
|
|
||||||
RCC->CR |= ((uint32_t)RCC_CR_HSEON);
|
|
||||||
|
|
||||||
/* Wait till HSE is ready and if Time out is reached exit */
|
|
||||||
do
|
|
||||||
{
|
|
||||||
HSEStatus = RCC->CR & RCC_CR_HSERDY;
|
|
||||||
StartUpCounter++;
|
|
||||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
|
||||||
|
|
||||||
if ((RCC->CR & RCC_CR_HSERDY) != RESET)
|
|
||||||
{
|
|
||||||
HSEStatus = (uint32_t)0x01;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
HSEStatus = (uint32_t)0x00;
|
return 0; // FAIL
|
||||||
}
|
|
||||||
|
|
||||||
if (HSEStatus == (uint32_t)0x01)
|
|
||||||
{
|
|
||||||
/* Enable Prefetch Buffer */
|
|
||||||
FLASH->ACR |= FLASH_ACR_PRFTBE;
|
|
||||||
|
|
||||||
/* Flash 2 wait state */
|
|
||||||
FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
|
|
||||||
FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
|
|
||||||
|
|
||||||
|
|
||||||
/* HCLK = SYSCLK */
|
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
|
|
||||||
|
|
||||||
/* PCLK2 = HCLK */
|
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
|
|
||||||
|
|
||||||
/* PCLK1 = HCLK */
|
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
|
|
||||||
|
|
||||||
#ifdef STM32F10X_CL
|
|
||||||
/* Configure PLLs ------------------------------------------------------*/
|
|
||||||
/* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
|
|
||||||
/* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
|
|
||||||
|
|
||||||
RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
|
|
||||||
RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
|
|
||||||
RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
|
|
||||||
RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
|
|
||||||
|
|
||||||
/* Enable PLL2 */
|
|
||||||
RCC->CR |= RCC_CR_PLL2ON;
|
|
||||||
/* Wait till PLL2 is ready */
|
|
||||||
while((RCC->CR & RCC_CR_PLL2RDY) == 0)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* PLL configuration: PLLCLK = PREDIV1 * 9 = 72 MHz */
|
|
||||||
RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
|
|
||||||
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
|
|
||||||
RCC_CFGR_PLLMULL9);
|
|
||||||
#else
|
|
||||||
/* PLL configuration: PLLCLK = HSE * 9 = 72 MHz */
|
|
||||||
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |
|
|
||||||
RCC_CFGR_PLLMULL));
|
|
||||||
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9);
|
|
||||||
#endif /* STM32F10X_CL */
|
|
||||||
|
|
||||||
/* Enable PLL */
|
|
||||||
RCC->CR |= RCC_CR_PLLON;
|
|
||||||
|
|
||||||
/* Wait till PLL is ready */
|
|
||||||
while((RCC->CR & RCC_CR_PLLRDY) == 0)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Select PLL as system clock source */
|
|
||||||
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
|
|
||||||
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
|
|
||||||
|
|
||||||
/* Wait till PLL is used as system clock source */
|
|
||||||
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{ /* If HSE fails to start-up, the application will have wrong clock
|
|
||||||
configuration. User can add here some code to deal with this error */
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
/******************************************************************************/
|
||||||
* @}
|
/* PLL (clocked by HSI) used as System clock source */
|
||||||
*/
|
/******************************************************************************/
|
||||||
|
uint8_t SetSysClock_PLL_HSI(void)
|
||||||
|
{
|
||||||
|
__IO uint32_t StartUpCounter = 0;
|
||||||
|
|
||||||
|
/* Enable Prefetch Buffer */
|
||||||
|
FLASH->ACR |= FLASH_ACR_PRFTBE;
|
||||||
|
|
||||||
|
/* Flash wait states
|
||||||
|
0WS for 0 < SYSCLK <= 24 MHz
|
||||||
|
1WS for 24 < SYSCLK <= 48 MHz
|
||||||
|
2WS for 48 < SYSCLK <= 72 MHz */
|
||||||
|
FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
|
||||||
|
FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
|
||||||
|
|
||||||
|
/* PLL configuration
|
||||||
|
PLLCLK = 64 MHz (HSI/2 * 16) */
|
||||||
|
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
|
||||||
|
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSI_Div2 | RCC_CFGR_PLLMULL16
|
||||||
|
| RCC_CFGR_HPRE_DIV1 /* HCLK = 64 MHz */
|
||||||
|
| RCC_CFGR_PPRE2_DIV1 /* PCLK2 = 64 MHz */
|
||||||
|
| RCC_CFGR_PPRE1_DIV2); /* PCLK1 = 32 MHz */
|
||||||
|
/* USBCLK = 42.667 MHz (64 MHz / 1.5) --> USB NOT POSSIBLE */
|
||||||
|
|
||||||
|
/* Enable PLL */
|
||||||
|
RCC->CR |= RCC_CR_PLLON;
|
||||||
|
|
||||||
|
/* Wait till PLL is ready */
|
||||||
|
while((RCC->CR & RCC_CR_PLLRDY) == 0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Select PLL as system clock source */
|
||||||
|
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
|
||||||
|
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
|
||||||
|
|
||||||
|
/* Wait till PLL is used as system clock source */
|
||||||
|
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1; // OK
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @}
|
* @}
|
||||||
|
@ -1106,4 +603,9 @@ static void SetSysClockTo72(void)
|
||||||
/**
|
/**
|
||||||
* @}
|
* @}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @}
|
||||||
|
*/
|
||||||
|
|
||||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
/* mbed Microcontroller Library
|
||||||
|
* Copyright (c) 2014, STMicroelectronics
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||||
|
* may be used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
extern void SystemCoreClockUpdate(void);
|
||||||
|
|
||||||
|
// This function is called after RAM initialization and before main.
|
||||||
|
void mbed_sdk_init() {
|
||||||
|
// Update the SystemCoreClock variable.
|
||||||
|
SystemCoreClockUpdate();
|
||||||
|
}
|
|
@ -30,6 +30,9 @@
|
||||||
#include "sleep_api.h"
|
#include "sleep_api.h"
|
||||||
#include "cmsis.h"
|
#include "cmsis.h"
|
||||||
|
|
||||||
|
// This function is in the system_stm32f10x.c file
|
||||||
|
extern void SetSysClock(void);
|
||||||
|
|
||||||
void sleep(void)
|
void sleep(void)
|
||||||
{
|
{
|
||||||
// Disable us_ticker update interrupt
|
// Disable us_ticker update interrupt
|
||||||
|
@ -53,6 +56,9 @@ void deepsleep(void)
|
||||||
// Request to enter STOP mode with regulator in low power mode
|
// Request to enter STOP mode with regulator in low power mode
|
||||||
PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
|
PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
|
||||||
|
|
||||||
|
// After wake-up from STOP reconfigure the PLL
|
||||||
|
SetSysClock();
|
||||||
|
|
||||||
// Re-enable us_ticker update interrupt
|
// Re-enable us_ticker update interrupt
|
||||||
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
|
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,25 +38,29 @@
|
||||||
|
|
||||||
static const PinMap PinMap_SPI_MOSI[] = {
|
static const PinMap PinMap_SPI_MOSI[] = {
|
||||||
{PA_7, SPI_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)},
|
{PA_7, SPI_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)},
|
||||||
{PB_5, SPI_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 1)}, // Remap
|
{PB_5, SPI_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 1)}, // GPIO_Remap_SPI1
|
||||||
|
{PB_15, SPI_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)},
|
||||||
{NC, NC, 0}
|
{NC, NC, 0}
|
||||||
};
|
};
|
||||||
|
|
||||||
static const PinMap PinMap_SPI_MISO[] = {
|
static const PinMap PinMap_SPI_MISO[] = {
|
||||||
{PA_6, SPI_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)},
|
{PA_6, SPI_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)},
|
||||||
{PB_4, SPI_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 1)}, // Remap
|
{PB_4, SPI_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 1)}, // GPIO_Remap_SPI1
|
||||||
|
{PB_14, SPI_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)},
|
||||||
{NC, NC, 0}
|
{NC, NC, 0}
|
||||||
};
|
};
|
||||||
|
|
||||||
static const PinMap PinMap_SPI_SCLK[] = {
|
static const PinMap PinMap_SPI_SCLK[] = {
|
||||||
{PA_5, SPI_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)},
|
{PA_5, SPI_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)},
|
||||||
{PB_3, SPI_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 1)}, // Remap
|
{PB_3, SPI_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 1)}, // GPIO_Remap_SPI1
|
||||||
|
{PB_13, SPI_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)},
|
||||||
{NC, NC, 0}
|
{NC, NC, 0}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Only used in Slave mode
|
|
||||||
static const PinMap PinMap_SPI_SSEL[] = {
|
static const PinMap PinMap_SPI_SSEL[] = {
|
||||||
{PB_6, SPI_1, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)}, // Generic IO, not real H/W NSS pin
|
{PA_4, SPI_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)},
|
||||||
|
{PA_15, SPI_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 1)}, // GPIO_Remap_SPI1
|
||||||
|
{PB_12, SPI_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)},
|
||||||
{NC, NC, 0}
|
{NC, NC, 0}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -100,6 +104,9 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
|
||||||
if (obj->spi == SPI_1) {
|
if (obj->spi == SPI_1) {
|
||||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
|
||||||
}
|
}
|
||||||
|
if (obj->spi == SPI_2) {
|
||||||
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
|
||||||
|
}
|
||||||
|
|
||||||
// Configure the SPI pins
|
// Configure the SPI pins
|
||||||
pinmap_pinout(mosi, PinMap_SPI_MOSI);
|
pinmap_pinout(mosi, PinMap_SPI_MOSI);
|
||||||
|
@ -110,7 +117,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
|
||||||
obj->bits = SPI_DataSize_8b;
|
obj->bits = SPI_DataSize_8b;
|
||||||
obj->cpol = SPI_CPOL_Low;
|
obj->cpol = SPI_CPOL_Low;
|
||||||
obj->cpha = SPI_CPHA_1Edge;
|
obj->cpha = SPI_CPHA_1Edge;
|
||||||
obj->br_presc = SPI_BaudRatePrescaler_256; // 1MHz
|
obj->br_presc = SPI_BaudRatePrescaler_256;
|
||||||
|
|
||||||
if (ssel == NC) { // Master
|
if (ssel == NC) { // Master
|
||||||
obj->mode = SPI_Mode_Master;
|
obj->mode = SPI_Mode_Master;
|
||||||
|
@ -171,22 +178,61 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void spi_frequency(spi_t *obj, int hz) {
|
void spi_frequency(spi_t *obj, int hz) {
|
||||||
// Choose the baud rate divisor (between 2 and 256)
|
if (obj->spi == SPI_1) {
|
||||||
uint32_t divisor = SystemCoreClock / hz;
|
// Values depend of PCLK2: 64 MHz if HSI is used, 72 MHz if HSE is used
|
||||||
|
if (hz < 500000) {
|
||||||
|
obj->br_presc = SPI_BaudRatePrescaler_256; // 250 kHz - 281 kHz
|
||||||
|
}
|
||||||
|
else if ((hz >= 500000) && (hz < 1000000)) {
|
||||||
|
obj->br_presc = SPI_BaudRatePrescaler_128; // 500 kHz - 563 kHz
|
||||||
|
}
|
||||||
|
else if ((hz >= 1000000) && (hz < 2000000)) {
|
||||||
|
obj->br_presc = SPI_BaudRatePrescaler_64; // 1 MHz - 1.13 MHz
|
||||||
|
}
|
||||||
|
else if ((hz >= 2000000) && (hz < 4000000)) {
|
||||||
|
obj->br_presc = SPI_BaudRatePrescaler_32; // 2 MHz - 2.25 MHz
|
||||||
|
}
|
||||||
|
else if ((hz >= 4000000) && (hz < 8000000)) {
|
||||||
|
obj->br_presc = SPI_BaudRatePrescaler_16; // 4 MHz - 4.5 MHz
|
||||||
|
}
|
||||||
|
else if ((hz >= 8000000) && (hz < 16000000)) {
|
||||||
|
obj->br_presc = SPI_BaudRatePrescaler_8; // 8 MHz - 9 MHz
|
||||||
|
}
|
||||||
|
else if ((hz >= 16000000) && (hz < 32000000)) {
|
||||||
|
obj->br_presc = SPI_BaudRatePrescaler_4; // 16 MHz - 18 MHz
|
||||||
|
}
|
||||||
|
else { // >= 32000000
|
||||||
|
obj->br_presc = SPI_BaudRatePrescaler_2; // 32 MHz - 36 MHz
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Find the nearest power-of-2
|
if (obj->spi == SPI_2) {
|
||||||
divisor = (divisor > 0 ? divisor-1 : 0);
|
// Values depend of PCLK1: 32 MHz if HSI is used, 36 MHz if HSE is used
|
||||||
divisor |= divisor >> 1;
|
if (hz < 250000) {
|
||||||
divisor |= divisor >> 2;
|
obj->br_presc = SPI_BaudRatePrescaler_256; // 125 kHz - 141 kHz
|
||||||
divisor |= divisor >> 4;
|
}
|
||||||
divisor |= divisor >> 8;
|
else if ((hz >= 250000) && (hz < 500000)) {
|
||||||
divisor |= divisor >> 16;
|
obj->br_presc = SPI_BaudRatePrescaler_128; // 250 kHz - 281 kHz
|
||||||
divisor++;
|
}
|
||||||
|
else if ((hz >= 500000) && (hz < 1000000)) {
|
||||||
uint32_t baud_rate = __builtin_ffs(divisor) - 2;
|
obj->br_presc = SPI_BaudRatePrescaler_64; // 500 kHz - 563 kHz
|
||||||
|
}
|
||||||
// Save new value
|
else if ((hz >= 1000000) && (hz < 2000000)) {
|
||||||
obj->br_presc = ((baud_rate > 7) ? (7 << 3) : (baud_rate << 3));
|
obj->br_presc = SPI_BaudRatePrescaler_32; // 1 MHz - 1.13 MHz
|
||||||
|
}
|
||||||
|
else if ((hz >= 2000000) && (hz < 4000000)) {
|
||||||
|
obj->br_presc = SPI_BaudRatePrescaler_16; // 2 MHz - 2.25 MHz
|
||||||
|
}
|
||||||
|
else if ((hz >= 4000000) && (hz < 8000000)) {
|
||||||
|
obj->br_presc = SPI_BaudRatePrescaler_8; // 4 MHz - 4.5 MHz
|
||||||
|
}
|
||||||
|
else if ((hz >= 8000000) && (hz < 16000000)) {
|
||||||
|
obj->br_presc = SPI_BaudRatePrescaler_4; // 8 MHz - 9 MHz
|
||||||
|
}
|
||||||
|
else { // >= 16000000
|
||||||
|
obj->br_presc = SPI_BaudRatePrescaler_2; // 16 MHz - 18 MHz
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
init_spi(obj);
|
init_spi(obj);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue