138 lines
2.7 KiB
C
138 lines
2.7 KiB
C
/*
|
|
* ----------------------------------------------------------------------------
|
|
* "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
|
|
* ----------------------------------------------------------------------------
|
|
*/
|
|
|
|
|
|
#include <avr/io.h>
|
|
#include "rtc.h"
|
|
#include <stdlib.h>
|
|
#include <avr/interrupt.h>
|
|
|
|
|
|
static volatile uint32_t _rtc_time;
|
|
|
|
ISR(TIMER2_OVF_vect) {
|
|
++_rtc_time;
|
|
return;
|
|
}
|
|
|
|
void rtc_init(uint32_t rtc_time)
|
|
{
|
|
// Stop all interrupts
|
|
cli();
|
|
TCCR2B = 0;
|
|
TCCR2A = 0;
|
|
TCNT2 = 0;
|
|
// Reset timer to zero
|
|
_rtc_time = rtc_time;
|
|
|
|
ASSR = (1<<AS2); // set to asynchronous operation
|
|
TCCR2A = 0;
|
|
TCCR2B = (1<<CS22)|(1<<CS20);
|
|
|
|
TIMSK2 = (1<<TOIE2); // enable overflow interrupt
|
|
}
|
|
|
|
|
|
/**
|
|
* 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();
|
|
t = _rtc_time;
|
|
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
|
|
*/
|
|
void rtc_decode(uint32_t time, uint16_t *days, uint8_t *hours, uint8_t *minutes, uint8_t *seconds)
|
|
{
|
|
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
|
|
|
|
|