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.
40 #include "commondef.h"
42 void akc6955_writecmd(unsigned char reg, unsigned char data)
46 i2c_open(I2C_MASTER, I2C_SLEW_OFF, 19);
59 OpenI2C(MASTER, SLEW_OFF);
75 unsigned char akc6955_readcmd(unsigned char reg)
80 i2c_open(I2C_MASTER, I2C_SLEW_OFF, 19);
82 OpenI2C(MASTER, SLEW_OFF);
124 void akc6955_chg_fm(unsigned char f)
127 b.byte = akc6955_readcmd(AKC6955_POWER);
134 akc6955_writecmd(AKC6955_POWER, b.byte);
137 unsigned char akc6955_get_fm(void)
140 b.byte = akc6955_readcmd(AKC6955_POWER);
148 void akc6955_set_amband(unsigned char band)
151 b = akc6955_readcmd(AKC6955_BAND);
152 b &= 0x07; // extract FM
153 b = b | ((band & 0x1f) << 3);
154 akc6955_writecmd(AKC6955_BAND, b);
157 void akc6955_set_fmband(unsigned char band)
160 b = akc6955_readcmd(AKC6955_BAND);
161 b &= 0xf8; // extract AM
162 b = b | (band & 0x07);
163 akc6955_writecmd(AKC6955_BAND, b);
166 unsigned char akc6955_get_amband(void)
169 b = akc6955_readcmd(AKC6955_BAND);
174 unsigned char akc6955_get_fmband(void)
177 b = akc6955_readcmd(AKC6955_BAND);
183 void akc6955_set_power(unsigned char on)
186 b.byte = akc6955_readcmd(AKC6955_POWER);
193 akc6955_writecmd(AKC6955_POWER, b.byte);
197 void akc6955_do_tune(void)
200 b.byte = akc6955_readcmd(AKC6955_POWER);
201 b.b5 = 0; // Tun = '0'
202 akc6955_writecmd(AKC6955_POWER, b.byte);
204 b.b5= 1; // Tun = '1'
205 akc6955_writecmd(AKC6955_POWER, b.byte);
208 unsigned char akc6955_tune(void)
211 b.byte = akc6955_readcmd(AKC6955_RCH_HI);
219 unsigned int akc6955_mode3k(unsigned char flag)
222 b.byte = akc6955_readcmd(AKC6955_CH_HI);
228 akc6955_writecmd(AKC6955_CH_HI, b.byte);
230 _AKC6955_WAIT_62_5MS();
231 return akc6955_get_freq();
234 void akc6955_set_tune(unsigned char mode_3k, unsigned int ch)
241 f.byte = akc6955_readcmd(AKC6955_POWER);
246 band = akc6955_get_amband();
251 i = ch / 3; // On MW2, Channnel must be multiple of 3.
252 i = i * 3; // i = (i/3) * 3
254 if(i > 0x1fff) i = 0x1fff;
260 b.b5 = 1; // Mode 3K ON
262 akc6955_writecmd(AKC6955_CH_HI, b.byte);
265 akc6955_writecmd(AKC6955_CH_LO, b.byte);
269 void akc6955_do_scan(unsigned char up)
273 b.byte = akc6955_readcmd(AKC6955_POWER);
279 akc6955_writecmd(AKC6955_POWER, b.byte); // Falldown seek bit to '0'.
281 akc6955_writecmd(AKC6955_POWER, b.byte); // Raise seek bit to '1'.
284 void akc6955_abort_scan(void)
287 b = akc6955_readcmd(AKC6955_POWER);
289 akc6955_writecmd(AKC6955_POWER, b); // Falldown seek bit to '0'.
292 void akc6955_set_scanrate_fm(unsigned char rate)
295 c = akc6955_readcmd(AKC6955_SPACE);
296 c = (c & 0xcf) | ((rate & 0x03) << 4);
297 akc6955_writecmd(AKC6955_SPACE, c);
300 unsigned char akc6955_chk_donescan(void)
303 b = akc6955_readcmd(AKC6955_RCH_HI) & 0x40;
309 * Get AM/FM Frequency: ret = KHz at AM, 10KHz @FM.
311 unsigned int akc6955_get_freq(void)
318 // b = akc6955_readcmd(AKC6955_CNR_AM) & 0x80;
319 l = akc6955_readcmd(AKC6955_RCH_LO) ;
320 h = akc6955_readcmd(AKC6955_RCH_HI) & 0x1f;
321 f.byte = akc6955_readcmd(AKC6955_POWER);
324 freq = (i * 5) / 2 + 3000; // freq' = 25*i[KHz] = (25 / 10) *i [10KHz]
326 // freq = freq' + 30MHz = freq' + 3000[10KHz]
328 b.byte = akc6955_readcmd(AKC6955_CH_HI);
330 freq = i * 3; // freq = 5*i[KHz] = (4+1)*i[KHz]
332 freq = i * 5; // freq = 3i[KHz] = (2+1)i[KHz]
338 void akc6955_set_freq(unsigned int freq)
345 f.byte = akc6955_readcmd(AKC6955_POWER);
347 band = akc6955_get_fmband();
348 if(freq < fmbands[band].start) freq = fmbands[band].start;
349 if(freq >= fmbands[band].end) freq = fmbands[band].end - 1;
353 band = akc6955_get_amband();
354 if(band >= AKC6955_BAND_AMEND) band = AKC6955_BAND_AMEND - 1;
355 if(freq < ambands[band].start) freq = ambands[band].start;
356 if(freq >= ambands[band].end) freq = ambands[band].end - 1;
357 mode3k.byte = akc6955_readcmd(AKC6955_CNR_AM);
364 akc6955_set_tune(mode3k.byte, ch);
367 void akc6955_set_userband(unsigned char start, unsigned char stop, unsigned int ch, unsigned char mode3k)
371 f.byte = akc6955_readcmd(AKC6955_POWER);
372 akc6955_writecmd(AKC6955_UCH_ST, start);
373 akc6955_writecmd(AKC6955_UCH_EN, stop);
375 akc6955_set_fmband(AKC6955_BAND_FMUSER);
377 akc6955_set_amband(AKC6955_BAND_AMUSER);
379 akc6955_set_tune(mode3k, ch);
383 unsigned char akc6955_get_cnr(void)
387 f.byte = akc6955_readcmd(AKC6955_POWER);
389 b.byte = akc6955_readcmd(AKC6955_CNR_FM);
391 b.byte = akc6955_readcmd(AKC6955_CNR_AM);
397 int akc6955_read_level(void)
400 unsigned char rflevel, iflevel;
407 f.byte = akc6955_readcmd(AKC6955_POWER);
408 b = akc6955_readcmd(AKC6955_PGALEVEL);
409 rflevel = (b & 0xe0) >> 5;
410 iflevel = (b & 0x1c) >> 2;
411 totallevel = rflevel + iflevel;
413 rssi = (int)(akc6955_readcmd(AKC6955_RSSI) & 0x7f);
414 // totallevel = rssi + 6*totallevel;
415 level = (int)(totallevel * 6 + rssi);
417 level = 103 - level; // totallevel * 6
419 freq = akc6955_get_freq();
420 if(freq > 2560) { // ASSUME SW
422 } else { // ASSUME MW,LW
429 // Get diff. unit = 100Hz.
430 int akc6955_get_diff(void)
436 diff.byte = akc6955_readcmd(AKC6955_FDNUM);
439 n = -((int) diff.byte);
445 f.byte = akc6955_readcmd(AKC6955_POWER);
452 unsigned int akc6955_up_freq(unsigned int step)
458 f.byte = akc6955_readcmd(AKC6955_POWER);
459 freq = akc6955_get_freq();
462 band = akc6955_get_fmband();
463 if(band >= AKC6955_BAND_FMEND) band = AKC6955_BAND_FMEND - 1;
464 if(freq >= fmbands[band].end) freq = fmbands[band].end - 1;
466 band = akc6955_get_amband();
467 if(band >= AKC6955_BAND_AMEND) band = AKC6955_BAND_AMEND - 1;
468 if(freq >= ambands[band].end) freq = ambands[band].end - 1;
470 akc6955_set_freq(freq);
471 return akc6955_get_freq();
475 unsigned int akc6955_down_freq(unsigned int step)
481 freq = akc6955_get_freq();
482 if(freq <= step) return freq;
485 f.byte = akc6955_readcmd(AKC6955_POWER);
487 band = akc6955_get_amband();
488 if(band >= AKC6955_BAND_AMEND) band = AKC6955_BAND_AMEND - 1;
489 if(freq < ambands[band].start) freq = ambands[band].start;
491 band = akc6955_get_fmband();
492 if(band >= AKC6955_BAND_FMEND) band = AKC6955_BAND_FMEND - 1;
493 if(freq < fmbands[band].start) freq = fmbands[band].start;
495 akc6955_set_freq(freq);
496 return akc6955_get_freq();
498 void akc6955_setvolumemode(unsigned char flag)
501 c.byte = akc6955_readcmd(AKC6955_ENABLE);
506 akc6955_writecmd(AKC6955_ENABLE, c.byte);
509 unsigned char akc6955_getvolumemode(void)
512 c.byte = akc6955_readcmd(AKC6955_ENABLE);
519 void akc6955_setvolume(unsigned char level)
522 // c = akc6955_readcmd(AKC6955_VOLUME) & 0x03;
523 if(level > 63) level = 63;
524 akc6955_writecmd(AKC6955_VOLUME, ((akc6955_readcmd(AKC6955_VOLUME) & 0x03) | (level << 2)));
527 unsigned char akc6955_getvolume(void)
530 c = (akc6955_readcmd(AKC6955_VOLUME) & 0xfc) >> 2;
534 void akc6955_set_prevolume(unsigned char level)
537 c = akc6955_readcmd(AKC6955_PRE) & 0xf3;
538 c |= ((level & 0x03) << 2);
539 akc6955_writecmd(AKC6955_PRE, c);
542 unsigned char akc6955_get_prevolume(void)
545 c = akc6955_readcmd(AKC6955_PRE) & 0x0c;
551 void akc6955_setphase(unsigned char flag)
554 c.byte = akc6955_readcmd(AKC6955_VOLUME);
560 akc6955_writecmd(AKC6955_VOLUME, c.byte);
563 void akc6955_setline(unsigned char flag)
566 c.byte = akc6955_readcmd(AKC6955_VOLUME);
571 akc6955_writecmd(AKC6955_VOLUME, c.byte);
574 void akc6955_set_lowboost(unsigned char flag)
577 c.byte = akc6955_readcmd(AKC6955_STEREO);
582 akc6955_writecmd(AKC6955_STEREO, c.byte);
585 void akc6955_set_stereomode(unsigned char mode)
588 c = akc6955_readcmd(AKC6955_STEREO);
589 mode = (mode & 0x03) << 2;
590 c = (c & 0xf3) | mode;
591 akc6955_writecmd(AKC6955_STEREO, c);
594 unsigned char akc6955_get_stereo(void)
597 c = akc6955_readcmd(AKC6955_RCH_HI) & 0x80;
606 unsigned int akc6955_get_battery(void)
610 c = akc6955_readcmd(AKC6955_VBAT) & 0x3f;