Cleanup code
This commit is contained in:
parent
e29a4a277a
commit
38e3c715dc
164
main.c
164
main.c
@ -31,18 +31,21 @@
|
||||
|
||||
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_START, // We drive DATA Low for minimum 18ms and then drive it HIGH and start waiting for response
|
||||
DHT11_RESPONSE, // Wait for DHT to drive DATA Low
|
||||
DHT11_DATA, // Receiving data
|
||||
DHT11_END, // Got last bit
|
||||
DHT11_TIMEOUT, // Not response or data got
|
||||
DHT11_TIMEOUT, // Not response or got partial data only
|
||||
};
|
||||
|
||||
int g_dht11_state = DHT11_STOP;
|
||||
|
||||
int g_dht_i = 0;
|
||||
uint16_t g_dht_vals[43] = {0};
|
||||
// 2 pulses are the DHT Response and last 40 are data pulses for 5 bytes
|
||||
// so we have total of 42 pulses
|
||||
#define DHT11_BIT_COUNT 42
|
||||
int g_dht_i = 0; // how many bits we have captured
|
||||
uint16_t g_dht_vals[DHT11_BIT_COUNT] = {0}; // captured bit times in us
|
||||
|
||||
|
||||
static void delay(int count)
|
||||
{
|
||||
@ -64,11 +67,15 @@ static void gpio_setup(void)
|
||||
// GPIOA_CRH = (GPIO_CNF_OUTPUT_PUSHPULL << (((5 - 8) * 4) + 2));
|
||||
// GPIOA_CRH |= (GPIO_MODE_OUTPUT_2_MHZ << ((5 - 8) * 4));
|
||||
/* Using API functions: */
|
||||
// DHT Data pin
|
||||
gpio_set_mode(DHT_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, DHT_PIN);
|
||||
// Maple mini led
|
||||
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO1);
|
||||
// A random DEBUG pin we can toggle and measure with logic
|
||||
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO2);
|
||||
}
|
||||
|
||||
// Onetime config of timer things that stay the same
|
||||
static void tim_setup(void)
|
||||
{
|
||||
nvic_enable_irq(NVIC_TIM3_IRQ);
|
||||
@ -77,115 +84,109 @@ static void tim_setup(void)
|
||||
rcc_periph_clock_enable(RCC_TIM3);
|
||||
rcc_periph_reset_pulse(RST_TIM3);
|
||||
|
||||
// configure the timer to be a normal up counting timer, nothing fancy
|
||||
timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
|
||||
timer_set_prescaler(TIM3, (rcc_apb1_frequency*2) / 1000000 - 1); // set timer tickrate to 1mhz
|
||||
timer_set_period(TIM3, 0xffff);
|
||||
//timer_one_shot_mode(TIM3);
|
||||
timer_set_period(TIM3, 0xffff); // ~64 ms, DHT11 packet is never longer than 26ms
|
||||
|
||||
timer_ic_set_input(TIM3, TIM_IC3, TIM_IC_IN_TI3);
|
||||
//timer_ic_set_filter(TIM3, TIM_IC3, 0b1111); // does not do anything ????
|
||||
//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);
|
||||
timer_enable_irq(TIM3, TIM_DIER_CC1IE); // triggered when DHT11_END_START
|
||||
timer_enable_irq(TIM3, TIM_DIER_UIE); // overflow
|
||||
timer_enable_irq(TIM3, TIM_DIER_CC3IE); // triggered when DHT drives DATA pin
|
||||
|
||||
}
|
||||
|
||||
void tim3_isr(void)
|
||||
{
|
||||
// Timer overflow that should only happen if DHT11 does not answer
|
||||
if (timer_get_flag(TIM3, TIM_SR_UIF)) {
|
||||
|
||||
timer_clear_flag(TIM3, TIM_SR_UIF);
|
||||
//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");
|
||||
}
|
||||
puts("overflow");
|
||||
g_dht11_state = DHT11_TIMEOUT;
|
||||
timer_disable_counter(TIM3);
|
||||
|
||||
//timer_set_counter(TIM3, 0);
|
||||
//timer_clear_flag(TIM3, TIM_SR_UIF);
|
||||
//timer_set_oc_value(TIM3, TIM_OC1, 1000);
|
||||
}
|
||||
|
||||
// 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);
|
||||
gpio_set_mode(DHT_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, DHT_PIN);
|
||||
|
||||
// setup timer to wait for start response from dht11
|
||||
timer_ic_set_polarity(TIM3, TIM_IC3, TIM_IC_FALLING);
|
||||
timer_clear_flag(TIM3, TIM_SR_CC3IF);
|
||||
timer_ic_enable(TIM3, TIM_IC3);
|
||||
//puts("waiting for response");
|
||||
}
|
||||
//gpio_toggle(GPIOB, GPIO2);
|
||||
}
|
||||
|
||||
else if (timer_get_flag(TIM3, TIM_SR_CC3IF)) // PB0 changed value
|
||||
{
|
||||
gpio_toggle(GPIOB, GPIO2);
|
||||
//puts("fall");
|
||||
if (g_dht11_state == DHT11_END_START) {
|
||||
//gpio_toggle(GPIOB, GPIO2);
|
||||
|
||||
// handle DHT11_END_START
|
||||
if (g_dht11_state == DHT11_START) {
|
||||
// Drive DATA pin high for a split second so that we have a nice sharp edge
|
||||
gpio_set(DHT_PORT, DHT_PIN);
|
||||
// Make the GPIO input so that we can wait for response
|
||||
// there is a external pull-up connected to the DATA pin
|
||||
// probably could use the internal pull-up also...
|
||||
gpio_set_mode(DHT_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, DHT_PIN);
|
||||
|
||||
// setup timer input capture to wait for start response from dht11
|
||||
// need to disable input capture before we can configure it.
|
||||
timer_ic_disable(TIM3, TIM_IC3);
|
||||
timer_ic_set_input(TIM3, TIM_IC3, TIM_IC_IN_TI3);
|
||||
timer_ic_set_polarity(TIM3, TIM_IC3, TIM_IC_FALLING);
|
||||
timer_clear_flag(TIM3, TIM_SR_CC3IF); // otherwise we will get a invalid interrupt
|
||||
timer_ic_enable(TIM3, TIM_IC3);
|
||||
|
||||
//puts("waiting for response");
|
||||
g_dht11_state = DHT11_RESPONSE;
|
||||
//gpio_toggle(GPIOB, GPIO2);
|
||||
//timer_ic_disable(TIM3, TIM_IC3);
|
||||
//timer_clear_flag(TIM3, TIM_SR_CC3IF);
|
||||
//timer_ic_set_polarity(TIM3, TIM_IC3, TIM_IC_RISING);
|
||||
g_dht_vals[g_dht_i++] = TIM_CCR3(TIM3); // store captured high time value
|
||||
//timer_ic_enable(TIM3, TIM_IC3);
|
||||
//gpio_toggle(GPIOB, GPIO2);
|
||||
} else {
|
||||
//gpio_toggle(GPIOB, GPIO2);
|
||||
g_dht11_state = DHT11_DATA;
|
||||
g_dht_vals[g_dht_i++] = TIM_CCR3(TIM3); // store length from last sample
|
||||
//gpio_toggle(GPIOB, GPIO2);
|
||||
}
|
||||
|
||||
if (g_dht_i > 41) {
|
||||
// we have the whole packet
|
||||
// DHT11 drove DATA low and has started talking to us
|
||||
else if (g_dht11_state == DHT11_RESPONSE) {
|
||||
// store time when DHT11 pulled DATA low
|
||||
g_dht_vals[g_dht_i++] = TIM_CCR3(TIM3);
|
||||
g_dht11_state = DHT11_DATA;
|
||||
}
|
||||
|
||||
// DHT11 is sending data to us
|
||||
else {
|
||||
// store length of the data bit
|
||||
g_dht_vals[g_dht_i++] = TIM_CCR3(TIM3);
|
||||
}
|
||||
|
||||
// Did we just capture our last bit?
|
||||
if (g_dht_i >= DHT11_BIT_COUNT) {
|
||||
// yes we did
|
||||
g_dht11_state = DHT11_END;
|
||||
timer_disable_counter(TIM3);
|
||||
}
|
||||
|
||||
timer_clear_flag(TIM3, TIM_SR_CC3IF);
|
||||
gpio_toggle(GPIOB, GPIO2);
|
||||
//gpio_toggle(GPIOB, GPIO2);
|
||||
}
|
||||
|
||||
// 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");
|
||||
// }
|
||||
// Should only happen if TIM_SR_CC3IF is too slow
|
||||
// at 72mhz it should never happen but could at lower cpu speeds
|
||||
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");
|
||||
g_dht11_state = DHT11_TIMEOUT;
|
||||
timer_disable_counter(TIM3);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Setup the timer and trigger DHT11 Start
|
||||
static void dht_start(void)
|
||||
{
|
||||
timer_disable_counter(TIM3);
|
||||
timer_ic_disable(TIM3, TIM_IC3);
|
||||
g_dht11_state = DHT11_BEGIN_START;
|
||||
g_dht11_state = DHT11_STOP;
|
||||
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);
|
||||
timer_clear_flag(TIM3, TIM_SR_CC3IF);
|
||||
|
||||
// capture compare channel must be disabled before we can configure it
|
||||
timer_ic_disable(TIM3, TIM_IC3);
|
||||
// set channel as output so that we can get an interrupt when OC3 values matches the timer counter
|
||||
timer_ic_set_input(TIM3, TIM_IC3, TIM_IC_OUT);
|
||||
// tick rate should be 1Mhz (via timer prescaler), so this should trigger in 20ms
|
||||
timer_set_oc_value(TIM3, TIM_OC3, 1000*20);
|
||||
// clear status flags so that we dont get any stray interrupts
|
||||
timer_clear_flag(TIM3, TIM_SR_CC3IF);
|
||||
// and make sure that we can get interrupts
|
||||
timer_ic_enable(TIM3, TIM_IC3);
|
||||
|
||||
// Begin the statemachine
|
||||
g_dht11_state = DHT11_START;
|
||||
|
||||
// Do DHT11_BEGIN_START aka drive DATA Low
|
||||
gpio_set_mode(DHT_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, DHT_PIN);
|
||||
@ -193,18 +194,20 @@ static void dht_start(void)
|
||||
|
||||
// And start counting
|
||||
timer_enable_counter(TIM3);
|
||||
|
||||
//gpio_toggle(GPIOB, GPIO2);
|
||||
|
||||
}
|
||||
|
||||
// Parse received DHT11 bit timing data from g_dht_vals and print out pretty values
|
||||
static void dht_parse_data(){
|
||||
int8_t data[5] = {0};
|
||||
uint8_t checksum = 0;
|
||||
int data_index = 0;
|
||||
int data_bit = 7;
|
||||
for (int i=2; i < 42; i++){ // First 2 pulses are start of response, ignore them
|
||||
|
||||
// create bytes from bits
|
||||
for (int i=2; i < DHT11_BIT_COUNT; i++){ // First 2 pulses are start of response, ignore them
|
||||
uint16_t bit_time = g_dht_vals[i] - g_dht_vals[i-1];
|
||||
|
||||
// ~80us is 0 and ~120us is 1
|
||||
if (bit_time > 100) {
|
||||
data[data_index] |= 1 << data_bit;
|
||||
}
|
||||
@ -227,6 +230,7 @@ static void dht_parse_data(){
|
||||
}
|
||||
}
|
||||
|
||||
// Just toggle the data pin, used only for debugging
|
||||
static void dht_start_signal(void)
|
||||
{
|
||||
gpio_set_mode(DHT_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, DHT_PIN);
|
||||
|
Loading…
Reference in New Issue
Block a user