Skip to content

Commit

Permalink
RTC support for STM32
Browse files Browse the repository at this point in the history
  • Loading branch information
pa-pa committed Jul 16, 2018
1 parent cd9bfed commit 01c4fcf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 53 deletions.
4 changes: 2 additions & 2 deletions Activity.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ class SleepRTC : public Idle<true> {
else if( ticks > millis2ticks(30) ) { sleeptime = SLEEP_30MS; }
else if( ticks > millis2ticks(15) ) { sleeptime = SLEEP_15MS; }

uint16_t c1 = rtc.getCounter(true);
uint32_t c1 = rtc.getCounter(true);
LowPower.powerExtStandby(sleeptime,ADC_OFF,BOD_OFF,TIMER2_ON);
uint16_t c2 = rtc.getCounter(false);
uint32_t c2 = rtc.getCounter(false);
offset = (c2 - c1) * seconds2ticks(1) / 256;
// DHEX(ticks);DPRINT(" ");DHEX(c1);DPRINT(":");DHEX(c2);DPRINT(" ");DHEXLN(offset);

Expand Down
18 changes: 8 additions & 10 deletions AlarmClock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,24 @@ namespace as {

SysClock sysclock;
RTC rtc;
Link pwm(0);

void callback(void) {
--sysclock;
SoftPWM* p = (SoftPWM*)pwm.select();
while( p != 0 ) {
p->count();
p = (SoftPWM*)p->select();
}
}

void rtccallback () {
// DPRINT(".");
rtc.overflow();
// rtc.debug();
--rtc;
}

#if ARDUINO_ARCH_AVR or ARDUINO_ARCH_ATMEGA32
ISR(TIMER1_OVF_vect) {
callback();
}
ISR(TIMER2_OVF_vect) {
// DPRINT(".");
rtc.overflow();
// rtc.debug();
--rtc;
rtccallback();
}
#endif

Expand Down
53 changes: 12 additions & 41 deletions AlarmClock.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class AlarmClock: protected Link {


extern void callback(void);
extern void rtccallback(void);


class SysClock : public AlarmClock {
public:
Expand Down Expand Up @@ -163,6 +165,9 @@ extern SysClock sysclock;

class RTC : public AlarmClock {
uint8_t ovrfl;
#if defined(ARDUINO_ARCH_STM32F1) && defined(_RTCLOCK_H_)
RTClock rt;
#endif
public:

RTC () : ovrfl(0) {}
Expand All @@ -185,17 +190,22 @@ class RTC : public AlarmClock {
while (ASSR & (1<<TCN2UB)); //wait for registers update
TIFR |= (1<<TOV2); //clear interrupt flags
TIMSK |= (1<<TOIE2); //enable TOV2 interrupt
#elif defined(ARDUINO_ARCH_STM32F1) && defined(_RTCLOCK_H_)
rt = RTClock(RTCSEL_LSE);
rt.attachSecondsInterrupt(rtccallback);
#else
#warning "RTC not supported"
#endif
}

uint16_t getCounter (bool resetovrflow) {
#if ARDUINO_ARCH_AVR or ARDUINO_ARCH_ATMEGA32
uint32_t getCounter (bool resetovrflow) {
if( resetovrflow == true ) {
ovrfl = 0;
}
#if ARDUINO_ARCH_AVR or ARDUINO_ARCH_ATMEGA32
return (256 * ovrfl) + TCNT2;
#elif defined(ARDUINO_ARCH_STM32F1) && defined(_RTCLOCK_H_)
return rtc_get_count();
#else
return 0;
#endif
Expand All @@ -215,45 +225,6 @@ class RTC : public AlarmClock {

extern RTC rtc;


extern Link pwm;

class SoftPWM : public Link {
private:
uint32_t duty, value;
uint8_t pin;

#define STEPS 100
#define FREQUENCE 2048
#define R ((STEPS * log10(2))/(log10(FREQUENCE)))

public:
SoftPWM () : Link(0), duty(0), value(0), pin(0) {}

void init (uint8_t p) {
pin = p;
pinMode(pin,OUTPUT);
digitalWrite(pin,LOW);
pwm.append(*this);
}

void count () {
++value;
if( value == FREQUENCE ) {
digitalWrite(pin,duty > 0 ? HIGH : LOW);
value=0;
}
else if( value == duty ) {
digitalWrite(pin,LOW);
}
}

void set (uint8_t level) {
duty = level > 0 ? pow(2,(level/R)) : 0;
}
};


}

#endif

0 comments on commit 01c4fcf

Please sign in to comment.