ATtiny 3216的Flash空间共有32KB,其分区如图1所示。
图1 Flash空间分区
FLASHSTART作为程序储存库访问时,为0x0000。当作为数据内存访问时,为0x8000。BOOTEND和APPEND每页为256bytes(128words)。具体的分区结构设置如表2-1所示。
在Atmel Studio 7.0中,可以通过 crtl+shift+P 进入device programming界面进行Fuse的设置。如图2所示
图2 Fuse设置界面
或者通过以下代码实现。该代码应放在main函数前,独立放置即可。
/* Fuse configuration
* BOOTEND sets the size (end) of the boot section in blocks of 256 bytes.
* APPEND = 0x00 defines the section from BOOTEND*256 to end of Flash as application code.
* Remaining fuses have default configuration.
*/
FUSES = {
.OSCCFG = FREQSEL_20MHZ_gc,
.SYSCFG0 = CRCSRC_NOCRC_gc | RSTPINCFG_UPDI_gc,
.SYSCFG1 = SUT_64MS_gc,
.APPEND = 0x00,
.BOOTEND = 0x02 // Boot section size = 0x02 * 256 bytes = 512 bytes
};
划分好区域之后,通过FLASH驱动向APP CODE区写入代码,写入完需要的代码之后,跳转到APP CODE区开头,程序升级即完成。
1、Flash的代码地址是words形式,而不是bytes形式。其代码在Flash内的储存形式如图3所示
图3 Flash内的代码形式
而我们在BOOT区间通过Flash驱动往APP CODE区域写入代码是bytes形式的。如图4所示
图4 写入Flash的代码形式
因此从BOOT区域跳转到APP CODE区域开头的代码应该为
((void (*)(void))(BOOTSIZE >> 1))();
或
((void (*)(void))(BOOTSIZE / 2))();
//#define BOOTSIZE (256 * bootend)
2、在编译APP区的代码时,要让编译器知道在Flash何处放置编译后的应用程序代码,编译器.txt代码段的开始部分必须配置为与Flash段的位置相对应。
在Atme Studio7.0 中,可以通过如下方式设置(以BOOTEND = 0x02为例)。Project Properties (Alt+F7) → Toolchain → AVR/GNU Linker → Memory Settings, by adding .text=0x100 to the FLASH segment