Guest User

Untitled

a guest
Mar 20th, 2014
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.15 KB | None | 0 0
  1. /*
  2.     ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
  3.  
  4.     Licensed under the Apache License, Version 2.0 (the "License");
  5.     you may not use this file except in compliance with the License.
  6.     You may obtain a copy of the License at
  7.  
  8.         http://www.apache.org/licenses/LICENSE-2.0
  9.  
  10.     Unless required by applicable law or agreed to in writing, software
  11.     distributed under the License is distributed on an "AS IS" BASIS,
  12.     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.     See the License for the specific language governing permissions and
  14.     limitations under the License.
  15. */
  16.  
  17. /**
  18.  * @file    STM32F30x/hal_lld.c
  19.  * @brief   STM32F30x HAL subsystem low level driver source.
  20.  *
  21.  * @addtogroup HAL
  22.  * @{
  23.  */
  24.  
  25. #include "ch.h"
  26. #include "hal.h"
  27.  
  28. /*===========================================================================*/
  29. /* Driver local definitions.                                                 */
  30. /*===========================================================================*/
  31.  
  32. /*===========================================================================*/
  33. /* Driver exported variables.                                                */
  34. /*===========================================================================*/
  35.  
  36. /*===========================================================================*/
  37. /* Driver local variables and types.                                         */
  38. /*===========================================================================*/
  39.  
  40. /*===========================================================================*/
  41. /* Driver local functions.                                                   */
  42. /*===========================================================================*/
  43.  
  44. /**
  45.  * @brief   Initializes the backup domain.
  46.  * @note    WARNING! Changing clock source impossible without resetting
  47.  *          of the whole BKP domain.
  48.  */
  49. static void hal_lld_backup_domain_init(void) {
  50.  
  51.   /* Backup domain access enabled and left open.*/
  52.   PWR->CR |= PWR_CR_DBP;
  53.  
  54.   /* Reset BKP domain if different clock source selected.*/
  55.   if ((RCC->BDCR & STM32_RTCSEL_MASK) != STM32_RTCSEL){
  56.     /* Backup domain reset.*/
  57.     RCC->BDCR = RCC_BDCR_BDRST;
  58.     RCC->BDCR = 0;
  59.   }
  60.  
  61.   /* If enabled then the LSE is started.*/
  62. #if STM32_LSE_ENABLED
  63. #if defined(STM32_LSE_BYPASS)
  64.   /* LSE Bypass.*/
  65.   RCC->BDCR |= STM32_LSEDRV | RCC_BDCR_LSEON | RCC_BDCR_LSEBYP;
  66. #else
  67.   /* No LSE Bypass.*/
  68.   RCC->BDCR |= STM32_LSEDRV | RCC_BDCR_LSEON;
  69. #endif
  70.   while ((RCC->BDCR & RCC_BDCR_LSERDY) == 0)
  71.     ;                                     /* Waits until LSE is stable.   */
  72. #endif
  73.  
  74. #if STM32_RTCSEL != STM32_RTCSEL_NOCLOCK
  75.   /* If the backup domain hasn't been initialized yet then proceed with
  76.      initialization.*/
  77.   if ((RCC->BDCR & RCC_BDCR_RTCEN) == 0) {
  78.     /* Selects clock source.*/
  79.     RCC->BDCR |= STM32_RTCSEL;
  80.  
  81.     /* RTC clock enabled.*/
  82.     RCC->BDCR |= RCC_BDCR_RTCEN;
  83.   }
  84. #endif /* STM32_RTCSEL != STM32_RTCSEL_NOCLOCK */
  85. }
  86.  
  87. /*===========================================================================*/
  88. /* Driver interrupt handlers.                                                */
  89. /*===========================================================================*/
  90.  
  91. /*===========================================================================*/
  92. /* Driver exported functions.                                                */
  93. /*===========================================================================*/
  94.  
  95. /**
  96.  * @brief   Low level HAL driver initialization.
  97.  *
  98.  * @notapi
  99.  */
  100. void hal_lld_init(void) {
  101.  
  102.   /* Reset of all peripherals.*/
  103.   rccResetAPB1(0xFFFFFFFF);
  104.   rccResetAPB2(0xFFFFFFFF);
  105.  
  106.   /* SysTick initialization using the system clock.*/
  107.   SysTick->LOAD = STM32_HCLK / CH_FREQUENCY - 1;
  108.   SysTick->VAL = 0;
  109.   SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |
  110.                   SysTick_CTRL_ENABLE_Msk |
  111.                   SysTick_CTRL_TICKINT_Msk;
  112.  
  113.   /* DWT cycle counter enable.*/
  114.   SCS_DEMCR |= SCS_DEMCR_TRCENA;
  115.   DWT_CTRL  |= DWT_CTRL_CYCCNTENA;
  116.  
  117.   /* PWR clock enabled.*/
  118.   rccEnablePWRInterface(FALSE);
  119.  
  120.   /* Initializes the backup domain.*/
  121.   hal_lld_backup_domain_init();
  122.  
  123. #if defined(STM32_DMA_REQUIRED)
  124.   dmaInit();
  125. #endif
  126.  
  127.   /* Programmable voltage detector enable.*/
  128. #if STM32_PVD_ENABLE
  129.   PWR->CR |= PWR_CR_PVDE | (STM32_PLS & STM32_PLS_MASK);
  130. #endif /* STM32_PVD_ENABLE */
  131.  
  132.   /* SYSCFG clock enabled here because it is a multi-functional unit shared
  133.      among multiple drivers.*/
  134.   rccEnableAPB2(RCC_APB2ENR_SYSCFGEN, TRUE);
  135.  
  136.   /* USB IRQ relocated to not conflict with CAN.*/
  137.   //SYSCFG->CFGR1 |= SYSCFG_CFGR1_USB_IT_RMP;
  138. }
  139.  
  140. /**
  141.  * @brief   STM32 clocks and PLL initialization.
  142.  * @note    All the involved constants come from the file @p board.h.
  143.  * @note    This function should be invoked just after the system reset.
  144.  *
  145.  * @special
  146.  */
  147. void stm32_clock_init(void) {
  148.  
  149. #if !STM32_NO_INIT
  150.   /* HSI setup, it enforces the reset situation in order to handle possible
  151.      problems with JTAG probes and re-initializations.*/
  152.   RCC->CR |= RCC_CR_HSION;                  /* Make sure HSI is ON.         */
  153.   while (!(RCC->CR & RCC_CR_HSIRDY))
  154.     ;                                       /* Wait until HSI is stable.    */
  155.   RCC->CR &= RCC_CR_HSITRIM | RCC_CR_HSION; /* CR Reset value.              */
  156.   RCC->CFGR = 0;                            /* CFGR reset value.            */
  157.   while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  158.     ;                                       /* Waits until HSI is selected. */
  159.  
  160. #if STM32_HSE_ENABLED
  161.   /* HSE activation.*/
  162. #if defined(STM32_HSE_BYPASS)
  163.   /* HSE Bypass.*/
  164.   RCC->CR |= RCC_CR_HSEON | RCC_CR_HSEBYP;
  165. #else
  166.   /* No HSE Bypass.*/
  167.   RCC->CR |= RCC_CR_HSEON;
  168. #endif
  169.   while (!(RCC->CR & RCC_CR_HSERDY))
  170.     ;                                       /* Waits until HSE is stable.   */
  171. #endif
  172.  
  173. #if STM32_LSI_ENABLED
  174.   /* LSI activation.*/
  175.   RCC->CSR |= RCC_CSR_LSION;
  176.   while ((RCC->CSR & RCC_CSR_LSIRDY) == 0)
  177.     ;                                       /* Waits until LSI is stable.   */
  178. #endif
  179.  
  180.   /* Clock settings.*/
  181.   RCC->CFGR  = STM32_MCOSEL    | STM32_USBPRE    | STM32_PLLMUL   |
  182.                STM32_PLLSRC    | STM32_PPRE1     | STM32_PPRE2    |
  183.                STM32_HPRE;
  184.   RCC->CFGR2 = STM32_ADC34PRES | STM32_ADC12PRES | STM32_PREDIV;
  185.   RCC->CFGR3 = STM32_UART5SW   | STM32_UART4SW   | STM32_USART3SW |
  186.                STM32_USART2SW  | STM32_TIM8SW    | STM32_TIM1SW   |
  187.                STM32_I2C2SW    | STM32_I2C1SW    | STM32_USART1SW;
  188.  
  189. #if STM32_ACTIVATE_PLL
  190.   /* PLL activation.*/
  191.   RCC->CR   |= RCC_CR_PLLON;
  192.   while (!(RCC->CR & RCC_CR_PLLRDY))
  193.     ;                                       /* Waits until PLL is stable.   */
  194. #endif
  195.  
  196.   /* Flash setup and final clock selection.   */
  197.   FLASH->ACR = STM32_FLASHBITS;
  198.  
  199.   /* Switching to the configured clock source if it is different from HSI.*/
  200. #if (STM32_SW != STM32_SW_HSI)
  201.   /* Switches clock source.*/
  202.   RCC->CFGR |= STM32_SW;
  203.   while ((RCC->CFGR & RCC_CFGR_SWS) != (STM32_SW << 2))
  204.     ;                                       /* Waits selection complete.    */
  205. #endif
  206. #endif /* !STM32_NO_INIT */
  207. }
  208.  
  209. /** @} */
Advertisement
Add Comment
Please, Sign In to add comment