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