Skip to content

Commit 4e837b7

Browse files
Narukarasuda-morris
authored andcommitted
feat(ds18b20): Add one_wire UART backend
1 parent cf20174 commit 4e837b7

File tree

8 files changed

+99
-13
lines changed

8 files changed

+99
-13
lines changed

components/.build-test-rules.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ components/esp_lvgl_port/test_apps/simd:
2727
- if: IDF_TARGET in ["esp32", "esp32s3"]
2828
reason: Supports only xtensa targets
2929

30-
components/ds18b20:
30+
components/ds18b20/examples/ds18b20_read:
3131
depends_filepatterns:
3232
- "components/ds18b20/**"
3333
disable:
34-
- if: SOC_RMT_SUPPORTED != 1
35-
reason: Onewire component depends on RMT peripheral
34+
- if: CONFIG_NAME == "rmt" and SOC_RMT_SUPPORTED != 1
35+
reason: RMT backend variant requires SOC RMT support
36+
- if: CONFIG_NAME == "uart" and SOC_UART_SUPPORTED != 1
37+
reason: UART backend variant requires SOC UART support
3638

3739
components/sensors/icm42670:
3840
depends_filepatterns:

components/ds18b20/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.3.1
2+
3+
- Bump dependency `onewire_bus` to `^1.1.0` to align with new backend capabilities (including UART backend support).
4+
- Update `ds18b20_read` example to support backend selection in menuconfig (RMT/UART).
5+
16
## 0.3.0
27

38
- Add detection for uninitialized power-on state (85.0C) and return `ESP_ERR_INVALID_STATE` when this value is read.

components/ds18b20/examples/ds18b20_read/README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,25 @@ This example shows how to use the 1-Wire temperature sensor DS18B20.
44

55
### Hardware Required
66

7-
* An ESP development board with RMT peripheral (e.g ESP32, ESP32-C3, ESP32-S3, etc)
8-
* An DS18B20 sensor connected to GPIO 18. To use a different pin, modify `EXAMPLE_ONEWIRE_BUS_GPIO` in [source file](main/ds18b20_example_main.c)
7+
* An ESP development board that supports the selected 1-Wire backend (`RMT` or `UART`)
8+
* A DS18B20 sensor connected to the configured 1-Wire GPIO (default: GPIO0)
99
* An USB cable for power supply and programming
1010

11+
### Configuration
12+
13+
Run `idf.py menuconfig`, then open:
14+
15+
* `Example Configuration` -> `1-Wire backend` to choose `RMT` or `UART`
16+
* `Example Configuration` -> `1-Wire bus GPIO number` to select bus pin
17+
* `Example Configuration` -> `Enable internal pull-up resistor on bus GPIO` as needed
18+
* `Example Configuration` -> `UART port number (for UART backend)` when UART backend is selected
19+
1120
### Example Output
1221

