mirror of
https://github.com/thelsing/knx.git
synced 2025-01-21 00:05:43 +01:00
* generate unique serial number (#90) * see https://github.com/ricaun/ArduinoUniqueID * calculated from ESP.getEfuseMac() on ESP32 * ESP.getChipId() on ESP8266 * SERIAL_NUMBER_WORD_0-3 on SAMD * HAL_GetUIDw0-2() on STM32 * defaults to 0x01020304 on other platforms * fix variable name for ESP platform * another fix variable name for ESP platform (need more coffee...)
This commit is contained in:
parent
036bd54c79
commit
e57bbf9dbe
@ -37,6 +37,18 @@ void Esp32Platform::macAddress(uint8_t * addr)
|
||||
esp_wifi_get_mac(WIFI_IF_STA, addr);
|
||||
}
|
||||
|
||||
uint32_t Esp32Platform::uniqueSerialNumber()
|
||||
{
|
||||
uint64_t chipid = ESP.getEfuseMac();
|
||||
uint32_t upperId = (chipid >> 32) & 0xFFFFFFFF;
|
||||
uint32_t lowerId = (chipid & 0xFFFFFFFF);
|
||||
uint32_t uniqueId = (upperId ^ lowerId);
|
||||
|
||||
Serial.printf("uniqueSerialNumber: %0X ^ %0X ==> %0X\n", upperId, lowerId, uniqueId);
|
||||
|
||||
return uniqueId;
|
||||
}
|
||||
|
||||
void Esp32Platform::restart()
|
||||
{
|
||||
println("restart");
|
||||
|
@ -16,6 +16,9 @@ public:
|
||||
uint32_t currentDefaultGateway() override;
|
||||
void macAddress(uint8_t* addr) override;
|
||||
|
||||
// unique serial number
|
||||
uint32_t uniqueSerialNumber() override;
|
||||
|
||||
// basic stuff
|
||||
void restart();
|
||||
|
||||
|
@ -38,6 +38,15 @@ void EspPlatform::macAddress(uint8_t * addr)
|
||||
wifi_get_macaddr(STATION_IF, addr);
|
||||
}
|
||||
|
||||
uint32_t EspPlatform::uniqueSerialNumber()
|
||||
{
|
||||
uint32_t chipid = ESP.getChipId();
|
||||
|
||||
Serial.printf("uniqueSerialNumber: %0X\n", chipid);
|
||||
|
||||
return chipid;
|
||||
}
|
||||
|
||||
void EspPlatform::restart()
|
||||
{
|
||||
println("restart");
|
||||
|
@ -16,6 +16,9 @@ class EspPlatform : public ArduinoPlatform
|
||||
uint32_t currentDefaultGateway() override;
|
||||
void macAddress(uint8_t* addr) override;
|
||||
|
||||
// unique serial number
|
||||
uint32_t uniqueSerialNumber() override;
|
||||
|
||||
// basic stuff
|
||||
void restart();
|
||||
|
||||
@ -30,7 +33,7 @@ class EspPlatform : public ArduinoPlatform
|
||||
void commitToEeprom();
|
||||
private:
|
||||
WiFiUDP _udp;
|
||||
uint32_t _mulitcastAddr;
|
||||
uint32_t _mulitcastAddr;
|
||||
uint16_t _mulitcastPort;
|
||||
};
|
||||
|
||||
|
@ -72,6 +72,11 @@ uint32_t Platform::currentDefaultGateway()
|
||||
void Platform::macAddress(uint8_t *data)
|
||||
{}
|
||||
|
||||
uint32_t Platform::uniqueSerialNumber()
|
||||
{
|
||||
return 0x01020304;
|
||||
}
|
||||
|
||||
void Platform::setupMultiCast(uint32_t addr, uint16_t port)
|
||||
{}
|
||||
|
||||
|
@ -20,6 +20,9 @@ class Platform
|
||||
virtual uint32_t currentDefaultGateway();
|
||||
virtual void macAddress(uint8_t* data);
|
||||
|
||||
// unique serial number
|
||||
virtual uint32_t uniqueSerialNumber();
|
||||
|
||||
// basic stuff
|
||||
virtual void restart() = 0;
|
||||
virtual void fatalError() = 0;
|
||||
|
@ -49,18 +49,21 @@ template <class P, class B> class KnxFacade : private SaveRestore
|
||||
KnxFacade() : _platformPtr(new P()), _bauPtr(new B(*_platformPtr)), _bau(*_bauPtr)
|
||||
{
|
||||
manufacturerId(0xfa);
|
||||
bauNumber(platform().uniqueSerialNumber());
|
||||
_bau.addSaveRestore(this);
|
||||
}
|
||||
|
||||
KnxFacade(B& bau) : _bau(bau)
|
||||
{
|
||||
manufacturerId(0xfa);
|
||||
bauNumber(platform().uniqueSerialNumber());
|
||||
_bau.addSaveRestore(this);
|
||||
}
|
||||
|
||||
KnxFacade(IsrFunctionPtr buttonISRFunction) : _platformPtr(new P()), _bauPtr(new B(*_platformPtr)), _bau(*_bauPtr)
|
||||
{
|
||||
manufacturerId(0xfa);
|
||||
bauNumber(platform().uniqueSerialNumber());
|
||||
_bau.addSaveRestore(this);
|
||||
setButtonISRFunction(buttonISRFunction);
|
||||
}
|
||||
@ -221,7 +224,7 @@ template <class P, class B> class KnxFacade : private SaveRestore
|
||||
{
|
||||
_bau.deviceObject().bauNumber(value);
|
||||
}
|
||||
|
||||
|
||||
void orderNumber(const uint8_t* value)
|
||||
{
|
||||
_bau.deviceObject().orderNumber(value);
|
||||
@ -231,7 +234,7 @@ template <class P, class B> class KnxFacade : private SaveRestore
|
||||
{
|
||||
_bau.deviceObject().hardwareType(value);
|
||||
}
|
||||
|
||||
|
||||
void version(uint16_t value)
|
||||
{
|
||||
_bau.deviceObject().version(value);
|
||||
|
@ -17,6 +17,30 @@ SamdPlatform::SamdPlatform( HardwareSerial* s) : ArduinoPlatform(s)
|
||||
{
|
||||
}
|
||||
|
||||
uint32_t SamdPlatform::uniqueSerialNumber()
|
||||
{
|
||||
#if defined (__SAMD51__)
|
||||
// SAMD51 from section 9.6 of the datasheet
|
||||
#define SERIAL_NUMBER_WORD_0 *(volatile uint32_t*)(0x008061FC)
|
||||
#define SERIAL_NUMBER_WORD_1 *(volatile uint32_t*)(0x00806010)
|
||||
#define SERIAL_NUMBER_WORD_2 *(volatile uint32_t*)(0x00806014)
|
||||
#define SERIAL_NUMBER_WORD_3 *(volatile uint32_t*)(0x00806018)
|
||||
#else
|
||||
//#elif defined (__SAMD21E17A__) || defined(__SAMD21G18A__) || defined(__SAMD21E18A__) || defined(__SAMD21J18A__)
|
||||
// SAMD21 from section 9.3.3 of the datasheet
|
||||
#define SERIAL_NUMBER_WORD_0 *(volatile uint32_t*)(0x0080A00C)
|
||||
#define SERIAL_NUMBER_WORD_1 *(volatile uint32_t*)(0x0080A040)
|
||||
#define SERIAL_NUMBER_WORD_2 *(volatile uint32_t*)(0x0080A044)
|
||||
#define SERIAL_NUMBER_WORD_3 *(volatile uint32_t*)(0x0080A048)
|
||||
#endif
|
||||
|
||||
uint32_t uniqueId = SERIAL_NUMBER_WORD_0 ^ SERIAL_NUMBER_WORD_1 ^ SERIAL_NUMBER_WORD_2 ^ SERIAL_NUMBER_WORD_3;
|
||||
|
||||
printf("uniqueSerialNumber: %0X\n", uniqueId);
|
||||
|
||||
return uniqueId;
|
||||
}
|
||||
|
||||
void SamdPlatform::restart()
|
||||
{
|
||||
println("restart");
|
||||
|
@ -10,6 +10,9 @@ public:
|
||||
SamdPlatform();
|
||||
SamdPlatform( HardwareSerial* s);
|
||||
|
||||
// unique serial number
|
||||
uint32_t uniqueSerialNumber() override;
|
||||
|
||||
void restart();
|
||||
uint8_t* getEepromBuffer(uint16_t size);
|
||||
void commitToEeprom();
|
||||
|
@ -20,6 +20,15 @@ Stm32Platform::~Stm32Platform()
|
||||
delete [] _eepromPtr;
|
||||
}
|
||||
|
||||
uint32_t Stm32Platform::uniqueSerialNumber()
|
||||
{
|
||||
uint32_t uniqueId = HAL_GetUIDw0() ^ HAL_GetUIDw1() ^ HAL_GetUIDw2();
|
||||
|
||||
printf("uniqueSerialNumber: %0X", uniqueId);
|
||||
|
||||
return uniqueId;
|
||||
}
|
||||
|
||||
void Stm32Platform::restart()
|
||||
{
|
||||
NVIC_SystemReset();
|
||||
|
@ -8,6 +8,9 @@ public:
|
||||
Stm32Platform( HardwareSerial* s);
|
||||
~Stm32Platform();
|
||||
|
||||
// unique serial number
|
||||
uint32_t uniqueSerialNumber() override;
|
||||
|
||||
// basic stuff
|
||||
void restart();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user