STM32:片上Flash 操作
之前IAP时候记录过一些,今天对特定地方写又加深了印象,写与擦除都需要先unclock
1 //读取指定地址的半字(16位数据) 2 //faddr:读地址(此地址必须为2的倍数!!) 3 //返回值:对应数据. 4 u16 STMFLASH_ReadHalfWord(u32 faddr) 5 { 6 return *(vu16*)faddr; 7 }
1 //读取指定地址的字(32位数据) 2 //faddr:读地址(此地址必须为2的倍数!!) 3 //返回值:对应数据. 4 u32 STMFLASH_ReadOneWord(u32 faddr) 5 { 6 return *(vu32*)faddr; 7 }
1 //从指定地址开始读出指定长度的数据 2 //ReadAddr:起始地址 3 //pBuffer:数据指针 4 //NumToWrite:半字(16位)数 5 void STMFLASH_Read(u32 ReadAddr,u16 *pBuffer,u16 NumToRead) 6 { 7 u16 i; 8 for(i=0;i<NumToRead;i++) 9 { 10 pBuffer[i]=STMFLASH_ReadHalfWord(ReadAddr);//读取2个字节. 11 ReadAddr+=2;//偏移2个字节. 12 } 13 }
1 /从指定地址开始读出指定长度的数据 2 //ReadAddr:起始地址 3 //pBuffer:数据指针 4 //NumToWrite:字(32位)数 5 void STMFLASH_Read_Word(u32 ReadAddr,u32 *pBuffer,u32 NumToRead) 6 { 7 u32 i; 8 for(i=0;i<NumToRead;i++) 9 { 10 pBuffer[i]=STMFLASH_ReadOneWord(ReadAddr);//读取4个字节. 11 ReadAddr+=4;//偏移4个字节. 12 } 13 }
1 //不检查的写入 2 //WriteAddr:起始地址 3 //pBuffer:数据指针 4 //NumToWrite:半字(16位)数 5 void STMFLASH_Write_NoCheck(u32 WriteAddr,u16 *pBuffer,u16 NumToWrite) 6 { 7 u16 i; 8 FLASH_Unlock(); 9 for(i=0;i<NumToWrite;i++) 10 { 11 FLASH_ProgramHalfWord(WriteAddr,pBuffer[i]); 12 WriteAddr+=2;//地址增加2. 13 } 14 FLASH_Lock();//上锁 15 16 }
1 //不检查的写入 2 //WriteAddr:起始地址 3 //pBuffer:数据指针 4 //NumToWrite:半字(16位)数 5 void STMFLASH_Write_NoCheck_OneWord(u32 WriteAddr,u32 *pBuffer,u16 NumToWrite) 6 { 7 u16 i; 8 FLASH_Unlock(); 9 for(i=0;i<NumToWrite;i++) 10 { 11 FLASH_ProgramWord(WriteAddr,pBuffer[i]); 12 WriteAddr+=4;//地址增加2. 13 } 14 FLASH_Lock();//上锁 15 }
1 FLASH_Unlock(); 2 FLASH_ErasePage(u32 address); 3 FLASH_Lock();