`1、新建一个RAM工程,新建过程请看本人发的第一篇帖子
2、调用和我们芯片相应的flash_API,flash烧写的程序如下:
- SECTOR_ST Sector[128] ={
- {(Uint32 *)0x700000,(Uint32 *)0x7007FF},
- {(Uint32 *)0x700800,(Uint32 *)0x700FFF},
- {(Uint32 *)0x701000,(Uint32 *)0x7017FF},
- {(Uint32 *)0x701800,(Uint32 *)0x701FFF},
- {(Uint32 *)0x702000,(Uint32 *)0x7027FF},
- {(Uint32 *)0x702800,(Uint32 *)0x702FFF},
- {(Uint32 *)0x703000,(Uint32 *)0x7037FF},
- {(Uint32 *)0x703800,(Uint32 *)0x703FFF},
- {(Uint32 *)0x704000,(Uint32 *)0x7047FF},
- {(Uint32 *)0x704800,(Uint32 *)0x704FFF},
- {(Uint32 *)0x705000,(Uint32 *)0x7057FF},
- {(Uint32 *)0x705800,(Uint32 *)0x705FFF},
- {(Uint32 *)0x706000,(Uint32 *)0x7067FF},
- {(Uint32 *)0x706800,(Uint32 *)0x706FFF},
- {(Uint32 *)0x707000,(Uint32 *)0x7077FF},
- {(Uint32 *)0x707800,(Uint32 *)0x707FFF},
- {(Uint32 *)0x708000,(Uint32 *)0x7087FF},
- {(Uint32 *)0x708800,(Uint32 *)0x708FFF},
- {(Uint32 *)0x709000,(Uint32 *)0x7097FF},
- {(Uint32 *)0x709800,(Uint32 *)0x709FFF},
- {(Uint32 *)0x70A000,(Uint32 *)0x70A7FF},
- {(Uint32 *)0x70A800,(Uint32 *)0x70AFFF},
- {(Uint32 *)0x70B000,(Uint32 *)0x70B7FF},
- {(Uint32 *)0x70B800,(Uint32 *)0x70BFFF},
- {(Uint32 *)0x70C000,(Uint32 *)0x70C7FF},
- {(Uint32 *)0x70C800,(Uint32 *)0x70CFFF},
- {(Uint32 *)0x70D000,(Uint32 *)0x70D7FF},
- {(Uint32 *)0x70D800,(Uint32 *)0x70DFFF},
- {(Uint32 *)0x70E000,(Uint32 *)0x70E7FF},
- {(Uint32 *)0x70E800,(Uint32 *)0x70EFFF},
- {(Uint32 *)0x70F000,(Uint32 *)0x70F7FF},
- {(Uint32 *)0x70F800,(Uint32 *)0x70FFFF},
- {(Uint32 *)0x710000,(Uint32 *)0x7107FF},
- {(Uint32 *)0x710800,(Uint32 *)0x710FFF},
- {(Uint32 *)0x711000,(Uint32 *)0x7117FF},
- {(Uint32 *)0x711800,(Uint32 *)0x711FFF},
- {(Uint32 *)0x712000,(Uint32 *)0x7127FF},
- {(Uint32 *)0x712800,(Uint32 *)0x712FFF},
- {(Uint32 *)0x713000,(Uint32 *)0x7137FF},
- {(Uint32 *)0x713800,(Uint32 *)0x713FFF},
- {(Uint32 *)0x714000,(Uint32 *)0x7147FF},
- {(Uint32 *)0x714800,(Uint32 *)0x714FFF},
- {(Uint32 *)0x715000,(Uint32 *)0x7157FF},
- {(Uint32 *)0x715800,(Uint32 *)0x715FFF},
- {(Uint32 *)0x716000,(Uint32 *)0x7167FF},
- {(Uint32 *)0x716800,(Uint32 *)0x716FFF},
- {(Uint32 *)0x717000,(Uint32 *)0x7177FF},
- {(Uint32 *)0x717800,(Uint32 *)0x717FFF},
- {(Uint32 *)0x718000,(Uint32 *)0x7187FF},
- {(Uint32 *)0x718800,(Uint32 *)0x718FFF},
- {(Uint32 *)0x719000,(Uint32 *)0x7197FF},
- {(Uint32 *)0x719800,(Uint32 *)0x719FFF},
- {(Uint32 *)0x71A000,(Uint32 *)0x71A7FF},
- {(Uint32 *)0x71A800,(Uint32 *)0x71AFFF},
- {(Uint32 *)0x71B000,(Uint32 *)0x71B7FF},
- {(Uint32 *)0x71B800,(Uint32 *)0x71BFFF},
- {(Uint32 *)0x71C000,(Uint32 *)0x71C7FF},
- {(Uint32 *)0x71C800,(Uint32 *)0x71CFFF},
- {(Uint32 *)0x71D000,(Uint32 *)0x71D7FF},
- {(Uint32 *)0x71D800,(Uint32 *)0x71DFFF},
- {(Uint32 *)0x71E000,(Uint32 *)0x71E7FF},
- {(Uint32 *)0x71E800,(Uint32 *)0x71EFFF},
- {(Uint32 *)0x71F000,(Uint32 *)0x71F7FF},
- {(Uint32 *)0x71F800,(Uint32 *)0x71FFFF},
- {(Uint32 *)0x720000,(Uint32 *)0x7207FF},
- {(Uint32 *)0x720800,(Uint32 *)0x720FFF},
- {(Uint32 *)0x721000,(Uint32 *)0x7217FF},
- {(Uint32 *)0x721800,(Uint32 *)0x721FFF},
- {(Uint32 *)0x722000,(Uint32 *)0x7227FF},
- {(Uint32 *)0x722800,(Uint32 *)0x722FFF},
- {(Uint32 *)0x723000,(Uint32 *)0x7237FF},
- {(Uint32 *)0x723800,(Uint32 *)0x723FFF},
- {(Uint32 *)0x724000,(Uint32 *)0x7247FF},
- {(Uint32 *)0x724800,(Uint32 *)0x724FFF},
- {(Uint32 *)0x725000,(Uint32 *)0x7257FF},
- {(Uint32 *)0x725800,(Uint32 *)0x725FFF},
- {(Uint32 *)0x726000,(Uint32 *)0x7267FF},
- {(Uint32 *)0x726800,(Uint32 *)0x726FFF},
- {(Uint32 *)0x727000,(Uint32 *)0x7277FF},
- {(Uint32 *)0x727800,(Uint32 *)0x727FFF},
- {(Uint32 *)0x728000,(Uint32 *)0x7287FF},
- {(Uint32 *)0x728800,(Uint32 *)0x728FFF},
- {(Uint32 *)0x729000,(Uint32 *)0x7297FF},
- {(Uint32 *)0x729800,(Uint32 *)0x729FFF},
- {(Uint32 *)0x72A000,(Uint32 *)0x72A7FF},
- {(Uint32 *)0x72A800,(Uint32 *)0x72AFFF},
- {(Uint32 *)0x72B000,(Uint32 *)0x72B7FF},
- {(Uint32 *)0x72B800,(Uint32 *)0x72BFFF},
- {(Uint32 *)0x72C000,(Uint32 *)0x72C7FF},
- {(Uint32 *)0x72C800,(Uint32 *)0x72CFFF},
- {(Uint32 *)0x72D000,(Uint32 *)0x72D7FF},
- {(Uint32 *)0x72D800,(Uint32 *)0x72DFFF},
- {(Uint32 *)0x72E000,(Uint32 *)0x72E7FF},
- {(Uint32 *)0x72E800,(Uint32 *)0x72EFFF},
- {(Uint32 *)0x72F000,(Uint32 *)0x72F7FF},
- {(Uint32 *)0x72F800,(Uint32 *)0x72FFFF},
- {(Uint32 *)0x730000,(Uint32 *)0x7307FF},
- {(Uint32 *)0x730800,(Uint32 *)0x730FFF},
- {(Uint32 *)0x731000,(Uint32 *)0x7317FF},
- {(Uint32 *)0x731800,(Uint32 *)0x731FFF},
- {(Uint32 *)0x732000,(Uint32 *)0x7327FF},
- {(Uint32 *)0x732800,(Uint32 *)0x732FFF},
- {(Uint32 *)0x733000,(Uint32 *)0x7337FF},
- {(Uint32 *)0x733800,(Uint32 *)0x733FFF},
- {(Uint32 *)0x734000,(Uint32 *)0x7347FF},
- {(Uint32 *)0x734800,(Uint32 *)0x734FFF},
- {(Uint32 *)0x735000,(Uint32 *)0x7357FF},
- {(Uint32 *)0x735800,(Uint32 *)0x735FFF},
- {(Uint32 *)0x736000,(Uint32 *)0x7367FF},
- {(Uint32 *)0x736800,(Uint32 *)0x736FFF},
- {(Uint32 *)0x737000,(Uint32 *)0x7377FF},
- {(Uint32 *)0x737800,(Uint32 *)0x737FFF},
- {(Uint32 *)0x738000,(Uint32 *)0x7387FF},
- {(Uint32 *)0x738800,(Uint32 *)0x738FFF},
- {(Uint32 *)0x739000,(Uint32 *)0x7397FF},
- {(Uint32 *)0x739800,(Uint32 *)0x739FFF},
- {(Uint32 *)0x73A000,(Uint32 *)0x73A7FF},
- {(Uint32 *)0x73A800,(Uint32 *)0x73AFFF},
- {(Uint32 *)0x73B000,(Uint32 *)0x73B7FF},
- {(Uint32 *)0x73B800,(Uint32 *)0x73BFFF},
- {(Uint32 *)0x73C000,(Uint32 *)0x73C7FF},
- {(Uint32 *)0x73C800,(Uint32 *)0x73CFFF},
- {(Uint32 *)0x73D000,(Uint32 *)0x73D7FF},
- {(Uint32 *)0x73D800,(Uint32 *)0x73DFFF},
- {(Uint32 *)0x73E000,(Uint32 *)0x73E7FF},
- {(Uint32 *)0x73E800,(Uint32 *)0x73EFFF},
- {(Uint32 *)0x73F000,(Uint32 *)0x73F7FF},
- {(Uint32 *)0x73F800,(Uint32 *)0x73FFFF},
- };
- //
- // Flash Status Structure
- //
- FLASH_ST FlashStatus;
- FLASH_ST FlashProgStatus,FlashEraseStatus;
- //Erase the whole chip, including FLASH & OTP
- Uint16 chip_erase(){
- Uint16 Status;
- Status = Flash28034_ChipErase(&FlashStatus);
- if(Status != SUCCESS)
- return FAIL;
- else
- return SUCCESS;
- }
- //Erase only 1 Sector once, sector_ide: 0-127,total 128 Sectors
- Uint16 erase(int sector_idx){
- Uint16 Status;
- Status = Flash28034_Erase(sector_idx, &FlashStatus);
- if(Status != SUCCESS)
- return FAIL;
- else
- return SUCCESS;
- }
- //Writing Data to Flash Space
- Uint16 flash_program(Uint32 *FlashAddr, Uint32 *BufAddr, Uint32 Length, FLASH_ST *FProgStatus){
- Uint16 Status;
- Status = Flash28034_Program((Uint32 *) FlashAddr, (Uint32 *)BufAddr, Length, FProgStatus);
- if(Status != SUCCESS)
- return FAIL;
- else
- return SUCCESS;
- }
- //Writing Data to OTP Space
- Uint16 otp_program(Uint32 *FlashAddr, Uint32 *BufAddr, Uint32 Length, FLASH_ST *FProgStatus){
- Uint16 Status;
- Status = Flash28034_OtpProgram((Uint32 *) FlashAddr, (Uint32 *)BufAddr, Length, FProgStatus);
- if(Status != SUCCESS)
- return FAIL;
- else
- return SUCCESS;
- }
- Uint16 Flash28034_Program(Uint32 *FlashAddr, Uint32*BufAddr, Uint32 Length, FLASH_ST *FProgStatus){
- Uint32 i;
- FLASH_ST VerifyStatus;
- FLASH_ST ProgVerifyStat;
- Uint16 Verify_Status;
- Uint32 Clock_T, FPROWAIT, DivSel, PllByPass;
- //CSM Unlock
- if(P_FlashRegs->SYSCsmRegs.CSMSCR.bit.SECURE == 1) return STATUS_FAIL_CSM_LOCKED;
- //FlashAddr Invalid
- if(((Uint32)(FlashAddr) < 0x700000) || ((Uint32)(FlashAddr) >= 0x73FFFF) || (((Uint32)(FlashAddr) + Length) >= 0x73FFFF)) return STATUS_FAIL_ADDR_INVALID;
- //Flash has been Written
- if(Flash28034_ProgVerify(FlashAddr,Length,&ProgVerifyStat) != SUCCESS) return STATUS_FAIL_ZERO_BIT_ERROR;
- EALLOW;
- P_FlashRegs->FMEMWREN.bit.FMEMWREN = 1;
- P_FlashRegs->FPROWAIT = FPROWAIT_120M; // OSC_CLK / 4
- P_FlashRegs->FNVSHWAIT = FNVSHWAIT_120M; //
- P_FlashRegs->FPGSWAIT = FPGSWAIT_120M; //
- P_FlashRegs->FPERWAIT = FPERWAIT_120M; // 20ms/T
- EDIS;
- for(i=0; i
- //while(P_FlashRegs->FSTAT.bit.BUSY || P_FlashRegs->FSTAT.bit.PROGRAMING || P_FlashRegs->FSTAT.bit.ERASEING || P_FlashRegs->FSTAT.bit.READING || P_FlashRegs->FSTAT.bit.MASS_ERASE);
- *(Uint32 *)(FlashAddr + i) = *(BufAddr+i);
- }
- while(P_FlashRegs->FSTAT.bit.BUSY || P_FlashRegs->FSTAT.bit.PROGRAMING);
- Verify_Status = Flash28034_Verify(FlashAddr,BufAddr,Length,&VerifyStatus);
- if(Verify_Status == SUCCESS)
- return SUCCESS;
- else
- return STATUS_FAIL_PROGRAM;
- }
- Uint16 Flash28034_OtpProgram(Uint32 *OtpAddr, Uint32*BufAddr, Uint32 Length, FLASH_ST *FProgStatus){
- Uint32 i;
- FLASH_ST VerifyStatus;
- FLASH_ST ProgVerifyStat;
- Uint16 Verify_Status;
- Uint32 Clock_T, FPROWAIT, DivSel, PllByPass;
- //CSM Unlock
- if(P_FlashRegs->SYSCsmRegs.CSMSCR.bit.SECURE == 1) return STATUS_FAIL_CSM_LOCKED;
- //FlashAddr Invalid
- if(((Uint32)(OtpAddr) < 0x7A0000) || ((Uint32)(OtpAddr) >= 0x7A3C00) || (((Uint32)(OtpAddr) + Length) >= 0x7A3C00)) return STATUS_FAIL_ADDR_INVALID;
- //Flash has been Written
- if(Flash28034_ProgVerify(OtpAddr,Length,&ProgVerifyStat) != SUCCESS) return STATUS_FAIL_ZERO_BIT_ERROR;
- //Flash has been Written
- if(Length%4 != 0) return STATUS_FAIL_PROGRAM;
- if((Uint32)(OtpAddr)%4 != 0) return STATUS_FAIL_PROGRAM;
- EALLOW;
- P_FlashRegs->OTPWREN.bit.OTPWREN = 1;
- P_FlashRegs->FPROWAIT = FPROWAIT_120M; // OSC_CLK / 4
- P_FlashRegs->FNVSHWAIT = FNVSHWAIT_120M; //
- P_FlashRegs->FPGSWAIT = FPGSWAIT_120M; //
- P_FlashRegs->FPERWAIT = FPERWAIT_120M; // 20ms/T
- EDIS;
- for(i=0; i
- //while(P_FlashRegs->FSTAT.bit.BUSY || P_FlashRegs->FSTAT.bit.PROGRAMING || P_FlashRegs->FSTAT.bit.ERASEING || P_FlashRegs->FSTAT.bit.READING || P_FlashRegs->FSTAT.bit.MASS_ERASE);
- *(Uint32 *)(OtpAddr + i) = *(BufAddr + i);
- *(Uint32 *)(OtpAddr + i + 1) = *(BufAddr + i + 1);
- *(Uint32 *)(OtpAddr + i + 2) = *(BufAddr + i + 2);
- *(Uint32 *)(OtpAddr + i + 3) = *(BufAddr + i + 3);
- }
- while(P_FlashRegs->OTPSTAT.bit.OTPSTAT_WRITE);
- Verify_Status = Flash28034_Verify(OtpAddr,BufAddr,Length,&VerifyStatus);
- if(Verify_Status == SUCCESS)
- return SUCCESS;
- else
- return STATUS_FAIL_PROGRAM;
- }
- Uint16 Flash28034_Verify(Uint32 *FlashAddr, Uint32 *BufAddr, Uint32 Length, FLASH_ST *FVerifyStat){
- Uint32 i, ECnt;
- ECnt = 0;
- for(i=0;i
- if(*(BufAddr+i) != *(FlashAddr + i)) ECnt++;
- }
- if(ECnt == 0) return SUCCESS;
- else return STATUS_FAIL_VERIFY;
- }
- Uint16 Flash28034_ProgVerify(Uint32 *FlashAddr, Uint32 Length, FLASH_ST *FVerifyStat){
- Uint32 i, ECnt;
- ECnt = 0;
- for(i=0;i
- if(0xFFFFFFFF != *(FlashAddr + i)) ECnt++;
- }
- if(ECnt == 0) return SUCCESS;
- else return STATUS_FAIL_ZERO_BIT_ERROR;
- }
- Uint16 Flash28034_EraseVerify(Uint32 *FlashAddr, Uint32 Length, FLASH_ST *FVerifyStat){
- Uint32 i, ECnt;
- ECnt = 0;
- for(i=0;i
- if((Uint32)(FlashAddr + i) < 0x73FFF0){
- if(0xFFFFFFFF != *(FlashAddr + i)) ECnt++;
- }
- }
- if(ECnt == 0) return SUCCESS;
- else return STATUS_FAIL_ERASE;
- }
- Uint16 Flash28034_Erase(Uint32 SectorIdx, FLASH_ST *FEraseStat){
- Uint32 i;
- FLASH_ST EraseVerifyStatus;
- Uint32 FPROWAIT, DivSel, PllByPass;
- Uint16 EraseVerify_Status;
- //CSM Unlock
- if(P_FlashRegs->SYSCsmRegs.CSMSCR.bit.SECURE == 1) return STATUS_FAIL_CSM_LOCKED;
- if(SectorIdx < 0x0 || SectorIdx > 0x7f) return STATUS_FAIL_NO_SECTOR_SPECIFIED;
- EALLOW;
- P_FlashRegs->FNVSHWAIT = FNVSHWAIT_120M; // 5us/T
- P_FlashRegs->FPERWAIT = FPERWAIT_120M; // 20ms/T
- EDIS;
- EALLOW;
- P_FlashRegs->FPERCTL.bit.erase = SectorIdx;
- EDIS;
- while(P_FlashRegs->FSTAT.bit.ERASEING);
- //Verify Each Sector, Length = 512 * 4B
- EraseVerify_Status = Flash28034_EraseVerify(Sector[SectorIdx].StartAddr,512,&EraseVerifyStatus);
- if(EraseVerify_Status != SUCCESS) return STATUS_FAIL_ERASE;
- return SUCCESS;
- }//End of Flash28034_Erase
- Uint16 Flash28034_ChipErase(FLASH_ST *FEraseStat){
- Uint32 i,SectorIdx;
- FLASH_ST EraseVerifyStatus;
- Uint32 FPROWAIT, DivSel, PllByPass;
- Uint16 EraseVerify_Status;
- //Obtain Current Clock Period
- EALLOW;
- DivSel = P_SysCtrlRegs->PLLSTS.bit.DIVSEL;
- PllByPass = P_SysCtrlRegs->PLLCR.bit.DIV;
- EDIS;
- if(DivSel != 0x0 && DivSel != 0x1) return STATUS_FAIL_ERASE;
- if(PllByPass != 0x0) return STATUS_FAIL_ERASE;
- //CSM Unlock
- if(P_FlashRegs->SYSCsmRegs.CSMSCR.bit.SECURE == 1) return STATUS_FAIL_CSM_LOCKED;
- EALLOW;
- P_FlashRegs->FNVSHWAIT = FNVSHWAIT_120M; //
- P_FlashRegs->FNVH1WAIT = FNVH1WAIT_120M; // 100us/T
- P_FlashRegs->FMERWAIT = FMERWAIT_120M; // 20ms/T
- EDIS;
- EALLOW;
- P_FlashRegs->OTPER = 1;
- EDIS;
- while(P_FlashRegs->OTPSTAT.bit.OTPSTAT_ERASE);
- //Verify Flash
- for(SectorIdx = 0; SectorIdx < 128; SectorIdx++){
- //Verify Each Sector, Length = 512 * 4B
- EraseVerify_Status = Flash28034_EraseVerify(Sector[SectorIdx].StartAddr,512,&EraseVerifyStatus);
- if(EraseVerify_Status != SUCCESS) return STATUS_FAIL_ERASE;
- }
- //Verify OTP
- EraseVerify_Status = Flash28034_EraseVerify((Uint32*)0x7A0000,3840,&EraseVerifyStatus);
- if(EraseVerify_Status != SUCCESS) return STATUS_FAIL_ERASE;
- return SUCCESS;
- }//End of Flash28034_ChipErase
复制代码
3、主函数调用flash_api,将写入得数据与读出的数据对比,写入读出的对比正确,说明,flash烧写成功。
- /******************************************************************
- 文 档 名:HX_EXAM_DSC28034_LED_Blink_Flash_EW_V0.0.1
- D S P:dsc28034
- IDE:haawking-workspace-V0.5.0
- 作 用:调用Flash API实现对片内的flash的读写擦除等操作。
- 实验现象:读写正确的时候,D400亮。否则,D402闪。
- 版 本:V0.0.1
- 时 间:2021年3月3日 15:48
- 作 者:shentingting
- [url=home.php?mod=space&uid=136761]@mail[/url] :tingting.shen@mail.haawking.com
- ******************************************************************/
-
- #include
- #include "dsc_config.h"
- #include "hx_rv32_dev.h"
- #include "hx_rv32_type.h"
- #include
- #include "xcustom.h"
- #include "IQmathLib.h"
- #include "flash.h"
- void GPIO_INIT(){
- EALLOW;
- GpioCtrlRegs.GPBDIR.bit.GPIO43 = 1;
- GpioDataRegs.GPBDAT.bit.GPIO43 = 1;
- GpioCtrlRegs.GPBDIR.bit.GPIO41 = 1;
- GpioDataRegs.GPBSET.bit.GPIO41 = 1;
- EDIS;
- }
- #define LEN 16
- Uint32 Buffer[LEN] = {1,2,3,4,5,6,7,8,11,12,13,14,15,16};
- Uint32 valirary;
- int main(void)
- {
- int temp, in;
- volatile unsigned int i;
- Uint16 status;
- FLASH_ST FlashProgStatus;
- EALLOW;
- P_SysCtrlRegs->INTOSC1TRIM.bit.FINETRIM = 0;
- P_SysCtrlRegs->INTOSC2TRIM.bit.FINETRIM = 0;
- EDIS;
- //DisableDog();
- InitSysCtrl(); //60MHz
- //We shall Unlock the chip when running from JTAG Mode
- CsmUnlock();
- GPIO_INIT();
- //Erase 126th Sector
- status = erase(126);
- //Write Buffer[LEN] to 126th Sector, From the beginning Address,0x73F000
- status = flash_program(Sector[126].StartAddr,Buffer,LEN,&FlashProgStatus);
- temp = 0;
- for(i=0;i
- valirary = (*(Uint32*)(Sector[126].StartAddr + i));
- //Read form 126th Sector, check whether we have written correctly
- if(valirary != Buffer[i])
- temp++;
- }
- while(1){
- if(temp == 0)
- {
- GpioDataRegs.GPBCLEAR.bit.GPIO41 = 1;
- }
- else
- {
- GpioDataRegs.GPBDAT.bit.GPIO43 = 0;
- for(i=0;i<120000;i++);
- GpioDataRegs.GPBDAT.bit.GPIO43 = 1;
- for(i=0;i<120000;i++);
- }
- }
- return 0;
- }
- // ----------------------------------------------------------------------------
复制代码
4、实验现象
在程序中temp变量为0的时候,GPIO41为低,D400常亮,表示flash的读写是相等的
`
|