-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcativity.c
More file actions
393 lines (328 loc) · 11.4 KB
/
cativity.c
File metadata and controls
393 lines (328 loc) · 11.4 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* CatIVity - CAT protocol (Icom CI-V) based remote VFO tuner
*
* Copyright (c) 2026 Helmut Sipos, YO6ASM <yo6asm@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdint.h>
// States of the indicator LED
#define LED_OFF 0
#define LED_ON 1
// UART default baudate
#define UART_BAUDRATE 19200
// CI-V protocol elements
#define CIV_PREAMBLE 0xFE
#define CIV_TRANSCEIVER_ADDRESS 0x70
#define CIV_CONTROLLER_ADDRESS 0xE0
#define CIV_END_OF_MESSAGE 0xFD
#define CIV_GET_ACTIVE_VFO_FREQUENCY 0x03
#define CIV_SET_ACTIVE_VFO_FREQUENCY 0x05
// Supported VFO frequency range 0.5 - 30.0 MHz
#define VFO_FRQUENCY_MIN 500000
#define VFO_FRQUENCY_MAX 30000000
// Pulses per rotation (x 2 as there is one on 'A' AND 'B')
#define ROTARY_ENCODER_PULSES_PER_ROTATION (600 * 2)
// Number of complete rotations per second. Determines the tuning steps.
// The faster the knob is spun, the lager the tuning steps.
volatile uint8_t gRotationsPerSecond = 0;
// RX buffer
volatile uint8_t gUartRXBuffer[64];
// Counter indicating how many bytes are in the RX buffer
volatile uint8_t gUartRXBytes = 0;
// The VFO frequency
volatile uint32_t gVFOFrequency = 0;
// Frequency increment/decrement step
volatile uint8_t gVFOFrequencyStep = 0;
// VFO synchronized with tranceiver indicator flag
volatile uint8_t gVFOFrequencyValid = 0;
// Number of pulses per time unit (1000ms) received from the rotary encoder
volatile uint16_t gEncoderPulses = 0;
// VFO frequency tuning increment/decrement steps
const uint8_t gTuningSteps[] = {1, 2, 3, 4, 5, 10};
#define TUNING_STEPS_IDX_MAX ((sizeof(gTuningSteps)/sizeof(gTuningSteps[0])) - 1)
// Keeps the time since last sync with the transceiver
volatile uint8_t gSecondsSinceLastSync = 0;
// External INT0 (PD2, rotary encoder 'A') interrupt service routine
ISR(INT0_vect)
{
gEncoderPulses++;
// Rotation RIGHT?
if (bit_is_set(PIND, PIND3)) {
gVFOFrequency += gVFOFrequencyStep;
}
}
// External INT1 (PD3, rotary encoder 'B') interrupt service routine
ISR(INT1_vect)
{
gEncoderPulses++;
// Rotation LEFT?
if (bit_is_set(PIND, PIND2)) {
gVFOFrequency -= gVFOFrequencyStep;
}
}
// UART RX interrupt service routine
ISR(USART_RX_vect)
{
// Copy the received byte inito the buffer if there is space left
// Increment the counter indicating how many bytes are in the buffer
if (gUartRXBytes < sizeof(gUartRXBuffer)) {
gUartRXBuffer[gUartRXBytes] = UDR0;
gUartRXBytes++;
}
}
// Timer1 overflow interrupt service routine
ISR(TIMER1_OVF_vect)
{
// Setup for new timer overflow interrupt after 1000ms
TCNT1H = 0x85;
TCNT1L = 0xEE;
gSecondsSinceLastSync++;
gRotationsPerSecond = gEncoderPulses / ROTARY_ENCODER_PULSES_PER_ROTATION;
gEncoderPulses = 0;
if (gRotationsPerSecond > TUNING_STEPS_IDX_MAX) {
gRotationsPerSecond = TUNING_STEPS_IDX_MAX;
}
gVFOFrequencyStep = gTuningSteps[gRotationsPerSecond];
}
// Set red LED to ON or OFF
void led_red(uint8_t state)
{
if (state == LED_ON) {
PORTD |= _BV(PORTD4);
}
else {
PORTD &= ~_BV(PORTD4);
}
}
// Send an array of bytes through the UART
void uart_send(const uint8_t *data, uint8_t length)
{
for (int i = 0; i < length; ++i) {
// Wait for empty transmit buffer
while (bit_is_clear(UCSR0A, UDRE0)) {
}
// Put tx data into the buffer. It will then be send by the hardware
UDR0 = data[i];
}
}
// Convert frequency Hz -> 5-byte BCD LSB-first
void frequency_to_bcd(uint32_t frequency, uint8_t *out)
{
uint8_t decimals[10];
// Get the decimal for each position
for (int i = 0; i < 10; ++i) {
decimals[i] = frequency % 10;
frequency /= 10;
}
// Pack the decimals into the byte array
out[0] = (decimals[1] << 4) | decimals[0];
out[1] = (decimals[3] << 4) | decimals[2];
out[2] = (decimals[5] << 4) | decimals[4];
out[3] = (decimals[7] << 4) | decimals[6];
out[4] = (decimals[9] << 4) | decimals[8];
}
// Convert 5-byte BCD LSB-first -> frequency Hz. Return 0 if invalid (any nibble >9)
uint32_t bcd_to_frequency(volatile const uint8_t *const data)
{
uint8_t decimals[10];
// Unpack the decimals from the byte array
decimals[0] = data[0] & 0x0F;
decimals[1] = (data[0] >> 4) & 0x0F;
decimals[2] = data[1] & 0x0F;
decimals[3] = (data[1] >> 4) & 0x0F;
decimals[4] = data[2] & 0x0F;
decimals[5] = (data[2] >> 4) & 0x0F;
decimals[6] = data[3] & 0x0F;
decimals[7] = (data[3] >> 4) & 0x0F;
decimals[8] = data[4] & 0x0F;
decimals[9] = (data[4] >> 4) & 0x0F;
// Check for plausible value
for (int i = 0; i < 10; ++i) {
if (decimals[i] > 9) {
return 0;
}
}
uint32_t frequency = 0;
uint32_t mul = 1;
// Assemble the frequency value from the decimals
for (int i = 0; i < 10; ++i) {
frequency += decimals[i] * mul;
mul *= 10;
}
return frequency;
}
// Retrieve the currently set VFO frequency from the transceiver
uint32_t get_current_vfo_frequency(void)
{
uint8_t timeout_counter = 0x00;
uint32_t frequency = 0x00;
// See CI-V protocol specification for message details
const uint8_t cmdGetActiveVFOFrequency[] = {
CIV_PREAMBLE,
CIV_PREAMBLE,
CIV_TRANSCEIVER_ADDRESS,
CIV_CONTROLLER_ADDRESS,
CIV_GET_ACTIVE_VFO_FREQUENCY,
CIV_END_OF_MESSAGE
};
// Request the current frequency from the transceiver
// Xiegu G90 is echoing back the received command and THEN sending the response
// therefore we need to wait for both messages.
gUartRXBytes = 0;
uart_send(cmdGetActiveVFOFrequency, sizeof(cmdGetActiveVFOFrequency) / sizeof(cmdGetActiveVFOFrequency[0]));
timeout_counter = 50;
while ((gUartRXBytes < 17) && --timeout_counter) {
_delay_ms(1);
}
if (timeout_counter == 0) {
// RX Timeout
led_red(LED_ON);
_delay_ms(1000);
led_red(LED_OFF);
_delay_ms(1000);
}
else {
// Check message for plausibility
if ( (gUartRXBuffer[6] == CIV_PREAMBLE)
&& (gUartRXBuffer[7] == CIV_PREAMBLE)
&& (gUartRXBuffer[8] == CIV_CONTROLLER_ADDRESS)
&& (gUartRXBuffer[9] == CIV_TRANSCEIVER_ADDRESS)
&& (gUartRXBuffer[10] == CIV_GET_ACTIVE_VFO_FREQUENCY)
&& (gUartRXBuffer[16] == CIV_END_OF_MESSAGE)
) {
// decode the frequency from the received response
frequency = bcd_to_frequency(&gUartRXBuffer[11]);
}
else {
// Malformed message
led_red(LED_ON);
_delay_ms(3000);
led_red(LED_OFF);
_delay_ms(3000);
}
}
return frequency;
}
// Initialize the MCU
void init_hardware(void)
{
// Initialize Ports
// Port B all inputs
DDRB = 0b00000000;
// Pulls-ups for all inputs
PORTB = 0b11111111;
// Port C all inputs
DDRC = 0b00000000;
// Pulls-ups for all inputs
PORTC = 0b11111111;
// Port D - all inputs except UART TX (PD1) and LED (PD4)
DDRD = 0b00010010;
// Enable internal pull-ups for all but UART TX and LED
PORTD = 0b11101101;
// Initialize external interrupts
// External interrupts 0,1: Select falling edge trigger
EICRA = 0b00001010;
// Enable External Interrupts
EIMSK = 0b00000011;
// Initialize UART
// Set format N81 and baud rate
UBRR0 = (uint8_t)((F_CPU / UART_BAUDRATE + 8) / 16 - 1);
// Enable receiver and transmitter as well as receive interrupt
UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);
// Initialize Timer1
TCCR1A = 0x00;
// 1:256 prescaler
TCCR1B = 0x04;
TCCR1C = 0x00;
// Enable overflow interrupt
TIMSK1 = 0x01;
// Timer overflows after 1000ms
TCNT1H = 0x85;
TCNT1L = 0xEE;
}
int main(void)
{
uint8_t timeout_counter = 0x00;
uint32_t last_vfo_frequency = 0x00;
// See CI-V protocol specification for message details
uint8_t cmdSetActiveVFOFrequency[] = {
CIV_PREAMBLE,
CIV_PREAMBLE,
CIV_TRANSCEIVER_ADDRESS,
CIV_CONTROLLER_ADDRESS,
CIV_SET_ACTIVE_VFO_FREQUENCY,
0x00, 0x00, 0x00, 0x00, 0x00, // Frequency -> BCD coded
CIV_END_OF_MESSAGE
};
// Disable interrupts while setting things up
cli();
// Initialize the microcontroller
init_hardware();
// Enable interrupts
sei();
// Forever
for (;;) {
// If not done so yet, retrieve the current frequency from the transceiver
if (!gVFOFrequencyValid) {
if (!((gVFOFrequency = get_current_vfo_frequency()))) {
continue;
}
gVFOFrequencyValid = 1;
}
// Clamp the frequency to the supported range
if (gVFOFrequency < VFO_FRQUENCY_MIN)
{
gVFOFrequency = VFO_FRQUENCY_MIN;
}
else if (gVFOFrequency > VFO_FRQUENCY_MAX)
{
gVFOFrequency = VFO_FRQUENCY_MAX;
}
// Do nothing while the frequency didn't change
if (gVFOFrequency == last_vfo_frequency) {
// In moments of inactivity sync every 3 seconds with the transceiver
// (in case the frequency on it was manually changed)
if (gSecondsSinceLastSync >= 3) {
gVFOFrequencyValid = 0;
}
continue;
}
// Reset - the frequency is set hence sync is done
gSecondsSinceLastSync = 0;
// Update last values
last_vfo_frequency = gVFOFrequency;
// Encode the frequency in BCD as per spec.
frequency_to_bcd(gVFOFrequency, &cmdSetActiveVFOFrequency[5]);
// Send the current frequency to the transceiver
// then read (and ignore) the response
gUartRXBytes = 0;
uart_send(cmdSetActiveVFOFrequency, sizeof(cmdSetActiveVFOFrequency) / sizeof(cmdSetActiveVFOFrequency[0]));
timeout_counter = 50;
while ((gUartRXBytes < 17) && --timeout_counter) {
_delay_ms(1);
}
}
}