-
Notifications
You must be signed in to change notification settings - Fork 0
/
he100.c
183 lines (158 loc) · 4.61 KB
/
he100.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
/*
* File: he100.c
* Author: Peter Thornton
* Comments: Interface to the He-100 VHF/UHF transceiver, via UART2
* Revision history: 2 March 2020
*/
#include "xc.h"
#include "he100.h"
#include "uart.h"
#include <string.h>
#include <stdio.h>
// CRC calculation. Stores the two checksum bytes in the two buffer locations
// after pointer+nbytes
void he100_checksum(unsigned char *buf, int nbytes)
{
int i;
unsigned char ck_a, ck_b;
ck_a = 0;
ck_b = 0;
for (i=0 ; i<nbytes ; i++)
{
ck_a += *buf++;
ck_b += ck_a;
}
*buf++ = ck_a;
*buf = ck_b;
}
int he100_noop(unsigned char* response)
{
int i;
unsigned char buf[8];
unsigned char ck_a, ck_b;
// fill the header
buf[0]=0x48; // 'H'
buf[1]=0x65; // 'e'
buf[2]=0x10; // command prefix (10 = send)
buf[3]=0x01; // command code (01 = no-op)
buf[4]=0x00; // no payload
buf[5]=0x00; // no payload
// set the CRC bytes for header
he100_checksum(&buf[2],4);
// write 8 bytes to UART2
for (i=0 ; i<8 ; i++)
{
write_char2(buf[i]);
}
// read 8-byte response from radio board on UART2
for (i=0 ; i<8 ; i++)
{
buf[i] = read_char2();
}
// copy from buf to response output array
for (i=0 ; i<8 ; i++)
{
*response++ = buf[i];
}
// test the response checksum
ck_a = buf[6];
ck_b = buf[7];
// calculate the response checksum
he100_checksum(&buf[2],4);
// return 1 if the checksum test fails, 0 otherwise
return ((ck_a != buf[6]) || (ck_b != buf[7]));
}
int he100_telemetry(unsigned char* telem_raw)
{
int i;
unsigned char buf[26]; // buffer big enough for header and telemetry
unsigned char ck_a, ck_b; // checksum bytes
int is_err = 0;
// fill the header for command
buf[0]=0x48; // 'H'
buf[1]=0x65; // 'e'
buf[2]=0x10; // command prefix (10 = send)
buf[3]=0x07; // command code (07 = retrieve telemetry)
buf[4]=0x00; // no payload
buf[5]=0x00; // no payload
// set the CRC bytes for header
he100_checksum(&buf[2],4);
// write 8 bytes to UART2
for (i=0 ; i<8 ; i++)
{
write_char2(buf[i]);
}
// read 26-byte response from radio board on UART2
// 8 bytes header, 16 bytes telemetry data, 2 bytes checksum
for (i=0 ; i<26 ; i++)
{
buf[i] = read_char2();
}
// save the checksum, then recalculate
ck_a = buf[24];
ck_b = buf[25];
he100_checksum(&buf[2],22);
if (ck_a == buf[24] && ck_b == buf[25])
{
// good checksum
// copy the 16-byte telemetry data to response, skip header
for (i=0 ; i<16 ; i++)
{
*telem_raw++ = buf[i+8];
}
is_err = 0;
}
else
{
is_err = 1;
}
return is_err;
}
void he100_transmit_packet(unsigned char* response, char* data)
{
int i;
unsigned char buf[512];
int data_len;
int packet_len;
// Note that the downlink handling assumes the data array is a null-terminated
// string.
// length of data string
data_len = strlen(data);
// If the string is longer than the max allowable payload length (255 bytes)
// replace with an error message
if (data_len > 255)
{
sprintf(data,"RamSat: he100_transmit_packet->ERROR: data packet too long (%d bytes)",data_len);
data_len = strlen(data);
}
// fill the header
buf[0]=0x48; // 'H'
buf[1]=0x65; // 'e'
buf[2]=0x10; // command prefix (10 = send)
buf[3]=0x03; // command code (03 = transmit)
buf[4]=0x00; // payload size, MSB
buf[5]=data_len; // payload size, LSB
// append 2 CRC bytes for header (skip first two bytes))
he100_checksum(&buf[2],4);
// fill the payload
for (i=0 ; i<data_len ; i++)
{
buf[8+i]=data[i];
}
// append 2 CRC bytes for entire packet (skip first two bytes of header)
he100_checksum(&buf[2],6+data_len);
// packet length = header(6 + 2 CRC) + data_len + 2 CRC
packet_len = 8 + data_len + 2;
// write complete packet to UART2 for transmission
for (i=0 ; i<packet_len ; i++)
{
write_char2(buf[i]);
}
// read 8-byte response from radio board on UART2
for (i=0 ; i<8 ; i++)
{
//*response++ = read_char2();
response[i] = read_char2();
}
//*response = read_char2();
}