This commit is contained in:
@@ -32,6 +32,11 @@ private email about OneWire).
|
||||
OneWire is now very mature code. No changes other than adding
|
||||
definitions for newer hardware support are anticipated.
|
||||
|
||||
ESP32 mods authored by stickbreaker:
|
||||
@stickbreaker 30APR2018 add IRAM_ATTR to read_bit() write_bit() to solve ICache miss timing failure.
|
||||
thanks @everslick re: https://github.com/espressif/arduino-esp32/issues/1335
|
||||
Altered by garyd9 for clean merge with Paul Stoffregen's source
|
||||
|
||||
Version 2.3:
|
||||
Unknown chip fallback mode, Roger Clark
|
||||
Teensy-LC compatibility, Paul Stoffregen
|
||||
@@ -143,6 +148,16 @@ sample code bearing this copyright.
|
||||
#include "OneWire.h"
|
||||
#include "util/OneWire_direct_gpio.h"
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
// due to the dual core esp32, a critical section works better than disabling interrupts
|
||||
# define noInterrupts() {portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;portENTER_CRITICAL(&mux)
|
||||
# define interrupts() portEXIT_CRITICAL(&mux);}
|
||||
// for info on this, search "IRAM_ATTR" at https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/general-notes.html
|
||||
# define CRIT_TIMING IRAM_ATTR
|
||||
#else
|
||||
# define CRIT_TIMING
|
||||
#endif
|
||||
|
||||
|
||||
void OneWire::begin(uint8_t pin)
|
||||
{
|
||||
@@ -161,10 +176,10 @@ void OneWire::begin(uint8_t pin)
|
||||
//
|
||||
// Returns 1 if a device asserted a presence pulse, 0 otherwise.
|
||||
//
|
||||
uint8_t OneWire::reset(void)
|
||||
uint8_t CRIT_TIMING OneWire::reset(void)
|
||||
{
|
||||
IO_REG_TYPE mask IO_REG_MASK_ATTR = bitmask;
|
||||
volatile IO_REG_TYPE *reg IO_REG_BASE_ATTR = baseReg;
|
||||
__attribute__((unused)) volatile IO_REG_TYPE *reg IO_REG_BASE_ATTR = baseReg;
|
||||
uint8_t r;
|
||||
uint8_t retries = 125;
|
||||
|
||||
@@ -195,10 +210,10 @@ uint8_t OneWire::reset(void)
|
||||
// Write a bit. Port and bit is used to cut lookup time and provide
|
||||
// more certain timing.
|
||||
//
|
||||
void OneWire::write_bit(uint8_t v)
|
||||
void CRIT_TIMING OneWire::write_bit(uint8_t v)
|
||||
{
|
||||
IO_REG_TYPE mask IO_REG_MASK_ATTR = bitmask;
|
||||
volatile IO_REG_TYPE *reg IO_REG_BASE_ATTR = baseReg;
|
||||
__attribute__((unused)) volatile IO_REG_TYPE *reg IO_REG_BASE_ATTR = baseReg;
|
||||
|
||||
if (v & 1) {
|
||||
noInterrupts();
|
||||
@@ -223,10 +238,10 @@ void OneWire::write_bit(uint8_t v)
|
||||
// Read a bit. Port and bit is used to cut lookup time and provide
|
||||
// more certain timing.
|
||||
//
|
||||
uint8_t OneWire::read_bit(void)
|
||||
uint8_t CRIT_TIMING OneWire::read_bit(void)
|
||||
{
|
||||
IO_REG_TYPE mask IO_REG_MASK_ATTR = bitmask;
|
||||
volatile IO_REG_TYPE *reg IO_REG_BASE_ATTR = baseReg;
|
||||
__attribute__((unused)) volatile IO_REG_TYPE *reg IO_REG_BASE_ATTR = baseReg;
|
||||
uint8_t r;
|
||||
|
||||
noInterrupts();
|
||||
@@ -578,3 +593,11 @@ uint16_t OneWire::crc16(const uint8_t* input, uint16_t len, uint16_t crc)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
// undef defines for no particular reason
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
# undef noInterrupts() {portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;portENTER_CRITICAL(&mux)
|
||||
# undef interrupts() portEXIT_CRITICAL(&mux);}
|
||||
#endif
|
||||
// for info on this, search "IRAM_ATTR" at https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/general-notes.html
|
||||
#undef CRIT_TIMING
|
||||
|
||||
@@ -52,7 +52,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/PaulStoffregen/OneWire"
|
||||
},
|
||||
"version": "2.3.7",
|
||||
"version": "2.3.8",
|
||||
"homepage": "https://www.pjrc.com/teensy/td_libs_OneWire.html",
|
||||
"frameworks": "Arduino",
|
||||
"examples": [
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
name=OneWire
|
||||
version=2.3.7
|
||||
version=2.3.8
|
||||
author=Jim Studt, Tom Pollard, Robin James, Glenn Trewitt, Jason Dangel, Guillermo Lovato, Paul Stoffregen, Scott Roberts, Bertrik Sikken, Mark Tillotson, Ken Butcher, Roger Clark, Love Nystrom
|
||||
maintainer=Paul Stoffregen
|
||||
sentence=Access 1-wire temperature sensors, memory and other chips.
|
||||
|
||||
@@ -106,18 +106,58 @@
|
||||
// DO NOT CREATE GITHUB ISSUES for ESP support. All ESP questions must be asked
|
||||
// on ESP community forums.
|
||||
#define PIN_TO_BASEREG(pin) ((volatile uint32_t*) GPO)
|
||||
#define PIN_TO_BITMASK(pin) (1 << pin)
|
||||
#define PIN_TO_BITMASK(pin) (1UL << (pin))
|
||||
#define IO_REG_TYPE uint32_t
|
||||
#define IO_REG_BASE_ATTR
|
||||
#define IO_REG_MASK_ATTR
|
||||
#define DIRECT_READ(base, mask) ((GPI & (mask)) ? 1 : 0) //GPIO_IN_ADDRESS
|
||||
#define DIRECT_MODE_INPUT(base, mask) (GPE &= ~(mask)) //GPIO_ENABLE_W1TC_ADDRESS
|
||||
#define DIRECT_MODE_OUTPUT(base, mask) (GPE |= (mask)) //GPIO_ENABLE_W1TS_ADDRESS
|
||||
#define DIRECT_WRITE_LOW(base, mask) (GPOC = (mask)) //GPIO_OUT_W1TC_ADDRESS
|
||||
#define DIRECT_WRITE_HIGH(base, mask) (GPOS = (mask)) //GPIO_OUT_W1TS_ADDRESS
|
||||
|
||||
static inline __attribute__((always_inline))
|
||||
void directModeInput(IO_REG_TYPE mask)
|
||||
{
|
||||
if(mask > 0x8000)
|
||||
{
|
||||
GP16FFS(GPFFS_GPIO(16));
|
||||
GPC16 = 0;
|
||||
GP16E &= ~1;
|
||||
}
|
||||
else
|
||||
{
|
||||
GPE &= ~(mask);
|
||||
}
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline))
|
||||
void directModeOutput(IO_REG_TYPE mask)
|
||||
{
|
||||
if(mask > 0x8000)
|
||||
{
|
||||
GP16FFS(GPFFS_GPIO(16));
|
||||
GPC16 = 0;
|
||||
GP16E |= 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
GPE |= (mask);
|
||||
}
|
||||
}
|
||||
static inline __attribute__((always_inline))
|
||||
bool directRead(IO_REG_TYPE mask)
|
||||
{
|
||||
if(mask > 0x8000)
|
||||
return GP16I & 0x01;
|
||||
else
|
||||
return ((GPI & (mask)) ? true : false);
|
||||
}
|
||||
|
||||
#define DIRECT_READ(base, mask) directRead(mask)
|
||||
#define DIRECT_MODE_INPUT(base, mask) directModeInput(mask)
|
||||
#define DIRECT_MODE_OUTPUT(base, mask) directModeOutput(mask)
|
||||
#define DIRECT_WRITE_LOW(base, mask) (mask > 0x8000) ? GP16O &= ~1 : (GPOC = (mask))
|
||||
#define DIRECT_WRITE_HIGH(base, mask) (mask > 0x8000) ? GP16O |= 1 : (GPOS = (mask))
|
||||
|
||||
#elif defined(ARDUINO_ARCH_ESP32)
|
||||
#include <driver/rtc_io.h>
|
||||
#include <soc/gpio_struct.h>
|
||||
#define PIN_TO_BASEREG(pin) (0)
|
||||
#define PIN_TO_BITMASK(pin) (pin)
|
||||
#define IO_REG_TYPE uint32_t
|
||||
@@ -427,6 +467,40 @@ void directWriteHigh(IO_REG_TYPE mask)
|
||||
#define DIRECT_MODE_INPUT(base, mask) directModeInput(mask)
|
||||
#define DIRECT_MODE_OUTPUT(base, mask) directModeOutput(mask)
|
||||
|
||||
#elif defined(__MBED__)
|
||||
|
||||
#include "platform/mbed_critical.h"
|
||||
#include "DigitalInOut.h"
|
||||
#include <cmsis_os2.h>
|
||||
#define PIN_TO_BASEREG(pin) (0)
|
||||
#define PIN_TO_BITMASK(pin) (new mbed::DigitalInOut(digitalPinToPinName(pin)))
|
||||
#define IO_REG_TYPE mbed::DigitalInOut*
|
||||
#define IO_REG_BASE_ATTR
|
||||
#define IO_REG_MASK_ATTR
|
||||
#define DIRECT_READ(base, pin) (*pin)
|
||||
#define DIRECT_WRITE_LOW(base, pin) (*pin = 0)
|
||||
#define DIRECT_WRITE_HIGH(base, pin) (*pin = 1)
|
||||
#define DIRECT_MODE_INPUT(base, pin) (pin->input())
|
||||
#define DIRECT_MODE_OUTPUT(base, pin) (pin->output())
|
||||
#undef interrupts
|
||||
#undef noInterrupts
|
||||
#define noInterrupts() osThreadSetPriority(osThreadGetId(), osPriorityRealtime) //core_util_critical_section_enter()
|
||||
#define interrupts() osThreadSetPriority(osThreadGetId(), osPriorityNormal) //core_util_critical_section_exit()
|
||||
|
||||
#elif defined(ARDUINO_ARCH_MBED_RP2040)|| defined(ARDUINO_ARCH_RP2040)
|
||||
#define delayMicroseconds(time) busy_wait_us(time)
|
||||
#define PIN_TO_BASEREG(pin) (0)
|
||||
#define PIN_TO_BITMASK(pin) (pin)
|
||||
#define IO_REG_TYPE unsigned int
|
||||
#define IO_REG_BASE_ATTR
|
||||
#define IO_REG_MASK_ATTR
|
||||
#define DIRECT_READ(base, pin) digitalRead(pin)
|
||||
#define DIRECT_WRITE_LOW(base, pin) digitalWrite(pin, LOW)
|
||||
#define DIRECT_WRITE_HIGH(base, pin) digitalWrite(pin, HIGH)
|
||||
#define DIRECT_MODE_INPUT(base, pin) pinMode(pin,INPUT)
|
||||
#define DIRECT_MODE_OUTPUT(base, pin) pinMode(pin,OUTPUT)
|
||||
#warning "OneWire. RP2040 in Fallback mode. Using API calls for pinMode,digitalRead and digitalWrite."
|
||||
|
||||
#else
|
||||
#define PIN_TO_BASEREG(pin) (0)
|
||||
#define PIN_TO_BITMASK(pin) (pin)
|
||||
|
||||
@@ -45,6 +45,10 @@
|
||||
#elif defined(__arc__) /* Arduino101/Genuino101 specifics */
|
||||
#define IO_REG_TYPE uint32_t
|
||||
|
||||
#elif defined(__MBED__)
|
||||
#include "DigitalInOut.h"
|
||||
#define IO_REG_TYPE mbed::DigitalInOut*
|
||||
|
||||
#elif defined(__riscv)
|
||||
#define IO_REG_TYPE uint32_t
|
||||
|
||||
|
||||
Reference in New Issue
Block a user