-
Notifications
You must be signed in to change notification settings - Fork 16
/
app_RAK811.c
303 lines (264 loc) · 10 KB
/
app_RAK811.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
#include "rui.h"
#include "board.h"
/* User Code */
/*!
* User sleep timer related variables.
*/
TimerEvent_t user_timer; // Timer instance.
uint32_t timer_interval = 1; // Timer timeout, in seconds.
bool timeout_flag = false;
/*!
* GPIO variables
*/
RUI_GPIO_ST gpio_output;
RUI_GPIO_ST gpio_input;
uint8_t pin_high = 1;
uint8_t pin_low = 0;
void user_timer_callback(void)
{
timeout_flag = true;
rui_timer_start(&user_timer);
}
void setup(void)
{
RUI_RETURN_STATUS ret_code;
ret_code = rui_timer_init(&user_timer, user_timer_callback);
if (ret_code != RUI_STATUS_OK)
RUI_LOG_PRINTF("Timer init error! %d\r\n", ret_code);
rui_timer_setvalue(&user_timer, timer_interval*1000);
rui_timer_start(&user_timer);
/* The available pins of the RAK811 module are:
* pin 2, pin 3, pin 4, pin 5, pin 8, pin 9, pin 14,
* pin 15, pin 16, pin 18, pin 19, pin 20, pin 22, pin 23.
*/
gpio_output.pin_num = 2; // Pin 2 of the RAK811 module, PB12 pin of the MCU.
gpio_output.dir = RUI_GPIO_PIN_DIR_OUTPUT;
gpio_output.pull = RUI_GPIO_PIN_NOPULL;
ret_code = rui_gpio_init(&gpio_output);
if (ret_code != RUI_STATUS_OK)
RUI_LOG_PRINTF("Output pin init error! %d\r\n", ret_code);
gpio_input.pin_num = 3; // Pin 3 of the RAK811 module, PB14 pin of the MCU.
gpio_input.dir = RUI_GPIO_PIN_DIR_INPUT;
gpio_input.pull = RUI_GPIO_PIN_PULLUP;
ret_code = rui_gpio_init(&gpio_input);
if (ret_code != RUI_STATUS_OK)
RUI_LOG_PRINTF("Input pin init error! %d\r\n", ret_code);
}
void loop(void)
{
static uint8_t output_state = 0;
if (timeout_flag)
{
// set gpio state
if (output_state)
rui_gpio_rw(RUI_IF_WRITE, &gpio_output, &pin_high);
else
rui_gpio_rw(RUI_IF_WRITE, &gpio_output, &pin_low);
output_state = (output_state + 1) % 2;
// get gpio state
uint8_t pin_value;
rui_gpio_rw(RUI_IF_READ, &gpio_input, &pin_value);
RUI_LOG_PRINTF("The state of pin %d is %d.\r\n", gpio_input.pin_num, pin_value);
timeout_flag = false;
}
}
/* End User Code */
/*******************************************************************************************
* LoRaMac callback functions
* * void LoRaReceive_callback(RUI_RECEIVE_T* Receive_datapackage);//LoRaWAN callback if receive data
* * void LoRaP2PReceive_callback(RUI_LORAP2P_RECEIVE_T *Receive_P2Pdatapackage);//LoRaP2P callback if receive data
* * void LoRaWANJoined_callback(uint32_t status);//LoRaWAN callback after join server request
* * void LoRaWANSendsucceed_callback(RUI_MCPS_T status);//LoRaWAN call back after send data complete
* *****************************************************************************************/
void LoRaReceive_callback(RUI_RECEIVE_T* Receive_datapackage)
{
char hex_str[3] = {0};
RUI_LOG_PRINTF("at+recv=%d,%d,%d,%d", Receive_datapackage->Port, Receive_datapackage->Rssi, Receive_datapackage->Snr, Receive_datapackage->BufferSize);
if ((Receive_datapackage->Buffer != NULL) && Receive_datapackage->BufferSize) {
RUI_LOG_PRINTF(":");
for (int i = 0; i < Receive_datapackage->BufferSize; i++) {
sprintf(hex_str, "%02x", Receive_datapackage->Buffer[i]);
RUI_LOG_PRINTF("%s", hex_str);
}
}
RUI_LOG_PRINTF("\r\n");
}
void LoRaP2PReceive_callback(RUI_LORAP2P_RECEIVE_T *Receive_P2Pdatapackage)
{
char hex_str[3]={0};
RUI_LOG_PRINTF("at+recv=%d,%d,%d:", Receive_P2Pdatapackage -> Rssi, Receive_P2Pdatapackage -> Snr, Receive_P2Pdatapackage -> BufferSize);
for(int i=0;i < Receive_P2Pdatapackage -> BufferSize; i++)
{
sprintf(hex_str, "%02X", Receive_P2Pdatapackage -> Buffer[i]);
RUI_LOG_PRINTF("%s",hex_str);
}
RUI_LOG_PRINTF("\r\n");
}
void LoRaWANJoined_callback(uint32_t status)
{
if(status) //Join Success
{
RUI_LOG_PRINTF("OK Join Success\r\n");
}
else
{
RUI_LOG_PRINTF("ERROR: %d\r\n",RUI_AT_LORA_INFO_STATUS_JOIN_FAIL);
}
}
void LoRaWANSendsucceed_callback(RUI_MCPS_T mcps_type,RUI_RETURN_STATUS status)
{
if(status == RUI_STATUS_OK)
{
switch( mcps_type )
{
case RUI_MCPS_UNCONFIRMED:
{
RUI_LOG_PRINTF("OK \r\n");
break;
}
case RUI_MCPS_CONFIRMED:
{
RUI_LOG_PRINTF("OK \r\n");
break;
}
case RUI_MCPS_MULTICAST:
{
RUI_LOG_PRINTF("OK \r\n");
break;
}
case RUI_MCPS_PROPRIETARY:
{
RUI_LOG_PRINTF("OK \r\n");
break;
}
default:
break;
}
}else if(status != RUI_AT_LORA_INFO_STATUS_ADDRESS_FAIL)RUI_LOG_PRINTF("ERROR: %d\r\n",status);
}
void LoRaP2PSendsucceed_callback(void)
{
RUI_LOG_PRINTF("OK \r\n");
}
/*******************************************************************************************
* The RUI is used to receive data from uart.
*
* *****************************************************************************************/
void rui_uart_recv(RUI_UART_DEF uart_def, uint8_t *pdata, uint16_t len)
{
switch(uart_def)
{
case RUI_UART1://process code if RUI_UART1 work at RUI_UART_UNVARNISHED
break;
case RUI_UART3://process code if RUI_UART3 received data ,the len is always 1
/*****************************************************************************
* user code
******************************************************************************/
// RUI_LOG_PRINTF("%c",*pdata);
default:break;
}
}
/*******************************************************************************************
* sleep and wakeup callback
*
* *****************************************************************************************/
void bsp_sleep(void)
{
/*****************************************************************************
* user process code before enter sleep
******************************************************************************/
}
void bsp_wakeup(void)
{
/*****************************************************************************
* user process code after exit sleep
******************************************************************************/
}
/*******************************************************************************************
* the app_main function
* *****************************************************************************************/
void main(void)
{
RUI_LORA_STATUS_T app_lora_status;
rui_init();
/*******************************************************************************************
* Register LoRaMac callback function
*
* *****************************************************************************************/
rui_lora_register_recv_callback(LoRaReceive_callback);
rui_lorap2p_register_recv_callback(LoRaP2PReceive_callback);
rui_lorajoin_register_callback(LoRaWANJoined_callback);
rui_lorasend_complete_register_callback(LoRaWANSendsucceed_callback);
rui_lorap2p_complete_register_callback(LoRaP2PSendsucceed_callback);
/*******************************************************************************************
* Register Sleep and Wakeup callback function
*
* *****************************************************************************************/
rui_sensor_register_callback(bsp_wakeup, bsp_sleep);
/*******************************************************************************************
* Obtain status information and print.
*
* *****************************************************************************************/
rui_lora_get_status(false, &app_lora_status);
switch(app_lora_status.work_mode)
{
case RUI_LORAWAN:
RUI_LOG_PRINTF("LoRa work mode:LoRaWAN,");
if(app_lora_status.join_mode == RUI_OTAA)
{
RUI_LOG_PRINTF(" join_mode:OTAA,");
if(app_lora_status.MulticastEnable)
{
RUI_LOG_PRINTF(" MulticastEnable:true.\r\n");
}else
{
RUI_LOG_PRINTF(" MulticastEnable: false,");
switch(app_lora_status.class_status)
{
case RUI_CLASS_A:RUI_LOG_PRINTF(" Class: A\r\n");
break;
case RUI_CLASS_B:RUI_LOG_PRINTF(" Class: B\r\n");
break;
case RUI_CLASS_C:RUI_LOG_PRINTF(" Class: C\r\n");
break;
default:break;
}
}
}
else if(app_lora_status.join_mode == RUI_ABP)
{
RUI_LOG_PRINTF(" join_mode:ABP,");
if(app_lora_status.MulticastEnable)
{
RUI_LOG_PRINTF(" MulticastEnable:true.\r\n");
}
else
{
RUI_LOG_PRINTF(" MulticastEnable: false,");
switch(app_lora_status.class_status)
{
case RUI_CLASS_A:RUI_LOG_PRINTF(" Class: A\r\n");
break;
case RUI_CLASS_B:RUI_LOG_PRINTF(" Class: B\r\n");
break;
case RUI_CLASS_C:RUI_LOG_PRINTF(" Class: C\r\n");
break;
default:break;
}
}
}
break;
default: break;
}
RUI_LOG_PRINTF("Initialization OK \r\n");
/* User Code */
setup();
/* End User Code */
while(1)
{
rui_running();
/* User Code */
loop();
/* End User Code */
}
}