-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoRa.h
149 lines (128 loc) · 4.32 KB
/
LoRa.h
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
//
// Created by MMKH on 10/23/2021.
//
#include "LinuxLoRaSPIHAL.c"
#define ceil(n, d) (((n) < 0) ? (-((-(n))/(d))) : (n)/(d) + ((n)%(d) != 0))
//#define floor(n, d) (((n) < 0) ? (-((-(n))/(d))) - ((n)%(d) != 0) : (n)/(d))
/* -------------------------------------------------- \\
// | | \\
// | WRITTEN BY: Salman Motlaq | \\
// | Telegram: @S_Motlaq | \\
// | E-MAIL: [email protected] | \\
// | | \\
// -------------------------------------------------- */
#define TRANSMIT_TIMEOUT 2000
#define RECEIVE_TIMEOUT 2000
//--------- MODES ---------//
#define SLEEP_MODE 0
#define STNBY_MODE 1
#define TRANSMIT_MODE 3
#define RXCONTIN_MODE 5
#define RXSINGLE_MODE 6
//------- BANDWIDTH -------//
#define BW_7_8KHz 0
#define BW_10_4KHz 1
#define BW_15_6KHz 2
#define BW_20_8KHz 3
#define BW_31_25KHz 4
#define BW_41_7KHz 5
#define BW_62_5KHz 6
#define BW_125KHz 7
#define BW_250KHz 8
#define BW_500KHz 9
//------ CODING RATE ------//
#define CR_4_5 1
#define CR_4_6 2
#define CR_4_7 3
#define CR_4_8 4
//--- SPREADING FACTORS ---//
#define SF_6 6
#define SF_7 7
#define SF_8 8
#define SF_9 9
#define SF_10 10
#define SF_11 11
#define SF_12 12
//------ POWER GAIN ------//
#define POWER_11db 0xF6
#define POWER_14db 0xF9
#define POWER_17db 0xFC
#define POWER_20db 0xFF
//------- REGISTERS -------//
#define RegFiFo 0x00
#define RegOpMode 0x01
#define RegFrMsb 0x06
#define RegFrMid 0x07
#define RegFrLsb 0x08
#define RegPaConfig 0x09
#define RegOcp 0x0B
#define RegLna 0x0C
#define RegFiFoAddPtr 0x0D
#define RegFiFoTxBaseAddr 0x0E
#define RegFiFoRxBaseAddr 0x0F
#define RegFiFoRxCurrentAddr 0x10
#define RegIrqFlags 0x12
#define RegRxNbBytes 0x13
#define RegPktRssiValue 0x1A
#define RegRssiValue 0x1B
#define RegModemConfig1 0x1D
#define RegModemConfig2 0x1E
#define RegSymbTimeoutL 0x1F
#define RegPreambleMsb 0x20
#define RegPreambleLsb 0x21
#define RegPayloadLength 0x22
#define RegFifoRxByteAddr 0x25
#define RegDetectOptimize 0x31
#define RegDetectionThreshold 0x37
#define RegDioMapping1 0x40
#define RegDioMapping2 0x41
#define RegVersion 0x42
//------ LORA STATUS ------//
#define LORA_OK 200
#define LORA_NOT_FOUND 404
#define LORA_LARGE_PAYLOAD 413
#define LORA_UNAVAILABLE 503
typedef struct LoRa_setting{
// Hardware setings:
// GPIO_TypeDef* CS_port;
// uint16_t CS_pin;
// GPIO_TypeDef* reset_port;
// uint16_t reset_pin;
// GPIO_TypeDef* DIO0_port;
// uint16_t DIO0_pin;
// SPI_HandleTypeDef* hSPIx;
char* SPI_Path;
char* Reset_Path;
// Module settings:
int current_mode;
int frequency;
uint8_t spredingFactor;
uint8_t bandWidth;
uint8_t crcRate;
uint16_t preamble;
uint8_t power;
uint8_t overCurrentProtection;
} LoRa;
LoRa newLoRa(irq_handler_t handler);
void LoRa_reset(LoRa* _LoRa);
void LoRa_readReg(LoRa* _LoRa, uint8_t* address, uint16_t r_length, uint8_t* output, uint16_t w_length);
void LoRa_writeReg(LoRa* _LoRa, uint8_t* address, uint16_t r_length, uint8_t* values, uint16_t w_length);
void LoRa_gotoMode(LoRa* _LoRa, int mode);
uint8_t LoRa_read(LoRa* _LoRa, uint8_t address);
void LoRa_write(LoRa* _LoRa, uint8_t address, uint8_t value);
void LoRa_BurstWrite(LoRa* _LoRa, uint8_t address, uint8_t *value, uint8_t length);
uint8_t LoRa_isvalid(LoRa* _LoRa);
void LoRa_setFrequency(LoRa* _LoRa, int freq);
void LoRa_setSpreadingFactor(LoRa* _LoRa, int SP);
void LoRa_setPower(LoRa* _LoRa, uint8_t power);
//void LoRa_setOCP(LoRa* _LoRa, uint8_t current);
void LoRa_setTOMsb_setCRCon(LoRa* _LoRa);
uint8_t LoRa_transmit(LoRa* _LoRa, uint8_t* data, uint8_t length, uint16_t timeout);
void LoRa_startReceiving(LoRa* _LoRa);
uint8_t LoRa_receive(LoRa* _LoRa, uint8_t* data, uint8_t length);
void LoRa_receive_IT(LoRa* _LoRa, uint8_t* data, uint8_t length);
int LoRa_getRSSI(LoRa* _LoRa);
int LoRa_getInstantRSSI(LoRa* _LoRa);
uint16_t LoRa_init(LoRa* _LoRa);
void LoRa_end(LoRa* _LoRa);
uint32_t LoRa_calculateTOA(LoRa* _LoRa, uint8_t length);