OSDN Git Service

[UI][HELP] Update help of setup.
[openi2cradio/OpenI2CRadio.git] / akc6955.c
1 /*
2  * OpenI2CRADIO
3  * RADIO CHIP AKC6955 Handler
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 #ifdef __SDCC
32 #include <delay.h>
33 #else
34 #include <xc.h>
35 #endif
36 #include <string.h>
37 #include "akc6955.h"
38 #include "i2c_io.h"
39 #include "idle.h"
40 #include "power.h"
41 #include "commondef.h"
42 #include "menu.h"
43 #include "ui.h"
44
45 unsigned char akc6955_readcmd(unsigned char reg)
46 {
47    return i2c_read_byte(AKC6955_ADDRESS, reg);
48 }
49
50 void akc6955_writecmd(unsigned char reg,unsigned char data)
51 {
52     i2c_send_byte(AKC6955_ADDRESS, reg, data);
53 }
54
55 unsigned char akc6955_get_band(void)
56 {
57     return akc6955_readcmd(AKC6955_BAND);
58 }
59
60 unsigned char akc6955_get_amband(void)
61 {
62    return akc6955_get_band() >> 3; 
63 }
64
65 unsigned char akc6955_get_fmband(void)
66 {
67    return akc6955_get_band() & 0x07; 
68 }
69
70
71 void akc6955_chg_fm(unsigned char f, unsigned int freq)
72 {
73     __bitops_t b;
74     b.byte = akc6955_readcmd(AKC6955_POWER);
75     b.b6 = 0;
76     if(f != 0){
77         b.b6 = 1;
78         akc6955_set_fmband(setup.fmband);
79         akc6955_writecmd(AKC6955_POWER, b.byte);
80         akc6955_set_freq(freq);
81         return;
82     }
83     akc6955_writecmd(AKC6955_POWER, b.byte);
84     akc6955_set_amband(setup.amband);
85     akc6955_set_freq(freq);
86
87 }
88
89 unsigned char akc6955_get_fm(void)
90 {
91     __bitops_t b;
92     b.byte = akc6955_readcmd(AKC6955_POWER);
93     if(b.b6){
94         return 0xff;
95     }
96     return 0;
97 }
98
99
100 void akc6955_set_amband(unsigned char band)
101 {
102     unsigned char b;
103     unsigned char c;
104
105     c = RFAMP_SW;
106     if((band < AKC6955_BAND_SW1) || (band == AKC6955_BAND_MW4)) {
107         c = RFAMP_MWLW;
108     }
109     rfamp_power(c);
110     b = akc6955_readcmd(AKC6955_BAND);
111     b &= 0x07; // extract FM
112     b = b | ((band & 0x1f) << 3);
113     akc6955_writecmd(AKC6955_BAND, b);
114     akc6955_do_tune();
115 }
116
117 void akc6955_set_fmband(unsigned char band)
118 {
119     unsigned char b;
120     rfamp_power(RFAMP_FM);
121     b = akc6955_readcmd(AKC6955_BAND);
122     b &= 0xf8; // extract AM
123     b = b | (band & 0x07);
124     akc6955_writecmd(AKC6955_BAND, b);
125     akc6955_do_tune();
126 }
127
128 void akc6955_set_power(unsigned char on)
129 {
130     __bitops_t b;
131     b.byte = akc6955_readcmd(AKC6955_POWER);
132     b.b7 = 0;
133     if(on != 0){
134         b.b7 = 1;
135     }
136     akc6955_writecmd(AKC6955_POWER, b.byte);
137 }
138
139 void akc6955_do_tune(void)
140 {
141     __bitops_t b;
142     b.byte = akc6955_readcmd(AKC6955_POWER);
143     b.b5 = 0; // Tun = '0'
144     b.b4 = 0; // Force abort scan.
145     akc6955_writecmd(AKC6955_POWER, b.byte);
146     idle_time_ms(1);
147     // Need sleep?-> Need! this sequence re                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 fer from reference code.
148     b.b5= 1; // Tun = '1'
149     akc6955_writecmd(AKC6955_POWER, b.byte);
150     idle_time_ms(1);
151     b.b5 = 0;
152     akc6955_writecmd(AKC6955_POWER, b.byte);
153     idle_time_ms(8);
154 }
155
156 unsigned char akc6955_tune(void)
157 {
158     __bitops_t b;
159     b.byte = akc6955_readcmd(AKC6955_RCH_HI);
160     if(b.b5) {
161         return 0xff;
162     }
163     return 0;
164 }
165
166
167 void akc6955_mode3k(unsigned char flag)
168 {
169     __bitops_t b;
170     b.byte = akc6955_readcmd(AKC6955_CH_LO);
171     akc6955_writecmd(AKC6955_CH_LO, b.byte);
172
173     b.byte = akc6955_readcmd(AKC6955_CH_HI);
174     b.b5 = 0;
175     if(flag != 0){
176         b.b5 = 1;
177     }
178     akc6955_writecmd(AKC6955_CH_HI, b.byte);
179     akc6955_do_tune();
180 }
181
182 unsigned char akc6955_get_mode3k(void)
183 {
184     __bitops_t c;
185     c.byte = akc6955_readcmd(AKC6955_CNR_AM);
186     if(c.b7) {
187         return 0xff;
188     }
189     return 0;
190 }
191
192 void akc6955_set_tune(unsigned char mode_3k, unsigned int ch)
193 {
194     unsigned char band;
195     unsigned char f;
196     __bitops_t b;
197     unsigned int i = ch;
198     unsigned char comp;
199     unsigned int start, stop;
200     unsigned char q;
201
202     do { // Wait for before completed
203         comp = akc6955_chk_donescan();
204         idle_time_35ms();
205     } while(comp == 0x00);
206
207     f = akc6955_get_fm();
208     band = 0;
209     if(f == 0){
210         band = akc6955_get_amband();
211         if(band == AKC6955_BAND_AMUSER){
212             start = userband.am_usrbands[setup.am_userbandnum].start * 32;
213             stop = userband.am_usrbands[setup.am_userbandnum].stop * 32;
214         } else {
215             if(mode_3k != 0) {
216                 q = 3;
217             } else {
218                 q = 5;
219             }
220             start = ambands[band].start / q;
221             stop  = ambands[band].end / q;
222         }
223     } else {
224         band = akc6955_get_fmband();
225         start = ((fmbands[band].start - 3000) * 2) / 5;
226         stop = ((fmbands[band].end - 3000) * 2) / 5;;
227         if(band == AKC6955_BAND_FMUSER){
228             start = userband.fm_usrbands[setup.fm_userbandnum].start * 32;
229             stop = userband.fm_usrbands[setup.fm_userbandnum].stop * 32;
230         }
231     }
232     if(i > stop) i = stop; // ADD
233     if(i < start) i = start;
234
235     b.byte = i & 0xff;
236     akc6955_writecmd(AKC6955_CH_LO, b.byte);
237
238     b.byte = i >> 8;
239     b.b6 = 1;
240     if(mode_3k != 0){
241         b.b5 = 1; // Mode 3K ON
242     }
243     akc6955_writecmd(AKC6955_CH_HI, b.byte);
244
245     akc6955_do_tune();
246 }
247
248 void akc6955_do_scan(unsigned char up)
249 {
250     __bitops_t b;
251     unsigned char c;
252
253     //20130823 Need wait for scan/tune completed w/o SDCC 3.3.x.
254 #ifndef __SDCC
255     while(akc6955_chk_donescan() == 0)
256     {
257         c = pollkey_single_timeout(1, 0);  // 23ms
258         if(c == charcode_a) return; // IF 'A' abort.
259         //idle_time_ms(35);
260     }
261 #endif
262     b.byte = akc6955_readcmd(AKC6955_POWER);
263     b.b3 = 0;
264     b.b4 = 0;
265 // 20130823 : Is this collect?
266 #ifdef __SDCC
267      b.b5 = 0;
268     akc6955_writecmd(AKC6955_POWER, b.byte);
269     idle_time_ms(35);
270 #endif
271
272     b.b5 = 1; // Tune 0->1.
273     akc6955_writecmd(AKC6955_POWER, b.byte);
274     idle_time_ms(35);
275
276     b.b4 = 1;
277     if(up != 0) {
278         b.b3= 1;
279     }
280     akc6955_writecmd(AKC6955_POWER, b.byte); // Raise seek bit to '1'.
281     idle_time_ms(35);
282 }
283
284 void akc6955_abort_scan(void)
285 {
286     unsigned char b;
287     b = akc6955_readcmd(AKC6955_POWER);
288     b &= 0xef;
289     akc6955_writecmd(AKC6955_POWER, b); // Falldown seek bit to '0'.
290 }
291
292 void akc6955_set_scanrate_fm(unsigned char rate)
293 {
294     unsigned char c;
295     c = akc6955_readcmd(AKC6955_SPACE);
296     c = (c & 0xcf) | ((rate & 0x03) << 4);
297     akc6955_writecmd(AKC6955_SPACE, c);
298 }
299
300 unsigned char akc6955_get_scanrate_fm(void)
301 {
302     unsigned char c;
303     c = akc6955_readcmd(AKC6955_SPACE);
304     c = (c & 0x30) >> 4;
305    return c;
306 }
307
308 unsigned char akc6955_chk_donescan(void)
309 {
310     __bitops_t b;
311     b.byte = akc6955_readcmd(AKC6955_RCH_HI);
312     if(b.b6){
313         return 0xff;
314     }
315     return 0;
316 }
317
318 unsigned int akc6955_get_channel(void)
319 {
320     unsigned char h, l;
321     unsigned int u;
322     l = akc6955_readcmd(AKC6955_RCH_LO) ;
323     h = akc6955_readcmd(AKC6955_RCH_HI) & 0x1f;
324     u = (h << 8) | l;
325     return u;
326 }
327
328 /*
329  * Get AM/FM Frequency: ret = KHz at AM, 10KHz @FM.
330  */
331 unsigned int akc6955_get_freq(void)
332 {
333     __bitops_t b;
334     unsigned int i;
335     unsigned int freq;
336     unsigned char band;
337     unsigned char q;
338     unsigned char f;
339
340     f = akc6955_get_fm();
341     i = akc6955_get_channel();
342     if(f != 0){ // 25KHz
343         freq = i * 2 + i / 2 + 3000; // freq' = 25*i[KHz] = (25 / 10) *i [10KHz]
344     } else { // 5K
345        b.byte = akc6955_readcmd(AKC6955_CH_HI);
346        band = akc6955_get_amband();
347        if((band == AKC6955_BAND_MW2) || (band == AKC6955_BAND_LW) || (b.b5)){
348            q = 3;
349        } else {
350            q = 5;
351        }
352        freq = i * q;
353     }
354     return freq;
355 }
356
357 void akc6955_set_freq(unsigned int freq)
358 {
359     unsigned int ch;
360     __bitops_t mode3k;
361     unsigned char band;
362     unsigned char q;
363     unsigned char f;
364
365     f = akc6955_get_fm();
366     if(f != 0) { // FM
367        band = akc6955_get_fmband();
368        //        band &= 7;
369        ch = ((freq - 3000) * 2) / 5;
370     } else {
371         band = akc6955_get_amband();
372         mode3k.byte = akc6955_readcmd(AKC6955_CNR_AM);
373         if((band == AKC6955_BAND_MW2) || (band == AKC6955_BAND_LW) || (mode3k.b7)){
374             q = 3;
375             mode3k.b7 = 1;
376         } else {
377             q = 5;
378             mode3k.b7 = 0;
379         }
380         ch = freq / q;
381     }
382     akc6955_set_tune(mode3k.b7, ch);
383 }
384
385 void akc6955_set_userband(unsigned char start, unsigned char stop, unsigned int ch, unsigned char mode3k)
386 {
387     unsigned char f;
388
389     f = akc6955_get_fm();
390     akc6955_writecmd(AKC6955_UCH_ST, start);
391     akc6955_writecmd(AKC6955_UCH_EN, stop);
392     if(f != 0){
393         akc6955_set_fmband(AKC6955_BAND_FMUSER);
394     } else {
395         akc6955_set_amband(AKC6955_BAND_AMUSER);
396     }
397     akc6955_set_tune(mode3k, ch);
398 }
399
400
401 unsigned char akc6955_get_cnr(void)
402 {
403     unsigned char f;
404     __bitops_t b;
405     f = akc6955_get_fm();
406     if(f != 0) { // FM
407         b.byte = akc6955_readcmd(AKC6955_CNR_FM);
408     } else { // AM
409         b.byte = akc6955_readcmd(AKC6955_CNR_AM);
410     }
411     b.b7 = 0;
412     return b.byte;
413 }
414
415 int akc6955_read_level(void)
416 {
417     unsigned char f;
418     unsigned char rflevel, iflevel;
419     unsigned char b;
420     int rssi;
421     unsigned char band;
422     int totallevel;
423     int level;
424
425     f = akc6955_get_fm();
426     b =  akc6955_readcmd(AKC6955_PGALEVEL);
427     rflevel = (b & 0xe0) >> 5;
428     iflevel = (b & 0x1c) >> 2;
429     totallevel = rflevel + iflevel;
430
431     rssi = (int)(akc6955_readcmd(AKC6955_RSSI) & 0x7f);
432     level = (int)(totallevel * 6 + rssi);
433     if(f != 0){
434         level = 103 - level; // totallevel * 6
435     } else {
436         band = akc6955_get_amband();
437         if((band >= AKC6955_BAND_SW1) && (band <= AKC6955_BAND_AMUSER)) { // SW
438             level = 103 - level;
439         } else { //  MW,LW
440             level = 123 - level;
441         }
442     }
443     return level;
444 }
445
446
447 // Get diff. unit = 100Hz.
448 int akc6955_get_diff(void)
449 {
450     __bitops_t diff;
451     unsigned char f;
452     int n;
453
454     diff.byte = akc6955_readcmd(AKC6955_FDNUM);
455     if(diff.b7) {
456         diff.b7 = 0;
457         n = -((int) diff.byte);
458     } else {
459         diff.b7 = 0;
460         n = (int)diff.byte;
461     }
462
463     f = akc6955_get_fm();
464     if(f != 0) { // FM
465         return n * 10;
466     }
467     return n; // 10n
468 }
469
470 void akc6955_up_freq(unsigned int step)
471 {
472     unsigned int freq;
473     __bitops_t mode3k;
474
475     freq = akc6955_get_channel();
476     freq += step;
477     mode3k.byte = akc6955_readcmd(AKC6955_CNR_AM);
478     akc6955_set_tune(mode3k.b7, freq);
479 }
480
481
482 void akc6955_down_freq(unsigned int step)
483 {
484     unsigned int freq;
485     __bitops_t mode3k;
486
487     freq = akc6955_get_channel();
488     if(freq <= step) return;
489     freq -= step;
490     mode3k.byte = akc6955_readcmd(AKC6955_CNR_AM);
491     akc6955_set_tune(mode3k.b7, freq);
492 }
493
494 void akc6955_setvolumemode(unsigned char flag)
495 {
496     __bitops_t c;
497     c.byte = akc6955_readcmd(AKC6955_ENABLE);
498     c.b3 = 0;
499     if(flag != 0x00){
500         c.b3 = 1;
501     }
502     akc6955_writecmd(AKC6955_ENABLE, c.byte);
503 }
504
505 unsigned char akc6955_getvolumemode(void)
506 {
507     __bitops_t c;
508     c.byte = akc6955_readcmd(AKC6955_ENABLE);
509     if(c.b3){
510         return 0xff;
511     }
512     return 0;
513 }
514
515 void akc6955_setvolume(unsigned char level)
516 {
517 //    unsigned char c;
518 //    c = akc6955_readcmd(AKC6955_VOLUME) & 0x03;
519     if(level > 63) level = 63;
520     akc6955_writecmd(AKC6955_VOLUME, ((akc6955_readcmd(AKC6955_VOLUME) & 0x03) | (level << 2)));
521 }
522
523 unsigned char akc6955_getvolume(void)
524 {
525     unsigned char c;
526     c = akc6955_readcmd(AKC6955_VOLUME) >> 2;
527     return c;
528 }
529
530 void akc6955_set_prevolume(unsigned char level)
531 {
532     unsigned char c;
533     c = akc6955_readcmd(AKC6955_PRE) & 0xf3;
534     c |= ((level & 0x03) << 2);
535     akc6955_writecmd(AKC6955_PRE, c);
536 }
537
538 unsigned char akc6955_get_prevolume(void)
539 {
540     unsigned char c;
541     c = akc6955_readcmd(AKC6955_PRE) & 0x0c;
542     c >>= 2;
543     return c;
544 }
545
546
547 void akc6955_setphase(unsigned char flag)
548 {
549     __bitops_t c;
550     c.byte = akc6955_readcmd(AKC6955_VOLUME);
551
552     c.b0 = 1;
553     if(flag == 0) {
554         c.b0 = 0; //
555     }
556     akc6955_writecmd(AKC6955_VOLUME, c.byte);
557 }
558
559 void akc6955_setline(unsigned char flag)
560 {
561     __bitops_t c;
562     c.byte = akc6955_readcmd(AKC6955_VOLUME);
563     c.b1 = 1;
564     if(flag == 0) {
565         c.b1 = 0;
566     }
567     akc6955_writecmd(AKC6955_VOLUME, c.byte);
568 }
569
570 void akc6955_set_lowboost(unsigned char flag)
571 {
572     __bitops_t c;
573     c.byte = akc6955_readcmd(AKC6955_STEREO);
574     c.b3 = 1;
575     if(flag == 0) {
576         c.b3 = 0;
577     }
578     akc6955_writecmd(AKC6955_STEREO, c.byte);
579 }
580
581 void akc6955_set_stereomode(unsigned char mode)
582 {
583     unsigned char c;
584     c = akc6955_readcmd(AKC6955_STEREO);
585     mode = (mode & 0x03) << 2;
586     c = (c & 0xf3) | mode;
587     akc6955_writecmd(AKC6955_STEREO, c);
588 }
589
590 unsigned char akc6955_get_stereo(void)
591 {
592     unsigned char c;
593     c = akc6955_readcmd(AKC6955_RCH_HI) & 0x80;
594     return c;
595 }
596
597 /*
598  * Get battery level.
599  * Unit = 0.01V
600  */
601
602 unsigned int akc6955_get_battery(void)
603 {
604     unsigned int batt;
605     unsigned char c;
606     c = akc6955_readcmd(AKC6955_VBAT) & 0x3f;
607     batt = 180 + c * 5;
608     return batt;
609 }
610
611 /*
612  * Misc
613  */
614 void akc6955_set_fmbandwidth(unsigned char bw)
615 {
616    unsigned char c = akc6955_readcmd(AKC6955_STEREO);
617    c = (c & 0xfc) | (bw & 0x03);
618    akc6955_writecmd(AKC6955_STEREO, c);
619 }
620
621 unsigned char akc6955_get_fmbandwidth(void)
622 {
623     return (akc6955_readcmd(AKC6955_STEREO) & 0x03);
624 }
625
626
627
628 void akc6955_set_thresh_fmstereo(unsigned char a)
629 {
630     unsigned char b;
631     a = a & 0x03;
632     setup.threshold_fmstereo = a;
633     b = akc6955_readcmd(AKC6955_THRESH) & 0xfc;
634     akc6955_writecmd(AKC6955_THRESH, a | b);
635 }
636
637 void akc6955_set_thresh_width(unsigned char a)
638 {
639     unsigned char b;
640     a = a & 0x03;
641     setup.threshold_width = a;
642     a = a << 2; // << 2
643     b = akc6955_readcmd(AKC6955_THRESH) & 0xf3;;
644     akc6955_writecmd(AKC6955_THRESH, a | b);
645 }
646
647 void akc6955_set_thresh_amcnr(unsigned char a)
648 {
649     unsigned char b;
650     a = a & 0x03;
651     setup.threshold_amcnr = a;
652     a = a << 4; // << 4
653     b = akc6955_readcmd(AKC6955_THRESH) & 0xcf;
654     akc6955_writecmd(AKC6955_THRESH, a | b);
655 }
656
657 void akc6955_set_thresh_fmcnr(unsigned char a)
658 {
659     unsigned char b;
660     a = a & 0x03;
661     setup.threshold_fmcnr = a;
662     a = a << 6; // << 4
663     b = akc6955_readcmd(AKC6955_THRESH) & 0x3f;
664     akc6955_writecmd(AKC6955_THRESH, a | b);
665 }