diff --git a/include/libopencm3/stm32/common/flash_common_f234.h b/include/libopencm3/stm32/common/flash_common_f234.h index 8657dd91..1f5ce3f1 100644 --- a/include/libopencm3/stm32/common/flash_common_f234.h +++ b/include/libopencm3/stm32/common/flash_common_f234.h @@ -28,8 +28,8 @@ /** @cond */ #ifdef LIBOPENCM3_FLASH_H /** @endcond */ -#ifndef LIBOPENCM3_FLASH_COMMON_F24_H -#define LIBOPENCM3_FLASH_COMMON_F24_H +#ifndef LIBOPENCM3_FLASH_COMMON_F234_H +#define LIBOPENCM3_FLASH_COMMON_F234_H #include @@ -40,27 +40,9 @@ #define FLASH_OPTKEYR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x08) #define FLASH_SR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x0C) #define FLASH_CR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x10) -#if !defined(STM32F3) -#define FLASH_OPTCR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x14) -#else -#define FLASH_AR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x14) -#define FLASH_OBR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x1C) -#define FLASH_WRPR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x20) -#endif /* --- FLASH_ACR values ---------------------------------------------------- */ -#if !defined(STM32F3) -#define FLASH_ACR_DCRST (1 << 12) -#define FLASH_ACR_ICRST (1 << 11) -#define FLASH_ACR_DCE (1 << 10) -#define FLASH_ACR_ICE (1 << 9) -#define FLASH_ACR_PRFTEN (1 << 8) -#else -#define FLASH_ACR_PRFTBS (1 << 5) -#define FLASH_ACR_PRFTBE (1 << 4) -#define FLASH_ACR_HLFCYA (1 << 3) -#endif #define FLASH_ACR_LATENCY_0WS 0x00 #define FLASH_ACR_LATENCY_1WS 0x01 #define FLASH_ACR_LATENCY_2WS 0x02 @@ -72,86 +54,12 @@ /* --- FLASH_SR values ----------------------------------------------------- */ -#if !defined(STM32F3) -#define FLASH_SR_BSY (1 << 16) -#define FLASH_SR_PGSERR (1 << 7) -#define FLASH_SR_PGPERR (1 << 6) -#define FLASH_SR_PGAERR (1 << 5) -#define FLASH_SR_WRPERR (1 << 4) -#define FLASH_SR_OPERR (1 << 1) -#define FLASH_SR_EOP (1 << 0) -#else -#define FLASH_SR_BSY (1 << 0) -#define FLASH_SR_ERLYBSY (1 << 1) -#define FLASH_SR_PGPERR (1 << 2) -#define FLASH_SR_WRPRTERR (1 << 4) -#define FLASH_SR_EOP (1 << 5) -#endif - /* --- FLASH_CR values ----------------------------------------------------- */ -#if !defined(STM32F3) -#define FLASH_CR_LOCK (1 << 31) -#define FLASH_CR_ERRIE (1 << 25) -#define FLASH_CR_EOPIE (1 << 24) -#define FLASH_CR_STRT (1 << 16) -#define FLASH_CR_MER (1 << 2) -#define FLASH_CR_SER (1 << 1) -#define FLASH_CR_PG (1 << 0) -#define FLASH_CR_SECTOR_0 (0x00 << 3) -#define FLASH_CR_SECTOR_1 (0x01 << 3) -#define FLASH_CR_SECTOR_2 (0x02 << 3) -#define FLASH_CR_SECTOR_3 (0x03 << 3) -#define FLASH_CR_SECTOR_4 (0x04 << 3) -#define FLASH_CR_SECTOR_5 (0x05 << 3) -#define FLASH_CR_SECTOR_6 (0x06 << 3) -#define FLASH_CR_SECTOR_7 (0x07 << 3) -#define FLASH_CR_SECTOR_8 (0x08 << 3) -#define FLASH_CR_SECTOR_9 (0x09 << 3) -#define FLASH_CR_SECTOR_10 (0x0a << 3) -#define FLASH_CR_SECTOR_11 (0x0b << 3) -#define FLASH_CR_PROGRAM_X8 (0x00 << 8) -#define FLASH_CR_PROGRAM_X16 (0x01 << 8) -#define FLASH_CR_PROGRAM_X32 (0x02 << 8) -#define FLASH_CR_PROGRAM_X64 (0x03 << 8) -#else -#define FLASH_CR_OBL_LAUNCH (1 << 13) -#define FLASH_CR_EOPIE (1 << 12) -#define FLASH_CR_ERRIE (1 << 10) -#define FLASH_CR_OPTWRE (1 << 9) -#define FLASH_CR_LOCK (1 << 7) -#define FLASH_CR_STRT (1 << 6) -#define FLASH_CR_OPTER (1 << 5) -#define FLASH_CR_OPTPG (1 << 4) -#define FLASH_CR_MER (1 << 2) -#define FLASH_CR_PER (1 << 1) -#define FLASH_CR_PG (1 << 0) -#endif - -#if !defined(STM32F3) -/* --- FLASH_OPTCR values -------------------------------------------------- */ - -/* FLASH_OPTCR[27:16]: nWRP */ -/* FLASH_OBR[15:8]: RDP */ -#define FLASH_OPTCR_NRST_STDBY (1 << 7) -#define FLASH_OPTCR_NRST_STOP (1 << 6) -#define FLASH_OPTCR_WDG_SW (1 << 5) -#define FLASH_OPTCR_OPTSTRT (1 << 1) -#define FLASH_OPTCR_OPTLOCK (1 << 0) -#define FLASH_OPTCR_BOR_LEVEL_3 (0x00 << 2) -#define FLASH_OPTCR_BOR_LEVEL_2 (0x01 << 2) -#define FLASH_OPTCR_BOR_LEVEL_1 (0x02 << 2) -#define FLASH_OPTCR_BOR_OFF (0x03 << 2) -#endif - /* --- FLASH Keys -----------------------------------------------------------*/ #define FLASH_KEYR_KEY1 ((uint32_t)0x45670123) #define FLASH_KEYR_KEY2 ((uint32_t)0xcdef89ab) -#if !defined(STM32F3) -#define FLASH_OPTKEYR_KEY1 ((uint32_t)0x08192a3b) -#define FLASH_OPTKEYR_KEY2 ((uint32_t)0x4c5d6e7f) -#endif /* --- Function prototypes ------------------------------------------------- */ @@ -165,36 +73,13 @@ void flash_clear_eop_flag(void); void flash_clear_bsy_flag(void); void flash_clear_status_flags(void); void flash_wait_for_last_operation(void); -#if !defined(STM32F3) -void flash_unlock_option_bytes(void); -void flash_lock_option_bytes(void); -void flash_clear_pgserr_flag(void); -void flash_clear_wrperr_flag(void); -void flash_clear_pgaerr_flag(void); -void flash_dcache_enable(void); -void flash_dcache_disable(void); -void flash_icache_enable(void); -void flash_icache_disable(void); -void flash_prefetch_enable(void); -void flash_prefetch_disable(void); -void flash_dcache_reset(void); -void flash_icache_reset(void); -void flash_erase_all_sectors(uint32_t program_size); -void flash_erase_sector(uint8_t sector, uint32_t program_size); -void flash_program_double_word(uint32_t address, uint64_t data); -void flash_program_word(uint32_t address, uint32_t data); -void flash_program_half_word(uint32_t address, uint16_t data); -void flash_program_byte(uint32_t address, uint8_t data); -void flash_program(uint32_t address, uint8_t *data, uint32_t len); -void flash_program_option_bytes(uint32_t data); -#endif END_DECLS #endif /** @cond */ #else -#warning "flash_common_f24.h should not be included direcitly, only via flash.h" +#warning "flash_common_f234.h should not be included direcitly," +#warning "only via flash.h" #endif /** @endcond */ - diff --git a/include/libopencm3/stm32/common/flash_common_f24.h b/include/libopencm3/stm32/common/flash_common_f24.h new file mode 100644 index 00000000..67c2fbd5 --- /dev/null +++ b/include/libopencm3/stm32/common/flash_common_f24.h @@ -0,0 +1,139 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * Copyright (C) 2010 Mark Butler + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* + * For details see: + * PM0081 Programming manual: STM32F40xxx and STM32F41xxx Flash programming + * September 2011, Doc ID 018520 Rev 1 + * https://github.com/libopencm3/libopencm3-archive/blob/master/st_micro/DM00023388.pdf + */ + +/** @cond */ +#ifdef LIBOPENCM3_FLASH_H +/** @endcond */ +#ifndef LIBOPENCM3_FLASH_COMMON_F24_H +#define LIBOPENCM3_FLASH_COMMON_F24_H + +#include +#include + +/* --- FLASH registers ----------------------------------------------------- */ + +#define FLASH_OPTCR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x14) + +/* --- FLASH_ACR values ---------------------------------------------------- */ + +#define FLASH_ACR_DCRST (1 << 12) +#define FLASH_ACR_ICRST (1 << 11) +#define FLASH_ACR_DCE (1 << 10) +#define FLASH_ACR_ICE (1 << 9) +#define FLASH_ACR_PRFTEN (1 << 8) + +/* --- FLASH_SR values ----------------------------------------------------- */ + +#define FLASH_SR_BSY (1 << 16) +#define FLASH_SR_PGSERR (1 << 7) +#define FLASH_SR_PGPERR (1 << 6) +#define FLASH_SR_PGAERR (1 << 5) +#define FLASH_SR_WRPERR (1 << 4) +#define FLASH_SR_OPERR (1 << 1) +#define FLASH_SR_EOP (1 << 0) + +/* --- FLASH_CR values ----------------------------------------------------- */ + +#define FLASH_CR_LOCK (1 << 31) +#define FLASH_CR_ERRIE (1 << 25) +#define FLASH_CR_EOPIE (1 << 24) +#define FLASH_CR_STRT (1 << 16) +#define FLASH_CR_MER (1 << 2) +#define FLASH_CR_SER (1 << 1) +#define FLASH_CR_PG (1 << 0) +#define FLASH_CR_SECTOR_0 (0x00 << 3) +#define FLASH_CR_SECTOR_1 (0x01 << 3) +#define FLASH_CR_SECTOR_2 (0x02 << 3) +#define FLASH_CR_SECTOR_3 (0x03 << 3) +#define FLASH_CR_SECTOR_4 (0x04 << 3) +#define FLASH_CR_SECTOR_5 (0x05 << 3) +#define FLASH_CR_SECTOR_6 (0x06 << 3) +#define FLASH_CR_SECTOR_7 (0x07 << 3) +#define FLASH_CR_SECTOR_8 (0x08 << 3) +#define FLASH_CR_SECTOR_9 (0x09 << 3) +#define FLASH_CR_SECTOR_10 (0x0a << 3) +#define FLASH_CR_SECTOR_11 (0x0b << 3) +#define FLASH_CR_PROGRAM_X8 (0x00 << 8) +#define FLASH_CR_PROGRAM_X16 (0x01 << 8) +#define FLASH_CR_PROGRAM_X32 (0x02 << 8) +#define FLASH_CR_PROGRAM_X64 (0x03 << 8) + +/* --- FLASH_OPTCR values -------------------------------------------------- */ + +/* FLASH_OPTCR[27:16]: nWRP */ +/* FLASH_OBR[15:8]: RDP */ +#define FLASH_OPTCR_NRST_STDBY (1 << 7) +#define FLASH_OPTCR_NRST_STOP (1 << 6) +#define FLASH_OPTCR_WDG_SW (1 << 5) +#define FLASH_OPTCR_OPTSTRT (1 << 1) +#define FLASH_OPTCR_OPTLOCK (1 << 0) +#define FLASH_OPTCR_BOR_LEVEL_3 (0x00 << 2) +#define FLASH_OPTCR_BOR_LEVEL_2 (0x01 << 2) +#define FLASH_OPTCR_BOR_LEVEL_1 (0x02 << 2) +#define FLASH_OPTCR_BOR_OFF (0x03 << 2) + +/* --- FLASH Keys -----------------------------------------------------------*/ + +#define FLASH_OPTKEYR_KEY1 ((uint32_t)0x08192a3b) +#define FLASH_OPTKEYR_KEY2 ((uint32_t)0x4c5d6e7f) + +/* --- Function prototypes ------------------------------------------------- */ + +BEGIN_DECLS + +void flash_unlock_option_bytes(void); +void flash_lock_option_bytes(void); +void flash_clear_pgserr_flag(void); +void flash_clear_wrperr_flag(void); +void flash_clear_pgaerr_flag(void); +void flash_dcache_enable(void); +void flash_dcache_disable(void); +void flash_icache_enable(void); +void flash_icache_disable(void); +void flash_prefetch_enable(void); +void flash_prefetch_disable(void); +void flash_dcache_reset(void); +void flash_icache_reset(void); +void flash_erase_all_sectors(uint32_t program_size); +void flash_erase_sector(uint8_t sector, uint32_t program_size); +void flash_program_double_word(uint32_t address, uint64_t data); +void flash_program_word(uint32_t address, uint32_t data); +void flash_program_half_word(uint32_t address, uint16_t data); +void flash_program_byte(uint32_t address, uint8_t data); +void flash_program(uint32_t address, uint8_t *data, uint32_t len); +void flash_program_option_bytes(uint32_t data); + +END_DECLS + +#endif +/** @cond */ +#else +#warning "flash_common_f24.h should not be included direcitly," +#warning "only via flash.h" +#endif +/** @endcond */ + diff --git a/include/libopencm3/stm32/f2/flash.h b/include/libopencm3/stm32/f2/flash.h index bffb0325..ea0b8bc9 100644 --- a/include/libopencm3/stm32/f2/flash.h +++ b/include/libopencm3/stm32/f2/flash.h @@ -19,7 +19,7 @@ #define LIBOPENCM3_FLASH_H #include -#include +#include #endif diff --git a/include/libopencm3/stm32/f3/flash.h b/include/libopencm3/stm32/f3/flash.h index bffb0325..cbb6ec1c 100644 --- a/include/libopencm3/stm32/f3/flash.h +++ b/include/libopencm3/stm32/f3/flash.h @@ -21,5 +21,39 @@ #include #include +/* --- FLASH registers ----------------------------------------------------- */ + +#define FLASH_AR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x14) +#define FLASH_OBR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x1C) +#define FLASH_WRPR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x20) + +/* --- FLASH_ACR values ---------------------------------------------------- */ + +#define FLASH_ACR_PRFTBS (1 << 5) +#define FLASH_ACR_PRFTBE (1 << 4) +#define FLASH_ACR_HLFCYA (1 << 3) + +/* --- FLASH_SR values ----------------------------------------------------- */ + +#define FLASH_SR_BSY (1 << 0) +#define FLASH_SR_ERLYBSY (1 << 1) +#define FLASH_SR_PGPERR (1 << 2) +#define FLASH_SR_WRPRTERR (1 << 4) +#define FLASH_SR_EOP (1 << 5) + +/* --- FLASH_CR values ----------------------------------------------------- */ + +#define FLASH_CR_OBL_LAUNCH (1 << 13) +#define FLASH_CR_EOPIE (1 << 12) +#define FLASH_CR_ERRIE (1 << 10) +#define FLASH_CR_OPTWRE (1 << 9) +#define FLASH_CR_LOCK (1 << 7) +#define FLASH_CR_STRT (1 << 6) +#define FLASH_CR_OPTER (1 << 5) +#define FLASH_CR_OPTPG (1 << 4) +#define FLASH_CR_MER (1 << 2) +#define FLASH_CR_PER (1 << 1) +#define FLASH_CR_PG (1 << 0) + #endif diff --git a/include/libopencm3/stm32/f4/flash.h b/include/libopencm3/stm32/f4/flash.h index bffb0325..ea0b8bc9 100644 --- a/include/libopencm3/stm32/f4/flash.h +++ b/include/libopencm3/stm32/f4/flash.h @@ -19,7 +19,7 @@ #define LIBOPENCM3_FLASH_H #include -#include +#include #endif diff --git a/lib/stm32/common/flash_common_f234.c b/lib/stm32/common/flash_common_f234.c index 35079f49..feee4299 100644 --- a/lib/stm32/common/flash_common_f234.c +++ b/lib/stm32/common/flash_common_f234.c @@ -20,71 +20,6 @@ #include -#if !defined(STM32F3) -static inline void flash_set_program_size(uint32_t psize) -{ - FLASH_CR &= ~(((1 << 0) | (1 << 1)) << 8); - FLASH_CR |= psize; -} - -void flash_dcache_enable(void) -{ - FLASH_ACR |= FLASH_ACR_DCE; -} - -void flash_dcache_disable(void) -{ - FLASH_ACR &= ~FLASH_ACR_DCE; -} - -void flash_icache_enable(void) -{ - FLASH_ACR |= FLASH_ACR_ICE; -} - -void flash_icache_disable(void) -{ - FLASH_ACR &= ~FLASH_ACR_ICE; -} - -void flash_prefetch_enable(void) -{ - FLASH_ACR |= FLASH_ACR_PRFTEN; -} - -void flash_prefetch_disable(void) -{ - FLASH_ACR &= ~FLASH_ACR_PRFTEN; -} - -void flash_dcache_reset(void) -{ - FLASH_ACR |= FLASH_ACR_DCRST; -} - -void flash_icache_reset(void) -{ - FLASH_ACR |= FLASH_ACR_ICRST; -} - -void flash_clear_pgserr_flag(void) -{ - FLASH_SR |= FLASH_SR_PGSERR; -} - -void flash_clear_pgaerr_flag(void) -{ - FLASH_SR |= FLASH_SR_PGAERR; -} - -void flash_clear_wrperr_flag(void) -{ - FLASH_SR |= FLASH_SR_WRPERR; -} - - -#endif - void flash_set_ws(uint32_t ws) { uint32_t reg32; @@ -125,155 +60,8 @@ void flash_clear_bsy_flag(void) FLASH_SR &= ~FLASH_SR_BSY; } -void flash_clear_status_flags(void) -{ -#if !defined(STM32F3) - flash_clear_pgserr_flag(); - flash_clear_pgaerr_flag(); - flash_clear_wrperr_flag(); -#endif - flash_clear_pgperr_flag(); - flash_clear_eop_flag(); - flash_clear_bsy_flag(); -} void flash_wait_for_last_operation(void) { while ((FLASH_SR & FLASH_SR_BSY) == FLASH_SR_BSY); } - -#if !defined(STM32F3) - -void flash_unlock_option_bytes(void) -{ - /* Clear the unlock state. */ - FLASH_OPTCR |= FLASH_OPTCR_OPTLOCK; - - /* Unlock option bytes. */ - FLASH_OPTKEYR = FLASH_OPTKEYR_KEY1; - FLASH_OPTKEYR = FLASH_OPTKEYR_KEY2; -} - -void flash_lock_option_bytes(void) -{ - FLASH_OPTCR |= FLASH_OPTCR_OPTLOCK; -} - -void flash_program_double_word(uint32_t address, uint64_t data) -{ - /* Ensure that all flash operations are complete. */ - flash_wait_for_last_operation(); - flash_set_program_size(FLASH_CR_PROGRAM_X64); - - /* Enable writes to flash. */ - FLASH_CR |= FLASH_CR_PG; - - /* Program the first half of the word. */ - MMIO64(address) = data; - - /* Wait for the write to complete. */ - flash_wait_for_last_operation(); - - /* Disable writes to flash. */ - FLASH_CR &= ~FLASH_CR_PG; -} - -void flash_program_word(uint32_t address, uint32_t data) -{ - /* Ensure that all flash operations are complete. */ - flash_wait_for_last_operation(); - flash_set_program_size(FLASH_CR_PROGRAM_X32); - - /* Enable writes to flash. */ - FLASH_CR |= FLASH_CR_PG; - - /* Program the first half of the word. */ - MMIO32(address) = data; - - /* Wait for the write to complete. */ - flash_wait_for_last_operation(); - - /* Disable writes to flash. */ - FLASH_CR &= ~FLASH_CR_PG; -} - -void flash_program_half_word(uint32_t address, uint16_t data) -{ - flash_wait_for_last_operation(); - flash_set_program_size(FLASH_CR_PROGRAM_X16); - - FLASH_CR |= FLASH_CR_PG; - - MMIO16(address) = data; - - flash_wait_for_last_operation(); - - FLASH_CR &= ~FLASH_CR_PG; /* Disable the PG bit. */ -} - -void flash_program_byte(uint32_t address, uint8_t data) -{ - flash_wait_for_last_operation(); - flash_set_program_size(FLASH_CR_PROGRAM_X8); - - FLASH_CR |= FLASH_CR_PG; - - MMIO8(address) = data; - - flash_wait_for_last_operation(); - - FLASH_CR &= ~FLASH_CR_PG; /* Disable the PG bit. */ -} - -void flash_program(uint32_t address, uint8_t *data, uint32_t len) -{ - /* TODO: Use dword and word size program operations where possible for - * turbo speed. - */ - uint32_t i; - for (i = 0; i < len; i++) { - flash_program_byte(address+i, data[i]); - } -} - -void flash_erase_sector(uint8_t sector, uint32_t program_size) -{ - flash_wait_for_last_operation(); - flash_set_program_size(program_size); - - FLASH_CR &= ~(0xF << 3); - FLASH_CR |= (sector << 3) & 0x78; - FLASH_CR |= FLASH_CR_SER; - FLASH_CR |= FLASH_CR_STRT; - - flash_wait_for_last_operation(); - FLASH_CR &= ~FLASH_CR_SER; - FLASH_CR &= ~(0xF << 3); -} - -void flash_erase_all_sectors(uint32_t program_size) -{ - flash_wait_for_last_operation(); - flash_set_program_size(program_size); - - FLASH_CR |= FLASH_CR_MER; /* Enable mass erase. */ - FLASH_CR |= FLASH_CR_STRT; /* Trigger the erase. */ - - flash_wait_for_last_operation(); - FLASH_CR &= ~FLASH_CR_MER; /* Disable mass erase. */ -} - -void flash_program_option_bytes(uint32_t data) -{ - flash_wait_for_last_operation(); - - if (FLASH_OPTCR & FLASH_OPTCR_OPTLOCK) { - flash_unlock_option_bytes(); - } - - FLASH_OPTCR = data & ~0x3; - FLASH_OPTCR |= FLASH_OPTCR_OPTSTRT; /* Enable option byte prog. */ - flash_wait_for_last_operation(); -} - -#endif diff --git a/lib/stm32/common/flash_common_f24.c b/lib/stm32/common/flash_common_f24.c new file mode 100644 index 00000000..6d3aad61 --- /dev/null +++ b/lib/stm32/common/flash_common_f24.c @@ -0,0 +1,224 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * Copyright (C) 2010 Mark Butler + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include + +static inline void flash_set_program_size(uint32_t psize) +{ + FLASH_CR &= ~(((1 << 0) | (1 << 1)) << 8); + FLASH_CR |= psize; +} + +void flash_dcache_enable(void) +{ + FLASH_ACR |= FLASH_ACR_DCE; +} + +void flash_dcache_disable(void) +{ + FLASH_ACR &= ~FLASH_ACR_DCE; +} + +void flash_icache_enable(void) +{ + FLASH_ACR |= FLASH_ACR_ICE; +} + +void flash_icache_disable(void) +{ + FLASH_ACR &= ~FLASH_ACR_ICE; +} + +void flash_prefetch_enable(void) +{ + FLASH_ACR |= FLASH_ACR_PRFTEN; +} + +void flash_prefetch_disable(void) +{ + FLASH_ACR &= ~FLASH_ACR_PRFTEN; +} + +void flash_dcache_reset(void) +{ + FLASH_ACR |= FLASH_ACR_DCRST; +} + +void flash_icache_reset(void) +{ + FLASH_ACR |= FLASH_ACR_ICRST; +} + +void flash_clear_pgserr_flag(void) +{ + FLASH_SR |= FLASH_SR_PGSERR; +} + +void flash_clear_pgaerr_flag(void) +{ + FLASH_SR |= FLASH_SR_PGAERR; +} + +void flash_clear_wrperr_flag(void) +{ + FLASH_SR |= FLASH_SR_WRPERR; +} + +void flash_clear_status_flags(void) +{ + flash_clear_pgserr_flag(); + flash_clear_pgaerr_flag(); + flash_clear_wrperr_flag(); + flash_clear_pgperr_flag(); + flash_clear_eop_flag(); + flash_clear_bsy_flag(); +} + +void flash_unlock_option_bytes(void) +{ + /* Clear the unlock state. */ + FLASH_OPTCR |= FLASH_OPTCR_OPTLOCK; + + /* Unlock option bytes. */ + FLASH_OPTKEYR = FLASH_OPTKEYR_KEY1; + FLASH_OPTKEYR = FLASH_OPTKEYR_KEY2; +} + +void flash_lock_option_bytes(void) +{ + FLASH_OPTCR |= FLASH_OPTCR_OPTLOCK; +} + +void flash_program_double_word(uint32_t address, uint64_t data) +{ + /* Ensure that all flash operations are complete. */ + flash_wait_for_last_operation(); + flash_set_program_size(FLASH_CR_PROGRAM_X64); + + /* Enable writes to flash. */ + FLASH_CR |= FLASH_CR_PG; + + /* Program the first half of the word. */ + MMIO64(address) = data; + + /* Wait for the write to complete. */ + flash_wait_for_last_operation(); + + /* Disable writes to flash. */ + FLASH_CR &= ~FLASH_CR_PG; +} + +void flash_program_word(uint32_t address, uint32_t data) +{ + /* Ensure that all flash operations are complete. */ + flash_wait_for_last_operation(); + flash_set_program_size(FLASH_CR_PROGRAM_X32); + + /* Enable writes to flash. */ + FLASH_CR |= FLASH_CR_PG; + + /* Program the first half of the word. */ + MMIO32(address) = data; + + /* Wait for the write to complete. */ + flash_wait_for_last_operation(); + + /* Disable writes to flash. */ + FLASH_CR &= ~FLASH_CR_PG; +} + +void flash_program_half_word(uint32_t address, uint16_t data) +{ + flash_wait_for_last_operation(); + flash_set_program_size(FLASH_CR_PROGRAM_X16); + + FLASH_CR |= FLASH_CR_PG; + + MMIO16(address) = data; + + flash_wait_for_last_operation(); + + FLASH_CR &= ~FLASH_CR_PG; /* Disable the PG bit. */ +} + +void flash_program_byte(uint32_t address, uint8_t data) +{ + flash_wait_for_last_operation(); + flash_set_program_size(FLASH_CR_PROGRAM_X8); + + FLASH_CR |= FLASH_CR_PG; + + MMIO8(address) = data; + + flash_wait_for_last_operation(); + + FLASH_CR &= ~FLASH_CR_PG; /* Disable the PG bit. */ +} + +void flash_program(uint32_t address, uint8_t *data, uint32_t len) +{ + /* TODO: Use dword and word size program operations where possible for + * turbo speed. + */ + uint32_t i; + for (i = 0; i < len; i++) { + flash_program_byte(address+i, data[i]); + } +} + +void flash_erase_sector(uint8_t sector, uint32_t program_size) +{ + flash_wait_for_last_operation(); + flash_set_program_size(program_size); + + FLASH_CR &= ~(0xF << 3); + FLASH_CR |= (sector << 3) & 0x78; + FLASH_CR |= FLASH_CR_SER; + FLASH_CR |= FLASH_CR_STRT; + + flash_wait_for_last_operation(); + FLASH_CR &= ~FLASH_CR_SER; + FLASH_CR &= ~(0xF << 3); +} + +void flash_erase_all_sectors(uint32_t program_size) +{ + flash_wait_for_last_operation(); + flash_set_program_size(program_size); + + FLASH_CR |= FLASH_CR_MER; /* Enable mass erase. */ + FLASH_CR |= FLASH_CR_STRT; /* Trigger the erase. */ + + flash_wait_for_last_operation(); + FLASH_CR &= ~FLASH_CR_MER; /* Disable mass erase. */ +} + +void flash_program_option_bytes(uint32_t data) +{ + flash_wait_for_last_operation(); + + if (FLASH_OPTCR & FLASH_OPTCR_OPTLOCK) { + flash_unlock_option_bytes(); + } + + FLASH_OPTCR = data & ~0x3; + FLASH_OPTCR |= FLASH_OPTCR_OPTSTRT; /* Enable option byte prog. */ + flash_wait_for_last_operation(); +} diff --git a/lib/stm32/f2/Makefile b/lib/stm32/f2/Makefile index adbc24b4..725b9427 100644 --- a/lib/stm32/f2/Makefile +++ b/lib/stm32/f2/Makefile @@ -39,7 +39,8 @@ OBJS += crc_common_all.o dac_common_all.o dma_common_f24.o \ gpio_common_all.o gpio_common_f234.o i2c_common_all.o \ iwdg_common_all.o rtc_common_bcd.o spi_common_all.o \ timer_common_all.o timer_common_f24.o usart_common_all.o \ - flash_common_f234.o hash_common_f24.o crypto_common_f24.o + flash_common_f234.o flash_common_f24.o hash_common_f24.o \ + crypto_common_f24.o VPATH += ../../usb:../:../../cm3:../common diff --git a/lib/stm32/f3/Makefile b/lib/stm32/f3/Makefile index aa3304dc..e0f85a58 100644 --- a/lib/stm32/f3/Makefile +++ b/lib/stm32/f3/Makefile @@ -39,7 +39,8 @@ OBJS = rcc.o gpio.o adc.o exti2.o i2c.o OBJS += gpio_common_all.o gpio_common_f234.o \ dac_common_all.o usart_common_all.o crc_common_all.o\ iwdg_common_all.o spi_common_all.o dma_common_f13.o\ - timer_common_all.o timer_common_f24.o flash_common_f234.o + timer_common_all.o timer_common_f24.o flash_common_f234.o \ + flash.o OBJS += usb.o usb_control.o usb_standard.o usb_f103.o diff --git a/lib/stm32/f3/flash.c b/lib/stm32/f3/flash.c new file mode 100644 index 00000000..c2d25d2d --- /dev/null +++ b/lib/stm32/f3/flash.c @@ -0,0 +1,28 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * Copyright (C) 2010 Mark Butler + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include + +void flash_clear_status_flags(void) +{ + flash_clear_pgperr_flag(); + flash_clear_eop_flag(); + flash_clear_bsy_flag(); +} diff --git a/lib/stm32/f4/Makefile b/lib/stm32/f4/Makefile index 52a354e5..f47c2c8c 100644 --- a/lib/stm32/f4/Makefile +++ b/lib/stm32/f4/Makefile @@ -40,8 +40,8 @@ OBJS += crc_common_all.o dac_common_all.o dma_common_f24.o \ gpio_common_all.o gpio_common_f234.o i2c_common_all.o \ iwdg_common_all.o pwr_common_all.o rtc_common_bcd.o \ spi_common_all.o timer_common_all.o timer_common_f24.o \ - usart_common_all.o flash_common_f234.o hash_common_f24.o \ - crypto_common_f24.o + usart_common_all.o flash_common_f234.o flash_common_f24.o \ + hash_common_f24.o crypto_common_f24.o OBJS += usb.o usb_standard.o usb_control.o usb_fx07_common.o \ usb_f107.o usb_f207.o