Skip to content

Commit

Permalink
💄 去除冗余 Flash Driver 代码
Browse files Browse the repository at this point in the history
Signed-off-by: DAVE <[email protected]>
  • Loading branch information
SummerFalls committed May 21, 2021
1 parent 1cfdca4 commit e223a29
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 178 deletions.
171 changes: 56 additions & 115 deletions Sources/NVM_Flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,32 +57,19 @@ tFlashDriverAPIInfo *g_pFlashDriverAPIRAM = NULL;

/* TODO S32K_FlashDrv: #03 本偏移结构体内成员必须与对应芯片型号的 Bootloader 工程中 flash.h 文件的 tFlashOptInfo 结构体类型的 Flash API 成员一一对应并且数量相等 */
#pragma GCC diagnostic ignored "-Wunused-const-variable="
static const tFlashDriverAPIInfo gs_FlashDriverAPI NVM_DRIVER_SECTION = {
#if defined (S32K116) || defined (S32K142)
(tpfFLASH_DRV_EraseSector) CAL_OFFSET(FLASH_DRV_EraseSector),
(tpfFLASH_DRV_Program) CAL_OFFSET(FLASH_DRV_Program),
(tpfFLASH_DRV_VerifySection) CAL_OFFSET(FLASH_DRV_VerifySection),
(tpfFLASH_DRV_GetDefaultConfig) CAL_OFFSET(FLASH_DRV_GetDefaultConfig),
#elif defined (S32K144) || defined (S32K118)
// (tpfFLASH_DRV_EraseAllBlock) CAL_OFFSET(FLASH_DRV_EraseAllBlock),
// (tpfFLASH_DRV_VerifyAllBlock) CAL_OFFSET(FLASH_DRV_VerifyAllBlock),
(tpfFLASH_DRV_EraseSector) CAL_OFFSET(FLASH_DRV_EraseSector),
(tpfFLASH_DRV_VerifySection) CAL_OFFSET(FLASH_DRV_VerifySection),
// (tpfFLASH_DRV_EraseSuspend) CAL_OFFSET(FLASH_DRV_EraseSuspend),
// (tpfFLASH_DRV_EraseResume) CAL_OFFSET(FLASH_DRV_EraseResume),
(tpfFLASH_DRV_Program) CAL_OFFSET(FLASH_DRV_Program),
(tpfFLASH_DRV_ProgramCheck) CAL_OFFSET(FLASH_DRV_ProgramCheck),
// (tpfFLASH_DRV_ProgramSection) CAL_OFFSET(FLASH_DRV_ProgramSection),
// (tpfFLASH_DRV_EraseBlock) CAL_OFFSET(FLASH_DRV_EraseBlock),
// (tpfFLASH_DRV_CommandSequence) CAL_OFFSET(FLASH_DRV_CommandSequence),
// (tpfFLASH_DRV_VerifyBlock) CAL_OFFSET(FLASH_DRV_VerifyBlock),
#endif
static const tFlashDriverAPIInfo gs_FlashDriverAPI NVM_DRIVER_SECTION =
{
(tpfFLASH_DRV_EraseSector) CAL_OFFSET(FLASH_DRV_EraseSector),
(tpfFLASH_DRV_VerifySection) CAL_OFFSET(FLASH_DRV_VerifySection),
(tpfFLASH_DRV_Program) CAL_OFFSET(FLASH_DRV_Program),
(tpfFLASH_DRV_ProgramCheck) CAL_OFFSET(FLASH_DRV_ProgramCheck),
};

/* TODO S32K_FlashDrv: #04 Flash 驱动函数表:包含前16(0x10)字节的指针映射偏移表以及相应的4个驱动函数
* 编译后,通过 J-Flash 打开 Hex 文件提取对应地址处的内容,粘贴到以下数组即可测试
*/
uint8_t g_flashDriverRAM[] = {
uint8_t g_flashDriverRAM[] =
{
0x11, 0x00, 0x00, 0x00, 0x49, 0x01, 0x00, 0x00, 0x65, 0x02, 0x00, 0x00, 0xB1, 0x03, 0x00, 0x00,
0x80, 0xB5, 0x88, 0xB0, 0x00, 0xAF, 0xF8, 0x60, 0xB9, 0x60, 0x7A, 0x60, 0x00, 0x23, 0x7B, 0x82,
0x7B, 0x68, 0xBB, 0x61, 0xFB, 0x68, 0x9B, 0x68, 0x7B, 0x61, 0xBA, 0x68, 0x7B, 0x69, 0x9A, 0x42,
Expand Down Expand Up @@ -169,93 +156,27 @@ uint8_t g_flashDriverRAM[] = {
0x00, 0x00, 0x02, 0x40, 0x0C, 0x00, 0x02, 0x40
};

#define BUFFER_SIZE 0x100u /* Size of data source */
/*******************************************************************************
* @name : DTek_TestFlashDriver
* @name : NVM_TestFlashDriver
* @brief : 用于测试 Flash API
* @param : void
* @retval : void
*******************************************************************************/
#if defined (S32K116) || defined (S32K142)
void DTek_TestFlashDriver(void)
{
flash_user_config_t FlashUserConfig;
flash_ssd_config_t SSDFlashConfig;
status_t result = STATUS_SUCCESS;
uint32_t destAddr = 0x18000u;
uint8_t errorCnt = 0u;

/* TODO S32K_FlashDrv: #05 注意不同MCU的Sector Size不同,需要进行相应调整,否则FLASH_DRV_EraseSector会执行失败 */
uint32_t size = FEATURE_FLS_PF_BLOCK_SECTOR_SIZE;
uint8_t aDataBuf[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};

#ifdef EN_FLASH_DRIVER_DEBUG /* TODO S32K_FlashDrv: #06 This macro is in the header file: flash_driver.h */
g_pFlashDriverAPIRAM = (tFlashDriverAPIInfo *)g_flashDriverRAM;
g_pFlashDriverAPIRAM->pfFLASH_DRV_EraseSector = FLASH_DRV_EraseSector;
g_pFlashDriverAPIRAM->pfFLASH_DRV_Program = FLASH_DRV_Program;
g_pFlashDriverAPIRAM->pfFLASH_DRV_VerifySection = FLASH_DRV_VerifySection;
g_pFlashDriverAPIRAM->pfFLASH_DRV_GetDefaultConfig = FLASH_DRV_GetDefaultConfig;
g_pFlashDriverAPIRAM->pfFLASH_DRV_GetDefaultConfig((flash_user_config_t *const )&FlashUserConfig);
#else
g_pFlashDriverAPIRAM = (tFlashDriverAPIInfo *)g_flashDriverRAM;
g_pFlashDriverAPIRAM->pfFLASH_DRV_EraseSector = (tpfFLASH_DRV_EraseSector) ((uint32_t)g_flashDriverRAM + (uint32_t)(g_pFlashDriverAPIRAM->pfFLASH_DRV_EraseSector));
g_pFlashDriverAPIRAM->pfFLASH_DRV_Program = (tpfFLASH_DRV_Program) ((uint32_t)g_flashDriverRAM + (uint32_t)(g_pFlashDriverAPIRAM->pfFLASH_DRV_Program));
g_pFlashDriverAPIRAM->pfFLASH_DRV_VerifySection = (tpfFLASH_DRV_VerifySection) ((uint32_t)g_flashDriverRAM + (uint32_t)(g_pFlashDriverAPIRAM->pfFLASH_DRV_VerifySection));
g_pFlashDriverAPIRAM->pfFLASH_DRV_GetDefaultConfig = (tpfFLASH_DRV_GetDefaultConfig)((uint32_t)g_flashDriverRAM + (uint32_t)(g_pFlashDriverAPIRAM->pfFLASH_DRV_GetDefaultConfig));

g_pFlashDriverAPIRAM->pfFLASH_DRV_GetDefaultConfig((flash_user_config_t *const)&FlashUserConfig);
#endif

MSCM->OCMDR[0u] |= MSCM_OCMDR_OCM1(0x3u);
MSCM->OCMDR[1u] |= MSCM_OCMDR_OCM1(0x3u);

INT_SYS_DisableIRQGlobal();

result = FLASH_DRV_Init((flash_user_config_t *const)&FlashUserConfig, (flash_ssd_config_t *const)&SSDFlashConfig);

if (STATUS_SUCCESS == result) {
result = g_pFlashDriverAPIRAM->pfFLASH_DRV_EraseSector(&SSDFlashConfig, destAddr, size);
} else {
errorCnt++;
}

if (STATUS_SUCCESS == result) {
result = g_pFlashDriverAPIRAM->pfFLASH_DRV_VerifySection(&SSDFlashConfig, destAddr, size / FTFx_DPHRASE_SIZE, 1);
} else {
errorCnt++;
}

if (STATUS_SUCCESS == result) {
while ((0u != size) && (STATUS_SUCCESS == result)) {
result = g_pFlashDriverAPIRAM->pfFLASH_DRV_Program(&SSDFlashConfig, destAddr, 8u, aDataBuf);
size -= 8u;
destAddr += 8u;
}
} else {
errorCnt++;
}

INT_SYS_EnableIRQGlobal();

for (;;) {
;
}
}
#elif defined (S32K144) || defined (S32K118)
#define BUFFER_SIZE 0x100u /* Size of data source */
void DTek_TestFlashDriver(void)
void NVM_TestFlashDriver(void)
{
flash_user_config_t FlashUserConfig;
flash_ssd_config_t SSDFlashConfig;
status_t result = STATUS_SUCCESS;
uint32_t destAddr = 20u * FEATURE_FLS_PF_BLOCK_SECTOR_SIZE; /* Erase the sixth PFlash sector */
uint8_t errorCnt = 0u;
uint32_t failAddr;

/* TODO S32K_FlashDrv: #05 注意不同MCU的Sector Size不同,需要进行相应调整,否则FLASH_DRV_EraseSector会执行失败 */
uint32_t size = FEATURE_FLS_PF_BLOCK_SECTOR_SIZE;
uint8_t sourceBuffer[BUFFER_SIZE];

for (uint32_t i = 0u; i < BUFFER_SIZE; i++) {
for (uint32_t i = 0u; i < BUFFER_SIZE; i++)
{
sourceBuffer[i] = i;
}

Expand All @@ -273,43 +194,52 @@ void DTek_TestFlashDriver(void)
g_pFlashDriverAPIRAM->pfFLASH_DRV_ProgramCheck = (tpfFLASH_DRV_ProgramCheck) ((uint32_t)g_flashDriverRAM + (uint32_t)(g_pFlashDriverAPIRAM->pfFLASH_DRV_ProgramCheck));
#endif

FLASH_DRV_GetDefaultConfig((flash_user_config_t *const )&FlashUserConfig);

FLASH_DRV_GetDefaultConfig((flash_user_config_t *const)&FlashUserConfig);
MSCM->OCMDR[0u] |= MSCM_OCMDR_OCM1(0x3u);
MSCM->OCMDR[1u] |= MSCM_OCMDR_OCM1(0x3u);

INT_SYS_DisableIRQGlobal();

result = FLASH_DRV_Init((flash_user_config_t *const)&FlashUserConfig, (flash_ssd_config_t *const)&SSDFlashConfig);

if (STATUS_SUCCESS == result) {
if (STATUS_SUCCESS == result)
{
result = g_pFlashDriverAPIRAM->pfFLASH_DRV_EraseSector(&SSDFlashConfig, destAddr, size);
} else {
}
else
{
errorCnt++;
}


if (STATUS_SUCCESS == result) {
if (STATUS_SUCCESS == result)
{
/* Verify the erase operation at margin level value of 1, user read */
result = g_pFlashDriverAPIRAM->pfFLASH_DRV_VerifySection(&SSDFlashConfig, destAddr, size / FTFx_DPHRASE_SIZE, 1u);
} else {
}
else
{
errorCnt++;
}

size = BUFFER_SIZE;

if (STATUS_SUCCESS == result) {
if (STATUS_SUCCESS == result)
{
/* Write some data to the erased PFlash sector */
result = g_pFlashDriverAPIRAM->pfFLASH_DRV_Program(&SSDFlashConfig, destAddr, size, sourceBuffer);
} else {
}
else
{
errorCnt++;
}


if (STATUS_SUCCESS == result) {
if (STATUS_SUCCESS == result)
{
/* Verify the program operation at margin level value of 1, user margin */
result = g_pFlashDriverAPIRAM->pfFLASH_DRV_ProgramCheck(&SSDFlashConfig, destAddr, size, sourceBuffer, &failAddr, 1u);
} else {
}
else
{
errorCnt++;
}

Expand All @@ -320,44 +250,55 @@ void DTek_TestFlashDriver(void)
destAddr = SSDFlashConfig.DFlashBase;
size = FEATURE_FLS_DF_BLOCK_SECTOR_SIZE;

if (STATUS_SUCCESS == result) {
if (STATUS_SUCCESS == result)
{
/* Erase a sector in DFlash */
result = g_pFlashDriverAPIRAM->pfFLASH_DRV_EraseSector(&SSDFlashConfig, destAddr, size);
} else {
}
else
{
errorCnt++;
}

if (STATUS_SUCCESS == result) {
if (STATUS_SUCCESS == result)
{
/* Verify the erase operation at margin level value of 1, user read */
result = g_pFlashDriverAPIRAM->pfFLASH_DRV_VerifySection(&SSDFlashConfig, destAddr, size / FTFx_PHRASE_SIZE, 1u);
} else {
}
else
{
errorCnt++;
}


destAddr = SSDFlashConfig.DFlashBase;
size = BUFFER_SIZE;

if (STATUS_SUCCESS == result) {
if (STATUS_SUCCESS == result)
{
/* Write some data to the erased DFlash sector */
result = g_pFlashDriverAPIRAM->pfFLASH_DRV_Program(&SSDFlashConfig, destAddr, size, sourceBuffer);
} else {
}
else
{
errorCnt++;
}

if (STATUS_SUCCESS == result) {
if (STATUS_SUCCESS == result)
{
/* Verify the program operation at margin level value of 1, user margin */
result = g_pFlashDriverAPIRAM->pfFLASH_DRV_ProgramCheck(&SSDFlashConfig, destAddr, size, sourceBuffer, &failAddr, 1u);
} else {
}
else
{
errorCnt++;
}

#endif /* FEATURE_FLS_HAS_FLEX_NVM */

for (;;) {
for (;;)
{
;
}
}
#endif

/* -------------------------------------------- END OF FILE -------------------------------------------- */
57 changes: 13 additions & 44 deletions Sources/NVM_Flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,52 +26,21 @@
#include "flash_driver.h"
#include "interrupt_manager.h"

/* TODO S32K_FlashDrv: #00 MCU 型号选择,影响生成的 Flash Driver 所包含的 API,与对应芯片型号的 Bootloader 工程对应 */
//#define S32K116
//#define S32K118
//#define S32K142
#define S32K144


typedef status_t (*tpfFLASH_DRV_EraseAllBlock)(const flash_ssd_config_t *pSSDConfig);
typedef status_t (*tpfFLASH_DRV_VerifyAllBlock)(const flash_ssd_config_t *pSSDConfig, uint8_t marginLevel);
typedef status_t (*tpfFLASH_DRV_EraseSector)(const flash_ssd_config_t *pSSDConfig, uint32_t dest, uint32_t size);
typedef status_t (*tpfFLASH_DRV_VerifySection)(const flash_ssd_config_t *pSSDConfig, uint32_t dest, uint16_t number, uint8_t marginLevel);
typedef void (*tpfFLASH_DRV_EraseSuspend)(void);
typedef void (*tpfFLASH_DRV_EraseResume)(void);
typedef status_t (*tpfFLASH_DRV_Program)(const flash_ssd_config_t *pSSDConfig, uint32_t dest, uint32_t size, const uint8_t *pData);
typedef status_t (*tpfFLASH_DRV_ProgramCheck)(const flash_ssd_config_t *pSSDConfig, uint32_t dest, uint32_t size, const uint8_t *pExpectedData, uint32_t *pFailAddr, uint8_t marginLevel);
typedef status_t (*tpfFLASH_DRV_ProgramSection)(const flash_ssd_config_t *pSSDConfig, uint32_t dest, uint16_t number);
typedef status_t (*tpfFLASH_DRV_EraseBlock)(const flash_ssd_config_t *pSSDConfig, uint32_t dest);
typedef status_t (*tpfFLASH_DRV_CommandSequence)(const flash_ssd_config_t *pSSDConfig);
typedef status_t (*tpfFLASH_DRV_VerifyBlock)(const flash_ssd_config_t *pSSDConfig, uint32_t dest, uint8_t marginLevel);
typedef status_t (*tpfFLASH_DRV_Init)(const flash_user_config_t *const pUserConf, flash_ssd_config_t *const pSSDConfig);
typedef void (*tpfFLASH_DRV_GetDefaultConfig)(flash_user_config_t *const config);

typedef struct {
#if defined (S32K116) || defined (S32K142)
tpfFLASH_DRV_EraseSector pfFLASH_DRV_EraseSector; /* erase sector flash memory */
tpfFLASH_DRV_Program pfFLASH_DRV_Program; /* program flash */
tpfFLASH_DRV_VerifySection pfFLASH_DRV_VerifySection; /* verify flash section */
tpfFLASH_DRV_GetDefaultConfig pfFLASH_DRV_GetDefaultConfig; /* get default config */
#elif defined (S32K144) || defined (S32K118)
// tpfFLASH_DRV_EraseAllBlock pfFLASH_DRV_EraseAllBlock;
// tpfFLASH_DRV_VerifyAllBlock pfFLASH_DRV_VerifyAllBlock;
tpfFLASH_DRV_EraseSector pfFLASH_DRV_EraseSector;
tpfFLASH_DRV_VerifySection pfFLASH_DRV_VerifySection;
// tpfFLASH_DRV_EraseSuspend pfFLASH_DRV_EraseSuspend;
// tpfFLASH_DRV_EraseResume pfFLASH_DRV_EraseResume;
tpfFLASH_DRV_Program pfFLASH_DRV_Program;
tpfFLASH_DRV_ProgramCheck pfFLASH_DRV_ProgramCheck;
// tpfFLASH_DRV_ProgramSection pfFLASH_DRV_ProgramSection;
// tpfFLASH_DRV_EraseBlock pfFLASH_DRV_EraseBlock;
// tpfFLASH_DRV_CommandSequence pfFLASH_DRV_CommandSequence;
// tpfFLASH_DRV_VerifyBlock pfFLASH_DRV_VerifyBlock;
#else
#error "请选择对应的芯片型号,即取消注释对应宏定义..."
#endif
typedef status_t (*tpfFLASH_DRV_EraseSector) (const flash_ssd_config_t *pSSDConfig, uint32_t dest, uint32_t size);
typedef status_t (*tpfFLASH_DRV_VerifySection) (const flash_ssd_config_t *pSSDConfig, uint32_t dest, uint16_t number, uint8_t marginLevel);
typedef status_t (*tpfFLASH_DRV_Program) (const flash_ssd_config_t *pSSDConfig, uint32_t dest, uint32_t size, const uint8_t *pData);
typedef status_t (*tpfFLASH_DRV_ProgramCheck) (const flash_ssd_config_t *pSSDConfig, uint32_t dest, uint32_t size, const uint8_t *pExpectedData, uint32_t *pFailAddr, uint8_t marginLevel);

typedef struct
{
tpfFLASH_DRV_EraseSector pfFLASH_DRV_EraseSector;
tpfFLASH_DRV_VerifySection pfFLASH_DRV_VerifySection;
tpfFLASH_DRV_Program pfFLASH_DRV_Program;
tpfFLASH_DRV_ProgramCheck pfFLASH_DRV_ProgramCheck;
} tFlashDriverAPIInfo;

void NVM_TestFlashDriver(void);

#endif /* NVM_FLASH_H_ */

/* -------------------------------------------- END OF FILE -------------------------------------------- */
31 changes: 13 additions & 18 deletions Sources/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
#include "Cpu.h"
#include "NVM_Flash.h"

extern void DTek_TestFlashDriver(void);

volatile int exit_code = 0;

/* User includes (#include below this line is not maintained by Processor Expert) */
Expand All @@ -41,31 +39,28 @@ volatile int exit_code = 0;
int main(void)
{
/* Write your local variable definition here */

/*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
#ifdef PEX_RTOS_INIT
PEX_RTOS_INIT(); /* Initialization of the selected RTOS. Macro is defined by the RTOS component. */
#endif
/*** End of Processor Expert internal initialization. ***/

/* Write your code here */
DTek_TestFlashDriver();
NVM_TestFlashDriver();
/* For example: for(;;) { } */

/*** Don't write any code pass this line, or it will be deleted during code generation. ***/
/*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
#ifdef PEX_RTOS_START
PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
#endif
/*** End of RTOS startup code. ***/
/*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
for (;;) {
if (exit_code != 0) {
break;
}
/*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
#ifdef PEX_RTOS_START
PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
#endif
/*** End of RTOS startup code. ***/
/*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
for(;;) {
if(exit_code != 0) {
break;
}
return exit_code;
/*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
}
return exit_code;
/*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

/* END main */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
for /R %%f in (*.c;*.h) do AStyle.exe -A3 -s4 -xV -S -w -Y -M60 -f -p -xg -H -k3 -W3 -xb -j -xf -xh -c -xC200 -n -v %%f
for /R %%f in (*.c;*.h) do AStyle.exe -A1 -s4 -xV -S -w -Y -M60 -f -p -xg -H -xe -k3 -W3 -xb -j -xf -xh -c -xC200 -n -v %%f
pause

0 comments on commit e223a29

Please sign in to comment.