#include "units/portexp.hpp"
#include "units/uart1.hpp"
#include "units/modbus.hpp"
+#include "units/rtc8563.hpp"
extern "C" {
void __cxa_pure_virtual () {
I2c i2cSparkfun(I2c::SparkFunEnvCombo);
I2c i2cMaster(I2c::Master);
I2c i2cSlave(I2c::Slave);
+ Rtc8563 rtc8563;
}
#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
--- /dev/null
+#include <stdio.h>
+#include <avr/io.h>
+#include <avr/pgmspace.h>
+#include <util/delay.h>
+#include <math.h>
+
+#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
+