Temperature/humidity/pressure values broadcast using BMP180 & Si7021 and onboard temperature sensor

Dependencies:   BLE_API TMP_nrf51 mbed nRF51822

Fork of VTT_NODEV3_LIS3DH by Juho Eskeli

Revision:
2:b221ba23b37f
Parent:
1:bd7fd35251ab
--- a/main.cpp	Mon Jan 25 13:27:15 2016 +0000
+++ b/main.cpp	Mon Jan 25 14:36:57 2016 +0000
@@ -1,8 +1,6 @@
 /** 
-*   LIS3DH & BLE broadcast example for VTT Node V3 & mbed
-*   broadcasts accelerometer xyz values. 
-*   Also generates LIS3DH interrupt on position change and lights up leds based on that interrupt.
-*   As a bonus, read temperature values from SOC and transmit those as well.
+*   Temperature/humidity/pressure broadcast example for mbed / VTT Node V3   
+*   o   Using BMP180 & Si7021 and onboard temperature sensor
 *   Juho Eskeli, VTT
 */
 
@@ -18,9 +16,11 @@
 #endif
 
 #include "AT45.h"
-#include "LIS3DH.h"
 #include "TMP_nrf51.h"
 
+#include "BMP180.h"
+#include "Si7021.h"
+
 //interrupt /gpio configuration
 #include "nrf_gpio.h"
 #include "nrf_gpiote.h"
@@ -35,8 +35,10 @@
 #define MISO SPI_PSELMISO0
 #define CS SPI_PSELSS0
 #define SCLK SPI_PSELSCK0
-static LIS3DH lis(MOSI, MISO, CS, SCLK);    
-static TMP_nrf51  tempSensor;                         
+
+static TMP_nrf51  tempSensor;   
+BMP180 bmp180(I2C_SDA0, I2C_SCL0);
+SI7021_I2C si7021(I2C_SDA0, I2C_SCL0);                
                                                                                             
 DigitalOut myled1(LED1);
 DigitalOut myled2(LED2);
@@ -45,79 +47,23 @@
 DigitalOut AT_CS(p5); //Dataflash CS
 DigitalOut AT_RS(p6); //Dataflash reset
 
