From 8fcd880a4ad5c2b59b3567abb7369153a1faceb8 Mon Sep 17 00:00:00 2001 From: Manfred Steiner Date: Wed, 11 Sep 2024 09:52:25 +0200 Subject: [PATCH] software/nano-1284/test_2024-07-23 (detect hardware version) --- .../nano-644/test_2024-07-23/src/main.cpp | 30 +++++++++++++++++++ .../nano-644/test_2024-07-23/src/main.hpp | 2 ++ 2 files changed, 32 insertions(+) diff --git a/software/nano-644/test_2024-07-23/src/main.cpp b/software/nano-644/test_2024-07-23/src/main.cpp index 2971b6e..00a2df7 100644 --- a/software/nano-644/test_2024-07-23/src/main.cpp +++ b/software/nano-644/test_2024-07-23/src/main.cpp @@ -42,6 +42,8 @@ const char PSTR_LINEFEED[] PROGMEM = "\n"; const char PSTR_ERROR[] PROGMEM = "ERROR"; const char PSTR_Done[] PROGMEM = "Done"; +uint8_t hardwareVersion = 0; + extern "C" { void __cxa_pure_virtual () {} int __cxa_guard_acquire(uint8_t *g) { return 0; } @@ -145,6 +147,25 @@ extern "C" { Cc1101 cc1101Receive(Cc1101::Receive); } +uint8_t detectHardwareVersion () { + ADMUX = (1 << ADLAR) | (1 << REFS0) | 7; // ADC7, VREF=AVCC=3.3V + ADCSRA = (1 << ADEN) | 7; // ADC Enable, Prescaler 128 + ADCSRA |= (1 << ADSC); // start ADC + while (ADCSRA & (1 << ADSC)) {} // wait for result + hardwareVersion = 0; // unknown board version + // printf("Hardware-Version: ADC7H = %d\n", ADCH); + if (ADCH >= 0xf0) { + hardwareVersion = 1; + } else if (ADCH >= 0xe0) { + hardwareVersion = 2; + } else { + printf("Invalid Hardware-Version: ADC7H = %d\n", ADCH); + } + ADMUX = 0; + ADCSRA = 0; + return hardwareVersion; +} + void setTimer (uint32_t ms) { ATOMIC_BLOCK(ATOMIC_FORCEON) { timer1ms = ms; @@ -216,6 +237,8 @@ int main () { }; #endif + detectHardwareVersion(); + while (1) { uint16_t i; char s[4]; @@ -226,6 +249,13 @@ int main () { printf_P(MAIN_CPP_DATE); printf_P(PSTR(" / ")); printf_P(MAIN_CPP_TIME); printf_P(PSTR_DIVIDER); + if (hardwareVersion >= 1 && hardwareVersion <= 2) { + printf_P(PSTR("Hardware: %d"), hardwareVersion); + } else { + printf_P(PSTR("ERROR: Invalid Hardware (%d)"), hardwareVersion); + for(;;); + } + printf_P(PSTR_DIVIDER); printf_P(PSTR_LINEFEED); printf_P(PSTR("Available units:\n\n")); for (i = 0; i < sizeof(unit) / sizeof(unit[0]); i++) { diff --git a/software/nano-644/test_2024-07-23/src/main.hpp b/software/nano-644/test_2024-07-23/src/main.hpp index a7f2ae8..7eeff16 100644 --- a/software/nano-644/test_2024-07-23/src/main.hpp +++ b/software/nano-644/test_2024-07-23/src/main.hpp @@ -12,6 +12,8 @@ extern int wait (uint32_t ms); extern int waitAndReadKey (uint32_t ms); extern uint64_t millis (); +extern uint8_t hardwareVersion; + extern const char PSTR_DIVIDER[]; extern const char PSTR_LINEFEED[]; extern const char PSTR_ERROR[]; -- 2.39.5