/**
 ******************************************************************************
 * @file           : main.c
 * @author         : Auto-generated by STM32CubeIDE
 * @brief          : Main program body
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by ST under BSD 3-Clause license,
 * the "License"; You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                        opensource.org/licenses/BSD-3-Clause
 *
 ******************************************************************************
 */

#include "stm32l073xx.h"

#if !defined(__SOFT_FP__) && defined(__ARM_FP)
  #warning "FPU is not initialized, but the project is compiling for an FPU. Please initialize the FPU before use."
#endif

void nastavUart(void)
{
	// zapne hodinovy signal pro port A
	RCC->IOPENR |= RCC_IOPENR_IOPAEN;

	// vynuluje nastaveni rezimu PA2, PA3 (vstupni rezim)
	GPIOA->MODER &= ~(GPIO_MODER_MODE2 | GPIO_MODER_MODE3);

	// nastavi  PA2 a PA3 do alternativniho rezimu
	GPIOA->MODER |= (GPIO_MODER_MODE2_1 | GPIO_MODER_MODE3_1);

	// nastavi pinu PA2 a PA3 alternativni rezim 4 - UART
	GPIOA->AFR[0] |= ((4U << GPIO_AFRL_AFSEL2_Pos) | (4U << GPIO_AFRL_AFSEL3_Pos));

	// zapne hodinovy signal pro USART2
	RCC->APB1ENR |= RCC_APB1ENR_USART2EN;

	// 2097 MHz / 219 = 9575 Baud
	USART2->BRR = 219U;

	// povoli USARTa prijem dat
	USART2->CR1 |= (USART_CR1_UE | USART_CR1_TE);
}

void zapniLse(void)
{
	// zapne periferii nastaveni napajeni
	RCC->APB1ENR |= RCC_APB1ENR_PWREN;

	// vypne ochranu pred zapisem do RCC->CSR registru
	PWR->CR |= PWR_CR_DBP;

	// zapne LSE s nejvyssim ridicim proudem
	RCC->CSR |= (RCC_CSR_LSEON | RCC_CSR_LSEDRV_Msk);

	// cekej, nez je LSE pripraven
	while((RCC->CSR & RCC_CSR_LSERDY) != RCC_CSR_LSERDY);
}

void nastavRtcTamp(void)
{
	// zapne periferii nastaveni napajeni
	RCC->APB1ENR |= RCC_APB1ENR_PWREN;

	// vypne ochranu pred zapisem do RCC->CSR registru
	// a do vsech registru RTC
	PWR->CR |= PWR_CR_DBP;

	// zvoli LSE jako zdroj hodinoveho signalu pro RTC a zapne jej
	RCC->CSR |= (RCC_CSR_RTCSEL_LSE | RCC_CSR_RTCEN);

	// zapne detekci poskozeni (kanal 1) a povoli preruseni
	// nastavi detekci pri vysoke urovni dlouhe 2 cykly (2 sekundy)
	RTC->TAMPCR |= (RTC_TAMPCR_TAMP1E | RTC_TAMPCR_TAMP1IE |
				    RTC_TAMPCR_TAMPFLT_0 | RTC_TAMPCR_TAMP1TRG);

	// povoli linku 19 externiho preruseni
	EXTI->IMR |= EXTI_IMR_IM19;

	// nastavi lince ext. preruseni 19 reakci na nabeznou hranu
	EXTI->RTSR |= EXTI_RTSR_RT19;

	// povoli preruseni linky ARM pro RTC
	NVIC->ISER[0] |= (1 << RTC_IRQn);
}


void RTC_IRQHandler(void)
{
	// vynuluje priznak preruseni periferie
	RTC->ISR &= ~RTC_ISR_TAMP1F;

	// vynuluje priznak externiho preruseni
	EXTI->PR |= EXTI_PR_PIF19;

	// vynuluje priznak preruseni linky ARM
	NVIC->ICPR[0] |= (1 << RTC_IRQn);

	// odesle znak
	USART2->TDR = 'x';
	while((USART2->ISR & USART_ISR_TC) != USART_ISR_TC);
}

int main(void)
{
	zapniLse();

	nastavUart();

	nastavRtcTamp();

	// vrati se do odstaveneho rezimu po opusteni ISR
	SCB->SCR |= SCB_SCR_SLEEPONEXIT_Msk;

	// nastavi odstaveny rezim pri operaci "wfi"
	SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;

	// zapne odstaveny rezim probuzeny prerusenim
	asm("wfi");
}
