Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
d00616 committed Feb 8, 2017
1 parent 1093dd3 commit c334a1f
Show file tree
Hide file tree
Showing 17 changed files with 2,175 additions and 1 deletion.
38 changes: 38 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
language: generic
addons:
apt:
packages:
- libc6:i386
- libstdc++6:i386
env:
global:
- IDE_VERSION=1.8.1
before_install:
- /sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_1.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :1 -ac -screen 0 1280x1024x16
- sleep 3
- export DISPLAY=:1.0
- wget http://downloads.arduino.cc/arduino-$IDE_VERSION-linux64.tar.xz
- tar xf arduino-$IDE_VERSION-linux64.tar.xz
- mv arduino-$IDE_VERSION $HOME/arduino-ide
- export PATH=$PATH:$HOME/arduino-ide
- arduino --pref "boardsmanager.additional.urls=https://redbearlab.github.io/arduino/package_redbearlab_index.json,http://rfduino.com/package_rfduino166_index.json,https://redbearlab.github.io/arduino/package_redbearlab_index.json,https://sandeepmistry.github.io/arduino-nRF5/package_nRF5_boards_index.json,https://d00616.github.io/arduino-nRF5/package_nRF5_boards_index.json" --save-prefs
- #arduino --install-boards arduino:sam >/dev/null
- #arduino --install-boards arduino:samd >/dev/null
- arduino --install-boards RFduino:RFduino >/dev/null
- arduino --install-boards RedBear:nRF51822 >/dev/null
- #arduino --install-boards sandeepmistry:nRF5 >/dev/null
- arduino --install-boards d00616:nRF5 >/dev/null
- buildExampleSketch() { arduino --verbose-build --verify --board $1 $PWD/examples/$2/$2.ino; }
install:
- mkdir -p $HOME/Arduino/libraries
- ln -s $PWD $HOME/Arduino/libraries/.
script:
- #buildExampleSketch "arduino:sam:arduino_due_x_dbg" test_all
- #buildExampleSketch "arduino:samd:arduino_zero_edbg" test_all
- buildExampleSketch "RFduino:RFduino:RFduino" test_all
- buildExampleSketch "RedBear:nRF51822:nRF51822" test_all
- buildExampleSketch "RedBear:nRF51822:nRF51822_NANO" test_all
- #buildExampleSketch "sandeepmistry:nRF5:Generic_nRF51822:chip=xxac" test_all
- #buildExampleSketch "sandeepmistry:nRF5:Generic_nRF52832" test_all
- buildExampleSketch "d00616:nRF5:Generic_nRF51822:chip=xxac" test_all
- buildExampleSketch "d00616:nRF5:Generic_nRF52832" test_all
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,49 @@
# arduino-NVM
Manage internal flash memory per page, VirutalPage or byte wise storage (EEPROM)

