PER Firmware
Loading...
Searching...
No Matches
flash_priv.c File Reference

STM32G4 internal-flash register-level implementation. More...

#include "common/phal_G4/flash/flash_priv.h"
#include "stm32g474xx.h"
Include dependency graph for flash_priv.c:

Functions

static bool flash_acquire (void)
 
static void flash_release (void)
 
static bool flash_is_busy (void)
 
static bool flash_wait_until_not_busy (void)
 
static void flash_clear_status_flags (void)
 
static bool flash_wait_for_last_operation (void)
 
static bool flash_unlock (void)
 
static bool flash_lock (void)
 
static void flash_disable_data_cache (flash_operation_context_t *context)
 
static void flash_restore_caches (const flash_operation_context_t *context)
 
static bool flash_dual_bank_enabled (void)
 
static bool flash_voltage_allows_programming (void)
 
static bool flash_banks_swapped (void)
 
bool FLASH_PRIV_begin_read (void)
 Acquire the flash operation lock, wait for idle, and clear status.
 
bool FLASH_PRIV_end_read (void)
 Finish a read operation and release the operation lock.
 
bool FLASH_PRIV_begin_operation (flash_operation_context_t *context)
 Prepare the controller for programming or page erase.
 
bool FLASH_PRIV_end_operation (const flash_operation_context_t *context)
 Finalize a completed operation, restore cache state, lock the controller, and release the operation lock.
 
uint32_t FLASH_PRIV_get_size_bytes (void)
 Read the device-reported internal flash capacity.
 
uint32_t FLASH_PRIV_get_page_size_bytes (void)
 Determine the active page size from the dual-bank option bit.
 
bool FLASH_PRIV_program_double_word (uint32_t address, uint64_t data)
 Program one STM32G4 double word.
 
bool FLASH_PRIV_erase_page (uint32_t page_address)
 Erase one complete flash page.
 

Variables

static constexpr uint32_t PHAL_FLASH_KEY_1 = 0x45670123U
 
static constexpr uint32_t PHAL_FLASH_KEY_2 = 0xCDEF89ABU
 
static constexpr uint32_t PHAL_FLASH_DEFAULT_SIZE_BYTES = 512U * 1024U
 
static constexpr uint32_t PHAL_FLASH_MAX_SIZE_BYTES = 512U * 1024U
 
static constexpr uint32_t PHAL_FLASH_DUAL_PAGE_BYTES = 2U * 1024U
 
static constexpr uint32_t PHAL_FLASH_SINGLE_PAGE_BYTES = 4U * 1024U
 
static constexpr uint32_t PHAL_FLASH_MAX_WAIT_ITERATIONS = 170'000'000U
 
static constexpr uint32_t PHAL_FLASH_STATUS_ERROR_MASK
 
static constexpr uint32_t PHAL_FLASH_CONTROL_OPERATION_MASK
 
static volatile bool g_flash_operation_active = false
 

Detailed Description

STM32G4 internal-flash register-level implementation.

Author
Ronak Jain (jain7.nosp@m.17@p.nosp@m.urdue.nosp@m..edu)

This file owns FLASH, PWR, RCC, and SYSCFG register access. The public layer supplies validated requests and receives simple success/failure results.

Function Documentation

◆ flash_acquire()

static bool flash_acquire ( void )
static

Acquire the software operation lock with interrupts masked briefly.

Here is the caller graph for this function:

◆ flash_banks_swapped()

static bool flash_banks_swapped ( void )
static

Read SYSCFG bank remap state without leaving its clock enabled.

Here is the caller graph for this function:

◆ flash_clear_status_flags()

static void flash_clear_status_flags ( void )
static

Clear completed/error flags using FLASH_SR's write-one-to-clear behavior.

Here is the caller graph for this function:

◆ flash_disable_data_cache()

static void flash_disable_data_cache ( flash_operation_context_t * context)
static

Save cache state and disable D-cache before modifying flash.

Here is the caller graph for this function:

◆ flash_dual_bank_enabled()

static bool flash_dual_bank_enabled ( void )
static

Read FLASH_OPTR.DBANK to select 2 KiB or 4 KiB page geometry.

Here is the caller graph for this function:

◆ flash_is_busy()

static bool flash_is_busy ( void )
static

FLASH_SR.BSY indicates that the controller still owns the flash array.

Here is the caller graph for this function:

◆ flash_lock()

static bool flash_lock ( void )
static

Relock the controller and verify FLASH_CR.LOCK was set.

Here is the caller graph for this function:

◆ FLASH_PRIV_begin_operation()

bool FLASH_PRIV_begin_operation ( flash_operation_context_t * context)

Prepare the controller for programming or page erase.

This validates the operating voltage, waits for idle, disables data cache, unlocks the controller, and clears stale status flags.

Parameters
contextOutput cache state restored by FLASH_PRIV_end_operation().
Returns
true when the controller is ready; false when unavailable or invalid.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FLASH_PRIV_begin_read()

bool FLASH_PRIV_begin_read ( void )

Acquire the flash operation lock, wait for idle, and clear status.

