2009-11-29 08:50:13 +00:00
|
|
|
/*
|
2011-03-18 18:38:08 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
|
|
|
|
2011 Giovanni Di Sirio.
|
2009-11-29 08:50:13 +00:00
|
|
|
|
|
|
|
This file is part of ChibiOS/RT.
|
|
|
|
|
|
|
|
ChibiOS/RT is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
ChibiOS/RT is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2011-09-14 18:59:52 +00:00
|
|
|
* @file STM32F1xx/hal_lld.c
|
|
|
|
* @brief STM32F1xx HAL subsystem low level driver source.
|
2010-05-10 16:23:55 +00:00
|
|
|
*
|
2010-10-25 18:48:13 +00:00
|
|
|
* @addtogroup HAL
|
2009-11-29 08:50:13 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2009-11-29 13:37:19 +00:00
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
2009-11-29 08:50:13 +00:00
|
|
|
|
|
|
|
/*===========================================================================*/
|
2009-12-29 13:15:29 +00:00
|
|
|
/* Driver exported variables. */
|
2009-11-29 08:50:13 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
2009-12-29 13:15:29 +00:00
|
|
|
/* Driver local variables. */
|
2009-11-29 08:50:13 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
2009-12-29 13:15:29 +00:00
|
|
|
/* Driver local functions. */
|
2009-11-29 08:50:13 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
2009-12-29 13:15:29 +00:00
|
|
|
/* Driver interrupt handlers. */
|
2009-11-29 08:50:13 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
2009-12-29 13:15:29 +00:00
|
|
|
/* Driver exported functions. */
|
2009-11-29 08:50:13 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
2010-10-04 17:16:18 +00:00
|
|
|
* @brief Low level HAL driver initialization.
|
|
|
|
*
|
|
|
|
* @notapi
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
|
|
|
void hal_lld_init(void) {
|
|
|
|
|
2011-02-28 18:58:07 +00:00
|
|
|
/* Reset of all peripherals.*/
|
2011-09-16 17:38:22 +00:00
|
|
|
rccResetAPB1(0xFFFFFFFF);
|
|
|
|
rccResetAPB2(0xFFFFFFFF);
|
2011-02-28 18:44:46 +00:00
|
|
|
|
2010-03-28 10:27:46 +00:00
|
|
|
/* SysTick initialization using the system clock.*/
|
2010-05-09 17:52:30 +00:00
|
|
|
SysTick->LOAD = STM32_HCLK / CH_FREQUENCY - 1;
|
2009-11-29 21:07:43 +00:00
|
|
|
SysTick->VAL = 0;
|
2010-03-28 08:04:45 +00:00
|
|
|
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |
|
|
|
|
SysTick_CTRL_ENABLE_Msk |
|
|
|
|
SysTick_CTRL_TICKINT_Msk;
|
2009-11-29 21:07:43 +00:00
|
|
|
|
2012-01-04 08:46:11 +00:00
|
|
|
/* DWT cycle counter enable.*/
|
|
|
|
DWT_CTRL |= DWT_CTRL_CYCCNTENA;
|
|
|
|
|
2011-04-10 16:45:41 +00:00
|
|
|
#if defined(STM32_DMA_REQUIRED)
|
2009-11-29 08:50:13 +00:00
|
|
|
dmaInit();
|
|
|
|
#endif
|
2012-01-04 20:03:49 +00:00
|
|
|
|
2012-01-04 22:00:44 +00:00
|
|
|
/* Programmable voltage detector enable.*/
|
2012-01-04 20:03:49 +00:00
|
|
|
#if STM32_PVD_ENABLE
|
|
|
|
rccEnablePWRInterface(FALSE);
|
|
|
|
PWR->CR |= PWR_CR_PVDE | (STM32_PLS & STM32_PLS_MASK);
|
|
|
|
#endif /* STM32_PVD_ENABLE */
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-10-04 17:16:18 +00:00
|
|
|
* @brief STM32 clocks and PLL initialization.
|
|
|
|
* @note All the involved constants come from the file @p board.h.
|
2011-06-14 15:09:28 +00:00
|
|
|
* @note This function should be invoked just after the system reset.
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @special
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
2011-04-23 12:13:26 +00:00
|
|
|
#if defined(STM32F10X_LD) || defined(STM32F10X_LD_VL) || \
|
|
|
|
defined(STM32F10X_MD) || defined(STM32F10X_MD_VL) || \
|
|
|
|
defined(STM32F10X_HD) || defined(STM32F10X_XL) || \
|
2011-04-29 17:49:42 +00:00
|
|
|
defined(__DOXYGEN__)
|
2010-05-13 14:02:17 +00:00
|
|
|
/*
|
2012-01-08 11:38:57 +00:00
|
|
|
* Clocks initialization for all sub-families except CL.
|
2010-05-13 14:02:17 +00:00
|
|
|
*/
|
2009-11-29 08:50:13 +00:00
|
|
|
void stm32_clock_init(void) {
|
|
|
|
|
2012-01-08 11:38:57 +00:00
|
|
|
#if !STM32_NO_INIT
|
2010-05-13 14:02:17 +00:00
|
|
|
/* HSI setup, it enforces the reset situation in order to handle possible
|
|
|
|
problems with JTAG probes and re-initializations.*/
|
|
|
|
RCC->CR |= RCC_CR_HSION; /* Make sure HSI is ON. */
|
2009-11-29 08:50:13 +00:00
|
|
|
while (!(RCC->CR & RCC_CR_HSIRDY))
|
2010-05-13 14:02:17 +00:00
|
|
|
; /* Wait until HSI is stable. */
|
|
|
|
RCC->CR &= RCC_CR_HSITRIM | RCC_CR_HSION; /* CR Reset value. */
|
|
|
|
RCC->CFGR = 0; /* CFGR reset value. */
|
|
|
|
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
|
2012-01-08 11:38:57 +00:00
|
|
|
; /* Waits until HSI is selected. */
|
2010-05-13 14:02:17 +00:00
|
|
|
|
2012-01-08 11:38:57 +00:00
|
|
|
#if STM32_HSE_ENABLED
|
|
|
|
/* HSE activation.*/
|
2009-11-29 08:50:13 +00:00
|
|
|
RCC->CR |= RCC_CR_HSEON;
|
|
|
|
while (!(RCC->CR & RCC_CR_HSERDY))
|
2012-01-08 11:38:57 +00:00
|
|
|
; /* Waits until HSE is stable. */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if STM32_LSI_ENABLED
|
|
|
|
/* LSI activation.*/
|
|
|
|
RCC->CSR |= RCC_CSR_LSION;
|
|
|
|
while ((RCC->CSR & RCC_CSR_LSIRDY) == 0)
|
|
|
|
; /* Waits until LSI is stable. */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if STM32_LSE_ENABLED
|
|
|
|
/* LSE activation, have to unlock the register.*/
|
|
|
|
PWR->CR |= PWR_CR_DBP;
|
|
|
|
RCC->BDCR |= RCC_BDCR_LSEON;
|
|
|
|
while ((RCC->BDCR & RCC_BDCR_LSERDY) == 0)
|
|
|
|
; /* Waits until LSE is stable. */
|
|
|
|
PWR->CR &= ~PWR_CR_DBP;
|
2010-05-13 14:02:17 +00:00
|
|
|
#endif
|
|
|
|
|
2012-01-08 11:38:57 +00:00
|
|
|
#if STM32_ACTIVATE_PLL
|
|
|
|
/* PLL activation.*/
|
2010-05-13 14:02:17 +00:00
|
|
|
RCC->CFGR |= STM32_PLLMUL | STM32_PLLXTPRE | STM32_PLLSRC;
|
|
|
|
RCC->CR |= RCC_CR_PLLON;
|
2009-11-29 08:50:13 +00:00
|
|
|
while (!(RCC->CR & RCC_CR_PLLRDY))
|
2012-01-08 11:38:57 +00:00
|
|
|
; /* Waits until PLL is stable. */
|
2010-05-09 17:52:30 +00:00
|
|
|
#endif
|
2010-05-13 14:02:17 +00:00
|
|
|
|
2010-05-09 17:52:30 +00:00
|
|
|
/* Clock settings.*/
|
2012-01-08 11:38:57 +00:00
|
|
|
RCC->CFGR = STM32_MCOSEL | STM32_USBPRE | STM32_PLLMUL | STM32_PLLXTPRE |
|
2011-01-15 08:02:07 +00:00
|
|
|
STM32_PLLSRC | STM32_ADCPRE | STM32_PPRE2 | STM32_PPRE1 |
|
2011-01-06 10:33:57 +00:00
|
|
|
STM32_HPRE;
|
2009-11-29 08:50:13 +00:00
|
|
|
|
|
|
|
/* Flash setup and final clock selection. */
|
2012-01-08 11:38:57 +00:00
|
|
|
FLASH->ACR = STM32_FLASHBITS;
|
2010-05-13 14:02:17 +00:00
|
|
|
|
2011-06-14 15:09:28 +00:00
|
|
|
/* Switching to the configured clock source if it is different from HSI.*/
|
2010-05-13 14:02:17 +00:00
|
|
|
#if (STM32_SW != STM32_SW_HSI)
|
2012-01-08 11:38:57 +00:00
|
|
|
/* Switches clock source.*/
|
|
|
|
RCC->CFGR |= STM32_SW;
|
2010-05-09 17:52:30 +00:00
|
|
|
while ((RCC->CFGR & RCC_CFGR_SWS) != (STM32_SW << 2))
|
2012-01-08 11:38:57 +00:00
|
|
|
; /* Waits selection complete. */
|
2010-05-13 14:02:17 +00:00
|
|
|
#endif
|
2012-01-08 11:38:57 +00:00
|
|
|
#endif /* STM32_NO_INIT */
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
2010-11-15 19:44:09 +00:00
|
|
|
|
2010-05-13 14:02:17 +00:00
|
|
|
#elif defined(STM32F10X_CL)
|
|
|
|
/*
|
|
|
|
* Clocks initialization for the CL sub-family.
|
|
|
|
*/
|
|
|
|
void stm32_clock_init(void) {
|
|
|
|
|
2011-03-12 11:19:24 +00:00
|
|
|
/* HSI setup.*/
|
2010-05-13 14:02:17 +00:00
|
|
|
RCC->CR |= RCC_CR_HSION; /* Make sure HSI is ON. */
|
|
|
|
while (!(RCC->CR & RCC_CR_HSIRDY))
|
|
|
|
; /* Wait until HSI is stable. */
|
2011-03-12 11:19:24 +00:00
|
|
|
RCC->CFGR = 0;
|
2010-05-13 14:02:17 +00:00
|
|
|
RCC->CR &= RCC_CR_HSITRIM | RCC_CR_HSION; /* CR Reset value. */
|
|
|
|
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
|
|
|
|
; /* Wait until HSI is the source.*/
|
|
|
|
|
2011-03-12 11:19:24 +00:00
|
|
|
/* HSE setup, it is only performed if the current configuration uses
|
|
|
|
it somehow.*/
|
|
|
|
#if STM32_ACTIVATE_PLL2 || \
|
|
|
|
STM32_ACTIVATE_PLL3 || \
|
|
|
|
(STM32_SW == STM32_SW_HSE) || \
|
|
|
|
((STM32_PREDIV1SRC == STM32_PREDIV1SRC_HSE) && \
|
|
|
|
(STM32_PLLSRC == STM32_PLLSRC_PREDIV1))
|
2010-05-13 14:02:17 +00:00
|
|
|
RCC->CR |= RCC_CR_HSEON;
|
|
|
|
while (!(RCC->CR & RCC_CR_HSERDY))
|
|
|
|
; /* Waits until HSE is stable. */
|
|
|
|
#endif
|
|
|
|
|
2011-03-12 11:19:24 +00:00
|
|
|
/* Settings of various dividers and multipliers in CFGR2.*/
|
|
|
|
RCC->CFGR2 = STM32_PLL3MUL | STM32_PLL2MUL | STM32_PREDIV2 |
|
|
|
|
STM32_PREDIV1 | STM32_PREDIV1SRC;
|
|
|
|
|
|
|
|
/* PLL2 setup, if activated.*/
|
|
|
|
#if STM32_ACTIVATE_PLL2
|
|
|
|
RCC->CR |= RCC_CR_PLL2ON;
|
2010-05-14 18:10:27 +00:00
|
|
|
while (!(RCC->CR & RCC_CR_PLL2RDY))
|
2011-03-12 11:19:24 +00:00
|
|
|
; /* Waits until PLL2 is stable. */
|
2010-05-13 14:02:17 +00:00
|
|
|
#endif
|
|
|
|
|
2011-03-12 11:19:24 +00:00
|
|
|
/* PLL3 setup, if activated.*/
|
|
|
|
#if STM32_ACTIVATE_PLL3
|
|
|
|
RCC->CR |= RCC_CR_PLL3ON;
|
|
|
|
while (!(RCC->CR & RCC_CR_PLL3RDY))
|
|
|
|
; /* Waits until PLL3 is stable. */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* PLL1 setup, if activated.*/
|
|
|
|
#if STM32_ACTIVATE_PLL1
|
|
|
|
RCC->CFGR |= STM32_PLLMUL | STM32_PLLSRC;
|
|
|
|
RCC->CR |= RCC_CR_PLLON;
|
2010-05-14 18:10:27 +00:00
|
|
|
while (!(RCC->CR & RCC_CR_PLLRDY))
|
2011-03-12 11:19:24 +00:00
|
|
|
; /* Waits until PLL1 is stable. */
|
2010-05-13 14:02:17 +00:00
|
|
|
#endif
|
|
|
|
|
2010-05-14 06:43:02 +00:00
|
|
|
/* Clock settings.*/
|
2011-01-15 08:01:07 +00:00
|
|
|
#if STM32_HAS_OTG1
|
|
|
|
RCC->CFGR = STM32_MCO | STM32_OTGFSPRE | STM32_PLLMUL | STM32_PLLSRC |
|
|
|
|
STM32_ADCPRE | STM32_PPRE2 | STM32_PPRE1 | STM32_HPRE;
|
|
|
|
#else
|
|
|
|
RCC->CFGR = STM32_MCO | STM32_PLLMUL | STM32_PLLSRC |
|
|
|
|
STM32_ADCPRE | STM32_PPRE2 | STM32_PPRE1 | STM32_HPRE;
|
|
|
|
#endif
|
2010-05-14 06:43:02 +00:00
|
|
|
|
|
|
|
/* Flash setup and final clock selection. */
|
|
|
|
FLASH->ACR = STM32_FLASHBITS; /* Flash wait states depending on clock. */
|
|
|
|
|
2011-06-14 15:09:28 +00:00
|
|
|
/* Switching to the configured clock source if it is different from HSI.*/
|
2010-05-13 14:02:17 +00:00
|
|
|
#if (STM32_SW != STM32_SW_HSI)
|
|
|
|
RCC->CFGR |= STM32_SW; /* Switches on the selected clock source. */
|
|
|
|
while ((RCC->CFGR & RCC_CFGR_SWS) != (STM32_SW << 2))
|
|
|
|
;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void stm32_clock_init(void) {}
|
|
|
|
#endif
|
2009-11-29 08:50:13 +00:00
|
|
|
|
|
|
|
/** @} */
|