paparazzi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[paparazzi-commits] [6307] Add Sensirion SHT25 humidity sensor module


From: Martin Mueller
Subject: [paparazzi-commits] [6307] Add Sensirion SHT25 humidity sensor module
Date: Fri, 29 Oct 2010 23:30:16 +0000

Revision: 6307
          http://svn.sv.gnu.org/viewvc/?view=rev&root=paparazzi&revision=6307
Author:   mmm
Date:     2010-10-29 23:30:15 +0000 (Fri, 29 Oct 2010)
Log Message:
-----------
Add Sensirion SHT25 humidity sensor module

Modified Paths:
--------------
    paparazzi3/trunk/conf/airframes/mm/fixed-wing/funjetmm.xml
    paparazzi3/trunk/conf/messages.xml

Added Paths:
-----------
    paparazzi3/trunk/conf/modules/humid_sht_i2c.xml
    paparazzi3/trunk/sw/airborne/modules/meteo/humid_sht_i2c.c
    paparazzi3/trunk/sw/airborne/modules/meteo/humid_sht_i2c.h

Modified: paparazzi3/trunk/conf/airframes/mm/fixed-wing/funjetmm.xml
===================================================================
--- paparazzi3/trunk/conf/airframes/mm/fixed-wing/funjetmm.xml  2010-10-29 
20:56:57 UTC (rev 6306)
+++ paparazzi3/trunk/conf/airframes/mm/fixed-wing/funjetmm.xml  2010-10-29 
23:30:15 UTC (rev 6307)
@@ -60,17 +60,18 @@
 
   <!-- modules -->
   <modules>
+    <load name="humid_sht_i2c.xml"/>
+    <!--load name="windturbine.xml"/>
+    <load name="dust_gp2y.xml"/>
+    <load name="light_temt.xml"/>
+    <load name="temp_temod.xml"/>
     <load name="adc_generic.xml"/>
-    <!--load name="windturbine.xml"/>
     <load name="trig_test.xml"/>
-    <load name="temp_temod.xml"/>
     <load name="airspeed_ets.xml"/>
-    <load name="dust_gp2y.xml"/>
     <load name="alt_srf08.xml"/>
     <load name="mag_micromag_fw.xml"/>
     <load name="baro_bmp.xml"/>
     <load name="baro_scp.xml"/>
-    <load name="light_temt.xml"/>
     <load name="humid_hih.xml"/>
     <load name="temp_tmp102.xml"/>
     <load name="temp_lm75.xml"/>

Modified: paparazzi3/trunk/conf/messages.xml
===================================================================
--- paparazzi3/trunk/conf/messages.xml  2010-10-29 20:56:57 UTC (rev 6306)
+++ paparazzi3/trunk/conf/messages.xml  2010-10-29 23:30:15 UTC (rev 6307)
@@ -638,7 +638,10 @@
     <field name="density"   type="float" unit="mg/m3" format="%.2f"/>
   </message>
 
- <!-- 99 is free -->
+  <message name="SHT_SERIAL" id="99">
+    <field name="serial0" type="uint32"/>
+    <field name="serial1" type="uint32"/>
+  </message>
 
   <message name="PPM" id="100">
     <field name="ppm_rate" type="uint8"></field>

Added: paparazzi3/trunk/conf/modules/humid_sht_i2c.xml
===================================================================
--- paparazzi3/trunk/conf/modules/humid_sht_i2c.xml                             
(rev 0)
+++ paparazzi3/trunk/conf/modules/humid_sht_i2c.xml     2010-10-29 23:30:15 UTC 
(rev 6307)
@@ -0,0 +1,21 @@
+<!DOCTYPE module SYSTEM "module.dtd">
+
+<!--
+     Sensirion SHT25 humidity sensor (I2C)
+     @param SCP_I2C_DEV i2c device (default i2c0)
+     -->
+
+<module name="humid_sht_i2c" dir="meteo">
+  <header>
+    <file name="humid_sht_i2c.h"/>
+  </header>
+  <init fun="humid_sht_init()"/>
+  <periodic fun="humid_sht_periodic()" freq="4"/>
+  <periodic fun="humid_sht_p_temp()" freq="4" delay="6"/>
+  <periodic fun="humid_sht_p_humid()"  freq="4" delay="9"/>
+  <event fun="humid_sht_event()"/>
+  <makefile target="ap">
+    <file name="humid_sht_i2c.c"/>
+  </makefile>
+</module>
+

Added: paparazzi3/trunk/sw/airborne/modules/meteo/humid_sht_i2c.c
===================================================================
--- paparazzi3/trunk/sw/airborne/modules/meteo/humid_sht_i2c.c                  
        (rev 0)
