/**
******************************************************************************
* File 03_Calibration/main.c
* Author MCD Application Team
* Version V1.2.0
* Date 05-February-2016
* Brief This code example shows how to configure the RTC
* in order to have a calendar with a calibration.
*
==============================================================================
##### RCC specific features #####
==============================================================================
[..] After reset the device is running from MSI (2 MHz) with Flash 0 WS,
and voltage scaling range is 2 (1.5V)
all peripherals are off except internal SRAM, Flash and SW-DP.
(+) There is no prescaler on High speed (AHB) and Low speed (APB) busses;
all peripherals mapped on these busses are running at MSI speed.
(+) The clock for all peripherals is switched off, except the SRAM and
FLASH.
(+) All GPIOs are in analog state, except the SW-DP pins which
are assigned to be used for debug purpose.
[..] Once the device started from reset, the user application has to:
(+) Configure the clock source to be used to drive the System clock
(if the application needs higher frequency/performance)
(+) Configure the System clock frequency and Flash settings
(+) Configure the AHB and APB busses prescalers
(+) Enable the clock for the peripheral(s) to be used
(+) Configure the clock source(s) for peripherals whose clocks are not
derived from the System clock (ADC, RTC/LCD, RNG and IWDG)
===============================================================================
##### MCU Resources #####
===============================================================================
- RCC
- GPIO PA9(USART1_TX),PA10(USART1_RX), PA0, PA5, PB4,
- RTC
- EXTI
===============================================================================
##### How to use this example #####
===============================================================================
- this file must be inserted in a project containing the following files :
o system_stm32l0xx.c, startup_stm32l053xx.s
o stm32l0xx.h to get the register definitions
o CMSIS files
===============================================================================
##### How to test this example #####
===============================================================================
- Plug cable " USB to TTL 3V3 " (from FTDIChip)
- Connect FTDI Rx to USART1 Tx(PA9)and FTDI Tx to USART1 Rx(PA10)
- Launch serial communication SW
- PC interface is configured as follows:
- Data Length = 8 Bits
- One Stop Bit
- None parity
- BaudRate = 9600 baud
- Flow control: None
- Launch the program
- The green LED is blinking Green to indicate that RTC is well configured
and time is displayed on PC interface
- Press the user button to enter in the RTC init mode
- Follow the step on the interface to change the RTC time
- The clock calibration output is selected on PC13 and can be check with an oscilloscope
- a smooth calibration is set at around +20ppm
- note: there is no LSE on board so the LSI is used instead, please keep in mind
to have a precise time the LSE has to be used in a normal application
*
******************************************************************************
* Attention
*
* <h2><center>© COPYRIGHT 2015 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 "stm32l0xx.h"
#include "string.h"
/** STM32L0_Snippets
*
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Time-out values */
#define HSI_TIMEOUT_VALUE ((uint32_t)100) /* 100 ms */
#define PLL_TIMEOUT_VALUE ((uint32_t)100) /* 100 ms */
#define CLOCKSWITCH_TIMEOUT_VALUE ((uint32_t)5000) /* 5 s */
/* Delay value : short one is used for the error coding, long one (~1s) in case
of no error or between two bursts */
#define SHORT_DELAY 200
#define LONG_DELAY 1000
/* Error codes used to make the red led blinking */
#define ERROR_RTC 0x01
#define ERROR_HSI_TIMEOUT 0x02
#define ERROR_PLL_TIMEOUT 0x03
#define ERROR_CLKSWITCH_TIMEOUT 0x04
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
static __IO uint32_t Tick;
volatile uint16_t error = 0; //initialized at 0 and modified by the functions
uint8_t send = 0;
uint8_t stringtosend[20] = "\n00 : 00 : 00 ";
uint8_t RTC_InitializationMode = 0;
uint8_t CharToReceive = 0;
uint8_t CharReceived = 0;
uint8_t Alarm = 1; /* set to 1 for the first print */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Configure_GPIO_LED(void);
void Configure_GPIO_USART1(void);
void Configure_USART1(void);
void Configure_RTC(void);
void Init_RTC(uint32_t Time);
void Configure_GPIO_Button(void);
void Configure_EXTI(void);
void Process(void);
/* Private functions ---------------------------------------------------------*/
/**
* Brief Main program.
* Param None
* Retval None
*/
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32l0xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32l0xx.c file
*/
SysTick_Config(2000); /* 1ms config */
SystemClock_Config();
Configure_GPIO_LED();
if (error != 0)
{
while(1) /* endless loop */
{
}
}
SysTick_Config(16000); /* 1ms config */
Configure_GPIO_USART1();
Configure_USART1();
Configure_RTC();
Init_RTC(0);
Configure_GPIO_Button();
Configure_EXTI();
/* Infinite loop */
while(1)
{
Process();
}
}
/**
* Brief This function configures the system clock @16MHz and voltage scale 1
* assuming the registers have their reset value before the call.
* POWER SCALE = RANGE 1
* SYSTEM CLOCK = PLL MUL8 DIV2
* PLL SOURCE = HSI/4
* FLASH LATENCY = 0
* Param None
* Retval None
*/
__INLINE void SystemClock_Config(void)
{
uint32_t tickstart;
/* (1) Enable power interface clock */
/* (2) Select voltage scale 1 (1.65V - 1.95V)
i.e. (01) for VOS bits in PWR_CR */
/* (3) Enable HSI divided by 4 in RCC-> CR */
/* (4) Wait for HSI ready flag and HSIDIV flag */
/* (5) Set PLL on HSI, multiply by 8 and divided by 2 */
/* (6) Enable the PLL in RCC_CR register */
/* (7) Wait for PLL ready flag */
/* (8