mirror of
https://github.com/letscontrolit/ESPEasy.git
synced 2026-09-12 01:24:04 +00:00
[Direct GPIO] Reduce iRAM usage
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
#include "GPIO_Direct_Access.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
# ifndef CORE_POST_3_0_0
|
||||
# define IRAM_ATTR ICACHE_RAM_ATTR
|
||||
# endif // ifndef CORE_POST_3_0_0
|
||||
#endif // if defined(ARDUINO_ARCH_ESP8266)
|
||||
|
||||
|
||||
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
||||
|
||||
IO_REG_TYPE DIRECT_pinRead(IO_REG_TYPE pin)
|
||||
{
|
||||
return DIRECT_READ(reg, PIN_TO_BITMASK(pin));
|
||||
}
|
||||
|
||||
void DIRECT_pinWrite(IO_REG_TYPE pin, bool pinstate)
|
||||
{
|
||||
if (pinstate) { DIRECT_WRITE_HIGH(reg, PIN_TO_BITMASK(pin)); }
|
||||
else { DIRECT_WRITE_LOW(reg, PIN_TO_BITMASK(pin)); }
|
||||
}
|
||||
|
||||
void DIRECT_PINMODE_OUTPUT(IO_REG_TYPE pin)
|
||||
{
|
||||
DIRECT_MODE_OUTPUT(reg, PIN_TO_BITMASK(pin));
|
||||
}
|
||||
|
||||
void DIRECT_PINMODE_INPUT(IO_REG_TYPE pin)
|
||||
{
|
||||
DIRECT_MODE_INPUT(reg, PIN_TO_BITMASK(pin));
|
||||
}
|
||||
|
||||
IO_REG_TYPE IRAM_ATTR DIRECT_pinRead_ISR(IO_REG_TYPE pin)
|
||||
{
|
||||
return DIRECT_READ(reg, PIN_TO_BITMASK(pin));
|
||||
}
|
||||
|
||||
void IRAM_ATTR DIRECT_pinWrite_ISR(IO_REG_TYPE pin, bool pinstate)
|
||||
{
|
||||
if (pinstate) { DIRECT_WRITE_HIGH(reg, PIN_TO_BITMASK(pin)); }
|
||||
else { DIRECT_WRITE_LOW(reg, PIN_TO_BITMASK(pin)); }
|
||||
}
|
||||
|
||||
void IRAM_ATTR DIRECT_PINMODE_OUTPUT_ISR(IO_REG_TYPE pin)
|
||||
{
|
||||
DIRECT_MODE_OUTPUT(reg, PIN_TO_BITMASK(pin));
|
||||
}
|
||||
|
||||
void IRAM_ATTR DIRECT_PINMODE_INPUT_ISR(IO_REG_TYPE pin)
|
||||
{
|
||||
DIRECT_MODE_INPUT(reg, PIN_TO_BITMASK(pin));
|
||||
}
|
||||
|
||||
#endif // if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
||||
@@ -119,7 +119,19 @@
|
||||
#define DIRECT_WRITE_LOW(base, mask) (GPOC = (mask)) //GPIO_OUT_W1TC_ADDRESS
|
||||
#define DIRECT_WRITE_HIGH(base, mask) (GPOS = (mask)) //GPIO_OUT_W1TS_ADDRESS
|
||||
|
||||
IO_REG_TYPE DIRECT_pinRead(IO_REG_TYPE pin);
|
||||
void DIRECT_pinWrite(IO_REG_TYPE pin, bool pinstate);
|
||||
void DIRECT_PINMODE_OUTPUT(IO_REG_TYPE pin);
|
||||
void DIRECT_PINMODE_INPUT(IO_REG_TYPE pin);
|
||||
|
||||
IO_REG_TYPE DIRECT_pinRead_ISR(IO_REG_TYPE pin);
|
||||
void DIRECT_pinWrite_ISR(IO_REG_TYPE pin, bool pinstate);
|
||||
void DIRECT_PINMODE_OUTPUT_ISR(IO_REG_TYPE pin);
|
||||
void DIRECT_PINMODE_INPUT_ISR(IO_REG_TYPE pin);
|
||||
|
||||
#elif defined(ARDUINO_ARCH_ESP32)
|
||||
|
||||
#include <esp32-hal-gpio.h>
|
||||
#include <driver/rtc_io.h>
|
||||
#define PIN_TO_BASEREG(pin) (0)
|
||||
#define PIN_TO_BITMASK(pin) (pin)
|
||||
@@ -225,6 +237,18 @@ void directModeOutput(IO_REG_TYPE pin)
|
||||
#define DIRECT_WRITE_HIGH(base, pin) directWriteHigh(pin)
|
||||
#define DIRECT_MODE_INPUT(base, pin) directModeInput(pin)
|
||||
#define DIRECT_MODE_OUTPUT(base, pin) directModeOutput(pin)
|
||||
|
||||
|
||||
IO_REG_TYPE DIRECT_pinRead(IO_REG_TYPE pin);
|
||||
void DIRECT_pinWrite(IO_REG_TYPE pin, bool pinstate);
|
||||
void DIRECT_PINMODE_OUTPUT(IO_REG_TYPE pin);
|
||||
void DIRECT_PINMODE_INPUT(IO_REG_TYPE pin);
|
||||
|
||||
IO_REG_TYPE DIRECT_pinRead_ISR(IO_REG_TYPE pin) IRAM_ATTR;
|
||||
void DIRECT_pinWrite_ISR(IO_REG_TYPE pin, bool pinstate) IRAM_ATTR;
|
||||
void DIRECT_PINMODE_OUTPUT_ISR(IO_REG_TYPE pin) IRAM_ATTR;
|
||||
void DIRECT_PINMODE_INPUT_ISR(IO_REG_TYPE pin) IRAM_ATTR;
|
||||
|
||||
/*
|
||||
// https://github.com/PaulStoffregen/OneWire/pull/47
|
||||
// https://github.com/stickbreaker/OneWire/commit/6eb7fc1c11a15b6ac8c60e5671cf36eb6829f82c
|
||||
|
||||
+11
-17
@@ -60,14 +60,11 @@ unsigned int NewPing::ping(unsigned int max_cm_distance) {
|
||||
|
||||
if (!ping_trigger()) return NO_ECHO; // Trigger a ping, if it returns false, return NO_ECHO to the calling function.
|
||||
|
||||
IO_REG_TYPE mask_echo IO_REG_MASK_ATTR = PIN_TO_BITMASK(_echoPin);
|
||||
|
||||
|
||||
#if URM37_ENABLED == true
|
||||
#if DO_BITWISE == true
|
||||
while (!(*_echoInput & _echoBit)) // Wait for the ping echo.
|
||||
#else
|
||||
while (!DIRECT_READ(reg_echo, mask_echo)) // Wait for the ping echo.
|
||||
while (!DIRECT_pinRead(_echoPin)) // Wait for the ping echo.
|
||||
#endif
|
||||
if (micros() > _max_time) {
|
||||
_errorState = STATUS_MAX_DISTANCE_EXCEEDED;
|
||||
@@ -77,7 +74,7 @@ unsigned int NewPing::ping(unsigned int max_cm_distance) {
|
||||
#if DO_BITWISE == true
|
||||
while (*_echoInput & _echoBit) // Wait for the ping echo.
|
||||
#else
|
||||
while (DIRECT_READ(reg_echo, mask_echo)) // Wait for the ping echo.
|
||||
while (DIRECT_pinRead(_echoPin)) // Wait for the ping echo.
|
||||
#endif
|
||||
if (micros() > _max_time) {
|
||||
_errorState = STATUS_MAX_DISTANCE_EXCEEDED;
|
||||
@@ -181,42 +178,39 @@ boolean NewPing::ping_trigger() {
|
||||
}
|
||||
#endif
|
||||
#else
|
||||
IO_REG_TYPE mask_trigger IO_REG_MASK_ATTR = PIN_TO_BITMASK(_triggerPin);
|
||||
IO_REG_TYPE mask_echo IO_REG_MASK_ATTR = PIN_TO_BITMASK(_echoPin);
|
||||
|
||||
|
||||
#if ONE_PIN_ENABLED == true
|
||||
DIRECT_MODE_OUTPUT(reg_trigger, mask_trigger); // Set trigger pin to output.
|
||||
DIRECT_PINMODE_OUTPUT(_triggerPin); // Set trigger pin to output.
|
||||
#endif
|
||||
|
||||
DIRECT_WRITE_LOW(reg_trigger, mask_trigger); // Set the trigger pin low, should already be low, but this will make sure it is.
|
||||
DIRECT_pinWrite(_triggerPin, 0); // Set the trigger pin low, should already be low, but this will make sure it is.
|
||||
delayMicroseconds(4); // Wait for pin to go low.
|
||||
DIRECT_WRITE_HIGH(reg_trigger, mask_trigger); // Set trigger pin high, this tells the sensor to send out a ping.
|
||||
DIRECT_pinWrite(_triggerPin, 1); // Set trigger pin high, this tells the sensor to send out a ping.
|
||||
delayMicroseconds(10); // Wait long enough for the sensor to realize the trigger pin is high. Sensor specs say to wait 10uS.
|
||||
DIRECT_WRITE_LOW(reg_trigger, mask_trigger); // Set trigger pin back to low.
|
||||
DIRECT_pinWrite(_triggerPin, 0); // Set trigger pin back to low.
|
||||
|
||||
#if ONE_PIN_ENABLED == true
|
||||
DIRECT_MODE_INPUT(reg_trigger, mask_trigger); // Set trigger pin to input (when using one Arduino pin, this is technically setting the echo pin to input as both are tied to the same Arduino pin).
|
||||
DIRECT_PINMODE_INPUT(_triggerPin); // Set trigger pin to input (when using one Arduino pin, this is technically setting the echo pin to input as both are tied to the same Arduino pin).
|
||||
#endif
|
||||
|
||||
#if URM37_ENABLED == true
|
||||
if (!DIRECT_READ(reg_echo, mask_echo)) {
|
||||
if (!DIRECT_pinRead(_echoPin)) {
|
||||
_errorState = STATUS_ECHO_STATE_ERROR;
|
||||
return false; // Previous ping hasn't finished, abort.
|
||||
}
|
||||
_max_time = micros() + _maxEchoTime + MAX_SENSOR_DELAY; // Maximum time we'll wait for ping to start (most sensors are <450uS, the SRF06 can take up to 34,300uS!)
|
||||
while (DIRECT_READ(reg_echo, mask_echo)) // Wait for ping to start.
|
||||
while (DIRECT_pinRead(_echoPin)) // Wait for ping to start.
|
||||
if (micros() > _max_time) {
|
||||
_errorState = STATUS_ECHO_START_TIMEOUT_DISTANCE;
|
||||
return false; // Took too long to start, abort.
|
||||
}
|
||||
#else
|
||||
if (DIRECT_READ(reg_echo, mask_echo)) {
|
||||
if (DIRECT_pinRead(_echoPin)) {
|
||||
_errorState = STATUS_ECHO_STATE_ERROR;
|
||||
return false; // Previous ping hasn't finished, abort.
|
||||
}
|
||||
_max_time = micros() + _maxEchoTime + MAX_SENSOR_DELAY; // Maximum time we'll wait for ping to start (most sensors are <450uS, the SRF06 can take up to 34,300uS!)
|
||||
while (!DIRECT_READ(reg_echo, mask_echo)) // Wait for ping to start.
|
||||
while (!DIRECT_pinRead(_echoPin)) // Wait for ping to start.
|
||||
if (micros() > _max_time) {
|
||||
_errorState = STATUS_ECHO_START_TIMEOUT_DISTANCE;
|
||||
return false; // Took too long to start, abort.
|
||||
|
||||
@@ -14,6 +14,15 @@
|
||||
#include <GPIO_Direct_Access.h>
|
||||
|
||||
|
||||
// ESP8266 does work fine without the IRAM attribute
|
||||
// But ESP32 may benefit from having the code always loaded in RAM.
|
||||
#ifdef ESP8266
|
||||
# define DALLAS_IRAM_ATTR
|
||||
#endif // ifdef ESP8266
|
||||
#ifdef ESP32
|
||||
# define DALLAS_IRAM_ATTR IRAM_ATTR
|
||||
#endif // ifdef ESP32
|
||||
|
||||
|
||||
#include <vector>
|
||||
|
||||
@@ -86,7 +95,7 @@ void Dallas_uint64_to_addr(uint64_t value, uint8_t addr[]) {
|
||||
}
|
||||
|
||||
void Dallas_addr_selector_webform_load(taskIndex_t TaskIndex, int8_t gpio_pin_rx, int8_t gpio_pin_tx, uint8_t nrVariables) {
|
||||
if (gpio_pin_rx == -1 || gpio_pin_tx == -1) {
|
||||
if ((gpio_pin_rx == -1) || (gpio_pin_tx == -1)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -129,7 +138,7 @@ void Dallas_addr_selector_webform_load(taskIndex_t TaskIndex, int8_t gpio_pin_rx
|
||||
// input and output pins, and therefor
|
||||
// doesn't switch between input and output
|
||||
// when running.
|
||||
if(gpio_pin_rx != gpio_pin_tx) {
|
||||
if (gpio_pin_rx != gpio_pin_tx) {
|
||||
pinMode(gpio_pin_rx, INPUT);
|
||||
pinMode(gpio_pin_tx, OUTPUT);
|
||||
}
|
||||
@@ -206,6 +215,7 @@ void Dallas_addr_selector_webform_save(taskIndex_t TaskIndex, int8_t gpio_pin_rx
|
||||
if (gpio_pin_rx == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (gpio_pin_tx == -1) {
|
||||
return;
|
||||
}
|
||||
@@ -324,20 +334,21 @@ void Dallas_startConversion(const uint8_t ROM[8], int8_t gpio_pin_rx, int8_t gpi
|
||||
bool Dallas_readTemp(const uint8_t ROM[8], float *value, int8_t gpio_pin_rx, int8_t gpio_pin_tx)
|
||||
{
|
||||
int16_t DSTemp;
|
||||
uint8_t ScratchPad[12];
|
||||
uint8_t ScratchPad[12];
|
||||
|
||||
if (!Dallas_address_ROM(ROM, gpio_pin_rx, gpio_pin_tx)) {
|
||||
return false;
|
||||
}
|
||||
Dallas_write(0xBE, gpio_pin_rx, gpio_pin_tx); // Read scratchpad
|
||||
Dallas_write(0xBE, gpio_pin_rx, gpio_pin_tx); // Read scratchpad
|
||||
|
||||
for (uint8_t i = 0; i < 9; i++) { // read 9 bytes
|
||||
for (uint8_t i = 0; i < 9; i++) { // read 9 bytes
|
||||
ScratchPad[i] = Dallas_read(gpio_pin_rx, gpio_pin_tx);
|
||||
}
|
||||
|
||||
bool crc_ok = Dallas_crc8(ScratchPad);
|
||||
|
||||
#ifndef BUILD_NO_DEBUG
|
||||
|
||||
if (loglevelActiveFor(LOG_LEVEL_DEBUG)) {
|
||||
String log = F("DS: SP: ");
|
||||
|
||||
@@ -364,7 +375,7 @@ bool Dallas_readTemp(const uint8_t ROM[8], float *value, int8_t gpio_pin_rx, int
|
||||
log += ll2String(presence_end, DEC);
|
||||
addLogMove(LOG_LEVEL_DEBUG, log);
|
||||
}
|
||||
#endif
|
||||
#endif // ifndef BUILD_NO_DEBUG
|
||||
|
||||
if (!crc_ok)
|
||||
{
|
||||
@@ -413,7 +424,7 @@ bool Dallas_readiButton(const uint8_t addr[8], int8_t gpio_pin_rx, int8_t gpio_p
|
||||
// end maybe this is needed to trigger the reading
|
||||
|
||||
uint8_t tmpaddr[8];
|
||||
bool found = false;
|
||||
bool found = false;
|
||||
|
||||
Dallas_reset(gpio_pin_rx, gpio_pin_tx);
|
||||
String log;
|
||||
@@ -480,11 +491,11 @@ bool Dallas_readCounter(const uint8_t ROM[8], float *value, int8_t gpio_pin_rx,
|
||||
count = (count << 8) + (uint32_t)data[j];
|
||||
}
|
||||
|
||||
uint16_t crc = Dallas_crc16(data, 43, 0);
|
||||
uint16_t crc = Dallas_crc16(data, 43, 0);
|
||||
const uint8_t *crcBytes = reinterpret_cast<const uint8_t *>(&crc);
|
||||
uint8_t crcLo = ~data[43];
|
||||
uint8_t crcHi = ~data[44];
|
||||
bool error = (crcLo != crcBytes[0]) || (crcHi != crcBytes[1]);
|
||||
uint8_t crcLo = ~data[43];
|
||||
uint8_t crcHi = ~data[44];
|
||||
bool error = (crcLo != crcBytes[0]) || (crcHi != crcBytes[1]);
|
||||
|
||||
if (!error)
|
||||
{
|
||||
@@ -511,9 +522,9 @@ uint8_t Dallas_getResolution(const uint8_t ROM[8], int8_t gpio_pin_rx, int8_t gp
|
||||
if (!Dallas_address_ROM(ROM, gpio_pin_rx, gpio_pin_tx)) {
|
||||
return 0;
|
||||
}
|
||||
Dallas_write(0xBE, gpio_pin_rx, gpio_pin_tx); // Read scratchpad
|
||||
Dallas_write(0xBE, gpio_pin_rx, gpio_pin_tx); // Read scratchpad
|
||||
|
||||
for (uint8_t i = 0; i < 9; i++) { // read 9 bytes
|
||||
for (uint8_t i = 0; i < 9; i++) { // read 9 bytes
|
||||
ScratchPad[i] = Dallas_read(gpio_pin_rx, gpio_pin_tx);
|
||||
}
|
||||
|
||||
@@ -554,9 +565,9 @@ bool Dallas_setResolution(const uint8_t ROM[8], uint8_t res, int8_t gpio_pin_rx,
|
||||
if (!Dallas_address_ROM(ROM, gpio_pin_rx, gpio_pin_tx)) {
|
||||
return false;
|
||||
}
|
||||
Dallas_write(0xBE, gpio_pin_rx, gpio_pin_tx); // Read scratchpad
|
||||
Dallas_write(0xBE, gpio_pin_rx, gpio_pin_tx); // Read scratchpad
|
||||
|
||||
for (uint8_t i = 0; i < 9; i++) { // read 9 bytes
|
||||
for (uint8_t i = 0; i < 9; i++) { // read 9 bytes
|
||||
ScratchPad[i] = Dallas_read(gpio_pin_rx, gpio_pin_tx);
|
||||
}
|
||||
|
||||
@@ -613,19 +624,12 @@ uint8_t Dallas_reset(int8_t gpio_pin_rx, int8_t gpio_pin_tx)
|
||||
{
|
||||
uint8_t retries = 125;
|
||||
|
||||
IO_REG_TYPE mask_rx IO_REG_MASK_ATTR = PIN_TO_BITMASK(gpio_pin_rx);
|
||||
IO_REG_TYPE mask_tx IO_REG_MASK_ATTR = PIN_TO_BITMASK(gpio_pin_tx);
|
||||
/*
|
||||
// Not used on ESP platforms
|
||||
volatile IO_REG_TYPE *reg_rx IO_REG_BASE_ATTR = PIN_TO_BASEREG(gpio_pin_rx);
|
||||
volatile IO_REG_TYPE *reg_tx IO_REG_BASE_ATTR = PIN_TO_BASEREG(gpio_pin_tx);
|
||||
*/
|
||||
|
||||
noInterrupts();
|
||||
if(gpio_pin_rx == gpio_pin_tx) {
|
||||
DIRECT_MODE_INPUT(reg_rx, mask_rx);
|
||||
|
||||
if (gpio_pin_rx == gpio_pin_tx) {
|
||||
DIRECT_PINMODE_INPUT(gpio_pin_rx);
|
||||
} else {
|
||||
DIRECT_WRITE_HIGH(reg_tx, mask_tx);
|
||||
DIRECT_pinWrite(gpio_pin_tx, 1);
|
||||
}
|
||||
bool success = true;
|
||||
|
||||
@@ -636,7 +640,7 @@ uint8_t Dallas_reset(int8_t gpio_pin_rx, int8_t gpio_pin_tx)
|
||||
}
|
||||
delayMicroseconds(2);
|
||||
}
|
||||
while (!DIRECT_READ(reg_rx, mask_rx) && success);
|
||||
while (!DIRECT_pinRead(gpio_pin_rx) && success);
|
||||
|
||||
usec_release = 0;
|
||||
presence_start = 0;
|
||||
@@ -646,15 +650,17 @@ uint8_t Dallas_reset(int8_t gpio_pin_rx, int8_t gpio_pin_tx)
|
||||
// The master starts a transmission with a reset pulse,
|
||||
// which pulls the wire to 0 volts for at least 480 µs.
|
||||
// This resets every slave device on the bus.
|
||||
DIRECT_WRITE_LOW(reg_tx, mask_tx);
|
||||
if(gpio_pin_rx == gpio_pin_tx) {
|
||||
DIRECT_MODE_OUTPUT(reg_rx, mask_rx);
|
||||
DIRECT_pinWrite(gpio_pin_tx, 0);
|
||||
|
||||
if (gpio_pin_rx == gpio_pin_tx) {
|
||||
DIRECT_PINMODE_OUTPUT(gpio_pin_rx);
|
||||
}
|
||||
delayMicroseconds(480);
|
||||
if(gpio_pin_rx == gpio_pin_tx) {
|
||||
DIRECT_MODE_INPUT(reg_rx, mask_rx);
|
||||
|
||||
if (gpio_pin_rx == gpio_pin_tx) {
|
||||
DIRECT_PINMODE_INPUT(gpio_pin_rx);
|
||||
} else {
|
||||
DIRECT_WRITE_HIGH(reg_tx, mask_tx);
|
||||
DIRECT_pinWrite(gpio_pin_tx, 1);
|
||||
}
|
||||
|
||||
// After that, any slave device, if present, shows that it exists with a "presence" pulse:
|
||||
@@ -666,12 +672,12 @@ uint8_t Dallas_reset(int8_t gpio_pin_rx, int8_t gpio_pin_tx)
|
||||
// - Presence condition end (minimal duration 60 usec, typ: 100 usec)
|
||||
// - Wait till 480 usec after release.
|
||||
const uint64_t start = getMicros64();
|
||||
int64_t usec_passed = 0;
|
||||
int64_t usec_passed = 0;
|
||||
|
||||
while (usec_passed < 480) {
|
||||
usec_passed = usecPassedSince(start);
|
||||
|
||||
const bool pin_state = !!DIRECT_READ(reg_rx, mask_rx);
|
||||
const bool pin_state = !!DIRECT_pinRead(gpio_pin_rx);
|
||||
|
||||
if (usec_release == 0) {
|
||||
if (pin_state) {
|
||||
@@ -890,9 +896,10 @@ void Dallas_write(uint8_t ByteToWrite, int8_t gpio_pin_rx, int8_t gpio_pin_tx)
|
||||
uint8_t Dallas_read_bit(int8_t gpio_pin_rx, int8_t gpio_pin_tx)
|
||||
{
|
||||
if (gpio_pin_rx == -1) { return 0; }
|
||||
|
||||
if (gpio_pin_tx == -1) { return 0; }
|
||||
const uint64_t start = getMicros64();
|
||||
uint8_t r = Dallas_read_bit_ISR(gpio_pin_rx, gpio_pin_tx, start);
|
||||
uint8_t r = Dallas_read_bit_ISR(gpio_pin_rx, gpio_pin_tx, start);
|
||||
|
||||
while (usecPassedSince(start) < 70ll) {
|
||||
// Wait for another 55 usec
|
||||
@@ -905,45 +912,42 @@ uint8_t Dallas_read_bit(int8_t gpio_pin_rx, int8_t gpio_pin_tx)
|
||||
return r;
|
||||
}
|
||||
|
||||
uint8_t IRAM_ATTR Dallas_read_bit_ISR(int8_t gpio_pin_rx, int8_t gpio_pin_tx, unsigned long start)
|
||||
uint8_t DALLAS_IRAM_ATTR Dallas_read_bit_ISR(
|
||||
int8_t gpio_pin_rx,
|
||||
int8_t gpio_pin_tx,
|
||||
unsigned long start)
|
||||
{
|
||||
uint8_t r;
|
||||
{
|
||||
IO_REG_TYPE mask_rx IO_REG_MASK_ATTR = PIN_TO_BITMASK(gpio_pin_rx);
|
||||
IO_REG_TYPE mask_tx IO_REG_MASK_ATTR = PIN_TO_BITMASK(gpio_pin_tx);
|
||||
/*
|
||||
// Not used on ESP platforms
|
||||
volatile IO_REG_TYPE *reg_rx IO_REG_BASE_ATTR = PIN_TO_BASEREG(gpio_pin_rx);
|
||||
volatile IO_REG_TYPE *reg_tx IO_REG_BASE_ATTR = PIN_TO_BASEREG(gpio_pin_tx);
|
||||
*/
|
||||
|
||||
{
|
||||
noInterrupts();
|
||||
DIRECT_WRITE_LOW(reg_tx, mask_tx);
|
||||
if(gpio_pin_rx == gpio_pin_tx) {
|
||||
DIRECT_MODE_OUTPUT(reg_rx, mask_rx);
|
||||
DIRECT_pinWrite(gpio_pin_tx, 0);
|
||||
|
||||
if (gpio_pin_rx == gpio_pin_tx) {
|
||||
DIRECT_PINMODE_OUTPUT(gpio_pin_rx);
|
||||
}
|
||||
|
||||
while (usecPassedSince(start) < 6) {
|
||||
// Wait for 6 usec
|
||||
}
|
||||
const uint64_t startwait = getMicros64();
|
||||
if(gpio_pin_rx == gpio_pin_tx) {
|
||||
DIRECT_MODE_INPUT(reg_rx, mask_rx); // let pin float, pull up will raise
|
||||
|
||||
if (gpio_pin_rx == gpio_pin_tx) {
|
||||
DIRECT_PINMODE_INPUT(gpio_pin_rx); // let pin float, pull up will raise
|
||||
} else {
|
||||
DIRECT_WRITE_HIGH(reg_tx, mask_tx);
|
||||
DIRECT_pinWrite(gpio_pin_tx, 1);
|
||||
}
|
||||
|
||||
while (usecPassedSince(startwait) < 9ll) {
|
||||
// Wait for another 9 usec
|
||||
}
|
||||
r = DIRECT_READ(reg_rx, mask_rx);
|
||||
r = DIRECT_pinRead(gpio_pin_rx);
|
||||
|
||||
interrupts();
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
/*********************************************************************************************\
|
||||
* Dallas Write bit
|
||||
\*********************************************************************************************/
|
||||
@@ -956,7 +960,7 @@ void Dallas_write_bit(uint8_t v, int8_t gpio_pin_rx, int8_t gpio_pin_tx)
|
||||
// write 0: low 60 usec, high 10 usec
|
||||
const long low_time = (v & 1) ? 6 : 60;
|
||||
const long high_time = (v & 1) ? 64 : 10;
|
||||
uint64_t start = getMicros64();
|
||||
uint64_t start = getMicros64();
|
||||
|
||||
Dallas_write_bit_ISR(v, gpio_pin_rx, gpio_pin_tx, low_time, high_time, start);
|
||||
|
||||
@@ -965,31 +969,25 @@ void Dallas_write_bit(uint8_t v, int8_t gpio_pin_rx, int8_t gpio_pin_tx)
|
||||
}
|
||||
}
|
||||
|
||||
void IRAM_ATTR Dallas_write_bit_ISR(uint8_t v,
|
||||
int8_t gpio_pin_rx,
|
||||
int8_t gpio_pin_tx,
|
||||
long low_time,
|
||||
long high_time,
|
||||
uint64_t &start)
|
||||
void DALLAS_IRAM_ATTR Dallas_write_bit_ISR(uint8_t v,
|
||||
int8_t gpio_pin_rx,
|
||||
int8_t gpio_pin_tx,
|
||||
long low_time,
|
||||
long high_time,
|
||||
uint64_t& start)
|
||||
{
|
||||
IO_REG_TYPE mask_rx IO_REG_MASK_ATTR = PIN_TO_BITMASK(gpio_pin_rx);
|
||||
IO_REG_TYPE mask_tx IO_REG_MASK_ATTR = PIN_TO_BITMASK(gpio_pin_tx);
|
||||
/*
|
||||
// Not used on ESP platforms
|
||||
volatile IO_REG_TYPE *reg_rx IO_REG_BASE_ATTR = PIN_TO_BASEREG(gpio_pin_rx);
|
||||
volatile IO_REG_TYPE *reg_tx IO_REG_BASE_ATTR = PIN_TO_BASEREG(gpio_pin_tx);
|
||||
*/
|
||||
noInterrupts();
|
||||
DIRECT_WRITE_LOW(reg_tx, mask_tx);
|
||||
if(gpio_pin_rx == gpio_pin_tx) {
|
||||
DIRECT_MODE_OUTPUT(reg_rx, mask_rx);
|
||||
DIRECT_pinWrite(gpio_pin_tx, 0);
|
||||
|
||||
if (gpio_pin_rx == gpio_pin_tx) {
|
||||
DIRECT_PINMODE_OUTPUT(gpio_pin_rx);
|
||||
}
|
||||
|
||||
while (usecPassedSince(start) < low_time) {
|
||||
// output remains low
|
||||
}
|
||||
start = getMicros64();
|
||||
DIRECT_WRITE_HIGH(reg_tx, mask_tx);
|
||||
DIRECT_pinWrite(gpio_pin_tx, 1);
|
||||
interrupts();
|
||||
}
|
||||
|
||||
@@ -1059,7 +1057,6 @@ uint16_t Dallas_crc16(const uint8_t *input, uint16_t len, uint16_t crc)
|
||||
return crc;
|
||||
}
|
||||
|
||||
|
||||
void Dallas_SensorData::set_measurement_inactive() {
|
||||
measurementActive = false;
|
||||
value = 0.0f;
|
||||
|
||||
@@ -78,21 +78,19 @@ P005_data_struct::P005_data_struct(struct EventStruct *event) {
|
||||
bool P005_data_struct::waitState(int state)
|
||||
{
|
||||
const uint64_t timeout = getMicros64() + 100;
|
||||
IO_REG_TYPE mask IO_REG_MASK_ATTR = PIN_TO_BITMASK(DHT_pin);
|
||||
|
||||
#ifdef DEBUG_LOGIC_ANALYZER_PIN
|
||||
// DEBUG code using logic analyzer for timings
|
||||
IO_REG_TYPE mask_debug_pin IO_REG_MASK_ATTR = PIN_TO_BITMASK(DEBUG_LOGIC_ANALYZER_PIN);
|
||||
DIRECT_WRITE_LOW(reg, mask_debug_pin);
|
||||
DIRECT_pinWrite(DEBUG_LOGIC_ANALYZER_PIN, 0);
|
||||
#endif
|
||||
|
||||
while (DIRECT_READ(reg, mask) != state)
|
||||
while (DIRECT_pinRead(DHT_pin) != state)
|
||||
{
|
||||
if (usecTimeOutReached(timeout)) { return false; }
|
||||
}
|
||||
|
||||
#ifdef DEBUG_LOGIC_ANALYZER_PIN
|
||||
DIRECT_WRITE_HIGH(reg, mask_debug_pin);
|
||||
DIRECT_pinWrite(DEBUG_LOGIC_ANALYZER_PIN, 1);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
@@ -101,21 +99,18 @@ bool P005_data_struct::waitState(int state)
|
||||
* Perform the actual reading + interpreting of data.
|
||||
\*********************************************************************************************/
|
||||
bool P005_data_struct::readDHT(struct EventStruct *event) {
|
||||
IO_REG_TYPE mask IO_REG_MASK_ATTR = PIN_TO_BITMASK(DHT_pin);
|
||||
|
||||
#ifdef DEBUG_LOGIC_ANALYZER_PIN
|
||||
// DEBUG code using logic analyzer for timings
|
||||
IO_REG_TYPE mask_debug_pin IO_REG_MASK_ATTR = PIN_TO_BITMASK(DEBUG_LOGIC_ANALYZER_PIN);
|
||||
DIRECT_MODE_OUTPUT(reg, mask_debug_pin);
|
||||
DIRECT_WRITE_LOW(reg, mask_debug_pin);
|
||||
DIRECT_PINMODE_OUTPUT(DEBUG_LOGIC_ANALYZER_PIN);
|
||||
DIRECT_pinWrite(DEBUG_LOGIC_ANALYZER_PIN, 0);
|
||||
#endif
|
||||
|
||||
// To begin asking the DHT22 for humidity and temperature data,
|
||||
// Start sequence to get data from a DHTxx sensor:
|
||||
// Pin must be a logic 0 (low) for at least 500 microseconds (DHT22, others may need different timing)
|
||||
// followed by a logic 1 (high).
|
||||
DIRECT_MODE_OUTPUT(reg, mask);
|
||||
DIRECT_WRITE_LOW(reg, mask); // Pull low
|
||||
DIRECT_PINMODE_OUTPUT(DHT_pin);
|
||||
DIRECT_pinWrite(DHT_pin, 0); // Pull low
|
||||
|
||||
switch (SensorModel) {
|
||||
case P005_DHT11: delay(19); break; // minimum 18ms
|
||||
@@ -127,14 +122,14 @@ bool P005_data_struct::readDHT(struct EventStruct *event) {
|
||||
|
||||
{
|
||||
#ifdef DEBUG_LOGIC_ANALYZER_PIN
|
||||
DIRECT_WRITE_HIGH(reg, mask_debug_pin);
|
||||
DIRECT_pinWrite(DEBUG_LOGIC_ANALYZER_PIN, 1);
|
||||
#endif
|
||||
|
||||
DIRECT_MODE_INPUT(reg, mask);
|
||||
DIRECT_PINMODE_INPUT(DHT_pin);
|
||||
// pinMode(DHT_pin, INPUT_PULLUP); // Way too slow, takes upto 227 usec
|
||||
|
||||
#ifdef DEBUG_LOGIC_ANALYZER_PIN
|
||||
DIRECT_WRITE_LOW(reg, mask_debug_pin);
|
||||
DIRECT_pinWrite(DEBUG_LOGIC_ANALYZER_PIN, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -158,7 +153,7 @@ bool P005_data_struct::readDHT(struct EventStruct *event) {
|
||||
receive_start = waitState(0) && waitState(1) && waitState(0);
|
||||
|
||||
#ifdef DEBUG_LOGIC_ANALYZER_PIN
|
||||
DIRECT_WRITE_LOW(reg, mask_debug_pin);
|
||||
DIRECT_pinWrite(DEBUG_LOGIC_ANALYZER_PIN, 0);
|
||||
#endif
|
||||
|
||||
if (receive_start) {
|
||||
@@ -170,7 +165,7 @@ bool P005_data_struct::readDHT(struct EventStruct *event) {
|
||||
{
|
||||
// Start reading next byte
|
||||
#ifdef DEBUG_LOGIC_ANALYZER_PIN
|
||||
DIRECT_WRITE_HIGH(reg, mask_debug_pin);
|
||||
DIRECT_pinWrite(DEBUG_LOGIC_ANALYZER_PIN, 1);
|
||||
#endif
|
||||
for (uint8_t t = 0; t < 16 && !readingAborted; ++t) {
|
||||
// "even" index = "low" duration
|
||||
@@ -178,7 +173,7 @@ bool P005_data_struct::readDHT(struct EventStruct *event) {
|
||||
const uint32_t current_state = (t & 1);
|
||||
|
||||
// Wait till pin state has changed, or timeout.
|
||||
while (DIRECT_READ(reg, mask) == current_state && !readingAborted)
|
||||
while (DIRECT_pinRead(DHT_pin) == current_state && !readingAborted)
|
||||
{
|
||||
// Keep track of last microsecond the state had not yet changed.
|
||||
// This way we are less dependent on any jitter caused by
|
||||
@@ -199,7 +194,7 @@ bool P005_data_struct::readDHT(struct EventStruct *event) {
|
||||
}
|
||||
}
|
||||
#ifdef DEBUG_LOGIC_ANALYZER_PIN
|
||||
DIRECT_WRITE_LOW(reg, mask_debug_pin);
|
||||
DIRECT_pinWrite(DEBUG_LOGIC_ANALYZER_PIN, 0);
|
||||
#endif
|
||||
|
||||
if (!readingAborted) {
|
||||
|
||||
@@ -29,41 +29,9 @@ else:
|
||||
"-DUSES_P002", # ADC
|
||||
"-DUSES_P003", # Generic Pulse Counter
|
||||
"-DUSES_P004", # Dallas DS18b20
|
||||
"-DUSES_P026", # System info
|
||||
"-DUSES_P027", # INA219
|
||||
"-DUSES_P028", # BME280
|
||||
"-DUSES_P033", # Dummy
|
||||
"-DUSES_P036", # FrameOLED
|
||||
"-DUSES_P045", # MPU6050
|
||||
"-DUSES_P049", # MHZ19
|
||||
"-DUSES_P052", # SenseAir
|
||||
"-DUSES_P056", # SDS011-Dust
|
||||
# "-DUSES_P059", # Encoder
|
||||
# "-DUSES_P080", # Dallas iButton
|
||||
"-DUSES_P081", # Cron
|
||||
"-DUSES_P082", # GPS
|
||||
"-DUSES_P085", # AcuDC24x
|
||||
"-DUSES_P098", # PWM motor
|
||||
|
||||
"-DUSES_P100", # Pulse Counter - DS2423
|
||||
# "-DUSES_P087", # Serial Proxy
|
||||
# "-DUSES_P094", # CUL Reader
|
||||
# "-DUSES_P095", # TFT ILI9341
|
||||
# "-DUSES_P106", # BME680
|
||||
# "-DUSES_P107", # SI1145 UV index
|
||||
|
||||
"-DUSES_C016", # Cache Controller
|
||||
"-DUSES_C018", # TTN/RN2483
|
||||
# "-DUSES_C015", # Blynk
|
||||
|
||||
# "-DFEATURE_MDNS=1",
|
||||
# "-DFEATURE_SD=1",
|
||||
"-DFEATURE_EXT_RTC=1",
|
||||
"-DFEATURE_I2CMULTIPLEXER=1",
|
||||
"-DFEATURE_TRIGONOMETRIC_FUNCTIONS_RULES=1",
|
||||
"-DFEATURE_CUSTOM_PROVISIONING=1",
|
||||
|
||||
"-DFEATURE_ESPEASY_P2P=1",
|
||||
|
||||
"-DFEATURE_SETTINGS_ARCHIVE=1"
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user