-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
355 lines (309 loc) · 10.4 KB
/
main.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
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
/* Includes ----------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "fmt.h"
#include "msg.h"
#include "net/loramac.h"
#include "periph/adc.h"
#include "periph/gpio.h"
#include "periph/pm.h"
#include "periph/rtc.h"
#include "scaling.h"
#include "semtech_loramac.h"
#include "thread.h"
#include "xtimer.h"
/* Private typedef ---------------------------------------------------*/
/* Private define ----------------------------------------------------*/
#define SENDER_PRIO (THREAD_PRIORITY_MAIN - 1)
#define RECV_MSG_QUEUE (4U)
#define ON_TIME 7
#define OUT0 GPIO_PIN(PORT_A, 0)
#define OUT1 GPIO_PIN(PORT_A, 1)
#define OUT2 GPIO_PIN(PORT_A, 2)
#define OUT3 GPIO_PIN(PORT_A, 3)
#ifndef DEFAULT_PERIOD_SENDING
#define DEFAULT_PERIOD_SENDING 600
#endif /* DEFAULT_PERIOD_SENDING */
#ifndef DEFAULT_RESOLUTION
#define DEFAULT_RESOLUTION 0
#endif /* DEFAULT_RESOLUTION */
#define ADC_VREF_INT 9
#define SEC_PER_HOUR 3600
/* Private macro -----------------------------------------------------*/
/* Private variables -------------------------------------------------*/
static kernel_pid_t sender_pid;
static char sender_stack[THREAD_STACKSIZE_MAIN / 2];
static char _recv_stack[THREAD_STACKSIZE_DEFAULT];
static uint8_t deveui[LORAMAC_DEVEUI_LEN];
static uint8_t appeui[LORAMAC_APPEUI_LEN];
static uint8_t appkey[LORAMAC_APPKEY_LEN];
static msg_t _recv_queue[RECV_MSG_QUEUE];
semtech_loramac_t loramac;
uint8_t message[50] = {0};
uint32_t interval_sec = DEFAULT_PERIOD_SENDING;
uint8_t resolution = DEFAULT_RESOLUTION;
struct tm received_data_time_s;
/* Private function prototypes ---------------------------------------*/
static void rtc_cb(void *arg);
static int _prepare_next_alarm(void);
static void _send_message(uint8_t length);
static void *sender(void *arg);
static void *_recv(void *arg);
float _get_vcc(void);
void _init_unused_pins(void);
/* Private functions -------------------------------------------------*/
int main(void) {
uint8_t number_join_tries = 0;
uint8_t loramac_datarate = 0;
/* Init peripherie */
_init_unused_pins();
adc_init(ADC_LINE(ADC_VREF_INT));
gpio_init(OUT0, GPIO_OUT);
gpio_init(OUT1, GPIO_OUT);
gpio_init(OUT2, GPIO_OUT);
gpio_init(OUT3, GPIO_OUT);
/* led test */
gpio_clear(OUT0);
gpio_clear(OUT1);
gpio_clear(OUT2);
gpio_clear(OUT3);
xtimer_sleep(3);
gpio_set(OUT0);
gpio_set(OUT1);
gpio_set(OUT2);
gpio_set(OUT3);
/* Convert identifiers and application key */
fmt_hex_bytes(deveui, DEVEUI);
fmt_hex_bytes(appeui, APPEUI);
fmt_hex_bytes(appkey, APPKEY);
/* Initialize the loramac stack */
semtech_loramac_init(&loramac);
semtech_loramac_set_deveui(&loramac, deveui);
semtech_loramac_set_appeui(&loramac, appeui);
semtech_loramac_set_appkey(&loramac, appkey);
/* Use a fast datarate, e.g. BW125/SF7 in EU868 */
semtech_loramac_set_dr(&loramac, LORAMAC_DR_5);
/* Use ADR */
semtech_loramac_set_adr(&loramac, true);
/* Use unconfirmed data mode */
semtech_loramac_set_tx_mode(&loramac, LORAMAC_TX_UNCNF);
/* Use port 1 for uplink masseges */
semtech_loramac_set_tx_port(&loramac, 1);
/* Start the Over-The-Air Activation (OTAA) procedure to retrieve the
* generated device address and to get the network and application session
* keys.
*/
puts("Starting join procedure");
LED0_ON;
while (semtech_loramac_join(&loramac, LORAMAC_JOIN_OTAA) != SEMTECH_LORAMAC_JOIN_SUCCEEDED) {
puts("Join procedure failed, try in 30s again");
xtimer_sleep(22);
/* increase datarate after 3 join tries */
number_join_tries++;
loramac_datarate = semtech_loramac_get_dr(&loramac);
if (number_join_tries > 2 && loramac_datarate > LORAMAC_DR_0) {
number_join_tries = 0;
loramac_datarate--;
semtech_loramac_set_dr(&loramac, loramac_datarate);
}
}
puts("Join procedure succeeded");
LED0_OFF;
/* start the sender thread */
sender_pid = thread_create(sender_stack, sizeof(sender_stack), SENDER_PRIO, 0, sender, NULL, "sender");
/* start the receive thread */
thread_create(_recv_stack, sizeof(_recv_stack), THREAD_PRIORITY_MAIN - 1, 0, _recv, NULL, "recv thread");
/* trigger the first send */
msg_t msg;
msg_send(&msg, sender_pid);
return 0;
}
static void rtc_cb(void *arg) {
(void)arg;
msg_t msg;
msg_send(&msg, sender_pid);
}
static int _prepare_next_alarm(void) {
struct tm time;
struct tm alarm;
int rc;
uint8_t tries = 3;
rtc_get_time(&time);
time.tm_sec += (interval_sec - ON_TIME);
mktime(&time);
do {
rtc_set_alarm(&time, rtc_cb, NULL);
rtc_get_alarm(&alarm);
rc = rtc_tm_compare(&time, &alarm);
tries--;
} while ((rc != 0) && (tries != 0));
if (rc == 0) {
puts("RTC alarm set\n");
} else {
puts("RTC alarm not set\n");
}
return rc;
}
static void _send_message(uint8_t length) {
printf("Sending\n");
/* Try to send the message */
uint8_t ret = semtech_loramac_send(&loramac, message, length);
if (ret != SEMTECH_LORAMAC_TX_DONE) {
printf("Cannot send message, ret code: %d\n", ret);
return;
}
}
static void *sender(void *arg) {
(void)arg;
msg_t msg;
msg_t msg_queue[8];
msg_init_queue(msg_queue, 8);
while (1) {
msg_receive(&msg);
/* turn off output after 24h */
struct tm now_s;
uint32_t now;
uint32_t received_data_time;
rtc_get_time(&now_s);
now = rtc_mktime(&now_s) + ON_TIME;
received_data_time = rtc_mktime(&received_data_time_s);
if (now - received_data_time >= 24*SEC_PER_HOUR) {
gpio_set(OUT0);
gpio_set(OUT1);
gpio_set(OUT2);
gpio_set(OUT3);
}
if (resolution == 0) {
semtech_loramac_set_tx_port(&loramac, 1); //TODO Port enum
uint8_t vbat = (uint8_t)scaling_float(_get_vcc(), 2.0, 4.0, 0.0, 255.0, LIMIT_OUTPUT);
message[0] = (uint8_t)vbat;
_send_message(1);
} else if (resolution == 1) {
semtech_loramac_set_tx_port(&loramac, 2); //TODO Port enum
uint16_t vbat = (uint16_t)scaling_float(_get_vcc(), 2.0, 4.0, 0.0, 65535.0, LIMIT_OUTPUT);
message[0] = (uint8_t)(vbat >> 8);
message[1] = (uint8_t)(vbat);
_send_message(2);
}
/* wait some time to give receive thread time to work */
xtimer_sleep(3);
/* Schedule the next wake-up alarm */
if (_prepare_next_alarm() == 0) {
/* going to deep sleep */
puts("Going to sleep");
pm_set(1);
} else {
/* rtc can not set, try reboot */
pm_reboot();
}
}
/* this should never be reached */
return NULL;
}
static void *_recv(void *arg) {
msg_init_queue(_recv_queue, RECV_MSG_QUEUE);
(void)arg;
uint16_t received_interval = 0;
uint8_t received_resolution = 0;
while (1) {
/* blocks until some data is received */
semtech_loramac_recv(&loramac);
loramac.rx_data.payload[loramac.rx_data.payload_len] = 0;
puts("Data received:");
for (uint8_t i = 0; i < loramac.rx_data.payload_len; i++) {
print_byte_hex(loramac.rx_data.payload[i]);
}
puts("\nPort:");
print_u32_dec((uint32_t)loramac.rx_data.port);
puts("");
/* process received data */
switch (loramac.rx_data.port) {
case 1: /* resolution */
received_resolution = loramac.rx_data.payload[0];
if (received_resolution <= 1) {
resolution = received_resolution;
}
break;
case 2: /* time of period sending */
received_interval = (loramac.rx_data.payload[0] << 8) + loramac.rx_data.payload[1];
if (received_interval >= 3) {
interval_sec = received_interval * 10;
}
break;
case 3: /* System reboot */
if (loramac.rx_data.payload[0] == 1) {
pm_reboot();
}
break;
case 4: /* rubbish data */
rtc_get_time(&received_data_time_s);
if (loramac.rx_data.payload[0] & 1) {
gpio_clear(OUT0);
} else {
gpio_set(OUT0);
}
if (loramac.rx_data.payload[0] & 2) {
gpio_clear(OUT1);
} else {
gpio_set(OUT1);
}
if (loramac.rx_data.payload[0] & 4) {
gpio_clear(OUT2);
} else {
gpio_set(OUT2);
}
if (loramac.rx_data.payload[0] & 8) {
gpio_clear(OUT3);
} else {
gpio_set(OUT3);
}
break;
}
}
return NULL;
}
float _get_vcc(void) {
float vbat = 4957.744 / adc_sample(ADC_LINE(ADC_VREF_INT), ADC_RES_12BIT);
return vbat;
}
void _init_unused_pins(void) {
gpio_t unused_pins[] = {
//GPIO_PIN(PORT_A, 0), OUT0
//GPIO_PIN(PORT_A, 1), OUT1
//GPIO_PIN(PORT_A, 2), OUT2
//GPIO_PIN(PORT_A, 3), OUT3
GPIO_PIN(PORT_A, 4),
GPIO_PIN(PORT_A, 5),
GPIO_PIN(PORT_A, 6),
GPIO_PIN(PORT_A, 7),
//GPIO_PIN(PORT_A, 8),MCO
GPIO_PIN(PORT_A, 9),
GPIO_PIN(PORT_A, 10),
GPIO_PIN(PORT_A, 11),
//GPIO_PIN(PORT_A, 12), //USB pullup
GPIO_PIN(PORT_A, 13),
GPIO_PIN(PORT_A, 14),
GPIO_PIN(PORT_A, 15),
GPIO_PIN(PORT_B, 0),
GPIO_PIN(PORT_B, 1),
GPIO_PIN(PORT_B, 3),
GPIO_PIN(PORT_B, 4),
//GPIO_PIN(PORT_B, 5), DIO2
//GPIO_PIN(PORT_B, 6), DIO1
//GPIO_PIN(PORT_B, 7), DIO0
GPIO_PIN(PORT_B, 8),
GPIO_PIN(PORT_B, 9),
GPIO_PIN(PORT_B, 10),
GPIO_PIN(PORT_B, 11),
//GPIO_PIN(PORT_B, 12), NNS
//GPIO_PIN(PORT_B, 13), SCK
//GPIO_PIN(PORT_B, 14), MISO
//GPIO_PIN(PORT_B, 15), MOSI
//GPIO_PIN(PORT_C, 13), LED0
};
for (uint8_t i = 0; i < (sizeof(unused_pins) / sizeof(gpio_t)); i++) {
gpio_init(unused_pins[i], GPIO_IN_PD);
}
}