Unique MPU id for due

This commit is contained in:
2018-11-12 02:21:38 +03:00
parent 2509075803
commit 1c3a4421f3
4 changed files with 62 additions and 11 deletions

View File

@@ -121,6 +121,7 @@ const char outprefix[] PROGMEM = OUTTOPIC;
const char inprefix[] PROGMEM = INTOPIC; const char inprefix[] PROGMEM = INTOPIC;
const char configserver[] PROGMEM = CONFIG_SERVER; const char configserver[] PROGMEM = CONFIG_SERVER;
unsigned int UniqueID[5] = {0,0,0,0,0};
aJsonObject *root = NULL; aJsonObject *root = NULL;
aJsonObject *items = NULL; aJsonObject *items = NULL;
@@ -1200,6 +1201,19 @@ void printFirmwareVersionAndBuildOptions() {
debugSerial<<F("\n(-)RESTART_LAN_ON_MQTT_ERRORS"); debugSerial<<F("\n(-)RESTART_LAN_ON_MQTT_ERRORS");
#endif #endif
debugSerial<<eol; debugSerial<<eol;
// WDT_Disable( WDT ) ;
Serial.println("Reading 128 bits unique identifier \n\r" ) ;
ReadUniqueID( UniqueID ) ;
Serial.print ("ID: ") ;
for (byte b = 0 ; b < 4 ; b++)
Serial.print ((unsigned int) UniqueID [b], HEX) ;
Serial.println () ;
} }
void printFreeRam(){ void printFreeRam(){
debugSerial<<F("\nfree RAM: ")<<freeRam(); debugSerial<<F("\nfree RAM: ")<<freeRam();

View File

@@ -33,11 +33,11 @@ extern "C" {
void PrintBytes(uint8_t *addr, uint8_t count, bool newline) { void PrintBytes(uint8_t *addr, uint8_t count, bool newline) {
for (uint8_t i = 0; i < count; i++) { for (uint8_t i = 0; i < count; i++) {
debugSerial<<_HEX(addr[i] >> 4); Serial.print(addr[i] >> 4, HEX);
debugSerial<<_HEX(addr[i] & 0x0f); Serial.print(addr[i] & 0x0f, HEX);
} }
if (newline) if (newline)
debugSerial<<eol; Serial.println();
} }
const char HEXSTR[] = "0123456789ABCDEF"; const char HEXSTR[] = "0123456789ABCDEF";
@@ -86,7 +86,7 @@ unsigned long freeRam ()
{return system_get_free_heap_size();} {return system_get_free_heap_size();}
#endif #endif
#if defined(ARDUINO_ARCH_AVR) #if defined(__AVR__)
unsigned long freeRam () unsigned long freeRam ()
{ {
extern int __heap_start, *__brkval; extern int __heap_start, *__brkval;
@@ -103,6 +103,7 @@ unsigned long freeRam() {
char *heapend = sbrk(0); char *heapend = sbrk(0);
register char *stack_ptr asm( "sp" ); register char *stack_ptr asm( "sp" );
struct mallinfo mi = mallinfo(); struct mallinfo mi = mallinfo();
return stack_ptr - heapend + mi.fordblks; return stack_ptr - heapend + mi.fordblks;
} }
@@ -140,7 +141,7 @@ void printFloatValueToStr(float value, char *valstr) {
#if defined(ESP8266) || defined(ARDUINO_ARCH_ESP32) #if defined(ESP8266) || defined(ARDUINO_ARCH_ESP32)
sprintf(valstr, "%2.1f", value); sprintf(valstr, "%2.1f", value);
#endif #endif
#if defined(ARDUINO_ARCH_AVR) #if defined(__AVR__)
sprintf(valstr, "%d", (int)value); sprintf(valstr, "%d", (int)value);
int fractional = 10.0*((float)abs(value)-(float)abs((int)value)); int fractional = 10.0*((float)abs(value)-(float)abs((int)value));
int val_len =strlen(valstr); int val_len =strlen(valstr);
@@ -155,8 +156,6 @@ void printFloatValueToStr(float value, char *valstr) {
#define ARDBUFFER 16 //Buffer for storing intermediate strings. Performance may vary depending on size. #define ARDBUFFER 16 //Buffer for storing intermediate strings. Performance may vary depending on size.
#ifndef ARDUINO_ARCH_STM32F1
int log(const char *str, ...)//TODO: __FlashStringHelper str support int log(const char *str, ...)//TODO: __FlashStringHelper str support
{ {
int i, count=0, j=0, flag=0; int i, count=0, j=0, flag=0;
@@ -171,7 +170,7 @@ int log(const char *str, ...)//TODO: __FlashStringHelper str support
{ {
//Clear buffer //Clear buffer
temp[j] = '\0'; temp[j] = '\0';
debugSerial<<temp; Serial.print(temp);
j=0; j=0;
temp[0] = '\0'; temp[0] = '\0';
@@ -205,10 +204,47 @@ int log(const char *str, ...)//TODO: __FlashStringHelper str support
} }
}; };
debugSerial<<eol; Serial.println(); //Print trailing newline
return count + 1; //Return number of arguments detected return count + 1; //Return number of arguments detected
} }
/* Code borrowed from http://forum.arduino.cc/index.php?topic=289190.0
Awesome work Mark T!*/
__attribute__ ((section (".ramfunc")))
void ReadUniqueID( unsigned int * pdwUniqueID )
{
unsigned int status ;
#if defined(__SAM3X8E__)
/* Send the Start Read unique Identifier command (STUI) by writing the Flash Command Register with the STUI command.*/
EFC1->EEFC_FCR = (0x5A << 24) | EFC_FCMD_STUI;
do
{
status = EFC1->EEFC_FSR ;
} while ( (status & EEFC_FSR_FRDY) == EEFC_FSR_FRDY ) ;
/* The Unique Identifier is located in the first 128 bits of the Flash memory mapping. So, at the address 0x400000-0x400003. */
pdwUniqueID[0] = *(uint32_t *)IFLASH1_ADDR;
pdwUniqueID[1] = *(uint32_t *)(IFLASH1_ADDR + 4);
pdwUniqueID[2] = *(uint32_t *)(IFLASH1_ADDR + 8);
pdwUniqueID[3] = *(uint32_t *)(IFLASH1_ADDR + 12);
/* To stop the Unique Identifier mode, the user needs to send the Stop Read unique Identifier
command (SPUI) by writing the Flash Command Register with the SPUI command. */
EFC1->EEFC_FCR = (0x5A << 24) | EFC_FCMD_SPUI ;
/* When the Stop read Unique Unique Identifier command (SPUI) has been performed, the
FRDY bit in the Flash Programming Status Register (EEFC_FSR) rises. */
do
{
status = EFC1->EEFC_FSR ;
} while ( (status & EEFC_FSR_FRDY) != EEFC_FSR_FRDY ) ;
#endif #endif
}
#pragma message(VAR_NAME_VALUE(debugSerial)) #pragma message(VAR_NAME_VALUE(debugSerial))

View File

@@ -41,3 +41,4 @@ unsigned long freeRam ();
void parseBytes(const char* str, char separator, byte* bytes, int maxBytes, int base); void parseBytes(const char* str, char separator, byte* bytes, int maxBytes, int base);
int log(const char *str, ...); int log(const char *str, ...);
void printFloatValueToStr(float value, char *valstr); void printFloatValueToStr(float value, char *valstr);
void ReadUniqueID( unsigned int * pdwUniqueID );

View File

@@ -9,14 +9,14 @@
; http://docs.platformio.org/page/projectconf.html ; http://docs.platformio.org/page/projectconf.html
[platformio] [platformio]
src_dir = lighthub src_dir = lighthub
;env_default = env_default =
; megaatmega2560 ; megaatmega2560
; megaatmega2560-net ; megaatmega2560-net
; due ; due
; esp8266 ; esp8266
; esp32 ; esp32
; megaatmega2560-5500 ; megaatmega2560-5500
; due-5500 due-5500
; controllino ; controllino
; stm32 ; stm32