avreal32.exe -ab -p1 +mega32 -w -fCKSEL=0x0F,JTAGEN=1
А это процедура:
#ifdef BootLoad_
void SelfReprogramming(void)@"MYCODE"
{
char R_W_E = 0;
// unsigned char __flash * address;
unsigned short size,address,page;
// long address,page;
unsigned int data, tempaddress;
_CLI();
WaitToReceive
address = UDR;
WaitToReceive
address |= (UDR<<8);
WaitToReceive
size = UDR;
WaitToReceive
size |= (UDR<<8);
WaitToReceive
R_W_E |= UDR;
WaitToReceive
if (UDR == 0x5A)
{
if (R_W_E == 'R')
for (int i = 0; i<size; i++)
{
WaitToSend;
UDR = __load_program_memory((unsigned char __flash *)address+i);
}
if (R_W_E == 'W')
{ // NOTE: For flash programming, 'address' is given in words.
for(page = address; page < address+size ;page += 128)
{ // NOTE: Here we use address as a byte-address, not word-address, for convenience.
_WAIT_FOR_SPM();
_PAGE_ERASE((unsigned char __flash *)page);
_WAIT_FOR_SPM();
_ENABLE_RWW_SECTION();
}
UDR = 'E';
WaitToSend;
tempaddress = address; // Store address in page.
do
{
WaitToReceive
data = UDR;
WaitToReceive
data |= (UDR << 8);
_FILL_TEMP_WORD((unsigned char __flash *)address,data);
(address)+=2; // Select next word in memory.
size -= 2; // Reduce number of bytes to write by two.
if (tempaddress == address-128)
{
_PAGE_WRITE(tempaddress);
_WAIT_FOR_SPM();
_ENABLE_RWW_SECTION();
tempaddress+=128;
PORTA = (PORTA^(1<<PA2));
flag.Lg = ~flag.Lg;
}
}
while(size); // Loop until all bytes written.
_PAGE_WRITE(tempaddress);
_WAIT_FOR_SPM();
_ENABLE_RWW_SECTION();
_ENABLE_RWW_SECTION();
funcptr();
}
if (R_W_E == 'E')
{ // NOTE: For flash programming, 'address' is given in words.
_WAIT_FOR_SPM();
_PAGE_ERASE((unsigned char __flash *)address);
_WAIT_FOR_SPM();
_ENABLE_RWW_SECTION();
}
}
_SEI();
}
#endif