OSDN Git Service

[v1.0] Fix not up-freq on MW1/MW4@5KHz, fix to 10KHz up/down.
[openi2cradio/OpenI2CRadio.git] / akc6955.c
index 76952ed..765618c 100644 (file)
--- a/akc6955.c
+++ b/akc6955.c
 #include "idle.h"
 #include "power.h"
 #include "commondef.h"
+#include "menu.h"
 
-void akc6955_writecmd(unsigned char reg, unsigned char data)
-{
-#ifdef __SDCC
-    i2c_open(I2C_MASTER, I2C_SLEW_ON, 5);
-    I2C_START();
-    i2c_writechar(0x20);
-    i2c_writechar(reg);
-    i2c_writechar(data);
-    I2C_STOP();
-    i2c_close();
-    delay100tcy(2);
-#else
-    OpenI2C(MASTER, SLEW_OFF);
-    StartI2C();
-    WriteI2C(0x20);
-    //delay1ktcy(8);
-    WriteI2C(reg);
-    //delay1ktcy(8);
-    WriteI2C(data);
-    //delay1ktcy(8);
-    StopI2C();
- //   delay1ktcy(8);
-    CloseI2C();
-//    CLOSEASMASTER();
-#endif  //    i2c_idle();
-}
-
-unsigned char akc6955_readcmd(unsigned char reg)
-{
-    unsigned char c;
-    //    OPENASMASTER();
-#ifdef __SDCC
-    i2c_open(I2C_MASTER, I2C_SLEW_ON, 5);
-#else
-    OpenI2C(MASTER, SLEW_OFF);
-#endif
-#ifdef __SDCC
-    I2C_START();
-    i2c_writechar(0x20);
-    i2c_writechar(reg);
-    I2C_STOP();
-    delay100tcy(2);
-    I2C_START();
-    i2c_writechar(0x21);
-    c = i2c_readchar();
-    I2C_ACK();
-    I2C_STOP();
-    i2c_close();
-#else
-    StartI2C();
-    WriteI2C(0x20);
-  //  delay1ktcy(8);
-    WriteI2C(reg);
-  //  delay1ktcy(8);
-    StopI2C();
-    __delay_us(13);
-    StartI2C();
-    WriteI2C(0x21);
-  //  delay1ktcy(8);
-    c = ReadI2C();
-    AckI2C();
-    StopI2C();
- //   delay1ktcy(8);
-    CloseI2C();
-#endif
-    //    CLOSEASMASTER();
-
-    return c;
-}
 
 void akc6955_chg_fm(unsigned char f, unsigned int freq)
 {
     __bitops_t b;
-    unsigned char c;
     b.byte = akc6955_readcmd(AKC6955_POWER);
     b.b6 = 0;
     if(f != 0){
-        rfamp_power(RFAMP_FM);
         b.b6 = 1;
-        akc6955_set_fmband(fmband);
+        akc6955_set_fmband(setup.fmband);
         akc6955_writecmd(AKC6955_POWER, b.byte);
         akc6955_set_freq(freq);
         return;
     }
-    c = RFAMP_SW;
-    if((amband < AKC6955_BAND_SW1) || (amband == AKC6955_BAND_MW4)) {
-        c = RFAMP_MWLW;
-    }
-    rfamp_power(c);
     akc6955_writecmd(AKC6955_POWER, b.byte);
-    akc6955_set_amband(amband);
+    akc6955_set_amband(setup.amband);
     akc6955_set_freq(freq);
 
 }
