-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlora.c
89 lines (79 loc) · 1.97 KB
/
lora.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
/*
* lora.c
*
* Created on: Feb 3, 2019
* Author: TritonCubed
*
* The breakout uses the following pins:
*
* RPI Chip
* --------------------
* Pin 14 (GND) 0V
* Pin 24 CS
* Pin 16 RT
* Pin 17 (3V3) 3V3
* Pin 23 (SCLK) CK
* Pin 21 (MISO) SO
* Pin 19 (MOSI) SI
*/
#include "lora.h"
#include "bcm2835.h"
#include <stdio.h>
#include <stdlib.h>
/*
* Boots the lora module.
* Requires SPI to be initialized.
*/
void lora_init( char bufferSize ) {
//get current mode
uint8_t bootmode = read_reg( REG_OP_MODE );
//Needs to be in FSK
if( !(bootmode & NON_FSK) ) {
write_reg( REG_OP_MODE, FSK_SLEEP );
}
//proper boot routine
write_reg( REG_OP_MODE, LORA_SLEEP );
write_reg( REG_OP_MODE, LORA_STANDBY );
//check that the module is in standby mode
bootmode = read_reg( REG_OP_MODE );
if( bootmode != LORA_STANDBY ) {
printf( "Could not initialize LORA.\n" );
exit( EXIT_FAILURE );
}
printf( "LORA on standby.\n" );
//if all's good, set the payload size
write_reg( REG_PAYLOAD_LEN, bufferSize - 1);
}
/*
* Cleans the lora module.
*/
void lora_close() {
write_reg( REG_OP_MODE, FSK_SLEEP );
}
/*
* Reads from the specified register.
* Returns the value in the register.
*/
uint8_t read_reg( uint8_t reg ){
char tbuf[] = { reg , 0x00 };
char rbuf[] = { 0x00, 0x00 };
bcm2835_spi_transfernb( tbuf, rbuf, sizeof( tbuf ) );
//fprintf( stdout, "Read value 0x%02X from register 0x%02X.\n", rbuf[1], reg );
return rbuf[1];
}
/*
* Writes to the specified register.
* Returns the previous value in the register.
*/
uint8_t write_reg( uint8_t reg, char data ) {
char tbuf[] = { reg | 0x80, data}; //flag reg MSB high to indicate write
char rbuf[] = { 0x00, 0x00 };
bcm2835_spi_transfernb( tbuf, rbuf, sizeof( tbuf ) );
if( reg == REG_FIFO ){
//printf( "Wrote '%c' to FIFO.\n", data );
}
else {
//printf( "Wrote value 0x%02X to register 0x%02X.\n", data, reg );
}
return rbuf[1];
}