CuVoodoo STM32F1 firmware template
sensor_dht11.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  */
22 /* standard libraries */
23 #include <stdint.h> // standard integer types
24 #include <stdlib.h> // general utilities
25 
26 /* STM32 (including CM3) libraries */
27 #include <libopencmsis/core_cm3.h> // Cortex M3 utilities
28 #include <libopencm3/cm3/nvic.h> // interrupt handler
29 #include <libopencm3/stm32/rcc.h> // real-time control clock library
30 #include <libopencm3/stm32/gpio.h> // general purpose input output library
31 #include <libopencm3/stm32/timer.h> // timer utilities
32 
33 /* own libraries */
34 #include "sensor_dht11.h" // PZEM electricity meter header and definitions
35 #include "global.h" // common methods
36 
40 #define SENSOR_DHT11_TIMER 3
41 #define SENSOR_DHT11_CHANNEL 1
42 #define SENSOR_DHT11_JITTER 0.1
45 volatile bool sensor_dht11_measurement_received = false;
46 
48 volatile enum sensor_dht11_state_t {
49  SENSOR_DHT11_OFF, // no request has started
50  SENSOR_DHT11_HOST_START, // host starts request (and waits >18ms)
51  SENSOR_DHT11_HOST_STARTED, // host started request and waits for slave answer
52  SENSOR_DHT11_SLAVE_START, // slave responds to request and puts signal low for 80 us and high for 80 us
53  SENSOR_DHT11_SLAVE_BIT, // slave is sending bit by putting signal low for 50 us and high (26-28 us = 0, 70 us = 1)
54  SENSOR_DHT11_MAX
55 } sensor_dht11_state = SENSOR_DHT11_OFF;
56 
58 volatile uint8_t sensor_dht11_bit = 0;
59 
61 volatile uint8_t sensor_dht11_bits[5] = {0};
62 
64 static void sensor_dht11_reset(void)
65 {
66  // reset states
67  sensor_dht11_state = SENSOR_DHT11_OFF;
68  sensor_dht11_bit = 0;
70  gpio_set(TIM_CH_PORT(SENSOR_DHT11_TIMER,SENSOR_DHT11_CHANNEL), TIM_CH_PIN(SENSOR_DHT11_TIMER,SENSOR_DHT11_CHANNEL)); // idle is high (using pull-up resistor), pull-up before setting as output else the signal will be low for short
71  gpio_set_mode(TIM_CH_PORT(SENSOR_DHT11_TIMER,SENSOR_DHT11_CHANNEL), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, TIM_CH_PIN(SENSOR_DHT11_TIMER,SENSOR_DHT11_CHANNEL)); // setup GPIO pin as output (host starts communication before slave replies)
72  timer_ic_disable(TIM(SENSOR_DHT11_TIMER), TIM_IC(SENSOR_DHT11_CHANNEL)); // enable capture interrupt only when receiving data
73  timer_disable_counter(TIM(SENSOR_DHT11_TIMER)); // disable timer
74 }
75 
77 {
78  // setup timer to measure signal timing for bit decoding (use timer channel as input capture)
79  rcc_periph_clock_enable(RCC_TIM_CH(SENSOR_DHT11_TIMER,SENSOR_DHT11_CHANNEL)); // enable clock for GPIO peripheral
80  rcc_periph_clock_enable(RCC_TIM(SENSOR_DHT11_TIMER)); // enable clock for timer peripheral
81  timer_reset(TIM(SENSOR_DHT11_TIMER)); // reset timer state
82  timer_set_mode(TIM(SENSOR_DHT11_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
83  timer_set_prescaler(TIM(SENSOR_DHT11_TIMER), 20-1); // set the prescaler so this 16 bits timer allows to wait for 18 ms for the start signal ( 1/(72E6/20/(2**16))=18.20ms )
84  timer_ic_set_input(TIM(SENSOR_DHT11_TIMER), TIM_IC(SENSOR_DHT11_CHANNEL), TIM_IC_IN_TI(SENSOR_DHT11_CHANNEL)); // configure ICx to use TIn
85  timer_ic_set_filter(TIM(SENSOR_DHT11_TIMER), TIM_IC(SENSOR_DHT11_CHANNEL), TIM_IC_OFF); // use no filter input (precise timing needed)
86  timer_ic_set_polarity(TIM(SENSOR_DHT11_TIMER), TIM_IC(SENSOR_DHT11_CHANNEL), TIM_IC_FALLING); // capture on rising edge
87  timer_ic_set_prescaler(TIM(SENSOR_DHT11_TIMER), TIM_IC(SENSOR_DHT11_CHANNEL), TIM_IC_PSC_OFF); // don't use any prescaler since we want to capture every pulse
88 
89  timer_clear_flag(TIM(SENSOR_DHT11_TIMER), TIM_SR_UIF); // clear flag
90  timer_update_on_overflow(TIM(SENSOR_DHT11_TIMER)); // only use counter overflow as UEV source (use overflow as start time or timeout)
91  timer_enable_irq(TIM(SENSOR_DHT11_TIMER), TIM_DIER_UIE); // enable update interrupt for timer
92 
93  timer_clear_flag(TIM(SENSOR_DHT11_TIMER), TIM_SR_CCIF(SENSOR_DHT11_CHANNEL)); // clear input compare flag
94  timer_enable_irq(TIM(SENSOR_DHT11_TIMER), TIM_DIER_CCIE(SENSOR_DHT11_CHANNEL)); // enable capture interrupt
95 
96  nvic_enable_irq(NVIC_TIM_IRQ(SENSOR_DHT11_TIMER)); // catch interrupt in service routine
97 
98  sensor_dht11_reset(); // reset state
99 }
100 
102 {
103  if (sensor_dht11_state!=SENSOR_DHT11_OFF) { // not the right state to start (wait up until timeout to reset state)
104  return false;
105  }
106  if (gpio_get(TIM_CH_PORT(SENSOR_DHT11_TIMER,SENSOR_DHT11_CHANNEL), TIM_CH_PIN(SENSOR_DHT11_TIMER,SENSOR_DHT11_CHANNEL))==0) { // signal should be high per default
107  return false;
108  }
109  if (TIM_CR1(SENSOR_DHT11_TIMER)&(TIM_CR1_CEN)) { // timer should be off
110  return false;
111  }
112  sensor_dht11_reset(); // reset states
113 
114  // send start signal (pull low for > 18 ms)
116  timer_set_counter(TIM(SENSOR_DHT11_TIMER), 0); // reset timer counter
117  timer_enable_counter(TIM(SENSOR_DHT11_TIMER)); // enable timer to wait for 18 ms until overflow
118  sensor_dht11_state = SENSOR_DHT11_HOST_START; // remember we started sending signal
119 
120  return true;
121 }
122 
124 {
125  struct sensor_dht11_measurement_t measurement = { 0xff, 0xff }; // measurement to return
126  if (sensor_dht11_bit<40) { // not enough bits received
127  return measurement;
128  }
129  if (sensor_dht11_bits[0]+sensor_dht11_bits[1]+sensor_dht11_bits[2]+sensor_dht11_bits[3]!=sensor_dht11_bits[4]) { // error in checksum (not really parity bit, as mentioned in the datasheet)
130  return measurement;
131  }
132  // calculate measured values (byte 1 and 3 should be the factional value but they are always 0)
133  measurement.humidity = sensor_dht11_bits[0];
134  measurement.temperature = sensor_dht11_bits[2];
135 
136  return measurement;
137 }
138 
141 {
142  if (timer_get_flag(TIM(SENSOR_DHT11_TIMER), TIM_SR_UIF)) { // overflow update event happened
143  timer_clear_flag(TIM(SENSOR_DHT11_TIMER), TIM_SR_UIF); // clear flag
144  if (sensor_dht11_state==SENSOR_DHT11_HOST_START) { // start signal sent
145  gpio_set_mode(TIM_CH_PORT(SENSOR_DHT11_TIMER,SENSOR_DHT11_CHANNEL), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, TIM_CH_PIN(SENSOR_DHT11_TIMER,SENSOR_DHT11_CHANNEL)); // switch pin to input (the external pull up with also set the signal high)
146  sensor_dht11_state = SENSOR_DHT11_HOST_STARTED; // switch to next state
147  timer_ic_enable(TIM(SENSOR_DHT11_TIMER), TIM_IC(SENSOR_DHT11_CHANNEL)); // enable capture interrupt only when receiving data
148  } else { // timeout occurred
149  sensor_dht11_reset(); // reset states
150  }
151  } else if (timer_get_flag(TIM(SENSOR_DHT11_TIMER), TIM_SR_CCIF(SENSOR_DHT11_CHANNEL))) { // edge detected on input capture
152  uint16_t time = TIM_CCR(SENSOR_DHT11_TIMER,SENSOR_DHT11_CHANNEL); // save captured bit timing (this clear also the flag)
153  timer_set_counter(TIM(SENSOR_DHT11_TIMER), 0); // reset timer counter
154  time = (time*1E6)/(rcc_ahb_frequency/(TIM_PSC(TIM(SENSOR_DHT11_TIMER))+1)); // calculate time in us
155  switch (sensor_dht11_state) {
156  case (SENSOR_DHT11_HOST_STARTED): // the host query data and the slave is responding
157  sensor_dht11_state = SENSOR_DHT11_SLAVE_START; // set new state
158  break;
159  case (SENSOR_DHT11_SLAVE_START): // the slave sent the start signal
160  if (time >= ((80+80)*(1-SENSOR_DHT11_JITTER)) && time <= ((80+80)*(1+SENSOR_DHT11_JITTER))) { // response time should be 80 us low and 80 us high
161  sensor_dht11_state = SENSOR_DHT11_SLAVE_BIT; // set new state
162  } else {
163  goto error;
164  }
165  break;
166  case (SENSOR_DHT11_SLAVE_BIT): // the slave sent a bit
167  if (sensor_dht11_bit>=40) { // no bits should be received after 40 bits
168  goto error;
169  }
170  if (time >= ((50+26)*(1-SENSOR_DHT11_JITTER)) && time <= ((50+28)*(1+SENSOR_DHT11_JITTER))) { // bit 0 time should be 50 us low and 26-28 us high
171  sensor_dht11_bits[sensor_dht11_bit/8] &= ~(1<<(7-(sensor_dht11_bit%8))); // clear bit
172  } else if (time >= ((50+70)*(1-SENSOR_DHT11_JITTER)) && time <= ((50+70)*(1+SENSOR_DHT11_JITTER))) { // bit 1 time should be 50 us low and 70 us high
173  sensor_dht11_bits[sensor_dht11_bit/8] |= (1<<(7-(sensor_dht11_bit%8))); // set bit
174  } else {
175  goto error;
176  }
178  if (sensor_dht11_bit>=40) { // all bits received
179  sensor_dht11_reset(); // reset states
180  sensor_dht11_bit = 40; // signal decoder all bits have been received
181  sensor_dht11_measurement_received = true; // signal user all bits have been received
182  }
183  break;
184  default: // unexpected state
185 error:
186  sensor_dht11_reset(); // reset states
187  }
188  } else { // no other interrupt should occur
189  while (true); // unhandled exception: wait for the watchdog to bite
190  }
191 }
library to query measurements from Aosong DHT11 temperature and relative humidity sensor (API) ...
#define RCC_TIM_CH(x, y)
get RCC for port based on TIMx_CHy identifier
Definition: global.h:55
#define TIM_ISR(x)
get interrupt service routine for timer base on TIM identifier
Definition: global.h:49
#define NVIC_TIM_IRQ(x)
get NVIC IRQ for timer base on TIM identifier
Definition: global.h:47
#define TIM_SR_CCIF(x)
get TIM_SR_CCxIF based on CHx identifier
Definition: global.h:84
static void sensor_dht11_reset(void)
reset all states
Definition: sensor_dht11.c:64
#define TIM_DIER_CCIE(x)
get TIM_DIER_CCxIE based on CHx identifier
Definition: global.h:86
global definitions and methods (API)
#define TIM_CCR(x, y)
get TIM_CCRy register based on TIMx_CHy identifier
Definition: global.h:88
#define SENSOR_DHT11_CHANNEL
channel used as input capture
Definition: sensor_dht11.c:41
sensor_dht11_state_t
communication state
Definition: sensor_dht11.c:48
void sensor_dht11_setup(void)
setup peripherals to communicate with sensor
Definition: sensor_dht11.c:76
volatile bool sensor_dht11_measurement_received
a measurement response has been received
Definition: sensor_dht11.c:45
volatile uint8_t sensor_dht11_bit
the bit number being sent (MSb first), up to 40
Definition: sensor_dht11.c:58
#define RCC_TIM(x)
get RCC for timer based on TIM identifier
Definition: global.h:45
#define TIM_CH_PIN(x, y)
get pin based on TIMx_CHy identifier
Definition: global.h:53
#define SENSOR_DHT11_TIMER
timer peripheral
Definition: sensor_dht11.c:40
#define TIM_IC(x)
get TIM_IC based on CHx identifier
Definition: global.h:80
bool sensor_dht11_measurement_request(void)
request measurement from sensor
Definition: sensor_dht11.c:101
#define TIM_IC_IN_TI(x)
get TIM_IC_IN_TI based on CHx identifier
Definition: global.h:82
#define TIM_CH_PORT(x, y)
get port based on TIMx_CHy identifier
Definition: global.h:51
measurement returned by sensor
Definition: sensor_dht11.h:27
struct sensor_dht11_measurement_t sensor_dht11_measurement_decode(void)
decode received measurement
Definition: sensor_dht11.c:123
uint8_t humidity
relative humidity in RH (20-95)
Definition: sensor_dht11.h:28
volatile uint8_t sensor_dht11_bits[5]
the 40 bits (5 bytes) being sent by the device
Definition: sensor_dht11.c:61
#define TIM(x)
get TIM based on TIM identifier
Definition: global.h:43
uint8_t temperature
temperature in °C (0-50)
Definition: sensor_dht11.h:29
#define SENSOR_DHT11_JITTER
signal timing jitter tolerated in timing
Definition: sensor_dht11.c:42