Skip to content

Commit 5bf11c2

Browse files
str4t0mcfriedt
authored andcommitted
drivers: flash: stm32g0: preparation for dual bank handling
This commit makes no functional changes, it only refactors the driver such that dual bank flash handling can be easily added. Instead of using HAL macros directly in the code, new macros with STM32G0 prefix are defined. The erase_page function gets passed the offset instead of the page, and the FLASH CR reg is written once with all erase parameters. flash_stm32_wait_flash_idle is already called before each write to CR, consequently it is also made sure CFGBSY flag is not set. Signed-off-by: Thomas Stranger <[email protected]>
1 parent 4c862c1 commit 5bf11c2

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

drivers/flash/flash_stm32g0x.c

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ LOG_MODULE_REGISTER(LOG_DOMAIN);
2121
#include "flash_stm32.h"
2222

2323

24-
#define STM32G0X_PAGE_SHIFT 11
24+
#define STM32G0_BANK_COUNT 1
2525

26+
#define STM32G0_FLASH_SIZE (FLASH_SIZE)
27+
#define STM32G0_FLASH_PAGE_SIZE (FLASH_PAGE_SIZE)
28+
#define STM32G0_PAGES_PER_BANK \
29+
((STM32G0_FLASH_SIZE / STM32G0_FLASH_PAGE_SIZE) / STM32G0_BANK_COUNT)
2630

2731
/*
2832
* offset and len must be aligned on 8 for write,
@@ -36,14 +40,6 @@ bool flash_stm32_valid_range(const struct device *dev, off_t offset,
3640
flash_stm32_range_exists(dev, offset, len);
3741
}
3842

39-
/*
40-
* STM32G0xx devices can have up to 64 2K pages
41-
*/
42-
static unsigned int get_page(off_t offset)
43-
{
44-
return offset >> STM32G0X_PAGE_SHIFT;
45-
}
46-
4743
static inline void flush_cache(FLASH_TypeDef *regs)
4844
{
4945
if (regs->ACR & FLASH_ACR_ICEN) {
@@ -101,11 +97,12 @@ static int write_dword(const struct device *dev, off_t offset, uint64_t val)
10197
return rc;
10298
}
10399

104-
static int erase_page(const struct device *dev, unsigned int page)
100+
static int erase_page(const struct device *dev, unsigned int offset)
105101
{
106102
FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
107103
uint32_t tmp;
108104
int rc;
105+
int page;
109106

110107
/* if the control register is locked, do not fail silently */
111108
if (regs->CR & FLASH_CR_LOCK) {
@@ -125,16 +122,17 @@ static int erase_page(const struct device *dev, unsigned int page)
125122
*/
126123
flush_cache(regs);
127124

125+
tmp = regs->CR;
126+
page = offset / STM32G0_FLASH_PAGE_SIZE;
127+
128128
/* Set the PER bit and select the page you wish to erase */
129-
regs->CR |= FLASH_CR_PER;
130-
regs->CR &= ~FLASH_CR_PNB_Msk;
131-
regs->CR |= ((page % 256) << 3);
129+
tmp |= FLASH_CR_PER;
130+
tmp &= ~FLASH_CR_PNB_Msk;
131+
tmp |= ((page << FLASH_CR_PNB_Pos) & FLASH_CR_PNB_Msk);
132132

133-
/* Set the STRT bit */
134-
regs->CR |= FLASH_CR_STRT;
135-
136-
/* flush the register write */
137-
tmp = regs->CR;
133+
/* Set the STRT bit and write the reg */
134+
tmp |= FLASH_CR_STRT;
135+
regs->CR = tmp;
138136

139137
/* Wait for the BSY bit */
140138
rc = flash_stm32_wait_flash_idle(dev);
@@ -148,11 +146,11 @@ int flash_stm32_block_erase_loop(const struct device *dev,
148146
unsigned int offset,
149147
unsigned int len)
150148
{
151-
int i, rc = 0;
149+
unsigned int addr = offset;
150+
int rc = 0;
152151

153-
i = get_page(offset);
154-
for (; i <= get_page(offset + len - 1) ; ++i) {
155-
rc = erase_page(dev, i);
152+
for (; addr <= offset + len - 1 ; addr += STM32G0_FLASH_PAGE_SIZE) {
153+
rc = erase_page(dev, addr);
156154
if (rc < 0) {
157155
break;
158156
}
@@ -188,8 +186,9 @@ void flash_stm32_page_layout(const struct device *dev,
188186
ARG_UNUSED(dev);
189187

190188
if (stm32g0_flash_layout.pages_count == 0) {
191-
stm32g0_flash_layout.pages_count = FLASH_SIZE / FLASH_PAGE_SIZE;
192-
stm32g0_flash_layout.pages_size = FLASH_PAGE_SIZE;
189+
stm32g0_flash_layout.pages_count =
190+
STM32G0_FLASH_SIZE / STM32G0_FLASH_PAGE_SIZE;
191+
stm32g0_flash_layout.pages_size = STM32G0_FLASH_PAGE_SIZE;
193192
}
194193

195194
*layout = &stm32g0_flash_layout;

0 commit comments

Comments
 (0)