我正在尝试为 ATmega AVR 编写一个定制的引导加载程序。我编写了一个代码,它在 ATmega32A 和 ATmega8A 等小型 AVR 中完美运行。但是当我想在 ATmega128A 中使用它时,它不会在闪存段中写入任何内容。我确定保险丝是正确的,ATmega103 模式被禁用,程序在引导部分启动,但它什么也不做。在调用函数“boot_program_page”之前,我设置了 PORTC 并打开了一些 LED,之后我清除了 PORTC 和 LED熄灭。所以代码也完全执行。我正在使用的函数是 avr/boot.h 中提供的示例。它应该在闪存的第 0 页中写入一些数据(实际上是 0x00)。这是我的代码:
#define F_CPU 8000000UL
#include <inttypes.h>
#include <avr/io.h>
#include <avr/boot.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
void boot_program_page (uint32_t page, uint8_t *buf);
int main(void)
{
uint8_t data[SPM_PAGESIZE] = {0};
uint32_t page = 0;
DDRC = 255;
PORTC = 255;
page *= (uint32_t)SPM_PAGESIZE;
boot_program_page(page,data);
_delay_ms(1000);
PORTC = 0;
while (1)
{
}
}
void boot_program_page (uint32_t page, uint8_t *buf)
{
uint16_t i;
uint8_t sreg;
// Disable interrupts.
sreg = SREG;
cli();
eeprom_busy_wait ();
boot_page_erase (page);
boot_spm_busy_wait (); // Wait until the memory is erased.
for (i=0; i<SPM_PAGESIZE; i+=2)
{
// Set up little-endian word.
uint16_t w = *buf++;
w += (*buf++) << 8;
boot_page_fill (page + i, w);
}
boot_page_write (page); // Store buffer in flash page.
boot_spm_busy_wait(); // Wait until the memory is written.
// Reenable RWW-section again. We need this if we want to jump back
// to the application after bootloading.
boot_rww_enable ();
// Re-enable interrupts (if they were ever enabled).
SREG = sreg;
}