204 lines
5.7 KiB
C
204 lines
5.7 KiB
C
/**
|
|
* Copyright (c) 2024 Wei-Lun Hsu. All Rights Reserved.
|
|
*/
|
|
/** @file hal_flash.h
|
|
*
|
|
* @author Wei-Lun Hsu
|
|
* @version 0.1
|
|
* @date 2024/09/10
|
|
* @license
|
|
* @description
|
|
*/
|
|
|
|
#ifndef __hal_flash_H_wm4SBnn4_laN2_HBZw_sxWM_uBboLm9QaVsk__
|
|
#define __hal_flash_H_wm4SBnn4_laN2_HBZw_sxWM_uBboLm9QaVsk__
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
|
|
#include "hal_def.h"
|
|
//=============================================================================
|
|
// Constant Definition
|
|
//=============================================================================
|
|
#define FLASH_1_SECTOR_SIZE 512
|
|
#define FLASH_1_PAGE_SIZE FLASH_1_SECTOR_SIZE
|
|
|
|
#define FLASH_AUTH_KEY 0xA5A50000ul
|
|
|
|
/**
|
|
* eFlash status
|
|
*/
|
|
typedef enum flash_state
|
|
{
|
|
FLASH_STATE_OK = 0,
|
|
FLASH_STATE_BUSY,
|
|
FLASH_STATE_FAIL_PROG,
|
|
FLASH_STATE_FAIL_ERASE,
|
|
FLASH_STATE_FAIL_ADDR,
|
|
FLASH_STATE_NOT_4_ALIGN,
|
|
FLASH_STATE_FAIL_PARAM,
|
|
FLASH_STATE_TIMEOUT
|
|
} flash_state_t;
|
|
|
|
/**
|
|
* eFlash latency type
|
|
*/
|
|
typedef enum flash_latency
|
|
{
|
|
FLASH_LATENCY_1 = (0x1ul << FLASH_CMD_NWS_Pos),
|
|
FLASH_LATENCY_2 = (0x2ul << FLASH_CMD_NWS_Pos),
|
|
FLASH_LATENCY_3 = (0x3ul << FLASH_CMD_NWS_Pos),
|
|
|
|
} flash_latency_t;
|
|
|
|
/**
|
|
* eFlash H/w error type
|
|
*/
|
|
typedef enum flash_err_type
|
|
{
|
|
FLASH_ERR_TYPE_OK = 0,
|
|
FLASH_ERR_TYPE_KEY = FLASH_SR_KEY_ERR_Msk,
|
|
FLASH_ERR_TYPE_ACCESS = FLASH_SR_ACC_ERR_Msk,
|
|
FLASH_ERR_TYPE_ADDRESS = FLASH_SR_ADDR_ERR_Msk,
|
|
|
|
FLASH_ERR_TYPE_SYSINFO_FAIL = (FLASH_SR_HSI_TC_ERR_Msk | FLASH_SR_RSTIO_AF_ERR_Msk | \
|
|
FLASH_SR_LDO_TRIM_ERR_Msk | FLASH_SR_VBUF_TRIM_ERR_Msk | \
|
|
FLASH_SR_LSI_TRIM_ERR_Msk | FLASH_SR_HSI_TRIM_ERR_Msk),
|
|
|
|
FLASH_ERR_TYPE_INVALID_TRIM_HSI = FLASH_SR_HSI_TRIM_ERR_Msk,
|
|
FLASH_ERR_TYPE_INVALID_TRIM_HSI_TC = FLASH_SR_HSI_TC_ERR_Msk,
|
|
FLASH_ERR_TYPE_INVALID_TRIM_LSI = FLASH_SR_LSI_TRIM_ERR_Msk,
|
|
FLASH_ERR_TYPE_INVALID_TRIM_VBUF = FLASH_SR_VBUF_TRIM_ERR_Msk,
|
|
FLASH_ERR_TYPE_INVALID_TRIM_LDO = FLASH_SR_LDO_TRIM_ERR_Msk,
|
|
FLASH_ERR_TYPE_INVALID_RSTIO_CFG = FLASH_SR_RSTIO_AF_ERR_Msk,
|
|
} flash_err_type_t;
|
|
//=============================================================================
|
|
// Macro Definition
|
|
//=============================================================================
|
|
/**
|
|
* \brief Check eFlash is idle or not.
|
|
*
|
|
* \return
|
|
* 0 : busy
|
|
* others: idle
|
|
*/
|
|
__STATIC_FORCEINLINE uint32_t FLASH_IsIdle(void)
|
|
{
|
|
return !REG_READ_MASK(FLASH->CMD, FLASH_CMD_START_Msk);
|
|
}
|
|
|
|
|
|
/**
|
|
* \brief Get the latency type of eFlash
|
|
*
|
|
* \return
|
|
* Latency value, @ref flash_latency_t
|
|
*/
|
|
__STATIC_FORCEINLINE uint32_t FLASH_GetLatency(void)
|
|
{
|
|
return REG_READ_MASK(FLASH->CMD, FLASH_CMD_NWS_Msk);
|
|
}
|
|
|
|
/**
|
|
* \brief Get eFlash H/w error type
|
|
*
|
|
* \return
|
|
* eFlash H/w error type, @ref flash_err_type_t
|
|
*/
|
|
__STATIC_FORCEINLINE flash_err_type_t FLASH_GetHwErr(void)
|
|
{
|
|
return (flash_err_type_t)REG_READ(FLASH->SR);
|
|
}
|
|
//=============================================================================
|
|
// Structure Definition
|
|
//=============================================================================
|
|
|
|
//=============================================================================
|
|
// Global Data Definition
|
|
//=============================================================================
|
|
|
|
//=============================================================================
|
|
// Private Function Definition
|
|
//=============================================================================
|
|
|
|
//=============================================================================
|
|
// Public Function Definition
|
|
//=============================================================================
|
|
/**
|
|
* \brief Set Flash latency
|
|
* When CPU frequency is over eFlash,
|
|
* CPU MUST wait eFlash response
|
|
*
|
|
* \param [in] latency CPU wait latency (cycles) for eFlash, @ref flash_latency_t
|
|
* \return
|
|
* state, @ref flash_state_t
|
|
*/
|
|
flash_state_t
|
|
FLASH_SetLatency(flash_latency_t latency);
|
|
|
|
|
|
/**
|
|
* \brief Erase a page of eFlash
|
|
*
|
|
* \param [in] page_addr the target address (FLASH_1_PAGE_SIZE align) of the eFlash
|
|
* \return
|
|
* state, @ref flash_state_t
|
|
*/
|
|
flash_state_t
|
|
FLASH_ErasePage(uint32_t page_addr);
|
|
|
|
|
|
/**
|
|
* \brief Program a word (32-bits) value to eFlash
|
|
*
|
|
* \param [in] addr the target address of the eFlash
|
|
* \param [in] value the target value (32-bits)
|
|
* \return
|
|
* state, @ref flash_state_t
|
|
*/
|
|
flash_state_t
|
|
FLASH_ProgWord(uint32_t addr, uint32_t value);
|
|
|
|
/**
|
|
* \brief Program a half-word (16-bits) value to eFlash
|
|
*
|
|
* \param [in] addr the target address of the eFlash
|
|
* \param [in] value the target value (16-bits)
|
|
* \return
|
|
* state, @ref flash_state_t
|
|
*/
|
|
flash_state_t
|
|
FLASH_ProgHWord(uint32_t addr, uint16_t value);
|
|
|
|
/**
|
|
* \brief Program a byte (8-bits) value to eFlash
|
|
*
|
|
* \param [in] addr the target address of the eFlash
|
|
* \param [in] value the target value (8-bits)
|
|
* \return
|
|
* state, @ref flash_state_t
|
|
*/
|
|
flash_state_t
|
|
FLASH_ProgByte(uint32_t addr, uint8_t value);
|
|
|
|
/**
|
|
* \brief Program data set to eFlash
|
|
*
|
|
* \param [in] addr the target address of the eFlash
|
|
* \param [in] pData pointer to a 32-bits buffer of data set
|
|
* \param [in] length the 4-align length of data
|
|
* \return
|
|
* state, @ref flash_state_t
|
|
*/
|
|
flash_state_t
|
|
FLASH_ProgData(uint32_t addr, uint32_t *pData, int length);
|
|
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif
|