Skip to content

Commit

Permalink
move to new esp rmt device driver: resolves DavidAntliff#28
Browse files Browse the repository at this point in the history
  • Loading branch information
mjcross committed Apr 6, 2023
1 parent cdd344f commit 0e1821c
Show file tree
Hide file tree
Showing 5 changed files with 682 additions and 409 deletions.
14 changes: 4 additions & 10 deletions include/owb.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,23 +125,17 @@ typedef enum
/** NOTE: Driver assumes that (*init) was called prior to any other methods */
struct owb_driver
{
/** Driver identification **/
const char* name;

/** Pointer to driver uninitialization function **/
owb_status (*uninitialize)(const OneWireBus * bus);

/** Pointer to driver reset functio **/
owb_status (*reset)(const OneWireBus * bus, bool *is_present);

/** NOTE: The data is shifted out of the low bits, eg. it is written in the order of lsb to msb */
owb_status (*reset)(const OneWireBus *bus, bool *is_present);
owb_status (*write_bits)(const OneWireBus *bus, uint8_t out, int number_of_bits_to_write);

/** NOTE: Data is read into the high bits, eg. each bit read is shifted down before the next bit is read */
owb_status (*write_bytes)(const OneWireBus *bus, uint8_t *bytes, int number_of_bytes_to_write); // new addition to the API
owb_status (*read_bits)(const OneWireBus *bus, uint8_t *in, int number_of_bits_to_read);
owb_status (*read_bytes)(const OneWireBus *bus, uint64_t *in, int number_of_bytes_to_read); // new addition to the API
};

/// @cond ignore
// note: the new owb_rmt driver uses the ESP-IDF's built-in `__containerof()` macro instead of this
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
Expand Down
47 changes: 38 additions & 9 deletions include/owb_rmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@

#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/ringbuf.h"
#include "driver/rmt.h"

#include "driver/rmt_tx.h"
#include "driver/rmt_rx.h"

#include "owb.h" // for tyoe OneWireBus

#ifdef __cplusplus
extern "C" {
Expand All @@ -49,25 +52,51 @@ extern "C" {
*/
typedef struct
{
int tx_channel; ///< RMT channel to use for TX
int rx_channel; ///< RMT channel to use for RX
RingbufHandle_t rb; ///< Ring buffer handle
int gpio; ///< OneWireBus GPIO
OneWireBus bus; ///< OneWireBus instance
rmt_channel_handle_t tx_channel_handle; ///< RMT transmit channel, allocated by RMT driver
rmt_channel_handle_t rx_channel_handle; ///< RMT receive channel, allocated by RMT driver
rmt_encoder_handle_t copy_encoder_handle; ///< RMT built-in data encoder that sends pre-defined symbols
rmt_encoder_handle_t bytes_encoder_handle; ///< RMT built-in data encoder that converts bits into symbols
rmt_symbol_word_t *rx_buffer; ///< RMT receive channel symbol buffer
size_t rx_buffer_size_in_bytes; ///< Size of the RMT received buffer, in bytes
QueueHandle_t rx_queue; ///< xQueue to receive RMT symbols from the `on_recv_done` callback
int gpio; ///< OneWireBus GPIO
OneWireBus bus; ///< OneWireBus instance (see owb.h)
} owb_rmt_driver_info;

/**
* @brief Initialise the RMT driver.
* @param[in] info Pointer to an uninitialized owb_rmt_driver_info structure.
* Note: the structure must remain in scope for the lifetime of this component.
* @param[in] gpio_num The GPIO number to use as the One Wire bus data line.
* @param[in] tx_channel The RMT channel to use for transmitting data to bus devices.
* @param[in] rx_channel the RMT channel to use for receiving data from bus devices.
* @param tx_channel NOW IGNORED: the new RMT driver allocates channels dynamically.
* @param rx_channel NOW IGNORED: the new RMT driver allocates channels dynamically.
* @return OneWireBus *, pass this into the other OneWireBus public API functions
*/
OneWireBus* owb_rmt_initialize(owb_rmt_driver_info * info, gpio_num_t gpio_num,
rmt_channel_t tx_channel, rmt_channel_t rx_channel);


/**
* @brief Legacy RMT channel IDs.
*
* These are no longer used for anything. They are defined here purely so
* that code written for the old rmt driver can still compile.
*/
typedef enum {
RMT_CHANNEL_0, /*!< RMT channel number 0 */
RMT_CHANNEL_1, /*!< RMT channel number 1 */
RMT_CHANNEL_2, /*!< RMT channel number 2 */
RMT_CHANNEL_3, /*!< RMT channel number 3 */
#if SOC_RMT_CHANNELS_PER_GROUP > 4
RMT_CHANNEL_4, /*!< RMT channel number 4 */
RMT_CHANNEL_5, /*!< RMT channel number 5 */
RMT_CHANNEL_6, /*!< RMT channel number 6 */
RMT_CHANNEL_7, /*!< RMT channel number 7 */
#endif
RMT_CHANNEL_MAX /*!< Number of RMT channels */
} rmt_channel_t;


#ifdef __cplusplus
}
#endif
Expand Down
41 changes: 41 additions & 0 deletions include/owb_rmt_bus_symbols.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright (c) 2023 mjcross
*
* SPDX-License-Identifier: MIT
**/

#include "owb_rmt_bus_timings.h"

// RMT transmit channel symbols for the onewire bus signals and conditions
//

// basic bus levels
// ----------------
// note: we configure the transmit channel to be hardware inverted,
// so that the bus initialises in the 'released' state
#define OWB_RMT_BUS_ASSERTED 1
#define OWB_RMT_BUS_RELEASED 0

// bus symbols as `rmt_symbol_word_t`
// ----------------------------------

// send 'zero' bit
#define OWB_RMT_SYMBOL_0BIT { \
.level0 = OWB_RMT_BUS_ASSERTED, \
.duration0 = OWB_TIMING_PARAM_C, \
.level1 = OWB_RMT_BUS_RELEASED, \
.duration1 = OWB_TIMING_PARAM_D }

// send 'one' bit
#define OWB_RMT_SYMBOL_1BIT { \
.level0 = OWB_RMT_BUS_ASSERTED, \
.duration0 = OWB_TIMING_PARAM_A, \
.level1 = OWB_RMT_BUS_RELEASED, \
.duration1 = OWB_TIMING_PARAM_B }

// send bus reset
#define OWB_RMT_SYMBOL_RESET { \
.level0 = OWB_RMT_BUS_ASSERTED, \
.duration0 = OWB_TIMING_PARAM_H, \
.level1 = OWB_RMT_BUS_RELEASED, \
.duration1 = OWB_TIMING_PARAM_I + OWB_TIMING_PARAM_J }
19 changes: 19 additions & 0 deletions include/owb_rmt_bus_timings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// onewire bus master timings

// standard rate master timings recommended in the 1-Wire specification
// the values below are the ones recommended by the specification.

// for the meaning of each parameter, see the following document:
// https://www.analog.com/en/technical-articles/1wire-communication-through-software.html

// all values in microsec
#define OWB_TIMING_PARAM_A 6
#define OWB_TIMING_PARAM_B 64
#define OWB_TIMING_PARAM_C 60
#define OWB_TIMING_PARAM_D 10
#define OWB_TIMING_PARAM_E 9
#define OWB_TIMING_PARAM_F 55
#define OWB_TIMING_PARAM_G 0
#define OWB_TIMING_PARAM_H 480
#define OWB_TIMING_PARAM_I 70
#define OWB_TIMING_PARAM_J 410
Loading

0 comments on commit 0e1821c

Please sign in to comment.