test
Dependencies: mbed BufferedSerial SX1276GenericLib X_NUCLEO_IKS01A2
main.cpp
- Committer:
- TMRL123
- Date:
- 2019-04-18
- Revision:
- 0:a73914f20498
- Child:
- 1:92160b13f3c3
- Child:
- 2:a0cfe0efcc5e
File content as of revision 0:a73914f20498:
/* Includes */ #include "mbed.h" /* Mbed include */ #include "XNucleoIKS01A2.h" /* Sensors include*/ /* LoRa includes */ #include "PinMap.h" #include "sx1276-mbed-hal.h" /* LoRa definitions */ /* Set this flag to '1' to display debug messages on the console */ #define DEBUG_MESSAGE 1 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ #define USE_MODEM_LORA 1 #define USE_MODEM_FSK !USE_MODEM_LORA #define RF_FREQUENCY RF_FREQUENCY_915_0 // Hz #define TX_OUTPUT_POWER 14 // 14 dBm #if USE_MODEM_LORA == 1 #define LORA_BANDWIDTH 125000 // LoRa default, details in SX1276::BandwidthMap #define LORA_SPREADING_FACTOR LORA_SF7 #define LORA_CODINGRATE LORA_ERROR_CODING_RATE_4_5 #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx #define LORA_SYMBOL_TIMEOUT 5 // Symbols #define LORA_FIX_LENGTH_PAYLOAD_ON false #define LORA_FHSS_ENABLED false #define LORA_NB_SYMB_HOP 4 #define LORA_IQ_INVERSION_ON false #define LORA_CRC_ENABLED true #endif #define RX_TIMEOUT_VALUE 3500 // in ms //#define BUFFER_SIZE 32 // Define the payload size here #define BUFFER_SIZE 512 // Define the payload size here /* Sensors instances */ /* Instantiate the expansion board */ static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); /* Retrieve the composing elements of the expansion board */ static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer; static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor; static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro; static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer; char buffer1[32], buffer2[32]; // buffers to help theprinting of doubles uint32_t dados[16]; //data vector /* LoRa modem instances and configurations */ static RadioEvents_t RadioEvents; // Calback functions struct SX1276Generic *Radio; //Defenition of a Radio object /*Configuration function*/ void SystemClock_Config(void); bool transmited = true; /* Callback functions prototypes */ void OnTxDone(void *radio, void *userThisPtr, void *userData); void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ); void OnTxTimeout(void *radio, void *userThisPtr, void *userData); void OnRxTimeout(void *radio, void *userThisPtr, void *userData); void OnRxError(void *radio, void *userThisPtr, void *userData); void OnFhssChangeChannel(void *radio, void *userThisPtr, void *userData, uint8_t channelIndex); void OnCadDone(void *radio, void *userThisPtr, void *userData); /* Serial communication to debug program */ Serial pc(USBTX,USBRX); int main() { /* General Header*/ pc.printf("Telemetry Tx inicial version program\r\n\r\n"); uint8_t id; //Sensor id parameter for debug purpose /* Enable all sensors */ hum_temp->enable(); press_temp->enable(); magnetometer->enable(); accelerometer->enable(); acc_gyro->enable_x(); acc_gyro->enable_g(); pc.printf("\r\n--- Starting the sensors ---\r\n"); hum_temp->read_id(&id); pc.printf("HTS221 humidity & temperature = 0x%X\r\n", id); press_temp->read_id(&id); pc.printf("LPS22HB pressure & temperature = 0x%X\r\n", id); magnetometer->read_id(&id); pc.printf("LSM303AGR magnetometer = 0x%X\r\n", id); accelerometer->read_id(&id); pc.printf("LSM303AGR accelerometer = 0x%X\r\n", id); acc_gyro->read_id(&id); pc.printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id); pc.printf("\r\n"); /* Radio setup */ pc.printf("\r\n--- Starting the modem LoRa ---\r\n"); Radio = new SX1276Generic(NULL, MURATA_SX1276, LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5, LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO); pc.printf("SX1276 Simple transmission aplication\r\n" ); pc.printf("Frequency: %.1f\r\n", (double)RF_FREQUENCY/1000000.0); pc.printf("TXPower: %d dBm\r\n", TX_OUTPUT_POWER); pc.printf("Bandwidth: %d Hz\r\n", LORA_BANDWIDTH); pc.printf("Spreading factor: SF%d\r\n", LORA_SPREADING_FACTOR); // Initialize Radio driver RadioEvents.TxDone = OnTxDone; RadioEvents.RxDone = OnRxDone; RadioEvents.RxError = OnRxError; RadioEvents.TxTimeout = OnTxTimeout; RadioEvents.RxTimeout = OnRxTimeout; while (Radio->Init( &RadioEvents ) == false) { pc.printf("Radio could not be detected!\r\n"); wait( 1 ); } switch(Radio->DetectBoardType()) { case SX1276MB1LAS: if (DEBUG_MESSAGE) pc.printf(" > Board Type: SX1276MB1LAS <\r\n"); break; case SX1276MB1MAS: if (DEBUG_MESSAGE) pc.printf(" > Board Type: SX1276MB1LAS <\r\n"); case MURATA_SX1276: if (DEBUG_MESSAGE) pc.printf(" > Board Type: MURATA_SX1276_STM32L0 <\r\n"); break; case RFM95_SX1276: if (DEBUG_MESSAGE) pc.printf(" > HopeRF RFM95xx <\r\n"); break; default: pc.printf(" > Board Type: unknown <\r\n"); } Radio->SetChannel(RF_FREQUENCY ); if (LORA_FHSS_ENABLED) pc.printf(" > LORA FHSS Mode <\r\n"); if (!LORA_FHSS_ENABLED) pc.printf(" > LORA Mode <\r\n"); pc.printf("\r\n"); Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, LORA_CODINGRATE, LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, LORA_IQ_INVERSION_ON, 2000 ); Radio->SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, LORA_IQ_INVERSION_ON, true ); Radio->Tx(1000000); while(1) { float p; //pressure float temperatureHTS221; //temperature from HTS221 float humidity; //humidity float temperatureLPS22HB; //temperature from LPS22HB int32_t w[3]; //angular velocity int32_t a[3]; //acceleration of the accelerometer LSM303AGR int32_t ag[3]; //acceleration of the accelerometer and gyroscope LSM6DSL int32_t m [3]; //heading press_temp->get_pressure(&p); //get the pressure press_temp->get_temperature(&temperatureLPS22HB); //get temperature from LPS22HB accelerometer->get_x_axes(a);//get the acceleration acc_gyro->get_x_axes(ag);//get the acceleration acc_gyro->get_g_axes(w);//get the angular velocity magnetometer->get_m_axes(m); //get the magnetometer heading hum_temp->get_temperature(&temperatureHTS221); //get temperature from HTS221 hum_temp->get_humidity(&humidity); //get humidity //sensors data dados[0] = a[0]; dados[1] = a[1]; dados[2] = a[2]; dados[3] = ag[0]; dados[4] = ag[1]; dados[5] = ag[2]; dados[6] = w[0]; dados[7] = w[1]; dados[8] = w[2]; dados[9] = m[0]; dados[10] = m[1]; dados[11] = m[2]; dados[12] = humidity; dados[13] = temperatureHTS221; dados[14] = temperatureLPS22HB; dados[15] = p; if (transmited==true) { transmited = false; wait_ms(10); Radio->Send( dados, sizeof(dados) ); } } } void SystemClock_Config(void) { #ifdef B_L072Z_LRWAN1_LORA /* * The L072Z_LRWAN1_LORA clock setup is somewhat differnt from the Nucleo board. * It has no LSE. */ RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; RCC_OscInitTypeDef RCC_OscInitStruct = {0}; /* Enable HSE Oscillator and Activate PLL with HSE as source */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSEState = RCC_HSE_OFF; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_6; RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_3; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { // Error_Handler(); } /* Set Voltage scale1 as MCU will run at 32MHz */ __HAL_RCC_PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /* Poll VOSF bit of in PWR_CSR. Wait until it is reset to 0 */ while (__HAL_PWR_GET_FLAG(PWR_FLAG_VOS) != RESET) {}; /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */ RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { // Error_Handler(); } #endif } /* Helper function for printing floats & doubles */ static char *print_double(char* str, double v, int decimalDigits=2) { int i = 1; int intPart, fractPart; int len; char *ptr; /* prepare decimal digits multiplicator */ for (;decimalDigits!=0; i*=10, decimalDigits--); /* calculate integer & fractinal parts */ intPart = (int)v; fractPart = (int)((v-(double)(int)v)*i); /* fill in integer part */ sprintf(str, "%i.", intPart); /* prepare fill in of fractional part */ len = strlen(str); ptr = &str[len]; /* fill in leading fractional zeros */ for (i/=10;i>1; i/=10, ptr++) { if (fractPart >= i) { break; } *ptr = '0'; } /* fill in (rest of) fractional part */ sprintf(ptr, "%i", fractPart); return str; } void OnTxDone(void *radio, void *userThisPtr, void *userData) { Radio->Sleep( ); transmited = true; if (DEBUG_MESSAGE) { pc.printf("> OnTxDone\r\n"); pc.printf("I transmited %6ld, %6ld, %6ld, %6ld, %6ld, %6ld, %6ld, %6ld, %6ld\r\n", dados[0], dados[1], dados[2], dados[3], dados[4], dados[5], dados[6], dados[7], dados[8]); pc.printf("and %6ld, %6ld, %6ld, %s, %7s, %7s %s\r\n", dados[9], dados[10], dados[11], print_double(buffer2, dados[12]), print_double(buffer1, dados[13]), print_double(buffer1, dados[14]), print_double(buffer2, dados[15])); } } void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) { Radio->Sleep( ); if (DEBUG_MESSAGE) pc.printf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d\r\n", rssi, snr); } void OnTxTimeout(void *radio, void *userThisPtr, void *userData) { Radio->Sleep( ); if(DEBUG_MESSAGE) pc.printf("> OnTxTimeout\r\n"); } void OnRxTimeout(void *radio, void *userThisPtr, void *userData) { Radio->Sleep( ); if (DEBUG_MESSAGE) pc.printf("> OnRxTimeout\r\n"); } void OnRxError(void *radio, void *userThisPtr, void *userData) { Radio->Sleep( ); if (DEBUG_MESSAGE) pc.printf("> OnRxError\r\n"); }