#include "main.h"
//读芯片寄存器
u16 OperationRTC6222_read(u8 reg_offset)
{
u16 reg_value = 0;
fm_i2c_start();
fm_i2c_send_byte(0xc8);
fm_i2c_recv_ack();
fm_i2c_send_byte(reg_offset);
fm_i2c_recv_ack();
fm_i2c_stop();
delay_ms(10);//RTC6222_Delay(10);
fm_i2c_start();
fm_i2c_send_byte(0xc9);
fm_i2c_recv_ack();
reg_value = fm_i2c_read_byte();
fm_i2c_send_ack();
reg_value = reg_value<<8;
reg_value += fm_i2c_read_byte();
fm_i2c_send_nack();
delay_ms(10);//RTC6222_Delay(10);
fm_i2c_stop();
return reg_value;
}
//写芯片寄存器
void OperationRTC6222_write(u16 in_data, u8 reg_offset)
{
fm_i2c_start();
fm_i2c_send_byte(0xc8);
fm_i2c_recv_ack();
fm_i2c_send_byte(reg_offset);
fm_i2c_recv_ack();
fm_i2c_send_byte((u8)(in_data >> 8));
fm_i2c_recv_ack();
fm_i2c_send_byte((u8)in_data);
fm_i2c_recv_ack();
fm_i2c_stop();
}
u8 RTC6222_online(void)
{
u8 i;
for(i=0;i<3;i++) {
if(0x1688 == OperationRTC6222_read(0)) {
return 1;
}
}
return 0;
}
void RTC6222_init_(void)
{
OperationRTC6222_write(0x16AA,0x00); //Power off
delay_ms(50);
OperationRTC6222_write(0x96AA,0x00);//Power on
delay_ms(100);
OperationRTC6222_write(0x4000,0x02);//vol = 0a ,disable mute
OperationRTC6222_write(0x1900,0x04);//enable rclk osc
OperationRTC6222_write(0x656f,0x05);//set seek noise th and spike th ,0x01h=0010bin,00=80(noise_th),01=144(spike_th),and rssi th=0x0c.
OperationRTC6222_write(0xB000,0x06);//bit15:ENABLE IC ,bit12:enable check same channal, bit:2-0set rlck choice one from 32k~38.4M
OperationRTC6222_write(0xB400,0x07); //正弦波
OperationRTC6222_write(0x043C,0x08);//set seek band of freq start_freq0x043C=1084. x100khz
OperationRTC6222_write(0x0280,0x09);//set seek start_freq 0x035C=860, x100khz
OperationRTC6222_write(0x0B10,0x11);//rclk force 0x0Bh= bin1011
OperationRTC6222_write(0x1108,0x20);//1008 hopping open is better than 1018 nohopping
delay_ms(100);
// OperationRTC6222_write((OperationRTC6222_read(0X06)|0X8000),0X06);
// delay_ms(100);
//print_all_reg();
}
void RTC6222_init(void)
{
OperationRTC6222_write(0x16AA,0x00); //Power off
delay_ms(50);
OperationRTC6222_write(0x96AA,0x00);//Power on
delay_ms(100);
OperationRTC6222_write(0x4000,0x02);//vol = 0a ,disable mute
OperationRTC6222_write(0x1900,0x04);//enable rclk osc
OperationRTC6222_write(0x656f,0x05);//set seek noise th and spike th ,0x01h=0010bin,00=80(noise_th),01=144(spike_th),and rssi th=0x0c.
OperationRTC6222_write(0xB000,0x06);//bit15:ENABLE IC ,bit12:enable check same channal, bit:2-0set rlck choice one from 32k~38.4M
// OperationRTC6222_write(0xB400,0x07); //正弦波
OperationRTC6222_write(0x043C,0x08);//set seek band of freq start_freq0x043C=1084. x100khz
OperationRTC6222_write(0x0280,0x09);//set seek start_freq 0x035C=860, x100khz
OperationRTC6222_write(0x0B10,0x11);//rclk force 0x0Bh= bin1011
OperationRTC6222_write(0x1108,0x20);//1008 hopping open is better than 1018 nohopping
delay_ms(100);
// OperationRTC6222_write((OperationRTC6222_read(0X06)|0X8000),0X06);
// delay_ms(100);
//print_all_reg();
}
void RTC6222_off(void)
{
OperationRTC6222_write(0x16AA,0x00);
delay_ms(50);
}
void RTC6222_set_vol(u8 FM_Volumn)
{
u16 write_byte=0;
if(FM_Volumn > 15) {
FM_Volumn = 15;
}
write_byte = 0x4000|(FM_Volumn&0x0f);
OperationRTC6222_write(write_byte,0x02);
//printf("RTC6222_set_vol vol=%d,0x%04x reg[2]=0x%04x\n",FM_Volumn,write_byte,OperationRTC6222_read(0x02));
}
void RTC6222_mute(bool is_mute)
{
u16 write_byte;
write_byte = OperationRTC6222_read(0x02);
if (is_mute) {
write_byte &= ~0x4000;
}
else {
write_byte |= 0x4000;
}
OperationRTC6222_write(write_byte,0x02);
//printf("RTC6222_mute is_mute = %d, reg[2]=0x%04x\n",is_mute,OperationRTC6222_read(0x02));
}
u8 RTC6222_set_freq(u16 channel_freq)
{
u16 write_byte;
u8 timeout = 0;
u16 Seek_Status = 0;
u16 test=0;
#ifndef __RTCFM_STEP_50K__
channel_freq *= 10;
#endif
//printf("==RTC6222_set_freq: %d\n",channel_freq);
OperationRTC6222_write(0x8000|channel_freq,0x03);
//New Algorithm part 1=on 0=off by Danny 20150724
do{
delay_ms(40); // 8 = 40ms(Default) 2 = 10ms(Speed limit) by Danny 20150724
timeout++;
Seek_Status = ((0xFC00 & OperationRTC6222_read(0x0B)) >> 10); //add by Danny 20150702
//WATCHDOG_CLR();
test=OperationRTC6222_read(0x0A);
}while ((Seek_Status == 0) && ((0x4000 & OperationRTC6222_read(0x0A))== 0) && (timeout < 200));
//printf("Seek_Status_STD1= 0x%02X ",Seek_Status);
Seek_Status = ((0xFC00 & OperationRTC6222_read(0x0B)) >> 10); //add by Danny 20150702
//printf("Seek_Status1= 0x%04X ",Seek_Status);
write_byte = channel_freq & (~0x8000);
OperationRTC6222_write(write_byte,0x03);
while(0x4000 & OperationRTC6222_read(0x0A));
Seek_Status = ((0xFC00 & OperationRTC6222_read(0x0B)) >> 10); //add by Danny 20150702
//printf("Seek_Status2= 0x%04X\n ",Seek_Status);
if(Seek_Status == 0) { //When Get Seek_Satus BK0_B[15:10] all 6b' = 0, Return 1. add by Danny 20150703
return 1;
}
else {
return 0;
}
}
u8 RTC6222_seek_hw(u16 channel_freq)
{
u16 write_byte;
u16 read_buf;
u8 return_value = 0;
#ifndef __RTCFM_STEP_50K__
channel_freq *= 10;
#endif
write_byte = OperationRTC6222_read(0x05);
write_byte &= (~0x8000);
OperationRTC6222_write(write_byte,0x05);
write_byte |= 0x8000;
OperationRTC6222_write(write_byte,0x05);
do {
read_buf = OperationRTC6222_read(0x0A);
delay_ms(45); //time out determined by reg0x04_9:8 seek time 0:40ms 1:160 ms
//WATCHDOG_CLR();
}
while(!(0x4000&read_buf));
if((0x6000&read_buf) == 0x6000) {//must judge at here and not move it at end of fuction
return_value = 0; //reach band top limited, all transport 0xff because app level has handled top limited freq like 110Mhz
}
else {
//printf("\t%5d\t%d\t", 0x7fff&OperationRTC6222_read(0x0C),0x00ff&OperationRTC6222_read(0x0B));
return_value = 1; //tell app lever that need to read cur_freq from driver lever.
}
write_byte &= (~0x8000);
OperationRTC6222_write(write_byte,0x05);
while(0x4000&OperationRTC6222_read(0x0A));
return return_value;
}
u8 RTC6222_seek(u16 channel_freq)
{
#if FM_SEEK_HW
return RTC6222_seek_hw(channel_freq);
#else
u8 Status0 = 0;
#ifdef __RTCFM_STEP_50K_CHOOSE_BEST_FROM_TW0__
u16 IF_Shift_1 = 0, IF_Shift_2 = 0;
u8 Status1 =0;
u8 shiftindex=0; //请看函数头的返回值说明,0表示当前上层传给底层的頻点,1表示上层传给底层的頻点再+50k。在搜到台时来告诉上层存台和放音选择正确的頻点。
#endif
Status0 = RTC6222_set_freq(channel_freq); // Check Seek_States is 0 ornot by Danny 20150702
//delay_5ms(20);
if(Status0)
{//Get one ture Channnel
#ifdef __RTCFM_STEP_50K_CHOOSE_BEST_FROM_TW0__
if((0x0040 & OperationRTC6222_read(0x0E)) != 0)
{
IF_Shift_1 = (~(0x003F & OperationRTC6222_read(0x0E))+1) &0x003F; //2's complement
}
else {
IF_Shift_1 = ((0x007F & OperationRTC6222_read(0x0E))); //2's complement
}
Status1 = RTC6222_set_freq(channel_freq+5);
if(Status1)
{
if((0x0040 & OperationRTC6222_read(0x0E)) != 0)
{
IF_Shift_2 = (~(0x003F & OperationRTC6222_read(0x0E))+1) &0x003F; //2's complement
}
else {
IF_Shift_2 = ((0x007F & OperationRTC6222_read(0x0E))); //2's complement
}
if(IF_Shift_1 < IF_Shift_2) {
RTC6222_set_freq(channel_freq);
shiftindex = 0;
}
else {
RTC6222_set_freq(channel_freq+5);
shiftindex = 1;
}
}
else {
RTC6222_set_freq(channel_freq); //Back Channel 50KHz for Return 1
}
RSSIValue = (0x00FF & (OperationRTC6222_read(0x0B)));
Reg_C = (OperationRTC6222_read(0x0C));
//printf("freq= %d RSSI= %02d IF_Shift_1= %02d IF_Shift_2= %02d shiftindex= %d Return %d \n",Reg_C,RSSIValue,IF_Shift_1,IF_Shift_2,shiftindex,1+shiftindex);
return (1+shiftindex);
#else
return 1;
#endif
}
else {
return 0;
}
#endif
}
u16 RTC6222_read_cur_seeking_freq(void)
{
#ifdef __RTCFM_STEP_50K__
return (0x7fff&OperationRTC6222_read(0x0C));
#else
return (0x7fff&OperationRTC6222_read(0x0C)) / 10;
#endif
}