Skip to content

Commit

Permalink
Update Optiboot Flash library
Browse files Browse the repository at this point in the history
  • Loading branch information
MCUdude committed Apr 4, 2021
1 parent 4ae60e2 commit 80b938c
Show file tree
Hide file tree
Showing 11 changed files with 1,021 additions and 217 deletions.
40 changes: 21 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# MiniCore
[![Build Status](https://travis-ci.org/MCUdude/MiniCore.svg?branch=master)](https://travis-ci.org/MCUdude/MiniCore) [![MiniCore forum thread](https://img.shields.io/badge/support-forum-blue.svg)](https://forum.arduino.cc/index.php?topic=412070.0)
[![Build Status](https://travis-ci.org/MCUdude/MiniCore.svg?branch=master)](https://travis-ci.org/MCUdude/MiniCore) [![MiniCore forum thread](https://img.shields.io/badge/support-forum-blue.svg)](https://forum.arduino.cc/index.php?topic=412070.0)

An Arduino core for the ATmega328, ATmega168, ATmega88, ATmega48 and ATmega8, all running a [custom version of Optiboot for increased functionality](#write-to-own-flash). This core requires at least Arduino IDE v1.6.2, where v1.8.5+ is recommended. <br/>
**This core gives you two extra IO pins if you're using the internal oscillator!** PB6 and PB7 is mapped to [Arduino pin 20 and 21](#pinout).<br/>
Expand All @@ -15,14 +15,14 @@ If you're into "generic" AVR programming, I'm happy to tell you that all relevan
* [Link time optimization / LTO](#link-time-optimization--lto)
* [Printf support](#printf-support)
* [Pin macros](#pin-macros)
* [Programmers](#programmers)
* [Write to own flash](#write-to-own-flash)
* [Programmers](#programmers)
* **[How to install](#how-to-install)**
- [Boards Manager Installation](#boards-manager-installation)
- [Manual Installation](#manual-installation)
- [PlatformIO](#platformio)
* **[Getting started with MiniCore](#getting-started-with-minicore)**
* [Wiring reference](#wiring-reference)
* [Wiring reference](#wiring-reference)
* **[Pinout](#pinout)**
* **[Minimal setup](#minimal-setup)**

Expand All @@ -44,13 +44,13 @@ Can't decide what microcontroller to choose? Have a look at the specification ta
| **RAM** | 2kB | 1kB | 1kB | 512B | 1kB |
| **EEPROM** | 1kB | 512B | 512B | 256B | 512B |
| **PWM pins** | 6/9<b>*</b> | 6 | 6 | 6 | 3 |

<b>*</b> ATmega328PB has 9 PWM pins


## Supported clock frequencies
MiniCore supports a variety of different clock frequencies. Select the microcontroller in the boards menu, then select the clock frequency. You'll have to hit "Burn bootloader" in order to set the correct fuses and upload the correct bootloader.
Make sure you connect an ISP programmer, and select the correct one in the "Programmers" menu. For time critical operations an external crystal/oscillator is recommended.
MiniCore supports a variety of different clock frequencies. Select the microcontroller in the boards menu, then select the clock frequency. You'll have to hit "Burn bootloader" in order to set the correct fuses and upload the correct bootloader.
Make sure you connect an ISP programmer, and select the correct one in the "Programmers" menu. For time critical operations an external crystal/oscillator is recommended.

You might experience upload issues when using the internal oscillator. It's factory calibrated but may be a little "off" depending on the calibration, ambient temperature and operating voltage. If uploading failes while using the 8 MHz internal oscillator you have these options:
* Edit the baudrate line in the boards.txt file, and choose either 115200, 57600, 38400 or 19200 baud.
Expand Down Expand Up @@ -102,7 +102,7 @@ If you want the EEPROM to be erased every time you burn the bootloader or upload


## Link time optimization / LTO
After Arduino IDE 1.6.11 where released, There have been support for link time optimization or LTO for short. The LTO optimizes the code at link time, making the code (often) significantly smaller without making it "slower". In Arduino IDE 1.6.11 and newer LTO is enabled by default. I've chosen to disable this by default to make sure the core keep its backwards compatibility. Enabling LTO in IDE 1.6.10 or older will return an error.
After Arduino IDE 1.6.11 where released, There have been support for link time optimization or LTO for short. The LTO optimizes the code at link time, making the code (often) significantly smaller without making it "slower". In Arduino IDE 1.6.11 and newer LTO is enabled by default. I've chosen to disable this by default to make sure the core keep its backwards compatibility. Enabling LTO in IDE 1.6.10 or older will return an error.
I encourage you to try the new LTO option and see how much smaller your code gets! Note that you don't need to hit "Burn Bootloader" in order to enable LTO. Simply enable it in the "Tools" menu, and your code is ready for compilation. If you want to read more about LTO and GCC flags in general, head over to the [GNU GCC website](https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html)!


Expand All @@ -125,17 +125,19 @@ digitalWrite(13, HIGH);
```
## Write to own flash
MiniCore uses Optiboot Flash, a bootloader that supports flash writing within the running application, thanks to the work of [@majekw](https://github.com/majekw).
This means that content from e.g. a sensor can be stored in the flash memory directly without the need of external memory. Flash memory is much faster than EEPROM, and can handle at least 10 000 write cycles before wear becomes an issue.
For more information on how it works and how you can use this in you own application, check out the [Serial_read_write](https://github.com/MCUdude/MiniCore/blob/master/avr/libraries/Optiboot_flasher/examples/Serial_read_write/Serial_read_write.ino) for a simple proof-of-concept demo, and
[Flash_put_get](https://github.com/MCUdude/MiniCore/blob/master/avr/libraries/Optiboot_flasher/examples/Flash_put_get/Flash_put_get.ino) + [Flash_iterate](https://github.com/MCUdude/MiniCore/blob/master/avr/libraries/Optiboot_flasher/examples/Flash_iterate/Flash_iterate.ino) for useful examples on how you can store strings, structs and variables to flash and retrieve then afterwards.
The [Read_write_without_buffer](https://github.com/MCUdude/MiniCore/blob/master/avr/libraries/Optiboot_flasher/examples/Read_write_without_buffer/Read_write_without_buffer.ino) example demonstrate how you can read and write to the flash memory on a lower level without using a RAM buffer.
## Programmers
MiniCore does not adds its own copies of all the standard programmers to the "Programmer" menu. Just select one of the stock programmers in the "Programmers" menu, and you're ready to "Burn Bootloader" or "Upload Using Programmer".
Select your microcontroller in the boards menu, then select the clock frequency. You'll have to hit "Burn bootloader" in order to set the correct fuses and upload the correct bootloader. <br/>
Make sure you connect an ISP programmer, and select the correct one in the "Programmers" menu. For time critical operations an external oscillator is recommended.
## Write to own flash
MiniCore implements [@majekw](https://github.com/majekw) fork of Optiboot, which enables flash writing functionality within the running application. This means that content from e.g. a sensor can be stored in the flash memory directly, without the need of external memory. Flash memory is much faster than EEPROM, and can handle about 10 000 write cycles.
To enable this feature your original bootloader needs to be replaced by the new one. Simply hit "Burn Bootloader", and it's done!
Check out the [Optiboot flasher example](https://github.com/MCUdude/MiniCore/tree/master/avr/libraries/Optiboot_flasher/examples/SerialReadWrite) for more info about how this feature works, and how you can try it on your MiniCore compatible microcontroller.
## How to install
Expand All @@ -147,7 +149,7 @@ This installation method requires Arduino IDE version 1.6.4 or greater.
```
https://mcudude.github.io/MiniCore/package_MCUdude_MiniCore_index.json
```
```
* Open the **Tools > Board > Boards Manager...** menu item.
* Wait for the platform indexes to finish downloading.
Expand All @@ -174,7 +176,7 @@ Open Arduino IDE, and a new category in the boards menu called "MiniCore" will s
## Getting started with MiniCore
Ok, so you're downloaded and installed MiniCore, but how to get started? Here's a quick guide:
* Hook up your microcontroller as shown in the [pinout diagram](#pinout), or simply just plut it into an Arduino UNO board.
- (If you're not planning to use the bootloader (uploading code using a USB to serial adapter), the FTDI header and the 100 nF capacitor on the reset pin can be omitted.)
- (If you're not planning to use the bootloader (uploading code using a USB to serial adapter), the FTDI header and the 100 nF capacitor on the reset pin can be omitted.)
* Open the **Tools > Board** menu item, and select a MiniCore compatible microcontroller.
* If the *BOD option* is presented, you can select at what voltage the microcontroller will shut down at. Read more about BOD [here](#bod-option).
* Select your prefered clock frequency. **16 MHz** is standard on most Arduino boards, including the Arduino UNO.
Expand All @@ -199,22 +201,22 @@ I hope you find this useful, because they really are!
* sleepMode()
* sleep()
* noSleep()
* enablePower()
* enablePower()
* disablePower()
### For further information please view the [Wiring reference page](https://github.com/MCUdude/MiniCore/blob/master/Wiring_reference.md)!
## Pinout
This core uses the standard Arduino UNO pinout and will not break compatibility of any existing code or libraries. What's different about this pinout compared to the original one is that this got three aditinal IO pins available. You can use digital pin 20 and 21 (PB6 and PB7) as regular IO pins if you're ussing the internal oscillator instead of an external crystal. If you're willing to disable the reset pin (can be enabled using [high voltage parallel programming](https://www.microchip.com/webdoc/stk500/stk500.highVoltageProgramming.html)) it can be used as a regular IO pin, and is assigned to digital pin 22 (PC6).
<b>Click to enlarge:</b>
This core uses the standard Arduino UNO pinout and will not break compatibility of any existing code or libraries. What's different about this pinout compared to the original one is that this got three aditinal IO pins available. You can use digital pin 20 and 21 (PB6 and PB7) as regular IO pins if you're ussing the internal oscillator instead of an external crystal. If you're willing to disable the reset pin (can be enabled using [high voltage parallel programming](https://www.microchip.com/webdoc/stk500/stk500.highVoltageProgramming.html)) it can be used as a regular IO pin, and is assigned to digital pin 22 (PC6).
<b>Click to enlarge:</b>
</br> </br>
| DIP-28 package *ATmega8/48/88/168/328* | TQFP-32 SMD package *ATmega8/48/88/168/328* | TQFP-32 SMD package *ATmega48/88/168/328PB* |
|-------------------------------------------------------|-------------------------------------------------------|-------------------------------------------------------|
|<img src="https://i.imgur.com/qXIEchT.jpg" width="280">|<img src="https://i.imgur.com/naweqE6.jpg" width="260">|<img src="https://i.imgur.com/ZQsjLwL.jpg" width="260">|
## Minimal setup
Here is a simple schematic showing a minimal setup using an external crystal. Skip the crystal and the two 22pF capacitors if you're using the internal oscillator. If you don't want to mess with breadboards, components and wiring; simply use your Arduino UNO! <b>Click to enlarge:</b> <br/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/***********************************************************************|
| Optiboot Flash read/write interface library |
| |
| Flash_iterate.ino |
| |
| A library for interfacing with Optiboot Flash's write functionality |
| Developed in 2021 by MCUdude |
| https://github.com/MCUdude/ |
| |
| In this example we fill the flash[] buffer with numbers and write it |
| to flash page 0. After a reset or power cycle, we read the values |
| back and prints them to the serial monitor. |
| |
| A RAM buffer (ram_buffer) is required for this library to work. It |
| acts as a memory pool you can read from and write to, and recommended |
| size for this buffer is one flash page, 256/128/64 bytes depending on |
| what chip you're using. ram_buffer[] and flash[] is the exact same |
| array, flash[] is just pointing to ram_buffer[]. |
|***********************************************************************/

#include <Flash.h>

// RAM buffer needed by the Flash library. Use flash[] to access the buffer. This
uint8_t ram_buffer[SPM_PAGESIZE];

// Allocate two flash pages for storing data. Use PROGMEM1 to allocate space above 64kiB
#define NUMBER_OF_PAGES 2
const uint8_t flashSpace[SPM_PAGESIZE * NUMBER_OF_PAGES] __attribute__((aligned(SPM_PAGESIZE))) PROGMEM = {};

// Flash constructor
Flash flash(flashSpace, sizeof(flashSpace), ram_buffer, sizeof(ram_buffer));

void write_data()
{
// First, make sure there are no content in out buffer
flash.clear_buffer();

// Fill buffer with numbers, starting from 0
for(uint8_t i = 0; i < 64; i++)
flash[i] = i + 100;

// Write buffer to the first allocated flash page (page 0)
flash.write_page(0);

// Now let's set a flag on another flash page to indicate that the flash memory contains content
// Here we're treating the object as an array
flash.clear_buffer();
flash[5] = 'X';
flash.write_page(1);

Serial.println(F("Page 0 written to!\nReset your board to view the contents!\n"));
}

void read_data()
{
Serial.println(F("Read float from flash page 0: "));

// Fetch first flash page
flash.fetch_page(0);

for(uint16_t i = 0; i < flash.buffer_size(); i++)
{
Serial.print(F("Addr: "));
Serial.print(i);
Serial.print(F(" \tData: "));
Serial.println(flash[i]);
}
}

void setup()
{
delay(2000);
Serial.begin(9600);

// If the allocated flash space is above 64kiB, a 16-bit pointer won't be enough anymore.
// As a workaround, you can set the address to the allocated space like this, and the
// library will handle the rest. You'll also have to allocate your flash space in
// PROGMEM1 rather than PROGMEM.
//flash.set_far_address(pgm_get_far_address(flashSpace));

// Fetch flash page 1, where we may have a flag
flash.fetch_page(1);

// Check if our flag is present
if(flash[5] == 'X')
{
Serial.println(F("Content found!"));
read_data();
}
else
{
Serial.print(F("Flash page size for this chip: "));
Serial.print(SPM_PAGESIZE);
Serial.print(F(" bytes\nTotal assigned flash space: "));
Serial.print(NUMBER_OF_PAGES * SPM_PAGESIZE);
Serial.println(F(" bytes"));

Serial.println(F("No content found! Writing new content..."));
write_data();
}
}

void loop()
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/***********************************************************************|
| Optiboot Flash read/write interface library |
| |
| Flash_get_put.ino |
| |
| A library for interfacing with Optiboot Flash's write functionality |
| Developed in 2021 by MCUdude |
| https://github.com/MCUdude/ |
| |
| In this example we write a float and a struct to flash page 0, and we |
| set a flag on flash page 1 to indicate there is content to be read |
| afterwards. After a reset we read back the flash content and prints |
| them to the serial monitor. |
| |
| A RAM buffer (ram_buffer) is required for this library to work. It |
| acts as a memory pool you can read from and write to, and recommended |
| size for this buffer is one flash page, 256/128/64 bytes depending on |
| what chip you're using. ram_buffer[] and flash[] is the exact same |
| array, flash[] is just pointing to ram_buffer[]. |
|***********************************************************************/

#include <Flash.h>

struct MyObject
{
float field1;
uint8_t field2;
char name[10];
};

// RAM buffer needed by the Flash library. Use flash[] to access the buffer. This
uint8_t ram_buffer[SPM_PAGESIZE];

// Allocate two flash pages for storing data. Use PROGMEM1 to allocate space above 64kiB
#define NUMBER_OF_PAGES 2
const uint8_t flashSpace[SPM_PAGESIZE * NUMBER_OF_PAGES] __attribute__((aligned(SPM_PAGESIZE))) PROGMEM = {};

// Flash constructor
Flash flash(flashSpace, sizeof(flashSpace), ram_buffer, sizeof(ram_buffer));

void write_data()
{
float f = 123.456f;
uint8_t buffer_address = 0;

// First, make sure there are no content in out buffer
flash.clear_buffer();

// One simple call, with the address first and the object second
flash.put(buffer_address, f);

Serial.println(F("Written float data type!"));

// Data to store
MyObject customVar =
{
3.14f,
65,
"MCUdude"
};

// Move address to the next byte after float 'f'
buffer_address += sizeof(float);
flash.put(buffer_address, customVar);

// Write buffer to the first allocated flash page (page 0)
flash.write_page(0);

// Now let's set a flag on another flash page to indicate that the flash memory contains content
// Here we're treating the object as an array
flash.clear_buffer();
flash[5] = 'X';
flash.write_page(1);

Serial.println(F("Written custom data type!\nReset your board to view the contents!\n"));
}

void read_data()
{
Serial.println(F("Read float from flash: "));

// Fetch first flash page
flash.fetch_page(0);

float f = 0.00f; // Variable to store data read from flash
uint8_t buffer_address = 0; // Buffer address to start from

// Get the float data from flash at position 'buffer_address'
flash.get(buffer_address, f);
Serial.print(F("The value of f is now: "));
Serial.println(f, 3);

buffer_address += sizeof(float); // Move address to the next byte after float 'f'
MyObject customVar; // Variable to store custom object read from flash.

flash.get(buffer_address, customVar);

Serial.println(F("Read custom object from flash: "));
Serial.println(customVar.field1);
Serial.println(customVar.field2);
Serial.println(customVar.name);
}

void setup()
{
delay(2000);
Serial.begin(9600);

// If the allocated flash space is above 64kiB, a 16-bit pointer won't be enough anymore.
// As a workaround, you can set the address to the allocated space like this, and the
// library will handle the rest. You'll also have to allocate your flash space in
// PROGMEM1 rather than PROGMEM.
//flash.set_far_address(pgm_get_far_address(flashSpace));

// Fetch flash page 1, where we may have a flag
flash.fetch_page(1);

// Check if our flag is present
if(flash[5] == 'X')
{
Serial.println(F("Content found!"));
read_data();
}
else
{
Serial.print(F("Flash page size for this chip: "));
Serial.print(SPM_PAGESIZE);
Serial.print(F(" bytes\nTotal assigned flash space: "));
Serial.print(NUMBER_OF_PAGES * SPM_PAGESIZE);
Serial.println(F(" bytes"));

Serial.println(F("No content found! Writing new content..."));
write_data();
}
}

void loop()
{

}
Loading

0 comments on commit 80b938c

Please sign in to comment.