@@ -149,6 +74,13 @@ unsigned char akc6955_get_fm(void)
 void akc6955_set_amband(unsigned char band)
 {
     unsigned char b;
+    unsigned char c;
+
+    c = RFAMP_SW;
+    if((band < AKC6955_BAND_SW1) || (band == AKC6955_BAND_MW4)) {
+        c = RFAMP_MWLW;
+    }
+    rfamp_power(c);
     b = akc6955_readcmd(AKC6955_BAND);
     b &= 0x07; // extract FM
     b = b | ((band & 0x1f) << 3);
@@ -159,6 +91,7 @@ void akc6955_set_amband(unsigned char band)
 void akc6955_set_fmband(unsigned char band)
 {
     unsigned char b;
+    rfamp_power(RFAMP_FM);
     b = akc6955_readcmd(AKC6955_BAND);
     b &= 0xf8; // extract AM
     b = b | (band & 0x07);
@@ -214,6 +147,9 @@ unsigned char akc6955_tune(void)
 void akc6955_mode3k(unsigned char flag)
 {
     __bitops_t b;
+    b.byte = akc6955_readcmd(AKC6955_CH_LO);
+    akc6955_writecmd(AKC6955_CH_LO, b.byte);
+
     b.byte = akc6955_readcmd(AKC6955_CH_HI);
     b.b5 = 0;
     if(flag != 0){
@@ -221,7 +157,6 @@ void akc6955_mode3k(unsigned char flag)
     }
     akc6955_writecmd(AKC6955_CH_HI, b.byte);
     akc6955_do_tune();
-    _AKC6955_WAIT_62_5MS();
 }
 
 void akc6955_set_tune(unsigned char mode_3k, unsigned int ch)
@@ -238,7 +173,7 @@ void akc6955_set_tune(unsigned char mode_3k, unsigned int ch)
     } while(comp == 0x00);
     f.byte = akc6955_readcmd(AKC6955_POWER);
     band = 0;
-    if(f.b6 == 0){
+    if(!f.b6){
         akc6955_get_amband(band);
     }
 
@@ -266,21 +201,34 @@ void akc6955_set_tune(unsigned char mode_3k, unsigned int ch)
 void akc6955_do_scan(unsigned char up)
 {
     __bitops_t b;
- //   akc6955_do_tune();
+
+    //20130823 Need wait for scan/tune completed w/o SDCC 3.3.x.
+#ifndef __SDCC
+    while(akc6955_chk_donescan() == 0)
+    {
+        idle_time_ms(5);
+    }
+#endif
     b.byte = akc6955_readcmd(AKC6955_POWER);
     b.b3 = 0;
     b.b4 = 0;
-    b.b5 = 0;
+// 20130823 : Is this collect?
+#ifdef __SDCC
+     b.b5 = 0;
     akc6955_writecmd(AKC6955_POWER, b.byte);
+    idle_time_ms(35);
+#endif
+
     b.b5 = 1; // Tune 0->1.
-    idle_time_35ms();
     akc6955_writecmd(AKC6955_POWER, b.byte);
-    idle_time_35ms();
+    idle_time_ms(35);
+
+    b.b4 = 1;
     if(up != 0) {
         b.b3= 1;
     }
-    b.b4 = 1;
     akc6955_writecmd(AKC6955_POWER, b.byte); // Raise seek bit to '1'.
+    idle_time_ms(35);
 }
 
 void akc6955_abort_scan(void)
@@ -350,20 +298,31 @@ void akc6955_set_freq(unsigned int freq)
     __bitops_t f;
     __bitops_t mode3k;
     unsigned char band;
+    unsigned int start, stop;
 
     f.byte = akc6955_readcmd(AKC6955_POWER);
     if(f.b6) { // FM
         akc6955_get_fmband(band);
 //        band &= 7;
-        if(freq <  fmbands[band].start) freq = fmbands[band].start;
-        if(freq >= fmbands[band].end)   freq = fmbands[band].end - 1;
+        if(band == AKC6955_BAND_FMUSER){
+            start = setup.fm_usrbands[setup.fm_userbandnum].start * 32;
+            stop = setup.fm_usrbands[setup.fm_userbandnum].stop * 32;
+        } else {
+            start = fmbands[band].start;
+            stop = fmbands[band].end;
+        }
         ch = freq - 3000;
-        ch = (ch * 4) / 10;
+        ch = (ch << 2) / 10;
     } else {
         akc6955_get_amband(band);
 //        if(band >= AKC6955_BAND_AMEND) band = AKC6955_BAND_AMEND - 1;
-        if(freq <  ambands[band].start) freq = ambands[band].start;
-        if(freq >= ambands[band].end)   freq = ambands[band].end - 1;
+        if(band == AKC6955_BAND_AMUSER){
+            start = setup.am_usrbands[setup.am_userbandnum].start * 32;
+            stop = setup.am_usrbands[setup.am_userbandnum].stop * 32;
+        } else {
+            start = ambands[band].start;
+            stop = ambands[band].end;
+        }
         mode3k.byte = akc6955_readcmd(AKC6955_CNR_AM);
         if(band == AKC6955_BAND_MW2) {
             ch = (freq / 9) * 3; // See datasheet.
@@ -375,6 +334,8 @@ void akc6955_set_freq(unsigned int freq)
             ch = freq / 5;
         }
     }
+    if(freq <  start) freq = start;
+    if(freq >= stop)   freq = stop - 1;
     akc6955_set_tune(mode3k.b7, ch);
 }
 
@@ -471,8 +432,9 @@ void akc6955_up_freq(unsigned int step)
 
     freq = akc6955_get_channel();
     freq += step;
-    mode3k.byte = akc6955_readcmd(AKC6955_CNR_AM);
-    akc6955_set_tune(mode3k.b7, freq);
+//    mode3k.byte = akc6955_readcmd(AKC6955_CNR_AM);
+//    akc6955_set_tune(mode3k.b7, freq);
+    akc6955_set_tune(setup.am_mode3k, freq);
 }
 
 
