From 16d015feae31ee20e5f010746767f53b20a897e9 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 4 Apr 2018 01:26:05 +0200 Subject: [PATCH] Fixed and Moved reading HW deveui to lorawan.cpp --- src/lorawan.cpp | 41 ++++++++++++++++++++++++++++++++++++++--- src/main.cpp | 46 ++++++++++++++++------------------------------ 2 files changed, 54 insertions(+), 33 deletions(-) diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 0f0f86eb..6fde8a50 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -7,6 +7,10 @@ #include #include +#ifdef MCP_24AA02E64_I2C_ADDRESS +#include // Needed for 24AA02E64, does not hurt anything if included and not used +#endif + uint8_t mydata[] = "0000"; // Local logging Tag @@ -31,9 +35,9 @@ void gen_lora_deveui(uint8_t *pdeveui) { *p++ = 0xFF; *p++ = 0xFE; // Then next 6 bytes are mac address reversed - for ( i=0; i<6 ; i++) { - *p++ = dmac[5-i]; - } + for ( i=0; i<6 ; i++) { + *p++ = dmac[5-i]; + } } // Function to do a byte swap in a byte array @@ -46,6 +50,37 @@ void RevBytes(unsigned char* b, size_t c) b[c - 1 - i] = t; } } +void get_hard_deveui(uint8_t *pdeveui) { + // read DEVEUI from Microchip 24AA02E64 2Kb serial eeprom if present +#ifdef MCP_24AA02E64_I2C_ADDRESS + uint8_t i2c_ret; + // Init this just in case, no more to 100KHz + Wire.begin(OLED_SDA, OLED_SCL, 100000); + Wire.beginTransmission(MCP_24AA02E64_I2C_ADDRESS); + Wire.write(MCP_24AA02E64_MAC_ADDRESS); + i2c_ret = Wire.endTransmission(); + // check if device seen on i2c bus + if (i2c_ret == 0) { + char deveui[32]=""; + uint8_t data; + Wire.beginTransmission(MCP_24AA02E64_I2C_ADDRESS); + Wire.write(MCP_24AA02E64_MAC_ADDRESS); + Wire.requestFrom(MCP_24AA02E64_I2C_ADDRESS, 8); + while (Wire.available()) { + data = Wire.read(); + sprintf(deveui+strlen(deveui), "%02X ", data) ; + *pdeveui++ = data; + } + i2c_ret = Wire.endTransmission(); + ESP_LOGI(TAG, "Serial EEPROM 24AA02E64 found, read DEVEUI %s", deveui); + } else { + ESP_LOGI(TAG, "Serial EEPROM 24AA02E64 not found ret=%d", i2c_ret); + } + // Set back to 400KHz to speed up OLED + Wire.setClock(400000); +#endif // MCP 24AA02E64 +} + #ifdef VERBOSE // Display a key diff --git a/src/main.cpp b/src/main.cpp index d808640b..98be1130 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,7 +29,7 @@ Refer to LICENSE.txt file in repository for more details. #include // OLED driver -#include // includes if needed for other on board i2c components +#include // LMIC-Arduino LoRaWAN Stack #include "loraconf.h" @@ -85,6 +85,8 @@ void loadConfig(void); // defined in lorawan.cpp void gen_lora_deveui(uint8_t * pdeveui); void RevBytes(unsigned char* b, size_t c); +void get_hard_deveui(uint8_t *pdeveui); + #ifdef VERBOSE void printKeys(void); @@ -103,12 +105,20 @@ void os_getArtEui (u1_t *buf) { void os_getDevEui (u1_t* buf) { int i=0, k=0; memcpy(buf, DEVEUI, 8); // get fixed DEVEUI from loraconf.h - for (i=0; i<8 ; i++) + for (i=0; i<8 ; i++) { k += buf[i]; - if (k) + } + if (k) { RevBytes(buf, 8); // use fixed DEVEUI and swap bytes to LSB format - else + } else { gen_lora_deveui(buf); // generate DEVEUI from device's MAC + } + + // Get MCP 24AA02E64 hardware DEVEUI (override default settings if found) + #ifdef MCP_24AA02E64_I2C_ADDRESS + get_hard_deveui(buf); + RevBytes(buf, 8); // swap bytes to LSB format + #endif } // LMIC enhanced Pin mapping @@ -413,7 +423,7 @@ void setup() { ESP_LOGI(TAG, "Starting %s %s", PROGNAME, PROGVERSION); rgb_set_color(COLOR_NONE); - + // initialize system event handler for wifi task, needed for wifi_sniffer_init() esp_event_loop_init(NULL, NULL); @@ -457,37 +467,13 @@ void setup() { antenna_init(); #endif - // read DEVEUI from Microchip 24AA02E64 2Kb serial eeprom if present -#ifdef MCP_24AA02E64_I2C_ADDRESS - uint8_t i2c_ret; - // Init this before OLED, we just need to get value then we're done with i2c bus - Wire.begin(OLED_SDA, OLED_SDA, 100000); - Wire.beginTransmission(MCP_24AA02E64_I2C_ADDRESS); - Wire.write(MCP_24AA02E64_MAC_ADDRESS); - i2c_ret = Wire.endTransmission(); - // check if device seen on i2c bus - if (i2c_ret == 0) { - char deveui[24]; - uint8_t data; - Wire.beginTransmission(MCP_24AA02E64_I2C_ADDRESS); - while (Wire.available()) { - data = Wire.read(); - sprintf(deveui+strlen(deveui), "%02X ", data) ; - } - i2c_ret = Wire.endTransmission(); - ESP_LOGI(TAG, "Serial EEPROM 24AA02E64 found, read DEVEUI %s", deveui); - } else { - ESP_LOGI(TAG, "Serial EEPROM 24AA02E64 not found ret=%d", i2c_ret); - } -#endif // MCP 24AA02E64 - // initialize display init_display(PROGNAME, PROGVERSION); u8x8.setPowerSave(!cfg.screenon); // set display off if disabled u8x8.setCursor(0,5); u8x8.printf(!cfg.rssilimit ? "RLIM: off" : "RLIM: %4i", cfg.rssilimit); u8x8.drawString(0,6,"Join Wait "); - + // output LoRaWAN keys to console #ifdef VERBOSE printKeys();