-/** @brief Function for initializing the GPIO Tasks/Events peripheral.
-*/
-static void gpiote_init(void)
-{
-    // Configure accelerometer interrupt pin
-    nrf_gpio_cfg_input(3, NRF_GPIO_PIN_PULLDOWN);
-        //nrf_gpio_cfg_input(4, NRF_GPIO_PIN_PULLDOWN);
-    
-    // Configure GPIOTE channel 0 to generate event when MOTION_INTERRUPT_PIN_NUMBER goes from Low to High
-    nrf_gpiote_event_config(0, 3, NRF_GPIOTE_POLARITY_LOTOHI);   //accelerometer int1
-        //nrf_gpiote_event_config(1, 4, NRF_GPIOTE_POLARITY_LOTOHI);   //accelerometer int2
-
-    // Enable interrupt for NRF_GPIOTE->EVENTS_IN[0] event
-    NRF_GPIOTE->INTENSET  = GPIOTE_INTENSET_IN0_Msk;
-    //NRF_GPIOTE->INTENSET |= GPIOTE_INTENSET_IN1_Msk;
-}
-
-extern "C"
-void GPIOTE_IRQHandler(void)
-{
-    // Event causing the interrupt must be cleared
-    NRF_GPIOTE->EVENTS_IN[0]  = 0;
-    //NRF_GPIOTE->EVENTS_IN[1]  = 0;
-    lis.LIS3DH_ResetInt1Latch();
-    
-    myled1 = !myled1;
-    myled2 = !myled2;
-}
-
-void disconnect_input_buffers()
-{
-    for(uint8_t i = 0; i < 3; i++)
-    {
-                    NRF_GPIO->PIN_CNF[i] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
-                                        | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
-                                        | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
-                                        | (GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos)
-                                        | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
-    }   
-    //Omit accelerometer interrupt pins (3&4)
-    for(uint8_t i = 5; i < 21; i++)
-    {
-                    NRF_GPIO->PIN_CNF[i] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
-                                        | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
-                                        | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
-                                        | (GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos)
-                                        | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
-    }   
-    //Omit I2C pins (21 & 22)
-    for(uint8_t i = 23; i < 31; i++)
-    {
-                    NRF_GPIO->PIN_CNF[i] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
-                                        | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
-                                        | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
-                                        | (GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos)
-                                        | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
-    }
-}
-
 __packed struct ApplicationData_t {
     uint16_t                    applicationSpecificId; /* An ID used to identify temperature value in the manufacture specific AD data field */
-    TMP_nrf51::tmpSensorValue_t tmpSensorValue;      /* User defined application data */
-    int8_t                      accel_temp;    
-    AxesRaw_t                   accel_raw;        
+    TMP_nrf51::tmpSensorValue_t tmpSensorValue;      /* User defined application data */           
+    int16_t                     bmp180_temp;
+    uint32_t                    bmp180_press;
+    float                       si7021_temperature;
+    float                       si7021_humidity;
 };
 
 void setupApplicationData(ApplicationData_t &appData)
 {
     static const uint16_t APP_SPECIFIC_ID_TEST = 0xFEFE;
     appData.applicationSpecificId = APP_SPECIFIC_ID_TEST;
-    appData.tmpSensorValue        = tempSensor.get();
-    lis.LIS3DH_GetAccAxesRaw(&appData.accel_raw);
-    lis.LIS3DH_GetTempRaw(&appData.accel_temp);    
+    appData.tmpSensorValue        = tempSensor.get();          
+    appData.bmp180_temp           = bmp180.BMP180GetTemperature();
+    appData.bmp180_press          = bmp180.BMP180GetPressure();
+    si7021.Measure_Humidity_Temp((float*)&(appData.si7021_humidity), (float*)&(appData.si7021_temperature));
 }
  
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
@@ -142,6 +88,23 @@
     AT_RS = 1;
 }
 
+bool init_bmp180(void) 
+{    
+    // Read the WHO_AM_I register of the BMP-180, this is a good test of communication
+    uint8_t c = bmp180.readByte(BMP180_ADDRESS, BMP180_WHO_AM_I);   
+    if(c == 0x55) {
+        //BMP-180 should be 0x55
+        //BMP-180 online...
+        bmp180.BMP180Calibration();
+        //BMP-180 calibration complete        
+        return true;
+    }
+    else 
+    {                
+        return false;
+    }
+}
+
 int main() 
 {    
     //LEDS off
@@ -156,25 +119,9 @@
     spi.frequency(8000000); 
     //setup AT45 dataflash to powersaving mode
     AT45* flash = new AT45(spi, p5);    
-    flash->ultra_deep_power_down(true); 
-    
-    //Accelerometer interrupt pin configuration
-    gpiote_init();  
-    
-    //Disconnect input buffers to save power
-    disconnect_input_buffers();
+    flash->ultra_deep_power_down(true);     
     
-    //Initialize LIS3DH driver
-    lis.InitLIS3DH(LIS3DH_NORMAL, LIS3DH_ODR_100Hz, LIS3DH_FULLSCALE_2);    //Init Acc-sensor
-    //enable threshold to generate interrupt
-    lis.SetLIS3DHActivityDetection(3, LIS3DH_INT_MODE_6D_POSITION, 1);  
-    
-    // Enable GPIOTE interrupt in Nested Vector Interrupt Controller.
-    NVIC_ClearPendingIRQ(GPIOTE_IRQn);
-    NRF_GPIOTE->INTENSET = GPIOTE_INTENSET_PORT_Set << GPIOTE_INTENSET_PORT_Pos;
-    NVIC_SetPriority(GPIOTE_IRQn, 1);
-    NVIC_EnableIRQ(GPIOTE_IRQn);
-    //sd_nvic_EnableIRQ(GPIOTE_IRQn);    
+    init_bmp180();
     
     //Initialize BLE stuff
     ble.init();