2019-02-06 21:26:55 -08:00
|
|
|
/**
|
2020-04-25 13:32:32 -07:00
|
|
|
* @file flash_int.h
|
2019-02-06 21:26:55 -08:00
|
|
|
*
|
|
|
|
*/
|
2020-02-18 05:16:19 -08:00
|
|
|
|
|
|
|
#pragma once
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
/* Error codes */
|
|
|
|
|
|
|
|
/** @brief Flash operation successful */
|
2016-02-04 18:03:01 -08:00
|
|
|
#define FLASH_RETURN_SUCCESS HAL_SUCCESS
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
/** @brief Flash operation error because of denied access, corrupted memory.*/
|
|
|
|
#define FLASH_RETURN_NO_PERMISSION -1
|
|
|
|
|
2021-08-01 15:26:42 -07:00
|
|
|
/** @brief Flash operation error */
|
|
|
|
#define FLASH_RETURN_OPERROR -2
|
|
|
|
|
|
|
|
/** @brief Flash write protection error */
|
|
|
|
#define FLASH_RETURN_WPERROR -3
|
|
|
|
|
|
|
|
/** @brief Flash alignment error */
|
|
|
|
#define FLASH_RETURN_ALIGNERROR -4
|
|
|
|
|
|
|
|
/** @brief Flash programming parallelism error */
|
|
|
|
#define FLASH_RETURN_PPARALLERROR -5
|
|
|
|
|
|
|
|
/** @brief Flash erase sequence error */
|
|
|
|
#define FLASH_RETURN_ESEQERROR -6
|
|
|
|
|
|
|
|
/** @brief Flash programming sequence error */
|
|
|
|
#define FLASH_RETURN_PSEQERROR -7
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/** @brief Flash operation error because of bad flash, corrupted memory */
|
|
|
|
#define FLASH_RETURN_BAD_FLASH -11
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Maximum program/erase parallelism
|
|
|
|
*
|
|
|
|
* FLASH_CR_PSIZE_MASK is the mask to configure the parallelism value.
|
|
|
|
* FLASH_CR_PSIZE_VALUE is the parallelism value suitable for the voltage range.
|
|
|
|
*
|
|
|
|
* PSIZE(1:0) is defined as:
|
|
|
|
* 00 to program 8 bits per step
|
|
|
|
* 01 to program 16 bits per step
|
|
|
|
* 10 to program 32 bits per step
|
|
|
|
* 11 to program 64 bits per step
|
|
|
|
*/
|
|
|
|
// Warning, flashdata_t must be unsigned!!!
|
2021-08-01 15:26:42 -07:00
|
|
|
#if defined(STM32F4XX) || defined(STM32H7XX)
|
|
|
|
#define FLASH_CR_PSIZE_MASK (FLASH_CR_PSIZE_0 | FLASH_CR_PSIZE_1)
|
2019-04-12 17:52:51 -07:00
|
|
|
#if ((STM32_VDD >= 270) && (STM32_VDD <= 360))
|
2015-07-10 06:01:56 -07:00
|
|
|
#define FLASH_CR_PSIZE_VALUE FLASH_CR_PSIZE_1
|
|
|
|
typedef uint32_t flashdata_t;
|
|
|
|
#elif (STM32_VDD >= 240) && (STM32_VDD < 270)
|
|
|
|
#define FLASH_CR_PSIZE_VALUE FLASH_CR_PSIZE_0
|
|
|
|
typedef uint16_t flashdata_t;
|
|
|
|
#elif (STM32_VDD >= 210) && (STM32_VDD < 240)
|
|
|
|
#define FLASH_CR_PSIZE_VALUE FLASH_CR_PSIZE_0
|
|
|
|
typedef uint16_t flashdata_t;
|
|
|
|
#elif (STM32_VDD >= 180) && (STM32_VDD < 210)
|
|
|
|
#define FLASH_CR_PSIZE_VALUE ((uint32_t)0x00000000)
|
|
|
|
typedef uint8_t flashdata_t;
|
|
|
|
#else
|
|
|
|
#error "invalid VDD voltage specified"
|
|
|
|
#endif
|
2021-08-01 15:26:42 -07:00
|
|
|
#endif /* defined(STM32F4XX) || defined(STM32H7XX) */
|
|
|
|
|
|
|
|
#if defined(STM32F7XX)
|
|
|
|
#define FLASH_CR_PSIZE_MASK (FLASH_CR_PSIZE_0 | FLASH_CR_PSIZE_1)
|
|
|
|
#if ((STM32_VDD >= 270) && (STM32_VDD <= 300))
|
|
|
|
#define FLASH_CR_PSIZE_VALUE FLASH_CR_PSIZE_1
|
|
|
|
typedef uint32_t flashdata_t;
|
|
|
|
#elif (STM32_VDD >= 210) && (STM32_VDD < 360)
|
|
|
|
#define FLASH_CR_PSIZE_VALUE FLASH_CR_PSIZE_0
|
|
|
|
typedef uint16_t flashdata_t;
|
|
|
|
#elif (STM32_VDD >= 170) && (STM32_VDD < 360)
|
|
|
|
#define FLASH_CR_PSIZE_VALUE ((uint32_t)0x00000000)
|
|
|
|
typedef uint8_t flashdata_t;
|
|
|
|
#else
|
|
|
|
#error "invalid VDD voltage specified"
|
|
|
|
#endif
|
|
|
|
#endif /* defined(STM32F7XX) */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
/** @brief Address in the flash memory */
|
|
|
|
typedef uintptr_t flashaddr_t;
|
|
|
|
|
|
|
|
/** @brief Index of a sector */
|
|
|
|
typedef uint8_t flashsector_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the size of @p sector.
|
|
|
|
* @return @p sector size in bytes.
|
|
|
|
*/
|
|
|
|
size_t flashSectorSize(flashsector_t sector);
|
|
|
|
|
2020-02-04 18:36:38 -08:00
|
|
|
uintptr_t getFlashAddrFirstCopy(void);
|
|
|
|
uintptr_t getFlashAddrSecondCopy(void);
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @brief Erase the flash @p sector.
|
|
|
|
* @details The sector is checked for errors after erase.
|
|
|
|
* @note The sector is deleted regardless of its current state.
|
|
|
|
*
|
|
|
|
* @param sector Sector which is going to be erased.
|
|
|
|
* @return FLASH_RETURN_SUCCESS No error erasing the sector.
|
|
|
|
* @return FLASH_RETURN_BAD_FLASH Flash cell error.
|
|
|
|
* @return FLASH_RETURN_NO_PERMISSION Access denied.
|
|
|
|
*/
|
2020-04-25 13:32:32 -07:00
|
|
|
int intFlashSectorErase(flashsector_t sector);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Erase the sectors containing the span of @p size bytes starting at @p address.
|
|
|
|
*
|
|
|
|
* @warning If @p address doesn't match the beginning of a sector, the
|
|
|
|
* data contained between the beginning of the sector and @p address will
|
|
|
|
* be erased too. The same applies for data contained at @p address + @p size
|
|
|
|
* up to the end of the sector.
|
|
|
|
*
|
|
|
|
* @param address Starting address of the span in flash memory.
|
|
|
|
* @param size Size of the span in bytes.
|
|
|
|
* @return FLASH_RETURN_SUCCESS No error erasing the flash memory.
|
|
|
|
* @return FLASH_RETURN_BAD_FLASH Flash cell error.
|
|
|
|
* @return FLASH_RETURN_NO_PERMISSION Access denied.
|
|
|
|
*/
|
2020-04-25 13:32:32 -07:00
|
|
|
int intFlashErase(flashaddr_t address, size_t size);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Check if the @p size bytes of flash memory starting at @p address are erased.
|
|
|
|
* @note If the memory is erased, one can write data into it safely.
|
|
|
|
* @param address First address in flash memory to be checked.
|
|
|
|
* @param size Size of the memory space to be checked in bytes.
|
|
|
|
* @return TRUE Memory is already erased.
|
|
|
|
* @return FALSE Memory is not erased.
|
|
|
|
*/
|
2020-04-25 13:32:32 -07:00
|
|
|
bool intFlashIsErased(flashaddr_t address, size_t size);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Check if the data in @p buffer are identical to the one in flash memory.
|
|
|
|
* @param address First address in flash memory to be checked.
|
|
|
|
* @param buffer Buffer containing the data to compare.
|
|
|
|
* @param size Size of @p buffer in bytes.
|
|
|
|
* @return TRUE if the flash memory and the buffer contain identical data.
|
|
|
|
* @return FALSE if the flash memory and the buffer don't contain identical data.
|
|
|
|
*/
|
2020-04-25 13:32:32 -07:00
|
|
|
bool intFlashCompare(flashaddr_t address, const char* buffer, size_t size);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Copy data from the flash memory to a @p buffer.
|
|
|
|
* @warning The @p buffer must be at least @p size bytes long.
|
|
|
|
* @param address First address of the flash memory to be copied.
|
|
|
|
* @param buffer Buffer to copy to.
|
|
|
|
* @param size Size of the data to be copied in bytes.
|
|
|
|
* @return FLASH_RETURN_SUCCESS if successfully copied.
|
|
|
|
*/
|
2020-04-25 13:32:32 -07:00
|
|
|
int intFlashRead(flashaddr_t address, char* buffer, size_t size);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Copy data from a @p buffer to the flash memory.
|
|
|
|
* @warning The flash memory area receiving the data must be erased.
|
|
|
|
* @warning The @p buffer must be at least @p size bytes long.
|
|
|
|
* @param address First address in the flash memory where to copy the data to.
|
|
|
|
* @param buffer Buffer containing the data to copy.
|
|
|
|
* @param size Size of the data to be copied in bytes.
|
|
|
|
* @return FLASH_RETURN_SUCCESS No error.
|
|
|
|
* @return FLASH_RETURN_NO_PERMISSION Access denied.
|
|
|
|
*/
|
2020-04-25 13:32:32 -07:00
|
|
|
int intFlashWrite(flashaddr_t address, const char* buffer, size_t size);
|