Revision 0:0f7bbfde44b7, committed 2011-03-21
- Comitter:
- Wimpie
- Date:
- Mon Mar 21 20:33:46 2011 +0000
- Child:
- 1:7218c076189b
- Commit message:
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/OneWireEEPROM.cpp Mon Mar 21 20:33:46 2011 +0000
@@ -0,0 +1,261 @@
+/*
+* OneWireEEPROM. Library for Maxim One-Wire EEPROM.
+*
+* see http://www.maxim-ic.com
+*
+* DS2433
+* DS28EC20
+*
+* Copyright (C) <2011> Wim De Roeve <wim312@gmail.com>
+*
+* Uses the OneWireCRC library. http://mbed.org/users/snatch59/programs/OneWireCRC/gpdz56
+*
+* OneWireEEPROM 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.
+*
+* OneWireEEPROM 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
+* If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "mbed.h"
+#include "OneWireEEPROM.h"
+
+Serial output(USBTX, USBRX);
+
+OneWireEEPROM::OneWireEEPROM(PinName pin, bool crcOn, bool useAddr, bool parasitic, DSTYPE ds)
+ :oneWire(pin, STANDARD) {
+ _useCRC=crcOn;
+ _useAddress=useAddr;
+ _useParasiticPower=parasitic;
+ switch (ds) {
+ case DS2433: {
+ _pages = DS2433PAGES;
+ _eeprom_id =DS2433EEPROM_ID;
+ break;
+ }
+ case DS28EC20: {
+ _pages =DS28EC20PAGES;
+ _eeprom_id =DS28EC20EEPROM_ID;
+ break;
+ }
+
+ }
+ _memsize = _pages * PAGESIZE;
+}
+
+
+bool OneWireEEPROM::Initialize(uint8_t* ROMaddress) {
+
+ active=false;
+
+ output.printf("\r\nScan for One-Wire devices\r\n");
+
+ oneWire.resetSearch();
+ if (!oneWire.search(oneWire.ROMCode)) { // search for 1-wire device with address
+ // pc_OneWireEEPROM.traceOut("No One-Wire Device found.\r\n");
+ wait_ms(100);
+ return false;
+ }
+
+ output.printf("Address = ");
+ for (int i = 0; i < ADDRESS_SIZE; i++) {
+ output.printf("%x ", (int)oneWire.ROMCode[i]);
+ }
+
+ output.printf("\r\n");
+
+ if (OneWireCRC::crc8(oneWire.ROMCode, ADDRESS_CRC_BYTE) != oneWire.ROMCode[ADDRESS_CRC_BYTE]) { // check address CRC is valid
+ output.printf("CRC is not valid!\r\n");
+ wait_ms(100);
+
+ return false;
+ }
+
+ if (oneWire.ROMCode[0] != _eeprom_id) {
+ output.printf("Device is not a OneWireEEPROM_ID device.\r\n");
+ wait_ms(100);
+ return false;
+ }
+
+ if (_useAddress) {
+ bool wrong=false;
+ for (int i = 0; i < ADDRESS_SIZE; i++) {
+ if (oneWire.ROMCode[i]!=ROMaddress[i])
+ wrong=true;
+
+ if (!wrong) {
+ output.printf("OneWireEEPROM present and correct.\r\n");
+ active=true;
+ return true;
+ } else {
+ output.printf("other OneWireEEPROM found\r\n");
+ return false;
+ }
+ }
+ } else {
+ active=true;
+ return true;
+ }
+ return false;
+}
+
+void OneWireEEPROM::ResetAndAddress() {
+ oneWire.reset(); // reset device
+ if (_useAddress) {
+ oneWire.matchROM(ROMCode); // select which device to talk to
+ } else {
+ oneWire.skipROM(); // broadcast
+ }
+}
+
+bool OneWireEEPROM::WriteMemory(uint8_t* Source, uint16_t Address, uint8_t Size) {
+ uint8_t _ES; //store Endif Address , datastatus
+ uint8_t B;
+
+ if (Address<_memsize) {
+ uint8_t _TA1=(uint8_t)(Address & 0x00FF);
+ uint8_t _TA2=(uint8_t)((Address & 0xFF00)>>8);
+
+ if ((Size<=PAGESIZE) and ((Size+Address)<=_memsize)) {
+
+ output.printf ("\r\nWriting to OneWireEEPROM %i Bytes",Size);
+
+ ResetAndAddress();
+ oneWire.writeByte(WRITESCRATCHPAD);
+ oneWire.writeByte(_TA1); //begin address T7..T0
+ oneWire.writeByte(_TA2); //begin address T15..T8
+
+ //write _memPage to scratchpad
+
+ for (int i = 0; i < Size; i++) {
+ oneWire.writeByte(Source[i]);
+// pc_OneWireEEPROM.traceOut ("%X ",Source[i]);
+ }
+ // pc_OneWireEEPROM.traceOut ("\r\nTA1=%X",_TA1);
+ // pc_OneWireEEPROM.traceOut ("\r\nTA2=%X\r\n",_TA2);
+
+ //read and check data in scratchpad
+ ResetAndAddress();
+ oneWire.writeByte(READSCRATCHPAD);
+ B=oneWire.readByte();
+ if (B != _TA1) { //check TA1, return if bad
+ output.printf("\r\nWrite error in TA1 %X - %X\r\n",B,_TA1);
+ return false;
+ }
+ B=oneWire.readByte();
+ if (B != _TA2) { //check TA2, return if bad
+ output.printf("\r\nWrite error in TA2 %X - %X\r\n",B,_TA2);
+ return false;
+ }
+ _ES = oneWire.readByte(); // ES Register
+
+ //check data written
+ for (int i = 0; i < Size; i++) {
+ B=oneWire.readByte();
+ if (B != Source[i]) { //return if bad
+ output.printf("\r\nWrite error in scratchpad on %i %X<->%X\r\n",i,B ,Source[i]);
+ return false;
+ }
+ }
+
+ output.printf("\r\nES=%X\r\n",_ES);
+
+ //issue copy with auth data
+ wait_ms(10);
+ ResetAndAddress();
+ oneWire.writeByte(COPYSCRATCHPAD);
+ oneWire.writeByte(_TA1);
+ oneWire.writeByte(_TA2);
+ oneWire.writeByte(_ES); //pull-up!
+ wait_ms(10); //10ms min strong pullup delay -> time needed to copy from scratchpad to memory
+
+ oneWire.reset();
+
+ output.printf ("\r\nData written\r\n");
+ return true;
+ } else {
+ output.printf ("\r\nTrying to write more then %i bytes-> %i\r\n",PAGESIZE,Size);
+ return false;
+ }
+ } else {
+ output.printf ("\r\nAddress %X not available, EEPROM isn't that bigg..-> %i\r\n",Address);
+ return false;
+ }
+}
+
+
+int OneWireEEPROM::ReadMemory(uint8_t* Destination, uint16_t Address, uint16_t Size) {
+ uint8_t tmpReader;
+ bool readFF = 0;
+ int memPtr;
+
+ if (Address<_memsize) {
+
+ uint8_t _TA1=(uint8_t)(Address & 0x00FF);
+ uint8_t _TA2=(uint8_t)((Address & 0xFF00)>>8);
+
+ if ((Size+Address)<=_memsize) {
+
+ ResetAndAddress();
+ oneWire.writeByte(READMEMORY);
+ oneWire.writeByte(_TA1);
+ oneWire.writeByte(_TA2);
+
+ output.printf ("\r\nReading... TA1=%X TA2=%X",_TA1, _TA2);
+
+ for (memPtr = 0; memPtr < Size; memPtr++) {
+ tmpReader = oneWire.readByte();
+ if (tmpReader == 0xff & !readFF)
+ readFF = 1;
+ else if (tmpReader == 0xff & readFF)
+ // 0xff read twice, hopefully EoF as we break here :)
+ break;
+
+ Destination[memPtr] = tmpReader;
+ }
+ output.printf ("-> %i byte(s)\r\n",memPtr);
+ return memPtr;
+
+ } else {
+ output.printf ("\r\nTrying to read outside MEMORY -> %i\r\n",Address+Size);
+ return 0;
+ }
+ } else {
+ output.printf ("\r\nAddress %X not available, EEPROM isn't that bigg..-> %i\r\n",Address);
+ return 0;
+ }
+}
+
+void OneWireEEPROM::ShowMemory(int PageFrom, int PageTo) {
+ int Size;
+ if ((PageFrom>=0) and (PageFrom<_pages) and
+ (PageTo>=0) and (PageTo<_pages)) {
+
+ uint8_t *MemAll = (uint8_t*) malloc(_memsize);
+
+ output.printf ("\r\nRead Page(s) from EEPROM\r\n");
+
+ Size=ReadMemory(MemAll,0x0000,_memsize);
+
+ for (int j=PageFrom;j<=PageTo;j++) {
+ output.printf ("\r\nPage %2i ->",j);
+ for (int i=0;i<PAGESIZE;i++) {
+ if ((j*32+i)<= Size)
+ output.printf("%2X ",MemAll[j*32+i]);
+ }
+ if ((j*32)>Size)
+ break;
+ }
+ output.printf ("\r\n");
+
+ free(MemAll);
+ }
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/OneWireEEPROM.h Mon Mar 21 20:33:46 2011 +0000
@@ -0,0 +1,104 @@
+/*
+* OneWireEEPROM. Library for Maxim One-Wire EEPROM.
+*
+* see http://www.maxim-ic.com
+*
+* DS2433
+* DS28EC20
+*
+* Copyright (C) <2011> Wim De Roeve <wim312@gmail.com>
+*
+* Uses the OneWireCRC library. http://mbed.org/users/snatch59/programs/OneWireCRC/gpdz56
+*
+* OneWire EEPROM 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.
+*
+* OneWireEEPROM 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
+* If not, see <http://www.gnu.org/licenses/>.
+*/
+
+
+#ifndef _OneWireEEPROM_H
+#define _OneWireEEPROM_H
+
+#include "OneWireCRC.h"
+
+// OneWire info
+#define ADDRESS_SIZE 8
+#define ADDRESS_CRC_BYTE 7
+
+// OneWire device-id
+#define DS2433EEPROM_ID 0x23
+#define DS28EC20EEPROM_ID 0x43
+
+// OneWireEEPROM related
+#define WRITESCRATCHPAD 0x0F
+#define READSCRATCHPAD 0xAA
+#define COPYSCRATCHPAD 0x55
+#define READMEMORY 0xF0
+
+#define PAGESIZE 0x20 // 32 bytes for each page
+#define DS2433PAGES 0x10 // 16 pages
+#define DS28EC20PAGES 0x50 // 80 pages
+
+/*
+
+DS2433 is a 4096 bit EEPROM
+ 4096 bits (512 bytes) in 16 pages of 256 bits (32 bytes)
+
+ADDRESS 32-BYTE intermediate storage scratchpad
+0x0000 to 0x001F 32 byte finale storage EEPROM PAGE 0
+0x0020 to 0x003F 32 byte finale storage EEPROM PAGE 1
+0x0040 to 0x01DF 32 byte finale storage EEPROM PAGE 2 to PAGE 14
+0x01E0 to 0x01FF 32 byte finale storage EEPROM PAGE 15
+
+DS28EC20 is a 20480-bit EEPROM
+ 20480 bits (2560 bytes) in 80 pages of 256 bits (32 bytes)
+
+ADDRESS 32-BYTE intermediate storage scratchpad
+0x0000 to 0x001F 32 byte finale storage EEPROM PAGE 0
+0x0020 to 0x003F 32 byte finale storage EEPROM PAGE 1
+0x0040 to 0x09DF 32 byte finale storage EEPROM PAGE 2 to PAGE 78
+0x09E0 to 0x09FF 32 byte finale storage EEPROM PAGE 79
+
+*/
+enum DSTYPE { DS2433 = 1, DS28EC20 = 2};
+
+class OneWireEEPROM {
+public:
+ OneWireEEPROM(PinName pin, bool crcOn, bool useAddr, bool parasitic, DSTYPE ds);
+ bool Initialize(uint8_t* ROMaddress);
+ //
+ bool WriteMemory(uint8_t* Source, uint16_t Address, uint8_t Size);
+ //
+ int ReadMemory(uint8_t* Destination, uint16_t Address, uint16_t Size);
+ void ShowMemory(int PageFrom, int PageTo);
+ bool active;
+
+ BYTE ROMCode[8];
+
+protected:
+
+ OneWireCRC oneWire;
+
+ bool _useParasiticPower;
+ bool _useCRC;
+ bool _useAddress;
+ int _memsize;
+ int _pages;
+ int _eeprom_id;
+
+
+ void ResetAndAddress();
+
+};
+
+
+#endif
\ No newline at end of file