2021-05-01 20:06:51 +03:00
|
|
|
/*
|
|
|
|
* This file is part of the libopencm3 project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
|
|
|
*
|
|
|
|
* This library is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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 Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-06-28 00:13:08 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2021-05-01 20:06:51 +03:00
|
|
|
#include <libopencm3/cm3/nvic.h>
|
|
|
|
#include <libopencm3/stm32/rcc.h>
|
|
|
|
#include <libopencm3/stm32/gpio.h>
|
|
|
|
#include <libopencm3/stm32/timer.h>
|
|
|
|
|
2021-06-27 18:32:43 +03:00
|
|
|
#include <SEGGER_RTT.h>
|
2021-06-26 11:34:10 +03:00
|
|
|
|
2021-06-28 00:13:08 +03:00
|
|
|
#define DHT_PORT GPIOB
|
|
|
|
#define DHT_PIN GPIO0
|
|
|
|
|
|
|
|
enum DHT11_STATE {
|
|
|
|
DHT11_STOP, // Stop statemachine
|
|
|
|
DHT11_BEGIN_START, // We drive DATA Low for min 18ms
|
|
|
|
DHT11_END_START, // We drive DATA High and set it to input
|
|
|
|
DHT11_RESPONSE, // Wait for DHT to drive DATA Low
|
|
|
|
DHT11_DATA,
|
|
|
|
DHT11_END
|
|
|
|
};
|
|
|
|
|
|
|
|
int g_dht11_state = DHT11_STOP;
|
|
|
|
|
|
|
|
int g_dht_i = 0;
|
|
|
|
uint16_t g_dht_vals[100] = {0};
|
|
|
|
|
2021-05-01 20:06:51 +03:00
|
|
|
static void delay(int count)
|
|
|
|
{
|
|
|
|
for (int i=0; i < count; i++) {
|
|
|
|
__asm__("nop");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gpio_setup(void)
|
|
|
|
{
|
|
|
|
/* Enable GPIOA clock. */
|
|
|
|
/* Manually: */
|
|
|
|
// RCC_APB2ENR |= RCC_APB2ENR_IOPCEN;
|
|
|
|
/* Using API functions: */
|
|
|
|
rcc_periph_clock_enable(RCC_GPIOB);
|
|
|
|
|
|
|
|
/* Set GPIO5 (in GPIO port A) to 'output push-pull'. */
|
|
|
|
/* Manually: */
|
|
|
|
// GPIOA_CRH = (GPIO_CNF_OUTPUT_PUSHPULL << (((5 - 8) * 4) + 2));
|
|
|
|
// GPIOA_CRH |= (GPIO_MODE_OUTPUT_2_MHZ << ((5 - 8) * 4));
|
|
|
|
/* Using API functions: */
|
2021-06-28 00:13:08 +03:00
|
|
|
gpio_set_mode(DHT_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, DHT_PIN);
|
2021-05-01 20:06:51 +03:00
|
|
|
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO1);
|
|
|
|
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tim_setup(void)
|
|
|
|
{
|
|
|
|
nvic_enable_irq(NVIC_TIM3_IRQ);
|
2021-06-28 00:13:08 +03:00
|
|
|
nvic_set_priority(NVIC_TIM3_IRQ, 1); // interrupts will not work without it???
|
2021-05-01 20:06:51 +03:00
|
|
|
|
|
|
|
rcc_periph_clock_enable(RCC_TIM3);
|
|
|
|
rcc_periph_reset_pulse(RST_TIM3);
|
|
|
|
|
|
|
|
timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
|
2021-06-28 00:13:08 +03:00
|
|
|
timer_set_prescaler(TIM3, (rcc_apb1_frequency*2) / 1000000 - 1); // set timer tickrate to 1mhz
|
2021-05-01 20:06:51 +03:00
|
|
|
timer_set_period(TIM3, 0xffff);
|
2021-06-28 00:13:08 +03:00
|
|
|
//timer_one_shot_mode(TIM3);
|
2021-05-01 20:06:51 +03:00
|
|
|
|
|
|
|
timer_ic_set_input(TIM3, TIM_IC3, TIM_IC_IN_TI3);
|
2021-06-28 00:13:08 +03:00
|
|
|
//timer_ic_set_filter(TIM3, TIM_IC3, 0b1111); // does not do anything ????
|
2021-05-01 20:06:51 +03:00
|
|
|
//timer_ic_set_polarity(TIM3, TIM_IC3, TIM_IC_FALLING); // TIM_IC_RISING
|
|
|
|
//timer_ic_enable(TIM3, TIM_IC3);
|
|
|
|
|
|
|
|
timer_enable_irq(TIM3, TIM_DIER_UIE);
|
2021-06-28 00:13:08 +03:00
|
|
|
timer_enable_irq(TIM3, TIM_DIER_CC1IE); // triggered when DHT11_END_START
|
|
|
|
timer_enable_irq(TIM3, TIM_DIER_CC3IE); // triggered when DHT drives DATA pin
|
2021-05-01 20:06:51 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void tim3_isr(void)
|
|
|
|
{
|
2021-06-28 00:13:08 +03:00
|
|
|
// Timer overflow that should only happen if DHT11 does not answer
|
|
|
|
if (timer_get_flag(TIM3, TIM_SR_UIF)) {
|
2021-05-01 20:06:51 +03:00
|
|
|
|
|
|
|
timer_clear_flag(TIM3, TIM_SR_UIF);
|
2021-06-28 00:13:08 +03:00
|
|
|
//gpio_toggle(GPIOB, GPIO2);
|
|
|
|
if (g_dht11_state == DHT11_RESPONSE) {
|
|
|
|
puts("no response");
|
|
|
|
} else if (g_dht11_state == DHT11_DATA) {
|
|
|
|
puts("got data");
|
|
|
|
} else {
|
|
|
|
puts("overflow");
|
|
|
|
}
|
|
|
|
g_dht11_state = DHT11_STOP;
|
|
|
|
timer_disable_counter(TIM3);
|
2021-05-01 20:06:51 +03:00
|
|
|
|
|
|
|
//timer_set_counter(TIM3, 0);
|
|
|
|
//timer_clear_flag(TIM3, TIM_SR_UIF);
|
|
|
|
//timer_set_oc_value(TIM3, TIM_OC1, 1000);
|
2021-06-28 00:13:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// handle DHT11_END_START
|
|
|
|
else if (timer_get_flag(TIM3, TIM_SR_CC1IF)) {
|
|
|
|
timer_clear_flag(TIM3, TIM_SR_CC1IF);
|
|
|
|
//gpio_toggle(GPIOB, GPIO2);
|
|
|
|
if (g_dht11_state == DHT11_BEGIN_START) {
|
|
|
|
g_dht11_state = DHT11_END_START;
|
|
|
|
|
|
|
|
// And make the GPIO input so that we can wait for response
|
|
|
|
gpio_set(DHT_PORT, DHT_PIN);
|
2021-07-07 23:37:56 +03:00
|
|
|
gpio_set_mode(DHT_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, DHT_PIN);
|
2021-05-01 20:06:51 +03:00
|
|
|
|
|
|
|
// setup timer to wait for start response from dht11
|
2021-07-07 23:37:56 +03:00
|
|
|
timer_ic_set_polarity(TIM3, TIM_IC3, TIM_IC_FALLING);
|
|
|
|
//timer_ic_set_polarity(TIM3, TIM_IC3, TIM_IC_RISING);// TIM_IC_RISING
|
2021-06-28 00:13:08 +03:00
|
|
|
timer_clear_flag(TIM3, TIM_SR_CC3IF);
|
2021-07-07 23:37:56 +03:00
|
|
|
timer_ic_enable(TIM3, TIM_IC3);
|
2021-06-28 00:13:08 +03:00
|
|
|
//puts("waiting for response");
|
2021-05-01 20:06:51 +03:00
|
|
|
}
|
2021-07-07 23:37:56 +03:00
|
|
|
//gpio_toggle(GPIOB, GPIO2);
|
2021-06-28 00:13:08 +03:00
|
|
|
}
|
2021-05-01 20:06:51 +03:00
|
|
|
|
2021-06-28 00:13:08 +03:00
|
|
|
else if (timer_get_flag(TIM3, TIM_SR_CC3IF)) // PB0 changed value
|
2021-05-01 20:06:51 +03:00
|
|
|
{
|
2021-07-07 23:37:56 +03:00
|
|
|
gpio_toggle(GPIOB, GPIO2);
|
|
|
|
//puts("fall");
|
2021-06-28 00:13:08 +03:00
|
|
|
if (g_dht11_state == DHT11_END_START) {
|
|
|
|
g_dht11_state = DHT11_RESPONSE;
|
2021-07-07 23:37:56 +03:00
|
|
|
//gpio_toggle(GPIOB, GPIO2);
|
2021-06-28 00:13:08 +03:00
|
|
|
//timer_ic_disable(TIM3, TIM_IC3);
|
|
|
|
//timer_clear_flag(TIM3, TIM_SR_CC3IF);
|
|
|
|
//timer_ic_set_polarity(TIM3, TIM_IC3, TIM_IC_FALLING);// TIM_IC_RISING
|
2021-07-07 23:37:56 +03:00
|
|
|
g_dht_vals[g_dht_i++] = TIM_CCR3(TIM3); // store captured high time value
|
2021-06-28 00:13:08 +03:00
|
|
|
//timer_ic_enable(TIM3, TIM_IC3);
|
2021-07-07 23:37:56 +03:00
|
|
|
//gpio_toggle(GPIOB, GPIO2);
|
2021-06-28 00:13:08 +03:00
|
|
|
} else {
|
2021-07-07 23:37:56 +03:00
|
|
|
//gpio_toggle(GPIOB, GPIO2);
|
2021-06-28 00:13:08 +03:00
|
|
|
g_dht11_state = DHT11_DATA;
|
|
|
|
g_dht_vals[g_dht_i] = TIM_CCR3(TIM3) - g_dht_vals[g_dht_i-1]; // store length from last sample
|
|
|
|
g_dht_i++;
|
2021-07-07 23:37:56 +03:00
|
|
|
//gpio_toggle(GPIOB, GPIO2);
|
2021-06-28 00:13:08 +03:00
|
|
|
}
|
2021-07-07 23:37:56 +03:00
|
|
|
timer_clear_flag(TIM3, TIM_SR_CC3IF);
|
|
|
|
gpio_toggle(GPIOB, GPIO2);
|
2021-05-01 20:06:51 +03:00
|
|
|
|
2021-06-28 00:13:08 +03:00
|
|
|
// if (timer_get_flag(TIM3, TIM_SR_CC3OF))
|
|
|
|
// {
|
|
|
|
// timer_clear_flag(TIM3, TIM_SR_CC3OF);
|
|
|
|
// puts("we are too slow to read and clear timer input capture flags");
|
|
|
|
// }
|
|
|
|
}
|
2021-05-01 20:06:51 +03:00
|
|
|
|
2021-06-28 00:13:08 +03:00
|
|
|
}
|
2021-05-01 20:06:51 +03:00
|
|
|
|
2021-06-28 00:13:08 +03:00
|
|
|
static void dht_start(void)
|
|
|
|
{
|
|
|
|
timer_disable_counter(TIM3);
|
2021-07-07 23:37:56 +03:00
|
|
|
timer_ic_disable(TIM3, TIM_IC3);
|
2021-06-28 00:13:08 +03:00
|
|
|
g_dht11_state = DHT11_BEGIN_START;
|
|
|
|
g_dht_i = 0;
|
|
|
|
|
|
|
|
// Setup DHT11_END_START trigger on output compare 1
|
|
|
|
timer_set_counter(TIM3, 0);
|
|
|
|
timer_set_oc_value(TIM3, TIM_OC1, 1000*20); // 20ms
|
|
|
|
timer_clear_flag(TIM3, TIM_SR_CC1IF);
|
2021-07-07 23:37:56 +03:00
|
|
|
timer_clear_flag(TIM3, TIM_SR_CC3IF);
|
|
|
|
|
2021-06-28 00:13:08 +03:00
|
|
|
|
|
|
|
// Do DHT11_BEGIN_START aka drive DATA Low
|
|
|
|
gpio_set_mode(DHT_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, DHT_PIN);
|
|
|
|
gpio_clear(DHT_PORT, DHT_PIN);
|
|
|
|
|
|
|
|
// And start counting
|
|
|
|
timer_enable_counter(TIM3);
|
|
|
|
|
|
|
|
//gpio_toggle(GPIOB, GPIO2);
|
2021-05-01 20:06:51 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dht_start_signal(void)
|
|
|
|
{
|
2021-06-28 00:13:08 +03:00
|
|
|
gpio_set_mode(DHT_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, DHT_PIN);
|
|
|
|
gpio_clear(DHT_PORT, DHT_PIN);
|
|
|
|
delay(100000);
|
2021-05-01 20:06:51 +03:00
|
|
|
//delay(30000);
|
2021-06-28 00:13:08 +03:00
|
|
|
gpio_set(DHT_PORT, DHT_PIN);
|
2021-05-01 20:06:51 +03:00
|
|
|
delay(100);
|
2021-06-28 00:13:08 +03:00
|
|
|
gpio_set_mode(DHT_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, DHT_PIN);
|
2021-05-01 20:06:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2021-06-27 18:32:43 +03:00
|
|
|
int i = 0;
|
2021-06-28 00:13:08 +03:00
|
|
|
puts("Start");
|
2021-05-01 20:06:51 +03:00
|
|
|
rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]);
|
|
|
|
gpio_setup();
|
2021-06-28 00:13:08 +03:00
|
|
|
tim_setup();
|
2021-06-26 11:34:10 +03:00
|
|
|
|
2021-05-01 20:06:51 +03:00
|
|
|
/* Blink the LED (PA5) on the board. */
|
|
|
|
while (1) {
|
|
|
|
/* Using API function gpio_toggle(): */
|
|
|
|
gpio_toggle(GPIOB, GPIO1); /* LED on/off */
|
2021-06-28 00:13:08 +03:00
|
|
|
delay(10000000);
|
|
|
|
dht_start();
|
2021-06-27 18:32:43 +03:00
|
|
|
|
|
|
|
i++;
|
2021-06-28 00:13:08 +03:00
|
|
|
SEGGER_RTT_printf(0, "Poll: %d\n", i);
|
2021-05-01 20:06:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|