Skip to content

Commit 4830c7c

Browse files
committed
drivers: flash: stm32 flash base address from the DTS node
For the flash driver, the base address is the MCU internal flash address (usualyy 0x8000000). This PR gets the that address from the device tree node "st,stm32-nv-flash" instead of relying on the CONFIG_FLASH_BASE_ADDRESS which might differ when building for another flash memory. Signed-off-by: Francois Ramu <[email protected]>
1 parent 4019d17 commit 4830c7c

14 files changed

+24
-20
lines changed

drivers/flash/flash_stm32.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ static void flash_stm32_flush_caches(const struct device *dev,
157157
regs->ACR |= FLASH_ACR_DCEN;
158158
}
159159
#elif defined(CONFIG_SOC_SERIES_STM32F7X)
160-
SCB_InvalidateDCache_by_Addr((uint32_t *)(CONFIG_FLASH_BASE_ADDRESS
160+
SCB_InvalidateDCache_by_Addr((uint32_t *)(FLASH_STM32_BASE_ADDRESS
161161
+ offset), len);
162162
#endif
163163
}
@@ -178,7 +178,7 @@ static int flash_stm32_read(const struct device *dev, off_t offset,
178178

179179
LOG_DBG("Read offset: %ld, len: %zu", (long int) offset, len);
180180

181-
memcpy(data, (uint8_t *) CONFIG_FLASH_BASE_ADDRESS + offset, len);
181+
memcpy(data, (uint8_t *) FLASH_STM32_BASE_ADDRESS + offset, len);
182182

183183
return 0;
184184
}
@@ -562,7 +562,8 @@ static int stm32_flash_init(const struct device *dev)
562562

563563
flash_stm32_sem_init(dev);
564564

565-
LOG_DBG("Flash initialized. BS: %zu",
565+
LOG_DBG("Flash @0x%x initialized. BS: %zu",
566+
FLASH_STM32_BASE_ADDRESS,
566567
flash_stm32_parameters.write_block_size);
567568

568569
/* Check Flash configuration */

drivers/flash/flash_stm32.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#include <zephyr/drivers/clock_control/stm32_clock_control.h>
1818
#endif
1919

20+
/* Get the base address of the flash from the DTS node */
21+
#define FLASH_STM32_BASE_ADDRESS DT_REG_ADDR(DT_INST(0, st_stm32_nv_flash))
22+
2023
struct flash_stm32_priv {
2124
FLASH_TypeDef *regs;
2225
#if DT_NODE_HAS_PROP(DT_INST(0, st_stm32_flash_controller), clocks) || \

drivers/flash/flash_stm32f1x.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static void erase_page_begin(FLASH_TypeDef *regs, unsigned int page)
6262
{
6363
/* Set the PER bit and select the page you wish to erase */
6464
regs->CR |= FLASH_CR_PER;
65-
regs->AR = CONFIG_FLASH_BASE_ADDRESS + page * FLASH_PAGE_SIZE;
65+
regs->AR = FLASH_STM32_BASE_ADDRESS + page * FLASH_PAGE_SIZE;
6666

6767
barrier_dsync_fence_full();
6868

@@ -99,7 +99,7 @@ static void write_disable(FLASH_TypeDef *regs)
9999
static void erase_page_begin(FLASH_TypeDef *regs, unsigned int page)
100100
{
101101
volatile flash_prg_t *page_base = (flash_prg_t *)(
102-
CONFIG_FLASH_BASE_ADDRESS + page * FLASH_PAGE_SIZE);
102+
FLASH_STM32_BASE_ADDRESS + page * FLASH_PAGE_SIZE);
103103
/* Enable programming in erase mode. An erase is triggered by
104104
* writing 0 to the first word of a page.
105105
*/
@@ -123,7 +123,7 @@ static int write_value(const struct device *dev, off_t offset,
123123
flash_prg_t val)
124124
{
125125
volatile flash_prg_t *flash = (flash_prg_t *)(
126-
offset + CONFIG_FLASH_BASE_ADDRESS);
126+
offset + FLASH_STM32_BASE_ADDRESS);
127127
FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
128128
int rc;
129129

drivers/flash/flash_stm32f2x.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static int write_byte(const struct device *dev, off_t offset, uint8_t val)
7676
/* flush the register write */
7777
tmp = regs->CR;
7878

79-
*((uint8_t *) offset + CONFIG_FLASH_BASE_ADDRESS) = val;
79+
*((uint8_t *) offset + FLASH_STM32_BASE_ADDRESS) = val;
8080

8181
/* Wait until the BSY bit is cleared */
8282
rc = flash_stm32_wait_flash_idle(dev);

drivers/flash/flash_stm32f4x.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static int write_byte(const struct device *dev, off_t offset, uint8_t val)
101101
/* flush the register write */
102102
tmp = regs->CR;
103103

104-
*((uint8_t *) offset + CONFIG_FLASH_BASE_ADDRESS) = val;
104+
*((uint8_t *) offset + FLASH_STM32_BASE_ADDRESS) = val;
105105

106106
rc = flash_stm32_wait_flash_idle(dev);
107107
regs->CR &= (~FLASH_CR_PG);

drivers/flash/flash_stm32f7x.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static int write_byte(const struct device *dev, off_t offset, uint8_t val)
6060
barrier_dsync_fence_full();
6161

6262
/* write the data */
63-
*((uint8_t *) offset + CONFIG_FLASH_BASE_ADDRESS) = val;
63+
*((uint8_t *) offset + FLASH_STM32_BASE_ADDRESS) = val;
6464
/* flush the register write */
6565
barrier_dsync_fence_full();
6666

drivers/flash/flash_stm32g0x.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static inline void flush_cache(FLASH_TypeDef *regs)
5656

5757
static int write_dword(const struct device *dev, off_t offset, uint64_t val)
5858
{
59-
volatile uint32_t *flash = (uint32_t *)(offset + CONFIG_FLASH_BASE_ADDRESS);
59+
volatile uint32_t *flash = (uint32_t *)(offset + FLASH_STM32_BASE_ADDRESS);
6060
FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
6161
uint32_t tmp;
6262
int rc;

drivers/flash/flash_stm32g4x.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static inline void flush_cache(FLASH_TypeDef *regs)
7575

7676
static int write_dword(const struct device *dev, off_t offset, uint64_t val)
7777
{
78-
volatile uint32_t *flash = (uint32_t *)(offset + CONFIG_FLASH_BASE_ADDRESS);
78+
volatile uint32_t *flash = (uint32_t *)(offset + FLASH_STM32_BASE_ADDRESS);
7979
FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
8080
#if defined(FLASH_STM32_DBANK)
8181
bool dcache_enabled = false;

drivers/flash/flash_stm32h7x.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ static int write_ndwords(const struct device *dev,
307307
uint8_t n)
308308
{
309309
volatile uint64_t *flash = (uint64_t *)(offset
310-
+ CONFIG_FLASH_BASE_ADDRESS);
310+
+ FLASH_STM32_BASE_ADDRESS);
311311
int rc;
312312
int i;
313313
struct flash_stm32_sector_t sector = get_sector(dev, offset);
@@ -451,7 +451,7 @@ static void flash_stm32h7_flush_caches(const struct device *dev,
451451
return; /* Cache not enabled */
452452
}
453453

454-
SCB_InvalidateDCache_by_Addr((uint32_t *)(CONFIG_FLASH_BASE_ADDRESS
454+
SCB_InvalidateDCache_by_Addr((uint32_t *)(FLASH_STM32_BASE_ADDRESS
455455
+ offset), len);
456456
}
457457
#endif /* CONFIG_CPU_CORTEX_M7 */
@@ -574,7 +574,7 @@ static int flash_stm32h7_read(const struct device *dev, off_t offset,
574574
barrier_dsync_fence_full();
575575
barrier_isync_fence_full();
576576

577-
memcpy(data, (uint8_t *) CONFIG_FLASH_BASE_ADDRESS + offset, len);
577+
memcpy(data, (uint8_t *) FLASH_STM32_BASE_ADDRESS + offset, len);
578578

579579
__set_FAULTMASK(0);
580580
SCB->CCR &= ~SCB_CCR_BFHFNMIGN_Msk;

drivers/flash/flash_stm32l4x.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static unsigned int get_page(off_t offset)
6969

7070
static int write_dword(const struct device *dev, off_t offset, uint64_t val)
7171
{
72-
volatile uint32_t *flash = (uint32_t *)(offset + CONFIG_FLASH_BASE_ADDRESS);
72+
volatile uint32_t *flash = (uint32_t *)(offset + FLASH_STM32_BASE_ADDRESS);
7373
FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
7474
#ifdef CONTROL_DCACHE
7575
bool dcache_enabled = false;

0 commit comments

Comments
 (0)