diff --git a/drivers/flash.c b/drivers/flash.c new file mode 100644 index 0000000..bed13ea --- /dev/null +++ b/drivers/flash.c @@ -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------------------------------------------------------------- + diff --git a/drivers/flash.h b/drivers/flash.h new file mode 100644 index 0000000..4e49659 --- /dev/null +++ b/drivers/flash.h @@ -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_ + diff --git a/drivers/rcc.c b/drivers/rcc.c index c071afa..dc488ec 100644 --- a/drivers/rcc.c +++ b/drivers/rcc.c @@ -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;