Returns
true when a read operation may proceed; false if already in use or the busy wait times out.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FLASH_PRIV_end_operation()

bool FLASH_PRIV_end_operation ( const flash_operation_context_t * context)

Finalize a completed operation, restore cache state, lock the controller, and release the operation lock.

Parameters
contextCache state returned by FLASH_PRIV_begin_operation().
Returns
true when the controller is idle, error-free, and locked again.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FLASH_PRIV_end_read()

bool FLASH_PRIV_end_read ( void )

Finish a read operation and release the operation lock.

Returns
true when no flash error flags were set.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FLASH_PRIV_erase_page()

bool FLASH_PRIV_erase_page ( uint32_t page_address)

Erase one complete flash page.

The page number and bank are derived from the absolute page address and the current device bank configuration.

Parameters
page_addressAddress of the page start.
Returns
true when erase completes without a flash error.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FLASH_PRIV_get_page_size_bytes()

uint32_t FLASH_PRIV_get_page_size_bytes ( void )

Determine the active page size from the dual-bank option bit.

Returns
2 KiB for dual-bank mode or 4 KiB for single-bank mode.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FLASH_PRIV_get_size_bytes()

uint32_t FLASH_PRIV_get_size_bytes ( void )

Read the device-reported internal flash capacity.

Returns
Flash size in bytes, using the supported-device fallback if invalid.
Here is the caller graph for this function:

◆ FLASH_PRIV_program_double_word()

bool FLASH_PRIV_program_double_word ( uint32_t address,
uint64_t data )

Program one STM32G4 double word.

Parameters
address8-byte-aligned flash destination.
data64-bit value written low word first.
Returns
true when programming completes without a flash error.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ flash_release()

static void flash_release ( void )
static

Release the software operation lock while preserving interrupt state.

Here is the caller graph for this function:

◆ flash_restore_caches()

static void flash_restore_caches ( const flash_operation_context_t * context)
static

Restore the cache states saved before a write or erase operation.

Here is the caller graph for this function:

◆ flash_unlock()

static bool flash_unlock ( void )
static

Unlock FLASH_CR with the required two-key sequence.

Here is the caller graph for this function:

◆ flash_voltage_allows_programming()

static bool flash_voltage_allows_programming ( void )
static

Confirm voltage scaling range 1 and a stable VOS transition.

PWR is clocked only for the duration of this check when necessary, and the caller's interrupt mask is restored before returning.

Here is the caller graph for this function:

◆ flash_wait_for_last_operation()

static bool flash_wait_for_last_operation ( void )
static

Wait for BSY, collect errors, and clear EOP/error flags.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ flash_wait_until_not_busy()

static bool flash_wait_until_not_busy ( void )
static

Wait for BSY to clear with a bounded, clock-scaled timeout.

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ g_flash_operation_active

volatile bool g_flash_operation_active = false
static

◆ PHAL_FLASH_CONTROL_OPERATION_MASK

uint32_t PHAL_FLASH_CONTROL_OPERATION_MASK
staticconstexpr
Initial value:
= FLASH_CR_PG | FLASH_CR_PER
| FLASH_CR_MER1 | FLASH_CR_PNB_Msk | FLASH_CR_BKER | FLASH_CR_MER2 | FLASH_CR_FSTPG

◆ PHAL_FLASH_DEFAULT_SIZE_BYTES

uint32_t PHAL_FLASH_DEFAULT_SIZE_BYTES = 512U * 1024U
staticconstexpr

◆ PHAL_FLASH_DUAL_PAGE_BYTES

uint32_t PHAL_FLASH_DUAL_PAGE_BYTES = 2U * 1024U
staticconstexpr

◆ PHAL_FLASH_KEY_1

uint32_t PHAL_FLASH_KEY_1 = 0x45670123U
staticconstexpr

◆ PHAL_FLASH_KEY_2

uint32_t PHAL_FLASH_KEY_2 = 0xCDEF89ABU
staticconstexpr

◆ PHAL_FLASH_MAX_SIZE_BYTES

uint32_t PHAL_FLASH_MAX_SIZE_BYTES = 512U * 1024U
staticconstexpr

◆ PHAL_FLASH_MAX_WAIT_ITERATIONS

uint32_t PHAL_FLASH_MAX_WAIT_ITERATIONS = 170'000'000U
staticconstexpr

◆ PHAL_FLASH_SINGLE_PAGE_BYTES

uint32_t PHAL_FLASH_SINGLE_PAGE_BYTES = 4U * 1024U
staticconstexpr

◆ PHAL_FLASH_STATUS_ERROR_MASK

uint32_t PHAL_FLASH_STATUS_ERROR_MASK
staticconstexpr
Initial value:
= FLASH_SR_OPERR | FLASH_SR_PROGERR
| FLASH_SR_WRPERR | FLASH_SR_PGAERR | FLASH_SR_SIZERR | FLASH_SR_PGSERR | FLASH_SR_MISERR
| FLASH_SR_FASTERR | FLASH_SR_RDERR | FLASH_SR_OPTVERR