diff options
author | Perry Hung <iperry@alum.mit.edu> | 2010-08-04 04:17:16 -0400 |
---|---|---|
committer | Perry Hung <iperry@alum.mit.edu> | 2010-08-04 04:17:16 -0400 |
commit | 2bb8c3fbe39ad12bc4669d499228961ad25e0ace (patch) | |
tree | 8037e64219d4a7b875c70baf2d5e0697c194cf5f | |
parent | d0e353ca9f3a0986c54beab3948117bdaade700e (diff) | |
download | librambutan-2bb8c3fbe39ad12bc4669d499228961ad25e0ace.tar.gz librambutan-2bb8c3fbe39ad12bc4669d499228961ad25e0ace.zip |
Basic flash peripheral management api for board bringup
-rw-r--r-- | libmaple/flash.c | 49 | ||||
-rw-r--r-- | libmaple/flash.h | 17 | ||||
-rw-r--r-- | libmaple/rcc.c | 8 | ||||
-rw-r--r-- | wirish/wirish.c | 6 |
4 files changed, 52 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); diff --git a/wirish/wirish.c b/wirish/wirish.c index e21f792..e166455 100644 --- a/wirish/wirish.c +++ b/wirish/wirish.c @@ -32,9 +32,15 @@ #include "gpio.h" #include "nvic.h" #include "usb.h" +#include "flash.h" void init(void) { + /* make sure the flash is ready before spinning the high speed clock up */ + flash_enable_prefetch(); + flash_set_latency(FLASH_WAIT_STATE_2); + rcc_init(); + nvic_init(); systick_init(); gpio_init(); |