Skip to content

Create a tiny flash write library #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MCUdude opened this issue Aug 27, 2021 · 0 comments
Closed

Create a tiny flash write library #131

MCUdude opened this issue Aug 27, 2021 · 0 comments

Comments

@MCUdude
Copy link
Owner

MCUdude commented Aug 27, 2021

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 flash
uint8_t data[32] = { "Hello from flash page two!"};

// Allocate two flash pages (2 x 2 bytes)
#define NUMBER_OF_PAGES  2

// Allocated flash space (with default content)
const uint8_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 parameter
void write_page(const uint8_t flash_space[], uint8_t page_number, uint8_t *data)
{
  // Store SREG
  uint8_t old_sreg = SREG;

  // Wait until ready and erase the page
  eeprom_busy_wait();
  boot_page_erase(&flash_space[page_number * SPM_PAGESIZE]);

  // Wait until the page has been erased
  boot_spm_busy_wait();

  // Fill page with data from the buffer
  for (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 finish
  boot_page_write(&flash_space[page_number * SPM_PAGESIZE]);
  boot_spm_busy_wait();

  // Restore SREG
  SREG = old_sreg;
}


void setup()
{
  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);
  }
}

void loop()
{
}

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: �
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant