Skip to content

Commit

Permalink
add SPI_FRAM Arduino example
Browse files Browse the repository at this point in the history
  • Loading branch information
moxondesign committed Aug 4, 2016
1 parent 66b29f0 commit 322b962
Show file tree
Hide file tree
Showing 23 changed files with 307 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 69 additions & 0 deletions examples/esp8266/Embedis_SPI_FRAM/EEPROM.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* Embedis - Embedded Dictionary Server
Copyright (C) 2015, 2016 PatternAgents, LLC
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

// Add an "EEPROM" dictionary to Embedis, using the Ardunio EEPROM API
//
// Since the esp8266 platform does not have real EEPROM internal to the device,
// it is emulated using SPI Flash memory.
// Use "SET/Write" sparingly to avoid SPI Flash memory wear leveling issues.
//
// We show in this example how to use Embedis to hold configuration settings.
// Due to Wear leveling issues this is not recommended for real-time and changing data.
// Use NVSRAM, FRAM or other suitable storage technology for rapidly changing data sets!
//
// To configure an EEPROM dictionary, call setup_EEPROM from your
// main setup() function. Optionally, supply the database name you want to use.
// e.g. setup_EEPROM();
// setup_EEPROM( F("MYEEPROM") );
//
// Use the Embedis "select" command to enable your optional database name
// e.g. embedis-> select MYEEPROM
// The Embedis default database is "EEPROM", using the internal EEPROM memory.
// (or in the case of ESP8266 or Arduino Due, Emulated EEPROM...)
//
#include <EEPROM.h>

// Set your particular EEPROM size, which may want to be less than the physical device size.
// As an example, a 16K byte EEPROM might use only the first 4096 bytes to
// hold an IMPI, DeviceTree or other persistant data structure.
// Since Embedis writes from high memory to low memory, you could use 12K bytes
// of that memory for an Embedis Keystore, coexisting with the other persistant data structure.
// If E2END isn't defined you can uncoment the line below and manually set the size (in bytes).
//
#define E2END 1023
const size_t EEPROM_SIZE = E2END + 1;

// We need this because the ESP8266 EEPROM is emulated in SPI Flash
// this is also the case in the Arduino Due platform
#include "spi_flash.h"

void setup_EEPROM()
{
setup_EEPROM( F("EEPROM") );
}

void setup_EEPROM(const String& dict)
{
EEPROM.begin(SPI_FLASH_SEC_SIZE);
Embedis::dictionary( dict,
SPI_FLASH_SEC_SIZE,
[](size_t pos) -> char { return EEPROM.read(pos); },
[](size_t pos, char value) { EEPROM.write(pos, value); },
[]() { EEPROM.commit(); }
);
}

94 changes: 94 additions & 0 deletions examples/esp8266/Embedis_SPI_FRAM/Embedis_SPI_FRAM.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* Embedis - Embedded Dictionary Server
Copyright (C) 2015 PatternAgents, LLC
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
======================================================================================
Embedis_SPI_FRAM : Embedis example with EEPROM and SPI FRAM Keystores
---------------------------------------------------------------------
This example Arduino IDE sketch works on a number of platforms including AVR & ESP8266
and provides an Embedis server that stores permenent (persistant) data in
either the internal EEPROM of the processor or an external SPI FRAM,
or Ferro-Electric Random Access Memory, such as the MB85RS256V or FM25V10 type parts.
The Embedis server can be used to get/set keys in the EEPROM or FRAM databases,
and can be used to store project and configuration settings.
You no longer need to embed that "data" into your "program"!
The EEPROM & FRAM are persistant memory structures, and can be
used across multiple projects to configure your program setting for
the specific hardware and network configuration you are using.
Now, you don't need to recompile your program and reflash your device
in order to change the device settings anymore.
We think that this is a much better mechanism for storing settings between projects.
Once you start using Embedis for your projects, you'll see how quick and easy
it is to move and reconfigure your devices without needing to recompile
and reflash your device firmware just to change a configuration setting.
Now settings can be changed "on-the-fly" with just an Embedis Server.
(some settings changes however, will require a restart/reboot to take effect.)
======================================================================================
*/

#include <Embedis.h>
#include <EEPROM.h>

// Embedis will run on the Serial port. Use the Arduino
// serial monitor and send "COMMANDS" to get started.
// Make sure "No line ending" is -not- selected. All others work.
//
Embedis embedis(Serial);

void setup()
{
Serial.begin(115200);
delay(50);

LOG( String() + F(" ") );
LOG( String() + F("[ Embedis SPI_FRAM Sketch ]") );

setup_EEPROM();
setup_SPI_FRAM();
setup_commands();

LOG( String() + F("[ type 'commands' to get a list... ]") );
}

void loop()
{
/* Always call the process method fro the main loop to run the Embedis system */
embedis.process();

/* Give the internal RTOS time to task switch in ESP8266, Edison, Currie, etc. */
/* not really necessary on AVR or Cortex platforms, but hey - it doesn't hurt... */
delay(20);
}

