OSDN Git Service

[SCHEMATIC] Modify SW/MW/LW Preamp, insert galbanic-isolator replace of common-mode...
[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 #include "rencoder.h"
35 #include "euart.h"
36
37 /*
38  * Detect reset condition.
39  */
40 unsigned char chk_reset(void)
41 {
42
43 #ifdef __SDCC
44     union {
45         __RCONbits_t b;
46         unsigned char byte;
47     } rcon;
48     union {
49         __STKPTRbits_t b;
50         unsigned char byte;
51     } stkptr;
52 #else
53     union {
54         RCONbits_t b;
55         unsigned char byte;
56     } rcon;
57     union {
58         STKPTRbits_t b;
59         unsigned char byte;
60     } stkptr;
61 #endif
62     rcon.byte = RCON;
63     stkptr.byte = STKPTR;
64     if(rcon.b.POR ==0) {
65         RCONbits.POR = 1;
66         return RESET_POR;
67     }
68
69     if(rcon.b.BOR == 0) {
70         RCONbits.BOR = 1;
71         return RESET_BOR;
72     }
73     if(rcon.b.RI == 0) {
74         RCONbits.RI = 1;
75         return RESET_SOFTWARE;
76     }
77     if(rcon.b.TO == 0) return RESET_WDT;
78     if(rcon.b.PD == 0) return RESET_POWERDOWN;
79     if(stkptr.b.STKOVF) return RESET_STACK_FULL;
80     if(stkptr.b.STKUNF) return RESET_STACK_UNDER;
81     return RESET_MCLR;
82 }
83
84 void power_on_inthook(void)
85 {
86
87     IOCB = 0x00;
88 //    IOCBbits.IOCB4 = 1;
89     INTCONbits.RBIE = 0;
90     INTCONbits.GIE = 1;
91     INTCONbits.PEIE = 1;
92 }
93
94 void power_on(unsigned char f)
95 {
96     if(f == 0x00){
97        set_radiopower(0x00);
98        set_examp_mute(0xff);
99 //        idle_time_ms(200);
100     } else {
101         set_examp_mute(0x00);
102         idle_time_ms(100);
103         set_radiopower(0xff);
104     }
105 }
106
107 void rfamp_power(unsigned char b)
108 {
109     __bitops_t bx;
110     
111     bx.byte = b;
112     if(bx.b0) {
113         _RFAMP_SEL_B0 = 1;
114     } else {
115         _RFAMP_SEL_B0 = 0;
116     }
117     if(bx.b1) {
118         _RFAMP_SEL_B1 = 1;
119     } else {
120         _RFAMP_SEL_B1 = 0;
121     }
122
123 }
124
125 unsigned char chk_powerbutton(void)
126 {
127     unsigned char count = 0;
128     char i;
129
130     TRISBbits.RB4 = 1; // Set Input
131     for(i = 0; i < 24; i++) {  // 2*24=48ms.
132         if(PORTBbits.RB4) count++;
133         _POWER_DELAY();
134         ClrWdt();
135     }
136     if(count <= 12) { // 24ms
137         return 0xff; // Pressed
138     }
139     return 0; // Not pressed
140 }
141
142 void shutdown(unsigned char savef)
143 {
144     rfamp_power(RFAMP_OFF);
145     power_on(0);
146     acm1602_suspend(LCD_I2CADDR);
147     lcd_setbacklight(0, 0);
148     power_off(savef);
149 }
150
151 void power_off(unsigned char savef)
152 {
153     unsigned char sts;
154
155     uart_sleep();
156     if(savef != 0) {
157         save_eeprom();
158         save_userbands();
159     }
160 //    set_powerlamp(0);
161     _AM_STATLAMP = 0;
162     _FM_STATLAMP = 0;
163
164     WDTCONbits.SWDTEN = 0; // Lame WDT OFF.
165     sts = 0;
166     rencoder_stop();
167     do {
168         RCONbits.IPEN = 1;
169         IOCB = 0x10; // IOCB4 ONLY.
170         OSCCONbits.IDLEN = 0; // Not Idle.
171         INTCON2bits.RBIP = 1; // Priority = HIGH;
172         INTCON = 0b00001000;
173         T0CONbits.TMR0ON = 0;
174         Sleep();
175         // Wake up
176         if(INTCONbits.RBIF) {
177                 INTCONbits.RBIF = 0;
178                 idle_init();
179                 sts = chk_powerbutton();
180           //      sts = 0xff;
181         }
182     } while(sts == 0);
183     
184     INTCONbits.RBIE = 0;
185     INTCONbits.RBIF = 0;
186     // Button pressed, software reset.
187     Reset();
188 }