Skip to content

Commit

Permalink
Merge pull request #16 from junkfix/junkfix-patch-1
Browse files Browse the repository at this point in the history
Possible compatiblity with ESP32C3
  • Loading branch information
junkfix committed Jul 4, 2024
2 parents c8d7849 + 9036888 commit e147627
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
22 changes: 6 additions & 16 deletions OneWireESP32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#define OW_SLOT_RECOVERY 5
#define OW_TIMEOUT 50

const size_t owbuflen = 64 * sizeof(rmt_symbol_word_t);

static rmt_symbol_word_t ow_bit0 = {
.duration0 = OW_SLOT_START + OW_SLOT_BIT,
Expand Down Expand Up @@ -74,7 +73,7 @@ OneWire32::OneWire32(uint8_t pin){
.gpio_num = owpin,
.clk_src = RMT_CLK_SRC_APB,
.resolution_hz = 1000000,
.mem_block_symbols = 64
.mem_block_symbols = MAX_BLOCKS
};

if(rmt_new_rx_channel(&rxconf, &(owrx)) != ESP_OK) {
Expand All @@ -85,15 +84,15 @@ OneWire32::OneWire32(uint8_t pin){
.gpio_num = owpin,
.clk_src = RMT_CLK_SRC_APB,
.resolution_hz = 1000000,
.mem_block_symbols = 64,
.mem_block_symbols = MAX_BLOCKS,
.trans_queue_depth = 4,
.flags = {
.io_loop_back = 1,
.io_od_mode = 1
}
};

if(rmt_new_tx_channel(&txconf, &(owtx)) != ESP_OK) {
if(rmt_new_tx_channel(&txconf, &owtx) != ESP_OK) {
return;
}

Expand All @@ -102,11 +101,6 @@ OneWire32::OneWire32(uint8_t pin){
return;
}

owbuf = (rmt_symbol_word_t *)malloc(owbuflen);
if(owbuf == NULL) {
return;
}

rmt_rx_event_callbacks_t rx_callbacks = {
.on_recv_done = owrxdone
};
Expand Down Expand Up @@ -154,9 +148,6 @@ OneWire32::~OneWire32(){
if(owqueue) {
vQueueDelete(owqueue);
}
if(owbuf != NULL) {
free(owbuf);
}
drv = 0;
}

Expand All @@ -178,7 +169,7 @@ bool OneWire32::reset(){
};

rmt_rx_done_event_data_t evt;
rmt_receive(owrx, owbuf, owbuflen, &owrxconf);
rmt_receive(owrx, owbuf, sizeof(owbuf), &owrxconf);
rmt_transmit(owtx, owcenc, &symbol_reset, sizeof(rmt_symbol_word_t), &owtxconf);
bool found = false;
if(xQueueReceive(owqueue, &evt, pdMS_TO_TICKS(OW_TIMEOUT)) == pdTRUE) {
Expand Down Expand Up @@ -209,7 +200,7 @@ bool OneWire32::reset(){
bool OneWire32::read(uint8_t &data, uint8_t len){

rmt_rx_done_event_data_t evt;
rmt_receive(owrx, owbuf, owbuflen, &owrxconf);
rmt_receive(owrx, owbuf, sizeof(owbuf), &owrxconf);

if(!write((len > 1)? 0xff : 1, len) || xQueueReceive(owqueue, &evt, pdMS_TO_TICKS(OW_TIMEOUT)) != pdTRUE) {
return false;
Expand All @@ -230,7 +221,6 @@ bool OneWire32::read(uint8_t &data, uint8_t len){


bool OneWire32::write(const uint8_t data, uint8_t len){
size_t ssz = sizeof(rmt_symbol_word_t);

if(len < 8){
const rmt_symbol_word_t *sb;
Expand All @@ -239,7 +229,7 @@ bool OneWire32::write(const uint8_t data, uint8_t len){
if((data & (1 << i)) != 0) {
sb = &ow_bit1;
}
if(rmt_transmit(owtx, owcenc, sb, ssz, &owtxconf) != ESP_OK){
if(rmt_transmit(owtx, owcenc, sb, sizeof(rmt_symbol_word_t), &owtxconf) != ESP_OK){
return false;
}
}
Expand Down
9 changes: 8 additions & 1 deletion OneWireESP32.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
#include "freertos/queue.h"
#include "driver/rmt_tx.h"
#include "driver/rmt_rx.h"
#include "sdkconfig.h"

#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
#define MAX_BLOCKS 64
#else
#define MAX_BLOCKS 48
#endif

IRAM_ATTR bool owrxdone(rmt_channel_handle_t ch, const rmt_rx_done_event_data_t *edata, void *udata);

Expand All @@ -20,7 +27,7 @@ class OneWire32 {
rmt_channel_handle_t owrx;
rmt_encoder_handle_t owcenc;
rmt_encoder_handle_t owbenc;
rmt_symbol_word_t *owbuf;
rmt_symbol_word_t owbuf[MAX_BLOCKS];
QueueHandle_t owqueue;
uint8_t drv = 0;
public:
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=esp32-ds18b20
version=2.0.0
version=2.0.1
author=junkfix
maintainer=junkfix
sentence=Minimal, non-blocking, DS18B20 sensor library for ESP32 using RMT pheripheral, supports multiple sensors, lightweight, no dependencies, will need Arduino esp32 3.x based on IDF 5.X
Expand Down

0 comments on commit e147627

Please sign in to comment.