+++ paparazzi3/trunk/sw/airborne/modules/meteo/humid_sht_i2c.c  2010-10-29 
23:30:15 UTC (rev 6307)
@@ -0,0 +1,207 @@
+/*
+ * $Id$
+ *  
+ * Copyright (C) 2010 Martin Mueller
+ *
+ * This file is part of paparazzi.
+ *
+ * paparazzi is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * paparazzi is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with paparazzi; see the file COPYING.  If not, write to
+ * the Free Software Foundation, 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA. 
+ *
+ */
+
+/** \file humid_sht_i2c.c
+ *  \brief Sensirion SHT25 humidity/temperature sensor interface
+ *
+ */
+
+
+#include "humid_sht_i2c.h"
+
+#include "i2c.h"
+#include "uart.h"
+#include "messages.h"
+#include "downlink.h"
+
+#ifndef SHT_I2C_DEV
+#define SHT_I2C_DEV i2c0
+#endif
+
+#define SHT_SLAVE_ADDR 0x80
+
+struct i2c_transaction sht_trans;
+uint8_t sht_status;
+uint8_t sht_serial[8] = {0};
+uint32_t sht_serial1=0, sht_serial2=0;
+uint16_t humidsht, tempsht;
+float fhumidsht, ftempsht;
+
+int8_t humid_sht_crc(volatile uint8_t* data) {
+  uint8_t i, bit, crc = 0;     
+
+  for (i = 0; i < 2; i++) {
+    crc ^= (data[i]);
+    for (bit = 8; bit > 0; bit--) {
+      if (crc & 0x80) 
+        crc = (crc << 1) ^ 0x131;
+      else
+        crc = (crc << 1);
+    }
+  }
+  if (crc != data[2]) 
+    return -1;
+  else
+    return 0;
+}
+
+void humid_sht_init(void) {
+  sht_status = SHT_UNINIT;
+}
+
+void humid_sht_periodic( void ) {
+  switch (sht_status) {
+ 
+  case SHT_UNINIT:
+    /* do soft reset, then wait at least 15ms */
+    sht_status = SHT_RESET;
+    sht_trans.buf[0] = SHT_SOFT_RESET;
+    I2CTransmit(SHT_I2C_DEV, sht_trans, SHT_SLAVE_ADDR, 1);
+    break;
+
+  case SHT_SERIAL:
+    /* get serial number part 1 */
+    sht_status = SHT_SERIAL1;
+    sht_trans.buf[0] = 0xFA;
+    sht_trans.buf[1] = 0x0F;
+    I2CTransceive(SHT_I2C_DEV, sht_trans, SHT_SLAVE_ADDR, 2, 8);
+    break;
+
+  case SHT_SERIAL1:
+  case SHT_SERIAL2:
+    break;
+
+  default:
+    /* trigger temp measurement, no master hold */
+    sht_trans.buf[0] = SHT_TRIGGER_TEMP;
+    sht_status = SHT_TRIG_TEMP;
+    I2CTransmit(SHT_I2C_DEV, sht_trans, SHT_SLAVE_ADDR, 1);
+    /* send serial number every 30 seconds */
+    RunOnceEvery((4*30), DOWNLINK_SEND_SHT_SERIAL(DefaultChannel, 
&sht_serial1, &sht_serial2));
+    break;
+  }
+}
+
+/* needs 85ms delay from temp trigger measurement */
+void humid_sht_p_temp( void ) {
+  if (sht_status == SHT_GET_TEMP) {
+    /* get temp */
+    sht_status = SHT_READ_TEMP;
+    I2CReceive(SHT_I2C_DEV, sht_trans, SHT_SLAVE_ADDR, 3);
+  }
+}
+
+/* needs 29ms delay from humid trigger measurement */
+void humid_sht_p_humid( void ) {
+  if (sht_status == SHT_GET_HUMID) {
+    /* read humid */
+    sht_status = SHT_READ_HUMID;
+    I2CReceive(SHT_I2C_DEV, sht_trans, SHT_SLAVE_ADDR, 3);
+  }
+}
+
+void humid_sht_event( void ) {
+  if (sht_trans.status == I2CTransSuccess) {
+    switch (sht_status) {
+
+    case SHT_TRIG_TEMP:
+      sht_status = SHT_GET_TEMP;
+      sht_trans.status = I2CTransDone;
+      break;
+
+    case SHT_READ_TEMP:
+      /* read temperature */
+      tempsht = (sht_trans.buf[0] << 8) | sht_trans.buf[1];
+      tempsht &= 0xFFFC;
+      if (humid_sht_crc(sht_trans.buf) == 0) {
+        /* trigger humid measurement, no master hold */
+        sht_trans.buf[0] = SHT_TRIGGER_HUMID;
+        sht_status = SHT_TRIG_HUMID;
+        I2CTransmit(SHT_I2C_DEV, sht_trans, SHT_SLAVE_ADDR, 1);
+      }
+      else {
+        /* checksum error, restart */
+        sht_status = SHT_IDLE;
+        sht_trans.status == I2CTransDone;
+      }
+      break;
+
+    case SHT_TRIG_HUMID:
+      sht_status = SHT_GET_HUMID;
+      sht_trans.status = I2CTransDone;
+      break;
+
+    case SHT_READ_HUMID:
+      /* read humidity */
+      humidsht = (sht_trans.buf[0] << 8) | sht_trans.buf[1];
+      humidsht &= 0xFFFC;
+      fhumidsht = -6. + 125. / 65536. * humidsht;
+      ftempsht = -46.85 + 175.72 / 65536. * tempsht;
+
+      sht_status = SHT_IDLE;
+      sht_trans.status = I2CTransDone;
+
+      if (humid_sht_crc(sht_trans.buf) == 0) {
+        DOWNLINK_SEND_SHT_STATUS(DefaultChannel, &humidsht, &tempsht, 
&fhumidsht, &ftempsht);
+      }
+      break;
+
+    case SHT_RESET:
+      sht_status = SHT_SERIAL;
+      sht_trans.status = I2CTransDone;
+      break;
+
+    case SHT_SERIAL1:
+      /* read serial number part 1 */
+      sht_serial[5] = sht_trans.buf[0];
+      sht_serial[4] = sht_trans.buf[2];
+      sht_serial[3] = sht_trans.buf[4];
+      sht_serial[2] = sht_trans.buf[6];
+      /* get serial number part 2 */
+      sht_status = SHT_SERIAL2;
+      sht_trans.buf[0] = 0xFC;
+      sht_trans.buf[1] = 0xC9;
+      I2CTransceive(SHT_I2C_DEV, sht_trans, SHT_SLAVE_ADDR, 2, 6);
+      break;
+
+    case SHT_SERIAL2:
+      /* read serial number part 2 */
+      sht_serial[1] = sht_trans.buf[0];
+      sht_serial[0] = sht_trans.buf[1];
+      sht_serial[7] = sht_trans.buf[3];
+      sht_serial[6] = sht_trans.buf[4];
+      
sht_serial1=sht_serial[7]<<24|sht_serial[6]<<16|sht_serial[5]<<8|sht_serial[4];
+      
sht_serial2=sht_serial[3]<<24|sht_serial[2]<<16|sht_serial[1]<<8|sht_serial[0];
+      DOWNLINK_SEND_SHT_SERIAL(DefaultChannel, &sht_serial1, &sht_serial2);
+      sht_status = SHT_IDLE;
+      sht_trans.status = I2CTransDone;
+      break;
+
+    default:
+      sht_trans.status = I2CTransDone;
+      break;
+    }
+  }
+}
+

