OSDN Git Service

[I2CEEPROM] Byte read/write is okay, but blockread/write is not tested yet.
[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 /*
57  * Config words.
58  */
59 #if defined(pic18f23k22) || defined(pic18f24k22) || defined(pic18f25k22) || defined(pic18f26k22)
60 //#pragma stack 0x200 256 // Set stack size to 256bytes.
61 #pragma config FOSC=INTIO67,BORV=190,BOREN=ON,PWRTEN=ON
62 #pragma config WDTEN=ON,WDTPS=32768
63 #pragma config PBADEN=OFF,MCLRE=EXTMCLR,STVREN=ON,LVP=OFF,DEBUG=ON,XINST=ON
64 //#pragma config PBADEN=OFF,MCLRE=EXTMCLR,STVREN=ON,LVP=OFF//,XINST=ON
65 #pragma config CP0=OFF,CP1=OFF,CPB=OFF,CPD=OFF
66 #pragma config WRT0=OFF,WRT1=OFF,WRTB=OFF,WRTC=OFF,WRTD=OFF
67 #pragma config EBTR0=OFF,EBTR1=OFF,EBTRB=OFF
68 #endif
69 // For 4xK20 or 2xK20 Series
70 #if defined(pic18f43k20) || defined(pic18f44k20) || defined(pic18f45k20) || defined(pic18f46k20) || \
71     defined(pic18f23k20) || defined(pic18f24k20) || defined(pic18f25k20) || defined(pic18f26k20) || \
72     defined(_18F43K20)  || defined(_18F44K20)  || defined(_18F45K20)  || defined(_18F46K20)  || \
73     defined(_18F23K20)  || defined(_18F24K20)  || defined(_18F25K20)  || defined(_18F26K20) 
74
75 #ifdef __SDCC
76 #pragma stack 0x200 256
77 #endif
78
79 #pragma config FOSC=HS,FCMEN=ON,PWRT=ON,BOREN=NOSLP,BORV=27, \
80                WDTEN=ON,WDTPS=32768,PBADEN=OFF,HFOFST=OFF,LPT1OSC=OFF, \
81                MCLRE=ON,STVREN=ON,DEBUG=ON, \
82                XINST=OFF
83 #endif
84 /*
85  * Statuses
86   */
87 unsigned char stereoflag;
88 unsigned char tuneflag;
89 unsigned char cnrlevel;
90 int diffstat;
91 unsigned int batlevel_6955;
92 unsigned int battlevel;
93
94 int recv_signal;
95 int backlight_counter;
96 unsigned char pollkeybuf[33];
97
98 //#define _LCD_DEBUG 1
99 #ifdef __XC
100 void TMR0_handler(void)
101 #else
102 SIGHANDLER(TMR0_handler)
103 #endif
104 {
105
106    // Stop timer0
107    T0CONbits.TMR0ON = 0;
108
109    // Clear interrupt flag
110 //   INTCONbits.TMR0IF = 0;
111    INTCONbits.TMR0IE = 0;;
112
113    return;
114 }
115 /*
116  * Interrupt wake up every 1ms.
117  */
118 #ifdef __XC
119 void TMR3_Handler(void)
120 #else
121 SIGHANDLER(TMR3_Handler)
122 #endif
123 {
124     PIR2bits.TMR3IF  = 0;
125     PIE2bits.TMR3IE  = 0;
126     T3CONbits.TMR3ON = 0;
127 }
128
129 #ifdef __XC
130 void EXINT_Handler(void)
131 #else
132 SIGHANDLER(EXINT_Handler)
133 #endif
134 {
135     INTCONbits.INT0IE = 0;
136     INTCONbits.INT0IF = 0;
137     INTCON3bits.INT1IF = 0;
138     INTCON3bits.INT2IF = 0;
139     INTCON3bits.INT1IE = 0;
140     INTCON3bits.INT2IE = 0;
141
142 }
143 #ifdef __XC
144 void RBIF_handler(void)
145 #else
146 SIGHANDLER(RBIF_handler)
147 #endif
148 {
149     power_on_inthook();
150 //    if(chk_powerbutton(0, 0) != 0) { // If pressed on
151 //      power_off(1); //
152 //    }
153 }
154 #ifdef __XC
155 void  EEPROM_handler(void)
156 #else
157 SIGHANDLER(EEPROM_handler)
158 #endif
159 {
160     PIR2bits.EEIF = 0;
161     EECON1bits.WREN = 0;
162 //    if(chk_powerbutton(0, 0) != 0) { // If pressed on
163 //      power_off(1); //
164 //    }
165 }
166
167 #ifdef __XC
168 void INADC_handler(void)
169 #else
170 SIGHANDLER(INADC_handler)
171 #endif
172 {
173 //    unsigned int a;
174 //    a = polladc();
175 //    if(a != 0xffff) {
176 //        battlevel = adc_rawtobatt(a);
177 //    }
178     PIR1bits.ADIF = 0;
179 }
180 #ifdef __XC
181 void I2C_handler(void)
182 #else
183 SIGHANDLER(I2C_handler)
184 #endif
185 {
186     PIR1bits.SSPIF = 0;
187 }
188 #ifdef __XC
189 void I2CBus_handler(void)
190 #else
191 SIGHANDLER(I2CBus_handler)
192 #endif
193 {
194     PIR2bits.BCLIF = 0;
195 }
196
197
198
199 #ifdef __SDCC
200 DEF_INTLOW(intlow_handler)
201   DEF_HANDLER(SIG_TMR0, TMR0_handler)
202   DEF_HANDLER(SIG_INT0, EXINT_Handler)
203 //  DEF_HANDLER(SIG_TMR3, I2C_handler)
204 END_DEF
205
206 DEF_INTHIGH(inthigh_handler)
207  DEF_HANDLER(SIG_RBIF, RBIF_handler)
208  DEF_HANDLER(SIG_EEIF, EEPROM_handler)
209  DEF_HANDLER(SIG_TMR3, TMR3_Handler)
210  DEF_HANDLER(SIG_INT1, EXINT_Handler)
211  DEF_HANDLER(SIG_INT2, EXINT_Handler)
212  DEF_HANDLER(SIG_AD, INADC_handler)
213   //DEF_HANDLER(SIG_SSP, I2C_handler)
214   //DEF_HANDLER(SIG_BCOL, I2CBus_handler)
215 END_DEF
216 #else
217 void interrupt low_priority intlow_handler(void)
218 {
219     if(INTCONbits.TMR0IF) TMR0_handler();
220     if(INTCONbits.INT0IF) EXINT_Handler();
221 }
222
223 void interrupt high_priority inthigh_handler(void)
224 {
225     if(INTCONbits.RBIF) RBIF_handler();
226     if(PIR2bits.EEIF)   EEPROM_handler();
227     if(PIR2bits.TMR3IF) TMR3_Handler();
228    // if(INTCONbits.TMR0IF) TMR0_handler();
229     if(INTCON3bits.INT1IF) EXINT_Handler();
230     if(INTCON3bits.INT2IF) EXINT_Handler();
231     if(PIR1bits.ADIF) INADC_handler();
232
233 //    if(PIR1bits.SSPIF)    I2C_handler();
234 }
235
236 #ifdef __XC
237
238
239 #endif
240 #endif
241
242
243 void lowbatt(void)
244 {
245 //    _CLS();
246 //    idle_time_ms(100);
247     printhelp_2lines("Low battery X)", "Press key to suspend");
248     shutdown(1);
249 }
250
251 int main(void)
252 {
253     unsigned char c;
254     unsigned char pbutton;
255     unsigned char reset_status;
256     unsigned char p;
257     unsigned char lvcount = 0;
258     unsigned char dispf = 0xff;
259     
260     OSCCON =  (0x80 & 0b11111100) | 0b00111000;
261 //    OSCCON =  (0x80 & 0b11111100) | 0b00110010; // 8MHz 
262     idle_init();
263     keyin_init();
264     keyin_ioinit();
265     i2c1_init();
266     reset_status = chk_reset();
267     idle_time_ms(300); // Wait for setup.
268     WDTCONbits.SWDTEN = 0; // WDT OFF.
269     switch(reset_status){
270         case RESET_MCLR:
271         case RESET_BOR:
272             shutdown(0); // Save and halt on BOR.
273             break;
274         case RESET_SOFTWARE: //
275             RCONbits.RI = 0;
276            pbutton = chk_powerbutton();
277             if(pbutton == 0) shutdown(0); // Not-Pressed power-button -> shutdown( not save).
278             break;
279         case RESET_POR:
280         case RESET_WDT:  // Workaround random reset.
281  //           shutdown(0);
282             break;
283         default:
284             break;
285     }
286     WDTCONbits.SWDTEN = 1; // WDT ON.
287     power_on(1);
288     intadc_init();
289     set_powerlamp(1);
290     valinit();
291 #if 1
292     acm1602_init(0xa0, 1); //Init LCD
293 //    idle_time_ms(125);
294 //    _LOCATE(0,0);  // It's BAD-KNOWHOW, but needs AKIZUKI'S LCD :(
295  //   _PUTCHAR(' '); //
296  //   _LOCATE(0,1);
297 //    printstr("Hello;-)");
298     lcd_setbacklight(0xff, 255);
299 //    idle_time_ms(1000);
300     /* Check EEPROM */
301     check_eeprom();
302   /* Push default parameters to AKC6955*/
303     setup_akc6955();
304     _CLS();
305     _LOCATE(0,0);
306     _PUTCHAR(' ');
307     update_status();
308     update_display();
309     ClrWdt();
310     lcd_setbacklight(0xff, 255);
311     do {
312             if(battlevel <= 340) { // 3.4V
313                 lvcount++;
314                 if(lvcount > 4) {
315                     if(dispf == 0) {
316                         acm1602_resume(LCD_I2CADDR);
317                         dispf = 0xff;
318                      }
319                     lowbatt(); //Zap 4Times on LowVoltage.
320                 }
321             } else {
322                 lvcount = 0;
323             }
324         /* Main routine*/
325 //           c = pollkeys(pollkeybuf, 19, 1); // 23*19=437ms
326             c = pollkey_single_timeout(41, 1); // 23*41 = 943ms
327            p = 0;
328 //            while(c > 0) {
329             if(c != charcode_null) {
330                 ClrWdt();
331                 if(dispf == 0) {
332                     acm1602_resume(LCD_I2CADDR);
333                     dispf = 0xff;
334                 }
335                 setfreq_updown(c);
336
337 //                setfreq_updown(pollkeybuf[p]);
338               //  c--;
339               //  p++;
340             }
341 //            _LOCATE(0,0);
342             pbutton = chk_powerbutton(); // 48ms
343             if(pbutton != 0) shutdown(1); // Button pressed.
344             ClrWdt();
345             update_status();
346             if(backlight_counter > 0) {
347                 if(dispf == 0) {
348                     acm1602_resume(LCD_I2CADDR);
349                     dispf = 0xff;
350                 }
351                 backlight_counter--;
352                 lcd_setbacklight(0xff, setup.backlight_level); // Turn ON
353                 update_display();
354             } else {
355                 lcd_setbacklight(0x00, 0); // Turn OFF
356                 acm1602_suspend(LCD_I2CADDR);
357                 dispf = 0;
358         }
359             idle_time_ms(9); // Pad 9ms, 1Loop = 1000ms.
360         ClrWdt();
361     } while(1);
362 #else // EEPROM TEST
363     acm1602_init(0xa0, 1); //Init LCD
364     lcd_setbacklight(0xff, 255);
365     _CLS();
366     {
367         unsigned int addr;
368         unsigned char i;
369         unsigned char pages[32];
370         addr = 0;
371         do {
372             _CLS();
373             print_numeric_nosupress(addr, 5);
374             _PUTCHAR(' ');
375             for(i = 0; i < 2; i++) {
376                 c = i2c_eeprom_byteread(0xa0, addr);
377                 idle_time_ms(10);
378                 print_numeric_nosupress(c, 3);
379                 _PUTCHAR(' ');
380                 addr++;
381                 if(addr > 0x1fff) addr = 0;
382             }
383             _LOCATE(0,1);
384             printstr("  ");
385             for(i = 0; i < 3; i++) {
386                 c = i2c_eeprom_byteread(0xa0, addr);
387                 idle_time_ms(10);
388                 print_numeric_nosupress(c, 3);
389                 _PUTCHAR(' ');
390                 addr++;
391                 if(addr > 0x1fff) addr = 0;
392             }
393             ClrWdt();
394             do {
395             } while(pollkey_single() != charcode_f);
396         } while(1);
397     } 
398 #endif
399 }
400