Diff: OneWireEEPROM.cpp
- Revision:
- 1:7218c076189b
- Parent:
- 0:0f7bbfde44b7
- Child:
- 2:193926923cb0
--- a/OneWireEEPROM.cpp Mon Mar 21 20:33:46 2011 +0000
+++ b/OneWireEEPROM.cpp Sun Apr 17 17:26:52 2011 +0000
@@ -8,8 +8,6 @@
*
* 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
@@ -29,7 +27,7 @@
Serial output(USBTX, USBRX);
-OneWireEEPROM::OneWireEEPROM(PinName pin, bool crcOn, bool useAddr, bool parasitic, DSTYPE ds)
+OneWireEEPROM::OneWireEEPROM(PinName pin, bool crcOn, bool useAddr, bool parasitic, DSTYPE ds, unsigned char *ROMaddress)
:oneWire(pin, STANDARD) {
_useCRC=crcOn;
_useAddress=useAddr;
@@ -48,68 +46,103 @@
}
_memsize = _pages * PAGESIZE;
+
+ if (_useAddress) {
+ for (int i = 0; i < ADDRESS_SIZE; i++)
+ _ROMCode[i]=ROMaddress[i];
+ } else {
+ for (int i = 0; i < ADDRESS_SIZE; i++)
+ _ROMCode[i]=0;
+ }
}
-bool OneWireEEPROM::Initialize(uint8_t* ROMaddress) {
+bool OneWireEEPROM::Initialize() {
+
+ int OneWireFound;
+ int OneWireSameAddress;
+ int i;
+ BYTE _dummyaddress[8];
+
- active=false;
+ if (_useAddress) {
+ output.printf("\r\nScan for device with address ");
+ for (i = 0; i < ADDRESS_SIZE; i++) {
+ output.printf("%x ", (int)_ROMCode[i]);
+ }
+ output.printf("\r\n");
+ }
- output.printf("\r\nScan for One-Wire devices\r\n");
+ OneWireSameAddress=0;
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);
+ do {
+ OneWireFound=(oneWire.search(_dummyaddress));
+ if (OneWireFound) {
+
+ if (!_useAddress) {
+ output.printf("Device found with Address = ");
+ for (i = 0; i < ADDRESS_SIZE; i++) {
+ output.printf("%x ", (int)_dummyaddress[i]);
+ }
+ }
+
+ OneWireSameAddress=1;
+
+ if (_useAddress) {
+ for (i = 0; i < ADDRESS_SIZE; i++) {
+ if (!((OneWireSameAddress) && (_ROMCode[i] ==_dummyaddress[i])))
+ OneWireSameAddress=0;
+ }
+ } else {
+ for (i = 0; i < ADDRESS_SIZE; i++) {
+ _ROMCode[i] =_dummyaddress[i];
+ }
+ }
+
+
+ /*if (OneWireSameAddress) {
+ output.printf("-> Address valid!\r\n");
+
+ } else {
+
+ }*/
+
+ } else {
+
+ output.printf("No more addresses.\r\n");
+ oneWire.resetSearch();
+ wait_ms(250); //500
+ }
+ } while (OneWireFound && !OneWireSameAddress);
+
+ if (!OneWireSameAddress) {
+ output.printf("-> No Valid ROM Code found.\r\n");
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
+ if (OneWireCRC::crc8(_ROMCode, ADDRESS_CRC_BYTE) != _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) {
+ if (_ROMCode[0] != _eeprom_id) {
output.printf("Device is not a OneWireEEPROM_ID device.\r\n");
wait_ms(100);
return false;
+
+ } else {
+ output.printf("OneWireEEPROM present and correct.\r\n");
}
- 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;
+ return true;
}
void OneWireEEPROM::ResetAndAddress() {
oneWire.reset(); // reset device
if (_useAddress) {
- oneWire.matchROM(ROMCode); // select which device to talk to
+ oneWire.matchROM(_ROMCode); // select which device to talk to
} else {
oneWire.skipROM(); // broadcast
}
@@ -125,7 +158,7 @@
if ((Size<=PAGESIZE) and ((Size+Address)<=_memsize)) {
- output.printf ("\r\nWriting to OneWireEEPROM %i Bytes",Size);
+ // output.printf ("\r\nWriting to OneWireEEPROM %i Bytes",Size);
ResetAndAddress();
oneWire.writeByte(WRITESCRATCHPAD);
@@ -136,10 +169,10 @@
for (int i = 0; i < Size; i++) {
oneWire.writeByte(Source[i]);
-// pc_OneWireEEPROM.traceOut ("%X ",Source[i]);
+ // output.printf ("%X ",Source[i]);
}
- // pc_OneWireEEPROM.traceOut ("\r\nTA1=%X",_TA1);
- // pc_OneWireEEPROM.traceOut ("\r\nTA2=%X\r\n",_TA2);
+ // output.printf ("\r\nTA1=%X",_TA1);
+ // output.printf ("\r\nTA2=%X\r\n",_TA2);
//read and check data in scratchpad
ResetAndAddress();
@@ -165,7 +198,7 @@
}
}
- output.printf("\r\nES=%X\r\n",_ES);
+ // output.printf("\r\nES=%X\r\n",_ES);
//issue copy with auth data
wait_ms(10);
@@ -178,7 +211,7 @@
oneWire.reset();
- output.printf ("\r\nData written\r\n");
+ // 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);
@@ -193,7 +226,7 @@
int OneWireEEPROM::ReadMemory(uint8_t* Destination, uint16_t Address, uint16_t Size) {
uint8_t tmpReader;
- bool readFF = 0;
+ // bool readFF = 0;
int memPtr;
if (Address<_memsize) {
@@ -212,11 +245,11 @@
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;
+ // if (tmpReader == 0xff & !readFF)
+ // readFF = 1;
+ // else if (tmpReader == 0xff & readFF)
+ // 0xff read twice, hopefully EoF as we break here :)
+ // break;
Destination[memPtr] = tmpReader;
}
@@ -235,27 +268,53 @@
void OneWireEEPROM::ShowMemory(int PageFrom, int PageTo) {
int Size;
+ int MemSize;
+
+ if (PageFrom<0)
+ PageFrom=0;
+ if (PageTo>=_pages)
+ PageTo=_pages-1;
+
if ((PageFrom>=0) and (PageFrom<_pages) and
(PageTo>=0) and (PageTo<_pages)) {
- uint8_t *MemAll = (uint8_t*) malloc(_memsize);
+ MemSize=(PageTo-PageFrom+1)*PAGESIZE;
+
+ uint8_t *MemAll = (uint8_t*) malloc(MemSize);
- output.printf ("\r\nRead Page(s) from EEPROM\r\n");
+ if (MemAll!=NULL) {
+ output.printf ("\r\nRead Page(s) from EEPROM %i to %i -> ", PageFrom,PageTo);
- Size=ReadMemory(MemAll,0x0000,_memsize);
+ Size=ReadMemory(MemAll,PageFrom*PAGESIZE,MemSize);
+
+ output.printf("%i bytes\r\n ",Size);
- 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]);
+ 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;
}
- if ((j*32)>Size)
- break;
- }
- output.printf ("\r\n");
+ output.printf ("\r\n");
- free(MemAll);
+ free(MemAll);
+ } else
+ output.printf ("\r\nNOT enough memory to display EEPROM content !!\r\n");
}
}
+bool OneWireEEPROM::WriteWord(uint16_t v, uint16_t Address) {
+ uint8_t Mem[2];
+ Mem[0]=(uint8_t)(v & 0x00FF);
+ Mem[1]=(uint8_t)((v & 0xFF00)>>8);
+ return WriteMemory(Mem,Address,2);
+}
+
+bool OneWireEEPROM::WriteByte(uint8_t b, uint16_t Address) {
+ uint8_t Mem[1];
+ Mem[0]=(uint8_t)(b & 0x00FF);
+ return WriteMemory(Mem,Address,1);
+}
\ No newline at end of file