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"
44 void akc6955_writecmd(unsigned char reg, unsigned char data)
47 i2c_open(I2C_MASTER, I2C_SLEW_ON, 5);
56 OpenI2C(MASTER, SLEW_OFF);
71 unsigned char akc6955_readcmd(unsigned char reg)
76 i2c_open(I2C_MASTER, I2C_SLEW_ON, 5);
78 OpenI2C(MASTER, SLEW_OFF);
114 void akc6955_chg_fm(unsigned char f, unsigned int freq)
117 b.byte = akc6955_readcmd(AKC6955_POWER);
121 akc6955_set_fmband(fmband);
122 akc6955_writecmd(AKC6955_POWER, b.byte);
123 akc6955_set_freq(freq);
126 akc6955_writecmd(AKC6955_POWER, b.byte);
127 akc6955_set_amband(amband);
128 akc6955_set_freq(freq);
132 unsigned char akc6955_get_fm(void)
135 b.byte = akc6955_readcmd(AKC6955_POWER);
143 void akc6955_set_amband(unsigned char band)
149 if((band < AKC6955_BAND_SW1) || (band == AKC6955_BAND_MW4)) {
153 b = akc6955_readcmd(AKC6955_BAND);
154 b &= 0x07; // extract FM
155 b = b | ((band & 0x1f) << 3);
156 akc6955_writecmd(AKC6955_BAND, b);
160 void akc6955_set_fmband(unsigned char band)
163 rfamp_power(RFAMP_FM);
164 b = akc6955_readcmd(AKC6955_BAND);
165 b &= 0xf8; // extract AM
166 b = b | (band & 0x07);
167 akc6955_writecmd(AKC6955_BAND, b);
171 unsigned char akc6955_get_band(void)
173 return akc6955_readcmd(AKC6955_BAND);
177 void akc6955_set_power(unsigned char on)
180 b.byte = akc6955_readcmd(AKC6955_POWER);
185 akc6955_writecmd(AKC6955_POWER, b.byte);
188 void akc6955_do_tune(void)
191 b.byte = akc6955_readcmd(AKC6955_POWER);
192 b.b5 = 0; // Tun = '0'
193 b.b4 = 0; // Force abort scan.
194 akc6955_writecmd(AKC6955_POWER, b.byte);
196 // Need sleep?-> Need! this sequence re fer from reference code.
197 b.b5= 1; // Tun = '1'
198 akc6955_writecmd(AKC6955_POWER, b.byte);
201 akc6955_writecmd(AKC6955_POWER, b.byte);
205 unsigned char akc6955_tune(void)
208 b.byte = akc6955_readcmd(AKC6955_RCH_HI);
216 void akc6955_mode3k(unsigned char flag)
219 b.byte = akc6955_readcmd(AKC6955_CH_HI);
224 akc6955_writecmd(AKC6955_CH_HI, b.byte);
226 _AKC6955_WAIT_62_5MS();
229 void akc6955_set_tune(unsigned char mode_3k, unsigned int ch)
237 do { // Wait for before completed
238 comp = akc6955_chk_donescan();
240 } while(comp == 0x00);
241 f.byte = akc6955_readcmd(AKC6955_POWER);
244 akc6955_get_amband(band);
247 if(band == AKC6955_BAND_MW2){
249 i = ch / 3; // On MW2, Channnel must be multiple of 3.
250 i = i * 3; // i = (i/3) * 3
252 if(i > 0x1fff) i = 0x1fff;
256 akc6955_writecmd(AKC6955_CH_LO, b.byte);
261 b.b5 = 1; // Mode 3K ON
263 akc6955_writecmd(AKC6955_CH_HI, b.byte);
268 void akc6955_do_scan(unsigned char up)
271 // akc6955_do_tune();
272 b.byte = akc6955_readcmd(AKC6955_POWER);
276 akc6955_writecmd(AKC6955_POWER, b.byte);
277 b.b5 = 1; // Tune 0->1.
279 akc6955_writecmd(AKC6955_POWER, b.byte);
285 akc6955_writecmd(AKC6955_POWER, b.byte); // Raise seek bit to '1'.
288 void akc6955_abort_scan(void)
291 b = akc6955_readcmd(AKC6955_POWER);
293 akc6955_writecmd(AKC6955_POWER, b); // Falldown seek bit to '0'.
296 void akc6955_set_scanrate_fm(unsigned char rate)
299 c = akc6955_readcmd(AKC6955_SPACE);
300 c = (c & 0xcf) | ((rate & 0x03) << 4);
301 akc6955_writecmd(AKC6955_SPACE, c);
304 unsigned char akc6955_chk_donescan(void)
307 b.byte = akc6955_readcmd(AKC6955_RCH_HI);
314 unsigned int akc6955_get_channel(void)
318 l = akc6955_readcmd(AKC6955_RCH_LO) ;
319 h = akc6955_readcmd(AKC6955_RCH_HI) & 0x1f;
325 * Get AM/FM Frequency: ret = KHz at AM, 10KHz @FM.
327 unsigned int akc6955_get_freq(void)
334 f.byte = akc6955_readcmd(AKC6955_POWER);
335 i = akc6955_get_channel();
337 freq = (i * 5) / 2 + 3000; // freq' = 25*i[KHz] = (25 / 10) *i [10KHz]
339 b.byte = akc6955_readcmd(AKC6955_CH_HI);
340 akc6955_get_amband(band);
342 if((band == AKC6955_BAND_MW2) || (b.b5)){
349 void akc6955_set_freq(unsigned int freq)
355 unsigned int start, stop;
357 f.byte = akc6955_readcmd(AKC6955_POWER);
359 akc6955_get_fmband(band);
361 if(band == AKC6955_BAND_FMUSER){
362 start = fm_usrbands[fm_userbandnum].start * 32;
363 stop = fm_usrbands[fm_userbandnum].stop * 32;
365 start = fmbands[band].start;
366 stop = fmbands[band].end;
371 akc6955_get_amband(band);
372 // if(band >= AKC6955_BAND_AMEND) band = AKC6955_BAND_AMEND - 1;
373 if(band == AKC6955_BAND_AMUSER){
374 start = am_usrbands[am_userbandnum].start * 32;
375 stop = am_usrbands[am_userbandnum].stop * 32;
377 start = ambands[band].start;
378 stop = ambands[band].end;
380 mode3k.byte = akc6955_readcmd(AKC6955_CNR_AM);
381 if(band == AKC6955_BAND_MW2) {
382 ch = (freq / 9) * 3; // See datasheet.
383 } else if(band == AKC6955_BAND_MW3) {
385 } else if(mode3k.b7){
391 if(freq < start) freq = start;
392 if(freq >= stop) freq = stop - 1;
393 akc6955_set_tune(mode3k.b7, ch);
396 void akc6955_set_userband(unsigned char start, unsigned char stop, unsigned int ch, unsigned char mode3k)
400 f.byte = akc6955_readcmd(AKC6955_POWER);
401 akc6955_writecmd(AKC6955_UCH_ST, start);
402 akc6955_writecmd(AKC6955_UCH_EN, stop);
404 akc6955_set_fmband(AKC6955_BAND_FMUSER);
406 akc6955_set_amband(AKC6955_BAND_AMUSER);
408 akc6955_set_tune(mode3k, ch);
412 unsigned char akc6955_get_cnr(void)
416 f.byte = akc6955_readcmd(AKC6955_POWER);
418 b.byte = akc6955_readcmd(AKC6955_CNR_FM);
420 b.byte = akc6955_readcmd(AKC6955_CNR_AM);
426 int akc6955_read_level(void)
429 unsigned char rflevel, iflevel;
436 f.byte = akc6955_readcmd(AKC6955_POWER);
437 b = akc6955_readcmd(AKC6955_PGALEVEL);
438 rflevel = (b & 0xe0) >> 5;
439 iflevel = (b & 0x1c) >> 2;
440 totallevel = rflevel + iflevel;
442 rssi = (int)(akc6955_readcmd(AKC6955_RSSI) & 0x7f);
443 // totallevel = rssi + 6*totallevel;
444 level = (int)(totallevel * 6 + rssi);
446 level = 103 - level; // totallevel * 6
448 freq = akc6955_get_freq();
449 if(freq > 2560) { // ASSUME SW
451 } else { // ASSUME MW,LW
459 // Get diff. unit = 100Hz.
460 int akc6955_get_diff(void)
466 diff.byte = akc6955_readcmd(AKC6955_FDNUM);
469 n = -((int) diff.byte);
475 f.byte = akc6955_readcmd(AKC6955_POWER);
482 void akc6955_up_freq(unsigned int step)
487 freq = akc6955_get_channel();
489 mode3k.byte = akc6955_readcmd(AKC6955_CNR_AM);
490 akc6955_set_tune(mode3k.b7, freq);
494 void akc6955_down_freq(unsigned int step)
499 freq = akc6955_get_channel();
500 if(freq <= step) return;
502 mode3k.byte = akc6955_readcmd(AKC6955_CNR_AM);
503 akc6955_set_tune(mode3k.b7, freq);
506 void akc6955_setvolumemode(unsigned char flag)
509 c.byte = akc6955_readcmd(AKC6955_ENABLE);
514 akc6955_writecmd(AKC6955_ENABLE, c.byte);
517 unsigned char akc6955_getvolumemode(void)
520 c.byte = akc6955_readcmd(AKC6955_ENABLE);
527 void akc6955_setvolume(unsigned char level)
530 // c = akc6955_readcmd(AKC6955_VOLUME) & 0x03;
531 if(level > 63) level = 63;
532 akc6955_writecmd(AKC6955_VOLUME, ((akc6955_readcmd(AKC6955_VOLUME) & 0x03) | (level << 2)));
535 unsigned char akc6955_getvolume(void)
538 c = akc6955_readcmd(AKC6955_VOLUME) >> 2;
542 void akc6955_set_prevolume(unsigned char level)
545 c = akc6955_readcmd(AKC6955_PRE) & 0xf3;
546 c |= ((level & 0x03) << 2);
547 akc6955_writecmd(AKC6955_PRE, c);
550 unsigned char akc6955_get_prevolume(void)
553 c = akc6955_readcmd(AKC6955_PRE) & 0x0c;
559 void akc6955_setphase(unsigned char flag)
562 c.byte = akc6955_readcmd(AKC6955_VOLUME);
568 akc6955_writecmd(AKC6955_VOLUME, c.byte);
571 void akc6955_setline(unsigned char flag)
574 c.byte = akc6955_readcmd(AKC6955_VOLUME);
579 akc6955_writecmd(AKC6955_VOLUME, c.byte);
582 void akc6955_set_lowboost(unsigned char flag)
585 c.byte = akc6955_readcmd(AKC6955_STEREO);
590 akc6955_writecmd(AKC6955_STEREO, c.byte);
593 void akc6955_set_stereomode(unsigned char mode)
596 c = akc6955_readcmd(AKC6955_STEREO);
597 mode = (mode & 0x03) << 2;
598 c = (c & 0xf3) | mode;
599 akc6955_writecmd(AKC6955_STEREO, c);
602 unsigned char akc6955_get_stereo(void)
605 c = akc6955_readcmd(AKC6955_RCH_HI) & 0x80;
614 unsigned int akc6955_get_battery(void)
618 c = akc6955_readcmd(AKC6955_VBAT) & 0x3f;
623 void akc6955_set_thresh_fmstereo(unsigned char a)
627 threshold_fmstereo = a;
628 b = akc6955_readcmd(AKC6955_THRESH) & 0xfc;
629 akc6955_writecmd(AKC6955_THRESH, a | b);
632 void akc6955_set_thresh_width(unsigned char a)
638 b = akc6955_readcmd(AKC6955_THRESH) & 0xf3;;
639 akc6955_writecmd(AKC6955_THRESH, a | b);
642 void akc6955_set_thresh_amcnr(unsigned char a)
648 b = akc6955_readcmd(AKC6955_THRESH) & 0xcf;
649 akc6955_writecmd(AKC6955_THRESH, a | b);
652 void akc6955_set_thresh_fmcnr(unsigned char a)
658 b = akc6955_readcmd(AKC6955_THRESH) & 0x3f;
659 akc6955_writecmd(AKC6955_THRESH, a | b);