CuVoodoo STM32F1 firmware template
sensor_dht22.c
Go to the documentation of this file.
1 /* This program is free software: you can redistribute it and/or modify
2  * it under the terms of the GNU General Public License as published by
3  * the Free Software Foundation, either version 3 of the License, or
4  * (at your option) any later version.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program. If not, see <http://www.gnu.org/licenses/>.
13  *
14  */
23 /* standard libraries */
24 #include <stdint.h> // standard integer types
25 #include <math.h> // maths utilities
26 
27 /* STM32 (including CM3) libraries */
28 #include <libopencmsis/core_cm3.h> // Cortex M3 utilities
29 #include <libopencm3/cm3/nvic.h> // interrupt handler
30 #include <libopencm3/stm32/rcc.h> // real-time control clock library
31 #include <libopencm3/stm32/gpio.h> // general purpose input output library
32 #include <libopencm3/stm32/timer.h> // timer utilities
33 
34 /* own libraries */
35 #include "sensor_dht22.h" // PZEM electricity meter header and definitions
36 #include "global.h" // common methods
37 
41 #define SENSOR_DHT22_TIMER 4
42 #define SENSOR_DHT22_CHANNEL 3
43 #define SENSOR_DHT22_JITTER 0.2
46 volatile bool sensor_dht22_measurement_received = false;
47 
49 volatile enum sensor_dht22_state_t {
50  SENSOR_DHT22_OFF, // no request has started
51  SENSOR_DHT22_HOST_START, // host starts request (and waits >18ms)
52  SENSOR_DHT22_HOST_STARTED, // host started request and waits for slave answer
53  SENSOR_DHT22_SLAVE_START, // slave responds to request and puts signal low for 80 us and high for 80 us
54  SENSOR_DHT22_SLAVE_BIT, // slave is sending bit by putting signal low for 50 us and high (26-28 us = 0, 70 us = 1)
55  SENSOR_DHT22_MAX
56 } sensor_dht22_state = SENSOR_DHT22_OFF;
59 volatile uint8_t sensor_dht22_bit = 0;
60 
62 volatile uint8_t sensor_dht22_bits[5] = {0};
63 
65 static void sensor_dht22_reset(void)
66 {
67  // reset states
68  sensor_dht22_state = SENSOR_DHT22_OFF;
69  sensor_dht22_bit = 0;
71 
72  gpio_set(TIM_CH_PORT(SENSOR_DHT22_TIMER,SENSOR_DHT22_CHANNEL), TIM_CH_PIN(SENSOR_DHT22_TIMER,SENSOR_DHT22_CHANNEL)); // idle is high (using pull-up resistor), pull-up before setting as output else the signal will be low for short
73  gpio_set_mode(TIM_CH_PORT(SENSOR_DHT22_TIMER,SENSOR_DHT22_CHANNEL), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, TIM_CH_PIN(SENSOR_DHT22_TIMER,SENSOR_DHT22_CHANNEL)); // setup GPIO pin as output (host starts communication before slave replies)
74 
75  timer_ic_disable(TIM(SENSOR_DHT22_TIMER), TIM_IC(SENSOR_DHT22_CHANNEL)); // enable capture interrupt only when receiving data
76  timer_disable_counter(TIM(SENSOR_DHT22_TIMER)); // disable timer
77 }
78 
80 {
81  // setup timer to measure signal timing for bit decoding (use timer channel as input capture)
82  rcc_periph_clock_enable(RCC_TIM_CH(SENSOR_DHT22_TIMER,SENSOR_DHT22_CHANNEL)); // enable clock for GPIO peripheral
83  rcc_periph_clock_enable(RCC_TIM(SENSOR_DHT22_TIMER)); // enable clock for timer peripheral
84  timer_reset(TIM(SENSOR_DHT22_TIMER)); // reset timer state
85  timer_set_mode(TIM(SENSOR_DHT22_TIMER), TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock,edge alignment (simple count), and count up
86  timer_set_prescaler(TIM(SENSOR_DHT22_TIMER), 2-1); // set the prescaler so this 16 bits timer allows to wait for 18 ms for the start signal ( 1/(72E6/2/(2**16))=1.820ms )
87  timer_ic_set_input(TIM(SENSOR_DHT22_TIMER), TIM_IC(SENSOR_DHT22_CHANNEL), TIM_IC_IN_TI(SENSOR_DHT22_CHANNEL)); // configure ICx to use TIn
88  timer_ic_set_filter(TIM(SENSOR_DHT22_TIMER), TIM_IC(SENSOR_DHT22_CHANNEL), TIM_IC_OFF); // use no filter input (precise timing needed)
89  timer_ic_set_polarity(TIM(SENSOR_DHT22_TIMER), TIM_IC(SENSOR_DHT22_CHANNEL), TIM_IC_FALLING); // capture on rising edge
90  timer_ic_set_prescaler(TIM(SENSOR_DHT22_TIMER), TIM_IC(SENSOR_DHT22_CHANNEL), TIM_IC_PSC_OFF); // don't use any prescaler since we want to capture every pulse
91 
92  timer_clear_flag(TIM(SENSOR_DHT22_TIMER), TIM_SR_UIF); // clear flag
93  timer_update_on_overflow(TIM(SENSOR_DHT22_TIMER)); // only use counter overflow as UEV source (use overflow as start time or timeout)
94  timer_enable_irq(TIM(SENSOR_DHT22_TIMER), TIM_DIER_UIE); // enable update interrupt for timer
95 
96  timer_clear_flag(TIM(SENSOR_DHT22_TIMER), TIM_SR_CCIF(SENSOR_DHT22_CHANNEL)); // clear input compare flag
97  timer_enable_irq(TIM(SENSOR_DHT22_TIMER), TIM_DIER_CCIE(SENSOR_DHT22_CHANNEL)); // enable capture interrupt
98 
99  nvic_enable_irq(NVIC_TIM_IRQ(SENSOR_DHT22_TIMER)); // catch interrupt in service routine
100 
101  sensor_dht22_reset(); // reset state
102 }
103 
105 {
106  if (sensor_dht22_state!=SENSOR_DHT22_OFF) { // not the right state to start (wait up until timeout to reset state)
107  return false;
108  }
109  if (gpio_get(TIM_CH_PORT(SENSOR_DHT22_TIMER,SENSOR_DHT22_CHANNEL), TIM_CH_PIN(SENSOR_DHT22_TIMER,SENSOR_DHT22_CHANNEL))==0) { // signal should be high per default
110  return false;
111  }
112  if (TIM_CR1(TIM(SENSOR_DHT22_TIMER))&(TIM_CR1_CEN)) { // timer should be off
113  return false;
114  }
115  sensor_dht22_reset(); // reset states
116 
117  // send start signal (pull low for > 1 ms)
119  timer_set_counter(TIM(SENSOR_DHT22_TIMER), 0); // reset timer counter
120  timer_enable_counter(TIM(SENSOR_DHT22_TIMER)); // enable timer to wait for 1.8 ms until overflow
121  sensor_dht22_state = SENSOR_DHT22_HOST_START; // remember we started sending signal
122 
123  return true;
124 }
125 
127 {
128  struct sensor_dht22_measurement_t measurement = { NAN, NAN }; // measurement to return
129  if (sensor_dht22_bit<40) { // not enough bits received
130  return measurement;
131  }
132  if ((uint8_t)(sensor_dht22_bits[0]+sensor_dht22_bits[1]+sensor_dht22_bits[2]+sensor_dht22_bits[3])!=sensor_dht22_bits[4]) { // error in checksum (not really parity bit, as mentioned in the datasheet)
133  return measurement;
134  }
135  // calculate measured values (stored as uint16_t deci-value)
136  measurement.humidity = (int16_t)((sensor_dht22_bits[0]<<8)+sensor_dht22_bits[1])/10.0;
137  measurement.temperature = (int16_t)((sensor_dht22_bits[2]<<8)+sensor_dht22_bits[3])/10.0;
138 
139  return measurement;
140 }
141 
144 {
145  if (timer_get_flag(TIM(SENSOR_DHT22_TIMER), TIM_SR_UIF)) { // overflow update event happened
146  timer_clear_flag(TIM(SENSOR_DHT22_TIMER), TIM_SR_UIF); // clear flag
147  if (sensor_dht22_state==SENSOR_DHT22_HOST_START) { // start signal sent
148  gpio_set_mode(TIM_CH_PORT(SENSOR_DHT22_TIMER,SENSOR_DHT22_CHANNEL), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, TIM_CH_PIN(SENSOR_DHT22_TIMER,SENSOR_DHT22_CHANNEL)); // switch pin to input (the external pull up with also set the signal high)
149  sensor_dht22_state = SENSOR_DHT22_HOST_STARTED; // switch to next state
150  timer_ic_enable(TIM(SENSOR_DHT22_TIMER), TIM_IC(SENSOR_DHT22_CHANNEL)); // enable capture interrupt only when receiving data
151  } else { // timeout occurred
152  sensor_dht22_reset(); // reset states
153  }
154  } else if (timer_get_flag(TIM(SENSOR_DHT22_TIMER), TIM_SR_CCIF(SENSOR_DHT22_CHANNEL))) { // edge detected on input capture
155  uint16_t time = TIM_CCR(SENSOR_DHT22_TIMER,SENSOR_DHT22_CHANNEL); // save captured bit timing (this clear also the flag)
156  timer_set_counter(TIM(SENSOR_DHT22_TIMER), 0); // reset timer counter
157  time = (time*1E6)/(rcc_ahb_frequency/(TIM_PSC(TIM(SENSOR_DHT22_TIMER))+1)); // calculate time in us
158  switch (sensor_dht22_state) {
159  case (SENSOR_DHT22_HOST_STARTED): // the host query data and the slave is responding
160  sensor_dht22_state = SENSOR_DHT22_SLAVE_START; // set new state
161  break;
162  case (SENSOR_DHT22_SLAVE_START): // the slave sent the start signal
163  if (time >= ((80+80)*(1-SENSOR_DHT22_JITTER)) && time <= ((80+80)*(1+SENSOR_DHT22_JITTER))) { // response time should be 80 us low and 80 us high
164  sensor_dht22_state = SENSOR_DHT22_SLAVE_BIT; // set new state
165  } else {
166  goto error;
167  }
168  break;
169  case (SENSOR_DHT22_SLAVE_BIT): // the slave sent a bit
170  if (sensor_dht22_bit>=40) { // no bits should be received after 40 bits
171  goto error;
172  }
173  if (time >= ((50+26)*(1-SENSOR_DHT22_JITTER)) && time <= ((50+28)*(1+SENSOR_DHT22_JITTER))) { // bit 0 time should be 50 us low and 26-28 us high
174  sensor_dht22_bits[sensor_dht22_bit/8] &= ~(1<<(7-(sensor_dht22_bit%8))); // clear bit
175  } else if (time >= ((50+70)*(1-SENSOR_DHT22_JITTER)) && time <= ((50+70)*(1+SENSOR_DHT22_JITTER))) { // bit 1 time should be 50 us low and 70 us high
176  sensor_dht22_bits[sensor_dht22_bit/8] |= (1<<(7-(sensor_dht22_bit%8))); // set bit
177  } else {
178  goto error;
179  }
181  if (sensor_dht22_bit>=40) { // all bits received
182  sensor_dht22_reset(); // reset states
183  sensor_dht22_bit = 40; // signal decoder all bits have been received
184  sensor_dht22_measurement_received = true; // signal user all bits have been received
185  }
186  break;
187  default: // unexpected state
188 error:
189  sensor_dht22_reset(); // reset states
190  }
191  } else { // no other interrupt should occur
192  while (true); // unhandled exception: wait for the watchdog to bite
193  }
194 }
#define RCC_TIM_CH(x, y)
get RCC for port based on TIMx_CHy identifier
Definition: global.h:119
#define TIM_ISR(x)
get interrupt service routine for timer base on TIM identifier
Definition: global.h:113
#define SENSOR_DHT22_CHANNEL
channel used as input capture
Definition: sensor_dht22.c:42
static void sensor_dht22_reset(void)
reset all states
Definition: sensor_dht22.c:65
bool sensor_dht22_measurement_request(void)
request measurement from sensor
Definition: sensor_dht22.c:104
#define NVIC_TIM_IRQ(x)
get NVIC IRQ for timer base on TIM identifier
Definition: global.h:111
float temperature
temperature in °C (-40-80)
Definition: sensor_dht22.h:29
#define TIM_SR_CCIF(x)
get TIM_SR_CCxIF based on CHx identifier
Definition: global.h:148
#define TIM_DIER_CCIE(x)
get TIM_DIER_CCxIE based on CHx identifier
Definition: global.h:150
void sensor_dht22_setup(void)
setup peripherals to communicate with sensor
Definition: sensor_dht22.c:79
global definitions and methods (API)
#define TIM_CCR(x, y)
get TIM_CCRy register based on TIMx_CHy identifier
Definition: global.h:152
library to query measurements from Aosong DHT22 (aka.
float humidity
relative humidity in RH (0-100)
Definition: sensor_dht22.h:28
#define RCC_TIM(x)
get RCC for timer based on TIM identifier
Definition: global.h:109
struct sensor_dht22_measurement_t sensor_dht22_measurement_decode(void)
decode received measurement
Definition: sensor_dht22.c:126
#define TIM_CH_PIN(x, y)
get pin based on TIMx_CHy identifier
Definition: global.h:117
#define TIM_IC(x)
get TIM_IC based on CHx identifier
Definition: global.h:144
#define TIM_IC_IN_TI(x)
get TIM_IC_IN_TI based on CHx identifier
Definition: global.h:146
#define SENSOR_DHT22_JITTER
signal timing jitter tolerated in timing
Definition: sensor_dht22.c:43
volatile bool sensor_dht22_measurement_received
a measurement response has been received
Definition: sensor_dht22.c:46
#define SENSOR_DHT22_TIMER
timer peripheral
Definition: sensor_dht22.c:41
#define TIM_CH_PORT(x, y)
get port based on TIMx_CHy identifier
Definition: global.h:115
sensor_dht22_state_t
communication states
Definition: sensor_dht22.c:49
volatile uint8_t sensor_dht22_bit
the bit number being sent (MSb first), up to 40
Definition: sensor_dht22.c:59
enum sensor_dht22_state_t sensor_dht22_state
current communication state
#define TIM(x)
get TIM based on TIM identifier
Definition: global.h:107
measurement returned by sensor
Definition: sensor_dht22.h:27
volatile uint8_t sensor_dht22_bits[5]
the 40 bits (5 bytes) being sent by the device
Definition: sensor_dht22.c:62