3 * RADIO CHIP AKC6955 Handler
4 * Copyright (C) 2013-06-10 K.Ohta <whatisthis.sowhat ai gmail.com>
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.
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,
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.
41 #include "commondef.h"
45 void akc6955_chg_fm(unsigned char f, unsigned int freq)
48 b.byte = akc6955_readcmd(AKC6955_POWER);
52 akc6955_set_fmband(setup.fmband);
53 akc6955_writecmd(AKC6955_POWER, b.byte);
54 akc6955_set_freq(freq);
57 akc6955_writecmd(AKC6955_POWER, b.byte);
58 akc6955_set_amband(setup.amband);
59 akc6955_set_freq(freq);
63 unsigned char akc6955_get_fm(void)
66 b.byte = akc6955_readcmd(AKC6955_POWER);
74 void akc6955_set_amband(unsigned char band)
80 if((band < AKC6955_BAND_SW1) || (band == AKC6955_BAND_MW4)) {
84 b = akc6955_readcmd(AKC6955_BAND);
85 b &= 0x07; // extract FM
86 b = b | ((band & 0x1f) << 3);
87 akc6955_writecmd(AKC6955_BAND, b);
91 void akc6955_set_fmband(unsigned char band)
94 rfamp_power(RFAMP_FM);
95 b = akc6955_readcmd(AKC6955_BAND);
96 b &= 0xf8; // extract AM
97 b = b | (band & 0x07);
98 akc6955_writecmd(AKC6955_BAND, b);
102 unsigned char akc6955_get_band(void)
104 return akc6955_readcmd(AKC6955_BAND);
108 void akc6955_set_power(unsigned char on)
111 b.byte = akc6955_readcmd(AKC6955_POWER);
116 akc6955_writecmd(AKC6955_POWER, b.byte);
119 void akc6955_do_tune(void)
122 b.byte = akc6955_readcmd(AKC6955_POWER);
123 b.b5 = 0; // Tun = '0'
124 b.b4 = 0; // Force abort scan.
125 akc6955_writecmd(AKC6955_POWER, b.byte);
127 // Need sleep?-> Need! this sequence re fer from reference code.
128 b.b5= 1; // Tun = '1'
129 akc6955_writecmd(AKC6955_POWER, b.byte);
132 akc6955_writecmd(AKC6955_POWER, b.byte);
136 unsigned char akc6955_tune(void)
139 b.byte = akc6955_readcmd(AKC6955_RCH_HI);
147 void akc6955_mode3k(unsigned char flag)
150 b.byte = akc6955_readcmd(AKC6955_CH_HI);
155 akc6955_writecmd(AKC6955_CH_HI, b.byte);
157 _AKC6955_WAIT_62_5MS();
160 void akc6955_set_tune(unsigned char mode_3k, unsigned int ch)
168 do { // Wait for before completed
169 comp = akc6955_chk_donescan();
171 } while(comp == 0x00);
172 f.byte = akc6955_readcmd(AKC6955_POWER);
175 akc6955_get_amband(band);
178 if(band == AKC6955_BAND_MW2){
180 i = ch / 3; // On MW2, Channnel must be multiple of 3.
181 i = i * 3; // i = (i/3) * 3
183 if(i > 0x1fff) i = 0x1fff;
187 akc6955_writecmd(AKC6955_CH_LO, b.byte);
192 b.b5 = 1; // Mode 3K ON
194 akc6955_writecmd(AKC6955_CH_HI, b.byte);
199 void akc6955_do_scan(unsigned char up)
203 //20130823 Need wait for scan/tune completed w/o SDCC 3.3.x.
205 while(akc6955_chk_donescan() == 0)
210 b.byte = akc6955_readcmd(AKC6955_POWER);
213 // 20130823 : Is this collect?
216 akc6955_writecmd(AKC6955_POWER, b.byte);
220 b.b5 = 1; // Tune 0->1.
221 akc6955_writecmd(AKC6955_POWER, b.byte);
228 akc6955_writecmd(AKC6955_POWER, b.byte); // Raise seek bit to '1'.
232 void akc6955_abort_scan(void)
235 b = akc6955_readcmd(AKC6955_POWER);
237 akc6955_writecmd(AKC6955_POWER, b); // Falldown seek bit to '0'.
240 void akc6955_set_scanrate_fm(unsigned char rate)
243 c = akc6955_readcmd(AKC6955_SPACE);
244 c = (c & 0xcf) | ((rate & 0x03) << 4);
245 akc6955_writecmd(AKC6955_SPACE, c);
248 unsigned char akc6955_chk_donescan(void)
251 b.byte = akc6955_readcmd(AKC6955_RCH_HI);
258 unsigned int akc6955_get_channel(void)
262 l = akc6955_readcmd(AKC6955_RCH_LO) ;
263 h = akc6955_readcmd(AKC6955_RCH_HI) & 0x1f;
269 * Get AM/FM Frequency: ret = KHz at AM, 10KHz @FM.
271 unsigned int akc6955_get_freq(void)
278 f.byte = akc6955_readcmd(AKC6955_POWER);
279 i = akc6955_get_channel();
281 freq = (i * 5) / 2 + 3000; // freq' = 25*i[KHz] = (25 / 10) *i [10KHz]
283 b.byte = akc6955_readcmd(AKC6955_CH_HI);
284 akc6955_get_amband(band);
286 if((band == AKC6955_BAND_MW2) || (b.b5)){
293 void akc6955_set_freq(unsigned int freq)
299 unsigned int start, stop;
301 f.byte = akc6955_readcmd(AKC6955_POWER);
303 akc6955_get_fmband(band);
305 if(band == AKC6955_BAND_FMUSER){
306 start = setup.fm_usrbands[setup.fm_userbandnum].start * 32;
307 stop = setup.fm_usrbands[setup.fm_userbandnum].stop * 32;
309 start = fmbands[band].start;
310 stop = fmbands[band].end;
315 akc6955_get_amband(band);
316 // if(band >= AKC6955_BAND_AMEND) band = AKC6955_BAND_AMEND - 1;
317 if(band == AKC6955_BAND_AMUSER){
318 start = setup.am_usrbands[setup.am_userbandnum].start * 32;
319 stop = setup.am_usrbands[setup.am_userbandnum].stop * 32;
321 start = ambands[band].start;
322 stop = ambands[band].end;
324 mode3k.byte = akc6955_readcmd(AKC6955_CNR_AM);
325 if(band == AKC6955_BAND_MW2) {
326 ch = (freq / 9) * 3; // See datasheet.
327 } else if(band == AKC6955_BAND_MW3) {
329 } else if(mode3k.b7){
335 if(freq < start) freq = start;
336 if(freq >= stop) freq = stop - 1;
337 akc6955_set_tune(mode3k.b7, ch);
340 void akc6955_set_userband(unsigned char start, unsigned char stop, unsigned int ch, unsigned char mode3k)
344 f.byte = akc6955_readcmd(AKC6955_POWER);
345 akc6955_writecmd(AKC6955_UCH_ST, start);
346 akc6955_writecmd(AKC6955_UCH_EN, stop);
348 akc6955_set_fmband(AKC6955_BAND_FMUSER);
350 akc6955_set_amband(AKC6955_BAND_AMUSER);
352 akc6955_set_tune(mode3k, ch);
356 unsigned char akc6955_get_cnr(void)
360 f.byte = akc6955_readcmd(AKC6955_POWER);
362 b.byte = akc6955_readcmd(AKC6955_CNR_FM);
364 b.byte = akc6955_readcmd(AKC6955_CNR_AM);
370 int akc6955_read_level(void)
373 unsigned char rflevel, iflevel;
380 f.byte = akc6955_readcmd(AKC6955_POWER);
381 b = akc6955_readcmd(AKC6955_PGALEVEL);
382 rflevel = (b & 0xe0) >> 5;
383 iflevel = (b & 0x1c) >> 2;
384 totallevel = rflevel + iflevel;
386 rssi = (int)(akc6955_readcmd(AKC6955_RSSI) & 0x7f);
387 // totallevel = rssi + 6*totallevel;
388 level = (int)(totallevel * 6 + rssi);
390 level = 103 - level; // totallevel * 6
392 freq = akc6955_get_freq();
393 if(freq > 2560) { // ASSUME SW
395 } else { // ASSUME MW,LW
403 // Get diff. unit = 100Hz.
404 int akc6955_get_diff(void)
410 diff.byte = akc6955_readcmd(AKC6955_FDNUM);
413 n = -((int) diff.byte);
419 f.byte = akc6955_readcmd(AKC6955_POWER);
426 void akc6955_up_freq(unsigned int step)
431 freq = akc6955_get_channel();
433 mode3k.byte = akc6955_readcmd(AKC6955_CNR_AM);
434 akc6955_set_tune(mode3k.b7, freq);
438 void akc6955_down_freq(unsigned int step)
443 freq = akc6955_get_channel();
444 if(freq <= step) return;
446 mode3k.byte = akc6955_readcmd(AKC6955_CNR_AM);
447 akc6955_set_tune(mode3k.b7, freq);
450 void akc6955_setvolumemode(unsigned char flag)
453 c.byte = akc6955_readcmd(AKC6955_ENABLE);
458 akc6955_writecmd(AKC6955_ENABLE, c.byte);
461 unsigned char akc6955_getvolumemode(void)
464 c.byte = akc6955_readcmd(AKC6955_ENABLE);
471 void akc6955_setvolume(unsigned char level)
474 // c = akc6955_readcmd(AKC6955_VOLUME) & 0x03;
475 if(level > 63) level = 63;
476 akc6955_writecmd(AKC6955_VOLUME, ((akc6955_readcmd(AKC6955_VOLUME) & 0x03) | (level << 2)));
479 unsigned char akc6955_getvolume(void)
482 c = akc6955_readcmd(AKC6955_VOLUME) >> 2;
486 void akc6955_set_prevolume(unsigned char level)
489 c = akc6955_readcmd(AKC6955_PRE) & 0xf3;
490 c |= ((level & 0x03) << 2);
491 akc6955_writecmd(AKC6955_PRE, c);
494 unsigned char akc6955_get_prevolume(void)
497 c = akc6955_readcmd(AKC6955_PRE) & 0x0c;
503 void akc6955_setphase(unsigned char flag)
506 c.byte = akc6955_readcmd(AKC6955_VOLUME);
512 akc6955_writecmd(AKC6955_VOLUME, c.byte);
515 void akc6955_setline(unsigned char flag)
518 c.byte = akc6955_readcmd(AKC6955_VOLUME);
523 akc6955_writecmd(AKC6955_VOLUME, c.byte);
526 void akc6955_set_lowboost(unsigned char flag)
529 c.byte = akc6955_readcmd(AKC6955_STEREO);
534 akc6955_writecmd(AKC6955_STEREO, c.byte);
537 void akc6955_set_stereomode(unsigned char mode)
540 c = akc6955_readcmd(AKC6955_STEREO);
541 mode = (mode & 0x03) << 2;
542 c = (c & 0xf3) | mode;
543 akc6955_writecmd(AKC6955_STEREO, c);
546 unsigned char akc6955_get_stereo(void)
549 c = akc6955_readcmd(AKC6955_RCH_HI) & 0x80;
558 unsigned int akc6955_get_battery(void)
562 c = akc6955_readcmd(AKC6955_VBAT) & 0x3f;
567 void akc6955_set_thresh_fmstereo(unsigned char a)
571 setup.threshold_fmstereo = a;
572 b = akc6955_readcmd(AKC6955_THRESH) & 0xfc;
573 akc6955_writecmd(AKC6955_THRESH, a | b);
576 void akc6955_set_thresh_width(unsigned char a)
580 setup.threshold_width = a;
582 b = akc6955_readcmd(AKC6955_THRESH) & 0xf3;;
583 akc6955_writecmd(AKC6955_THRESH, a | b);
586 void akc6955_set_thresh_amcnr(unsigned char a)
590 setup.threshold_amcnr = a;
592 b = akc6955_readcmd(AKC6955_THRESH) & 0xcf;
593 akc6955_writecmd(AKC6955_THRESH, a | b);
596 void akc6955_set_thresh_fmcnr(unsigned char a)
600 setup.threshold_fmcnr = a;
602 b = akc6955_readcmd(AKC6955_THRESH) & 0x3f;
603 akc6955_writecmd(AKC6955_THRESH, a | b);