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  * \author    Marten Lootsma(TWTG) on behalf of Microchip/Atmel (c)2017
29  */
30 #include "board.h"
31 #include "gpio.h"
32 #include "timer.h"
33 #include "radio.h"
34 
35 #if defined( REGION_AS923 )
36 
37 #define RF_FREQUENCY                                923000000 // Hz
38 
39 #elif defined( REGION_AU915 )
40 
41 #define RF_FREQUENCY                                915000000 // Hz
42 
43 #elif defined( REGION_CN470 )
44 
45 #define RF_FREQUENCY                                470000000 // Hz
46 
47 #elif defined( REGION_CN779 )
48 
49 #define RF_FREQUENCY                                779000000 // Hz
50 
51 #elif defined( REGION_EU433 )
52 
53 #define RF_FREQUENCY                                433000000 // Hz
54 
55 #elif defined( REGION_EU868 )
56 
57 #define RF_FREQUENCY                                868000000 // Hz
58 
59 #elif defined( REGION_KR920 )
60 
61 #define RF_FREQUENCY                                920000000 // Hz
62 
63 #elif defined( REGION_IN865 )
64 
65 #define RF_FREQUENCY                                865000000 // Hz
66 
67 #elif defined( REGION_US915 )
68 
69 #define RF_FREQUENCY                                915000000 // Hz
70 
71 #elif defined( REGION_RU864 )
72 
73 #define RF_FREQUENCY                                864000000 // Hz
74 
75 #else
76     #error "Please define a frequency band in the compiler options."
77 #endif
78 
79 #if defined( USE_MODEM_LORA )
80 
81 #define LORA_BANDWIDTH                              0         // [0: 125 kHz,
82                                                               //  1: 250 kHz,
83                                                               //  2: 500 kHz,
84                                                               //  3: Reserved]
85 #define LORA_SPREADING_FACTOR                       10        // [SF7..SF12]
86 #define LORA_CODINGRATE                             1         // [1: 4/5,
87                                                               //  2: 4/6,
88                                                               //  3: 4/7,
89                                                               //  4: 4/8]
90 #define LORA_SYMBOL_TIMEOUT                         5         // Symbols
91 #define LORA_PREAMBLE_LENGTH                        8         // Same for Tx and Rx
92 #define LORA_FIX_LENGTH_PAYLOAD_ON                  false
93 #define LORA_IQ_INVERSION_ON                        false
94 
95 #elif defined( USE_MODEM_FSK )
96 
97 #define FSK_DATARATE                                50000     // bps
98 #define FSK_BANDWIDTH                               50000     // Hz
99 #define FSK_AFC_BANDWIDTH                           83333     // Hz
100 #define FSK_PREAMBLE_LENGTH                         5         // Same for Tx and Rx
101 #define FSK_FIX_LENGTH_PAYLOAD_ON                   false
102 
103 #else
104     #error "Please define a modem in the compiler options."
105 #endif
106 
107 /*!
108  * Radio events function pointer
109  */
110 static RadioEvents_t RadioEvents;
111 
112 /*!
113  * LED GPIO pins objects
114  */
115 extern Gpio_t Led1;
116 
117 /*!
118  * \brief Function to be executed on Radio Rx Done event
119  */
120 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
121 
122 /*!
123  * Main application entry point.
124  */
main(void)125 int main( void )
126 {
127     // Target board initialization
128     BoardInitMcu( );
129     BoardInitPeriph( );
130 
131     // Radio initialization
132     RadioEvents.RxDone = OnRxDone;
133 
134     Radio.Init( &RadioEvents );
135 
136     Radio.SetChannel( RF_FREQUENCY );
137 
138 #if defined( USE_MODEM_LORA )
139 
140     Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
141                                    LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
142                                    LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
143                                    0, true, 0, 0, LORA_IQ_INVERSION_ON, true );
144 
145     Radio.SetMaxPayloadLength( MODEM_LORA, 255 );
146 
147 #elif defined( USE_MODEM_FSK )
148 
149     Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
150                                   0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
151                                   0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, true,
152                                   0, 0, false, true );
153 
154     Radio.SetMaxPayloadLength( MODEM_FSK, 255 );
155 
156 #else
157     #error "Please define a frequency band in the compiler options."
158 #endif
159 
160     Radio.Rx( 0 ); // Continuous Rx
161 
162     while( 1 )
163     {
164         // Tick the RTC to execute callback in context of the main loop (in stead of the IRQ)
165         TimerProcess( );
166 
167         BoardLowPowerHandler( );
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