rgbyteclock-code/rtc.c

145 lines
2.9 KiB
C
Raw Normal View History

2014-03-01 04:11:40 +01:00
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <struppi@struppi.name> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return.
* (c) 2014 Stefan Rupp
* ----------------------------------------------------------------------------
*/
2014-03-01 04:11:40 +01:00
#include <avr/io.h>
#include "rtc.h"
#include <stdlib.h>
#include <avr/interrupt.h>
2014-03-02 08:01:37 +01:00
static volatile uint32_t _rtc_time;
2014-03-01 04:11:40 +01:00
2014-03-02 08:01:37 +01:00
ISR(TIMER2_OVF_vect) {
++_rtc_time;
2014-03-01 04:11:40 +01:00
return;
}
/*
* initialize the RTC module
* this resets the internal time to the value given in the
* rtc_time parameter
*/
2014-03-02 08:01:37 +01:00
void rtc_init(uint32_t rtc_time)
2014-03-01 04:11:40 +01:00
{
// Stop all interrupts
uint8_t sreg = SREG;
2014-03-01 04:11:40 +01:00
cli();
2014-03-02 08:01:37 +01:00
TCCR2B = 0;
TCCR2A = 0;
TCNT2 = 0;
2014-03-01 04:11:40 +01:00
// Reset timer to zero
2014-03-02 08:01:37 +01:00
_rtc_time = rtc_time;
2014-03-01 04:11:40 +01:00
2014-03-02 08:01:37 +01:00
ASSR = (1<<AS2); // set to asynchronous operation
TCCR2A = 0;
TCCR2B = (1<<CS22)|(1<<CS20);
2014-03-01 04:11:40 +01:00
2014-03-02 08:01:37 +01:00
TIMSK2 = (1<<TOIE2); // enable overflow interrupt
SREG = sreg;
2014-03-01 04:11:40 +01:00
}
/**
* Get the current time
* \return the current time (in s)
*/
inline uint32_t rtc_get(void)
{
uint32_t t;
uint8_t sreg;
sreg = SREG;
cli();
2014-03-02 08:01:37 +01:00
t = _rtc_time;
2014-03-01 04:11:40 +01:00
SREG = sreg;
return t;
}
#if 0
/**
* Decode a integer timestamp (in seconds)
* into days, hours, minutes, seconds
* \param time the time in seconds
* \param days will be filled in to contain the days after return
* \param hours will be filled in to contain the hours after return
* \param minutes will be filled in to contain the minutes after return
* \param seconds will be filled in to contain the seconds after return
* \param ms be filled in to contain the microseconds after return
*/
2014-03-02 08:01:37 +01:00
void rtc_decode(uint32_t time, uint16_t *days, uint8_t *hours, uint8_t *minutes, uint8_t *seconds)
2014-03-01 04:11:40 +01:00
{
if (seconds != NULL) {
*seconds = time % 60;
}
time /= 60;
if (minutes != NULL) {
*minutes = time % 60;
}
time /= 60;
if (hours != NULL) {
*hours = time;
}
return;
}
/**
* Encode a time given as hours, minutes, seconds and microsecods into a integer storing micorseconds
* \param time the variable to store the time into
* \param hour the hours to store
* \param hour the minutes to store
* \param hour the seconds to store
* \param hour the microseconds to store
*/
void timer_encode(uint32_t *time, uint8_t hours, uint8_t minutes, uint8_t seconds, uint16_t ms)
{
*time = hours;
*time *= 60;
*time += minutes;
*time *= 60;
*time += seconds;
*time *= 1000U;
*time += ms;
return;
}
/*
void timer_validate(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint16_t *ms)
{
while (*ms >= 1000) {
*ms -= 1000;
(*seconds)++;
}
while (*seconds >= 60) {
*ms -= 60;
(*minutes)++;
}
while (*minutes >= 60) {
*minutes -= 60;
(*hours)++;
}
return;
}
*/
#endif
2014-03-02 08:01:37 +01:00
2014-03-01 04:11:40 +01:00