Open adrianmak opened 6 years ago
I do not use ESP. I'm depending on the ESP community to resolve problems and submit pull requests for this open source software, to improve its support for ESP.
@PaulStoffregen - if you can describe the process of making it work with a new controller (ESP8266 or ESP32, in particular), I will take a stab at it. Is it ONLY adding a section like this one for the particular controller, or is there more to it?
That should be it. Look at OneWire's headers. Sorry, I can't answer more questions or guide you.
Thanks. One more quickie - if you can. Can I assume that the Arduino IDE (when you select the Board you are using) takes care of defining the controller you're using? IOW, if I properly define the PIN_TO_BASEREG, etc. (above) for a particular board (Wemos D1 mini, for example), and then select the Wemos D1 mini in the Arduino IDE... that OUGHT to do it?
@PaulStoffregen - if you can describe the process of making it work with a new controller (ESP8266 or ESP32, in particular), I will take a stab at it. Is it ONLY adding a section like this one for the particular controller, or is there more to it?
elif defined(SAM3X8E)
define PIN_TO_BASEREG(pin) (&(digitalPinToPort(pin)->PIO_PER))
define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
define IO_REG_TYPE uint32_t
define IO_REG_ASM
define DIRECTREAD(base, mask) (((((base)+15)) & (mask)) ? 1 : 0) #define DIRECT_MODEINPUT(base, mask) ((((base)+5)) = (mask))
define DIRECT_MODEOUTPUT(base, mask) ((((base)+4)) = (mask)) #define DIRECT_WRITELOW(base, mask) ((((base)+13)) = (mask))
define DIRECT_WRITE_HIGH(base, mask) ((*((base)+12)) = (mask))
Did you end up getting this working? I've got capacitive touch working just using touchRead(). But for the particular project I'm currently working on I'd like to be able to have more control over the sensitivity which is possible with CapacitiveSense.
Is it just a matter of creating a section started with:
#elif defined(ESP32)
and then just paste in say the contents from the ESP8266 section that's in the header?
Edit: That seems to resolve the errors when compiling. But I'm assuming those GPIO addresses all still need to be corrected for the ESP32.
@jonmacd - I haven't gotten to it. Not sure I will. I have gone off in a different direction for my project for the time being. I'm hoping that Paul's reply above ("That should be it. Look at OneWire's headers.") is all you need to do, though. I did look at the headers for OneWire, and did find a section for ESP8266, so if I were going to try to get this to work, I think I'd make the #elif defined section look like the ESP8266 section for the OneWire header. Good luck.
@jonmacd - I haven't gotten to it. Not sure I will. I have gone off in a different direction for my project for the time being. I'm hoping that Paul's reply above ("That should be it. Look at OneWire's headers.") is all you need to do, though. I did look at the headers for OneWire, and did find a section for ESP8266, so if I were going to try to get this to work, I think I'd make the #elif defined section look like the ESP8266 section for the OneWire header. Good luck.
Thanks for the reply. Yeah, sorry, I was just using the ESP8266 as an example in my comment because I figured it would be similar (it appears the CapacitiveSensor library already supports the ESP8266). I am trying to use this with an ESP32.
I managed to track down the #elif defined(ARDUINO_ARCH_ESP32)
section in the OneWire header file Paul mentioned. I just pasted that entire section into the CapacitiveSensor.h file with all the other #elif defined
sections. Everything compiles no problem. Using a 10Mohm resistor, at first I couldn't get any readings using a few combinations of touch pins 0-3 on my ESP32 (not sure what's going on there). Moved my resistor to the other side of the board using pins 27 and 12 and I've got nice looking data streaming in.
Thanks!
Glad you got it working! I think you can make a pull request on this GitHub page to add what you did, for future reference. I think Paul will accept it and update it - he just isn't doing any active development.
[jonmacd] I managed to track down the
#elif defined(ARDUINO_ARCH_ESP32)
section in the OneWire header file Paul mentioned. I just pasted that entire section into the CapacitiveSensor.h file with all the other#elif defined
sections. Everything compiles no problem. Using a 10Mohm resistor, at first I couldn't get any readings using a few combinations of touch pins 0-3 on my ESP32 (not sure what's going on there). Moved my resistor to the other side of the board using pins 27 and 12 and I've got nice looking data streaming in.Thanks!
@jonmacd Could you please share your CapacitiveSensor.h file which is ok for ESP32 ? Errors below occur when importing the CapacitiveSensor Library (#include
[Nov 11, 2020]C:\Users\Admin\Documents\Arduino\libraries\CapacitiveSensor/CapacitiveSensor.h:222:2: error: 'IO_REG_TYPE' does not name a type IO_REG_TYPE sBit; // send pin's ports and bitmask ^ C:\Users\Admin\Documents\Arduino\libraries\CapacitiveSensor/CapacitiveSensor.h:223:11: error: 'IO_REG_TYPE' does not name a type volatile IO_REG_TYPE sReg; ^ C:\Users\Admin\Documents\Arduino\libraries\CapacitiveSensor/CapacitiveSensor.h:224:2: error: 'IO_REG_TYPE' does not name a type IO_REG_TYPE rBit; // receive pin's ports and bitmask ^ C:\Users\Admin\Documents\Arduino\libraries\CapacitiveSensor/CapacitiveSensor.h:225:11: error: 'IO_REG_TYPE' does not name a type volatile IO_REG_TYPE rReg; ^ Using the CapacitiveSensor version 0.5.1 library
Same question, how to make this library work on ESP32 ? (I use esp-c3-32s) Guide described here https://github.com/PaulStoffregen/CapacitiveSensor/issues/24#issuecomment-444718037 don't work.
First, you must find someone from the ESP community with knowledge of using the ESP's hardware registers.
The abstraction layer this library uses is nearly the same as OneWire. So perhaps an expert in ESP hardware registers could copy the OneWire defines and get this library to work?
If so, please send a pull request when it is confirmed working. I do not personally use ESP, so I'm depending on the ESP community to submit pull requests. This generic guidance to copy the nearly-identical defines from OneWire is as much as I can do to help.
The suggestion from @jonmacd worked for me.
The modified lines are from 89 to 188. I'll try to get to do a pull request for it. This should be your CapacitiveSensor.h file's code:
`/ CapacitiveSense.h v.04 - Capacitive Sensing Library for 'duino / Wiring https://github.com/PaulStoffregen/CapacitiveSensor http://www.pjrc.com/teensy/td_libs_CapacitiveSensor.html http://playground.arduino.cc/Main/CapacitiveSensor Copyright (c) 2008 Paul Bagder All rights reserved. Version 05 by Paul Stoffregen - Support non-AVR board: Teensy 3.x, Arduino Due Version 04 by Paul Stoffregen - Arduino 1.0 compatibility, issue 146 fix vim: set ts=4: /
// ensure this library description is only included once
// Direct I/O through registers and bitmask (from OneWire library)
static inline attribute((always_inline)) IO_REG_TYPE directRead(IO_REG_TYPE pin) {
return (GPIO.in.val >> pin) & 0x1;
if ( pin < 32 )
return (GPIO.in >> pin) & 0x1;
else if ( pin < 46 )
return (GPIO.in1.val >> (pin - 32)) & 0x1;
return 0;
}
static inline attribute((always_inline)) void directWriteLow(IO_REG_TYPE pin) {
GPIO.out_w1tc.val = ((uint32_t)1 << pin);
if ( pin < 32 )
GPIO.out_w1tc = ((uint32_t)1 << pin);
else if ( pin < 46 )
GPIO.out1_w1tc.val = ((uint32_t)1 << (pin - 32));
}
static inline attribute((always_inline)) void directWriteHigh(IO_REG_TYPE pin) {
GPIO.out_w1ts.val = ((uint32_t)1 << pin);
if ( pin < 32 )
GPIO.out_w1ts = ((uint32_t)1 << pin);
else if ( pin < 46 )
GPIO.out1_w1ts.val = ((uint32_t)1 << (pin - 32));
}
static inline attribute((always_inline)) void directModeInput(IO_REG_TYPE pin) {
GPIO.enable_w1tc.val = ((uint32_t)1 << (pin));
if ( digitalPinIsValid(pin) )
{
uint32_t rtc_reg(rtc_gpio_desc[pin].reg);
if ( rtc_reg ) // RTC pins PULL settings
{
ESP_REG(rtc_reg) = ESP_REG(rtc_reg) & ~(rtc_gpio_desc[pin].mux);
ESP_REG(rtc_reg) = ESP_REG(rtc_reg) & ~(rtc_gpio_desc[pin].pullup | rtc_gpio_desc[pin].pulldown);
}
// Input
if ( pin < 32 )
GPIO.enable_w1tc = ((uint32_t)1 << pin);
else
GPIO.enable1_w1tc.val = ((uint32_t)1 << (pin - 32));
}
}
static inline attribute((always_inline)) void directModeOutput(IO_REG_TYPE pin) {
GPIO.enable_w1ts.val = ((uint32_t)1 << (pin));
if ( digitalPinIsValid(pin) && pin <= 33 ) // pins above 33 can be only inputs
{
uint32_t rtc_reg(rtc_gpio_desc[pin].reg);
if ( rtc_reg ) // RTC pins PULL settings
{
ESP_REG(rtc_reg) = ESP_REG(rtc_reg) & ~(rtc_gpio_desc[pin].mux);
ESP_REG(rtc_reg) = ESP_REG(rtc_reg) & ~(rtc_gpio_desc[pin].pullup | rtc_gpio_desc[pin].pulldown);
}
// Output
if ( pin < 32 )
GPIO.enable_w1ts = ((uint32_t)1 << pin);
else // already validated to pins <= 33
GPIO.enable1_w1ts.val = ((uint32_t)1 << (pin - 32));
}
}
// runs extremely slow/unreliable on Arduino Zero - help wanted....
/ GPIO registers base address /
static inline attribute((always_inline)) IO_REG_TYPE directRead(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) { IO_REG_TYPE ret; if (SS_GPIO == GPIO_TYPE(pin)) { ret = READ_ARC_REG(((IO_REG_TYPE)base + EXT_PORT_OFFSET_SS)); } else { ret = MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, EXT_PORT_OFFSET_SOC); } return ((ret >> GPIO_ID(pin)) & 0x01); }
static inline attribute((always_inline)) void directModeInput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) { if (SS_GPIO == GPIO_TYPE(pin)) { WRITE_ARC_REG(READ_ARC_REG((((IO_REG_TYPE)base) + DIR_OFFSET_SS)) & ~(0x01 << GPIO_ID(pin)), ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); } else { MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) &= ~(0x01 << GPIO_ID(pin)); } }
static inline attribute((always_inline)) void directModeOutput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) { if (SS_GPIO == GPIO_TYPE(pin)) { WRITE_ARC_REG(READ_ARC_REG(((IO_REG_TYPE)(base) + DIR_OFFSET_SS)) | (0x01 << GPIO_ID(pin)), ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); } else { MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) |= (0x01 << GPIO_ID(pin)); } }
static inline attribute((always_inline)) void directWriteLow(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) { if (SS_GPIO == GPIO_TYPE(pin)) { WRITE_ARC_REG(READ_ARC_REG(base) & ~(0x01 << GPIO_ID(pin)), base); } else { MMIO_REG_VAL(base) &= ~(0x01 << GPIO_ID(pin)); } }
static inline attribute((always_inline)) void directWriteHigh(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) { if (SS_GPIO == GPIO_TYPE(pin)) { WRITE_ARC_REG(READ_ARC_REG(base) | (0x01 << GPIO_ID(pin)), base); } else { MMIO_REG_VAL(base) |= (0x01 << GPIO_ID(pin)); } }
// some 3.3V chips with 5V tolerant pins need this workaround //
// library interface description class CapacitiveSensor { // user-accessible "public" interface public: // methods CapacitiveSensor(uint8_t sendPin, uint8_t receivePin); long capacitiveSensorRaw(uint8_t samples); long capacitiveSensor(uint8_t samples); void set_CS_Timeout_Millis(unsigned long timeout_millis); void reset_CS_AutoCal(); void set_CS_AutocaL_Millis(unsigned long autoCal_millis); // library-accessible "private" interface private: // variables int error; unsigned long leastTotal; unsigned int loopTimingFactor; unsigned long CS_Timeout_Millis; unsigned long CS_AutocaL_Millis; unsigned long lastCal; unsigned long total; IO_REG_TYPE sBit; // send pin's ports and bitmask volatile IO_REG_TYPE sReg; IO_REG_TYPE rBit; // receive pin's ports and bitmask volatile IO_REG_TYPE rReg; // methods int SenseOneCycle(void); };
`
Actually the code is already updated to work properly with esp32 boards in the master branch of this repo but in the released version you can get from within the Arduino IDE there is still no support for esp32
@arjuna-dev Are you sure about that? When I try using the latest master to compile for ESP32 boards, I get 'rtc_gpio_desc' was not declared in this scope
and 'esp32_gpioMux' was not declared in this scope
and it fails to build.
Update: It will compile for ESP32... on pre-v2 versions of the ESP board manager. It doesn't work on recent versions. I think we need to do something like this, but I'm not sure I understand it intimately enough to do it myself.
no updates to this I don't suppose (re fixing the 'rtc_gpio_desc' was not declared in this scope) issue
I owned this board https://www.banggood.com/ESP32-Development-Board-WiFiBluetooth-Ultra-Low-Power-Consumption-Dual-Cores-ESP-32-ESP-32S-Board-p-1109512.html
I tried to copy from demo code for testing but firmware ha built error.
In file included from src\main.cpp:3:0: lib\CapacitiveSensor/CapacitiveSensor.h:239:2: error: 'IO_REG_TYPE' does not name a type IO_REG_TYPE sBit; // send pin's ports and bitmask ^ lib\CapacitiveSensor/CapacitiveSensor.h:240:11: error: 'IO_REG_TYPE' does not name a type volatile IO_REG_TYPE sReg; ^ lib\CapacitiveSensor/CapacitiveSensor.h:241:2: error: 'IO_REG_TYPE' does not name a type IO_REG_TYPE rBit; // receive pin's ports and bitmask ^ lib\CapacitiveSensor/CapacitiveSensor.h:242:11: error: 'IO_REG_TYPE' does not name a type volatile IO_REG_TYPE rReg;