1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
/*
* spreadspace avr utils
*
*
* Copyright (C) 2013 Christian Pointner <equinox@spreadspace.org>
* Othmar Gsenger <otti@wirdorange.org>
*
* This file is part of spreadspace avr utils.
*
* spreadspace avr utils is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* spreadspace avr utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with spreadspace avr utils. If not, see <http://www.gnu.org/licenses/>.
*/
#include "LUFA/Drivers/Peripheral/TWI.h"
#include <stdio.h>
#include "rda1846.h"
#include "rda1846_defines.h"
static uint8_t rda1846_write_register_raw(const uint8_t addr, const uint16_t data)
{
if(TWI_StartTransmission(RDA1846_CHIP_ADDR | TWI_ADDRESS_WRITE,10) != TWI_ERROR_NoError)
return 1;
if(!TWI_SendByte(addr | RDA1846_ADDR_W))
goto i2c_error;
if(!TWI_SendByte((uint8_t)(data>>8)))
goto i2c_error;
if(!TWI_SendByte((uint8_t)data))
goto i2c_error;
TWI_StopTransmission();
return 0;
i2c_error:
TWI_StopTransmission();
return 1;
}
static uint8_t rda1846_read_register_raw(const uint8_t addr, uint16_t* data)
{
if(TWI_StartTransmission(RDA1846_CHIP_ADDR | TWI_ADDRESS_WRITE,10) != TWI_ERROR_NoError)
return 1;
if(!TWI_SendByte(addr | RDA1846_ADDR_R))
goto i2c_error;
if(TWI_StartTransmission(RDA1846_CHIP_ADDR | TWI_ADDRESS_READ,10) != TWI_ERROR_NoError)
goto i2c_error;
uint8_t tmp;
if(!TWI_ReceiveByte(&tmp, 0))
goto i2c_error;
*data = tmp << 8;
if(!TWI_ReceiveByte(&tmp, 1))
goto i2c_error;
*data |= tmp;
TWI_StopTransmission();
return 0;
i2c_error:
TWI_StopTransmission();
return 1;
}
static uint8_t rda1846_write_register(const uint8_t addr, const uint16_t data)
{
if(addr < RDA1846_ADDR_LIMIT)
return rda1846_write_register_raw(addr, data);
if(addr > RDA1846_ADDR_LIMIT) {
uint8_t ret = rda1846_write_register_raw(RDA1846_ADDR_LIMIT, 1);
if(ret) return ret;
ret = rda1846_write_register_raw(addr, data);
if(ret) return ret;
return rda1846_write_register_raw(RDA1846_ADDR_LIMIT, 0);
}
return 1;
}
static uint8_t rda1846_read_register(const uint8_t addr, uint16_t* data)
{
if(addr < RDA1846_ADDR_LIMIT)
return rda1846_read_register_raw(addr, data);
if(addr > RDA1846_ADDR_LIMIT) {
uint8_t ret = rda1846_write_register_raw(RDA1846_ADDR_LIMIT, 1);
if(ret) return ret;
ret = rda1846_read_register_raw(addr - RDA1846_ADDR_LIMIT + 1, data);
if(ret) return ret;
return rda1846_write_register_raw(RDA1846_ADDR_LIMIT, 0);
}
return 1;
}
void rda1846_init(void)
{
TWI_Init(TWI_BIT_PRESCALE_1, TWI_BITLENGTH_FROM_FREQ(1, 200000));
// switch to 2m Band (134 ~ 174 MHz)
rda1846_write_register(RDA1846_REG_RF_BAND, RDA1846_RF_BAND_2M);
// set crystal frequency (12.288 MHz)
rda1846_write_register(RDA1846_REG_XTAL, RDA1846_XTAL_FREQ);
rda1846_write_register(RDA1846_REG_ADCLK, RDA1846_ADCLK_FREQ);
rda1846_write_register(RDA1846_REG_CLK_MODE, RDA1846_CLK_MODE);
rda1846_write_register(RDA1846_REG_CTL, RDA1846_CTL_CH_12K5 | RDA1846_CTL_RX_M_TX_M | RDA1846_CTL_RX);
rda1846_write_register(RDA1846_REG_INT, RDA1846_INT_DTMF_IDLE);
// use this if VOX, SQ, TXON_RF, RXON_RF are meant as outputs
rda1846_write_register(RDA1846_REG_GPIO, RDA1846_GPIO_7_VOX | RDA1846_GPIO_6_SQ |
RDA1846_GPIO_5_TXON_RF | RDA1846_GPIO_4_RXON_RF |
RDA1846_GPIO_2_INT);
// use this if VOX, SQ, TXON_RF, RXON_RF are meant as inputs
// rda1846_write_register(RDA1846_REG_GPIO, RDA1846_GPIO_2_INT);
rda1846_write_register(RDA1846_REG_TX_VOICE, RDA1846_TX_VOICE_NONE);
rda1846_write_register(RDA1846_REG_DTMF_CTL, RDA1846_DTMF_DUAL | RDA1846_DTMF_EN);
}
void rda1846_soft_reset(void)
{
rda1846_write_register(RDA1846_REG_CTL, RDA1846_CTL_SOFT_RST);
}
int16_t rda1846_get_rssi(void)
{
uint16_t data;
if(rda1846_read_register(RDA1846_REG_RSSI, &data))
return -1;
return (int16_t)data;
}
int16_t rda1846_get_vssi(void)
{
uint16_t data;
if(rda1846_read_register(RDA1846_REG_VSSI, &data))
return -1;
return (int16_t)data;
}
uint16_t rda1846_get_flags(void)
{
uint16_t data;
if(rda1846_read_register(RDA1846_REG_VSSI, &data))
return 0xFFFF;
return data;
}
uint8_t rda1846_get_dtmf(uint8_t* idx1, uint8_t* idx2, uint8_t* code)
{
uint16_t data;
if(rda1846_read_register(RDA1846_REG_DTMF_OUT, &data))
return 1;
if(idx1) *idx1 = (uint8_t)((data & 0x0380)>>7);
if(idx2) *idx2 = (uint8_t)((data & 0x0070)>>4);
if(code) *code = (uint8_t)(data & 0x000F);
return 0;
}
int32_t rda1846_get_freq_kHz(void)
{
int32_t freq;
uint16_t data;
uint8_t ret = rda1846_read_register(RDA1846_REG_FREQH, &data);
if(ret)
return -1;
freq = (((uint32_t)data)<<16);
ret = rda1846_read_register(RDA1846_REG_FREQL, &data);
if(ret)
return -1;
freq |= data;
return (freq>>3);
}
uint8_t rda1846_set_freq_kHz(int32_t freq)
{
if(freq < RDA1846_BAND_LOW || freq > RDA1846_BAND_HIGH)
return 1;
freq = freq<<3;
uint8_t ret = rda1846_write_register(RDA1846_REG_FREQH, (uint16_t)(freq>>16));
if(ret)
return 1;
return rda1846_write_register(RDA1846_REG_FREQL, (uint16_t)(freq & 0x0000FFFF));
}
|