From 44a2e34005acd673cf82716316d9287733a90940 Mon Sep 17 00:00:00 2001 From: dak664 Date: Wed, 22 Dec 2010 16:50:30 +0000 Subject: [PATCH] Resolve TIMER2 conflict between sleep and possible clock usage. --- cpu/avr/dev/clock.c | 12 +++ .../apps/raven-lcd-interface/raven-lcd.c | 80 +++++++++++++------ 2 files changed, 69 insertions(+), 23 deletions(-) diff --git a/cpu/avr/dev/clock.c b/cpu/avr/dev/clock.c index ddab18f2e..03243d761 100644 --- a/cpu/avr/dev/clock.c +++ b/cpu/avr/dev/clock.c @@ -31,7 +31,19 @@ extern uint8_t RF230_receive_on; For longer intervals a 32 bit global is incremented every second. clock-avr.h contains the specific setup code for each mcu. + + The 1284p can use TIMER2 with external 32768 Hz clock at 125 ticks/sec. + The 1284p routine also uses TIMER2 to sleep a variable number of seconds. + It calls this routine to adjust the clock after a sleep. */ +#if AVR_CONF_USE32KCRYSTAL +void clock_adjust_seconds(uint8_t howmany) { + seconds += howmany; +#if RADIOSTATS + if (RF230_receive_on) radioontime += howmany; +#endif +} +#endif /*---------------------------------------------------------------------------*/ //SIGNAL(SIG_OUTPUT_COMPARE0) ISR(AVR_OUTPUT_COMPARE_INT) diff --git a/platform/avr-raven/apps/raven-lcd-interface/raven-lcd.c b/platform/avr-raven/apps/raven-lcd-interface/raven-lcd.c index ca1a272c4..fd392e24a 100644 --- a/platform/avr-raven/apps/raven-lcd-interface/raven-lcd.c +++ b/platform/avr-raven/apps/raven-lcd-interface/raven-lcd.c @@ -28,7 +28,7 @@ * 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.9 2010/12/18 20:40:46 dak664 Exp $ + * $Id: raven-lcd.c,v 1.10 2010/12/22 16:50:31 dak664 Exp $ */ /** @@ -159,25 +159,36 @@ send_frame(uint8_t cmd, uint8_t len, uint8_t *payload) rs232_send(0, EOF_CHAR); } char serial_char_received; - /*---------------------------------------------------------------------------*/ -/* Sleep for howlong seconds using TIMER2, or forever if howlong==0*/ +/* 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 + * If so the clock is adjusted to reflect the sleep time + * + * Until someone figures out how to get UART to wake from powerdown, + * a three second powersave cycle is used with exit based on any character received. + */ void micro_sleep(uint8_t howlong) { - uint8_t saved_sreg = SREG; + uint8_t saved_sreg = SREG, saved_howlong = howlong; +#if AVR_CONF_USE32KCRYSTAL +/* Save TIMER2 configuration if clock.c is using it */ + uint8_t savedTCNT2=TCNT2, savedTCCR2A=TCCR2A, savedTCCR2B = TCCR2B, savedOCR2A = OCR2A; +#endif // if (howlong==0) { -// set_sleep_mode(SLEEP_MODE_PWR_DOWN); //Sleep until UART interrupt +// set_sleep_mode(SLEEP_MODE_PWR_DOWN); // UART can't wake from powerdown // } else { - set_sleep_mode(SLEEP_MODE_PWR_SAVE); //Sleep for howlong seconds - cli(); //Disable interrupts for the present - ASSR |= (1 << AS2); //Set TIMER2 asyncronous - TCCR2B|=((1<