Change timer setups for arbitrary CLOCK_CONF_SECOND

Change raven to 128 ticks and enable 32KHz crystal as default.
This commit is contained in:
David Kopf 2011-08-05 15:14:35 -04:00
parent bfbc3234ea
commit c9b19ce655
6 changed files with 74 additions and 50 deletions

View File

@ -14,11 +14,11 @@
\
/* \
* Set comparison register: \
* Crystal freq. is 16000000,\
* pre-scale factor is 1024, i.e. we have 125 "ticks" / sec: \
* 16000000 = 1024 * 125 * 125 \
* Crystal freq. is F_CPU,\
* pre-scale factor is 1024, we want CLOCK_CONF_SECOND ticks / sec: \
* F_CPU = 1024 * CLOCK_CONF_SECOND * OCR0 \
*/ \
OCR0 = 125; \
OCR0 = F_CPU/1024UL/CLOCK_CONF_SECOND; \
\
/* \
* Set timer control register: \
@ -48,11 +48,11 @@
\
/* \
* Set comparison register: \
* Crystal freq. is 8000000,\
* pre-scale factor is 1024, we want 125 ticks / sec: \
* 8000000 = 1024 * 126.01 * 62, less 1 for CTC mode \
* Crystal freq. is F_CPU,\
* pre-scale factor is 1024, we want CLOCK_CONF_SECOND ticks / sec: \
* F_CPU = 1024 * CLOCK_CONF_SECOND * OCR0A, less 1 for CTC mode \
*/ \
OCR0A = 61; \
OCR0A = F_CPU/1024/CLOCK_CONF_SECOND - 1; \
\
/* \
* Set timer control register: \
@ -92,10 +92,10 @@
/* \
* Set comparison register: \
* Crystal freq. is 32768,\
* pre-scale factor is 8, we want 125 ticks / sec: \
* 32768 = 8 * 124.1 * 33, less 1 for CTC mode\
* pre-scale factor is 8, we want CLOCK_CONF_SECOND ticks / sec: \
* 32768 = 8 * CLOCK_CONF_SECOND * OCR2A, less 1 for CTC mode\
*/ \
OCR2A = 32; \
OCR2A = 32768/8/CLOCK_CONF_SECOND - 1; \
\
/* \
* Set timer control register: \
@ -124,11 +124,11 @@
\
/* \
* Set comparison register: \
* Crystal freq. is 8000000,\
* pre-scale factor is 1024, we want 125 ticks / sec: \
* 8000000 = 1024 * 126.01 * 62, less 1 for CTC mode \
* Crystal freq. is F_CPU,\
* pre-scale factor is 1024, we want CLOCK_CONF_SECOND ticks / sec: \
* F_CPU = 1024 * CLOCK_CONF_SECOND * OCR2A, less 1 for CTC mode \
*/ \
OCR0A = 61; \
OCR0A = F_CPU/1024UL/CLOCK_CONF_SECOND - 1; \
\
/* \
* Set timer control register: \
@ -156,11 +156,10 @@
\
/* \
* Set comparison register: \
* Crystal freq. is 8000000,\
* pre-scale factor is 256, i.e. we have 125 "ticks" / sec: \
* 8000000 = 256 * 250 * 125 \
* Crystal freq. is F_CPU,\
* pre-scale factor is 256, want CLOCK_CONF_SECOND ticks / sec: \
*/ \
OCR0A = 250; \
OCR0A = F_CPU/256UL/CLOCK_CONF_SECOND - 1; \
\
/* \
* Set timer control register: \
@ -191,11 +190,11 @@
\
/* \
* Set comparison register: \
* Crystal freq. is 8000000,\
* pre-scale factor is 256, i.e. we have 125 "ticks" / sec: \
* 8000000 = 256 * 250 * 125 \
* Crystal freq. is F_CPU,\
* pre-scale factor is 256, we want CLOCK_CONF_SECOND ticks / sec: \
* F_CPU = 256 * CLOCK_CONF_SECOND * OCR0 \
*/ \
OCR0 = 250; \
OCR0 = F_CPU/256UL/CLOCK_CONF_SECOND; \
\
/* \
* Set timer control register: \
@ -223,11 +222,11 @@
\
/* \
* Set comparison register: \
* Crystal freq. is 8000000,\
* pre-scale factor is 256, i.e. we have 125 "ticks" / sec: \
* 8000000 = 256 * 250 * 125 \
* Crystal freq. is F_CPU,\
* pre-scale factor is 256, we want CLOCK_CONF_SECOND ticks / sec: \
* F_CPU = 256 * CLOCK_CONF_SECOND * OCR2 \
*/ \
OCR2 = 250; \
OCR2 = F_CPU/256UL/CLOCK_CONF_SECOND; \
\
/* \
* Set timer control register: \

View File

@ -34,7 +34,8 @@
/**
* \file
* AVR-specific rtimer code
* Currently only works on ATMEGAs that have Timer 3.
* Defaults to Timer3 for those ATMEGAs that have it.
* If Timer3 not present Timer1 will be used.
* \author
* Fredrik Osterlind <fros@sics.se>
* Joakim Eriksson <joakime@sics.se>
@ -51,7 +52,6 @@
#include "rtimer-arch.h"
#if defined(__AVR_ATmega1281__) || defined(__AVR_ATmega1284P__)
//#error FTH081029 test timer 3
#define ETIMSK TIMSK3
#define ETIFR TIFR3
#define TICIE3 ICIE3
@ -78,7 +78,7 @@
#endif
/*---------------------------------------------------------------------------*/
#ifdef TCNT3
#if defined(TCNT3) && RTIMER_ARCH_PRESCALER
ISR (TIMER3_COMPA_vect) {
ENERGEST_ON(ENERGEST_TYPE_IRQ);
@ -104,6 +104,7 @@ ISR (TIMER1_COMPA_vect) {
void
rtimer_arch_init(void)
{
#if RTIMER_ARCH_PRESCALER
/* Disable interrupts (store old state) */
uint8_t sreg;
sreg = SREG;
@ -125,8 +126,19 @@ rtimer_arch_init(void)
/* Reset counter */
TCNT3 = 0;
/* Start clock, maximum prescaler (1024)*/
#if RTIMER_ARCH_PRESCALER==1024
TCCR3B |= 5;
#elif RTIMER_ARCH_PRESCALER==256
TCCR3B |= 4;
#elif RTIMER_ARCH_PRESCALER==64
TCCR3B |= 3;
#elif RTIMER_ARCH_PRESCALER==8
TCCR3B |= 2;
#elif RTIMER_ARCH_PRESCALER==1
TCCR3B |= 1;
#else
#error Timer3 PRESCALER factor not supported.
#endif
#elif RTIMER_ARCH_PRESCALER
/* Leave timer1 alone if PRESCALER set to zero */
@ -154,18 +166,20 @@ rtimer_arch_init(void)
#elif RTIMER_ARCH_PRESCALER==1
TCCR1B |= 1;
#else
#error PRESCALER factor not supported.
#error Timer1 PRESCALER factor not supported.
#endif
#endif /* TCNT3 */
/* Restore interrupt state */
SREG = sreg;
#endif /* RTIMER_ARCH_PRESCALER */
}
/*---------------------------------------------------------------------------*/
void
rtimer_arch_schedule(rtimer_clock_t t)
{
#if RTIMER_ARCH_PRESCALER
/* Disable interrupts (store old state) */
uint8_t sreg;
sreg = SREG;
@ -190,4 +204,5 @@ rtimer_arch_schedule(rtimer_clock_t t)
/* Restore interrupt state */
SREG = sreg;
#endif /* RTIMER_ARCH_PRESCALER */
}

View File

@ -36,16 +36,22 @@
#include <avr/interrupt.h>
/* Nominal ARCH_SECOND is F_CPU/prescaler, e.g. 8000000/1024 = 7812 */
/* Nominal ARCH_SECOND is F_CPU/prescaler, e.g. 8000000/1024 = 7812
* Other prescaler values (1, 8, 64, 256) will give greater precision
* with shorter maximum intervals.
* Setting RTIMER_ARCH_PRESCALER to 0 will leave Timers alone.
* rtimer_arch_now() will then return 0, likely hanging the cpu if used.
* Timer1 is used if Timer3 is not available.
*/
#ifndef RTIMER_ARCH_PRESCALER
#define RTIMER_ARCH_PRESCALER 1024UL
#endif
#if RTIMER_ARCH_PRESCALER
#define RTIMER_ARCH_SECOND (F_CPU/RTIMER_ARCH_PRESCALER)
#else
#define RTIMER_ARCH_SECOND 0
#endif
/* Use TCNT1 if TCNT3 not available.
* Setting RTIMER_ARCH_PRESCALER to 0 will leave timer1 alone.
* Obviously this will disable rtimers.
*/
#ifdef TCNT3
#define rtimer_arch_now() (TCNT3)

View File

@ -28,7 +28,6 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* $Id: raven-lcd.c,v 1.11 2010/12/22 17:09:03 dak664 Exp $
*/
/**
@ -166,7 +165,7 @@ char serial_char_received;
/*---------------------------------------------------------------------------*/
/* Sleep for howlong seconds, or until UART interrupt if howlong==0.
* Uses TIMER2 with external 32768 Hz crystal to sleep in 1 second multiples.
* TIMER2 may have already been set up for 125 ticks/second in clock.c
* TIMER2 may have already been set up for CLOCK_CONF_SECOND ticks/second in clock.c
*
* Until someone figures out how to get UART to wake from powerdown,

View File

@ -68,15 +68,21 @@ unsigned long clock_seconds(void);
#define INFINITE_TIME 0xffff
/* Clock ticks per second */
#define CLOCK_CONF_SECOND 125
#define CLOCK_CONF_SECOND 128
/* Maximum tick interval is 0xffff/125 = 524 seconds */
#define RIME_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME CLOCK_CONF_SECOND * 524UL /* Default uses 600UL */
#define COLLECT_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME CLOCK_CONF_SECOND * 524UL /* Default uses 600UL */
/* Maximum tick interval is 0xffff/128 = 511 seconds */
#define RIME_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME INFINITE_TIME/CLOCK_CONF_SECOND /* Default uses 600 */
#define COLLECT_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME INFINITE_TIME/CLOCK_CONF_SECOND /* Default uses 600 */
/* The 1284p can use TIMER2 with the external 32768Hz crystal to keep time. Else TIMER0 is used. */
/* The sleep timer in raven-lcd.c also uses the crystal and adds a TIMER2 interrupt routine if not already define by clock.c */
#define AVR_CONF_USE32KCRYSTAL 0
#define AVR_CONF_USE32KCRYSTAL 1
/* Rtimer is implemented through the 16 bit Timer1, clocked at F_CPU through a 1024 prescaler. */
/* This gives 7812 counts per second, 128 microsecond precision and maximum interval 8.388 seconds. */
/* Change clock source and prescaler for greater precision and shorter maximum interval. */
/* 0 will disable the Rtimer code */
//#define RTIMER_ARCH_PRESCALER 256UL /*0, 1, 8, 64, 256, 1024 */
/* COM port to be used for SLIP connection. Not tested on Raven */
#define SLIP_PORT RS232_PORT_0
@ -103,7 +109,7 @@ unsigned long clock_seconds(void);
#define UIP_CONF_ICMP6 1
#define UIP_CONF_UDP 1
#define UIP_CONF_TCP 1
#define UIP_CONF_IPV6_RPL 1
#define UIP_CONF_IPV6_RPL 0
#define NETSTACK_CONF_NETWORK sicslowpan_driver
#define SICSLOWPAN_CONF_COMPRESSION SICSLOWPAN_COMPRESSION_HC06
#else

View File

@ -97,9 +97,9 @@
#define TESTRTIMER 0
#if TESTRTIMER
//#define PINGS 64
#define ROUTES 64
#define STAMPS 30
#define STACKMONITOR 128
#define ROUTES 300
#define STAMPS 60
#define STACKMONITOR 600
uint8_t rtimerflag=1;
uint16_t rtime;
@ -416,10 +416,9 @@ main(void)
#endif
#if TESTRTIMER
/* Timeout can be increased up to 8 seconds maximum.
/* Timeout can be increased up to 8 seconds maximum (at 8MHz with 1024 prescaler)
* A one second cycle is convenient for triggering the various debug printouts.
* The triggers are staggered to avoid printing everything at once.
* My raven is 6% slow.
*/
if (rtimerflag) {
rtimer_set(&rt, RTIMER_NOW()+ RTIMER_ARCH_SECOND*1UL, 1,(void *) rtimercycle, NULL);