diff --git a/DataFlash_CopyFromSD/DataFlash_CopyFromSD.ino b/DataFlash_CopyFromSD/DataFlash_CopyFromSD.ino index 7205536..a03f65f 100644 --- a/DataFlash_CopyFromSD/DataFlash_CopyFromSD.ino +++ b/DataFlash_CopyFromSD/DataFlash_CopyFromSD.ino @@ -46,7 +46,7 @@ void setup() // if ( !dbfile ) Serial.println("Failed to open db file."); long swatch = millis(); - long count = 1450; + long count = 1450; if (datafile) { count = 0; buf[0] = 0; @@ -59,7 +59,7 @@ void setup() continue; } buf[15] = 0; - if ( count < 4 || count > 1440 ) { + if ( count < 4 ) { Serial.print(count); Serial.print(" "); Serial.println((char*)buf); @@ -72,9 +72,10 @@ void setup() buf[0] = 0; } } - datafile.close(); flash.flush(); + datafile.close(); // dbfile.close(); + Serial.println(); Serial.println(millis() - swatch); Serial.println("Loading data finished."); Serial.println(); @@ -89,12 +90,12 @@ void setup() for(long i = 0; i < count; i++) { flash.read(i*15, buf, 15); buf[15] = 0; - if ( i < 5 || i > count - 12 ) { + //if ( i < 5 || i > count - 12 ) { Serial.print(i); Serial.print(" "); Serial.print((char*) buf); Serial.println(); - } + //} } Serial.println(millis() - swatch); Serial.println("Dumping "); diff --git a/DataFlash_Prototypying/DataFlash_Prototypying.ino b/DataFlash_Prototypying/DataFlash_Prototypying.ino index 890b0b8..b01d03f 100644 --- a/DataFlash_Prototypying/DataFlash_Prototypying.ino +++ b/DataFlash_Prototypying/DataFlash_Prototypying.ino @@ -1,9 +1,13 @@ +#include + #include "SPI.h" #include "DataFlash_SPI.h" +#include "text.h" + DataFlash dflash(5); -char text[] = "Paradise Lost: \n John Milton\n If thou beest he; \nBut O how fall'n! how chang'd From him, \nwho in the happy Realms of Light \nCloth'd with transcendent brightnes didst outshine \nMyriads though bright: \nIf he whom mutual league, \nUnited thoughts and counsels, equal hope, \nAnd hazard in the Glorious Enterprize, \nJoynd with me once, now misery hath joynd \nIn equal ruin: into what \nPit thou seest \nFrom what highth fal'n, so much the stronger provd \nHe with his Thunder: and till then who knew \nThe force of those dire \nArms? yet not for those \nNor what the Potent Victor in his rage \nCan else inflict do I repent or change, "; unsigned long addr = 0; +char buffer[16]; void setup() { byte not_used_cs[] = { @@ -16,7 +20,8 @@ void setup() { Serial.begin(19200); Serial.flush(); - + Serial.println("Hi."); + SPI.begin(); if ( ! dflash.begin() ) { Serial.println(dflash.status(), BIN); @@ -28,18 +33,24 @@ void setup() { Serial.print("Address Bits per page: "); Serial.println(dflash.pageBits()); Serial.println(); -//goto skipped; + //goto skipped; Serial.println("Writing text... "); - Serial.println(text); - dflash.write(0, (byte*)text, sizeof(text)); + word textlength = strlen_P(text); + for(int i = 0; i < textlength; i += 16) { + memcpy_P(buffer, text+i, 16); + dflash.write(i, (byte*) buffer, 16); + // dflash.read(i, (byte*) buffer, 16); + buffer[16] = 0; + Serial.print((char*)buffer); + } // dflash.write('\n'); - Serial.println("finished. "); + Serial.println(" writing finished. "); dflash.flush(); skipped: delay(1000); char c; - for( addr = 0; addr < sizeof(text); addr++) { + for( addr = 0; addr < strlen(text); addr++) { if ( (addr % 0x100) == 0 ) { Serial.println(); Serial.print(addr>>16&0x0f, HEX); @@ -48,7 +59,6 @@ skipped: Serial.print(addr>>4&0x0f, HEX); Serial.print(addr& 0x0f, HEX); Serial.println(": "); - Serial.println(); } c = dflash.read(addr); if ( c == 0 ) { @@ -56,7 +66,7 @@ skipped: continue; } Serial.print(c); - delay(20); + delay(5); } Serial.println(" --- end ----"); } @@ -68,3 +78,4 @@ void loop() { + diff --git a/DataFlash_Prototypying/text.h b/DataFlash_Prototypying/text.h new file mode 100644 index 0000000..eb26a7f --- /dev/null +++ b/DataFlash_Prototypying/text.h @@ -0,0 +1,4 @@ +#include + +PROGMEM prog_char text[] = "Book I, 84:\n If thou beest he; But O how fall'n! how chang'd\nFrom him, who in the happy Realms of Light \nCloth'd with transcendent brightness didst out-shine\nMyriads though bright: If he Whom mutual league,\nUnited thoughts and counsels, equal hope\nAnd hazard in the Glorious Enterprize,\nJoynd with me once, now misery hath joynd \nIn equal ruin: into what Pit thou seest\nFrom what highth fall'n, so much the stronger prov'd\nHe with his Thunder: and till then who knew\nThe force of those dire Arms? yet not for those,\nNor what the Potent Victor in his rage \nCan else inflict, do I repent or change,\nThough chang'd in outward lustre; that fixt mind\nAnd high disdain, from sence of injur'd merit,\nThat with the mightiest rais'd me to contend,\nAnd to the fierce contention brought along \nInnumerable force of Spirits arm'd\nThat durst dislike his reign, and me preferring,\nHis utmost power with adverse power oppos'd\nIn dubious Battel on the Plains of Heav'n,\nAnd shook his throne. What though the field be lost? \nAll is not lost; the unconquerable Will,\nAnd study of revenge, immortal hate,\nAnd courage never to submit or yield:\nAnd what is else not to be overcome?\nThat Glory never shall his wrath or might \nExtort from me. To bow and sue for grace\nWith suppliant knee, and deifie his power,\nWho from the terrour of this Arm so late\nDoubted his Empire, that were low indeed,\nThat were an ignominy and shame beneath \nThis downfall; since by Fate the strength of Gods\nAnd this Empyreal substance cannot fail,\nSince through experience of this great event\nIn Arms not worse, in foresight much advanc't,\nWe may with more successful hope resolve \nTo wage by force or guile eternal Warr\nIrreconcileable, to our grand Foe,\nWho now triumphs, and in th' excess of joy\nSole reigning holds the Tyranny of Heav'n.\n"; + diff --git a/Ethernet_W5100/MultiTaskEthernetTerminal/MultiTaskEthernetTerminal.ino b/Ethernet_W5100/MultiTaskEthernetTerminal/MultiTaskEthernetTerminal.ino index df876d7..aa6e6d5 100644 --- a/Ethernet_W5100/MultiTaskEthernetTerminal/MultiTaskEthernetTerminal.ino +++ b/Ethernet_W5100/MultiTaskEthernetTerminal/MultiTaskEthernetTerminal.ino @@ -6,13 +6,14 @@ #include "PN532_I2C.h" #include "ISO14443.h" //#include "SD_SPI.h" -#include "SPISRAM.h" +//#include "SPISRAM.h" -//#include "Des.h" +#include "Des.h" #include "TextStream.h" #include "IDCard.h" #include "queue.h" +#include "keycodes.h" PN532 nfc(PN532::I2C_ADDRESS, 2, 7); byte polling[] = { @@ -60,13 +61,13 @@ SDClass sd(SD_CS); SdVolume volume; SdFile root; */ -const int SRAM_CS = 8; -SPISRAM spisram(SRAM_CS); +//const int SRAM_CS = 8; +//SPISRAM spisram(SRAM_CS); byte buf[128]; long stwatch; -//Des codec; +Des codec((byte*)uni_key); void setup() { // Open serial communications and wait for port to open: @@ -86,7 +87,7 @@ void setup() { mon << "SD Card initialization failed." << endl; } */ - spisram.begin(); +// spisram.begin(); rtc_spi.begin(); // start the Ethernet connection and the server: diff --git a/Ethernet_W5100/MultiTaskUDPTerminal/IDCard.h b/Ethernet_W5100/MultiTaskUDPTerminal/IDCard.h new file mode 100644 index 0000000..8b6693e --- /dev/null +++ b/Ethernet_W5100/MultiTaskUDPTerminal/IDCard.h @@ -0,0 +1,53 @@ +const byte KawazuKey_a[] = { + 0xAA, 0x4B, 0x61, 0x5A, 0x75, 0x49, 0x69 }; +const byte KawazuKey_b[] = { + 0xBB, 0x63, 0x45, 0x74, 0x55, 0x79, 0x4B }; + +/* + char kana[][4] = { + "xx", "xx", "xx", "xx", "xx", "xx", "wo", "xa", + "xi", "xu", "xe", "xo", "ya", "yu", "yo", "xtu", + "-", "A", "I", "U", "E", "O", "KA", "KI", + "KU", "KE", "KO", "SA", "SHI", "SU", "SE", "SO", + "aaa", "bbb", "ccc", "ddd", "eee", "NA", "NI", "NU", + "NE", "NO", "HA", "HI", "FU", "HE", "HO", "MA", + "MI", "MU", "ME", "MO", "YA", "YU", "YO", "RA", + "RI", "RU", "RE", "RO", "WA", "NN", "\"", "o", + }; + */ + +union IDCardData { + struct FCF { + enum GENDER { + UNKNOWN = 0, MALE = 1, FEMALE = 2 + }; + static const long KyushuInstOfTech = 40010071; + word division; + char pin[12]; + char issue; + char gender; + // + char kana[16]; + char school[8]; + char dateofissue[8]; + char goodthru[8]; + byte pmcontrol[8]; + } + felica; + struct IizukaMagTape { + word division; + char pin[8]; + char issue; + char res0[5]; + word kanji[8]; + char dayofbirth[7]; + char gender; + char dateofissue[7]; + char res1[1]; + } + mifare; +}; + + + + diff --git a/Ethernet_W5100/MultiTaskUDPTerminal/Keycodes.h b/Ethernet_W5100/MultiTaskUDPTerminal/Keycodes.h new file mode 100644 index 0000000..bc65de4 --- /dev/null +++ b/Ethernet_W5100/MultiTaskUDPTerminal/Keycodes.h @@ -0,0 +1,5 @@ +const char uni_key[8] = { 0x7F, 0x48, 0xE5, 0xEC, 0x12, 0x0F, 0xE1, 0x9E}; /* defoult net_key */ +const char sys_key[8] = { 0x5C, 0x78, 0x51, 0xCB, 0x6A, 0xB5, 0x4E, 0x5E}; +const char crd_keya[6] = { 0x4B, 0x61, 0x5A, 0x75, 0x49, 0x69}; +const char crd_keyb[6] = {0x63, 0x45, 0x74, 0x55, 0x79, 0x4B}; + diff --git a/Ethernet_W5100/MultiTaskUDPTerminal/MultiTaskUDPTerminal.ino b/Ethernet_W5100/MultiTaskUDPTerminal/MultiTaskUDPTerminal.ino new file mode 100644 index 0000000..95b598e --- /dev/null +++ b/Ethernet_W5100/MultiTaskUDPTerminal/MultiTaskUDPTerminal.ino @@ -0,0 +1,406 @@ +#include +#include +#include "Ethernet_w5100.h" +#include "DS3234.h" +#include "Wire.h" +#include "PN532_I2C.h" +#include "ISO14443.h" +//#include "SD_SPI.h" +//#include "SPISRAM.h" +#include "DataFlash_SPI.h" + +#include "Des.h" +#include "TextStream.h" + +#include "IDCard.h" +#include "queue.h" +#include "keycodes.h" + +PN532 nfc(PN532::I2C_ADDRESS, 2, 7); +byte polling[] = { + 2, TypeF, TypeA }; +long lastpoll; +long lastread; +enum { + IDLE, + POLLING, + RECEIVED_NFCID, + AUTHENTICATED, + ERROR +} +reader_status = IDLE; + +ISO14443 card; +Queue cardlog; + +byte mac[] = { + 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; +byte ip[] = { + 192,168,1, 177 }; +//byte subnet[] = { 255, 255, 255, 0 }; +//byte gtway[] = { 192, 168, 1, 1 }; +const unsigned int PORT_COMMAND = 7602; +EthernetUDP udpPort; + +TextStream mon(Serial); +//EthernetClient client; +long client_idle_since = 0; +enum { + DISCONNECTED, + CONNECTED, + LISTING +} +server_status = DISCONNECTED; +int listix; + +DS3234 rtc_spi(9); +long lastrtcupdate; + +DataFlash dflash(5); +/* +const int SD_CS = 4; +SDClass sd(SD_CS); +SdVolume volume; +SdFile root; +*/ +//const int SRAM_CS = 8; +//SPISRAM spisram(SRAM_CS); + +byte buf[128]; +long stwatch; + +//Des codec((byte*)uni_key); + +void setup() { + // Open serial communications and wait for port to open: + Serial.begin(19200); + mon << "starting Ethernet terminal test on port " << PORT_COMMAND << "." << endl; + + if ( EEPROM.read(0) == 0x9e && EEPROM.read(1) == 'I' ) { + EEPROM_read(2, mac, 6); + EEPROM_read(2+6, ip, 4); + // EEPROM_read(2+6+4, subnet, 4); + // EEPROM_read(2+6+4+4, gtway, 4); + } + + SPI.begin(); +/* + if (!sd.begin()) { + mon << "SD Card initialization failed." << endl; + } + */ +// spisram.begin(); + + rtc_spi.begin(); + // start the Ethernet connection and the server: + Ethernet.begin(mac, ip); + udpPort.begin(PORT_COMMAND); + mon << "server is at " << Ethernet.localIP() << endl; + + Wire.begin(); + nfc.begin(); + PN532_setup(); + + card.clear(); + cardlog.init(); + + mon << "initialized." << endl; + + buf[0] = 0; +} + + +void loop() { + // listen for incoming clients + // char c; + byte cnt; + long current; + int pos, len; + word blklist[] = { + 0, + 1, + 2, + 3 + }; +char tmp[32]; + + if ( millis() > lastrtcupdate + 133 ) { + current = rtc_spi.time; + rtc_spi.update(); + if ( current != rtc_spi.time ) { + mon.print(rtc_spi.time, HEX); + mon << " "; + mon.print(rtc_spi.cal, HEX); + mon << endl; + } + } + + stwatch = millis(); + switch(reader_status) { + case IDLE: + if (millis() < lastpoll + 101 ) + break; + if ( card.type != Type_Empty && millis() > lastread + 5000 ) + card.clear(); + if ( nfc.InAutoPoll(1, 1, polling+1, polling[0]) ) { + lastpoll = millis(); + reader_status = POLLING; + } + else { + reader_status = ERROR; + } + break; + + case POLLING: + if ( !nfc.IRQ_ready() ) + break; + cnt = nfc.getAutoPollResponse((byte*) buf); + if ( nfc.status() != nfc.RESP_RECEIVED ) { + reader_status = ERROR; + break; + } + if ( cnt == 0 ) { + reader_status = IDLE; + break; + } + if (card.type == nfc.target.NFCType + && memcmp(card.ID, nfc.target.IDm, nfc.target.IDLength) == 0 ) { + //mon << "hey" << endl; + reader_status = IDLE; + lastpoll = millis() + 1000; + break; + } + reader_status = RECEIVED_NFCID; + card.set(nfc.target.NFCType, nfc.target.IDm, nfc.target.IDLength); + card.printOn(mon); + mon << endl; + break; + + case RECEIVED_NFCID: + switch(nfc.target.NFCType) { + case FeliCa212kb: + if ( nfc.felica_Polling(buf, 0x00fe) && (cnt = nfc.felica_RequestService(0x1a8b) != 0xffff) ) { + mon << "authenticaterd." << endl; + reader_status = AUTHENTICATED; + } + break; + case Mifare: + if ( nfc.mifare_AuthenticateBlock(4, KawazuKey_b) + && nfc.getCommandResponse(buf) && buf[0] == 0 ) { + mon << "authenticaterd." << endl; + reader_status = AUTHENTICATED; + } + break; + default: + mon << "Card is unknown type" << endl; + break; + } + if ( reader_status != AUTHENTICATED ) { + mon << "Failed authentication " << endl; + reader_status = IDLE; + } + break; + + case AUTHENTICATED: + switch (card.type) { + case FeliCa212kb: + if ( nfc.felica_ReadBlocksWithoutEncryption(buf, 0x1a8b, (byte) 4, blklist) ) { + IDCardData & idcard((IDCardData &) buf); + mon << "PIN "; + mon.printBytes(idcard.felica.pin, 8); + mon << " ISSUE " << idcard.felica.issue << endl; + cardlog.add(rtc_spi.cal, rtc_spi.time, card.type, card.IDm, idcard.felica.pin); + //mon << cardlog.count() << endl; + } + break; + case Mifare: + if ( nfc.mifare_ReadDataBlock(4, buf) ) { + IDCardData & idcard((IDCardData &) buf); + mon << "PIN "; + mon.printBytes(idcard.mifare.pin, 8); + mon << " ISSUE " << idcard.mifare.issue << endl; + cardlog.add(rtc_spi.cal, rtc_spi.time, card.type, card.UID, idcard.felica.pin); + //mon << cardlog.count() << endl; + } + break; + } + lastread = millis(); + reader_status = IDLE; + break; + default: + // mon << "reader_status = " << (byte) reader_status << endl; + reader_status = IDLE; + break; + } + stwatch = millis() - stwatch; + if ( stwatch > 250 ) + mon << "Task > 250 msec " << stwatch << " millis to " << (byte) reader_status << endl; + + + if ( server_status == DISCONNECTED ) { + server_status = CONNECTED; + } else if (server_status == CONNECTED) { + if (millis() < client_idle_since + 30000L ) { + // an http request ends with a blank line + if ( udpPort.available() ) { + TextStream cmon(udpPort); + if ( cmon.readLine((char*)buf, 127) > 0 ) { + for(int i = 0; buf[i] ; i++) + buf[i] = toupper(buf[i]); + len = cmon.ithToken((const char*)buf, 0, pos); + len = min(len, 8); + if ( len > 0 && strncmp((char*)buf+pos,"INFO", len) == 0 ) { + cmon << "INFO >> "; + cmon.printBytes(mac, 6, '-'); + cmon << ", "; + cmon << Ethernet.localIP() << endl; + } + else if ( len > 0 && strncmp((char*)buf+pos, "TIME", len) == 0 ) { + cmon << ">> " << "TIME "; + cmon.print(rtc_spi.cal, HEX); + cmon << " - "; + cmon.print(rtc_spi.time>>16&0xff, HEX); + cmon << ":"; + cmon.print(rtc_spi.time>>8&0xff, HEX); + cmon << ":"; + cmon.print(rtc_spi.time&0xff, HEX); + cmon << endl; + mon << "TIME command. " << endl; + } + else if ( len > 0 && strncmp((char*)buf+pos, "TSET", len) == 0 ) { + len = cmon.ithToken((const char*)buf, 1, pos); + if ( len ) { + buf[pos+len] = 0; + cmon << ">> " << "SET TIME TO " << ((char*)buf+pos) << endl; + current = strtol((const char*) buf+pos, NULL, HEX); + rtc_spi.setTime(current); + } + } + else if ( len > 0 && strncmp((char*)buf+pos, "CSET", len) == 0 ) { + len = cmon.ithToken((const char*)buf, 1, pos); + if ( len ) { + buf[pos+len] = 0; + cmon << ">> " << "SET CALENDAR TO " << ((char*)buf+pos) << endl; + current = strtol((const char*) buf+pos, NULL, HEX); + rtc_spi.setCalendar(current); + } + } + else if ( len > 0 && strncmp((char*)buf+pos, "LIST", len) == 0 ) { + server_status = LISTING; + listix = 0; + } + else if ( len > 0 && strncmp((char*)buf+pos, "CODEC_TEST", len) == 0 ) { + len = cmon.ithToken((const char*)buf, 1, pos); + if ( (len % 8) == 0 ) { + buf[pos+len] = 0; + cmon << ">> " << "ENCRYPT "; + cmon.printBytes((char*)buf+pos, len); + cmon << endl; + //codec.key_set(buf); + ecb_crypt((char*)buf+pos, (char*)tmp, len>>3, true); + cmon << ">> " << "CODED "; + cmon.printBytes((char*)tmp, len); + cmon << endl; + ecb_crypt(tmp, (char*)buf+pos, len>>3, false); + cmon << ">> " << "DECODED "; + cmon.printBytes((char*)buf+pos, len); + cmon << endl; + } + + } + else if ( len > 0 && strncmp((char*)buf+pos, "DELETE", len) == 0 ) { + len = cmon.ithToken((const char*)buf, 1, pos); + if ( len ) { + buf[pos+len] = 0; + cmon << ">> " << "DELETE ENTRIES"; + current = strtol((const char*) buf+pos, NULL, HEX); + current = cardlog.remove(current); + cmon << " " << current << ". " << endl; + } + } + else if ( len > 0 && strncmp((char*)buf+pos, "QUIT", len) == 0 ) { + cmon << ">> " << "bye bye." << endl; + delay(5); + udpPort.stop(); + server_status = DISCONNECTED; + mon << "disonnected client." << endl; + } + buf[0] = 0; + client_idle_since = millis(); + } + + } + // close the connection: + } + else { + udpPort.stop(); + mon << "disonnected client by timeout." << endl; + } + } + else if ( server_status == LISTING ) { + current = millis(); + TextStream cmon(udpPort); + for( /* value is set already */; + listix < cardlog.count() && millis() < current + 200 ; listix++) { + if ( listix == 0 ) { + cmon << "No Date TIME TYPE Card ID PIN"; + cmon << endl; + } + cmon << listix+1 << " "; + cmon.print(cardlog[listix].date, HEX); + cmon << " "; + cmon.print(cardlog[listix].time, HEX); + cmon << " "; + cmon.print(cardlog[listix].type, HEX); + cmon << " "; + cmon.printBytes(cardlog[listix].NFCID, 8, '-'); + cmon << " "; + cmon.printBytes(cardlog[listix].PIN, 8); + cmon << endl; + } + if ( !(listix < cardlog.count()) ) + // if finished. + server_status = CONNECTED; + } +} + +int EEPROM_read(int addr, byte * ptr, int n) { + for(int i = 0 ; i < n; i++, ptr++) { + *ptr = EEPROM.read(addr+i); + } + return n; +} + +boolean PN532_setup() { + byte cnt = 0; + for (int i = 0; i < 3 && !( nfc.GetFirmwareVersion() && (cnt = nfc.getCommandResponse((byte*)buf)) ); i++) + delay(500); + if (! cnt ) { + Serial << "Couldn't find PN53x on Wire." << endl; + return false; + } + Serial << "PN53x IC ver. " << (char) buf[0] << ", Firmware ver. " + << (byte) buf[1] << '.' << (byte) buf[2] << endl; + + if ( nfc.SAMConfiguration() && nfc.getCommandResponse((byte*)buf) + && nfc.status() == PN532::RESP_RECEIVED) { + Serial << "SAMConfiguration," << endl; + } + nfc.CPU_PowerMode(2); + nfc.getCommandResponse((byte*)buf); + reader_status = IDLE; + return true; +} + +void ecb_crypt(char * intext, char * outtext, int n, boolean encode) { + if ( n == 0 ) { + outtext[0] = 0; + return; + } + for(int ith = 0; ith < (n>>3); ith++) { + memset(outtext+(ith*8), 0x20, 8); + //codec.ecb_crypt(intext+(ith*8), outtext+(ith*8), encode); + } +} + + diff --git a/Ethernet_W5100/MultiTaskUDPTerminal/queue.h b/Ethernet_W5100/MultiTaskUDPTerminal/queue.h new file mode 100644 index 0000000..ff513a3 --- /dev/null +++ b/Ethernet_W5100/MultiTaskUDPTerminal/queue.h @@ -0,0 +1,63 @@ + +struct Queue { + static const int history_size = 8; + union Event { + struct { + long date, time; + byte type; + byte NFCID[8]; + char CL[2]; + char PIN[8]; + char SUFFIX; + }; + byte raw[32]; + } + history[history_size]; + int tally; + int sindex; + + void init() { + sindex = 0; + tally = 0; + } + + int count() { + return tally; + } + + void add(long dt, long tm, byte type, byte cid[], char pin[]) { + int cindex = (sindex + tally) % history_size; + history[cindex].date = dt; + history[cindex].time = tm; + history[cindex].type = type; + for(int i = 0; i < 8; i++) { + history[cindex].NFCID[i] = cid[i]; + history[cindex].PIN[i] = pin[i]; + } + if ( tally == history_size ) { + sindex++; + sindex %= history_size; + } + else { + tally++; + } + } + + int remove(int n) { + Serial.println(tally); + Serial.println(sindex); + n = min(n, tally); + sindex += n; + sindex %= history_size; + tally -= n; + Serial.println(tally); + Serial.println(sindex); + return n; + } + + Event & operator[](const int i) { + int cindex = (sindex + i) % history_size; + return history[cindex]; + } +}; + diff --git a/Ethernet_W5100/examples/UDPSendReceiveString/UDPSendReceiveString.ino b/Ethernet_W5100/examples/UDPSendReceiveString/UDPSendReceiveString.ino index 4d4045c..8b7d0be 100644 --- a/Ethernet_W5100/examples/UDPSendReceiveString/UDPSendReceiveString.ino +++ b/Ethernet_W5100/examples/UDPSendReceiveString/UDPSendReceiveString.ino @@ -14,7 +14,7 @@ #include // needed for Arduino versions later than 0018 -#include +#include #include // UDP library from: bjoern@cs.stanford.edu 12/30/2008 @@ -22,9 +22,9 @@ // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; -IPAddress ip(192, 168, 1, 177); +IPAddress ip(192, 168, 24, 177); -unsigned int localPort = 8888; // local port to listen on +unsigned int localPort = 7602; // local port to listen on // buffers for receiving and sending data char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet, @@ -34,11 +34,22 @@ char ReplyBuffer[] = "acknowledged"; // a string to send back EthernetUDP Udp; void setup() { + pinMode(4, OUTPUT); + digitalWrite(4, HIGH); + pinMode(5, OUTPUT); + digitalWrite(5, HIGH); + pinMode(9, OUTPUT); + digitalWrite(9, HIGH); + + Serial.begin(9600); + Serial.println("Hi."); + + SPI.begin(); // start the Ethernet and UDP: Ethernet.begin(mac,ip); +Serial.print("started"); Udp.begin(localPort); - - Serial.begin(9600); +Serial.println("."); } void loop() { diff --git a/libraries/DataFlash_SPI/DataFlash_SPI.cpp b/libraries/DataFlash_SPI/DataFlash_SPI.cpp index c17cf83..b577944 100755 --- a/libraries/DataFlash_SPI/DataFlash_SPI.cpp +++ b/libraries/DataFlash_SPI/DataFlash_SPI.cpp @@ -50,6 +50,8 @@ #include #include "DataFlash_SPI.h" +//#define DEBUG_READ +//#define DEBUG_WRITE //#define DEBUG DataFlash::DataFlash(byte cs) : @@ -77,14 +79,10 @@ boolean DataFlash::init(void) { bitWidthPerPage = 10; } PageToBufferTransfer(0); - PageToBufferTransfer(1); #ifdef DEBUG Serial.print("page read cache: "); - Serial.println(pageCached[0], HEX); - Serial.print("page read cache: "); - Serial.println(pageCached[1], HEX); + Serial.println(pageCached, HEX); #endif -// currentBuffer = 0; // reset(); if ((stat & 0b00111100) != 0b00101100) return false; @@ -111,91 +109,61 @@ boolean DataFlash::init(void) { */ byte DataFlash::read(const unsigned long & addr) { - word page = addr / bytesPerPage; // upper 12 bits - word offset = addr % bytesPerPage; // lower 10/9 bits - byte bufsel = page & 1; - - if (pageCached[bufsel] != page) { - if (pageModified[bufsel]) - BufferToPageProgram(pageCached[bufsel]); - PageToBufferTransfer(page); -#ifdef DEBUG - Serial.print(" page read w/ cache: "); - Serial.println(page, HEX); -#endif - } - return readBuffer(bufsel, offset); + byte t; + read(addr, &t, 1); + return t; } void DataFlash::read(const unsigned long & addr, byte *buf, const long & n) { - long count = n, ulimit; + long count = n, n_to_read; word page = addr / bytesPerPage; // upper 12 bits - word offset = addr % bytesPerPage; // lower 10/9 bits - byte bufsel; - + word bufaddr = addr % bytesPerPage; // lower 10/9 bits +setSPIMode(); do { - bufsel = page & 1; - if (pageCached[bufsel] != page) { - if (pageModified[bufsel]) - BufferToPageProgram(pageCached[bufsel]); + if (pageCached != page) { + if (cacheModified) + BufferToPageProgram(pageCached); PageToBufferTransfer(page); -#ifdef DEBUG - Serial.print(" page read w/ cache: "); - Serial.println(page, HEX); +#ifdef DEBUG_READ + Serial.println(); + Serial.print(" read page "); + Serial.print(page, HEX); + Serial.print(" to cache "); #endif } - ulimit = min(bytesPerPage - offset, count); - readBuffer(bufsel, offset, buf, ulimit); - count -= ulimit; - page++; - offset = 0; + n_to_read = min(bytesPerPage - bufaddr, count); + readBuffer(bufaddr, buf, n_to_read); #ifdef DEBUG - if (count > 0) { - Serial.print(" read next page "); - Serial.print(page); - Serial.print(" for "); - Serial.print(count); - Serial.print(" bytes. "); - } + Serial.print(" n_to_read "); + Serial.print(n_to_read); + Serial.println(", "); #endif + buf += n_to_read; + count -= n_to_read; + page++; + bufaddr = 0; } while (count > 0); } void DataFlash::write(const unsigned long & addr, byte b) { - word page = addr / bytesPerPage; // upper 12 bits - word offset = addr % bytesPerPage; // lower 10/9 bits - byte bufsel = page & 1; + write(addr, &b, 1); - if (pageCached[bufsel] != page) { - if (pageModified[bufsel]) - BufferToPageProgram(pageCached[bufsel]); -#ifdef DEBUG - Serial.print("page read cache: "); - Serial.println(page, HEX); -#endif - PageToBufferTransfer(page); -#ifdef DEBUG - Serial.println(" Ok. "); -#endif - } - pageModified[bufsel] = true; - writeBuffer(bufsel, offset, b); } void DataFlash::write(const unsigned long & addr, byte *buf, const long & n) { word page = addr / bytesPerPage; // upper 12 bits - word offset = addr % bytesPerPage; // lower 10/9 bits - byte bufsel; - long ulimit, count = n; + word bufaddr = addr % bytesPerPage; // lower 10/9 bits + long count = n; + long n_to_write; + setSPIMode(); do { - bufsel = page & 1; - if (pageCached[bufsel] != page) { - if (pageModified[bufsel]) - BufferToPageProgram(pageCached[bufsel]); -#ifdef DEBUG - Serial.print("page read cache: "); + if (pageCached != page) { + if (cacheModified) + BufferToPageProgram(pageCached); +#ifdef DEBUG_WRITE + Serial.print(" ### page read cache: "); Serial.println(page, HEX); #endif PageToBufferTransfer(page); @@ -203,20 +171,26 @@ void DataFlash::write(const unsigned long & addr, byte *buf, const long & n) { Serial.println(" Ok. "); #endif } - ulimit = min(bytesPerPage - offset, count); - writeBuffer(bufsel, offset, buf, ulimit); - pageModified[bufsel] = true; - count -= ulimit; + + n_to_write = min(bytesPerPage - bufaddr, count); + writeBuffer(bufaddr, buf, n_to_write); + cacheModified = true; + +#ifdef DEBUG_WRITE + Serial.print(" ### n_to_write "); + Serial.print(n_to_write); + Serial.println(", "); +#endif + buf += n_to_write; + count -= n_to_write; page++; - offset = 0; + bufaddr = 0; } while (count > 0); } void DataFlash::flush() { - if (pageModified[0]) - BufferToPageProgram(pageCached[0]); - if (pageModified[1]) - BufferToPageProgram(pageCached[1]); + if (cacheModified) + BufferToPageProgram(pageCached); } /***************************************************************************** @@ -234,12 +208,9 @@ void DataFlash::flush() { * ******************************************************************************/ void DataFlash::PageToBufferTransfer(unsigned int page) { - byte bufsel = (page & 1) != 0; - pageCached[bufsel] = page & 0x7fff; - pageModified[bufsel] = false; - select(); //to reset DataFlash command decoder - byte cmd[] = { MainMemoryPagetoBuffer1Transfer, - MainMemoryPagetoBuffer2Transfer }; + pageCached = page & 0x7fff; + cacheModified = false; + csLow(); //to reset DataFlash command decoder #ifdef DEBUG Serial.print(" page "); Serial.print(pageCached[bufsel], DEC); @@ -247,7 +218,7 @@ void DataFlash::PageToBufferTransfer(unsigned int page) { Serial.print(bufsel); Serial.println(", "); #endif - SPI.transfer(cmd[bufsel]); // + SPI.transfer(MainMemoryPagetoBuffer1Transfer); // if (bytesPerPage == 528) { page = page << 2; } else { @@ -280,20 +251,15 @@ void DataFlash::PageToBufferTransfer(unsigned int page) { * internal SRAM buffers * ******************************************************************************/ -void DataFlash::readBuffer(const boolean bufsel, const word baddr, byte *data, - const word n) { -#ifdef DEBUG_MORE +void DataFlash::readBuffer(const word baddr, byte *data, const word n) { +#ifdef DEBUG Serial.print(" read buffer "); - Serial.print(bufsel+1, DEC); + Serial.print(bufsel, DEC); Serial.print(": "); - Serial.println(offaddr, HEX); + Serial.println(baddr, HEX); #endif - select(); - if (bufsel) { - SPI.transfer(Buffer2Read); //DF_SPI_RW(Buffer1Read); //buffer 1 read op-code - } else { - SPI.transfer(Buffer1Read); //DF_SPI_RW(Buffer1Read); - } + csLow(); + SPI.transfer(Buffer1Read); //DF_SPI_RW(Buffer1Read); SPI.transfer((byte) 0x00); //DF_SPI_RW(0x00); //don't cares SPI.transfer((byte) (baddr >> 8)); // DF_SPI_RW( //upper part of internal buffer address SPI.transfer((byte) baddr & 0xff); //DF_SPI_RW( //lower part of internal buffer address @@ -301,12 +267,12 @@ void DataFlash::readBuffer(const boolean bufsel, const word baddr, byte *data, for (int i = 0; i < n; i++) { *data++ = SPI.transfer((byte) 0x00); //DF_SPI_RW(0x00); //read byte } - deselect(); + csHigh(); } -byte DataFlash::readBuffer(const boolean bufsel, const word baddr) { +byte DataFlash::readBuffer(const word baddr) { byte b; - readBuffer(bufsel, baddr, &b, 1); + readBuffer(baddr, &b, 1); return b; } /***************************************************************************** @@ -403,21 +369,15 @@ byte DataFlash::readBuffer(const boolean bufsel, const word baddr) { * internal SRAM buffers * ******************************************************************************/ -void DataFlash::writeBuffer(const boolean bufsel, const word baddr, byte * data, - const word n) { -#ifdef DEBUG_MORE - Serial.print(" write buffer "); - Serial.print(bufsel+1, DEC); +void DataFlash::writeBuffer(const word baddr, byte * data, const word n) { +#ifdef DEBUG_WRITE + Serial.print(" ##write buffer "); Serial.print(": "); - Serial.println(offaddr, HEX); + Serial.println(baddr, HEX); #endif // deselect(); //make sure to toggle CS signal in order select(); //to reset DataFlash command decoder - if (bufsel) { - SPI.transfer(Buffer2Write); //buffer 2 write op-code - } else { - SPI.transfer(Buffer1Write); //buffer 1 write op-code - } + SPI.transfer(Buffer1Write); //buffer 1 write op-code SPI.transfer(0); // DF_SPI_RW(0x00); //don't cares @@ -430,8 +390,8 @@ void DataFlash::writeBuffer(const boolean bufsel, const word baddr, byte * data, deselect(); } -void DataFlash::writeBuffer(const boolean bufsel, const word baddr, byte data) { - writeBuffer(bufsel, baddr, &data, 1); +void DataFlash::writeBuffer(const word baddr, byte data) { + writeBuffer(baddr, &data, 1); } /***************************************************************************** * @@ -501,27 +461,19 @@ void DataFlash::writeBuffer(const boolean bufsel, const word baddr, byte data) { ******************************************************************************/ void DataFlash::BufferToPageProgram(unsigned int dstpage) { - byte bufsel = dstpage & 1; - pageCached[bufsel] = dstpage & 0x7fff; - pageModified[bufsel] = false; + pageCached = dstpage; + cacheModified = false; // deselect(); //make sure to toggle CS signal in order -#ifdef DEBUG - Serial.print(" buffer "); - Serial.print(bufsel); - Serial.print(" to page "); - Serial.print(pageCached[bufsel], DEC); +#ifdef DEBUG_WRITE + Serial.println(); + Serial.print(" ##buffer to page "); + Serial.print(pageCached, DEC); Serial.println(", "); #endif - select(); //to reset DataFlash command decoder - if (bufsel) { - SPI.transfer(Buffer2toMainMemoryPageProgramWithBuiltinErase); - //DF_SPI_RW(Buffer1toMainMemoryPageProgramWithBuiltinErase); - //buffer 1 to flash with erase op-code - } else { - SPI.transfer(Buffer1toMainMemoryPageProgramWithBuiltinErase); - //DF_SPI_RW(Buffer1toMainMemoryPageProgramWithBuiltinErase); - //buffer 1 to flash with erase op-code - } + csLow(); //to reset DataFlash command decoder + SPI.transfer(Buffer1toMainMemoryPageProgramWithBuiltinErase); + //DF_SPI_RW(Buffer1toMainMemoryPageProgramWithBuiltinErase); + //buffer 1 to flash with erase op-code if (bytesPerPage == 528) { dstpage = dstpage << 2; } else { @@ -690,6 +642,7 @@ void DataFlash::setSPIMode(void) { } void DataFlash::select() { + setSPIMode(); csLow(); } diff --git a/libraries/DataFlash_SPI/DataFlash_SPI.h b/libraries/DataFlash_SPI/DataFlash_SPI.h index 458df7d..a8a96f1 100755 --- a/libraries/DataFlash_SPI/DataFlash_SPI.h +++ b/libraries/DataFlash_SPI/DataFlash_SPI.h @@ -102,9 +102,8 @@ class DataFlash /*: Print */ { word bytesPerPage; byte bitWidthPerPage; // - word pageCached[2]; - boolean pageModified[2]; -// byte currentBuffer; + word pageCached; + boolean cacheModified; private: @@ -141,10 +140,10 @@ class DataFlash /*: Print */ { return SPI.transfer(0); } - void readBuffer(const boolean select, const word offset, byte * b, const word n); - inline byte readBuffer(const boolean select, const word offset); - void writeBuffer(const boolean select, const word offset, byte *b, const word n); - inline void writeBuffer(const boolean select, const word offset, byte b); + void readBuffer(const word offset, byte * b, const word n); + inline byte readBuffer(const word offset); + void writeBuffer(const word offset, byte *b, const word n); + inline void writeBuffer(const word offset, byte b); void BufferToPageProgram(unsigned int dstpage); void PageToBufferTransfer(unsigned int page);