/**
 ******************************************************************************
 * @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"

#define VELIKOST_DAT 1000
uint32_t data1[VELIKOST_DAT];

void prenosDat(uint32_t pocetIteraci)
{
	uint32_t data2[VELIKOST_DAT];

	for(uint32_t x = 0U; x < pocetIteraci; x++)
	{
		for(uint32_t y = 0U; y < VELIKOST_DAT; y++)
		{
			data1[y] = (data2[y])++;
		}
	}
}

void nastavHsi(void)
{
	// zapne interni vysokorychlostni oscilator
	RCC->CR |= RCC_CR_HSION;

	// cekej, dokud HSI neni pripraveny
	while (!(RCC->CR & RCC_CR_HSIRDY));

	// nastavi HSI jako zdroj hodinoveho signalu
	RCC->CFGR |= RCC_CFGR_SW_HSI;

	// cekej, dokud neni zdroj prepnuty
	while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI);

	// vypne interni vicerychlostni oscilator
	RCC->CR &= ~RCC_CR_MSION;
}

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

	// nastavi napetovou uroven 3
	PWR->CR = (3 << PWR_CR_VOS_Pos);

	// reset
	RCC->ICSCR &= ~(RCC_ICSCR_MSIRANGE_Msk);

	// nastavi MSI na 4,194 MHz
	RCC->ICSCR |= (6 << RCC_ICSCR_MSIRANGE_Pos);

	prenosDat(400);

	// vydeli HSI 4x -> 4 MHz
	RCC->CR |= RCC_CR_HSIDIVEN;

	nastavHsi();

	prenosDat(400);

	// nastavi napetovou uroven 2
	PWR->CR = (2 << PWR_CR_VOS_Pos);

	// delicka kmitoctu /2 -> systemovy kmitocet = 2 MHz
	RCC->CFGR = (RCC_CFGR_HPRE_DIV2 | (RCC->CFGR & ~(RCC_CFGR_HPRE_Msk)));

	// vypne delicku HSI -> 8 MHz
	RCC->CR &= ~RCC_CR_HSIDIVEN;

	prenosDat(400);

	// prida cekaci stav ke cteni pameti
	FLASH->ACR |= FLASH_ACR_LATENCY;
	while ((FLASH->ACR & FLASH_ACR_LATENCY) == 0);

	// delicka kmitoctu /1 -> systemovy kmitocet = 16 MHz
	RCC->CFGR = (RCC_CFGR_HPRE_DIV1 | (RCC->CFGR & ~(RCC_CFGR_HPRE_Msk)));

	prenosDat(400);

	// nastavi napetovou uroven 1
	PWR->CR = (1 << PWR_CR_VOS_Pos);

	// odebre cekaci stav ke cteni pameti
	FLASH->ACR &= ~FLASH_ACR_LATENCY;

	prenosDat(400);

	while(1)
	{
		prenosDat(400);
	}
}
