36 #include "freertos/FreeRTOS.h" 
   37 #include "freertos/task.h" 
   39 #include "sdkconfig.h" 
   40 #include "driver/gpio.h" 
   45 static const char *
TAG = CONFIG_OWB_GPIO_TAG;
 
   57 #define PHY_DEBUG_GPIO GPIO_NUM_27 
   58 #define PHY_DEBUG_GPIO_MASK GPIO_SEL_27 
   62 struct _OneWireBus_Timing
 
   64     uint32_t A, B, C, D, E, F, G, H, I, J;
 
   70 static const struct _OneWireBus_Timing _StandardTiming = {
 
   83 static void _us_delay(uint32_t time_us)
 
   85     ets_delay_us(time_us);
 
   89 #define info_from_bus(owb) container_of(owb, owb_gpio_driver_info, bus) 
  100     bool present = 
false;
 
  101     portMUX_TYPE timeCriticalMutex = portMUX_INITIALIZER_UNLOCKED;
 
  102     portENTER_CRITICAL(&timeCriticalMutex);
 
  106     gpio_set_direction(i->
gpio, GPIO_MODE_OUTPUT);
 
  107     _us_delay(bus->
timing->G);
 
  108     gpio_set_level(i->
gpio, 0); 
 
  109     _us_delay(bus->
timing->H);
 
  110     gpio_set_direction(i->
gpio, GPIO_MODE_INPUT); 
 
  111     gpio_set_level(i->
gpio, 1);                   
 
  112     _us_delay(bus->
timing->I);
 
  115     gpio_set_level(PHY_DEBUG_GPIO, 1);
 
  118     int level1 = gpio_get_level(i->
gpio);
 
  121     gpio_set_level(PHY_DEBUG_GPIO, 0);
 
  124     _us_delay(bus->
timing->J); 
 
  127     gpio_set_level(PHY_DEBUG_GPIO, 1);
 
  130     int level2 = gpio_get_level(i->
gpio);
 
  133     gpio_set_level(PHY_DEBUG_GPIO, 0);
 
  136     portEXIT_CRITICAL(&timeCriticalMutex);
 
  138     present = (level1 == 0) && (level2 == 1); 
 
  139     ESP_LOGD(
TAG, 
"reset: level1 0x%x, level2 0x%x, present %d", level1, level2, present);
 
  141     *is_present = present;
 
  151 static void _write_bit(
const OneWireBus *bus, 
int bit)
 
  157     portMUX_TYPE timeCriticalMutex = portMUX_INITIALIZER_UNLOCKED;
 
  158     portENTER_CRITICAL(&timeCriticalMutex);
 
  160     gpio_set_direction(i->
gpio, GPIO_MODE_OUTPUT);
 
  161     gpio_set_level(i->
gpio, 0); 
 
  163     gpio_set_level(i->
gpio, 1); 
 
  166     portEXIT_CRITICAL(&timeCriticalMutex);
 
  178     portMUX_TYPE timeCriticalMutex = portMUX_INITIALIZER_UNLOCKED;
 
  179     portENTER_CRITICAL(&timeCriticalMutex);
 
  181     gpio_set_direction(i->
gpio, GPIO_MODE_OUTPUT);
 
  182     gpio_set_level(i->
gpio, 0); 
 
  183     _us_delay(bus->
timing->A);
 
  184     gpio_set_direction(i->
gpio, GPIO_MODE_INPUT); 
 
  185     gpio_set_level(i->
gpio, 1);                   
 
  186     _us_delay(bus->
timing->E);
 
  189     gpio_set_level(PHY_DEBUG_GPIO, 1);
 
  192     int level = gpio_get_level(i->
gpio);
 
  195     gpio_set_level(PHY_DEBUG_GPIO, 0);
 
  198     _us_delay(bus->
timing->F); 
 
  200     portEXIT_CRITICAL(&timeCriticalMutex);
 
  202     result = level & 0x01;
 
  216     ESP_LOGD(
TAG, 
"write 0x%02x", data);
 
  217     for (
int i = 0; i < number_of_bits_to_write; ++i)
 
  219         _write_bit(bus, data & 0x01);
 
  235     for (
int i = 0; i < number_of_bits_to_read; ++i)
 
  243     ESP_LOGD(
TAG, 
"read 0x%02x", result);
 
  255 static const struct owb_driver gpio_function_table =
 
  258         .uninitialize = _uninitialize,
 
  260         .write_bits = _write_bits,
 
  261         .read_bits = _read_bits};
 
  265     ESP_LOGD(
TAG, 
"%s(): gpio %d", __func__, gpio);
 
  267     driver_info->
gpio = gpio;
 
  268     driver_info->
bus.
driver = &gpio_function_table;
 
  269     driver_info->
bus.
timing = &_StandardTiming;
 
  273     gpio_pad_select_gpio(driver_info->
gpio);
 
  276     gpio_config_t io_conf;
 
  277     io_conf.intr_type = GPIO_INTR_DISABLE;
 
  278     io_conf.mode = GPIO_MODE_OUTPUT;
 
  279     io_conf.pin_bit_mask = PHY_DEBUG_GPIO_MASK;
 
  280     io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE;
 
  281     io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
 
  282     ESP_ERROR_CHECK(gpio_config(&io_conf));
 
  285     return &(driver_info->
bus);
 
Interface definitions for the 1-Wire bus component.
owb_status
Represents the result of OWB API functions.
@ OWB_STATUS_OK
Operation succeeded.
#define GPIO_NUM_NC
ESP-IDF prior to v4.x does not define GPIO_NUM_NC.
OneWireBus * owb_gpio_initialize(owb_gpio_driver_info *driver_info, int gpio)
Initialise the GPIO driver.
Interface definitions for the ESP32 GPIO driver used to communicate with devices on the One Wire Bus.
Structure containing 1-Wire bus information relevant to a single instance.
const struct _OneWireBus_Timing * timing
Pointer to timing information.
gpio_num_t strong_pullup_gpio
Set if an external strong pull-up circuit is required.
const struct owb_driver * driver
Pointer to hardware driver instance.
OneWireBus bus
OneWireBus instance.
int gpio
Value of the GPIO connected to the 1-Wire bus.