diff options
Diffstat (limited to 'libmaple')
-rw-r--r-- | libmaple/flash.c | 49 | ||||
-rw-r--r-- | libmaple/flash.h | 17 | ||||
-rw-r--r-- | libmaple/rcc.c | 8 |
3 files changed, 46 insertions, 28 deletions
diff --git a/libmaple/flash.c b/libmaple/flash.c index 9442c44..828f938 100644 --- a/libmaple/flash.c +++ b/libmaple/flash.c @@ -22,32 +22,49 @@ * THE SOFTWARE. * ****************************************************************************/ +/** + * @brief flash peripheral management functions + */ + + #include "libmaple.h" #include "flash.h" -#define ACR_PRFTBE ((uint32)0xFFFFFFEF) -#define ACR_PRFTBE_ENABLE ((uint32)0x00000010) /* FLASH Prefetch Buffer Enable */ -#define ACR_PRFTBE_DISABLE ((uint32)0x00000000) /* FLASH Prefetch Buffer Disable */ +/* flash registers */ +#define FLASH_BASE 0x40022000 +#define FLASH_ACR FLASH_BASE + +/* flash prefetcher */ +#define ACR_PRFTBE BIT(4) +#define ACR_PRFTBE_ENABLE BIT(4) -#define ACR_LATENCY ((uint32)0x00000038) -#define ACR_LATENCY_0 ((uint32)0x00000000) /* FLASH Zero Latency cycle */ -#define ACR_LATENCY_1 ((uint32)0x00000001) /* FLASH One Latency cycle */ -#define ACR_LATENCY_2 ((uint32)0x00000002) /* FLASH Two Latency cycles */ +/* flash wait states */ +#define ACR_LATENCY (0x7) +#define FLASH_WRITE_ACR(val) __write(FLASH_ACR, val) +#define FLASH_READ_ACR() __read(FLASH_ACR) + +/** + * @brief turn on the hardware prefetcher + */ void flash_enable_prefetch(void) { - uint32 acr = __read(FLASH_ACR); + uint32 val = FLASH_READ_ACR(); - acr &= ACR_PRFTBE; - acr |= ACR_PRFTBE_ENABLE; + val |= ACR_PRFTBE_ENABLE; - __write(FLASH_ACR, acr); + FLASH_WRITE_ACR(val); } -void flash_set_latency(void) { - uint32 acr = __read(FLASH_ACR); - acr &= ACR_LATENCY; - acr |= ACR_LATENCY_2; +/** + * @brief set flash wait states + * @param number of wait states + */ +void flash_set_latency(uint32 wait_states) { + uint32 val = FLASH_READ_ACR(); + + val &= ~ACR_LATENCY; + val |= wait_states; - __write(FLASH_ACR, acr); + FLASH_WRITE_ACR(val); } diff --git a/libmaple/flash.h b/libmaple/flash.h index dca5984..a1ae0a4 100644 --- a/libmaple/flash.h +++ b/libmaple/flash.h @@ -24,17 +24,26 @@ /** - * @brief + * @brief basic stm32 flash setup routines */ #ifndef _FLASH_H_ #define _FLASH_H_ -#define FLASH_BASE 0x40022000 -#define FLASH_ACR FLASH_BASE +#define FLASH_WAIT_STATE_0 0x0 +#define FLASH_WAIT_STATE_1 0x1 +#define FLASH_WAIT_STATE_2 0x2 + +#ifdef __cplusplus +extern "C"{ +#endif void flash_enable_prefetch(void); -void flash_set_latency(void); +void flash_set_latency(uint32 wait_states); + +#ifdef __cplusplus +} +#endif #endif diff --git a/libmaple/rcc.c b/libmaple/rcc.c index 079c4d6..bb423b9 100644 --- a/libmaple/rcc.c +++ b/libmaple/rcc.c @@ -116,14 +116,6 @@ static void hse_init(void) { void rcc_init(void) { hse_init(); - - /* Leave this here for now... */ - /* Enable Prefetch Buffer */ - flash_enable_prefetch(); - - /* Flash 2 wait state */ - flash_set_latency(); - set_ahb_prescaler(SYSCLK_DIV_1); set_apb1_prescaler(HCLK_DIV_2); set_apb2_prescaler(HCLK_DIV_1); |