forked from ps2/subg_rfspy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.c
224 lines (207 loc) · 4.14 KB
/
commands.c
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
#include <stdint.h>
#include "hardware.h"
#include "serial.h"
#include "radio.h"
#include "commands.h"
uint8_t interrupting_cmd = 0;
typedef void (*CommandHandler)();
CommandHandler handlers[] = {
/* 0 */ 0,
/* 1 */ cmd_get_state,
/* 2 */ cmd_get_version,
/* 3 */ cmd_get_packet,
/* 4 */ cmd_send_packet,
/* 5 */ cmd_send_and_listen,
/* 6 */ cmd_update_register,
/* 7 */ cmd_reset
};
void cmd_get_packet() {
uint8_t channel;
uint32_t timeout_ms;
uint8_t result;
channel = serial_rx_byte();
timeout_ms = serial_rx_long();
result = get_packet_and_write_to_serial(channel, timeout_ms);
if (result != 0) {
serial_tx_byte(result);
serial_tx_byte(0);
}
}
void cmd_get_state() {
serial_tx_str("OK");
}
void cmd_get_version() {
serial_tx_str("subg_rfspy 0.8");
}
void do_cmd(uint8_t cmd) {
if (cmd > 0 && cmd < sizeof(handlers)/sizeof(handlers[0])) {
handlers[cmd]();
}
}
void get_command() {
uint8_t cmd;
cmd = serial_rx_byte();
do_cmd(cmd);
if (interrupting_cmd) {
do_cmd(interrupting_cmd);
interrupting_cmd = 0;
}
}
void cmd_send_packet() {
uint8_t channel;
uint8_t repeat_count;
uint8_t delay_ms;
channel = serial_rx_byte();
repeat_count = serial_rx_byte();
delay_ms = serial_rx_byte();
send_packet_from_serial(channel, repeat_count, delay_ms);
serial_tx_byte(0);
}
/* Combined send and receive */
void cmd_send_and_listen() {
uint8_t send_channel;
uint8_t repeat_count;
uint8_t delay_ms;
uint8_t listen_channel;
uint32_t timeout_ms;
uint8_t retry_count;
uint8_t result;
send_channel = serial_rx_byte();
repeat_count = serial_rx_byte();
delay_ms = serial_rx_byte();
listen_channel = serial_rx_byte();
timeout_ms = serial_rx_long();
retry_count = serial_rx_byte();
send_packet_from_serial(send_channel, repeat_count, delay_ms);
result = get_packet_and_write_to_serial(listen_channel, timeout_ms);
while (result == ERROR_RX_TIMEOUT && retry_count > 0) {
resend_from_tx_buf(send_channel);
result = get_packet_and_write_to_serial(listen_channel, timeout_ms);
retry_count--;
}
if (result != 0) {
// Error, and no retries left
serial_tx_byte(result);
serial_tx_byte(0);
}
}
void cmd_update_register() {
uint8_t addr;
uint8_t value;
uint8_t rval;
addr = serial_rx_byte();
value = serial_rx_byte();
rval = 1;
switch(addr) {
case 0x00:
SYNC1 = value;
break;
case 0x01:
SYNC0 = value;
break;
case 0x02:
PKTLEN = value;
break;
case 0x03:
PKTCTRL1 = value;
break;
case 0x04:
PKTCTRL0 = value;
break;
case 0x05:
ADDR = value;
break;
case 0x06:
CHANNR = value;
break;
case 0x07:
FSCTRL1 = value;
break;
case 0x08:
FSCTRL0 = value;
break;
case 0x09:
FREQ2 = value;
break;
case 0x0A:
FREQ1 = value;
break;
case 0x0B:
FREQ0 = value;
break;
case 0x0C:
MDMCFG4 = value;
break;
case 0x0D:
MDMCFG3 = value;
break;
case 0x0E:
MDMCFG2 = value;
break;
case 0x0F:
MDMCFG1 = value;
break;
case 0x10:
MDMCFG0 = value;
break;
case 0x11:
DEVIATN = value;
break;
case 0x12:
MCSM2 = value;
break;
case 0x13:
MCSM1 = value;
break;
case 0x14:
MCSM0 = value;
break;
case 0x15:
FOCCFG = value;
break;
case 0x16:
BSCFG = value;
break;
case 0x17:
AGCCTRL2 = value;
break;
case 0x18:
AGCCTRL1= value;
break;
case 0x19:
AGCCTRL0 = value;
break;
case 0x1A:
FREND1 = value;
break;
case 0x1B:
FREND0 = value;
break;
case 0x1C:
FSCAL3 = value;
break;
case 0x1D:
FSCAL2 = value;
break;
case 0x1E:
FSCAL1 = value;
break;
case 0x1F:
FSCAL0 = value;
break;
case 0x20:
PA_TABLE1 = value;
break;
case 0x21:
PA_TABLE0 = value;
break;
default:
rval = 2;
}
serial_tx_byte(rval);
serial_tx_byte(0);
}
void cmd_reset() {
EA = 0;
WDCTL = BIT3 | BIT0;
}