OSDN Git Service

[v1.0][Doc] v1.0(.0) Release.
[openi2cradio/OpenI2CRadio.git] / power.c
1 /*
2  * OpenI2CRADIO
3  * Power switch.
4  * Copyright (C) 2013-06-10 K.Ohta <whatisthis.sowhat ai gmail.com>
5  * License: GPL2+LE
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2,
10  *  or (at your option) any later version.
11  *  This library / program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  *  See the GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this library; see the file COPYING. If not, write to the
18  *  Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
19  *  MA 02110-1301, USA.
20  *
21  *  As a special exception, if you link this(includeed from sdcc) library
22  *  with other files, some of which are compiled with SDCC,
23  *  to produce an executable, this library does not by itself cause
24  *  the resulting executable to be covered by the GNU General Public License.
25  *  This exception does not however invalidate any other reasons why
26  *  the executable file might be covered by the GNU General Public License.
27  */
28
29 #include "commondef.h"
30 #include "lcd_acm1602.h"
31 #include "power.h"
32 #include "menu.h"
33 #include "backlight.h"
34
35 /*
36  * Detect reset condition.
37  */
38 unsigned char chk_reset(void)
39 {
40
41 #ifdef __SDCC
42     union {
43         __RCONbits_t b;
44         unsigned char byte;
45     } rcon;
46     union {
47         __STKPTRbits_t b;
48         unsigned char byte;
49     } stkptr;
50 #else
51     union {
52         RCONbits_t b;
53         unsigned char byte;
54     } rcon;
55     union {
56         STKPTRbits_t b;
57         unsigned char byte;
58     } stkptr;
59 #endif
60     rcon.byte = RCON;
61     stkptr.byte = STKPTR;
62     if(rcon.b.POR ==0) {
63         RCONbits.POR = 1;
64         return RESET_POR;
65     }
66
67     if(rcon.b.BOR == 0) {
68         RCONbits.BOR = 1;
69         return RESET_BOR;
70     }
71     if(rcon.b.RI == 0) {
72         RCONbits.RI = 1;
73         return RESET_SOFTWARE;
74     }
75     if(rcon.b.TO == 0) return RESET_WDT;
76     if(rcon.b.PD == 0) return RESET_POWERDOWN;
77     if(stkptr.b.STKOVF) return RESET_STACK_FULL;
78     if(stkptr.b.STKUNF) return RESET_STACK_UNDER;
79     return RESET_MCLR;
80 }
81
82 void power_on_inthook(void)
83 {
84
85     IOCB = 0x00;
86 //    IOCBbits.IOCB4 = 1;
87     INTCONbits.RBIE = 0;
88     INTCONbits.GIE = 1;
89     INTCONbits.PEIE = 1;
90 }
91
92 void power_on(unsigned char f)
93 {
94     if(f == 0x00){
95         set_radiopower(0x00);
96 //        idle_time_ms(200);
97         set_examp_mute(0xff);
98     } else {
99         set_examp_mute(0x00);
100         idle_time_ms(100);
101         set_radiopower(0xff);
102     }
103 }
104
105 void rfamp_power(unsigned char b)
106 {
107     __bitops_t bx;
108     
109     bx.byte = b;
110     if(bx.b0) {
111         _RFAMP_SEL_B0 = 1;
112     } else {
113         _RFAMP_SEL_B0 = 0;
114     }
115     if(bx.b1) {
116         _RFAMP_SEL_B1 = 1;
117     } else {
118         _RFAMP_SEL_B1 = 0;
119     }
120
121 }
122
123 unsigned char chk_powerbutton(void)
124 {
125     unsigned char count = 0;
126     char i;
127
128     TRISBbits.RB4 = 1; // Set Input
129     for(i = 0; i < 24; i++) {  // 2*24=48ms.
130         if(PORTBbits.RB4) count++;
131         _POWER_DELAY();
132         ClrWdt();
133     }
134     if(count <= 12) { // 24ms
135         return 0xff; // Pressed
136     }
137     return 0; // Not pressed
138 }
139
140 void shutdown(unsigned char save_v)
141 {
142     rfamp_power(RFAMP_OFF);
143     power_on(0);
144     acm1602_suspend(LCD_I2CADDR);
145     lcd_setbacklight(0, 0);
146     power_off(save_v);
147 }
148
149 void power_off(unsigned char save_v)
150 {
151     unsigned char sts;
152
153     if(save_v != 0) {
154         save_eeprom();
155 //        save_frequencies();
156     }
157     _AM_STATLAMP = 0;
158     _FM_STATLAMP = 0;
159     set_powerlamp(0);
160
161     WDTCONbits.SWDTEN = 0; // Lame WDT OFF.
162     sts = 0;
163     do {
164         RCONbits.IPEN = 1;
165         IOCB = 0x10; // IOCB4 ONLY.
166         OSCCONbits.IDLEN = 0; // Not Idle.
167         INTCON2bits.RBIP = 1; // Priority = HIGH;
168         INTCON = 0b00001000;
169         T0CONbits.TMR0ON = 0;
170         Sleep();
171         // Wake up
172         if(INTCONbits.RBIF) {
173                 INTCONbits.RBIF = 0;
174                 idle_init();
175                 sts = chk_powerbutton();
176           //      sts = 0xff;
177         }
178     } while(sts == 0);
179     
180     INTCONbits.RBIE = 0;
181     INTCONbits.RBIF = 0;
182     // Button pressed, software reset.
183     Reset();
184 }