TX
Dependencies: mbed BufferedSerial SX1276GenericLib X_NUCLEO_IKS01A2
main.cpp@4:a0cfe0efcc5e, 2019-06-05 (annotated)
- Committer:
- TMRL123
- Date:
- Wed Jun 05 00:27:24 2019 +0000
- Revision:
- 4:a0cfe0efcc5e
- Parent:
- 0:a73914f20498
Last to actualy work
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TMRL123 | 0:a73914f20498 | 1 | /* Includes */ |
TMRL123 | 0:a73914f20498 | 2 | #include "mbed.h" /* Mbed include */ |
TMRL123 | 0:a73914f20498 | 3 | |
TMRL123 | 0:a73914f20498 | 4 | #include "XNucleoIKS01A2.h" /* Sensors include*/ |
TMRL123 | 0:a73914f20498 | 5 | |
TMRL123 | 0:a73914f20498 | 6 | /* LoRa includes */ |
TMRL123 | 0:a73914f20498 | 7 | #include "PinMap.h" |
TMRL123 | 0:a73914f20498 | 8 | #include "sx1276-mbed-hal.h" |
TMRL123 | 0:a73914f20498 | 9 | |
TMRL123 | 4:a0cfe0efcc5e | 10 | /* Serial communication include */ |
TMRL123 | 4:a0cfe0efcc5e | 11 | #include "BufferedSerial.h" |
TMRL123 | 4:a0cfe0efcc5e | 12 | |
TMRL123 | 0:a73914f20498 | 13 | /* LoRa definitions */ |
TMRL123 | 0:a73914f20498 | 14 | |
TMRL123 | 0:a73914f20498 | 15 | /* Set this flag to '1' to display debug messages on the console */ |
TMRL123 | 0:a73914f20498 | 16 | #define DEBUG_MESSAGE 1 |
TMRL123 | 0:a73914f20498 | 17 | |
TMRL123 | 0:a73914f20498 | 18 | /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ |
TMRL123 | 0:a73914f20498 | 19 | #define USE_MODEM_LORA 1 |
TMRL123 | 0:a73914f20498 | 20 | #define USE_MODEM_FSK !USE_MODEM_LORA |
TMRL123 | 0:a73914f20498 | 21 | #define RF_FREQUENCY RF_FREQUENCY_915_0 // Hz |
TMRL123 | 0:a73914f20498 | 22 | #define TX_OUTPUT_POWER 14 // 14 dBm |
TMRL123 | 0:a73914f20498 | 23 | |
TMRL123 | 0:a73914f20498 | 24 | #if USE_MODEM_LORA == 1 |
TMRL123 | 0:a73914f20498 | 25 | |
TMRL123 | 0:a73914f20498 | 26 | #define LORA_BANDWIDTH 125000 // LoRa default, details in SX1276::BandwidthMap |
TMRL123 | 0:a73914f20498 | 27 | #define LORA_SPREADING_FACTOR LORA_SF7 |
TMRL123 | 0:a73914f20498 | 28 | #define LORA_CODINGRATE LORA_ERROR_CODING_RATE_4_5 |
TMRL123 | 0:a73914f20498 | 29 | |
TMRL123 | 0:a73914f20498 | 30 | #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx |
TMRL123 | 0:a73914f20498 | 31 | #define LORA_SYMBOL_TIMEOUT 5 // Symbols |
TMRL123 | 0:a73914f20498 | 32 | #define LORA_FIX_LENGTH_PAYLOAD_ON false |
TMRL123 | 0:a73914f20498 | 33 | #define LORA_FHSS_ENABLED false |
TMRL123 | 0:a73914f20498 | 34 | #define LORA_NB_SYMB_HOP 4 |
TMRL123 | 0:a73914f20498 | 35 | #define LORA_IQ_INVERSION_ON false |
TMRL123 | 0:a73914f20498 | 36 | #define LORA_CRC_ENABLED true |
TMRL123 | 0:a73914f20498 | 37 | |
TMRL123 | 0:a73914f20498 | 38 | #endif |
TMRL123 | 0:a73914f20498 | 39 | |
TMRL123 | 0:a73914f20498 | 40 | |
TMRL123 | 4:a0cfe0efcc5e | 41 | #define RX_TIMEOUT_VALUE 0 // In ms |
TMRL123 | 4:a0cfe0efcc5e | 42 | #define TX_TIMEOUT_VALUE 1000000 // In ms |
TMRL123 | 0:a73914f20498 | 43 | |
TMRL123 | 0:a73914f20498 | 44 | //#define BUFFER_SIZE 32 // Define the payload size here |
TMRL123 | 4:a0cfe0efcc5e | 45 | #define BUFFER_SIZE 64 // Define the payload size here |
TMRL123 | 0:a73914f20498 | 46 | |
TMRL123 | 0:a73914f20498 | 47 | /* Sensors instances */ |
TMRL123 | 0:a73914f20498 | 48 | |
TMRL123 | 0:a73914f20498 | 49 | /* Instantiate the expansion board */ |
TMRL123 | 0:a73914f20498 | 50 | static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); |
TMRL123 | 0:a73914f20498 | 51 | |
TMRL123 | 0:a73914f20498 | 52 | /* Retrieve the composing elements of the expansion board */ |
TMRL123 | 0:a73914f20498 | 53 | static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer; |
TMRL123 | 0:a73914f20498 | 54 | static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; |
TMRL123 | 0:a73914f20498 | 55 | static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor; |
TMRL123 | 0:a73914f20498 | 56 | static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro; |
TMRL123 | 0:a73914f20498 | 57 | static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer; |
TMRL123 | 0:a73914f20498 | 58 | |
TMRL123 | 4:a0cfe0efcc5e | 59 | typedef struct { |
TMRL123 | 4:a0cfe0efcc5e | 60 | float p; // Pressure |
TMRL123 | 4:a0cfe0efcc5e | 61 | float temperatureHTS221; // Temperature from HTS221 |
TMRL123 | 4:a0cfe0efcc5e | 62 | float humidity; // Humidity |
TMRL123 | 4:a0cfe0efcc5e | 63 | float temperatureLPS22HB; // Temperature from LPS22HB |
TMRL123 | 4:a0cfe0efcc5e | 64 | int32_t w[3]; // Angular velocity |
TMRL123 | 4:a0cfe0efcc5e | 65 | int32_t a[3]; // Acceleration of the accelerometer LSM303AGR |
TMRL123 | 4:a0cfe0efcc5e | 66 | int32_t ag[3]; // Acceleration of the accelerometer and gyroscope LSM6DSL |
TMRL123 | 4:a0cfe0efcc5e | 67 | int32_t m [3]; // Heading |
TMRL123 | 4:a0cfe0efcc5e | 68 | }Data; // Data struct |
TMRL123 | 4:a0cfe0efcc5e | 69 | |
TMRL123 | 4:a0cfe0efcc5e | 70 | Data data; |
TMRL123 | 0:a73914f20498 | 71 | |
TMRL123 | 0:a73914f20498 | 72 | /* LoRa modem instances and configurations */ |
TMRL123 | 0:a73914f20498 | 73 | |
TMRL123 | 0:a73914f20498 | 74 | static RadioEvents_t RadioEvents; // Calback functions struct |
TMRL123 | 0:a73914f20498 | 75 | |
TMRL123 | 4:a0cfe0efcc5e | 76 | SX1276Generic *Radio; // Defenition of a Radio object |
TMRL123 | 0:a73914f20498 | 77 | |
TMRL123 | 4:a0cfe0efcc5e | 78 | /* Configuration function */ |
TMRL123 | 0:a73914f20498 | 79 | void SystemClock_Config(void); |
TMRL123 | 0:a73914f20498 | 80 | |
TMRL123 | 4:a0cfe0efcc5e | 81 | bool transmited = true;// Flag to indicate the and of transmission |
TMRL123 | 0:a73914f20498 | 82 | |
TMRL123 | 0:a73914f20498 | 83 | /* Callback functions prototypes */ |
TMRL123 | 4:a0cfe0efcc5e | 84 | |
TMRL123 | 4:a0cfe0efcc5e | 85 | // Brief Function to be executed on Radio Tx Done event |
TMRL123 | 0:a73914f20498 | 86 | void OnTxDone(void *radio, void *userThisPtr, void *userData); |
TMRL123 | 0:a73914f20498 | 87 | |
TMRL123 | 4:a0cfe0efcc5e | 88 | // Brief Function to be executed on Radio Rx Done event |
TMRL123 | 0:a73914f20498 | 89 | void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ); |
TMRL123 | 0:a73914f20498 | 90 | |
TMRL123 | 4:a0cfe0efcc5e | 91 | // Brief Function executed on Radio Tx Timeout event |
TMRL123 | 0:a73914f20498 | 92 | void OnTxTimeout(void *radio, void *userThisPtr, void *userData); |
TMRL123 | 0:a73914f20498 | 93 | |
TMRL123 | 4:a0cfe0efcc5e | 94 | // Brief Function executed on Radio Rx Timeout event |
TMRL123 | 0:a73914f20498 | 95 | void OnRxTimeout(void *radio, void *userThisPtr, void *userData); |
TMRL123 | 0:a73914f20498 | 96 | |
TMRL123 | 4:a0cfe0efcc5e | 97 | // Brief Function executed on Radio Rx Error event |
TMRL123 | 0:a73914f20498 | 98 | void OnRxError(void *radio, void *userThisPtr, void *userData); |
TMRL123 | 0:a73914f20498 | 99 | |
TMRL123 | 4:a0cfe0efcc5e | 100 | // Brief Function executed on Radio Fhss Change Channel event |
TMRL123 | 0:a73914f20498 | 101 | void OnFhssChangeChannel(void *radio, void *userThisPtr, void *userData, uint8_t channelIndex); |
TMRL123 | 0:a73914f20498 | 102 | |
TMRL123 | 0:a73914f20498 | 103 | /* Serial communication to debug program */ |
TMRL123 | 4:a0cfe0efcc5e | 104 | BufferedSerial *ser; |
TMRL123 | 0:a73914f20498 | 105 | |
TMRL123 | 0:a73914f20498 | 106 | int main() { |
TMRL123 | 4:a0cfe0efcc5e | 107 | SystemClock_Config(); /* Synchronize clock for TX and RX boards*/ |
TMRL123 | 0:a73914f20498 | 108 | |
TMRL123 | 4:a0cfe0efcc5e | 109 | /* Serial configuration */ |
TMRL123 | 4:a0cfe0efcc5e | 110 | if (DEBUG_MESSAGE) { |
TMRL123 | 4:a0cfe0efcc5e | 111 | ser = new BufferedSerial(USBTX, USBRX); |
TMRL123 | 4:a0cfe0efcc5e | 112 | ser->baud(115200); |
TMRL123 | 4:a0cfe0efcc5e | 113 | ser->format(8); |
TMRL123 | 4:a0cfe0efcc5e | 114 | } |
TMRL123 | 4:a0cfe0efcc5e | 115 | |
TMRL123 | 4:a0cfe0efcc5e | 116 | /* General Header*/ |
TMRL123 | 4:a0cfe0efcc5e | 117 | if (DEBUG_MESSAGE) |
TMRL123 | 4:a0cfe0efcc5e | 118 | ser->printf("Telemetry Rx inicial version program\r\n\r\n"); |
TMRL123 | 0:a73914f20498 | 119 | |
TMRL123 | 0:a73914f20498 | 120 | uint8_t id; //Sensor id parameter for debug purpose |
TMRL123 | 0:a73914f20498 | 121 | |
TMRL123 | 0:a73914f20498 | 122 | /* Enable all sensors */ |
TMRL123 | 0:a73914f20498 | 123 | hum_temp->enable(); |
TMRL123 | 0:a73914f20498 | 124 | press_temp->enable(); |
TMRL123 | 0:a73914f20498 | 125 | magnetometer->enable(); |
TMRL123 | 0:a73914f20498 | 126 | accelerometer->enable(); |
TMRL123 | 0:a73914f20498 | 127 | acc_gyro->enable_x(); |
TMRL123 | 0:a73914f20498 | 128 | acc_gyro->enable_g(); |
TMRL123 | 0:a73914f20498 | 129 | |
TMRL123 | 4:a0cfe0efcc5e | 130 | if (DEBUG_MESSAGE) { |
TMRL123 | 4:a0cfe0efcc5e | 131 | ser->printf("\r\n--- Starting the sensors ---\r\n"); |
TMRL123 | 4:a0cfe0efcc5e | 132 | |
TMRL123 | 4:a0cfe0efcc5e | 133 | hum_temp->read_id(&id); |
TMRL123 | 4:a0cfe0efcc5e | 134 | ser->printf("HTS221 humidity & temperature = 0x%X\r\n", id); |
TMRL123 | 4:a0cfe0efcc5e | 135 | press_temp->read_id(&id); |
TMRL123 | 4:a0cfe0efcc5e | 136 | ser->printf("LPS22HB pressure & temperature = 0x%X\r\n", id); |
TMRL123 | 4:a0cfe0efcc5e | 137 | magnetometer->read_id(&id); |
TMRL123 | 4:a0cfe0efcc5e | 138 | ser->printf("LSM303AGR magnetometer = 0x%X\r\n", id); |
TMRL123 | 4:a0cfe0efcc5e | 139 | accelerometer->read_id(&id); |
TMRL123 | 4:a0cfe0efcc5e | 140 | ser->printf("LSM303AGR accelerometer = 0x%X\r\n", id); |
TMRL123 | 4:a0cfe0efcc5e | 141 | acc_gyro->read_id(&id); |
TMRL123 | 4:a0cfe0efcc5e | 142 | ser->printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id); |
TMRL123 | 4:a0cfe0efcc5e | 143 | |
TMRL123 | 4:a0cfe0efcc5e | 144 | ser->printf("\r\n"); |
TMRL123 | 4:a0cfe0efcc5e | 145 | } |
TMRL123 | 4:a0cfe0efcc5e | 146 | /* Radio setup */ |
TMRL123 | 4:a0cfe0efcc5e | 147 | if (DEBUG_MESSAGE) |
TMRL123 | 4:a0cfe0efcc5e | 148 | ser->printf("\r\n--- Starting the modem LoRa ---\r\n"); |
TMRL123 | 0:a73914f20498 | 149 | |
TMRL123 | 0:a73914f20498 | 150 | Radio = new SX1276Generic(NULL, MURATA_SX1276, |
TMRL123 | 0:a73914f20498 | 151 | LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, |
TMRL123 | 0:a73914f20498 | 152 | LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5, |
TMRL123 | 0:a73914f20498 | 153 | LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO); |
TMRL123 | 4:a0cfe0efcc5e | 154 | if (DEBUG_MESSAGE) { |
TMRL123 | 4:a0cfe0efcc5e | 155 | ser->printf("SX1276 Simple transmission aplication\r\n" ); |
TMRL123 | 4:a0cfe0efcc5e | 156 | ser->printf("Frequency: %.1f\r\n", (double)RF_FREQUENCY/1000000.0); |
TMRL123 | 4:a0cfe0efcc5e | 157 | ser->printf("TXPower: %d dBm\r\n", TX_OUTPUT_POWER); |
TMRL123 | 4:a0cfe0efcc5e | 158 | ser->printf("Bandwidth: %d Hz\r\n", LORA_BANDWIDTH); |
TMRL123 | 4:a0cfe0efcc5e | 159 | ser->printf("Spreading factor: SF%d\r\n", LORA_SPREADING_FACTOR); |
TMRL123 | 4:a0cfe0efcc5e | 160 | } |
TMRL123 | 0:a73914f20498 | 161 | // Initialize Radio driver |
TMRL123 | 0:a73914f20498 | 162 | RadioEvents.TxDone = OnTxDone; |
TMRL123 | 0:a73914f20498 | 163 | RadioEvents.RxDone = OnRxDone; |
TMRL123 | 0:a73914f20498 | 164 | RadioEvents.RxError = OnRxError; |
TMRL123 | 0:a73914f20498 | 165 | RadioEvents.TxTimeout = OnTxTimeout; |
TMRL123 | 4:a0cfe0efcc5e | 166 | RadioEvents.RxTimeout = OnRxTimeout; |
TMRL123 | 4:a0cfe0efcc5e | 167 | |
TMRL123 | 0:a73914f20498 | 168 | while (Radio->Init( &RadioEvents ) == false) { |
TMRL123 | 4:a0cfe0efcc5e | 169 | if (DEBUG_MESSAGE) |
TMRL123 | 4:a0cfe0efcc5e | 170 | ser->printf("Radio could not be detected!\r\n"); |
TMRL123 | 0:a73914f20498 | 171 | wait( 1 ); |
TMRL123 | 0:a73914f20498 | 172 | } |
TMRL123 | 0:a73914f20498 | 173 | |
TMRL123 | 4:a0cfe0efcc5e | 174 | // Display the board type |
TMRL123 | 0:a73914f20498 | 175 | switch(Radio->DetectBoardType()) { |
TMRL123 | 0:a73914f20498 | 176 | case SX1276MB1LAS: |
TMRL123 | 0:a73914f20498 | 177 | if (DEBUG_MESSAGE) |
TMRL123 | 4:a0cfe0efcc5e | 178 | ser->printf(" > Board Type: SX1276MB1LAS <\r\n"); |
TMRL123 | 0:a73914f20498 | 179 | break; |
TMRL123 | 0:a73914f20498 | 180 | case SX1276MB1MAS: |
TMRL123 | 0:a73914f20498 | 181 | if (DEBUG_MESSAGE) |
TMRL123 | 4:a0cfe0efcc5e | 182 | ser->printf(" > Board Type: SX1276MB1LAS <\r\n"); |
TMRL123 | 0:a73914f20498 | 183 | case MURATA_SX1276: |
TMRL123 | 0:a73914f20498 | 184 | if (DEBUG_MESSAGE) |
TMRL123 | 4:a0cfe0efcc5e | 185 | ser->printf(" > Board Type: MURATA_SX1276_STM32L0 <\r\n"); |
TMRL123 | 0:a73914f20498 | 186 | break; |
TMRL123 | 0:a73914f20498 | 187 | case RFM95_SX1276: |
TMRL123 | 0:a73914f20498 | 188 | if (DEBUG_MESSAGE) |
TMRL123 | 4:a0cfe0efcc5e | 189 | ser->printf(" > HopeRF RFM95xx <\r\n"); |
TMRL123 | 0:a73914f20498 | 190 | break; |
TMRL123 | 0:a73914f20498 | 191 | default: |
TMRL123 | 4:a0cfe0efcc5e | 192 | if (DEBUG_MESSAGE) |
TMRL123 | 4:a0cfe0efcc5e | 193 | ser->printf(" > Board Type: unknown <\r\n"); |
TMRL123 | 0:a73914f20498 | 194 | } |
TMRL123 | 0:a73914f20498 | 195 | |
TMRL123 | 4:a0cfe0efcc5e | 196 | Radio->SetChannel(RF_FREQUENCY ); // Sets the frequency of the communication |
TMRL123 | 0:a73914f20498 | 197 | |
TMRL123 | 4:a0cfe0efcc5e | 198 | // Debug message of the state of fhss |
TMRL123 | 4:a0cfe0efcc5e | 199 | if (LORA_FHSS_ENABLED) { |
TMRL123 | 4:a0cfe0efcc5e | 200 | if (DEBUG_MESSAGE) |
TMRL123 | 4:a0cfe0efcc5e | 201 | ser->printf(" > LORA FHSS Mode <\r\n"); |
TMRL123 | 4:a0cfe0efcc5e | 202 | } |
TMRL123 | 4:a0cfe0efcc5e | 203 | if (!LORA_FHSS_ENABLED) { |
TMRL123 | 4:a0cfe0efcc5e | 204 | if (DEBUG_MESSAGE) |
TMRL123 | 4:a0cfe0efcc5e | 205 | ser->printf(" > LORA Mode <\r\n"); |
TMRL123 | 4:a0cfe0efcc5e | 206 | } |
TMRL123 | 4:a0cfe0efcc5e | 207 | |
TMRL123 | 4:a0cfe0efcc5e | 208 | // Sets the configuration of the transmission |
TMRL123 | 0:a73914f20498 | 209 | Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, |
TMRL123 | 0:a73914f20498 | 210 | LORA_SPREADING_FACTOR, LORA_CODINGRATE, |
TMRL123 | 0:a73914f20498 | 211 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, |
TMRL123 | 0:a73914f20498 | 212 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
TMRL123 | 0:a73914f20498 | 213 | LORA_IQ_INVERSION_ON, 2000 ); |
TMRL123 | 0:a73914f20498 | 214 | |
TMRL123 | 4:a0cfe0efcc5e | 215 | // Sets the configuration of the reception |
TMRL123 | 0:a73914f20498 | 216 | Radio->SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, |
TMRL123 | 0:a73914f20498 | 217 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, |
TMRL123 | 0:a73914f20498 | 218 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, |
TMRL123 | 0:a73914f20498 | 219 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
TMRL123 | 0:a73914f20498 | 220 | LORA_IQ_INVERSION_ON, true ); |
TMRL123 | 0:a73914f20498 | 221 | |
TMRL123 | 4:a0cfe0efcc5e | 222 | Radio->Tx(TX_TIMEOUT_VALUE); // Puts the device in transmission mode for a long period |
TMRL123 | 0:a73914f20498 | 223 | |
TMRL123 | 0:a73914f20498 | 224 | while(1) { |
TMRL123 | 4:a0cfe0efcc5e | 225 | |
TMRL123 | 4:a0cfe0efcc5e | 226 | press_temp->get_pressure(&data.p); // Get the pressure |
TMRL123 | 4:a0cfe0efcc5e | 227 | press_temp->get_temperature(&data.temperatureLPS22HB); // Get temperature from LPS22HB |
TMRL123 | 4:a0cfe0efcc5e | 228 | accelerometer->get_x_axes(data.a);// Get the acceleration |
TMRL123 | 4:a0cfe0efcc5e | 229 | acc_gyro->get_x_axes(data.ag);// Get the acceleration |
TMRL123 | 4:a0cfe0efcc5e | 230 | acc_gyro->get_g_axes(data.w);// Get the angular velocity |
TMRL123 | 4:a0cfe0efcc5e | 231 | magnetometer->get_m_axes(data.m); // Get the magnetometer heading |
TMRL123 | 4:a0cfe0efcc5e | 232 | hum_temp->get_temperature(&data.temperatureHTS221); // Get temperature from HTS221 |
TMRL123 | 4:a0cfe0efcc5e | 233 | hum_temp->get_humidity(&data.humidity); // Get humidity |
TMRL123 | 0:a73914f20498 | 234 | |
TMRL123 | 4:a0cfe0efcc5e | 235 | // Only sends a new packet when the device already have transmited the previous one |
TMRL123 | 0:a73914f20498 | 236 | if (transmited==true) { |
TMRL123 | 0:a73914f20498 | 237 | transmited = false; |
TMRL123 | 0:a73914f20498 | 238 | wait_ms(10); |
TMRL123 | 4:a0cfe0efcc5e | 239 | Radio->Send( &data, sizeof(data) ); |
TMRL123 | 0:a73914f20498 | 240 | } |
TMRL123 | 0:a73914f20498 | 241 | } |
TMRL123 | 0:a73914f20498 | 242 | } |
TMRL123 | 0:a73914f20498 | 243 | |
TMRL123 | 0:a73914f20498 | 244 | void SystemClock_Config(void) |
TMRL123 | 0:a73914f20498 | 245 | { |
TMRL123 | 0:a73914f20498 | 246 | #ifdef B_L072Z_LRWAN1_LORA |
TMRL123 | 0:a73914f20498 | 247 | /* |
TMRL123 | 0:a73914f20498 | 248 | * The L072Z_LRWAN1_LORA clock setup is somewhat differnt from the Nucleo board. |
TMRL123 | 0:a73914f20498 | 249 | * It has no LSE. |
TMRL123 | 0:a73914f20498 | 250 | */ |
TMRL123 | 0:a73914f20498 | 251 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
TMRL123 | 0:a73914f20498 | 252 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
TMRL123 | 0:a73914f20498 | 253 | |
TMRL123 | 0:a73914f20498 | 254 | /* Enable HSE Oscillator and Activate PLL with HSE as source */ |
TMRL123 | 0:a73914f20498 | 255 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; |
TMRL123 | 0:a73914f20498 | 256 | RCC_OscInitStruct.HSEState = RCC_HSE_OFF; |
TMRL123 | 0:a73914f20498 | 257 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
TMRL123 | 0:a73914f20498 | 258 | RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; |
TMRL123 | 0:a73914f20498 | 259 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
TMRL123 | 0:a73914f20498 | 260 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
TMRL123 | 0:a73914f20498 | 261 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_6; |
TMRL123 | 0:a73914f20498 | 262 | RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_3; |
TMRL123 | 0:a73914f20498 | 263 | |
TMRL123 | 0:a73914f20498 | 264 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
TMRL123 | 0:a73914f20498 | 265 | // Error_Handler(); |
TMRL123 | 0:a73914f20498 | 266 | } |
TMRL123 | 0:a73914f20498 | 267 | |
TMRL123 | 0:a73914f20498 | 268 | /* Set Voltage scale1 as MCU will run at 32MHz */ |
TMRL123 | 0:a73914f20498 | 269 | __HAL_RCC_PWR_CLK_ENABLE(); |
TMRL123 | 0:a73914f20498 | 270 | __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
TMRL123 | 0:a73914f20498 | 271 | |
TMRL123 | 0:a73914f20498 | 272 | /* Poll VOSF bit of in PWR_CSR. Wait until it is reset to 0 */ |
TMRL123 | 0:a73914f20498 | 273 | while (__HAL_PWR_GET_FLAG(PWR_FLAG_VOS) != RESET) {}; |
TMRL123 | 0:a73914f20498 | 274 | |
TMRL123 | 0:a73914f20498 | 275 | /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 |
TMRL123 | 0:a73914f20498 | 276 | clocks dividers */ |
TMRL123 | 0:a73914f20498 | 277 | RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); |
TMRL123 | 0:a73914f20498 | 278 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
TMRL123 | 0:a73914f20498 | 279 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
TMRL123 | 0:a73914f20498 | 280 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; |
TMRL123 | 0:a73914f20498 | 281 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
TMRL123 | 0:a73914f20498 | 282 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { |
TMRL123 | 0:a73914f20498 | 283 | // Error_Handler(); |
TMRL123 | 0:a73914f20498 | 284 | } |
TMRL123 | 0:a73914f20498 | 285 | #endif |
TMRL123 | 0:a73914f20498 | 286 | } |
TMRL123 | 0:a73914f20498 | 287 | |
TMRL123 | 0:a73914f20498 | 288 | /* Helper function for printing floats & doubles */ |
TMRL123 | 0:a73914f20498 | 289 | |
TMRL123 | 0:a73914f20498 | 290 | |
TMRL123 | 0:a73914f20498 | 291 | void OnTxDone(void *radio, void *userThisPtr, void *userData) |
TMRL123 | 0:a73914f20498 | 292 | { |
TMRL123 | 0:a73914f20498 | 293 | Radio->Sleep( ); |
TMRL123 | 0:a73914f20498 | 294 | transmited = true; |
TMRL123 | 0:a73914f20498 | 295 | if (DEBUG_MESSAGE) { |
TMRL123 | 4:a0cfe0efcc5e | 296 | ser->printf("> OnTxDone\r\n"); |
TMRL123 | 4:a0cfe0efcc5e | 297 | ser->printf("I transmited %d mg, %d mg, %d mg, %d mg, %d mg, %d mg, %d mdps, %d mdps, %d mdps\r\n", data.a[0], data.a[1], data.a[2], data.ag[0], data.ag[1], data.ag[2], data.w[0], data.w[1], data.w[2]); |
TMRL123 | 4:a0cfe0efcc5e | 298 | ser->printf("and %d mG, %d mG, %d mG, %g %%, %g C, %g C, %g mBar\r\n", data.m[0], data.m[1], data.m[2], data.humidity, data.temperatureHTS221, data.temperatureLPS22HB, data.p); |
TMRL123 | 0:a73914f20498 | 299 | } |
TMRL123 | 0:a73914f20498 | 300 | } |
TMRL123 | 0:a73914f20498 | 301 | |
TMRL123 | 0:a73914f20498 | 302 | void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) |
TMRL123 | 0:a73914f20498 | 303 | { |
TMRL123 | 0:a73914f20498 | 304 | Radio->Sleep( ); |
TMRL123 | 0:a73914f20498 | 305 | if (DEBUG_MESSAGE) |
TMRL123 | 4:a0cfe0efcc5e | 306 | ser->printf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d\r\n", rssi, snr); |
TMRL123 | 0:a73914f20498 | 307 | } |
TMRL123 | 0:a73914f20498 | 308 | |
TMRL123 | 0:a73914f20498 | 309 | void OnTxTimeout(void *radio, void *userThisPtr, void *userData) |
TMRL123 | 0:a73914f20498 | 310 | { |
TMRL123 | 0:a73914f20498 | 311 | Radio->Sleep( ); |
TMRL123 | 0:a73914f20498 | 312 | if(DEBUG_MESSAGE) |
TMRL123 | 4:a0cfe0efcc5e | 313 | ser->printf("> OnTxTimeout\r\n"); |
TMRL123 | 0:a73914f20498 | 314 | } |
TMRL123 | 0:a73914f20498 | 315 | |
TMRL123 | 0:a73914f20498 | 316 | void OnRxTimeout(void *radio, void *userThisPtr, void *userData) |
TMRL123 | 0:a73914f20498 | 317 | { |
TMRL123 | 0:a73914f20498 | 318 | Radio->Sleep( ); |
TMRL123 | 0:a73914f20498 | 319 | if (DEBUG_MESSAGE) |
TMRL123 | 4:a0cfe0efcc5e | 320 | ser->printf("> OnRxTimeout\r\n"); |
TMRL123 | 0:a73914f20498 | 321 | } |
TMRL123 | 0:a73914f20498 | 322 | |
TMRL123 | 0:a73914f20498 | 323 | void OnRxError(void *radio, void *userThisPtr, void *userData) |
TMRL123 | 0:a73914f20498 | 324 | { |
TMRL123 | 0:a73914f20498 | 325 | Radio->Sleep( ); |
TMRL123 | 0:a73914f20498 | 326 | if (DEBUG_MESSAGE) |
TMRL123 | 4:a0cfe0efcc5e | 327 | ser->printf("> OnRxError\r\n"); |
TMRL123 | 0:a73914f20498 | 328 | } |