Contiki 2.5
sleep.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in
12  * the documentation and/or other materials provided with the
13  * distribution.
14  * * Neither the name of the copyright holders nor the names of
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 /**
31  * \file
32  *
33  * \brief
34  * This controls the sleep operation for the LCD.
35  *
36  * \author
37  * Mike Vidales mavida404@gmail.com
38  *
39  */
40 
41 #include <avr/interrupt.h>
42 #include <avr/sleep.h>
43 #include <avr/wdt.h>
44 #include <util/delay.h>
45 #include <stdbool.h>
46 #include "main.h"
47 #include "sleep.h"
48 #include "uart.h"
49 #include "key.h"
50 #include "timer.h"
51 #include "lcd.h" //temp
52 
53 /**
54  * \addtogroup lcd
55  * \{
56 */
57 
58 /*---------------------------------------------------------------------------*/
59 
60 /**
61  * \brief Prepares for and executes sleep. This function sets up the
62  * processor to enter sleep mode, and to wake up when the joystick
63  * button (PE2/PCINT2) is pressed or after the specified interval.
64  *
65  * \param howlong Seconds to sleep, 0=until button pushed
66 */
67 void
68 sleep_now(int howlong)
69 {
70  /* Disable watchdog (not currently used elsewhere) */
71  wdt_disable();
72 
73  /* Setup sleep mode */
74  if (howlong==0) {
75  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
76  MCUCR |= (1<<JTD); //Disable JTAG so clock can stop
77  } else {
78  set_sleep_mode(SLEEP_MODE_PWR_SAVE);
79  /* Using 8 bit TIMER2 */
80  TCNT2 = 0;
81  TCCR2A = (1<<CS22)|(1<<CS21)|(1<<CS20); //Prescale by 1024
82  TIMSK2 = (1<<TOIE2); //Enable overflow interrupt
83  howlong*=30; //which is approximately 30 Hz
84 
85  /* Using 16 bit TIMER1, which takes a bit more power
86  timer_stop; //Disable interrupt
87  timer_init; //make sure initialized for 1 second
88  timer_start; //Start timer, enable interrupt
89  */
90  }
91 
92  /* Enable pin change 0 wakeup interrupt */
93  EIMSK |= (1 << PCIE0);
94  /* Select joystick button input pin */
95  PCMSK0 |= (1 << PCINT2);
96 
97  /* Sleep until timeout or button pushed */
98  while (ENTER_PORT & (1<<ENTER_PIN)) {
99  sleep_mode();
100  if (!howlong--) break;
101  }
102 
103  /* Disable the interrupts for the enter button and TIMER2 */
104  EIMSK &= ~(1 << PCIE0);
105  PCMSK0&= ~(1 <<PCINT2);
106  TIMSK2&= ~(1 << TOIE2);
107 }
108 
109 /*---------------------------------------------------------------------------*/
110 
111 /**
112 
113  * \brief This will send a wakeup command to ATmega1284p
114  * It may already be awake, if not it will respond during the next wake cycle
115  * Upon receiving the command it will return an acknowledgement frame
116  *
117  * \brief This will send a single character forever to the ATmega1284p to cause a wakeup.
118  * The 1284p polls the USART for new data during each sleep cycle. Upon receiving a
119  * character from the user LCD, it will wake up and send an acknowledgement frame.
120 */
121 void
123 {
124  lcd_puts_P(PSTR("WAKE 1284p"));
125 
126  /* Flood 1284p with wake commands until it responds*/
127  for(;;){
128  uart_serial_send_frame(SEND_WAKE,0,0);
129  _delay_us(1000);
130  if (rx_char_ready())
131  break;
132  }
133 
134  /* Get a frame back */
135  uart_serial_rcv_frame(true);
136 }
137 
138 /*---------------------------------------------------------------------------*/
139 
140 /**
141  * \brief This is the wake up button interrupt. When this interrupt fires,
142  * nothing is done. The program will simply continue from the end of the sleep
143  * command.
144 */
145 ISR
146 (PCINT0_vect)
147 {
148 
149 }
150 /*---------------------------------------------------------------------------*/
151 
152 /**
153  * \brief This is the timer2 overflow interrupt. When this interrupt fires,
154  * the CPU will wake.
155 */
156 ISR
157 (TIMER2_OVF_vect)
158 {
159 
160 }
161 
162 /** \} */