-
Notifications
You must be signed in to change notification settings - Fork 0
/
loratx.c
102 lines (84 loc) · 2 KB
/
loratx.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
/*
* lorarx.c
*
* Author: Christopher Madrigal
* Modified: 21 February 2020
*
* To connect to the Pi, use this pinout:
*
* RPI Chip
* --------------------
* Pin 14 (GND) 0V
* Pin 16 RT
* Pin 17 (3V3) 3V3
* Pin 19 (MOSI) SI
* Pin 21 (MISO) SO
* Pin 23 (SCLK) CK
* Pin 24 CS
*/
#include "spi.h"
#include "lora.h"
#include "helper.h"
#include <bcm2835.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
/*
* Signal capture
*/
void handle_signal( int signum ) {
printf( "\r\n\r\nProcess killed ;(\r\nHave a nice day!\r\n\r\n" );
lora_close();
spi_close();
bcm2835_close();
exit( EXIT_SUCCESS );
}
//payload
char payload[BUFFER_SIZE] = "Hello, world!";
void tx_test() {
printf( "Starting TX Test...\r\n" );
while( 1 ) {
//set SPI access to fifo to the Tx base reg
write_reg( REG_FIFO_ADDR_PTR, read_reg( REG_FIFO_TX_BASE_ADDR ) );
//load payload into FIFO
for( size_t i = 0; i < sizeof( payload ); i++ ){
write_reg( REG_FIFO, payload[i] );
}
//commence Tx
write_reg( REG_OP_MODE, LORA_TX );
//wait for Tx to end
while( (read_reg( REG_IRQ_FLAGS ) & FLAG_TX_DONE) == 0 ) {}
//confirm Tx
if( read_reg( REG_OP_MODE ) == LORA_STANDBY ) {
write_reg( REG_IRQ_FLAGS, CLEAR_IRQ_FLAGS );
printf( "Transmitted payload.\r\n" );
}
else {
//reinit lora module
lora_init();
printf( "LoRa was reinitialized.\r\n" );
}
//wait 5 seconds
time_delay( 0.5 );
}
}
int main( int argc, char **argv ){
//handle any signal errors
signal( SIGINT, handle_signal );
// BCM Lib initialization
if( !bcm2835_init() ) {
printf( "bcm2835_init failed. Must run as root.\r\n\r\n" );
return EXIT_FAILURE;
}
// setup spi
spi_init();
// boot sequence for LoRa standby
if( lora_init() ) {
tx_test();
}
lora_close();
spi_close();
bcm2835_close();
return EXIT_SUCCESS;
}