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"
43 void akc6955_writecmd(unsigned char reg, unsigned char data)
46 i2c_open(I2C_MASTER, I2C_SLEW_ON, 5);
55 OpenI2C(MASTER, SLEW_OFF);
70 unsigned char akc6955_readcmd(unsigned char reg)
75 i2c_open(I2C_MASTER, I2C_SLEW_ON, 5);
77 OpenI2C(MASTER, SLEW_OFF);
113 void akc6955_chg_fm(unsigned char f, unsigned int freq)
116 b.byte = akc6955_readcmd(AKC6955_POWER);
120 akc6955_set_fmband(fmband);
121 akc6955_writecmd(AKC6955_POWER, b.byte);
122 akc6955_set_freq(freq);
125 akc6955_writecmd(AKC6955_POWER, b.byte);
126 akc6955_set_amband(amband);
127 akc6955_set_freq(freq);
131 unsigned char akc6955_get_fm(void)
134 b.byte = akc6955_readcmd(AKC6955_POWER);
142 void akc6955_set_amband(unsigned char band)
148 if((band < AKC6955_BAND_SW1) || (band == AKC6955_BAND_MW4)) {
152 b = akc6955_readcmd(AKC6955_BAND);
153 b &= 0x07; // extract FM
154 b = b | ((band & 0x1f) << 3);
155 akc6955_writecmd(AKC6955_BAND, b);
159 void akc6955_set_fmband(unsigned char band)
162 rfamp_power(RFAMP_FM);
163 b = akc6955_readcmd(AKC6955_BAND);
164 b &= 0xf8; // extract AM
165 b = b | (band & 0x07);
166 akc6955_writecmd(AKC6955_BAND, b);
170 unsigned char akc6955_get_band(void)
172 return akc6955_readcmd(AKC6955_BAND);
176 void akc6955_set_power(unsigned char on)
179 b.byte = akc6955_readcmd(AKC6955_POWER);
184 akc6955_writecmd(AKC6955_POWER, b.byte);
187 void akc6955_do_tune(void)
190 b.byte = akc6955_readcmd(AKC6955_POWER);
191 b.b5 = 0; // Tun = '0'
192 b.b4 = 0; // Force abort scan.
193 akc6955_writecmd(AKC6955_POWER, b.byte);
195 // Need sleep?-> Need! this sequence re fer from reference code.
196 b.b5= 1; // Tun = '1'
197 akc6955_writecmd(AKC6955_POWER, b.byte);
200 akc6955_writecmd(AKC6955_POWER, b.byte);
204 unsigned char akc6955_tune(void)
207 b.byte = akc6955_readcmd(AKC6955_RCH_HI);
215 void akc6955_mode3k(unsigned char flag)
218 b.byte = akc6955_readcmd(AKC6955_CH_HI);
223 akc6955_writecmd(AKC6955_CH_HI, b.byte);
225 _AKC6955_WAIT_62_5MS();
228 void akc6955_set_tune(unsigned char mode_3k, unsigned int ch)
236 do { // Wait for before completed
237 comp = akc6955_chk_donescan();
239 } while(comp == 0x00);
240 f.byte = akc6955_readcmd(AKC6955_POWER);
243 akc6955_get_amband(band);
246 if(band == AKC6955_BAND_MW2){
248 i = ch / 3; // On MW2, Channnel must be multiple of 3.
249 i = i * 3; // i = (i/3) * 3
251 if(i > 0x1fff) i = 0x1fff;
255 akc6955_writecmd(AKC6955_CH_LO, b.byte);
260 b.b5 = 1; // Mode 3K ON
262 akc6955_writecmd(AKC6955_CH_HI, b.byte);
267 void akc6955_do_scan(unsigned char up)
270 // akc6955_do_tune();
271 b.byte = akc6955_readcmd(AKC6955_POWER);
275 akc6955_writecmd(AKC6955_POWER, b.byte);
276 b.b5 = 1; // Tune 0->1.
278 akc6955_writecmd(AKC6955_POWER, b.byte);
284 akc6955_writecmd(AKC6955_POWER, b.byte); // Raise seek bit to '1'.
287 void akc6955_abort_scan(void)
290 b = akc6955_readcmd(AKC6955_POWER);
292 akc6955_writecmd(AKC6955_POWER, b); // Falldown seek bit to '0'.
295 void akc6955_set_scanrate_fm(unsigned char rate)
298 c = akc6955_readcmd(AKC6955_SPACE);
299 c = (c & 0xcf) | ((rate & 0x03) << 4);
300 akc6955_writecmd(AKC6955_SPACE, c);
303 unsigned char akc6955_chk_donescan(void)
306 b.byte = akc6955_readcmd(AKC6955_RCH_HI);
313 unsigned int akc6955_get_channel(void)
317 l = akc6955_readcmd(AKC6955_RCH_LO) ;
318 h = akc6955_readcmd(AKC6955_RCH_HI) & 0x1f;
324 * Get AM/FM Frequency: ret = KHz at AM, 10KHz @FM.
326 unsigned int akc6955_get_freq(void)
333 f.byte = akc6955_readcmd(AKC6955_POWER);
334 i = akc6955_get_channel();
336 freq = (i * 5) / 2 + 3000; // freq' = 25*i[KHz] = (25 / 10) *i [10KHz]
338 b.byte = akc6955_readcmd(AKC6955_CH_HI);
339 akc6955_get_amband(band);
341 if((band == AKC6955_BAND_MW2) || (b.b5)){
348 void akc6955_set_freq(unsigned int freq)
354 unsigned int start, stop;
356 f.byte = akc6955_readcmd(AKC6955_POWER);
358 akc6955_get_fmband(band);
360 if(band == AKC6955_BAND_FMUSER){
361 start = fm_usrbands[fm_userbandnum].start * 32;
362 stop = fm_usrbands[fm_userbandnum].stop * 32;
364 start = fmbands[band].start;
365 stop = fmbands[band].end;
370 akc6955_get_amband(band);
371 // if(band >= AKC6955_BAND_AMEND) band = AKC6955_BAND_AMEND - 1;
372 if(band == AKC6955_BAND_AMUSER){
373 start = am_usrbands[am_userbandnum].start * 32;
374 stop = am_usrbands[am_userbandnum].stop * 32;
376 start = ambands[band].start;
377 stop = ambands[band].end;
379 mode3k.byte = akc6955_readcmd(AKC6955_CNR_AM);
380 if(band == AKC6955_BAND_MW2) {
381 ch = (freq / 9) * 3; // See datasheet.
382 } else if(band == AKC6955_BAND_MW3) {
384 } else if(mode3k.b7){
390 if(freq < start) freq = start;
391 if(freq >= stop) freq = stop - 1;
392 akc6955_set_tune(mode3k.b7, ch);
395 void akc6955_set_userband(unsigned char start, unsigned char stop, unsigned int ch, unsigned char mode3k)
399 f.byte = akc6955_readcmd(AKC6955_POWER);
400 akc6955_writecmd(AKC6955_UCH_ST, start);
401 akc6955_writecmd(AKC6955_UCH_EN, stop);
403 akc6955_set_fmband(AKC6955_BAND_FMUSER);
405 akc6955_set_amband(AKC6955_BAND_AMUSER);
407 akc6955_set_tune(mode3k, ch);
411 unsigned char akc6955_get_cnr(void)
415 f.byte = akc6955_readcmd(AKC6955_POWER);
417 b.byte = akc6955_readcmd(AKC6955_CNR_FM);
419 b.byte = akc6955_readcmd(AKC6955_CNR_AM);
425 int akc6955_read_level(void)
428 unsigned char rflevel, iflevel;
435 f.byte = akc6955_readcmd(AKC6955_POWER);
436 b = akc6955_readcmd(AKC6955_PGALEVEL);
437 rflevel = (b & 0xe0) >> 5;
438 iflevel = (b & 0x1c) >> 2;
439 totallevel = rflevel + iflevel;
441 rssi = (int)(akc6955_readcmd(AKC6955_RSSI) & 0x7f);
442 // totallevel = rssi + 6*totallevel;
443 level = (int)(totallevel * 6 + rssi);
445 level = 103 - level; // totallevel * 6
447 freq = akc6955_get_freq();
448 if(freq > 2560) { // ASSUME SW
450 } else { // ASSUME MW,LW
458 // Get diff. unit = 100Hz.
459 int akc6955_get_diff(void)
465 diff.byte = akc6955_readcmd(AKC6955_FDNUM);
468 n = -((int) diff.byte);
474 f.byte = akc6955_readcmd(AKC6955_POWER);
481 void akc6955_up_freq(unsigned int step)
486 freq = akc6955_get_channel();
488 mode3k.byte = akc6955_readcmd(AKC6955_CNR_AM);
489 akc6955_set_tune(mode3k.b7, freq);
493 void akc6955_down_freq(unsigned int step)
498 freq = akc6955_get_channel();
499 if(freq <= step) return;
501 mode3k.byte = akc6955_readcmd(AKC6955_CNR_AM);
502 akc6955_set_tune(mode3k.b7, freq);
505 void akc6955_setvolumemode(unsigned char flag)
508 c.byte = akc6955_readcmd(AKC6955_ENABLE);
513 akc6955_writecmd(AKC6955_ENABLE, c.byte);
516 unsigned char akc6955_getvolumemode(void)
519 c.byte = akc6955_readcmd(AKC6955_ENABLE);
526 void akc6955_setvolume(unsigned char level)
529 // c = akc6955_readcmd(AKC6955_VOLUME) & 0x03;
530 if(level > 63) level = 63;
531 akc6955_writecmd(AKC6955_VOLUME, ((akc6955_readcmd(AKC6955_VOLUME) & 0x03) | (level << 2)));
534 unsigned char akc6955_getvolume(void)
537 c = akc6955_readcmd(AKC6955_VOLUME) >> 2;
541 void akc6955_set_prevolume(unsigned char level)
544 c = akc6955_readcmd(AKC6955_PRE) & 0xf3;
545 c |= ((level & 0x03) << 2);
546 akc6955_writecmd(AKC6955_PRE, c);
549 unsigned char akc6955_get_prevolume(void)
552 c = akc6955_readcmd(AKC6955_PRE) & 0x0c;
558 void akc6955_setphase(unsigned char flag)
561 c.byte = akc6955_readcmd(AKC6955_VOLUME);
567 akc6955_writecmd(AKC6955_VOLUME, c.byte);
570 void akc6955_setline(unsigned char flag)
573 c.byte = akc6955_readcmd(AKC6955_VOLUME);
578 akc6955_writecmd(AKC6955_VOLUME, c.byte);
581 void akc6955_set_lowboost(unsigned char flag)
584 c.byte = akc6955_readcmd(AKC6955_STEREO);
589 akc6955_writecmd(AKC6955_STEREO, c.byte);
592 void akc6955_set_stereomode(unsigned char mode)
595 c = akc6955_readcmd(AKC6955_STEREO);
596 mode = (mode & 0x03) << 2;
597 c = (c & 0xf3) | mode;
598 akc6955_writecmd(AKC6955_STEREO, c);
601 unsigned char akc6955_get_stereo(void)
604 c = akc6955_readcmd(AKC6955_RCH_HI) & 0x80;
613 unsigned int akc6955_get_battery(void)
617 c = akc6955_readcmd(AKC6955_VBAT) & 0x3f;