/************************************************************************ Project: Welling Motor Control Paltform Filename: flash_master.c Partner Filename: flash_master.h Description: flash master driver Complier: IAR Embedded Workbench for ARM 8.40.2 CPU TYPE : STM32F30x ************************************************************************* Copyright (c) 20 Welling Motor Technology(Shanghai) Co. Ltd. All rights reserved. ************************************************************************* ************************************************************************* Revising History (ECL of this file): M0_20220410, by Ling Baizhou, create this file; ************************************************************************/ /************************************************************************ Beginning of File, do not put anything above here except notes Compiler Directives: *************************************************************************/ #ifndef _FLASH_MASTER_C #define _FLASH_MASTER_C_ #endif /************************************************************************ Included File *************************************************************************/ #include "flash_master.h" #include "gd32f30x.h" #include "queue.h" #include "string.h" /************************************************************************* Exported Functions (N/A) *************************************************************************/ /************************************************************************* Function: Description: Call by: Input Variables: Output/Return Variables: Subroutine Call: Reference: *************************************************************************/ FLASH_PARA_TYPE flash_stPara; UWORD dataIndex; //从指定地址开始写入多个数据 void FLASH_voWriteMoreData(ULONG startAddress, UWORD *writeData, UWORD countToWrite) { if (startAddress < FLASH_BASE || ((startAddress + countToWrite * 2) >= (FLASH_END))) { return; //非法地址 } fmc_unlock(); //解锁写保护 while (fmc_page_erase(startAddress) != FMC_READY) { ; } //擦除这个扇区 for (dataIndex = 0; dataIndex < countToWrite; dataIndex++) { fmc_halfword_program(startAddress + dataIndex * 2, writeData[dataIndex]); } fmc_lock();//上锁写保护 } //读取指定地址的半字(16位数据) UWORD FLASH_ReadWord(ULONG address) { return *(__IO UWORD *)address; } //从指定地址开始读取多个数据 void FLASH_voReadMoreData(ULONG startAddress, UWORD *readData, UWORD countToRead) { UWORD dataIndex; for (dataIndex = 0; dataIndex < sizeof(FLASH_PARA_TYPE) / 2; dataIndex++) { readData[dataIndex] = FLASH_ReadWord(startAddress + dataIndex * 2); } } void flash_voWrite(void) { UWORD count_len = sizeof(FLASH_PARA_TYPE) / 2; FLASH_voWriteMoreData(StartServerManageFlashAddress, (UWORD *)(&flash_stPara), count_len); } void flash_voRead(void) { UWORD count_len = sizeof(FLASH_PARA_TYPE) / 2; FLASH_voReadMoreData(StartServerManageFlashAddress, (UWORD *)(&flash_stPara), count_len); } void flash_voParaInit(void) { memset(&flash_stPara.ubMotorVersion, 0, sizeof(FLASH_PARA_TYPE)); SLONG Flash_TorqueAssGain[15][4] = TORQUE_ASSIST_DEFAULT; SLONG Flash_CadAssGain[5][4] = CADENCE_ASSIST_DEFAULT; memcpy(&flash_stPara.slTorqAssGain[0], &Flash_TorqueAssGain[0], sizeof(Flash_TorqueAssGain)); memcpy(&flash_stPara.slCadAssGain[0], &Flash_CadAssGain[0], sizeof(Flash_CadAssGain)); flash_voWrite(); } void flash_voErrorWrite(void) { UWORD count_len = sizeof(FLASH_ERROR_QUEUE) / 2; FLASH_voWriteMoreData(StartServerManageErrorFlashAddress, (UWORD *)(&stFlashErrorLog), count_len); } void flash_voErrorRead(void) { UWORD count_len = sizeof(FLASH_ERROR_QUEUE) / 2; FLASH_voReadMoreData(StartServerManageErrorFlashAddress, (UWORD *)(&stFlashErrorLog), count_len); if(stFlashErrorLog.ubHead == 0xFF) { que_voInit(&stFlashErrorLog); } }