Fixed and Moved reading HW deveui to lorawan.cpp

This commit is contained in:
Charles 2018-04-04 01:26:05 +02:00
parent 2edae9d51b
commit 16d015feae
2 changed files with 54 additions and 33 deletions

View File

@ -7,6 +7,10 @@
#include <lmic.h> #include <lmic.h>
#include <hal/hal.h> #include <hal/hal.h>
#ifdef MCP_24AA02E64_I2C_ADDRESS
#include <Wire.h> // Needed for 24AA02E64, does not hurt anything if included and not used
#endif
uint8_t mydata[] = "0000"; uint8_t mydata[] = "0000";
// Local logging Tag // Local logging Tag
@ -46,6 +50,37 @@ void RevBytes(unsigned char* b, size_t c)
b[c - 1 - i] = t; } 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 #ifdef VERBOSE
// Display a key // Display a key

View File

@ -29,7 +29,7 @@ Refer to LICENSE.txt file in repository for more details.
#include <set> #include <set>
// OLED driver // OLED driver
#include <U8x8lib.h> // includes <wire.h> if needed for other on board i2c components #include <U8x8lib.h>
// LMIC-Arduino LoRaWAN Stack // LMIC-Arduino LoRaWAN Stack
#include "loraconf.h" #include "loraconf.h"
@ -85,6 +85,8 @@ void loadConfig(void);
// defined in lorawan.cpp // defined in lorawan.cpp
void gen_lora_deveui(uint8_t * pdeveui); void gen_lora_deveui(uint8_t * pdeveui);
void RevBytes(unsigned char* b, size_t c); void RevBytes(unsigned char* b, size_t c);
void get_hard_deveui(uint8_t *pdeveui);
#ifdef VERBOSE #ifdef VERBOSE
void printKeys(void); void printKeys(void);
@ -103,12 +105,20 @@ void os_getArtEui (u1_t *buf) {
void os_getDevEui (u1_t* buf) { void os_getDevEui (u1_t* buf) {
int i=0, k=0; int i=0, k=0;
memcpy(buf, DEVEUI, 8); // get fixed DEVEUI from loraconf.h 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]; k += buf[i];
if (k) }
if (k) {
RevBytes(buf, 8); // use fixed DEVEUI and swap bytes to LSB format RevBytes(buf, 8); // use fixed DEVEUI and swap bytes to LSB format
else } else {
gen_lora_deveui(buf); // generate DEVEUI from device's MAC 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 // LMIC enhanced Pin mapping
@ -457,30 +467,6 @@ void setup() {
antenna_init(); antenna_init();
#endif #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 // initialize display
init_display(PROGNAME, PROGVERSION); init_display(PROGNAME, PROGVERSION);
u8x8.setPowerSave(!cfg.screenon); // set display off if disabled u8x8.setPowerSave(!cfg.screenon); // set display off if disabled