From 7db1b1f0806ed9e81a66efea59c3b64abfa2676b Mon Sep 17 00:00:00 2001 From: Roger Clark Date: Thu, 30 Oct 2014 21:22:40 +1100 Subject: [PATCH] Put temporary hack for page settings into EEPROM.h, so that it would compile --- Maple/libraries/EEPROM/EEPROM.cpp | 2 +- Maple/libraries/EEPROM/EEPROM.h | 6 +- .../EEPROM_example/EEPROM_example.ino | 165 ++++++++++++++++++ 3 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 Maple/libraries/EEPROM/examples/EEPROM_example/EEPROM_example.ino diff --git a/Maple/libraries/EEPROM/EEPROM.cpp b/Maple/libraries/EEPROM/EEPROM.cpp index 61062b5..2cfd648 100644 --- a/Maple/libraries/EEPROM/EEPROM.cpp +++ b/Maple/libraries/EEPROM/EEPROM.cpp @@ -1,5 +1,5 @@ -#include "wirish.h" #include "EEPROM.h" +// See http://www.st.com/web/en/resource/technical/document/application_note/CD00165693.pdf /** * @brief Check page for blank diff --git a/Maple/libraries/EEPROM/EEPROM.h b/Maple/libraries/EEPROM/EEPROM.h index b330770..fc1a2a6 100644 --- a/Maple/libraries/EEPROM/EEPROM.h +++ b/Maple/libraries/EEPROM/EEPROM.h @@ -1,9 +1,13 @@ #ifndef __EEPROM_H #define __EEPROM_H -#include "wirish.h" +#include #include "flash_stm32.h" +// HACK ALERT. This definition may not match your processor +// To Do. Work out correct value for EEPROM_PAGE_SIZE on the STM32F103CT6 etc +#define MCU_STM32F103RB + #ifndef EEPROM_PAGE_SIZE #if defined (MCU_STM32F103RB) #define EEPROM_PAGE_SIZE (uint16)0x400 /* Page size = 1KByte */ diff --git a/Maple/libraries/EEPROM/examples/EEPROM_example/EEPROM_example.ino b/Maple/libraries/EEPROM/examples/EEPROM_example/EEPROM_example.ino new file mode 100644 index 0000000..754b738 --- /dev/null +++ b/Maple/libraries/EEPROM/examples/EEPROM_example/EEPROM_example.ino @@ -0,0 +1,165 @@ +#include + +int ledPin = 13; // LED connected to digital pin 13 +const char HELP_MSG[] = "Press :\r\n" \ + " 0 display configuration\r\n" \ + " 1 set configuration to 0x801F000 / 0x801F800 / 0x400 (RB MCU)\r\n" \ + " 2 set configuration to 0x801F000 / 0x801F800 / 0x800 (ZE/RE MCU)\r\n" \ + " 3 write/read variable\r\n" \ + " 4 increment address\r\n" \ + " 5 display pages top/bottom\r\n" \ + " 6 initialize EEPROM\r\n" \ + " 7 format EEPROM\r\n"; +uint16 DataWrite = 0; +uint16 AddressWrite = 0x10; + +void setup() +{ + // initialize the digital pin as an output: + pinMode(ledPin, OUTPUT); + SerialUSB.print(HELP_MSG); +} + +void loop() +{ + uint16 Status; + uint16 Data; + + while (SerialUSB.available()) + { + char cmd = (char)SerialUSB.read(); + SerialUSB.println(cmd); + if (cmd == '0') + { + DisplayConfig(); + } + else if (cmd == '1') + { + EEPROM.PageBase0 = 0x801F000; + EEPROM.PageBase1 = 0x801F800; + EEPROM.PageSize = 0x400; + DisplayConfig(); + } + else if (cmd == '2') + { + EEPROM.PageBase0 = 0x801F000; + EEPROM.PageBase1 = 0x801F800; + EEPROM.PageSize = 0x800; + DisplayConfig(); + } + else if (cmd == '3') + { + Status = EEPROM.write(AddressWrite, DataWrite); + SerialUSB.print("EEPROM.write(0x"); + SerialUSB.print(AddressWrite, HEX); + SerialUSB.print(", 0x"); + SerialUSB.print(DataWrite, HEX); + SerialUSB.print(") : Status : "); + SerialUSB.println(Status, HEX); + + Status = EEPROM.read(AddressWrite, &Data); + SerialUSB.print("EEPROM.read(0x"); + SerialUSB.print(AddressWrite, HEX); + SerialUSB.print(", &..) = 0x"); + SerialUSB.print(Data, HEX); + SerialUSB.print(" : Status : "); + SerialUSB.println(Status, HEX); + + ++DataWrite; + } + else if (cmd == '4') + { + ++AddressWrite; + } + else if (cmd == '5') + { + DisplayPages(0x20); + DisplayPagesEnd(0x20); + } + else if (cmd == '6') + { + Status = EEPROM.init(); + SerialUSB.print("EEPROM.init() : "); + SerialUSB.println(Status, HEX); + SerialUSB.println(); + } + else if (cmd == '7') + { + Status = EEPROM.format(); + SerialUSB.print("EEPROM.format() : "); + SerialUSB.println(Status, HEX); + } + else + SerialUSB.print(HELP_MSG); + } + digitalWrite(ledPin, HIGH); + delay(500); + digitalWrite(ledPin, LOW); + delay(500); +} + +void DisplayConfig(void) +{ + SerialUSB.print ("EEPROM.PageBase0 : 0x"); + SerialUSB.println(EEPROM.PageBase0, HEX); + SerialUSB.print ("EEPROM.PageBase1 : 0x"); + SerialUSB.println(EEPROM.PageBase1, HEX); + SerialUSB.print ("EEPROM.PageSize : 0x"); + SerialUSB.print (EEPROM.PageSize, HEX); + SerialUSB.print (" ("); + SerialUSB.print (EEPROM.PageSize, DEC); + SerialUSB.println(")"); +} + +void DisplayHex(uint16 value) +{ + if (value <= 0xF) + SerialUSB.print("000"); + else if (value <= 0xFF) + SerialUSB.print("00"); + else if (value <= 0xFFF) + SerialUSB.print("0"); + SerialUSB.print(value, HEX); +} + +void DisplayPages(uint32 endIndex) +{ + SerialUSB.println("Page 0 Top Page 1"); + + for (uint32 idx = 0; idx < endIndex; idx += 4) + { + SerialUSB.print (EEPROM.PageBase0 + idx, HEX); + SerialUSB.print (" : "); + DisplayHex(*(uint16*)(EEPROM.PageBase0 + idx)); + SerialUSB.print (" "); + DisplayHex(*(uint16*)(EEPROM.PageBase0 + idx + 2)); + SerialUSB.print (" "); + SerialUSB.print (EEPROM.PageBase1 + idx, HEX); + SerialUSB.print (" : "); + DisplayHex(*(uint16*)(EEPROM.PageBase1 + idx)); + SerialUSB.print (" "); + DisplayHex(*(uint16*)(EEPROM.PageBase1 + idx + 2)); + SerialUSB.println(); + } +} + +void DisplayPagesEnd(uint32 endIndex) +{ + SerialUSB.println("Page 0 Bottom Page 1"); + + for (uint32 idx = EEPROM.PageSize - endIndex; idx < EEPROM.PageSize; idx += 4) + { + SerialUSB.print (EEPROM.PageBase0 + idx, HEX); + SerialUSB.print (" : "); + DisplayHex(*(uint16*)(EEPROM.PageBase0 + idx)); + SerialUSB.print (" "); + DisplayHex(*(uint16*)(EEPROM.PageBase0 + idx + 2)); + SerialUSB.print (" "); + SerialUSB.print (EEPROM.PageBase1 + idx, HEX); + SerialUSB.print (" : "); + DisplayHex(*(uint16*)(EEPROM.PageBase1 + idx)); + SerialUSB.print (" "); + DisplayHex(*(uint16*)(EEPROM.PageBase1 + idx + 2)); + SerialUSB.println(); + } +}