Implement Flash configuration

This commit is contained in:
Steins7 2023-03-26 21:07:37 +02:00
parent 7f7cb077dc
commit 3a768d9190
3 changed files with 92 additions and 3 deletions

47
drivers/flash.c Normal file
View File

@ -0,0 +1,47 @@
/** @file flash.c
* Module handling flash memory interface registers.
*
* The module provides functions to configure the flash and perform read and
* writes
*/
//--includes--------------------------------------------------------------------
#include "flash.h"
#include "flash_regs.h"
//--local definitions-----------------------------------------------------------
//--local variables-------------------------------------------------------------
static volatile struct FLASH* regs = (struct FLASH*)FLASH_BASE_ADDRESS;
//--public functions------------------------------------------------------------
void flash_configure(enum FlashPreset preset)
{
//restore default configuration
regs->ACR.word &= !0x3f;
regs->ACR.word |= 0x30;
//apply new configuration
switch (preset) {
case FLASH_PRESET_LOW_CLOCK_SPEED:
regs->ACR.HLFCYA = 0x1; //half cycle for power saving
break;
case FLASH_PRESET_MEDIUM_CLOCK_SPEED:
regs->ACR.LATENCY = 0x1;
break;
case FLASH_PRESET_HIGH_CLOCK_SPEED:
regs->ACR.LATENCY = 0x2;
break;
default:
break;
}
}
//--local functions-------------------------------------------------------------

39
drivers/flash.h Normal file
View File

@ -0,0 +1,39 @@
/** @file flash.h
* Module handling flash memory interface registers.
*
* The module provides functions to configure the flash and perform read and
* writes
*/
#ifndef _FLASH_H_
#define _FLASH_H_
//--includes--------------------------------------------------------------------
#include "stdint.h"
//--type definitions------------------------------------------------------------
/**
* Available flash configuration presets
*/
enum FlashPreset {
FLASH_PRESET_LOW_CLOCK_SPEED, //for 0 < clock <= 24Mz
FLASH_PRESET_MEDIUM_CLOCK_SPEED, //for 24MHz < clock <= 48MHz
FLASH_PRESET_HIGH_CLOCK_SPEED, //for 48MHz < clock <= 72MHz
};
//--functions-------------------------------------------------------------------
/**
* Configure the flash according to the given preset. Warning: calling this
* function with a sysclk higher than 24Mhz may cause the system to crash,
* please lower the clocks first
*/
void flash_configure(enum FlashPreset preset);
#endif //_FLASH_H_

View File

@ -9,6 +9,7 @@
#include "rcc.h"
#include "rcc_regs.h"
#include "flash.h"
//--local definitions-----------------------------------------------------------
@ -78,6 +79,9 @@ static void apply_default_preset(void)
regs->CIR.HSIRDYIE = 0x0;
regs->CIR.HSERDYIE = 0x0;
regs->CIR.PLLRDYIE = 0x0;
//reconfigure flash
flash_configure(FLASH_PRESET_LOW_CLOCK_SPEED);
}
/**
@ -116,9 +120,8 @@ static void apply_speed_preset(void)
regs->CFGR.PPRE1 = 0x4; // /2
regs->CFGR.ADCPRE = 0x2; // /6
//configure flash
volatile uint32_t* ACR = (uint32_t*)0x40022000;
*ACR |= 0x12;
//reconfigure flash
flash_configure(FLASH_PRESET_HIGH_CLOCK_SPEED);
//switch to PLL output
regs->CFGR.SW = 0x2;