[![Build Status](https://travis-ci.org/d00616/arduino-NVM.svg?branch=master)](https://travis-ci.org/d00616/arduino-NVM)

This library allows the usage of internal Flash memory. To enhance the limited erase cycles a VirtualPage layer is available. On top of VirtualPage, there is an NVRAM class to allow a lot of writes by using a log-based storage.

For Arduino compatibility, a subset of avr/eeprom.h functionality and a complete port of EEPROM.h is provided.

Accessing bytes via NVRAM or EEPROM is faster than an AVR controller until the internal log is full. At this point, a new page must build. This process takes up to 3400ms (nRF51) or 1300ms (nRF52) depending on your hardware and the highest written address.

To find out more about timing, please run "test_all" example.

_This code is not compatible with any SoftDevice. You have to use the [radio notification](https://devzone.nordicsemi.com/tutorials/14/radio-notification/) and VirtualPage.clean_up()/NVRAM.write_prepare(NUMBER) to ensure that writes are only used in a time without radio activation._

## Flash.h

This class is the hardware abstraction to the Flash controller. Please look into Flash.h for a more detailed description.

Please read the documentation of your microcontroller to find out limitations about writing into flash. You can use the FLASH_... defines in your code to take care about quirks.

## VirtualPage.h

This class provides manages Flash pages. This helps you to reach more erase cycles and handle page faults. The underlying Flash page needs to hold some metadata so a VirtualPage is some bytes smaller than 4k. The size can differ between different hardware.

If you need to allocate VirtualPages in performance critical situations, call VirtualPage.clean_up(), when you have a time slot of more than 100ms.

For VirtualPages the last 16k(nRF51) or 32k(nRF52) are used. This allows the same number of erase cycles on both platforms.

## NVRAM.h

This class provides a 3072 bytes large memory. You can access this memory in a random order without needing to take care of the underlying flash architecture. This class is stateless, this means there is nothing cached in RAM. With every access, the data structure is parsed. This saves RAM and avoids conflicts when you have more than one instance of NVRAM class in your code.

To reach a maximum of write cycles and performance, place all your data at the beginning of the memory. This allows a maximum of write cycles.

When you only use the first 8 Bytes of the NVRAM, you have 5,100,000 write cycles per byte. If you use all 3072 bytes, you have only 3,300 write cycles per byte.

For your own calculation of write cycles, you can calculate the sum of available writes with: (VirtualPage.length()-4-HIGHEST_ADDRESS/4)*40,000

Reading or writing the NVRAM is fast until the internal log is full. On nRF51 you can calculate with 1.2ms and on nRF52 with 0,5ms. If the log is full a new VirtualPage is allocated and written. This procedure can take a time of 3400ms (nRF51) or 1300ms (nRF52).

If you use this code in performance critical situations. Use NVRAM.write_prepare(NUMBER) before to guarantee a fast write for the given number of bytes.

## EEPROM.h and avr_eeprom.h

Both libraries are for Arduino compatibility. Instead of avr/eeprom.h, you have to include avr_eeprom.h. This file maps a limited set of functions to NVRAM.

The EEPROM.h is fully compatible with the AVR version without committing changes.

If you use one of both files, please keep in mind that writing a single byte is fast until the log becomes full. In that case, a single write operation can take up to 3400ms (nRF51) or 1300ms (nRF52).
57 changes: 57 additions & 0 deletions examples/flash_erase_all/flash_erase_all.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* This skech erases all content of your controller. Use it only, when
* you are able to install all firemware parts via your programmer.
*
* There is no chance to to updates over the air or via serial port
* after erasing the Flash!
*/

#include <Flash.h>

void setup() {
Serial.begin(9600);
}

char num=0;
char yes=10;
void loop() {
if ((millis()%5000)<100) {
Serial.println();
Serial.println("**************************************************");
Serial.println("* This sketch deletes all Flash memory including *");
Serial.println("* this sketch, bootloaders and SoftDevices!!! *");
Serial.println("* ---------------------------------------------- *");
Serial.println("* After erasing the Flash, you need to flash *");
Serial.println("* a new Sketch with a programmer like J-Link, *");
Serial.println("* ST-Link v2 or CMSIS-DAP. Other methods dosn't *");
Serial.println("* work! *");
Serial.println("* ---------------------------------------------- *");
Serial.println("* If you are shure what you are doing, send a *");
Serial.println("* 'Y' character to erase the nRF CPU completely. *");
Serial.println("* ---------------------------------------------- *");
Serial.println("* You may brick your device! *");
Serial.println("**************************************************");
num=0;
} else {
if (num++<49) {
Serial.print('.');
} else {
Serial.println('.');
num=0;
}
}
if (Serial.available() > 0) {
if (Serial.read() == 'Y') {
if (yes>0) {
Serial.print("\r\nYou may brick your device. Please give me ");
Serial.print(yes, DEC);
Serial.println(" additional 'Y' characters.");
yes--;
} else {
Serial.println("\r\nYou have been warned! Erase flash. Goodbye.");
Flash.erase_all();
}
}
}
delay(50);
}
53 changes: 53 additions & 0 deletions examples/flash_erase_and_write/flash_erase_and_write.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* This skech do sime erase and write operations to the last available
* Flash page.
*/

#include <Flash.h>

// Output function
void print_word(uint32_t *address);

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

// Print some flash data
Serial.print("Flash page size: ");
Serial.println(Flash.page_size());
Serial.print("Number of flash pages: ");
Serial.println(Flash.page_count());
Serial.print("Address of first page: 0x");
Serial.println((size_t)Flash.page_address(0), HEX);

// Find out address of the last available page
uint32_t *page = Flash.page_address(Flash.page_count() - 1);
print_word(page);

// Erase the page
Serial.println("Erase page");
Flash.erase(page, Flash.page_size());
print_word(page);

// Inform about write
Serial.println("Write 0x12345678");

// Write to flash, you can do more writes until writing is disabled
Flash.write(page, 0x12345678);

// Print memory content
print_word(page);
}

void loop() {
// Nothing to do here
yield();
}

// Print data
void print_word(uint32_t *address) {
Serial.print("Word at address 0x");
Serial.print((size_t)address, HEX);
Serial.print("=0x");
Serial.println(*address, HEX);
}
Loading

0 comments on commit c334a1f

Please sign in to comment.