Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Heltec-Aaron-Lee committed May 6, 2020
2 parents e92c783 + d28bbfa commit b31c53a
Show file tree
Hide file tree
Showing 8 changed files with 390 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ void setup() {
Serial.begin(115200);
boardInitMcu();
Radio.Sleep( );
TimerInit( &sleep, OnSleep );
TimerInit( &sleep, onSleep );
TimerInit( &wakeUp, onWakeUp );
OnSleep();
onSleep();
}

void loop() {
Expand Down
166 changes: 166 additions & 0 deletions libraries/EEPROM/EEPROM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
EEPROM.cpp - ASR650x EEPROM emulation
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

/*
* note that the chip has 1K user flash.;
* the size of user flash row is 256;
* user flash row 0-2 can be edited;
* user flash row 3 is reservated, must not be edited;
*
* CY_FLASH_SIZEOF_ROW is 256 , CY_SFLASH_USERBASE is 0x0ffff400
*
*/

#include "Arduino.h"
#include "EEPROM.h"
#include "debug.h"

#ifdef DEBUG_ASR_CORE
#define DEBUGV(fmt, ...) ::printf((PGM_P)PSTR(fmt), ## __VA_ARGS__)
#endif

#ifndef DEBUGV
#define DEBUGV(...) do { (void)0; } while (0)
#endif

#define _EEPROM_SIZE (CY_FLASH_SIZEOF_ROW * 3)

EEPROMClass::EEPROMClass(uint32_t baddr)
: _baddr(baddr)
, _data(0)
, _size(0)
, _dirty(false)
{
}

EEPROMClass::EEPROMClass(void)
: _baddr(CY_SFLASH_USERBASE)
, _data(0)
, _size(0)
, _dirty(false)
{
}

void EEPROMClass::begin(size_t size) {
if (size <= 0) {
DEBUGV("EEPROMClass::begin error, size == 0\n");
return;
}
if (size > _EEPROM_SIZE) {
DEBUGV("EEPROMClass::begin error, %d > %d\n", size, _EEPROM_SIZE);
size = _EEPROM_SIZE;
}

size = (size + 3) & (~3);

//In case begin() is called a 2nd+ time, don't reallocate if size is the same
if(_data && size != _size) {
delete[] _data;
_data = new uint8_t[size];
} else if(!_data) {
_data = new uint8_t[size];
}

_size = size;

if (FLASH_read_at(_baddr, reinterpret_cast<uint8_t *>(_data), _size) == -1) {
DEBUGV("EEPROMClass::begin flash read failed\n");
}

_dirty = false; //make sure dirty is cleared in case begin() is called 2nd+ time
}

void EEPROMClass::end() {
if (!_size)
return;

commit();
if(_data) {
delete[] _data;
}
_data = 0;
_size = 0;
_dirty = false;
}


uint8_t EEPROMClass::read(int const address) {
if (address < 0 || (size_t)address >= _size) {
DEBUGV("EEPROMClass::read error, address %d > %d or %d < 0\n", address, _size, address);
return 0;
}
if (!_data) {
DEBUGV("EEPROMClass::read without ::begin\n");
return 0;
}

return _data[address];
}

void EEPROMClass::write(int const address, uint8_t const value) {
if (address < 0 || (size_t)address >= _size) {
DEBUGV("EEPROMClass::write error, address %d > %d or %d < 0\n", address, _size, address);
return;
}
if(!_data) {
DEBUGV("EEPROMClass::read without ::begin\n");
return;
}

// Optimise _dirty. Only flagged if data written is different.
uint8_t* pData = &_data[address];
if (*pData != value)
{
*pData = value;
_dirty = true;
}
}

bool EEPROMClass::commit() {
if (!_size)
return false;
if(!_dirty)
return true;
if(!_data)
return false;

if (FLASH_update(_baddr, reinterpret_cast<const void *>(_data), _size) == 0) {
_dirty = false;
return true;
}

DEBUGV("EEPROMClass::commit failed\n");
return false;
}

uint8_t * EEPROMClass::getDataPtr() {
_dirty = true;
return &_data[0];
}

uint8_t const * EEPROMClass::getConstDataPtr() const {
return &_data[0];
}

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM)
EEPROMClass EEPROM;
#endif

