first commit
This commit is contained in:
85
libraries/AVR_Standard_C_Time_Library/src/AVR_RTC.h
Normal file
85
libraries/AVR_Standard_C_Time_Library/src/AVR_RTC.h
Normal file
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* (C)2020 Phillip Stevens All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer. Redistributions in binary
|
||||
* form must reproduce the above copyright notice, this list of conditions
|
||||
* and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution. Neither the name of the copyright holders
|
||||
* nor the names of contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef AVR_RTC_H
|
||||
#define AVR_RTC_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <time.h>
|
||||
|
||||
/**
|
||||
The tm structure contains a representation of time 'broken down' into components of the
|
||||
Gregorian calendar.
|
||||
|
||||
The normal ranges of the elements are..
|
||||
|
||||
\code
|
||||
tm_sec seconds after the minute - [ 0 to 59 ]
|
||||
tm_min minutes after the hour - [ 0 to 59 ]
|
||||
tm_hour hours since midnight - [ 0 to 23 ]
|
||||
tm_mday day of the month - [ 1 to 31 ]
|
||||
tm_wday days since Sunday - [ 0 to 6 ]
|
||||
tm_mon months since January - [ 0 to 11 ]
|
||||
tm_year years since 2000
|
||||
tm_yday days since January 1 - [ 0 to 365 ]
|
||||
tm_isdst Daylight Saving Time flag *
|
||||
|
||||
\endcode
|
||||
|
||||
*The value of tm_isdst is zero if Daylight Saving Time is not in effect, and is negative if
|
||||
the information is not available.
|
||||
|
||||
When Daylight Saving Time is in effect, the value represents the number of
|
||||
seconds the clock is advanced.
|
||||
*/
|
||||
|
||||
typedef struct tm tm;
|
||||
|
||||
/**
|
||||
Convert a Y2K time stamp into a FAT file system time stamp.
|
||||
*/
|
||||
uint32_t system_fatfs(const struct tm * timeptr);
|
||||
|
||||
/**
|
||||
Convert a FAT file system time stamp into a Y2K time stamp.
|
||||
*/
|
||||
uint32_t fatfs_system( uint16_t fsdate, uint16_t fstime, struct tm * timeptr);
|
||||
|
||||
/**
|
||||
* Perform hardware setup to enable 1 second sys_tick() from RTC on Timer 2.
|
||||
*/
|
||||
void setup_RTC_interrupt( void );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* AVR_RTC_H */
|
||||
99
libraries/AVR_Standard_C_Time_Library/src/fatfs_time.c
Normal file
99
libraries/AVR_Standard_C_Time_Library/src/fatfs_time.c
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* (C)2012 Michael Duane Rice All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer. Redistributions in binary
|
||||
* form must reproduce the above copyright notice, this list of conditions
|
||||
* and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution. Neither the name of the copyright holders
|
||||
* nor the names of contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* $Id$ */
|
||||
|
||||
/*
|
||||
Return a value suitable for use as a FAT file system time stamp.
|
||||
*/
|
||||
|
||||
#include "AVR_RTC.h"
|
||||
|
||||
uint32_t
|
||||
system_fatfs(const struct tm * timeptr)
|
||||
{
|
||||
uint32_t ret;
|
||||
uint32_t n;
|
||||
|
||||
n = timeptr->tm_year - 80;
|
||||
n <<= 25;
|
||||
ret = n;
|
||||
|
||||
n = timeptr->tm_mon + 1;
|
||||
n <<= 21;
|
||||
ret |= n;
|
||||
|
||||
n = timeptr->tm_mday;
|
||||
n <<= 16;
|
||||
ret |= n;
|
||||
|
||||
n = timeptr->tm_hour;
|
||||
n <<= 11;
|
||||
ret |= n;
|
||||
|
||||
n = timeptr->tm_min;
|
||||
n <<= 5;
|
||||
ret |= n;
|
||||
|
||||
ret |= timeptr->tm_sec >> 1;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Convert a FAT file system time stamp into a Y2K time stamp.
|
||||
|
||||
wFatDate [in]
|
||||
The MS-DOS date. The date is a packed value with the following format.
|
||||
Bits Description
|
||||
0-4 Day of the month (1–31)
|
||||
5-8 Month (1 = January, 2 = February, and so on)
|
||||
9-15 Year offset from 1980, subtract 20 to get Y2K year, add 2000 for Gregorian year.
|
||||
|
||||
wFatTime [in]
|
||||
The MS-DOS time. The time is a packed value with the following format.
|
||||
Bits Description
|
||||
0-4 Second divided by 2
|
||||
5-10 Minute (0–59)
|
||||
11-15 Hour (0–23 on a 24-hour clock)
|
||||
*/
|
||||
|
||||
uint32_t
|
||||
fatfs_system( uint16_t fsdate, uint16_t fstime, struct tm * timeptr)
|
||||
{
|
||||
timeptr->tm_year = ((fsdate >> 9) & 0x007F) + 80;
|
||||
timeptr->tm_mon = ((uint8_t)(fsdate >> 5 ) & 0x0F) - 1;
|
||||
timeptr->tm_mday = (uint8_t) fsdate & 0x1F;
|
||||
|
||||
timeptr->tm_hour = (uint8_t)(fstime >> 11) & 0x1F;
|
||||
timeptr->tm_min = (uint8_t)(fstime >> 5) & 0x3F;
|
||||
timeptr->tm_sec = ((uint8_t) fstime & 0x001F) << 1;
|
||||
return mktime( timeptr );
|
||||
}
|
||||
|
||||
50
libraries/AVR_Standard_C_Time_Library/src/timer2.c
Normal file
50
libraries/AVR_Standard_C_Time_Library/src/timer2.c
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
Set up the RTC based on using the AVR Timer 2 attached Clock Crystal.
|
||||
Developed for the Goldilocks Analogue, but also relevant for the Arduino Mega
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <util/atomic.h>
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
#include "AVR_RTC.h"
|
||||
|
||||
void
|
||||
setup_RTC_interrupt( void )
|
||||
{
|
||||
|
||||
/* Using 8bit Timer2 to generate the tick.
|
||||
* A 32.768 KHz crystal must be attached to the appropriate pins.
|
||||
* We then adjust the scale factor and counter to roll over at the top
|
||||
* so we can get EXACT seconds for the Real Time clock.
|
||||
*
|
||||
* This code is correct for an ATmega328p (Arduino Uno) but will fail to function
|
||||
* because the pins are used for an external crystal.
|
||||
*/
|
||||
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
|
||||
{
|
||||
TIMSK2 &= ~( _BV(OCIE2B)|_BV(OCIE2A)|_BV(TOIE2) ); // disable all Timer2 interrupts
|
||||
TIFR2 |= _BV(OCF2B)|_BV(OCF2A)|_BV(TOV2); // clear all pending interrupts
|
||||
ASSR = _BV(AS2); // set Timer/Counter2 to be asynchronous from the CPU clock
|
||||
// with a second external clock (32,768kHz) driving it.
|
||||
TCNT2 = 0x00; // zero out the counter
|
||||
TCCR2A = 0x00; // Normal mode
|
||||
TCCR2B = _BV(CS22) | _BV(CS20); // divide timer clock by 128 so counter will roll over at MAX
|
||||
|
||||
while( ASSR & (_BV(TCN2UB)|_BV(OCR2AUB)|_BV(TCR2AUB))); // Wait until Timer2 update complete
|
||||
|
||||
/* Enable the interrupt - this is okay as interrupts are currently globally disabled. */
|
||||
TIMSK2 |= _BV(TOIE2); // When the TOIE2 bit is written to one, the interrupt is enabled
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Tick ISR for the RTC. All this does is increment the RTC tick count, once per second.
|
||||
* Use ISR_NOBLOCK where there is an important timer running, that should preempt the RTC.
|
||||
* As long as it completes within one second, then there is no issue.
|
||||
*/
|
||||
ISR(TIMER2_OVF_vect, ISR_NAKED ISR_NOBLOCK ) __attribute__ ((hot, flatten));
|
||||
ISR(TIMER2_OVF_vect)
|
||||
{
|
||||
system_tick();
|
||||
reti();
|
||||
}
|
||||
Reference in New Issue
Block a user