// This will log to an embedis channel called "log".
// Use SUBSCRIBE LOG to get these messages.
// Logs are also printed to Serial until an empty message is received.
void LOG(const String& message) {
static bool inSetup = true;
if (inSetup) {
if (!message.length()) {
inSetup = false;
return;
}
SERIAL_PORT_MONITOR.println(message);
}
Embedis::publish("log", message);
}
68 changes: 68 additions & 0 deletions examples/esp8266/Embedis_SPI_FRAM/SPI_FRAM.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* Embedis - Embedded Dictionary Server
Copyright (C) 2015 PatternAgents, LLC
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* SPI_FRAM (MB85RS256, FM25V10, etc.) */
/* e.g. https://www.adafruit.com/product/1897 */

/*
* SPI FRAM Pin Assignment for ESP8266
* (see the Fritzing Wiring Diagram for details)
*/
uint8_t FRAM_CS = 15; /* FRAM Chip Select */
uint8_t FRAM_SCK = 14; /* FRAM Clock */
uint8_t FRAM_MISO = 12; /* FRAM Master Out */
uint8_t FRAM_MOSI = 13; /* FRAM Mater In */
//uint8_t FRAM_HOLD = 16; /* FRAM HOLD */
//uint8_t FRAM_WRTP = 2; /* FRAM Write Protect */
uint32_t FRAM_SIZE = 32768; /* MB85RS256 is 256K bits or 32768 x 8 bits*/

#include <SPI.h>
#include "Adafruit_FRAM_SPI.h"

/* Create the Adafruit_FRAM_SPI instance */
//Adafruit_FRAM_SPI fram = Adafruit_FRAM_SPI(FRAM_CS); /* use hardware SPI */
Adafruit_FRAM_SPI fram = Adafruit_FRAM_SPI(FRAM_SCK, FRAM_MISO, FRAM_MOSI, FRAM_CS); /* use software SPI */

/* the settings for your particlar SPI_FRAM type... */
#define SPI_FRAM_MODEL MB85RS256 /* Cypress/Ramtron/etc. */

void setup_SPI_FRAM()
{
setup_SPI_FRAM( F("SPI_FRAM") );
}

void setup_SPI_FRAM(const String& dict)
{
/* Configure the FRAM control pins correctly */
// pinMode(FRAM_HOLD, OUTPUT);
// digitalWrite(FRAM_HOLD, HIGH);
// pinMode(FRAM_WRTP, OUTPUT);
// digitalWrite(FRAM_WRTP, HIGH);

if (fram.begin()) {
LOG( String() + F("[ Found SPI_FRAM ]") );
} else {
LOG( String() + F("[ No SPI_FRAM found ... check your connections and pin settings! ]") );
while (1);
}

Embedis::dictionary( dict,
(FRAM_SIZE),
[](size_t pos) -> char { return fram.read8(pos); },
[](size_t pos, char value) { fram.writeEnable(true); fram.write8(pos, value); fram.writeEnable(false); },
[]() { }
);
}
76 changes: 76 additions & 0 deletions examples/esp8266/Embedis_SPI_FRAM/commands.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* Embedis - Embedded Dictionary Server
Copyright (C) 2015, 2016 PatternAgents, LLC
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

// Adds some useful commands to Embedis.
// We'll add some Arduino I/O commands for making it interpretive...
//
// Call setup_commands from your main setup() function.
// e.g. setup_commands();

//#include <ESP8266WiFi.h>
//#include <StreamString.h>

void setup_commands()
{
/* create an interactive "pinMode" command */
Embedis::command( F("pinMode"), [](Embedis* e) {
if (e->argc != 3) return e->response(Embedis::ARGS_ERROR);
int pin = String(e->argv[1]).toInt();
String argv2(e->argv[2]);
argv2.toUpperCase();
int mode;
if (argv2 == "INPUT") mode = INPUT;
else if (argv2 == "OUTPUT") mode = OUTPUT;
else if (argv2 == "INPUT_PULLUP") mode = INPUT_PULLUP;
else return e->response(Embedis::ARGS_ERROR);
pinMode(pin, mode);
e->response(Embedis::OK);
});

/* create an interactive "digitalWrite" command */
Embedis::command( F("digitalWrite"), [](Embedis* e) {
if (e->argc != 3) return e->response(Embedis::ARGS_ERROR);
int pin = String(e->argv[1]).toInt();
String argv2(e->argv[2]);
argv2.toUpperCase();
int mode;
if (argv2 == "HIGH") mode = HIGH;
else if (argv2 == "LOW") mode = LOW;
else mode = argv2.toInt();
digitalWrite(pin, mode);
e->response(Embedis::OK);
});

/* create an interactive "digitalRead" command */
Embedis::command( F("digitalRead"), [](Embedis* e) {
if (e->argc != 2) return e->response(Embedis::ARGS_ERROR);
int pin = String(e->argv[1]).toInt();
if (digitalRead(pin)) {
e->response(F("HIGH"));
} else {
e->response(F("LOW"));
}
});

/* create an interactive "analogRead" command */
Embedis::command( F("analogRead"), [](Embedis* e) {
if (e->argc != 2) return e->response(Embedis::ARGS_ERROR);
int pin = String(e->argv[1]).toInt();
e->response(':', analogRead(pin));
});

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 322b962

Please sign in to comment.