-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
96 lines (82 loc) · 1.92 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
/*
* main.c
*
* Created on: 20 sty 2022
* Author: kpier
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <avr/wdt.h>
#include "AES/cmac.h"
#include "AES/qqq_aes.h"
#include "lorawan.h"
#include "lorawan_join.h"
#include "lorawan_downlink.h"
#include "lorawan_uplink.h"
#include "MK_USART/mkuart.h"
void parse_uart( char * buf ) {
if( !strncasecmp( "AT+RST?", buf, 7 ) ) {
cli();
// disable interrupts
wdt_enable( 0 );// set watchdog
while( 1 )
; // wait for RESET
}
}
void parse_lorawan( uint8_t fport, uint8_t * buf, uint8_t len ) {
uart_puts_P( PSTR( "\r\n\r\nPARSE_LORAWAN: " ) );
uart_puthex( buf, len );
uart_puts_P( PSTR( "On fport: " ) );
uart_putint( fport, 10 );
uart_putln();
}
int main() {
// WAIT AFTER BOOTLOADER FLASH
_delay_ms( 5000 );
// ===========================
// DEBUG LED
DDRC |= ( 1 << PC5 );
PORTC &= ~( 1 << PC5 );
// =========
// INIT UART
USART_Init( __UBRR );
register_uart_str_rx_event_callback( parse_uart );
sei();
// =========
if( lorawan_init() != JOIN_SUCCESS ) {
uart_puts_P( PSTR( "Failed to init lorawan module.\r\n" ) );
while( 1 )
;
}
register_lorawan_downlink_callback( parse_lorawan );
uart_puts_P( PSTR( "Trying to join.\r\n" ) );
while( 1 ) {
if( lorawan_join() == JOIN_SUCCESS ) {
uart_puts_P( PSTR( "Join success.\r\n" ) );
break;
}
_delay_ms( 10000 );
uart_puts_P( PSTR( "Join failed. Trying to join.\r\n" ) );
}
_delay_ms( 5000 );
uint16_t cnt = 0, led = 0;
char uart_buf[ 32 ];
while( 1 ) {
if( cnt == 18000 ) {
uint8_t buf[ 3 ] = { 'K', ':', ( cnt % 10 ) + 48 };
lorawan_uplink( buf, 3 );
cnt = 0;
}
if( led == 100 ) {
PORTC ^= ( 1 << PC5 );
led = 0;
}
_delay_ms( 10 );
cnt++;
led++;
UART_RX_STR_EVENT( uart_buf );
}
return 0;
}