OSDN Git Service

[SCHEMATIC] Modify SW/MW/LW Preamp, insert galbanic-isolator replace of common-mode...
[openi2cradio/OpenI2CRadio.git] / main.c
1 /*
2  * OpenI2CRADIO
3  * Config & Main routine.
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 <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #if defined(__SDCC)
35 #include <sdcc-lib.h>
36 #include <pic18fregs.h> /* ONLY FOR PIC18x */
37 #include <delay.h>
38 #else
39 #include <xc.h>
40 #endif
41
42 #include <signal.h>
43
44 #include "iodef.h"
45 #include "idle.h"
46 #include "i2c_io.h"
47 #include "akc6955.h"
48 #include "lcd_acm1602.h"
49 #include "ui.h"
50 #include "eeprom.h"
51 #include "ioports.h"
52 #include "menu.h"
53 #include "power.h"
54 #include "adc_int.h"
55 #include "i2c_eeprom.h"
56 #include "backlight.h"
57 #include "rencoder.h"
58 #include "euart.h"
59 #include "uart_termio.h"
60 #include "shell_strutl.h"
61 #include "term_shell.h"
62
63 /*
64  * Config words.
65  */
66 #if defined(pic18f23k22) || defined(pic18f24k22) || defined(pic18f25k22) || defined(pic18f26k22)
67 //#pragma stack 0x200 256 // Set stack size to 256bytes.
68 #pragma config FOSC=INTIO67,BORV=190,BOREN=ON,PWRTEN=ON
69 #pragma config WDTEN=ON,WDTPS=32768
70 #pragma config PBADEN=OFF,MCLRE=EXTMCLR,STVREN=ON,LVP=OFF,DEBUG=ON,XINST=ON
71 //#pragma config PBADEN=OFF,MCLRE=EXTMCLR,STVREN=ON,LVP=OFF//,XINST=ON
72 #pragma config CP0=OFF,CP1=OFF,CPB=OFF,CPD=OFF
73 #pragma config WRT0=OFF,WRT1=OFF,WRTB=OFF,WRTC=OFF,WRTD=OFF
74 #pragma config EBTR0=OFF,EBTR1=OFF,EBTRB=OFF
75 #endif
76 // For 4xK20 or 2xK20 Series
77 #if defined(pic18f43k20) || defined(pic18f44k20) || defined(pic18f45k20) || defined(pic18f46k20) || \
78     defined(pic18f23k20) || defined(pic18f24k20) || defined(pic18f25k20) || defined(pic18f26k20) || \
79     defined(_18F43K20)  || defined(_18F44K20)  || defined(_18F45K20)  || defined(_18F46K20)  || \
80     defined(_18F23K20)  || defined(_18F24K20)  || defined(_18F25K20)  || defined(_18F26K20) 
81
82 #ifdef __SDCC
83 #pragma stack 0x200 256
84 #endif
85
86 #pragma config FOSC=HS,FCMEN=ON,PWRT=ON,BOREN=NOSLP,BORV=27, \
87                WDTEN=ON,WDTPS=32768,PBADEN=OFF,HFOFST=OFF,LPT1OSC=OFF, \
88                MCLRE=ON,STVREN=ON,DEBUG=ON,LVP=OFF, \
89                XINST=OFF
90 #endif
91
92
93 /*
94  * Statuses
95   */
96 unsigned char stereoflag;
97 unsigned char tuneflag;
98 unsigned char cnrlevel;
99 int diffstat;
100 unsigned int batlevel_6955;
101 unsigned int battlevel;
102 unsigned char ui_idlekey;
103 unsigned char ui_idlepad;
104
105 int recv_signal;
106 unsigned char pollkeybuf[33];
107
108 //#define _LCD_DEBUG 1
109 #ifdef __XC
110 void TMR0_handler(void)
111 #else
112 SIGHANDLER(TMR0_handler)
113 #endif
114 {
115
116    // Stop timer0
117    T0CONbits.TMR0ON = 0;
118
119    // Clear interrupt flag
120 //   INTCONbits.TMR0IF = 0;
121    INTCONbits.TMR0IE = 0;;
122
123    return;
124 }
125 /*
126  * Interrupt wake up every 1ms.
127  */
128 #ifdef __XC
129 void TMR1_Handler(void)
130 #else
131 SIGHANDLER(TMR1_Handler)
132 #endif
133 {
134 //    rencoder_count();
135         rencoder_tmrhook();
136 }
137
138 #ifdef __XC
139 void TMR3_Handler(void)
140 #else
141 SIGHANDLER(TMR3_Handler)
142 #endif
143 {
144    PIR2bits.TMR3IF = 0;
145    PIE2bits.TMR3IE = 0;
146    IPR2bits.TMR3IP = 0;
147 }
148
149 #ifdef __XC
150 void EXINT_Handler(void)
151 #else
152 SIGHANDLER(EXINT_Handler)
153 #endif
154 {
155     INTCONbits.INT0IE = 0;
156     INTCONbits.INT0IF = 0;
157     INTCON3bits.INT1IF = 0;
158     INTCON3bits.INT2IF = 0;
159     INTCON3bits.INT1IE = 0;
160     INTCON3bits.INT2IE = 0;
161
162 }
163 #ifdef __XC
164 void RBIF_handler(void)
165 #else
166 SIGHANDLER(RBIF_handler)
167 #endif
168 {
169     if(IOCBbits.IOCB4 != 0) {
170         power_on_inthook();
171     } else {
172         rencoder_inthook();
173     }
174 }
175 #ifdef __XC
176 void  EEPROM_handler(void)
177 #else
178 SIGHANDLER(EEPROM_handler)
179 #endif
180 {
181     PIR2bits.EEIF = 0;
182     EECON1bits.WREN = 0;
183 //    if(chk_powerbutton(0, 0) != 0) { // If pressed on
184 //      power_off(1); //
185 //    }
186 }
187
188 #ifdef __XC
189 void INADC_handler(void)
190 #else
191 SIGHANDLER(INADC_handler)
192 #endif
193 {
194 //    unsigned int a;
195 //    a = polladc();
196 //    if(a != 0xffff) {
197 //        battlevel = adc_rawtobatt(a);
198 //    }
199     PIR1bits.ADIF = 0;
200 }
201 #ifdef __XC
202 void I2C_handler(void)
203 #else
204 SIGHANDLER(I2C_handler)
205 #endif
206 {
207     PIR1bits.SSPIF = 0;
208 }
209 #ifdef __XC
210 void I2CBus_handler(void)
211 #else
212 SIGHANDLER(I2CBus_handler)
213 #endif
214 {
215     PIR2bits.BCLIF = 0;
216 }
217
218 #ifdef __XC
219 void UART_R_Handler(void)
220 #else
221 SIGHANDLER(UART_R_Handler)
222 #endif
223 {
224     uart_inthdr_rx();
225 }
226
227
228 #ifdef __SDCC
229 DEF_INTLOW(intlow_handler)
230   DEF_HANDLER(SIG_TMR0, TMR0_handler)
231   DEF_HANDLER(SIG_INT0, EXINT_Handler)
232 //  DEF_HANDLER(SIG_TMR3, TMR3_Handler)
233 END_DEF
234
235 DEF_INTHIGH(inthigh_handler)
236  DEF_HANDLER(SIG_RBIF, RBIF_handler)
237  DEF_HANDLER(SIG_RCIF, UART_R_handler)
238 // DEF_HANDLER(SIG_TMR1, TMR1_Handler)
239 // DEF_HANDLER(SIG_INT1, EXINT_Handler)
240  DEF_HANDLER(SIG_INT2, EXINT_Handler)
241  DEF_HANDLER(SIG_AD, INADC_handler)
242   //DEF_HANDLER(SIG_SSP, I2C_handler)
243   //DEF_HANDLER(SIG_BCOL, I2CBus_handler)
244 END_DEF
245 #else
246 void interrupt low_priority intlow_handler(void)
247 {
248     if(INTCONbits.TMR0IF) TMR0_handler();
249     if(INTCONbits.INT0IF) EXINT_Handler();
250 //    if(PIR1bits.TMR1IF) TMR1_Handler();
251 }
252
253 void interrupt high_priority inthigh_handler(void)
254 {
255     if(INTCONbits.RBIF) RBIF_handler();
256     if(PIR1bits.TMR1IF) TMR1_Handler();
257     if(PIR1bits.RC1IF)   UART_R_Handler();
258 //    if(PIR2bits.TMR3IF) TMR3_Handler();
259 //    if(INTCON3bits.INT1IF) EXINT_Handler();
260 //    if(INTCON3bits.INT2IF) EXINT_Handler();
261     if(PIR1bits.ADIF) INADC_handler();
262
263 //    if(PIR1bits.SSPIF)    I2C_handler();
264 }
265
266 #ifdef __XC
267
268
269 #endif
270 #endif
271
272
273 void lowbatt(void)
274 {
275 //    _CLS();
276 //    idle_time_ms(100);
277     printhelp_2lines("Low battery X)", "Press key to suspend");
278     shutdown(1);
279 }
280
281
282 int main(void)
283 {
284     unsigned char c;
285     unsigned char pbutton;
286     unsigned char reset_status;
287     unsigned char p;
288     unsigned char lvcount = 0;
289     unsigned char dispf = 0xff;
290     unsigned char i;
291     unsigned int step;
292
293     OSCCON =  (0x80 & 0b11111100) | 0b00111000;
294 //    OSCCON =  (0x80 & 0b11111100) | 0b00110010; // 8MHz 
295     idle_init();
296     keyin_init();
297     keyin_ioinit();
298     i2c1_init();
299     reset_status = chk_reset();
300     idle_time_ms(300); // Wait for setup.
301     WDTCONbits.SWDTEN = 0; // WDT OFF.
302     switch(reset_status){
303         case RESET_MCLR:
304         case RESET_BOR:
305             shutdown(0); // Save and halt on BOR.
306             break;
307         case RESET_SOFTWARE: //
308             RCONbits.RI = 0;
309            pbutton = chk_powerbutton();
310             if(pbutton == 0) shutdown(0); // Not-Pressed power-button -> shutdown( not save).
311             break;
312         case RESET_POR:
313         case RESET_WDT:  // Workaround random reset.
314  //           shutdown(0);
315             break;
316         default:
317             break;
318     }
319     WDTCONbits.SWDTEN = 1; // WDT ON.
320     power_on(1);
321     //intadc_init();
322     valinit();
323     acm1602_init(LCD_I2CADDR, 1); //Init LCD
324     lcd_setbacklight(0xff, 255);
325     /* Check EEPROM */
326     check_eeprom();
327   /* Push default parameters to AKC6955*/
328     setup_akc6955();
329     _CLS();
330     uart_init();
331     //_LOCATE(0,0);
332     _PUTCHAR(' ');
333     update_status();
334     update_display();
335     ClrWdt();
336    ui_idlekey = setup.ui_idlecount / 92;
337    ui_idlepad = setup.ui_idlecount % 23;
338    rencoder_init();
339    rencoder_start();
340    do {
341
342         if(battlevel < 340) { // 3.4V
343                 lvcount++;
344                 if(lvcount > 4) {
345                     if(dispf == 0) {
346                         acm1602_resume(LCD_I2CADDR);
347                         dispf = 0xff;
348                      }
349                     lowbatt(); //Zap 4Times on LowVoltage.
350                 }
351             } else {
352                 lvcount = 0;
353             }
354         /* Main routine*/
355
356         term_shell(0); // Steel Shell
357         for(i = 0; i < 4; i++) {
358            c = pollkey_single_timeout(ui_idlekey, 1); // 23*41 = 943ms
359            p = 0;
360             if(c != charcode_null) {
361                 ClrWdt();
362                 if(dispf == 0) {
363                     acm1602_resume(LCD_I2CADDR);
364                     dispf = 0xff;
365                 }
366                 setfreq_updown(c);
367                 update_status();
368                 update_display();
369                 rencoder_start();
370             }
371             ClrWdt();
372          }
373          dispf = setfreq_renc_updown(dispf);
374          update_status();
375          dispf = backlight_dec(dispf); 
376          if(dispf != 0)  update_display();
377          if(ui_idlepad != 0) idle_time_ms(ui_idlepad);
378          ClrWdt();
379     } while(1);
380 }
381