81 changes: 81 additions & 0 deletions libraries/EEPROM/EEPROM.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
EEPROM.cpp - ASR650x EEPROM emulation
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef EEPROM_h
#define EEPROM_h

#include <stddef.h>
#include <stdint.h>
#include <string.h>

class EEPROMClass {
public:
EEPROMClass(uint32_t baddr);
EEPROMClass(void);

void begin(size_t size);
uint8_t read(int const address);
void write(int const address, uint8_t const val);
bool commit();
void end();

uint8_t * getDataPtr();
uint8_t const * getConstDataPtr() const;

template<typename T>
T &get(int const address, T &t) {
if (address < 0 || address + sizeof(T) > _size)
return t;

memcpy((uint8_t*) &t, _data + address, sizeof(T));
return t;
}

template<typename T>
const T &put(int const address, const T &t) {
if (address < 0 || address + sizeof(T) > _size)
return t;
if (memcmp(_data + address, (const uint8_t*)&t, sizeof(T)) != 0) {
_dirty = true;
memcpy(_data + address, (const uint8_t*)&t, sizeof(T));
}

return t;
}

size_t length() {return _size;}

uint8_t& operator[](int const address) {return getDataPtr()[address];}
uint8_t const & operator[](int const address) const {return getConstDataPtr()[address];}

protected:
uint32_t _baddr;
uint8_t* _data;
size_t _size;
bool _dirty;
};

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM)
extern EEPROMClass EEPROM;
#endif

#endif

25 changes: 25 additions & 0 deletions libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
EEPROM Clear
Sets all of the bytes of the EEPROM to 0.
This example code is in the public domain.
*/

#include <EEPROM.h>

void setup() {
EEPROM.begin(512);
// write a 0 to all 512 bytes of the EEPROM
for (int i = 0; i < 512; i++) {
EEPROM.write(i, 0);
}

// turn the LED on when we're done
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
EEPROM.end();
}

void loop() {
}
43 changes: 43 additions & 0 deletions libraries/EEPROM/examples/eeprom_read/eeprom_read.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
EEPROM Read
Reads the value of each byte of the EEPROM and prints it
to the computer.
This example code is in the public domain.
*/

#include <EEPROM.h>

// start reading from the first byte (address 0) of the EEPROM
int address = 0;
byte value;

void setup() {
// initialize serial and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
EEPROM.begin(512);
}

void loop() {
// read a byte from the current address of the EEPROM
value = EEPROM.read(address);

Serial.print(address);
Serial.print("\t");
Serial.print(value, DEC);
Serial.println();

// advance to the next address of the EEPROM
address = address + 1;

// there are only 512 bytes of EEPROM, from 0 to 511, so if we're
// on address 512, wrap around to address 0
if (address == 512) {
address = 0;
}

delay(500);
}
45 changes: 45 additions & 0 deletions libraries/EEPROM/examples/eeprom_write/eeprom_write.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
EEPROM Write
Stores values read from analog input 0 into the EEPROM.
These values will stay in the EEPROM when the board is
turned off and may be retrieved later by another sketch.
*/

#include <EEPROM.h>

// the current address in the EEPROM (i.e. which byte
// we're going to write to next)
int addr = 0;

void setup() {
Serial.begin(115200);
EEPROM.begin(512);
}

void loop() {
// need to divide by 4 because analog inputs range from
// 0 to 1023 and each byte of the EEPROM can only hold a
// value from 0 to 255.
int val = analogRead(A0) / 4;

// write the value to the appropriate byte of the EEPROM.
// these values will remain there when the board is
// turned off.
EEPROM.write(addr, val);

// advance to the next address. there are 512 bytes in
// the EEPROM, so go back to 0 when we hit 512.
// save all changes to the flash.
addr = addr + 1;
if (addr == 512) {
addr = 0;
if (EEPROM.commit()) {
Serial.println("EEPROM successfully committed");
} else {
Serial.println("ERROR! EEPROM commit failed");
}
}

delay(100);
}
18 changes: 18 additions & 0 deletions libraries/EEPROM/keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#######################################
# Syntax Coloring Map For Ultrasound
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

EEPROM KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################

#######################################
# Constants (LITERAL1)
#######################################

Loading

0 comments on commit b31c53a

Please sign in to comment.