d

Dependencies:   mbed BufferedSerial SX1276GenericLib2

Committer:
TMRL123
Date:
Wed Jun 05 00:33:35 2019 +0000
Revision:
0:fa750b405a24
works

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TMRL123 0:fa750b405a24 1 /* Includes */
TMRL123 0:fa750b405a24 2 #include "mbed.h" /* Mbed include */
TMRL123 0:fa750b405a24 3
TMRL123 0:fa750b405a24 4 /* Lora includes */
TMRL123 0:fa750b405a24 5 #include "PinMap.h"
TMRL123 0:fa750b405a24 6 #include "sx1276-mbed-hal.h"
TMRL123 0:fa750b405a24 7
TMRL123 0:fa750b405a24 8 /* Serial communication include */
TMRL123 0:fa750b405a24 9 #include "BufferedSerial.h"
TMRL123 0:fa750b405a24 10
TMRL123 0:fa750b405a24 11 /* Set this flag to '1' to display debug messages on the console */
TMRL123 0:fa750b405a24 12 #define DEBUG_MESSAGE 1
TMRL123 0:fa750b405a24 13
TMRL123 0:fa750b405a24 14 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */
TMRL123 0:fa750b405a24 15 #define USE_MODEM_LORA 1
TMRL123 0:fa750b405a24 16 #define USE_MODEM_FSK !USE_MODEM_LORA
TMRL123 0:fa750b405a24 17 #define RF_FREQUENCY RF_FREQUENCY_915_0 // Hz
TMRL123 0:fa750b405a24 18 #define TX_OUTPUT_POWER 14 // 14 dBm
TMRL123 0:fa750b405a24 19
TMRL123 0:fa750b405a24 20 #if USE_MODEM_LORA == 1
TMRL123 0:fa750b405a24 21
TMRL123 0:fa750b405a24 22 #define LORA_BANDWIDTH 125000 // LoRa default, details in SX1276::BandwidthMap
TMRL123 0:fa750b405a24 23 #define LORA_SPREADING_FACTOR LORA_SF7
TMRL123 0:fa750b405a24 24 #define LORA_CODINGRATE LORA_ERROR_CODING_RATE_4_5
TMRL123 0:fa750b405a24 25
TMRL123 0:fa750b405a24 26 #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
TMRL123 0:fa750b405a24 27 #define LORA_SYMBOL_TIMEOUT 5 // Symbols
TMRL123 0:fa750b405a24 28 #define LORA_FIX_LENGTH_PAYLOAD_ON false
TMRL123 0:fa750b405a24 29 #define LORA_FHSS_ENABLED false
TMRL123 0:fa750b405a24 30 #define LORA_NB_SYMB_HOP 4
TMRL123 0:fa750b405a24 31 #define LORA_IQ_INVERSION_ON false
TMRL123 0:fa750b405a24 32 #define LORA_CRC_ENABLED true
TMRL123 0:fa750b405a24 33
TMRL123 0:fa750b405a24 34 #endif
TMRL123 0:fa750b405a24 35
TMRL123 0:fa750b405a24 36 #define RX_TIMEOUT_VALUE 0 // In ms
TMRL123 0:fa750b405a24 37 #define TX_TIMEOUT_VALUE 1000000 // In ms
TMRL123 0:fa750b405a24 38
TMRL123 0:fa750b405a24 39 //#define BUFFER_SIZE 32 // Define the payload size here
TMRL123 0:fa750b405a24 40 #define BUFFER_SIZE 64 // Define the payload size here
TMRL123 0:fa750b405a24 41
TMRL123 0:fa750b405a24 42 typedef struct {
TMRL123 0:fa750b405a24 43 float p; // Pressure
TMRL123 0:fa750b405a24 44 float temperatureHTS221; // Temperature from HTS221
TMRL123 0:fa750b405a24 45 float humidity; // Humidity
TMRL123 0:fa750b405a24 46 float temperatureLPS22HB; // Temperature from LPS22HB
TMRL123 0:fa750b405a24 47 int32_t w[3]; // Angular velocity
TMRL123 0:fa750b405a24 48 int32_t a[3]; // Acceleration of the accelerometer LSM303AGR
TMRL123 0:fa750b405a24 49 int32_t ag[3]; // Acceleration of the accelerometer and gyroscope LSM6DSL
TMRL123 0:fa750b405a24 50 int32_t m [3]; // Heading
TMRL123 0:fa750b405a24 51 }Dados; // Data struct
TMRL123 0:fa750b405a24 52
TMRL123 0:fa750b405a24 53 Dados dados;
TMRL123 0:fa750b405a24 54
TMRL123 0:fa750b405a24 55 /* LoRa modem instances and configurations */
TMRL123 0:fa750b405a24 56
TMRL123 0:fa750b405a24 57 static RadioEvents_t RadioEvents; // Calback functions struct
TMRL123 0:fa750b405a24 58
TMRL123 0:fa750b405a24 59 SX1276Generic *Radio; // Definition of a Radio object
TMRL123 0:fa750b405a24 60
TMRL123 0:fa750b405a24 61 bool received = false; // Flag to indicate the end of reception
TMRL123 0:fa750b405a24 62
TMRL123 0:fa750b405a24 63 /* Configuration function */
TMRL123 0:fa750b405a24 64 void SystemClock_Config(void);
TMRL123 0:fa750b405a24 65
TMRL123 0:fa750b405a24 66 /* Callback functions prototypes */
TMRL123 0:fa750b405a24 67
TMRL123 0:fa750b405a24 68 // Brief Function to be executed on Radio Tx Done event
TMRL123 0:fa750b405a24 69 void OnTxDone(void *radio, void *userThisPtr, void *userData);
TMRL123 0:fa750b405a24 70
TMRL123 0:fa750b405a24 71 // Brief Function to be executed on Radio Rx Done event
TMRL123 0:fa750b405a24 72 void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
TMRL123 0:fa750b405a24 73
TMRL123 0:fa750b405a24 74 // Brief Function executed on Radio Tx Timeout event
TMRL123 0:fa750b405a24 75 void OnTxTimeout(void *radio, void *userThisPtr, void *userData);
TMRL123 0:fa750b405a24 76
TMRL123 0:fa750b405a24 77 // Brief Function executed on Radio Rx Timeout event
TMRL123 0:fa750b405a24 78 void OnRxTimeout(void *radio, void *userThisPtr, void *userData);
TMRL123 0:fa750b405a24 79
TMRL123 0:fa750b405a24 80 // Brief Function executed on Radio Rx Error event
TMRL123 0:fa750b405a24 81 void OnRxError(void *radio, void *userThisPtr, void *userData);
TMRL123 0:fa750b405a24 82
TMRL123 0:fa750b405a24 83 // Brief Function executed on Radio Fhss Change Channel event
TMRL123 0:fa750b405a24 84 void OnFhssChangeChannel(void *radio, void *userThisPtr, void *userData, uint8_t channelIndex);
TMRL123 0:fa750b405a24 85
TMRL123 0:fa750b405a24 86 // Brief Function executed on CAD Done event
TMRL123 0:fa750b405a24 87 void OnCadDone(void *radio, void *userThisPtr, void *userData);
TMRL123 0:fa750b405a24 88
TMRL123 0:fa750b405a24 89 /* Serial communication to debug program */
TMRL123 0:fa750b405a24 90 BufferedSerial *ser;
TMRL123 0:fa750b405a24 91
TMRL123 0:fa750b405a24 92 int main() {
TMRL123 0:fa750b405a24 93 SystemClock_Config(); /* Synchronize clock for TX and RX boards */
TMRL123 0:fa750b405a24 94
TMRL123 0:fa750b405a24 95 /* Serial configuration */
TMRL123 0:fa750b405a24 96 if (DEBUG_MESSAGE) {
TMRL123 0:fa750b405a24 97 ser = new BufferedSerial(USBTX, USBRX);
TMRL123 0:fa750b405a24 98 ser->baud(115200);
TMRL123 0:fa750b405a24 99 ser->format(8);
TMRL123 0:fa750b405a24 100 }
TMRL123 0:fa750b405a24 101
TMRL123 0:fa750b405a24 102 /* General Header*/
TMRL123 0:fa750b405a24 103 if (DEBUG_MESSAGE)
TMRL123 0:fa750b405a24 104 ser->printf("Telemetry Rx inicial version program\r\n\r\n");
TMRL123 0:fa750b405a24 105
TMRL123 0:fa750b405a24 106 Radio = new SX1276Generic(NULL, MURATA_SX1276,
TMRL123 0:fa750b405a24 107 LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET,
TMRL123 0:fa750b405a24 108 LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5,
TMRL123 0:fa750b405a24 109 LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO);
TMRL123 0:fa750b405a24 110
TMRL123 0:fa750b405a24 111 if (DEBUG_MESSAGE) {
TMRL123 0:fa750b405a24 112 ser->printf("SX1276 Simple receiver aplication\r\n" );
TMRL123 0:fa750b405a24 113 ser->printf("Frequency: %.1f\r\n", (double)RF_FREQUENCY/1000000.0);
TMRL123 0:fa750b405a24 114 ser->printf("TXPower: %d dBm\r\n", TX_OUTPUT_POWER);
TMRL123 0:fa750b405a24 115 ser->printf("Bandwidth: %d Hz\r\n", LORA_BANDWIDTH);
TMRL123 0:fa750b405a24 116 ser->printf("Spreading factor: SF%d\r\n", LORA_SPREADING_FACTOR);
TMRL123 0:fa750b405a24 117 }
TMRL123 0:fa750b405a24 118
TMRL123 0:fa750b405a24 119 // Initialize Radio driver
TMRL123 0:fa750b405a24 120 RadioEvents.TxDone = OnTxDone;
TMRL123 0:fa750b405a24 121 RadioEvents.RxDone = OnRxDone;
TMRL123 0:fa750b405a24 122 RadioEvents.RxError = OnRxError;
TMRL123 0:fa750b405a24 123 RadioEvents.TxTimeout = OnTxTimeout;
TMRL123 0:fa750b405a24 124 RadioEvents.RxTimeout = OnRxTimeout;
TMRL123 0:fa750b405a24 125
TMRL123 0:fa750b405a24 126 // Initializes the radio
TMRL123 0:fa750b405a24 127 while (Radio->Init( &RadioEvents ) == false) {
TMRL123 0:fa750b405a24 128 if (DEBUG_MESSAGE)
TMRL123 0:fa750b405a24 129 ser->printf("Radio could not be detected!\r\n");
TMRL123 0:fa750b405a24 130 wait( 1 );
TMRL123 0:fa750b405a24 131 }
TMRL123 0:fa750b405a24 132
TMRL123 0:fa750b405a24 133 // Display the board type
TMRL123 0:fa750b405a24 134 switch(Radio->DetectBoardType()) {
TMRL123 0:fa750b405a24 135 case SX1276MB1LAS:
TMRL123 0:fa750b405a24 136 if (DEBUG_MESSAGE)
TMRL123 0:fa750b405a24 137 ser->printf(" > Board Type: SX1276MB1LAS <\r\n");
TMRL123 0:fa750b405a24 138 break;
TMRL123 0:fa750b405a24 139 case SX1276MB1MAS:
TMRL123 0:fa750b405a24 140 if (DEBUG_MESSAGE)
TMRL123 0:fa750b405a24 141 ser->printf(" > Board Type: SX1276MB1LAS <\r\n");
TMRL123 0:fa750b405a24 142 case MURATA_SX1276:
TMRL123 0:fa750b405a24 143 if (DEBUG_MESSAGE)
TMRL123 0:fa750b405a24 144 ser->printf(" > Board Type: MURATA_SX1276_STM32L0 <\r\n");
TMRL123 0:fa750b405a24 145 break;
TMRL123 0:fa750b405a24 146 case RFM95_SX1276:
TMRL123 0:fa750b405a24 147 if (DEBUG_MESSAGE)
TMRL123 0:fa750b405a24 148 ser->printf(" > HopeRF RFM95xx <\r\n");
TMRL123 0:fa750b405a24 149 break;
TMRL123 0:fa750b405a24 150 default:
TMRL123 0:fa750b405a24 151 if (DEBUG_MESSAGE)
TMRL123 0:fa750b405a24 152 ser->printf(" > Board Type: unknown <\r\n");
TMRL123 0:fa750b405a24 153 }
TMRL123 0:fa750b405a24 154
TMRL123 0:fa750b405a24 155 Radio->SetChannel(RF_FREQUENCY ); // Sets the frequency of the communication
TMRL123 0:fa750b405a24 156
TMRL123 0:fa750b405a24 157 // Debug message of the state of fhss
TMRL123 0:fa750b405a24 158 if (LORA_FHSS_ENABLED) {
TMRL123 0:fa750b405a24 159 if (DEBUG_MESSAGE)
TMRL123 0:fa750b405a24 160 ser->printf(" > LORA FHSS Mode <\r\n");
TMRL123 0:fa750b405a24 161 }
TMRL123 0:fa750b405a24 162 if (!LORA_FHSS_ENABLED) {
TMRL123 0:fa750b405a24 163 if (DEBUG_MESSAGE)
TMRL123 0:fa750b405a24 164 ser->printf(" > LORA Mode <\r\n");
TMRL123 0:fa750b405a24 165 }
TMRL123 0:fa750b405a24 166 // Sets the configuration of the transmission
TMRL123 0:fa750b405a24 167 Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
TMRL123 0:fa750b405a24 168 LORA_SPREADING_FACTOR, LORA_CODINGRATE,
TMRL123 0:fa750b405a24 169 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
TMRL123 0:fa750b405a24 170 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
TMRL123 0:fa750b405a24 171 LORA_IQ_INVERSION_ON, 2000 );
TMRL123 0:fa750b405a24 172
TMRL123 0:fa750b405a24 173 // Sets the configuration of the reception
TMRL123 0:fa750b405a24 174 Radio->SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
TMRL123 0:fa750b405a24 175 LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
TMRL123 0:fa750b405a24 176 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
TMRL123 0:fa750b405a24 177 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
TMRL123 0:fa750b405a24 178 LORA_IQ_INVERSION_ON, true );
TMRL123 0:fa750b405a24 179 if (DEBUG_MESSAGE)
TMRL123 0:fa750b405a24 180 ser->printf("Starting Receive loop\r\n");
TMRL123 0:fa750b405a24 181
TMRL123 0:fa750b405a24 182 Radio->Rx(RX_TIMEOUT_VALUE); // Puts the device in reception mode continuously
TMRL123 0:fa750b405a24 183
TMRL123 0:fa750b405a24 184 while( 1 )
TMRL123 0:fa750b405a24 185 {
TMRL123 0:fa750b405a24 186 //After the receiving, puts the device again in receive mode
TMRL123 0:fa750b405a24 187 if (received == true) {
TMRL123 0:fa750b405a24 188 received = false;
TMRL123 0:fa750b405a24 189 Radio->Rx(RX_TIMEOUT_VALUE);
TMRL123 0:fa750b405a24 190 }
TMRL123 0:fa750b405a24 191 }
TMRL123 0:fa750b405a24 192
TMRL123 0:fa750b405a24 193 }
TMRL123 0:fa750b405a24 194
TMRL123 0:fa750b405a24 195
TMRL123 0:fa750b405a24 196 void SystemClock_Config(void)
TMRL123 0:fa750b405a24 197 {
TMRL123 0:fa750b405a24 198 #ifdef B_L072Z_LRWAN1_LORA
TMRL123 0:fa750b405a24 199 /*
TMRL123 0:fa750b405a24 200 * The L072Z_LRWAN1_LORA clock setup is somewhat differnt from the Nucleo board.
TMRL123 0:fa750b405a24 201 * It has no LSE.
TMRL123 0:fa750b405a24 202 */
TMRL123 0:fa750b405a24 203 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
TMRL123 0:fa750b405a24 204 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
TMRL123 0:fa750b405a24 205
TMRL123 0:fa750b405a24 206 /* Enable HSE Oscillator and Activate PLL with HSE as source */
TMRL123 0:fa750b405a24 207 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
TMRL123 0:fa750b405a24 208 RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
TMRL123 0:fa750b405a24 209 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
TMRL123 0:fa750b405a24 210 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
TMRL123 0:fa750b405a24 211 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
TMRL123 0:fa750b405a24 212 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
TMRL123 0:fa750b405a24 213 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_6;
TMRL123 0:fa750b405a24 214 RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_3;
TMRL123 0:fa750b405a24 215
TMRL123 0:fa750b405a24 216 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
TMRL123 0:fa750b405a24 217 // Error_Handler();
TMRL123 0:fa750b405a24 218 }
TMRL123 0:fa750b405a24 219
TMRL123 0:fa750b405a24 220 /* Set Voltage scale1 as MCU will run at 32MHz */
TMRL123 0:fa750b405a24 221 __HAL_RCC_PWR_CLK_ENABLE();
TMRL123 0:fa750b405a24 222 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
TMRL123 0:fa750b405a24 223
TMRL123 0:fa750b405a24 224 /* Poll VOSF bit of in PWR_CSR. Wait until it is reset to 0 */
TMRL123 0:fa750b405a24 225 while (__HAL_PWR_GET_FLAG(PWR_FLAG_VOS) != RESET) {};
TMRL123 0:fa750b405a24 226
TMRL123 0:fa750b405a24 227 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
TMRL123 0:fa750b405a24 228 clocks dividers */
TMRL123 0:fa750b405a24 229 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
TMRL123 0:fa750b405a24 230 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
TMRL123 0:fa750b405a24 231 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
TMRL123 0:fa750b405a24 232 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
TMRL123 0:fa750b405a24 233 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
TMRL123 0:fa750b405a24 234 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {
TMRL123 0:fa750b405a24 235 // Error_Handler();
TMRL123 0:fa750b405a24 236 }
TMRL123 0:fa750b405a24 237 #endif
TMRL123 0:fa750b405a24 238 }
TMRL123 0:fa750b405a24 239
TMRL123 0:fa750b405a24 240 void OnTxDone(void *radio, void *userThisPtr, void *userData)
TMRL123 0:fa750b405a24 241 {
TMRL123 0:fa750b405a24 242 Radio->Sleep( );
TMRL123 0:fa750b405a24 243 if (DEBUG_MESSAGE)
TMRL123 0:fa750b405a24 244 ser->printf("> OnTxDone\r\n");
TMRL123 0:fa750b405a24 245 }
TMRL123 0:fa750b405a24 246
TMRL123 0:fa750b405a24 247 void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
TMRL123 0:fa750b405a24 248 {
TMRL123 0:fa750b405a24 249 Radio->Sleep( );
TMRL123 0:fa750b405a24 250 received = true;
TMRL123 0:fa750b405a24 251 memcpy(&dados, payload, sizeof(dados));
TMRL123 0:fa750b405a24 252 if (DEBUG_MESSAGE) {
TMRL123 0:fa750b405a24 253 ser->printf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d\r\n", rssi, snr);
TMRL123 0:fa750b405a24 254 ser->printf("I received %d mg, %d mg, %d mg, %d mg, %d mg, %d mg, %d mdps, %d mdps, %d mdps\r\n", dados.a[0], dados.a[1], dados.a[2], dados.ag[0], dados.ag[1], dados.ag[2], dados.w[0], dados.w[1], dados.w[2]);
TMRL123 0:fa750b405a24 255 ser->printf("and %d mG, %d mG, %d mG, %g %%, %g C, %g C, %g mBar\r\n", dados.m[0], dados.m[1], dados.m[2], dados.humidity, dados.temperatureHTS221, dados.temperatureLPS22HB, dados.p);
TMRL123 0:fa750b405a24 256 }
TMRL123 0:fa750b405a24 257
TMRL123 0:fa750b405a24 258 }
TMRL123 0:fa750b405a24 259
TMRL123 0:fa750b405a24 260 void OnTxTimeout(void *radio, void *userThisPtr, void *userData)
TMRL123 0:fa750b405a24 261 {
TMRL123 0:fa750b405a24 262 Radio->Sleep( );
TMRL123 0:fa750b405a24 263 if(DEBUG_MESSAGE)
TMRL123 0:fa750b405a24 264 ser->printf("> OnTxTimeout\r\n");
TMRL123 0:fa750b405a24 265 }
TMRL123 0:fa750b405a24 266
TMRL123 0:fa750b405a24 267 void OnRxTimeout(void *radio, void *userThisPtr, void *userData)
TMRL123 0:fa750b405a24 268 {
TMRL123 0:fa750b405a24 269 Radio->Sleep( );
TMRL123 0:fa750b405a24 270 if (DEBUG_MESSAGE)
TMRL123 0:fa750b405a24 271 ser->printf("> OnRxTimeout\r\n");
TMRL123 0:fa750b405a24 272 }
TMRL123 0:fa750b405a24 273
TMRL123 0:fa750b405a24 274 void OnRxError(void *radio, void *userThisPtr, void *userData)
TMRL123 0:fa750b405a24 275 {
TMRL123 0:fa750b405a24 276 Radio->Sleep( );
TMRL123 0:fa750b405a24 277 received = true;
TMRL123 0:fa750b405a24 278 if (DEBUG_MESSAGE)
TMRL123 0:fa750b405a24 279 ser->printf("> OnRxError\r\n");
TMRL123 0:fa750b405a24 280 }