anyThing Connected Team / mbed-dev

Dependents:   BREAK_SENSOR_LED

Fork of mbed-dev by mbed official

Committer:
Anythingconnected
Date:
Mon Dec 18 10:14:27 2017 +0000
Revision:
180:d79f997829d6
Parent:
170:19eb464bc2be
Getting byte by byte read to work

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 170:19eb464bc2be 1 /**
Kojto 170:19eb464bc2be 2 ******************************************************************************
Kojto 170:19eb464bc2be 3 * @file system_stm32f7xx.c
Kojto 170:19eb464bc2be 4 * @author MCD Application Team
Kojto 170:19eb464bc2be 5 * @version V1.2.0
Kojto 170:19eb464bc2be 6 * @date 30-December-2016
Kojto 170:19eb464bc2be 7 * @brief CMSIS Cortex-M7 Device Peripheral Access Layer System Source File.
Kojto 170:19eb464bc2be 8 *
Kojto 170:19eb464bc2be 9 * This file provides two functions and one global variable to be called from
Kojto 170:19eb464bc2be 10 * user application:
Kojto 170:19eb464bc2be 11 * - SystemInit(): This function is called at startup just after reset and
Kojto 170:19eb464bc2be 12 * before branch to main program. This call is made inside
Kojto 170:19eb464bc2be 13 * the "startup_stm32f7xx.s" file.
Kojto 170:19eb464bc2be 14 *
Kojto 170:19eb464bc2be 15 * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
Kojto 170:19eb464bc2be 16 * by the user application to setup the SysTick
Kojto 170:19eb464bc2be 17 * timer or configure other parameters.
Kojto 170:19eb464bc2be 18 *
Kojto 170:19eb464bc2be 19 * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
Kojto 170:19eb464bc2be 20 * be called whenever the core clock is changed
Kojto 170:19eb464bc2be 21 * during program execution.
Kojto 170:19eb464bc2be 22 *
Kojto 170:19eb464bc2be 23 *
Kojto 170:19eb464bc2be 24 ******************************************************************************
Kojto 170:19eb464bc2be 25 * @attention
Kojto 170:19eb464bc2be 26 *
Kojto 170:19eb464bc2be 27 * <h2><center>&copy; COPYRIGHT 2016 STMicroelectronics</center></h2>
Kojto 170:19eb464bc2be 28 *
Kojto 170:19eb464bc2be 29 * Redistribution and use in source and binary forms, with or without modification,
Kojto 170:19eb464bc2be 30 * are permitted provided that the following conditions are met:
Kojto 170:19eb464bc2be 31 * 1. Redistributions of source code must retain the above copyright notice,
Kojto 170:19eb464bc2be 32 * this list of conditions and the following disclaimer.
Kojto 170:19eb464bc2be 33 * 2. Redistributions in binary form must reproduce the above copyright notice,
Kojto 170:19eb464bc2be 34 * this list of conditions and the following disclaimer in the documentation
Kojto 170:19eb464bc2be 35 * and/or other materials provided with the distribution.
Kojto 170:19eb464bc2be 36 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Kojto 170:19eb464bc2be 37 * may be used to endorse or promote products derived from this software
Kojto 170:19eb464bc2be 38 * without specific prior written permission.
Kojto 170:19eb464bc2be 39 *
Kojto 170:19eb464bc2be 40 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Kojto 170:19eb464bc2be 41 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Kojto 170:19eb464bc2be 42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Kojto 170:19eb464bc2be 43 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Kojto 170:19eb464bc2be 44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Kojto 170:19eb464bc2be 45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Kojto 170:19eb464bc2be 46 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Kojto 170:19eb464bc2be 47 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Kojto 170:19eb464bc2be 48 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Kojto 170:19eb464bc2be 49 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Kojto 170:19eb464bc2be 50 *
Kojto 170:19eb464bc2be 51 ******************************************************************************
Kojto 170:19eb464bc2be 52 */
Kojto 170:19eb464bc2be 53
Kojto 170:19eb464bc2be 54 /** @addtogroup CMSIS
Kojto 170:19eb464bc2be 55 * @{
Kojto 170:19eb464bc2be 56 */
Kojto 170:19eb464bc2be 57
Kojto 170:19eb464bc2be 58 /** @addtogroup stm32f7xx_system
Kojto 170:19eb464bc2be 59 * @{
Kojto 170:19eb464bc2be 60 */
Kojto 170:19eb464bc2be 61
Kojto 170:19eb464bc2be 62 /** @addtogroup STM32F7xx_System_Private_Includes
Kojto 170:19eb464bc2be 63 * @{
Kojto 170:19eb464bc2be 64 */
Kojto 170:19eb464bc2be 65
Kojto 170:19eb464bc2be 66 #include "stm32f7xx.h"
Kojto 170:19eb464bc2be 67
Kojto 170:19eb464bc2be 68 #if !defined (HSE_VALUE)
Kojto 170:19eb464bc2be 69 #define HSE_VALUE ((uint32_t)25000000) /*!< Default value of the External oscillator in Hz */
Kojto 170:19eb464bc2be 70 #endif /* HSE_VALUE */
Kojto 170:19eb464bc2be 71
Kojto 170:19eb464bc2be 72 #if !defined (HSI_VALUE)
Kojto 170:19eb464bc2be 73 #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/
Kojto 170:19eb464bc2be 74 #endif /* HSI_VALUE */
Kojto 170:19eb464bc2be 75
Kojto 170:19eb464bc2be 76 /**
Kojto 170:19eb464bc2be 77 * @}
Kojto 170:19eb464bc2be 78 */
Kojto 170:19eb464bc2be 79
Kojto 170:19eb464bc2be 80 /** @addtogroup STM32F7xx_System_Private_TypesDefinitions
Kojto 170:19eb464bc2be 81 * @{
Kojto 170:19eb464bc2be 82 */
Kojto 170:19eb464bc2be 83
Kojto 170:19eb464bc2be 84 /**
Kojto 170:19eb464bc2be 85 * @}
Kojto 170:19eb464bc2be 86 */
Kojto 170:19eb464bc2be 87
Kojto 170:19eb464bc2be 88 /** @addtogroup STM32F7xx_System_Private_Defines
Kojto 170:19eb464bc2be 89 * @{
Kojto 170:19eb464bc2be 90 */
Kojto 170:19eb464bc2be 91
Kojto 170:19eb464bc2be 92 /************************* Miscellaneous Configuration ************************/
Kojto 170:19eb464bc2be 93
Kojto 170:19eb464bc2be 94 /*!< Uncomment the following line if you need to relocate your vector Table in
Kojto 170:19eb464bc2be 95 Internal SRAM. */
Kojto 170:19eb464bc2be 96 /* #define VECT_TAB_SRAM */
Kojto 170:19eb464bc2be 97 #define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field.
Kojto 170:19eb464bc2be 98 This value must be a multiple of 0x200. */
Kojto 170:19eb464bc2be 99 /******************************************************************************/
Kojto 170:19eb464bc2be 100
Kojto 170:19eb464bc2be 101 /**
Kojto 170:19eb464bc2be 102 * @}
Kojto 170:19eb464bc2be 103 */
Kojto 170:19eb464bc2be 104
Kojto 170:19eb464bc2be 105 /** @addtogroup STM32F7xx_System_Private_Macros
Kojto 170:19eb464bc2be 106 * @{
Kojto 170:19eb464bc2be 107 */
Kojto 170:19eb464bc2be 108
Kojto 170:19eb464bc2be 109 /**
Kojto 170:19eb464bc2be 110 * @}
Kojto 170:19eb464bc2be 111 */
Kojto 170:19eb464bc2be 112
Kojto 170:19eb464bc2be 113 /** @addtogroup STM32F7xx_System_Private_Variables
Kojto 170:19eb464bc2be 114 * @{
Kojto 170:19eb464bc2be 115 */
Kojto 170:19eb464bc2be 116
Kojto 170:19eb464bc2be 117 /* This variable is updated in three ways:
Kojto 170:19eb464bc2be 118 1) by calling CMSIS function SystemCoreClockUpdate()
Kojto 170:19eb464bc2be 119 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
Kojto 170:19eb464bc2be 120 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
Kojto 170:19eb464bc2be 121 Note: If you use this function to configure the system clock; then there
Kojto 170:19eb464bc2be 122 is no need to call the 2 first functions listed above, since SystemCoreClock
Kojto 170:19eb464bc2be 123 variable is updated automatically.
Kojto 170:19eb464bc2be 124 */
Kojto 170:19eb464bc2be 125 uint32_t SystemCoreClock = 16000000;
Kojto 170:19eb464bc2be 126 const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
Kojto 170:19eb464bc2be 127 const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
Kojto 170:19eb464bc2be 128
Kojto 170:19eb464bc2be 129 /**
Kojto 170:19eb464bc2be 130 * @}
Kojto 170:19eb464bc2be 131 */
Kojto 170:19eb464bc2be 132
Kojto 170:19eb464bc2be 133 /** @addtogroup STM32F7xx_System_Private_FunctionPrototypes
Kojto 170:19eb464bc2be 134 * @{
Kojto 170:19eb464bc2be 135 */
Kojto 170:19eb464bc2be 136
Kojto 170:19eb464bc2be 137 /**
Kojto 170:19eb464bc2be 138 * @}
Kojto 170:19eb464bc2be 139 */
Kojto 170:19eb464bc2be 140
Kojto 170:19eb464bc2be 141 /** @addtogroup STM32F7xx_System_Private_Functions
Kojto 170:19eb464bc2be 142 * @{
Kojto 170:19eb464bc2be 143 */
Kojto 170:19eb464bc2be 144
Kojto 170:19eb464bc2be 145 /*+ MBED */
Kojto 170:19eb464bc2be 146 #if 0
Kojto 170:19eb464bc2be 147 /*- MBED */
Kojto 170:19eb464bc2be 148
Kojto 170:19eb464bc2be 149 /**
Kojto 170:19eb464bc2be 150 * @brief Setup the microcontroller system
Kojto 170:19eb464bc2be 151 * Initialize the Embedded Flash Interface, the PLL and update the
Kojto 170:19eb464bc2be 152 * SystemFrequency variable.
Kojto 170:19eb464bc2be 153 * @param None
Kojto 170:19eb464bc2be 154 * @retval None
Kojto 170:19eb464bc2be 155 */
Kojto 170:19eb464bc2be 156 void SystemInit(void)
Kojto 170:19eb464bc2be 157 {
Kojto 170:19eb464bc2be 158 /* FPU settings ------------------------------------------------------------*/
Kojto 170:19eb464bc2be 159 #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
Kojto 170:19eb464bc2be 160 SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
Kojto 170:19eb464bc2be 161 #endif
Kojto 170:19eb464bc2be 162 /* Reset the RCC clock configuration to the default reset state ------------*/
Kojto 170:19eb464bc2be 163 /* Set HSION bit */
Kojto 170:19eb464bc2be 164 RCC->CR |= (uint32_t)0x00000001;
Kojto 170:19eb464bc2be 165
Kojto 170:19eb464bc2be 166 /* Reset CFGR register */
Kojto 170:19eb464bc2be 167 RCC->CFGR = 0x00000000;
Kojto 170:19eb464bc2be 168
Kojto 170:19eb464bc2be 169 /* Reset HSEON, CSSON and PLLON bits */
Kojto 170:19eb464bc2be 170 RCC->CR &= (uint32_t)0xFEF6FFFF;
Kojto 170:19eb464bc2be 171
Kojto 170:19eb464bc2be 172 /* Reset PLLCFGR register */
Kojto 170:19eb464bc2be 173 RCC->PLLCFGR = 0x24003010;
Kojto 170:19eb464bc2be 174
Kojto 170:19eb464bc2be 175 /* Reset HSEBYP bit */
Kojto 170:19eb464bc2be 176 RCC->CR &= (uint32_t)0xFFFBFFFF;
Kojto 170:19eb464bc2be 177
Kojto 170:19eb464bc2be 178 /* Disable all interrupts */
Kojto 170:19eb464bc2be 179 RCC->CIR = 0x00000000;
Kojto 170:19eb464bc2be 180
Kojto 170:19eb464bc2be 181 /* Configure the Vector Table location add offset address ------------------*/
Kojto 170:19eb464bc2be 182 #ifdef VECT_TAB_SRAM
Kojto 170:19eb464bc2be 183 SCB->VTOR = RAMDTCM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
Kojto 170:19eb464bc2be 184 #else
Kojto 170:19eb464bc2be 185 SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
Kojto 170:19eb464bc2be 186 #endif
Kojto 170:19eb464bc2be 187 }
Kojto 170:19eb464bc2be 188
Kojto 170:19eb464bc2be 189 /*+ MBED */
Kojto 170:19eb464bc2be 190 #endif
Kojto 170:19eb464bc2be 191 /*- MBED */
Kojto 170:19eb464bc2be 192
Kojto 170:19eb464bc2be 193 /**
Kojto 170:19eb464bc2be 194 * @brief Update SystemCoreClock variable according to Clock Register Values.
Kojto 170:19eb464bc2be 195 * The SystemCoreClock variable contains the core clock (HCLK), it can
Kojto 170:19eb464bc2be 196 * be used by the user application to setup the SysTick timer or configure
Kojto 170:19eb464bc2be 197 * other parameters.
Kojto 170:19eb464bc2be 198 *
Kojto 170:19eb464bc2be 199 * @note Each time the core clock (HCLK) changes, this function must be called
Kojto 170:19eb464bc2be 200 * to update SystemCoreClock variable value. Otherwise, any configuration
Kojto 170:19eb464bc2be 201 * based on this variable will be incorrect.
Kojto 170:19eb464bc2be 202 *
Kojto 170:19eb464bc2be 203 * @note - The system frequency computed by this function is not the real
Kojto 170:19eb464bc2be 204 * frequency in the chip. It is calculated based on the predefined
Kojto 170:19eb464bc2be 205 * constant and the selected clock source:
Kojto 170:19eb464bc2be 206 *
Kojto 170:19eb464bc2be 207 * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
Kojto 170:19eb464bc2be 208 *
Kojto 170:19eb464bc2be 209 * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
Kojto 170:19eb464bc2be 210 *
Kojto 170:19eb464bc2be 211 * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
Kojto 170:19eb464bc2be 212 * or HSI_VALUE(*) multiplied/divided by the PLL factors.
Kojto 170:19eb464bc2be 213 *
Kojto 170:19eb464bc2be 214 * (*) HSI_VALUE is a constant defined in stm32f7xx_hal_conf.h file (default value
Kojto 170:19eb464bc2be 215 * 16 MHz) but the real value may vary depending on the variations
Kojto 170:19eb464bc2be 216 * in voltage and temperature.
Kojto 170:19eb464bc2be 217 *
Kojto 170:19eb464bc2be 218 * (**) HSE_VALUE is a constant defined in stm32f7xx_hal_conf.h file (default value
Kojto 170:19eb464bc2be 219 * 25 MHz), user has to ensure that HSE_VALUE is same as the real
Kojto 170:19eb464bc2be 220 * frequency of the crystal used. Otherwise, this function may
Kojto 170:19eb464bc2be 221 * have wrong result.
Kojto 170:19eb464bc2be 222 *
Kojto 170:19eb464bc2be 223 * - The result of this function could be not correct when using fractional
Kojto 170:19eb464bc2be 224 * value for HSE crystal.
Kojto 170:19eb464bc2be 225 *
Kojto 170:19eb464bc2be 226 * @param None
Kojto 170:19eb464bc2be 227 * @retval None
Kojto 170:19eb464bc2be 228 */
Kojto 170:19eb464bc2be 229 void SystemCoreClockUpdate(void)
Kojto 170:19eb464bc2be 230 {
Kojto 170:19eb464bc2be 231 uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
Kojto 170:19eb464bc2be 232
Kojto 170:19eb464bc2be 233 /* Get SYSCLK source -------------------------------------------------------*/
Kojto 170:19eb464bc2be 234 tmp = RCC->CFGR & RCC_CFGR_SWS;
Kojto 170:19eb464bc2be 235
Kojto 170:19eb464bc2be 236 switch (tmp)
Kojto 170:19eb464bc2be 237 {
Kojto 170:19eb464bc2be 238 case 0x00: /* HSI used as system clock source */
Kojto 170:19eb464bc2be 239 SystemCoreClock = HSI_VALUE;
Kojto 170:19eb464bc2be 240 break;
Kojto 170:19eb464bc2be 241 case 0x04: /* HSE used as system clock source */
Kojto 170:19eb464bc2be 242 SystemCoreClock = HSE_VALUE;
Kojto 170:19eb464bc2be 243 break;
Kojto 170:19eb464bc2be 244 case 0x08: /* PLL used as system clock source */
Kojto 170:19eb464bc2be 245
Kojto 170:19eb464bc2be 246 /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
Kojto 170:19eb464bc2be 247 SYSCLK = PLL_VCO / PLL_P
Kojto 170:19eb464bc2be 248 */
Kojto 170:19eb464bc2be 249 pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
Kojto 170:19eb464bc2be 250 pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
Kojto 170:19eb464bc2be 251
Kojto 170:19eb464bc2be 252 if (pllsource != 0)
Kojto 170:19eb464bc2be 253 {
Kojto 170:19eb464bc2be 254 /* HSE used as PLL clock source */
Kojto 170:19eb464bc2be 255 pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
Kojto 170:19eb464bc2be 256 }
Kojto 170:19eb464bc2be 257 else
Kojto 170:19eb464bc2be 258 {
Kojto 170:19eb464bc2be 259 /* HSI used as PLL clock source */
Kojto 170:19eb464bc2be 260 pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
Kojto 170:19eb464bc2be 261 }
Kojto 170:19eb464bc2be 262
Kojto 170:19eb464bc2be 263 pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
Kojto 170:19eb464bc2be 264 SystemCoreClock = pllvco/pllp;
Kojto 170:19eb464bc2be 265 break;
Kojto 170:19eb464bc2be 266 default:
Kojto 170:19eb464bc2be 267 SystemCoreClock = HSI_VALUE;
Kojto 170:19eb464bc2be 268 break;
Kojto 170:19eb464bc2be 269 }
Kojto 170:19eb464bc2be 270 /* Compute HCLK frequency --------------------------------------------------*/
Kojto 170:19eb464bc2be 271 /* Get HCLK prescaler */
Kojto 170:19eb464bc2be 272 tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
Kojto 170:19eb464bc2be 273 /* HCLK frequency */
Kojto 170:19eb464bc2be 274 SystemCoreClock >>= tmp;
Kojto 170:19eb464bc2be 275 }
Kojto 170:19eb464bc2be 276
Kojto 170:19eb464bc2be 277 /**
Kojto 170:19eb464bc2be 278 * @}
Kojto 170:19eb464bc2be 279 */
Kojto 170:19eb464bc2be 280
Kojto 170:19eb464bc2be 281 /**
Kojto 170:19eb464bc2be 282 * @}
Kojto 170:19eb464bc2be 283 */
Kojto 170:19eb464bc2be 284
Kojto 170:19eb464bc2be 285 /**
Kojto 170:19eb464bc2be 286 * @}
Kojto 170:19eb464bc2be 287 */
Kojto 170:19eb464bc2be 288 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/