Skip to content

Commit 740a580

Browse files
authored
Merge pull request #510 from JellevanKraaij/master
feat(ds18b20): Add single device mode (no enumeration 1-Wire bus) (BSP-637)
2 parents 56792df + 29daa0a commit 740a580

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

components/ds18b20/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.1.2
2+
3+
- Add single device function (ds18b20_new_single_device) to create a new DS18B20 device instance without enumerating all devices on the bus.
4+
15
## 0.1.1
26

37
- Fix the issue that sign-bit is not extended properly when doing temperature value conversion.

components/ds18b20/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "0.1.1"
1+
version: "0.1.2"
22
description: DS18B20 device driver
33
url: https://github.com/espressif/esp-bsp/tree/master/components/ds18b20
44
dependencies:

components/ds18b20/include/ds18b20.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ typedef struct {
4242
*/
4343
esp_err_t ds18b20_new_device(onewire_device_t *device, const ds18b20_config_t *config, ds18b20_device_handle_t *ret_ds18b20);
4444

45+
/**
46+
* @brief Create a new singe device bus DS18B20 device based on a 1-Wire bus (not enumerated)
47+
* this function assumes that the device is a DS18B20 device and there is only one device on the bus
48+
*
49+
* @param[in] bus 1-Wire bus handle
50+
* @param[in] config DS18B20 configuration
51+
* @param[out] ret_ds18b20 Returned DS18B20 device handle
52+
* @return esp_err_t
53+
* - ESP_OK: Create DS18B20 device successfully
54+
* - ESP_ERR_INVALID_ARG: Create DS18B20 device failed due to invalid argument
55+
* - ESP_ERR_NO_MEM: Create DS18B20 device failed due to out of memory
56+
* - ESP_FAIL: Create DS18B20 device failed due to other reasons
57+
*/
58+
esp_err_t ds18b20_new_single_device(onewire_bus_handle_t bus, const ds18b20_config_t *config, ds18b20_device_handle_t *ret_ds18b20);
59+
4560
/**
4661
* @brief Delete DS18B20 device
4762
*

components/ds18b20/src/ds18b20.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ typedef struct {
3535

3636
typedef struct ds18b20_device_t {
3737
onewire_bus_handle_t bus;
38+
bool single_mode;
3839
onewire_device_address_t addr;
3940
uint8_t th_user1;
4041
uint8_t tl_user2;
@@ -61,6 +62,22 @@ esp_err_t ds18b20_new_device(onewire_device_t *device, const ds18b20_config_t *c
6162
return ESP_OK;
6263
}
6364

65+
esp_err_t ds18b20_new_single_device(onewire_bus_handle_t bus, const ds18b20_config_t *config, ds18b20_device_handle_t *ret_ds18b20)
66+
{
67+
ds18b20_device_t *ds18b20 = NULL;
68+
ESP_RETURN_ON_FALSE(bus && config && ret_ds18b20, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
69+
70+
ds18b20 = calloc(1, sizeof(ds18b20_device_t));
71+
ESP_RETURN_ON_FALSE(ds18b20, ESP_ERR_NO_MEM, TAG, "no mem for ds18b20");
72+
ds18b20->bus = bus;
73+
ds18b20->addr = 0;
74+
ds18b20->single_mode = true;
75+
ds18b20->resolution = DS18B20_RESOLUTION_12B; // DS18B20 default resolution is 12 bits
76+
77+
*ret_ds18b20 = ds18b20;
78+
return ESP_OK;
79+
}
80+
6481
esp_err_t ds18b20_del_device(ds18b20_device_handle_t ds18b20)
6582
{
6683
ESP_RETURN_ON_FALSE(ds18b20, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
@@ -70,6 +87,11 @@ esp_err_t ds18b20_del_device(ds18b20_device_handle_t ds18b20)
7087

7188
static esp_err_t ds18b20_send_command(ds18b20_device_handle_t ds18b20, uint8_t cmd)
7289
{
90+
// No addres mode (singe device connectd to the bus) created using ds18b20_new_single_device
91+
if (ds18b20->single_mode) {
92+
uint8_t tx_buffer[2] = {ONEWIRE_CMD_SKIP_ROM, cmd};
93+
return onewire_bus_write_bytes(ds18b20->bus, tx_buffer, sizeof(tx_buffer));
94+
}
7395
// send command
7496
uint8_t tx_buffer[10] = {0};
7597
tx_buffer[0] = ONEWIRE_CMD_MATCH_ROM;

0 commit comments

Comments
 (0)