@@ -484,8 +446,9 @@ void akc6955_down_freq(unsigned int step)
     freq = akc6955_get_channel();
     if(freq <= step) return;
     freq -= step;
-    mode3k.byte = akc6955_readcmd(AKC6955_CNR_AM);
-    akc6955_set_tune(mode3k.b7, freq);
+//    mode3k.byte = akc6955_readcmd(AKC6955_CNR_AM);
+//    akc6955_set_tune(mode3k.b7, freq);
+    akc6955_set_tune(setup.am_mode3k, freq);
 }
 
 void akc6955_setvolumemode(unsigned char flag)
@@ -546,9 +509,9 @@ void akc6955_setphase(unsigned char flag)
     __bitops_t c;
     c.byte = akc6955_readcmd(AKC6955_VOLUME);
 
-    c.b0 = 0;
-    if(flag != 0) {
-        c.b0 = 1; //
+    c.b0 = 1;
+    if(flag == 0) {
+        c.b0 = 0; //
     }
     akc6955_writecmd(AKC6955_VOLUME, c.byte);
 }
@@ -557,9 +520,9 @@ void akc6955_setline(unsigned char flag)
 {
     __bitops_t c;
     c.byte = akc6955_readcmd(AKC6955_VOLUME);
-    c.b1 = 0;
-    if(flag != 0) {
-        c.b1 = 1;
+    c.b1 = 1;
+    if(flag == 0) {
+        c.b1 = 0;
     }
     akc6955_writecmd(AKC6955_VOLUME, c.byte);
 }
@@ -568,9 +531,9 @@ void akc6955_set_lowboost(unsigned char flag)
 {
     __bitops_t c;
     c.byte = akc6955_readcmd(AKC6955_STEREO);
-    c.b3 = 0;
-    if(flag != 0) {
-        c.b3 = 1;
+    c.b3 = 1;
+    if(flag == 0) {
+        c.b3 = 0;
     }
     akc6955_writecmd(AKC6955_STEREO, c.byte);
 }
@@ -605,3 +568,41 @@ unsigned int akc6955_get_battery(void)
     return batt;
 }
 
+void akc6955_set_thresh_fmstereo(unsigned char a)
+{
+    unsigned char b;
+    a = a & 0x03;
+    setup.threshold_fmstereo = a;
+    b = akc6955_readcmd(AKC6955_THRESH) & 0xfc;
+    akc6955_writecmd(AKC6955_THRESH, a | b);
+}
+
+void akc6955_set_thresh_width(unsigned char a)
+{
+    unsigned char b;
+    a = a & 0x03;
+    setup.threshold_width = a;
+    a = a << 2; // << 2
+    b = akc6955_readcmd(AKC6955_THRESH) & 0xf3;;
+    akc6955_writecmd(AKC6955_THRESH, a | b);
+}
+
+void akc6955_set_thresh_amcnr(unsigned char a)
+{
+    unsigned char b;
+    a = a & 0x03;
+    setup.threshold_amcnr = a;
+    a = a << 4; // << 4
+    b = akc6955_readcmd(AKC6955_THRESH) & 0xcf;
+    akc6955_writecmd(AKC6955_THRESH, a | b);
+}
+
+void akc6955_set_thresh_fmcnr(unsigned char a)
+{
+    unsigned char b;
+    a = a & 0x03;
+    setup.threshold_fmcnr = a;
+    a = a << 6; // << 4
+    b = akc6955_readcmd(AKC6955_THRESH) & 0x3f;
+    akc6955_writecmd(AKC6955_THRESH, a | b);
+}