ATMEGE32在bootloader區(qū)對Flash進行讀寫操作
官方提供了bootloader相關的寫入擦除函數(shù)。
主函數(shù)如下:
int main(void)
{
USARTInit(115200);
for (int i=0;i<256;i++)
{//此處數(shù)據(jù)均為00010203形式
if ((i&3)==0)
{
databuff.databuff8[i]=0;
}
else if ((i&3)==1)
{
databuff.databuff8[i]=1;
}
else if ((i&3)==2)
{
databuff.databuff8[i]=2;
}
else if ((i&3)==3)
{
databuff.databuff8[i]=3;
}
}
address = 0;
boot_write_one_page();
for (int i=0;i<256;i++)
{//此處數(shù)據(jù)均為00ff00ff形式
if (i&1)
{
databuff.databuff8[i]=0XFF;
}
else
{
databuff.databuff8[i]=0;
}
}
address += 256;
boot_write_one_page();
address = 0;
boot_check_flash();
address += 256;
boot_check_flash();
while(1);
}
下面是寫入一頁的程序:
void boot_write_one_page(void)
{
int i;
DataUnionByte TempData;
boot_page_erase(address);// 擦除一頁
boot_spm_busy_wait();// 等待擦除完成
for (i=0;i
{
TempData.databuff8[0]=databuff.databuff8[i];
TempData.databuff8[1]=databuff.databuff8[i+1];
boot_page_fill_safe(i,TempData.databuff16);
}
boot_page_write_safe(address);
boot_rww_enable_safe();
}
unsigned int read_program_memory (unsigned int adr ,unsigned char cmd)
{
asm("movw r30, r24");//復制地址到z寄存器
asm("SBRC r22, 0");
asm("STS 0x37, r22");//r22->0x37(SPMCR)
asm("LPM r24, Z+");
asm("LPM r25, Z");//獲取返回數(shù)據(jù)
asm("ret");
}
下面是對flash數(shù)據(jù)進行讀取并串口打印的函數(shù)
void boot_check_flash(void)
{
DataUnionByte TempData;
for (int i=0;i<128;i+=2)
{
TempData.databuff16 = read_program_memory(address+i,0x00);
USARTTransmit(TempData.databuff8[0]);
USARTTransmit(TempData.databuff8[1]);
}
}
運行結(jié)果如下:
評論