1322
```text
1423
...
1524
I (297) main_task: Calling app_main()
16-
I (297) example: 1-Wire bus installed on GPIO18
25+
I (297) example: 1-Wire bus installed on GPIO0 by RMT backend
1726
I (297) example: Device iterator created, start searching...
1827
I (407) example: Found a DS18B20[0], address: 070822502019FC28
1928
I (517) example: Found a DS18B20[1], address: FC0921C076034628
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
menu "Example Configuration"
2+
3+
config EXAMPLE_ONEWIRE_BUS_GPIO
4+
int "1-Wire bus GPIO number"
5+
range 0 48
6+
default 0
7+
help
8+
GPIO used by the 1-Wire bus.
9+
10+
config EXAMPLE_ONEWIRE_MAX_DS18B20
11+
int "Maximum number of DS18B20 devices to enumerate"
12+
range 1 32
13+
default 2
14+
help
15+
Stop searching after this many DS18B20 devices are found.
16+
17+
config EXAMPLE_ONEWIRE_ENABLE_INTERNAL_PULLUP
18+
bool "Enable internal pull-up resistor on bus GPIO"
19+
default y
20+
help
21+
Enable this when there is no external pull-up resistor on the
22+
1-Wire data line.
23+
24+
choice EXAMPLE_ONEWIRE_BACKEND
25+
prompt "1-Wire backend"
26+
default EXAMPLE_ONEWIRE_BACKEND_RMT
27+
help
28+
Select which backend is used by onewire_bus.
29+
30+
config EXAMPLE_ONEWIRE_BACKEND_RMT
31+
bool "RMT backend"
32+
33+
config EXAMPLE_ONEWIRE_BACKEND_UART
34+
bool "UART backend"
35+
endchoice
36+
37+
config EXAMPLE_ONEWIRE_UART_PORT_NUM
38+
int "UART port number (for UART backend)"
39+
range 0 2
40+
default 1
41+
depends on EXAMPLE_ONEWIRE_BACKEND_UART
42+
help
43+
UART port index passed to onewire_new_bus_uart().
44+
45+
endmenu

components/ds18b20/examples/ds18b20_read/main/ds18b20_example_main.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2026 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Unlicense OR CC0-1.0
55
*/
66

77
#include <stdio.h>
88
#include "esp_log.h"
9+
#include "sdkconfig.h"
910
#include "freertos/FreeRTOS.h"
1011
#include "freertos/task.h"
1112
#include "onewire_bus.h"
1213
#include "ds18b20.h"
1314

14-
#define EXAMPLE_ONEWIRE_BUS_GPIO 18
15-
#define EXAMPLE_ONEWIRE_MAX_DS18B20 2
15+
#if CONFIG_EXAMPLE_ONEWIRE_ENABLE_INTERNAL_PULLUP
16+
#define EXAMPLE_ONEWIRE_ENABLE_INTERNAL_PULLUP 1
17+
#else
18+
#define EXAMPLE_ONEWIRE_ENABLE_INTERNAL_PULLUP 0
19+
#endif
20+
21+
#if CONFIG_EXAMPLE_ONEWIRE_BACKEND_UART
22+
#define EXAMPLE_ONEWIRE_UART_PORT_NUM CONFIG_EXAMPLE_ONEWIRE_UART_PORT_NUM
23+
#endif
24+
25+
#define EXAMPLE_ONEWIRE_BUS_GPIO CONFIG_EXAMPLE_ONEWIRE_BUS_GPIO
26+
#define EXAMPLE_ONEWIRE_MAX_DS18B20 CONFIG_EXAMPLE_ONEWIRE_MAX_DS18B20
1627

1728
static const char *TAG = "example";
1829

@@ -23,14 +34,25 @@ void app_main(void)
2334
onewire_bus_config_t bus_config = {
2435
.bus_gpio_num = EXAMPLE_ONEWIRE_BUS_GPIO,
2536
.flags = {
26-
.en_pull_up = true, // enable the internal pull-up resistor in case the external device didn't have one
37+
.en_pull_up = EXAMPLE_ONEWIRE_ENABLE_INTERNAL_PULLUP,
2738
}
2839
};
40+
#if CONFIG_EXAMPLE_ONEWIRE_BACKEND_RMT
2941
onewire_bus_rmt_config_t rmt_config = {
3042
.max_rx_bytes = 10, // 1byte ROM command + 8byte ROM number + 1byte device command
3143
};
3244
ESP_ERROR_CHECK(onewire_new_bus_rmt(&bus_config, &rmt_config, &bus));
33-
ESP_LOGI(TAG, "1-Wire bus installed on GPIO%d", EXAMPLE_ONEWIRE_BUS_GPIO);
45+
ESP_LOGI(TAG, "1-Wire bus installed on GPIO%d by RMT backend", EXAMPLE_ONEWIRE_BUS_GPIO);
46+
#elif CONFIG_EXAMPLE_ONEWIRE_BACKEND_UART
47+
onewire_bus_uart_config_t uart_config = {
48+
.uart_port_num = EXAMPLE_ONEWIRE_UART_PORT_NUM,
49+
};
50+
ESP_ERROR_CHECK(onewire_new_bus_uart(&bus_config, &uart_config, &bus));
51+
ESP_LOGI(TAG, "1-Wire bus installed on GPIO%d by UART backend (UART%d)",
52+
EXAMPLE_ONEWIRE_BUS_GPIO, EXAMPLE_ONEWIRE_UART_PORT_NUM);
53+
#else
54+
#error "No 1-Wire backend selected in menuconfig"
55+
#endif
3456

3557
int ds18b20_device_num = 0;
3658
ds18b20_device_handle_t ds18b20s[EXAMPLE_ONEWIRE_MAX_DS18B20];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_EXAMPLE_ONEWIRE_BACKEND_RMT=y
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_EXAMPLE_ONEWIRE_BACKEND_UART=y
2+
CONFIG_EXAMPLE_ONEWIRE_UART_PORT_NUM=1
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
version: "0.3.0"
1+
version: "0.3.1"
22
description: DS18B20 device driver
33
url: https://github.com/espressif/esp-bsp/tree/master/components/ds18b20
44
dependencies:
5-
onewire_bus: "^1.0.0"
5+
onewire_bus: "^1.1.0"

0 commit comments

Comments
 (0)