1 /*!
2 * \file main.c
3 *
4 * \brief Radio sensitivity test
5 *
6 * \remark When LED1 stops blinking LoRa packets aren't received any more and
7 * the sensitivity level has been reached.
8 * By reading the RF generator output power we can estimate the board
9 * sensitivity
10 *
11 * \copyright Revised BSD License, see section \ref LICENSE.
12 *
13 * \code
14 * ______ _
15 * / _____) _ | |
16 * ( (____ _____ ____ _| |_ _____ ____| |__
17 * \____ \| ___ | (_ _) ___ |/ ___) _ \
18 * _____) ) ____| | | || |_| ____( (___| | | |
19 * (______/|_____)_|_|_| \__)_____)\____)_| |_|
20 * (C)2013-2017 Semtech
21 *
22 * \endcode
23 *
24 * \author Miguel Luis ( Semtech )
25 *
26 * \author Gregory Cristian ( Semtech )
27 */
28 #include "board.h"
29 #include "gpio.h"
30 #include "timer.h"
31 #include "radio.h"
32
33 #if defined( REGION_AS923 )
34
35 #define RF_FREQUENCY 923000000 // Hz
36
37 #elif defined( REGION_AU915 )
38
39 #define RF_FREQUENCY 915000000 // Hz
40
41 #elif defined( REGION_CN470 )
42
43 #define RF_FREQUENCY 470000000 // Hz
44
45 #elif defined( REGION_CN779 )
46
47 #define RF_FREQUENCY 779000000 // Hz
48
49 #elif defined( REGION_EU433 )
50
51 #define RF_FREQUENCY 433000000 // Hz
52
53 #elif defined( REGION_EU868 )
54
55 #define RF_FREQUENCY 868000000 // Hz
56
57 #elif defined( REGION_KR920 )
58
59 #define RF_FREQUENCY 920000000 // Hz
60
61 #elif defined( REGION_IN865 )
62
63 #define RF_FREQUENCY 865000000 // Hz
64
65 #elif defined( REGION_US915 )
66
67 #define RF_FREQUENCY 915000000 // Hz
68
69 #elif defined( REGION_RU864 )
70
71 #define RF_FREQUENCY 864000000 // Hz
72
73 #else
74 #error "Please define a frequency band in the compiler options."
75 #endif
76
77 #if defined( USE_MODEM_LORA )
78
79 #define LORA_BANDWIDTH 0 // [0: 125 kHz,
80 // 1: 250 kHz,
81 // 2: 500 kHz,
82 // 3: Reserved]
83 #define LORA_SPREADING_FACTOR 10 // [SF7..SF12]
84 #define LORA_CODINGRATE 1 // [1: 4/5,
85 // 2: 4/6,
86 // 3: 4/7,
87 // 4: 4/8]
88 #define LORA_SYMBOL_TIMEOUT 5 // Symbols
89 #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
90 #define LORA_FIX_LENGTH_PAYLOAD_ON false
91 #define LORA_IQ_INVERSION_ON false
92
93 #elif defined( USE_MODEM_FSK )
94
95 #define FSK_DATARATE 50000 // bps
96 #define FSK_BANDWIDTH 50000 // Hz
97 #define FSK_AFC_BANDWIDTH 83333 // Hz
98 #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx
99 #define FSK_FIX_LENGTH_PAYLOAD_ON false
100
101 #else
102 #error "Please define a modem in the compiler options."
103 #endif
104
105 /*!
106 * Radio events function pointer
107 */
108 static RadioEvents_t RadioEvents;
109
110 /*!
111 * LED GPIO pins objects
112 */
113 extern Gpio_t Led1;
114
115 /*!
116 * \brief Function to be executed on Radio Rx Done event
117 */
118 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
119
120 /*!
121 * Main application entry point.
122 */
main(void)123 int main( void )
124 {
125 // Target board initialization
126 BoardInitMcu( );
127 BoardInitPeriph( );
128
129 // Radio initialization
130 RadioEvents.RxDone = OnRxDone;
131
132 Radio.Init( &RadioEvents );
133
134 Radio.SetChannel( RF_FREQUENCY );
135
136 #if defined( USE_MODEM_LORA )
137
138 Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
139 LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
140 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
141 0, true, 0, 0, LORA_IQ_INVERSION_ON, true );
142
143 Radio.SetMaxPayloadLength( MODEM_LORA, 255 );
144
145 #elif defined( USE_MODEM_FSK )
146
147 Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
148 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
149 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, true,
150 0, 0, false, true );
151
152 Radio.SetMaxPayloadLength( MODEM_FSK, 255 );
153
154 #else
155 #error "Please define a frequency band in the compiler options."
156 #endif
157
158 Radio.Rx( 0 ); // Continuous Rx
159
160 while( 1 )
161 {
162 BoardLowPowerHandler( );
163 // Process Radio IRQ
164 if( Radio.IrqProcess != NULL )
165 {
166 Radio.IrqProcess( );
167 }
168 }
169 }
170
OnRxDone(uint8_t * payload,uint16_t size,int16_t rssi,int8_t snr)171 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
172 {
173 static uint8_t ledState = 1;
174 // Toggle LED 1
175 ledState ^= 1;
176 GpioWrite( &Led1, ledState );
177 }
178