You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The ATtiny13 supports "Self programming". This means that it can write to its own flash memory from within the running application, without the need of a bootloader like most ATmegas do.
With a little effort, I should be able to create a relatively portable and light-weight library for reading and writing any string or datatype to EEPROM, hopefully without having to use any RAM buffer. @SpenceKonde is this something you would be interested in for ATTinyCore as well?
#include<avr/boot.h>// Data to write to flashuint8_t data[32] = { "Hello from flash page two!"};
// Allocate two flash pages (2 x 2 bytes)
#defineNUMBER_OF_PAGES2// Allocated flash space (with default content)constuint8_t flashSpace[SPM_PAGESIZE * NUMBER_OF_PAGES] __attribute__ (( aligned(SPM_PAGESIZE) )) PROGMEM = {
"Default content page 1.........."
};
// Write data to flash where the flash space is passed as a parametervoidwrite_page(constuint8_t flash_space[], uint8_t page_number, uint8_t *data)
{
// Store SREGuint8_t old_sreg = SREG;
// Wait until ready and erase the pageeeprom_busy_wait();
boot_page_erase(&flash_space[page_number * SPM_PAGESIZE]);
// Wait until the page has been erasedboot_spm_busy_wait();
// Fill page with data from the bufferfor (uint8_t index = 0; index < SPM_PAGESIZE; index += 2)
boot_page_fill(&flash_space[page_number * SPM_PAGESIZE] + index, (data[index + 1] << 8) | data[index]);
// Write the data and wait for it to finishboot_page_write(&flash_space[page_number * SPM_PAGESIZE]);
boot_spm_busy_wait();
// Restore SREG
SREG = old_sreg;
}
voidsetup()
{
Serial.begin(115200);
write_page(flashSpace, 1, data);
uint8_t data;
Serial.print(F("Content Page 1:\n"));
for (uint8_t i = 0; i < 32; i++)
{
data = pgm_read_byte(flashSpace + i);
delay(100);
Serial.print(F("Addr: 0x"));
Serial.print((uint16_t)&flashSpace[i], HEX);
Serial.print(F("\tData: "));
Serial.println((char)data);
}
Serial.print(F("\nContent Page 2:\n"));
for (uint8_t i = 0; i < 32; i++)
{
data = pgm_read_byte(flashSpace + SPM_PAGESIZE + i);
delay(100);
Serial.print(F("Addr: 0x"));
Serial.print((uint16_t)&flashSpace[SPM_PAGESIZE + i], HEX);
Serial.print(F("\tData: "));
Serial.println((char)data);
}
}
voidloop()
{
}
Output:
Content Page 1:
Addr: 0x80 Data: D
Addr: 0x81 Data: e
Addr: 0x82 Data: f
Addr: 0x83 Data: a
Addr: 0x84 Data: u
Addr: 0x85 Data: l
Addr: 0x86 Data: t
Addr: 0x87 Data:
Addr: 0x88 Data: c
Addr: 0x89 Data: o
Addr: 0x8A Data: n
Addr: 0x8B Data: t
Addr: 0x8C Data: e
Addr: 0x8D Data: n
Addr: 0x8E Data: t
Addr: 0x8F Data:
Addr: 0x90 Data: p
Addr: 0x91 Data: a
Addr: 0x92 Data: g
Addr: 0x93 Data: e
Addr: 0x94 Data:
Addr: 0x95 Data: 1
Addr: 0x96 Data: .
Addr: 0x97 Data: .
Addr: 0x98 Data: .
Addr: 0x99 Data: .
Addr: 0x9A Data: .
Addr: 0x9B Data: .
Addr: 0x9C Data: .
Addr: 0x9D Data: .
Addr: 0x9E Data: .
Addr: 0x9F Data: .
Content Page 2:
Addr: 0xA0 Data: H
Addr: 0xA1 Data: e
Addr: 0xA2 Data: l
Addr: 0xA3 Data: l
Addr: 0xA4 Data: o
Addr: 0xA5 Data:
Addr: 0xA6 Data: f
Addr: 0xA7 Data: r
Addr: 0xA8 Data: o
Addr: 0xA9 Data: m
Addr: 0xAA Data:
Addr: 0xAB Data: f
Addr: 0xAC Data: l
Addr: 0xAD Data: a
Addr: 0xAE Data: s
Addr: 0xAF Data: h
Addr: 0xB0 Data:
Addr: 0xB1 Data: p
Addr: 0xB2 Data: a
Addr: 0xB3 Data: g
Addr: 0xB4 Data: e
Addr: 0xB5 Data:
Addr: 0xB6 Data: t
Addr: 0xB7 Data: w
Addr: 0xB8 Data: o
Addr: 0xB9 Data: !
Addr: 0xBA Data: �
Addr: 0xBB Data: �
Addr: 0xBC Data: �
Addr: 0xBD Data: �
Addr: 0xBE Data: �
Addr: 0xBF Data: �
The text was updated successfully, but these errors were encountered:
The ATtiny13 supports "Self programming". This means that it can write to its own flash memory from within the running application, without the need of a bootloader like most ATmegas do.
With a little effort, I should be able to create a relatively portable and light-weight library for reading and writing any string or datatype to EEPROM, hopefully without having to use any RAM buffer. @SpenceKonde is this something you would be interested in for ATTinyCore as well?
Output:
The text was updated successfully, but these errors were encountered: