Skip to content

Issue 22 #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ AC_CONFIG_SUBDIRS([examples/0blink])
AC_CONFIG_SUBDIRS([examples/0hello])
AC_CONFIG_SUBDIRS([examples/0ledctrl])
AC_CONFIG_SUBDIRS([examples/1rmtblink])
AC_CONFIG_SUBDIRS([examples/1rmtdht])
AC_CONFIG_SUBDIRS([examples/1rmtmorse])
AC_CONFIG_SUBDIRS([examples/1rmtmusic])
AC_CONFIG_SUBDIRS([examples/1rmtws2812])
Expand All @@ -35,6 +36,7 @@ AC_CONFIG_FILES([
examples/0hello/Makefile
examples/0ledctrl/Makefile
examples/1rmtblink/Makefile
examples/1rmtdht/Makefile
examples/1rmtmorse/Makefile
examples/1rmtmusic/Makefile
examples/1rmtws2812/Makefile
Expand Down
37 changes: 37 additions & 0 deletions examples/1rmtdht/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
include $(top_srcdir)/scripts/elf2bin.mk
include $(top_srcdir)/ld/flags.mk

noinst_HEADERS = defines.h

AM_CFLAGS = -std=c11 -flto

if WITH_BINARIES
AM_LDFLAGS += \
-T $(top_srcdir)/ld/esp32.rom.ld \
-T $(top_srcdir)/ld/esp32.rom.libgcc.ld \
-T $(top_srcdir)/ld/esp32.rom.newlib-data.ld \
-T $(top_srcdir)/ld/esp32.rom.newlib-locale.ld \
-T $(top_srcdir)/ld/esp32.rom.newlib-nano.ld \
-T $(top_srcdir)/ld/esp32.rom.newlib-time.ld \
-T $(top_srcdir)/ld/esp32.rom.redefined.ld \
-T $(top_srcdir)/ld/esp32.rom.syscalls.ld
else
AM_LDFLAGS += \
-T $(top_srcdir)/ld/esp32.rom.ld \
-T $(top_srcdir)/ld/esp32.rom.libgcc.ld \
-T $(top_srcdir)/ld/esp32.rom.redefined.ld \
-T $(top_srcdir)/ld/esp32.rom.syscalls.ld
endif

AM_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/modules
LDADD = $(top_builddir)/src/libesp32basic.a $(top_builddir)/modules/libesp32modules.a

bin_PROGRAMS = \
rmtdht.elf

if WITH_BINARIES
CLEANFILES = \
rmtdht.bin
endif

BUILT_SOURCES = $(CLEANFILES)
21 changes: 21 additions & 0 deletions examples/1rmtdht/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
### DHT22 sensor sampling example

In this example we periodically read sensor data (temperature and rel. humidity)
from DHT22 external device.
Special 1-wire protocol is used when communicating to DHT22.
The 1-wire protocol is implemented in `modules/dht22.c`.

#### Hardware components

* S1: DHT22 temp/hum sensor

#### Connections

```
ESP32.GPIO2 -- S1.OUT
ESP32.GND -- S1.-
ESP32.VCC -- S1.+
```

#### Practices

39 changes: 39 additions & 0 deletions examples/1rmtdht/defines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2024 SZIGETI János
*
* This file is part of Bilis ESP32 Basic, which is released under GNU General Public License.version 3.
* See LICENSE or <https://www.gnu.org/licenses/> for full license details.
*/
#ifndef DEFINES_H
#define DEFINES_H

#ifdef __cplusplus
extern "C" {
#endif

// TIMINGS
// const -- do not change this value
#define APB_FREQ_HZ 80000000U // 80 MHz

// variables
#define TIM0_0_DIVISOR 2U
#define START_APP_CPU 0U
#define SCHEDULE_FREQ_HZ 1000U // 1KHz

// derived invariants
#define CLK_FREQ_HZ (APB_FREQ_HZ / TIM0_0_DIVISOR) // 40 MHz
#define TICKS_PER_MS (CLK_FREQ_HZ / 1000U) // 40000
#define TICKS_PER_US (CLK_FREQ_HZ / 1000000U) // 40
#define NS_PER_TICKS (1000000000 / CLK_FREQ_HZ)

#define TICKS2NS(X) ((X) * NS_PER_TICKS)
#define TICKS2US(X) ((X) / TICKS_PER_US)
#define MS2TICKS(X) ((X) * TICKS_PER_MS)
#define HZ2APBTICKS(X) (APB_FREQ_HZ / (X))

#ifdef __cplusplus
}
#endif

#endif /* DEFINES_H */

109 changes: 109 additions & 0 deletions examples/1rmtdht/rmtdht.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright 2024 SZIGETI János
*
* This file is part of Bilis ESP32 Basic, which is released under GNU General Public License.version 3.
* See LICENSE or <https://www.gnu.org/licenses/> for full license details.
*/
#include <stdbool.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "dht22.h"
#include "gpio.h"
#include "main.h"
#include "rmt.h"
#include "defines.h"
#include "romfunctions.h"
#include "iomux.h"
#include "dport.h"
#include "timg.h"
#include "utils/uartutils.h"

// =================== Hard constants =================
// #1: Timings
#define RMTDHT_PERIOD_MS 2000U ///< Higher than 8 * 0.2s, so RMT blinks will not overlap.

// #2: Channels / wires / addresses
#define RMTDHT_GPIO 21U
#define RMTDHT_CH RMT_CH0
#define RMTINT_CH 23U

// ============= Local types ===============

// ================ Local function declarations =================
static void _rmtdht_init();
static void _rmtdht_cycle(uint64_t u64Ticks);

// =================== Global constants ================
const bool gbStartAppCpu = START_APP_CPU;
const uint16_t gu16Tim00Divisor = TIM0_0_DIVISOR;
const uint64_t gu64tckSchedulePeriod = (CLK_FREQ_HZ / SCHEDULE_FREQ_HZ);

// ==================== Local Data ================
static SDht22Descriptor gsDht22Desc;

// ==================== Implementation ================

void _done_rx(void *pvParam, SDht22Data *psParam) {
uart_printf(&gsUART0, "INVALID: %02X %02X %02X %02X %02X\n",
psParam->au8Invalid[0],
psParam->au8Invalid[1],
psParam->au8Invalid[2],
psParam->au8Invalid[3],
psParam->au8Invalid[4]
);
uart_printf(&gsUART0, "DATA: %02X %02X %02X %02X %02X\n",
psParam->au8Data[0],
psParam->au8Data[1],
psParam->au8Data[2],
psParam->au8Data[3],
psParam->au8Data[4]
);
uart_printf(&gsUART0, "raw data (%c) T: %d, RH: %u\n",
dht22_data_valid(psParam) ? '+' : '-',
dht22_get_temp(psParam),
dht22_get_rhum(psParam)
);
}

static void _rmtdht_init() {
rmt_isr_init();
rmt_init_controller(true, true);
gsDht22Desc = dht22_config(RMTDHT_CH, _done_rx, NULL);
dht22_init(RMTDHT_GPIO, APB_FREQ_HZ, &gsDht22Desc);

rmt_isr_start(CPU_PRO, RMTINT_CH);
}

static void _rmtdht_cycle(uint64_t u64Ticks) {
static uint64_t u64NextTick = 0;

if (u64NextTick <= u64Ticks) {
dht22_run(&gsDht22Desc);

u64NextTick += MS2TICKS(RMTDHT_PERIOD_MS);
}
}

// ====================== Interface functions =========================

void prog_init_pro_pre() {
gsUART0.CLKDIV = APB_FREQ_HZ / 115200;

_rmtdht_init();
}

void prog_init_app() {
}

void prog_init_pro_post() {
}

void prog_cycle_app(uint64_t u64tckNow) {
}

void prog_cycle_pro(uint64_t u64tckNow) {
_rmtdht_cycle(u64tckNow);
}
2 changes: 1 addition & 1 deletion examples/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
AUTOMAKE_OPTIONS =
SUBDIRS=0blink 0button 0hello 0ledctrl 1rmtblink 1rmtmorse 1rmtmusic 3prog1 1rmtws2812
SUBDIRS=0blink 0button 0hello 0ledctrl 1rmtblink 1rmtdht 1rmtmorse 1rmtmusic 3prog1 1rmtws2812
4 changes: 2 additions & 2 deletions modules/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ libesp32modules_a_AR=$(AR) rcs

lib_LIBRARIES = libesp32modules.a

include_HEADERS = bh1750.h bme280.h ws2812.h
include_HEADERS = bh1750.h bme280.h dht22.h ws2812.h
nodist_include_HEADERS =

libesp32modules_a_SOURCES = bh1750.c bme280.c ws2812.c
libesp32modules_a_SOURCES = bh1750.c bme280.c dht22.c ws2812.c
nodist_libesp32modules_a_SOURCES =

CLEANFILES =
Expand Down
Loading
Loading