OSDN Git Service

[I2C_ROM] Block Write/Block Read even unaligned address and size.
[openi2cradio/OpenI2CRadio.git] / main.c
diff --git a/main.c b/main.c
index d6cd9dd..a2cd8bc 100644 (file)
--- a/main.c
+++ b/main.c
@@ -52,7 +52,7 @@
 #include "menu.h"
 #include "power.h"
 #include "adc_int.h"
-
+#include "i2c_eeprom.h"
 /*
  * Config words.
  */
@@ -93,7 +93,6 @@ unsigned int battlevel;
 
 int recv_signal;
 int backlight_counter;
-unsigned char backlight_level;
 unsigned char pollkeybuf[33];
 
 //#define _LCD_DEBUG 1
@@ -215,7 +214,7 @@ DEF_INTHIGH(inthigh_handler)
   //DEF_HANDLER(SIG_BCOL, I2CBus_handler)
 END_DEF
 #else
-void interrupt high_priority intlow_handler(void)
+void interrupt low_priority intlow_handler(void)
 {
     if(INTCONbits.TMR0IF) TMR0_handler();
     if(INTCONbits.INT0IF) EXINT_Handler();
@@ -226,7 +225,7 @@ void interrupt high_priority inthigh_handler(void)
     if(INTCONbits.RBIF) RBIF_handler();
     if(PIR2bits.EEIF)   EEPROM_handler();
     if(PIR2bits.TMR3IF) TMR3_Handler();
-    if(INTCONbits.TMR0IF) TMR0_handler();
+   // if(INTCONbits.TMR0IF) TMR0_handler();
     if(INTCON3bits.INT1IF) EXINT_Handler();
     if(INTCON3bits.INT2IF) EXINT_Handler();
     if(PIR1bits.ADIF) INADC_handler();
@@ -252,55 +251,52 @@ void lowbatt(void)
 int main(void)
 {
     unsigned char c;
-    unsigned int sum = 0;
     unsigned char pbutton;
     unsigned char reset_status;
     unsigned char p;
+    unsigned char lvcount = 0;
+    unsigned char dispf = 0xff;
     
     OSCCON =  (0x80 & 0b11111100) | 0b00111000;
 //    OSCCON =  (0x80 & 0b11111100) | 0b00110010; // 8MHz 
     idle_init();
     keyin_init();
     keyin_ioinit();
-    WDTCONbits.SWDTEN = 0; // WDT OFF.
+    i2c1_init();
     reset_status = chk_reset();
     idle_time_ms(300); // Wait for setup.
+    WDTCONbits.SWDTEN = 0; // WDT OFF.
     switch(reset_status){
         case RESET_MCLR:
-            shutdown(0); // When press reset, state to power_off.
-            break;
-        case RESET_WDT:  // Workaround random reset.
-            shutdown(0);
-            break;
         case RESET_BOR:
             shutdown(0); // Save and halt on BOR.
             break;
         case RESET_SOFTWARE: //
-//            do {
-//                pbutton = chk_powerbutton();
-//                ClrWdt();
- //               if(pbutton == 0) power_off(0); // Button not pressed.
-//            } while(pbutton == 0);
+            RCONbits.RI = 0;
+           pbutton = chk_powerbutton();
+            if(pbutton == 0) shutdown(0); // Not-Pressed power-button -> shutdown( not save).
+            break;
+        case RESET_POR:
+        case RESET_WDT:  // Workaround random reset.
+ //           shutdown(0);
             break;
         default:
             break;
     }
     WDTCONbits.SWDTEN = 1; // WDT ON.
-    i2c1_init();
     power_on(1);
     intadc_init();
     set_powerlamp(1);
-    idle_time_ms(125);
     valinit();
-
+#if 1
     acm1602_init(0xa0, 1); //Init LCD
-    idle_time_ms(125);
-    _LOCATE(0,0);  // It's BAD-KNOWHOW, but needs AKIZUKI'S LCD :(
-    _PUTCHAR(' '); //
-    _LOCATE(0,1);
-    printstr("Hello;-)");
+//    idle_time_ms(125);
+//    _LOCATE(0,0);  // It's BAD-KNOWHOW, but needs AKIZUKI'S LCD :(
//   _PUTCHAR(' '); //
//   _LOCATE(0,1);
+//    printstr("Hello;-)");
     lcd_setbacklight(0xff, 255);
-    idle_time_ms(1000);
+//    idle_time_ms(1000);
     /* Check EEPROM */
     check_eeprom();
   /* Push default parameters to AKC6955*/
@@ -308,40 +304,97 @@ int main(void)
     _CLS();
     _LOCATE(0,0);
     _PUTCHAR(' ');
-//   battlevel = 350;
     update_status();
     update_display();
     ClrWdt();
     lcd_setbacklight(0xff, 255);
     do {
             if(battlevel <= 340) { // 3.4V
-               lowbatt();
+                lvcount++;
+                if(lvcount > 4) {
+                    if(dispf == 0) {
+                        acm1602_resume(LCD_I2CADDR);
+                        dispf = 0xff;
+                     }
+                    lowbatt(); //Zap 4Times on LowVoltage.
+                }
+            } else {
+                lvcount = 0;
             }
         /* Main routine*/
-           c = pollkeys(pollkeybuf, 5, 1); // 23*10=135ms
+//           c = pollkeys(pollkeybuf, 19, 1); // 23*19=437ms
+            c = pollkey_single_timeout(41, 1); // 23*41 = 943ms
            p = 0;
-            while(c > 0) {
+//            while(c > 0) {
+            if(c != charcode_null) {
                 ClrWdt();
-                setfreq_updown(pollkeybuf[p]);
-                c--;
-                 p++;
+                if(dispf == 0) {
+                    acm1602_resume(LCD_I2CADDR);
+                    dispf = 0xff;
+                }
+                setfreq_updown(c);
+
+//                setfreq_updown(pollkeybuf[p]);
+              //  c--;
+              //  p++;
             }
-//       idle_time_ms(ui_idlecount);
-            _LOCATE(0,0);
-            pbutton = chk_powerbutton();
+//            _LOCATE(0,0);
+            pbutton = chk_powerbutton(); // 48ms
             if(pbutton != 0) shutdown(1); // Button pressed.
-
+            ClrWdt();
+            update_status();
             if(backlight_counter > 0) {
+                if(dispf == 0) {
+                    acm1602_resume(LCD_I2CADDR);
+                    dispf = 0xff;
+                }
                 backlight_counter--;
-                lcd_setbacklight(0xff, backlight_level); // Turn ON
+                lcd_setbacklight(0xff, setup.backlight_level); // Turn ON
+                update_display();
             } else {
                 lcd_setbacklight(0x00, 0); // Turn OFF
+                acm1602_suspend(LCD_I2CADDR);
+                dispf = 0;
         }
-//        ClrWdt();
-        // Debug routines
-        update_status();
-        update_display();
+            idle_time_ms(9); // Pad 9ms, 1Loop = 1000ms.
         ClrWdt();
     } while(1);
+#else // EEPROM TEST
+    acm1602_init(0xa0, 1); //Init LCD
+    lcd_setbacklight(0xff, 255);
+    _CLS();
+    {
+        unsigned int addr;
+        unsigned char i;
+        unsigned char pages[32];
+        addr = 0;
+        do {
+            _CLS();
+            print_numeric_nosupress(addr, 5);
+            _PUTCHAR(' ');
+            for(i = 0; i < 2; i++) {
+                c = i2c_eeprom_byteread(0xa0, addr);
+                idle_time_ms(10);
+                print_numeric_nosupress(c, 3);
+                _PUTCHAR(' ');
+                addr++;
+                if(addr > 0x1fff) addr = 0;
+            }
+            _LOCATE(0,1);
+            printstr("  ");
+            for(i = 0; i < 3; i++) {
+                c = i2c_eeprom_byteread(0xa0, addr);
+                idle_time_ms(10);
+                print_numeric_nosupress(c, 3);
+                _PUTCHAR(' ');
+                addr++;
+                if(addr > 0x1fff) addr = 0;
+            }
+            ClrWdt();
+            do {
+            } while(pollkey_single() != charcode_f);
+        } while(1);
+    } 
+#endif
 }