summaryrefslogtreecommitdiff
path: root/Project
diff options
context:
space:
mode:
Diffstat (limited to 'Project')
-rw-r--r--Project/inc/main.h41
-rw-r--r--Project/inc/stm32f0xx_conf.h84
-rw-r--r--Project/inc/stm32f0xx_it.h53
-rw-r--r--Project/src/main.c178
-rw-r--r--Project/src/stm32f0xx_it.c254
-rw-r--r--Project/src/system_stm32f0xx.c402
6 files changed, 1012 insertions, 0 deletions
diff --git a/Project/inc/main.h b/Project/inc/main.h
new file mode 100644
index 0000000..ce89a06
--- /dev/null
+++ b/Project/inc/main.h
@@ -0,0 +1,41 @@
+/**
+ ******************************************************************************
+ * @file STM32L152_Ex01_3TKeys_EVAL\inc\main.h
+ * @author MCD Application Team
+ * @version V1.1.0
+ * @date 24-February-2014
+ * @brief Header for main.c module
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
+ *
+ * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.st.com/software_license_agreement_liberty_v2
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************
+ */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __MAIN_H
+#define __MAIN_H
+
+
+/* Exported types ------------------------------------------------------------*/
+/* Exported variables --------------------------------------------------------*/
+/* Exported macros -----------------------------------------------------------*/
+/* Exported functions ------------------------------------------------------- */
+
+
+#endif /* __MAIN_H */
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff --git a/Project/inc/stm32f0xx_conf.h b/Project/inc/stm32f0xx_conf.h
new file mode 100644
index 0000000..adbf30b
--- /dev/null
+++ b/Project/inc/stm32f0xx_conf.h
@@ -0,0 +1,84 @@
+/**
+ ******************************************************************************
+ * @file Projects\Demonstration\stm32f0xx_conf.h
+ * @author MCD Application Team
+ * @version V1.0.0
+ * @date 17-January-2014
+ * @brief Library configuration file.
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
+ *
+ * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.st.com/software_license_agreement_liberty_v2
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************
+ */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __STM32F0XX_CONF_H
+#define __STM32F0XX_CONF_H
+
+
+/* Includes ------------------------------------------------------------------*/
+/* Comment the line below to disable peripheral header file inclusion */
+#include "stm32f0xx_adc.h"
+//#include "stm32f0xx_can.h"
+//#include "stm32f0xx_cec.h"
+//#include "stm32f0xx_crc.h"
+//#include "stm32f0xx_crs.h"
+//#include "stm32f0xx_comp.h"
+//#include "stm32f0xx_dac.h"
+//#include "stm32f0xx_dbgmcu.h"
+#include "stm32f0xx_dma.h"
+//#include "stm32f0xx_exti.h"
+//#include "stm32f0xx_flash.h"
+#include "stm32f0xx_gpio.h"
+#include "stm32f0xx_syscfg.h"
+//#include "stm32f0xx_i2c.h"
+//#include "stm32f0xx_iwdg.h"
+#include "stm32f0xx_pwr.h"
+#include "stm32f0xx_rcc.h"
+#include "stm32f0xx_rtc.h"
+#include "stm32f0xx_spi.h"
+#include "stm32f0xx_tim.h"
+//#include "stm32f0xx_usart.h"
+//#include "stm32f0xx_wwdg.h"
+#include "stm32f0xx_misc.h" /* High level functions for NVIC and SysTick (add-on to CMSIS functions) */
+
+/* Exported types ------------------------------------------------------------*/
+/* Exported constants --------------------------------------------------------*/
+/* Uncomment the line below to expanse the "assert_param" macro in the
+ Standard Peripheral Library drivers code */
+/*#define USE_FULL_ASSERT 1*/
+
+/* Exported macro ------------------------------------------------------------*/
+#ifdef USE_FULL_ASSERT
+
+/**
+ * @brief The assert_param macro is used for function's parameters check.
+ * @param expr: If expr is false, it calls assert_failed function which reports
+ * the name of the source file and the source line number of the call
+ * that failed. If expr is true, it returns no value.
+ * @retval None
+ */
+ #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__))
+/* Exported functions ------------------------------------------------------- */
+ void assert_failed(uint8_t* file, uint32_t line);
+#else
+ #define assert_param(expr) ((void)0)
+#endif /* USE_FULL_ASSERT */
+
+#endif /* __STM32F0XX_CONF_H */
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff --git a/Project/inc/stm32f0xx_it.h b/Project/inc/stm32f0xx_it.h
new file mode 100644
index 0000000..4abda02
--- /dev/null
+++ b/Project/inc/stm32f0xx_it.h
@@ -0,0 +1,53 @@
+/**
+ ******************************************************************************
+ * @file Projects\Demonstration\stm32f0xx_it.h
+ * @author MCD Application Team
+ * @version V1.0.0
+ * @date 17-January-2014
+ * @brief This file contains the headers of the interrupt handlers.
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
+ *
+ * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.st.com/software_license_agreement_liberty_v2
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************
+ */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __STM32F0XX_IT_H
+#define __STM32F0XX_IT_H
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+/* Includes ------------------------------------------------------------------*/
+#include "main.h"
+
+/* Exported types ------------------------------------------------------------*/
+/* Exported constants --------------------------------------------------------*/
+/* Exported macro ------------------------------------------------------------*/
+/* Exported functions ------------------------------------------------------- */
+
+void NMI_Handler(void);
+void HardFault_Handler(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __STM32F0XX_IT_H */
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff --git a/Project/src/main.c b/Project/src/main.c
new file mode 100644
index 0000000..bf1c5b0
--- /dev/null
+++ b/Project/src/main.c
@@ -0,0 +1,178 @@
+/**
+ ******************************************************************************
+ * @file STM32L152_Ex01_3TKeys_EVAL\src\main.c
+ * @author MCD Application Team
+ * @version V1.1.0
+ * @date 24-February-2014
+ * @brief Basic example of how to use the STMTouch Driver.
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
+ *
+ * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.st.com/software_license_agreement_liberty_v2
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************
+ */
+
+/*
+ Здесь необходимо создать большой комментарий о характеристиках железа МК. Или
+же лучше создать отдельный файл и сделать на него ссылку. Кстати, такой файл
+создает программа STM32CubeMX.
+
+И вообще, нужно создать глобальную папку проекта, где будет лежать все по
+полочкам ( схема, программа, текстовые файлы, документация и тд )
+*/
+
+//#define LED_DRIVER
+
+// Это программа для версии NixieClock_v2, которая является модифицированной версией предыдущей
+// NixieLUT
+
+/* Includes ------------------------------------------------------------------*/
+#include "main.h"
+
+// FreeRTOS includes
+#include "FreeRTOS.h"
+#include "task.h"
+#include "queue.h"
+#include "semphr.h"
+
+#include "ltimers.h"
+#include "head_task.h"
+#include "button_task.h"
+#include "button_handler.h"
+#include "nixie_driver_task.h"
+#include "light_sensor_task.h"
+
+#ifdef LED_DRIVER
+#include "led_driver_task.h"
+#endif
+
+#include "indicate_modes_task.h"
+#include "tsl_user.h"
+#include "time.h"
+
+
+/* Private typedefs ----------------------------------------------------------*/
+/* Private defines -----------------------------------------------------------*/
+/* Private macros ------------------------------------------------------------*/
+
+// Задаем размеры кучи для каждой задачи отдельно
+// - как правильно определить необходимый минимум для кучи задачи?
+
+#define STACK_SIZE_HEAD ( (unsigned short) 230 )
+#define STACK_SIZE_NIXIE_DRIVER ( (unsigned short) 80 )
+#define STACK_SIZE_BUTTON ( (unsigned short) 200 )
+#define STACK_SIZE_LIGHT_SENSOR ( (unsigned short) 70 )
+#ifdef LED_DRIVER
+#define STACK_SIZE_LED_DRIVER ( (unsigned short) 70 )
+#endif
+#define STACK_SIZE_INDICATE_MODES ( (unsigned short) 250 )
+
+/* Private functions prototype ---------------------------------------------- */
+
+
+// ----------------------------------------------------------------------------
+// Инициализация до создания задач
+// ----------------------------------------------------------------------------
+void InitAll ( void )
+{
+ InitLTimers ();
+ NixieDriverInit ();
+ LightSensorInit();
+#ifdef LED_DRIVER
+ LED_DriverInit ();
+#endif
+ TSL_user_Init ();
+ TimeInit ();
+ HeadTaskInit ();
+ IndicateModesInit ();
+ ButtonInit ();
+}
+
+
+BaseType_t TaskSuccess = NULL;
+
+// Главный цикл ------------------------------------------------------------- //
+int main ()
+{
+ InitAll ();
+
+ // Создаем задачи FreeRTOS ----------------------------------------------- //
+
+ // ВНИМАНИЕ !!!
+ // Необходимо проверять кучу при создании задачи! ( и очереди )
+
+ // Если в программе используется подключаемая библиотека, которая использует
+ // ОСРВ, то задача должна там и создаватья. То есть это нужно оформить в
+ // в виде специальной функции
+
+ //BaseType_t TaskSuccess = NULL;
+
+ TaskSuccess = xTaskCreate ( Head_Task, "Head_Task", STACK_SIZE_HEAD, NULL, tskIDLE_PRIORITY + 2, NULL );
+ configASSERT( TaskSuccess );
+
+ TaskSuccess = xTaskCreate ( IndicateModes_Task, "IndicateModes_Task", STACK_SIZE_INDICATE_MODES, NULL, tskIDLE_PRIORITY + 2, NULL );
+ configASSERT( TaskSuccess );
+
+ TaskSuccess = xTaskCreate ( Button_Task, "Button_Task", STACK_SIZE_BUTTON, NULL, tskIDLE_PRIORITY + 2, NULL );
+ configASSERT( TaskSuccess );
+
+ TaskSuccess = xTaskCreate ( LightSensor_Task, "LightSensor_Task", STACK_SIZE_LIGHT_SENSOR, NULL, tskIDLE_PRIORITY + 2, NULL );
+ configASSERT( TaskSuccess );
+
+ TaskSuccess = xTaskCreate ( NixieDriver_Task, "NixieDriver_Task", STACK_SIZE_NIXIE_DRIVER, NULL, tskIDLE_PRIORITY + 2, NULL );
+ configASSERT( TaskSuccess );
+
+#ifdef LED_DRIVER
+ TaskSuccess = xTaskCreate ( LED_Driver_Task, "LED_Driver_Task", STACK_SIZE_LED_DRIVER, NULL, tskIDLE_PRIORITY + 2, NULL );
+ configASSERT( TaskSuccess );
+#endif
+
+ vTaskStartScheduler (); // И запускаем диспетчер задач ( он же планировщик )
+
+} // end of main
+
+
+
+#ifdef USE_FULL_ASSERT
+/**
+ * @brief Reports the name of the source file and the source line number
+ * where the assert_param error has occurred.
+ * @param file: pointer to the source file name
+ * @param line: assert_param error line source number
+ * @retval None
+ */
+void assert_failed ( uint8_t* file, uint32_t line )
+{
+ /* User can add his own implementation to report the file name and line number,
+ ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
+
+ /* Infinite loop */
+ for (;;)
+ {
+ }
+}
+#endif
+
+
+// Специальные ф-ии FreeRTOS ------------------------------------------------ //
+void vApplicationMallocFailedHook ( void ) { for( ;; ); }
+void vApplicationStackOverflowHook ( TaskHandle_t pxTask, char *pcTaskName ) { for( ;; ); }
+void vApplicationIdleHook ( void ) { }
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
+
+
+
diff --git a/Project/src/stm32f0xx_it.c b/Project/src/stm32f0xx_it.c
new file mode 100644
index 0000000..20e6b67
--- /dev/null
+++ b/Project/src/stm32f0xx_it.c
@@ -0,0 +1,254 @@
+/**
+ ******************************************************************************
+ * @file Projects\Demonstration\stm32f0xx_it.c
+ * @author MCD Application Team
+ * @version V1.0.0
+ * @date 17-January-2014
+ * @brief Main Interrupt Service Routines.
+ * This file provides template for all exceptions handler and
+ * peripherals interrupt service routine.
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
+ *
+ * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.st.com/software_license_agreement_liberty_v2
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************
+ */
+
+/* Includes ------------------------------------------------------------------*/
+#include "stm32f0xx_it.h"
+#include "stm32f0xx_conf.h"
+
+#include "ltimers.h"
+#include "ltimers_config.h"
+
+#include "nixie_driver_config.h"
+#include "nixie_driver_process.h"
+#include "led_driver_process.h"
+#include "led_driver_config.h"
+
+#include "tsl_types.h"
+#include "tsl_user.h"
+#include "tsl_time_stm32f0xx.h"
+
+
+/** @addtogroup STM32F072B_DISCOVERY
+ * @{
+ */
+
+/* Private typedef -----------------------------------------------------------*/
+/* Private define ------------------------------------------------------------*/
+/* Private macro -------------------------------------------------------------*/
+/* Private variables ---------------------------------------------------------*/
+
+/* Private function prototypes -----------------------------------------------*/
+/* Private functions ---------------------------------------------------------*/
+
+/******************************************************************************/
+/* Cortex-M0 Processor Exceptions Handlers */
+/******************************************************************************/
+
+/**
+ * @brief This function handles NMI exception.
+ * @param None
+ * @retval None
+ */
+void NMI_Handler(void)
+{
+}
+
+/**
+ * @brief This function handles Hard Fault exception.
+ * @param None
+ * @retval None
+ */
+void HardFault_Handler(void)
+{
+ /* Go to infinite loop when Hard Fault exception occurs */
+ while (1)
+ {
+ }
+}
+
+/**
+ * @brief This function handles SVCall exception.
+ * @param None
+ * @retval None
+ * @ Используется в файле portasm.s
+ */
+//void SVC_Handler(void)
+//{
+//}
+
+/**
+ * @brief This function handles PendSVC exception.
+ * @param None
+ * @retval None
+ * @ Используется в файле portasm.s
+ */
+//void PendSV_Handler(void)
+//{
+//}
+
+/**
+ * @brief This function handles SysTick Handler.
+ * @param None
+ * @retval None
+ */
+//void SysTick_Handler(void)
+//{
+// TimingDelay_Decrement();
+// TSL_tim_ProcessIT();
+//}
+
+/******************************************************************************/
+/* STM32F0xx Peripherals Interrupt Handlers */
+/* Add here the Interrupt Handler for the used peripheral(s) (PPP), for the */
+/* available peripheral interrupt handler's name please refer to the startup */
+/* file (startup_stm32f0xx.s). */
+/******************************************************************************/
+
+// ----------------------------------------------------------------------------
+// Обработчик прерывания таймера общего назначения для тиков в 1 мс
+// ----------------------------------------------------------------------------
+void LTIMER_IRQHandler ( void )
+{
+ if ( TIM_GetITStatus ( LTIMER_TIMx, TIM_IT_Update ) != RESET )
+ {
+ TIM_ClearITPendingBit ( LTIMER_TIMx, TIM_IT_Update );
+
+ ProcessLTimers ();
+ //NIX_TEST_PIN_PB11_TOGGLE;
+ }
+}
+
+
+// ----------------------------------------------------------------------------
+// Обработчик прерывания таймера для работы NixieDriver
+// Это прерывание имеет самый высокий приоритет, чтобы обеспечить стабильную
+// частоту динамической индикации ламп Nixie
+// ----------------------------------------------------------------------------
+void NIX_DRIVER_TIM_IRQHandler ( void )
+{
+ if ( TIM_GetITStatus ( NIX_DRIVER_TIMx, TIM_IT_Update ) != RESET )
+ {
+ TIM_ClearITPendingBit ( NIX_DRIVER_TIMx, TIM_IT_Update );
+
+ ProcessNixieDriverFromISR ();
+ }
+}
+
+// ----------------------------------------------------------------------------
+// Обработчик прерывания таймера для работы LED_Driver
+// ----------------------------------------------------------------------------
+void LED_TIM_IRQHandler ( void )
+{
+ if ( TIM_GetITStatus ( LED_TIMx, TIM_IT_Update ) != RESET )
+ {
+ TIM_ClearITPendingBit ( LED_TIMx, TIM_IT_Update );
+
+ LEDDriverProcessFromISR ();
+ }
+}
+
+
+// ----------------------------------------------------------------------------
+// - поставить этому прерыванию наивысший приоритет, чтобы передавать 2 байта
+// подряд гарантированно без промежутка времени (или не так важно? может ли
+// между двумя байтами задержка повлиять на вывод данных на лампы?)
+// ----------------------------------------------------------------------------
+//static uint16_t empty_data;
+
+void NIX_SPIx_IRQHandler ( void )
+{
+ /* SPI in Master Receiver mode--------------------------------------- */
+ if ( SPI_I2S_GetITStatus ( NIX_SPIx, SPI_I2S_IT_TXE ) == SET )
+ {
+ // Обязательно надо считать, чтобы сбросить флаг
+ SPI_I2S_ReceiveData16 ( NIX_SPIx );
+ SPI_I2S_ITConfig ( NIX_SPIx, SPI_I2S_IT_TXE, DISABLE );
+ //NixieDriverCheckDPPins();
+ NIX_DRIVER_SET_ST_PIN;
+ }
+}
+
+
+// ----------------------------------------------------------------------------
+//
+// ----------------------------------------------------------------------------
+#ifdef LED_DRIVER
+void LED_SPIx_IRQHandler ( void )
+{
+ /* SPI in Master Receiver mode--------------------------------------- */
+ if ( SPI_I2S_GetITStatus ( LED_SPIx, SPI_I2S_IT_RXNE ) == SET )
+ {
+ //empty_data = SPI_I2S_ReceiveData16 ( LED_SPIx );
+ // Обязательно надо считать, чтобы сбросить флаг
+ SPI_I2S_ReceiveData16 ( LED_SPIx );
+ LED_ST_PIN_SET;
+ }
+}
+#endif
+
+
+// ----------------------------------------------------------------------------
+/**
+ * @brief This function handles Touch Sensing Controller Handler.
+ * @param None
+ * @retval None
+ */
+// ----------------------------------------------------------------------------
+extern __IO uint32_t Gv_EOA;
+
+void TSC_IRQHandler ( void )
+{
+#if TSLPRM_TSC_IODEF > 0 // Default = Input Floating
+ /* Set IO default in Output PP Low to discharge all capacitors */
+ TSC->CR &= (uint32_t)(~(1 << 4));
+#endif
+ TSC->ICR |= 0x03; /* Clear EOAF and MCEF flags */
+ Gv_EOA = 1; /* To inform the main loop routine of the End Of Acquisition */
+}
+
+
+// ----------------------------------------------------------------------------
+//
+// ----------------------------------------------------------------------------
+void TS_TIM_IRQHandler ( void )
+{
+ TIM_ClearITPendingBit ( TS_TIMx, TIM_IT_Update );
+ TSL_tim_ProcessIT();
+}
+
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/Project/src/system_stm32f0xx.c b/Project/src/system_stm32f0xx.c
new file mode 100644
index 0000000..e9db4f2
--- /dev/null
+++ b/Project/src/system_stm32f0xx.c
@@ -0,0 +1,402 @@
+/**
+ ******************************************************************************
+ * @file system_stm32f0xx.c
+ * @author MCD Application Team
+ * @version V1.0.0
+ * @date 17-January-2014
+ * @brief CMSIS Cortex-M0 Device Peripheral Access Layer System Source File.
+ * This file contains the system clock configuration for STM32F0xx devices,
+ * and is customized for use with STM32F072B-DISCOVERY Kit.
+ *
+ *
+ * The STM32F072x is configured to run at 48 MHz, following the five
+ * configuration below:
+ * - SOURCE_HSI48 (default) : HSI48 (48MHz) used as system clock source.
+ * - PLL_SOURCE_HSI : HSI (~8MHz) used to clock the PLL, and
+ * the PLL is used as system clock source.
+ * - PLL_SOURCE_HSI48 : HSI48 (48MHz) used to clock the PLL, and
+ * the PLL is used as system clock source.
+ * - PLL_SOURCE_HSE : HSE (8MHz) used to clock the PLL, and
+ * the PLL is used as system clock source.
+ * - PLL_SOURCE_HSE_BYPASS : HSE bypassed with an external clock
+ * (8MHz, coming from ST-Link) used to clock
+ * the PLL, and the PLL is used as system
+ * clock source.
+ *
+ *
+ * 1. This file provides two functions and one global variable to be called from
+ * user application:
+ * - SystemInit(): Setups the system clock (System clock source, PLL Multiplier
+ * and Divider factors, AHB/APBx prescalers and Flash settings),
+ * depending on the configuration made in the clock xls tool.
+ * This function is called at startup just after reset and
+ * before branch to main program. This call is made inside
+ * the "startup_stm32f0xx.s" file.
+ *
+ * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
+ * by the user application to setup the SysTick
+ * timer or configure other parameters.
+ *
+ * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
+ * be called whenever the core clock is changed
+ * during program execution.
+ *
+ * 2. After each device reset the HSI (8 MHz Range) is used as system clock source.
+ * Then SystemInit() function is called, in "startup_stm32f0xx.s" file, to
+ * configure the system clock before to branch to main program.
+ *
+ * 3. If the system clock source selected by user fails to startup, the SystemInit()
+ * function will do nothing and HSI still used as system clock source. User can
+ * add some code to deal with this issue inside the SetSysClock() function.
+ *
+ * 4. The default value of HSE crystal is set to 8MHz, refer to "HSE_VALUE" define
+ * in "stm32f0xx.h" file. 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 configuration.
+ *
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
+ *
+ * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.st.com/software_license_agreement_liberty_v2
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************
+ */
+
+/** @addtogroup CMSIS
+ * @{
+ */
+
+/** @addtogroup stm32f0xx_system
+ * @{
+ */
+
+/** @addtogroup STM32F0xx_System_Private_Includes
+ * @{
+ */
+
+#include "stm32f0xx.h"
+
+/**
+ * @}
+ */
+
+/** @addtogroup STM32F0xx_System_Private_TypesDefinitions
+ * @{
+ */
+
+/**
+ * @}
+ */
+
+/** @addtogroup STM32F0xx_System_Private_Defines
+ * @{
+ */
+/**
+ * @}
+ */
+
+/*!< Uncomment the following line if you need to relocate your vector Table in
+ Internal SRAM. */
+/* #define VECT_TAB_SRAM */
+#define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field.
+ This value must be a multiple of 0x200. */
+
+/* Select the PLL clock source */
+
+//#define SOURCE_HSI48
+//#define PLL_SOURCE_HSI48 /* HSI48 (48MHz) used to clock USB, system clock and the PLL*/
+//#define PLL_SOURCE_HSI /* HSI (~8MHz) used to clock the PLL, and the PLL is used as system clock source*/
+#define PLL_SOURCE_HSE /* HSE (8MHz) used to clock the PLL, and the PLL is used as system clock source */
+//#define PLL_SOURCE_HSE_BYPASS /* HSE bypassed with an external clock (8MHz, coming from ST-Link) used to clock
+// the PLL, and the PLL is used as system clock source */
+
+
+/** @addtogroup STM32F0xx_System_Private_Macros
+ * @{
+ */
+
+/**
+ * @}
+ */
+
+/** @addtogroup STM32F0xx_System_Private_Variables
+ * @{
+ */
+uint32_t SystemCoreClock = 48000000;
+__I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
+
+/**
+ * @}
+ */
+
+/** @addtogroup STM32F0xx_System_Private_FunctionPrototypes
+ * @{
+ */
+
+static void SetSysClock(void);
+
+/**
+ * @}
+ */
+
+/** @addtogroup STM32F0xx_System_Private_Functions
+ * @{
+ */
+
+/**
+ * @brief Setup the microcontroller system.
+ * Initialize the Embedded Flash Interface, the PLL and update the
+ * SystemCoreClock variable.
+ * @param None
+ * @retval None
+ */
+void SystemInit (void)
+{
+ /* Set HSION bit */
+ RCC->CR |= (uint32_t)0x00000001;
+
+ /* Reset SW[1:0], HPRE[3:0], PPRE[2:0], ADCPRE and MCOSEL[3:0] bits MCOPRE[2:0] */
+ RCC->CFGR &= (uint32_t)0x80FFB80C;
+
+ /* Reset HSEON, CSSON and PLLON bits */
+ RCC->CR &= (uint32_t)0xFEF6FFFF;
+
+ /* Reset HSEBYP bit */
+ RCC->CR &= (uint32_t)0xFFFBFFFF;
+
+ /* Reset PLLSRC, PLLXTPRE and PLLMUL[3:0] bits */
+ RCC->CFGR &= (uint32_t)0xFFC07FFF;
+
+ /* Reset PREDIV1[3:0] bits */
+ RCC->CFGR2 &= (uint32_t)0xFFFFFFF0;
+
+ /* Reset USARTSW[1:0], I2CSW, CECSW and ADCSW bits */
+ RCC->CFGR3 &= (uint32_t)0xFFFFFEAC;
+
+ /* Reset HSI14 & HSI48 bit */
+ RCC->CR2 &= (uint32_t)0xFFFEFFFE;
+
+ /* Disable all interrupts */
+ RCC->CIR = 0x00000000;
+
+ /* Configure the System clock frequency, AHB/APBx prescalers and Flash settings */
+ SetSysClock();
+}
+
+/**
+ * @brief Update SystemCoreClock according to Clock Register Values
+ * The SystemCoreClock variable contains the core clock (HCLK), it can
+ * be used by the user application to setup the SysTick timer or configure
+ * other parameters.
+ *
+ * @note Each time the core clock (HCLK) changes, this function must be called
+ * to update SystemCoreClock variable value. Otherwise, any configuration
+ * based on this variable will be incorrect.
+ *
+ * @note - The system frequency computed by this function is not the real
+ * frequency in the chip. It is calculated based on the predefined
+ * constant and the selected clock source:
+ *
+ * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
+ *
+ * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
+ *
+ * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
+ * or HSI_VALUE(*) multiplied/divided by the PLL factors.
+ * @note - If SYSCLK source is HSI48, function returns constant HSI48_VALUE(***)
+ *
+ * (*) HSI_VALUE is a constant defined in stm32f0xx.h file (default value
+ * 8 MHz) but the real value may vary depending on the variations
+ * in voltage and temperature.
+ *
+ * (**) HSE_VALUE is a constant defined in stm32f0xx.h file (default value
+ * 8 MHz), user has to ensure that HSE_VALUE is same as the real
+ * frequency of the crystal used. Otherwise, this function may
+ * have wrong result.
+ * @note (***) HSI48_VALUE is a constant defined in stm32f0xx.h file (default value
+ * 48 MHz) but the real value may vary depending on the variations
+ * in voltage and temperature.
+ *
+ * - The result of this function could be not correct when using fractional
+ * value for HSE crystal.
+ * @param None
+ * @retval None
+ */
+void SystemCoreClockUpdate (void)
+{
+ uint32_t tmp = 0, pllmull = 0, pllsource = 0, prediv1factor = 0;
+
+ /* Get SYSCLK source -------------------------------------------------------*/
+ tmp = RCC->CFGR & RCC_CFGR_SWS;
+
+ switch (tmp)
+ {
+ case 0x00: /* HSI used as system clock */
+ SystemCoreClock = HSI_VALUE;
+ break;
+ case 0x04: /* HSE used as system clock */
+ SystemCoreClock = HSE_VALUE;
+ break;
+ case 0x08: /* PLL used as system clock */
+ /* Get PLL clock source and multiplication factor ----------------------*/
+ pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
+ pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
+ pllmull = ( pllmull >> 18) + 2;
+
+ if (pllsource == 0x00)
+ {
+ /* HSI oscillator clock divided by 2 selected as PLL clock entry */
+ SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
+ }
+ else
+ {
+ prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
+ /* HSE oscillator clock selected as PREDIV1 clock entry */
+ SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
+ }
+ break;
+ case 0x0C: /* HSI48 used as system clock */
+ SystemCoreClock = HSI48_VALUE;
+ break;
+ default: /* HSI used as system clock */
+ SystemCoreClock = HSI_VALUE;
+ break;
+
+ }
+ /* Compute HCLK clock frequency ----------------*/
+ /* Get HCLK prescaler */
+ tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
+ /* HCLK clock frequency */
+ SystemCoreClock >>= tmp;
+}
+
+/**
+ * @brief Configures the System clock frequency, AHB/APBx prescalers and Flash
+ * settings.
+ * @note This function should be called only once the RCC clock configuration
+ * is reset to the default reset state (done in SystemInit() function).
+ * @param None
+ * @retval None
+ */
+static void SetSysClock(void)
+{
+ __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
+
+ /* SYSCLK, HCLK, PCLK configuration ----------------------------------------*/
+ /* At this stage the HSI is already enabled */
+
+ /* Enable Prefetch Buffer and set Flash Latency */
+ FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY;
+
+ /* HCLK = SYSCLK */
+ RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
+
+ /* PCLK = HCLK */
+ RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE_DIV1;
+
+#ifndef SOURCE_HSI48
+
+ #if defined (PLL_SOURCE_HSI)
+ /* PLL configuration = HSI * 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_HSI_PREDIV | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL6);
+ #elif defined (PLL_SOURCE_HSI48)
+ /* Enable HSI48 */
+ RCC->CR2 |= RCC_CR2_HSI48ON;
+ /* PLL configuration = HSI48 * 1 = 48 MHz */
+ RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
+ RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSI48_PREDIV | RCC_CFGR_PLLXTPRE_PREDIV1_Div2);
+
+ #else
+ /* Enable HSE */
+ RCC->CR |= ((uint32_t)RCC_CR_HSEON);
+ #if defined (PLL_SOURCE_HSE_BYPASS)
+ /* HSE oscillator bypassed with external clock */
+ RCC->CR |= (uint32_t)(RCC_CR_HSEBYP);
+ #endif
+ /* 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)
+ {
+ /* PLL configuration = 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_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL6);
+
+ }
+ 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 /* PLL_SOURCE_HSI */
+ /* 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)
+ {
+ }
+#else
+ /* Enable HSI48 */
+ RCC->CR2 |= RCC_CR2_HSI48ON;
+ /* Wait till HSI48RDY is set */
+ while((RCC->CR2 & RCC_CR2_HSI48RDY) == 0)
+ { }
+ /* Select HSI48 as system clock source */
+ RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
+ RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSI48;
+
+#endif
+
+}
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/