Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/thirdpin/libopencm3.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKarl Palsson <karlp@tweak.net.au>2018-07-26 02:37:12 +0300
committerKarl Palsson <karlp@tweak.net.au>2018-08-17 03:15:01 +0300
commitb8ede60d9d2c54a90b4065fdd9a2cca655495fba (patch)
tree03c9f7543979d7a1371b584af6b98dc3a61de152
parent3293913be24735ad5e59a5c82f95b1211729c3dd (diff)
stm32f3: flash: support basic write/erase operations
Originally filed as https://github.com/libopencm3/libopencm3/pull/627
-rw-r--r--include/libopencm3/stm32/f3/flash.h3
-rw-r--r--lib/stm32/f3/flash.c46
2 files changed, 48 insertions, 1 deletions
diff --git a/include/libopencm3/stm32/f3/flash.h b/include/libopencm3/stm32/f3/flash.h
index ca561efb..a978ebf7 100644
--- a/include/libopencm3/stm32/f3/flash.h
+++ b/include/libopencm3/stm32/f3/flash.h
@@ -99,6 +99,9 @@ BEGIN_DECLS
void flash_clear_pgerr_flag(void);
void flash_clear_wrprterr_flag(void);
+void flash_program_half_word(uint32_t address, uint16_t data);
+void flash_erase_page(uint32_t page_address);
+void flash_erase_all_pages(void);
END_DECLS
diff --git a/lib/stm32/f3/flash.c b/lib/stm32/f3/flash.c
index ffd03c30..da8dfb68 100644
--- a/lib/stm32/f3/flash.c
+++ b/lib/stm32/f3/flash.c
@@ -46,6 +46,11 @@
#include <libopencm3/stm32/flash.h>
+void flash_wait_for_last_operation(void)
+{
+ while ((FLASH_SR & FLASH_SR_BSY) == FLASH_SR_BSY);
+}
+
void flash_clear_pgerr_flag(void)
{
FLASH_SR |= FLASH_SR_PGERR;
@@ -57,10 +62,11 @@ void flash_clear_wrprterr_flag(void)
}
/*---------------------------------------------------------------------------*/
+
/** @brief Clear All Status Flags
Clears program error, end of operation, busy flags.
-*/
+ */
void flash_clear_status_flags(void)
{
@@ -69,5 +75,43 @@ void flash_clear_status_flags(void)
flash_clear_eop_flag();
}
+void flash_program_half_word(uint32_t address, uint16_t data)
+{
+ flash_wait_for_last_operation();
+
+ FLASH_CR |= FLASH_CR_PG;
+
+ MMIO16(address) = data;
+
+ flash_wait_for_last_operation();
+
+ FLASH_CR &= ~FLASH_CR_PG;
+}
+
+void flash_erase_page(uint32_t page_address)
+{
+ flash_wait_for_last_operation();
+
+ FLASH_CR |= FLASH_CR_PER;
+ FLASH_AR = page_address;
+ FLASH_CR |= FLASH_CR_STRT;
+
+ flash_wait_for_last_operation();
+
+ FLASH_CR &= ~FLASH_CR_PER;
+}
+
+void flash_erase_all_pages(void)
+{
+ flash_wait_for_last_operation();
+
+ FLASH_CR |= FLASH_CR_MER;
+ FLASH_CR |= FLASH_CR_STRT;
+
+ flash_wait_for_last_operation();
+ FLASH_CR &= ~FLASH_CR_MER;
+}
+
+
/**@}*/