From: Manfred Steiner Date: Tue, 6 Aug 2024 15:58:41 +0000 (+0200) Subject: Test-Software Unit RTC-8563 X-Git-Url: https://git.htl-mechatronik.at/public/?a=commitdiff_plain;h=355604c6439406d877899ca3af745cb4feea51a6;p=nano-x-base.git Test-Software Unit RTC-8563 --- 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 23bcc90..4b6b734 100644 --- a/software/nano-644/test_2024-07-23/src/main.cpp +++ b/software/nano-644/test_2024-07-23/src/main.cpp @@ -22,6 +22,7 @@ #include "units/portexp.hpp" #include "units/uart1.hpp" #include "units/modbus.hpp" +#include "units/rtc8563.hpp" extern "C" { void __cxa_pure_virtual () { @@ -97,6 +98,7 @@ extern "C" { I2c i2cSparkfun(I2c::SparkFunEnvCombo); I2c i2cMaster(I2c::Master); I2c i2cSlave(I2c::Slave); + Rtc8563 rtc8563; } @@ -153,7 +155,7 @@ int main () { #ifdef __AVR_ATmega644P__ TestUnit *unit[] = { &led, &sw, &rgb, &seg7, &poti, &encoder, &r2r, &motor, &portExp, &lcd, &uart1, &modbus, &ieee485, - &i2cMaster, &i2cSlave, &i2cSparkfun + &i2cMaster, &i2cSlave, &i2cSparkfun, &rtc8563 }; #endif diff --git a/software/nano-644/test_2024-07-23/src/units/rtc8563.cpp b/software/nano-644/test_2024-07-23/src/units/rtc8563.cpp new file mode 100644 index 0000000..ab08131 --- /dev/null +++ b/software/nano-644/test_2024-07-23/src/units/rtc8563.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include + +#include "rtc8563.hpp" +#include "../adafruit/bme280.h" +#include "../main.hpp" + +// RTC BME653EMA on Nano-644 + +void Rtc8563::handleTwiIrq () { + TWCR |= (1 << TWINT); // clear Interrupt Request +} + +#ifdef __AVR_ATmega328P__ +void Modbus::init () {} +void Modbus::cleanup () {} +int8_t Modbus::run (uint8_t subtest) { return -1; } +void Modbus::handleRxByte (uint8_t b) {} +#endif + +#ifdef __AVR_ATmega644P__ + +void Rtc8563::init () { + PORTC |= (1 << PC7); // nInt + DDRC &= ~(1 << PC7); + TWBR = 13; // 100kHz (TWPS1:0 = 00), TWBR = (F_CPU - 16 * 100000) / (2 * 100000 * 4); + TWBR = 28; // 50kHz (TWPS1:0 = 00), TWBR = (F_CPU - 16 * 50000) / (2 * 50000 * 4); + TWBR = 100; // 50kHz (TWPS1:0 = 00), TWBR = (F_CPU - 16 * 50000) / (2 * 50000 * 4); + TWCR = (1 << TWEN); + enabled = true; +} + +void Rtc8563::cleanup () { + enabled = false; + TWCR = (1 << TWEN); + TWBR = 0; + PORTC &= ~(1 << PC7); + DDRC &= ~(1 << PC7); +} + +uint8_t Rtc8563::bcd2bin (uint8_t value) { + return (value >> 4) * 10 + (value & 0x0f); +} + +int8_t Rtc8563::run (uint8_t subtest) { + if (subtest == 0) { + // printf_P(PSTR(" BM280 ... ")); + rtc8563.begin(0x51); + + uint8_t buffer[16] = { + /* config -> */ 0x00, 0x00, + /* enable timer -> */ 0x0d, 0x83, + /* set clock -> */ 0x02, 0x00, 0x00, 0x08, 0x06, 0x02, 0x08, 0x24 + } ; + + printf_P(PSTR("\n => init write: 0x00:0x00 0x00; 0x0d:0x83 0x80 => ")); + if (!rtc8563.write(buffer, 2) || !rtc8563.write(&buffer[2], 2)) { + // if (!rtc8563.write(&buffer[2], 2)) { + printf_P(PSTR("E1")); + } else { + printf_P(PSTR("done")); + } + + + printf_P(PSTR("\n => write clock ... ")); + if (!rtc8563.write(&buffer[4], 8)) { + // if (!rtc8563.write(&buffer[2], 2)) { + printf_P(PSTR("E1")); + } else { + printf_P(PSTR("done")); + } + + + do { + printf_P(PSTR("\n => read: 0x02:")); + buffer[0] = 2; + if (!rtc8563.write_then_read(buffer, 1, buffer, 7)) { + printf_P(PSTR("E2")); + } else { + for (uint8_t i = 0; i < 7; i++) { + printf_P(PSTR(" 0x%02x"), buffer[i]); + } + uint8_t sec = bcd2bin(buffer[0]); + uint8_t min = bcd2bin(buffer[1]); + uint8_t hrs = bcd2bin(buffer[2]); + uint8_t days = bcd2bin(buffer[3]); + uint8_t weekDay = bcd2bin(buffer[4]); + uint8_t month = bcd2bin(buffer[5]); + uint16_t year = 2000 + bcd2bin(buffer[6]); + PGM_P d; + switch (weekDay) { + case 0: d = PSTR("So"); break; + case 1: d = PSTR("Mo"); break; + case 2: d = PSTR("Di"); break; + case 3: d = PSTR("Mi"); break; + case 4: d = PSTR("Do"); break; + case 5: d = PSTR("Fr"); break; + case 6: d = PSTR("Sa"); break; + default: d = PSTR("??"); break; + } + printf_P(PSTR(" --> ")); + printf_P(d); + printf_P(PSTR(", %04d-%02d-%02d %02d:%02d:%02d"), year, month, days, hrs, min, sec); + } + } while (wait(1000) == EOF); + + return 0; + } + + return -1; +} + +#endif + diff --git a/software/nano-644/test_2024-07-23/src/units/rtc8563.hpp b/software/nano-644/test_2024-07-23/src/units/rtc8563.hpp new file mode 100644 index 0000000..6155831 --- /dev/null +++ b/software/nano-644/test_2024-07-23/src/units/rtc8563.hpp @@ -0,0 +1,32 @@ +#ifndef RTC8563_HPP +#define RTC8563_HPP + +#include +#include "../main.hpp" +#include "../adafruit/bme280.h" +#include "../adafruit/ens160.h" +#include "../i2cmaster.hpp" +#include "../i2cslave.hpp" + + +class Rtc8563 : public TestUnit { + private: + I2cMaster rtc8563; + + public: + bool enabled; + + public: + Rtc8563 () { enabled = false; } + void tick1ms () { rtc8563.tick1ms(); } + virtual void init (); + virtual void cleanup (); + virtual int8_t run (uint8_t subtest); + virtual PGM_P getName () { return PSTR("RTC-8563"); }; + void handleTwiIrq (); + + private: + uint8_t bcd2bin (uint8_t value); +}; + +#endif \ No newline at end of file