Added: paparazzi3/trunk/sw/airborne/modules/meteo/humid_sht_i2c.h
===================================================================
--- paparazzi3/trunk/sw/airborne/modules/meteo/humid_sht_i2c.h                  
        (rev 0)
+++ paparazzi3/trunk/sw/airborne/modules/meteo/humid_sht_i2c.h  2010-10-29 
23:30:15 UTC (rev 6307)
@@ -0,0 +1,39 @@
+#ifndef HUMID_SHT_I2C_H
+#define HUMID_SHT_I2C_H
+
+#include "std.h"
+
+#define SHT_WRITE_USER          0xE6
+#define SHT_READ_USER           0xE7
+#define SHT_TRIGGER_TEMP        0xF3
+#define SHT_TRIGGER_HUMID       0xF5
+#define SHT_SOFT_RESET          0xFE
+
+enum sht_stat{
+  SHT_UNINIT,
+  SHT_IDLE,
+  SHT_RESET,
+  SHT_SERIAL,
+  SHT_SERIAL1,
+  SHT_SERIAL2,
+  SHT_SET_CONFIG,
+  SHT_READ_SERIAL,
+  SHT_TRIG_TEMP,
+  SHT_GET_TEMP,
+  SHT_READ_TEMP,
+  SHT_TRIG_HUMID,
+  SHT_GET_HUMID,
+  SHT_READ_HUMID
+};
+
+int8_t humid_sht_crc(volatile uint8_t* data);
+void humid_sht_init(void);
+void humid_sht_periodic(void);
+void humid_sht_p_temp(void);
+void humid_sht_p_humid(void);
+void humid_sht_event(void);
+
+extern uint16_t humidsht, tempsht;
+extern float fhumidsht, ftempsht;
+
+#endif




reply via email to

[Prev in Thread] Current Thread [Next in Thread]