-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTX_Demo.cpp
199 lines (169 loc) · 4.67 KB
/
TX_Demo.cpp
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
#include "cc1100_raspi.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <wiringPi.h>
#include <wiringPiSPI.h>
#define PACKAGE "CC1100 SW"
#define VERSION_SW "0.9.6"
#define INTERVAL_1S_TIMER 1000
uint8_t Tx_fifo[FIFOBUFFER], Rx_fifo[FIFOBUFFER];
uint8_t Tx_addr, Rx_addr, Pktlen, pktlen, Lqi, Rssi;
uint8_t rx_addr, sender, lqi;
int8_t rssi_dbm;
int cc1100_freq_select, cc1100_mode_select, cc1100_channel_select;
uint32_t prev_millis_1s_timer = 0;
uint8_t cc1100_debug = 0;
uint8_t tx_retries = 1;
uint8_t rx_demo_addr = 3;
uint32_t interval = 1000;
uint8_t SelfAddress = 8;
//set PA level in dbm
uint8_t Power = 0;
CC1100 cc1100;
void print_help(int exval)
{
printf("%s,%s by CW\r\n", PACKAGE, VERSION_SW);
printf("%s [-h] [-V] [-a SelfAddress] [-r RxDemo_Addr] [-i Msg_Interval] [-t tx_retries] [-c channel] [-f frequency]\r\n", PACKAGE);
printf(" [-m modulation]\n\r\n\r");
printf(" -h print this help and exit\r\n");
printf(" -V print version and exit\r\n\r\n");
printf(" -v set verbose flag\r\n");
printf(" -a my address [1-255] set my address\r\n\r\n");
printf(" -r rx address [1-255] set RxDemo receiver address\r\n\r\n");
printf(" -i interval ms[1-6000] sets message interval timing\r\n\r\n");
printf(" -t tx_retries [0-255] sets message send retries\r\n\r\n");
printf(" -c channel [1-255] set transmit channel\r\n");
printf(" -f frequency [315,434,868,915] set ISM band\r\n\r\n");
printf(" -m modulation [1,38,100,250,500,4] set modulation\r\n\r\n");
exit(exval);
}
int main(int argc, char *argv[])
{
int opt;
if (argc == 1)
{
fprintf(stderr, "This program needs arguments....\n\r\n\r");
print_help(1);
}
while ((opt = getopt(argc, argv, "hVva:r:i:t:c:f:m:")) != -1)
{
switch (opt)
{
case 'h':
print_help(0);
break;
case 'V':
printf("%s %s\n\n", PACKAGE, VERSION_SW);
exit(0);
break;
case 'v':
printf("%s: Verbose option is set\n", PACKAGE);
cc1100_debug = 1;
break;
case 'a':
SelfAddress = atoi(optarg);
break;
case 'r':
rx_demo_addr = atoi(optarg);
break;
case 'i':
interval = atoi(optarg);
break;
case 't':
tx_retries = atoi(optarg);
break;
case 'c':
cc1100_channel_select = atoi(optarg);
break;
case 'f':
cc1100_freq_select = atoi(optarg);
switch (cc1100_freq_select)
{
case 315:
cc1100_freq_select = 1;
break;
case 434:
cc1100_freq_select = 2;
break;
case 868:
cc1100_freq_select = 3;
break;
case 915:
cc1100_freq_select = 4;
break;
}
break;
case 'm':
cc1100_mode_select = atoi(optarg);
switch (cc1100_mode_select)
{
case 1:
cc1100_mode_select = 1;
break;
case 38:
cc1100_mode_select = 2;
break;
case 100:
cc1100_mode_select = 3;
break;
case 250:
cc1100_mode_select = 4;
break;
case 500:
cc1100_mode_select = 5;
break;
case 4:
cc1100_mode_select = 6;
break;
}
break;
case ':':
fprintf(stderr, "%s: Error - Option `%c' needs a value\n\n", PACKAGE, optopt);
print_help(1);
break;
case '?':
fprintf(stderr, "%s: Error - No such option: `%c'\n\n", PACKAGE, optopt);
print_help(1);
}
}
// print all remaining options
for (; optind < argc; optind++)
printf("argument: %s\n", argv[optind]);
//------------- welcome message ------------------------
printf("Raspberry CC1101 SPI Library test\n");
//------------- hardware setup ------------------------
wiringPiSetup(); //setup wiringPi library
cc1100.begin(SelfAddress); //setup cc1000 RF IC
cc1100.sidle();
//set PA level
cc1100.set_output_power_level(Power);
cc1100.receive();
cc1100.show_main_settings(); //shows setting debug messages to UART
cc1100.show_register_settings();
//------------------------- Main Loop ------------------------
for (;;)
{
delay(1); //delay to reduce system load
if (millis() - prev_millis_1s_timer >= interval) // one second update timer
{
Rx_addr = rx_demo_addr; //receiver address
uint32_t time_stamp = millis(); //generate time stamp
Tx_fifo[3] = (uint8_t)(time_stamp >> 24); //split 32-Bit timestamp to 4 byte array
Tx_fifo[4] = (uint8_t)(time_stamp >> 16);
Tx_fifo[5] = (uint8_t)(time_stamp >> 8);
Tx_fifo[6] = (uint8_t)(time_stamp);
Pktlen = 0x07; //set packet len to 0x13
uint8_t res = cc1100.sent_packet(SelfAddress, Rx_addr, Tx_fifo, Pktlen, tx_retries);
if (res == 1) //sents package over air. ACK is received via GPIO polling
{
printf("transmitted tx_timestamp: %ums \r\n\r\n", time_stamp);
}
prev_millis_1s_timer = millis();
}
}
printf("finished!\n");
return 0;
}