Library for KT0915, DSP radio IC

Committer:
ritarosakai
Date:
Wed Jan 10 10:04:55 2018 +0000
Revision:
4:d3d45a8ac7a5
Parent:
3:a86c8f7a4e47
Fixed some comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ritarosakai 0:e5bccd46b6db 1 #include "mbed.h"
ritarosakai 0:e5bccd46b6db 2 #include "KT0915.h"
ritarosakai 0:e5bccd46b6db 3
ritarosakai 0:e5bccd46b6db 4 KT0915::KT0915(PinName sda,PinName scl):
ritarosakai 0:e5bccd46b6db 5 _KT0915(sda,scl)
ritarosakai 0:e5bccd46b6db 6 {
ritarosakai 0:e5bccd46b6db 7 _KT0915.frequency(100000);
ritarosakai 0:e5bccd46b6db 8 }
ritarosakai 0:e5bccd46b6db 9
ritarosakai 0:e5bccd46b6db 10 void KT0915::init(void)
ritarosakai 0:e5bccd46b6db 11 {
ritarosakai 0:e5bccd46b6db 12 write_reg(0x02, 0b00000000, 0b00000111);//FM space:100KHz, R/L mute:disable
ritarosakai 0:e5bccd46b6db 13 write_reg(0x04, 0b11100000, 0b10010000);//FM/AM/device mute:disable, bass:disable DAC cap:60uF
ritarosakai 0:e5bccd46b6db 14 write_reg(0x05, 0b10011000, 0b00100000);//Mono:enable, de-emphasis:50uF, blend:disable
ritarosakai 0:e5bccd46b6db 15 write_reg(0x0A, 0b00000100, 0b00000000);//LDO:highest, FM AFC:enable
ritarosakai 0:e5bccd46b6db 16 write_reg(0x0C, 0b00000000, 0b00101100);//FM wide freq:enable
ritarosakai 0:e5bccd46b6db 17 write_reg(0x0F, 0b10001000, 0b00000000);//Stanby:disable, volume:0
ritarosakai 2:108a1ae04859 18 read_reg(0x16,&data1,&data2);
ritarosakai 3:a86c8f7a4e47 19 write_reg(0x16,data1&0b000111111, 0b11000010);//mode:FM, use internal defined band, audio gain:0dB, AM AFC:enable
ritarosakai 0:e5bccd46b6db 20 write_reg(0x22, 0b10100010, 0b11101100);//AM AGC fast win:fastest, AM AGC short win:slowest
ritarosakai 0:e5bccd46b6db 21 //AM bandwidth:6KHz, AM gain:12dB, Left Inverse Control:enable
ritarosakai 0:e5bccd46b6db 22 read_reg(0x23, &data1, &data2);
ritarosakai 0:e5bccd46b6db 23 write_reg(0x23, (data1 | 0b00011100), data2);//low th:B
ritarosakai 0:e5bccd46b6db 24 write_reg(0x2E, 0b00101000, 0b10001100);//softmute:fast, AM softmute start level:0b100
ritarosakai 0:e5bccd46b6db 25 //softmute target volume:0b0100, softmute mode:RSSI, FM softmute start threshold:0b100
ritarosakai 0:e5bccd46b6db 26 write_reg(0x33, 0b01010100, 0b00000001);//AM space:9KHz
ritarosakai 0:e5bccd46b6db 27 read_reg(0x3F, &data1, &data2);
ritarosakai 0:e5bccd46b6db 28 write_reg(0x3F, data1, ((data2 & 0b10001000) | 0b00010011));//RF AGC patch
ritarosakai 0:e5bccd46b6db 29 }
ritarosakai 0:e5bccd46b6db 30
ritarosakai 0:e5bccd46b6db 31 int KT0915::getID(void)
ritarosakai 0:e5bccd46b6db 32 {
ritarosakai 0:e5bccd46b6db 33 read_reg(0x01,&data1,&data2);
ritarosakai 0:e5bccd46b6db 34 return (data1<<8)+data2;
ritarosakai 0:e5bccd46b6db 35 }
ritarosakai 0:e5bccd46b6db 36
ritarosakai 2:108a1ae04859 37 void KT0915::set_xtal(int xtal)
ritarosakai 2:108a1ae04859 38 {
ritarosakai 2:108a1ae04859 39 read_reg(0x16,&data1,&data2);
ritarosakai 2:108a1ae04859 40 if(xtal==32) {
ritarosakai 2:108a1ae04859 41 write_reg(0x16,data1&0b11100000,data2);
ritarosakai 2:108a1ae04859 42 } else if(xtal==38) {
ritarosakai 2:108a1ae04859 43 write_reg(0x16,(data1&0b11100000)|0b00001001,data2);
ritarosakai 2:108a1ae04859 44 } else if(xtal==12000) {
ritarosakai 2:108a1ae04859 45 write_reg(0x16,(data1&0b11100000)|0b00010011,data2);
ritarosakai 2:108a1ae04859 46 } else if(xtal==24000) {
ritarosakai 2:108a1ae04859 47 write_reg(0x16,(data1&0b11100000)|0b00010111,data2);
ritarosakai 2:108a1ae04859 48 }
ritarosakai 2:108a1ae04859 49 }
ritarosakai 2:108a1ae04859 50
ritarosakai 2:108a1ae04859 51 void KT0915::set_space(bool mode,int space)
ritarosakai 2:108a1ae04859 52 {
ritarosakai 2:108a1ae04859 53 if(mode==0) {
ritarosakai 2:108a1ae04859 54 read_reg(0x33,&data1,&data2);
ritarosakai 2:108a1ae04859 55 read_reg(0x22,&data1,&data2);
ritarosakai 2:108a1ae04859 56 if(space==1) {
ritarosakai 2:108a1ae04859 57 write_reg(0x33,data1&0b00111111,data2);
ritarosakai 2:108a1ae04859 58 write_reg(0x22,data1,data2&0b00111111);
ritarosakai 2:108a1ae04859 59 } else if(space==9) {
ritarosakai 2:108a1ae04859 60 write_reg(0x33,(data1&0b00111111)|0b01000000,data2);
ritarosakai 2:108a1ae04859 61 write_reg(0x22,data1,data2|0b11000000);
ritarosakai 2:108a1ae04859 62 } else if(space==10) {
ritarosakai 2:108a1ae04859 63 write_reg(0x33,data1|0b11000000,data2);
ritarosakai 2:108a1ae04859 64 write_reg(0x22,data1,data2|0b11000000);
ritarosakai 2:108a1ae04859 65 }
ritarosakai 2:108a1ae04859 66 } else {
ritarosakai 2:108a1ae04859 67 read_reg(0x02,&data1,&data2);
ritarosakai 2:108a1ae04859 68 if(space==50) {
ritarosakai 2:108a1ae04859 69 write_reg(0x02,data1,(data2&0b11110011)|0b00001000);
ritarosakai 2:108a1ae04859 70 } else if(space==100) {
ritarosakai 2:108a1ae04859 71 write_reg(0x02,data1,(data2&0b11110011)|0b00000100);
ritarosakai 2:108a1ae04859 72 } else if(space==200) {
ritarosakai 2:108a1ae04859 73 write_reg(0x02,data1,data2&0b11110011);
ritarosakai 2:108a1ae04859 74 }
ritarosakai 2:108a1ae04859 75 }
ritarosakai 2:108a1ae04859 76 }
ritarosakai 2:108a1ae04859 77
ritarosakai 0:e5bccd46b6db 78 void KT0915::set_vol(int vol)
ritarosakai 0:e5bccd46b6db 79 {
ritarosakai 0:e5bccd46b6db 80 read_reg(0x0F, &data1, &data2);
ritarosakai 0:e5bccd46b6db 81 if ((data2 & 0b00011111) != vol) {
ritarosakai 0:e5bccd46b6db 82 write_reg(0x0F, data1, (data2 >> 5) * 0b100000 + vol);
ritarosakai 0:e5bccd46b6db 83 }
ritarosakai 0:e5bccd46b6db 84 }
ritarosakai 0:e5bccd46b6db 85
ritarosakai 0:e5bccd46b6db 86 void KT0915::set_freq(bool mode,int freq)
ritarosakai 0:e5bccd46b6db 87 {
ritarosakai 0:e5bccd46b6db 88 int f_upper, f_lower, freq2;
ritarosakai 2:108a1ae04859 89 read_reg(0x16,&data1,&data2);
ritarosakai 0:e5bccd46b6db 90 if (mode == 0) {
ritarosakai 2:108a1ae04859 91 write_reg(0x16, data1|0b10000000,data2);
ritarosakai 0:e5bccd46b6db 92 f_upper = (freq >> 8 | 0b10000000);
ritarosakai 0:e5bccd46b6db 93 f_lower = freq & 0b11111111;
ritarosakai 0:e5bccd46b6db 94 write_reg(0x17, f_upper, f_lower);
ritarosakai 0:e5bccd46b6db 95 }
ritarosakai 0:e5bccd46b6db 96 if (mode == 1) {
ritarosakai 2:108a1ae04859 97 write_reg(0x16, data1&0b01111111,data2);
ritarosakai 0:e5bccd46b6db 98 freq2 = freq / 5;
ritarosakai 0:e5bccd46b6db 99 f_upper = (freq2 >> 8 | 0b10000000);
ritarosakai 0:e5bccd46b6db 100 f_lower = freq2 & 0b11111111;
ritarosakai 0:e5bccd46b6db 101 write_reg(0x03, f_upper, f_lower);
ritarosakai 0:e5bccd46b6db 102 }
ritarosakai 0:e5bccd46b6db 103 }
ritarosakai 0:e5bccd46b6db 104
ritarosakai 0:e5bccd46b6db 105 int KT0915::get_rssi(bool mode)
ritarosakai 0:e5bccd46b6db 106 {
ritarosakai 2:108a1ae04859 107 int rssi;
ritarosakai 0:e5bccd46b6db 108 if (mode == 0) {
ritarosakai 0:e5bccd46b6db 109 read_reg(0x24, &data1, &data2);
ritarosakai 0:e5bccd46b6db 110 rssi = -90 + ( data1 & 0b00011111) * 3;
ritarosakai 0:e5bccd46b6db 111 }
ritarosakai 0:e5bccd46b6db 112 if (mode == 1) {
ritarosakai 0:e5bccd46b6db 113 read_reg(0x12, &data1, &data2);
ritarosakai 0:e5bccd46b6db 114 rssi = -100 + (data2 >> 3) * 3;
ritarosakai 0:e5bccd46b6db 115 }
ritarosakai 0:e5bccd46b6db 116 return rssi;
ritarosakai 0:e5bccd46b6db 117 }
ritarosakai 0:e5bccd46b6db 118
ritarosakai 0:e5bccd46b6db 119 int KT0915::get_snr(bool mode)
ritarosakai 0:e5bccd46b6db 120 {
ritarosakai 2:108a1ae04859 121 int snr;
ritarosakai 0:e5bccd46b6db 122 if (mode == 1) {
ritarosakai 0:e5bccd46b6db 123 read_reg(0x14, &data1, &data2);
ritarosakai 0:e5bccd46b6db 124 snr = (data1 & 0b00011111) * 0b100 + (data2 >> 6);
ritarosakai 0:e5bccd46b6db 125 }
ritarosakai 0:e5bccd46b6db 126 return snr;
ritarosakai 0:e5bccd46b6db 127 }
ritarosakai 0:e5bccd46b6db 128
ritarosakai 0:e5bccd46b6db 129 bool KT0915::get_ready(void)
ritarosakai 0:e5bccd46b6db 130 {
ritarosakai 0:e5bccd46b6db 131 read_reg(0x14,&data1,&data2);
ritarosakai 0:e5bccd46b6db 132 return (data1&0b00100000)>>5;
ritarosakai 0:e5bccd46b6db 133 }
ritarosakai 0:e5bccd46b6db 134
ritarosakai 2:108a1ae04859 135 void KT0915::cali(void) //re_cali
ritarosakai 0:e5bccd46b6db 136 {
ritarosakai 0:e5bccd46b6db 137 write_reg(0x10,0b01100000,0b00000000);
ritarosakai 0:e5bccd46b6db 138 }
ritarosakai 0:e5bccd46b6db 139
ritarosakai 0:e5bccd46b6db 140
ritarosakai 2:108a1ae04859 141 void KT0915::write_reg(int memory_address,int byte1,int byte2)
ritarosakai 0:e5bccd46b6db 142 {
ritarosakai 2:108a1ae04859 143 char cmd[3]= {memory_address,byte1,byte2};
ritarosakai 0:e5bccd46b6db 144 _KT0915.write(KT0915_address,cmd,3);
ritarosakai 0:e5bccd46b6db 145 }
ritarosakai 0:e5bccd46b6db 146
ritarosakai 2:108a1ae04859 147 void KT0915::read_reg(int memory_address,int *byte1,int *byte2)
ritarosakai 0:e5bccd46b6db 148 {
ritarosakai 0:e5bccd46b6db 149 char cmd[2];
ritarosakai 0:e5bccd46b6db 150 cmd[0]=memory_address;
ritarosakai 0:e5bccd46b6db 151 _KT0915.write(KT0915_address,cmd,1,true);
ritarosakai 0:e5bccd46b6db 152 _KT0915.read(KT0915_address|1,cmd,2);
ritarosakai 2:108a1ae04859 153 *byte1=cmd[0];
ritarosakai 2:108a1ae04859 154 *byte2=cmd[1];
ritarosakai 0:e5bccd46b6db 155 }