/**
 ******************************************************************************
 * @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 nastavExti13()
{
	// zapne hodinovy signal pro port C
	RCC->IOPENR |= RCC_IOPENR_IOPCEN;

	// nastavi PC13 jako vstupni pin
	GPIOC->MODER &= ~GPIO_MODER_MODE13;

	// zapne hodinovy signal pro System Configuration Controller
	RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;

	// pripoji externi preruseni 13 na port C
	SYSCFG->EXTICR[3] |= SYSCFG_EXTICR4_EXTI13_PC;

	// nastavi reakci linky 13 na sestupnou hranu signalu
	EXTI->FTSR |= EXTI_FTSR_FT13;

	// povoli externi preruseni linky 13
	EXTI->IMR |= EXTI_IMR_IM13;

	// povoli externi preruseni linky 4 az 15 ARM jadra
	NVIC->ISER[0] |= (1U << EXTI4_15_IRQn);
}

void EXTI4_15_IRQHandler(void)
{
	// vynulovani priznaku preruseni
	EXTI->PR |= EXTI_PR_PIF13;

	// vynulovani priznaku preruseni v jadru ARM
	NVIC->ICPR[0] |= (1 << EXTI4_15_IRQn);
}

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

	nastavExti13();

	// nastavi HSI jako zdroj hodinoveho signalu
	// po probuzeni (vychozi MSI)
	//RCC->CFGR |= RCC_CFGR_STOPWUCK;

	// vypne zdroj referencniho napeti, s nim se
	// vypne i BOR, PVD a interni senzor teploty
	PWR->CR |= PWR_CR_ULP;

	// prepne regulator napeti do nizkoodberoveho rezimu
	PWR->CR |= PWR_CR_LPSDSR;

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

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

	while(1);
}


