Skip to content

Commit 13f3ac5

Browse files
sorscodeme-no-dev
authored andcommitted
Enable pulseIn() (#140)
* Add files via upload enable pulseIn() * Remove optimistic_yield
1 parent 57dbc9a commit 13f3ac5

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

cores/esp32/Arduino.h

+6
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ void initArduino(void);
133133
void setup(void);
134134
void loop(void);
135135

136+
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
137+
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout);
138+
136139
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
137140
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
138141

@@ -156,6 +159,9 @@ uint16_t makeWord(byte h, byte l);
156159

157160
#define word(...) makeWord(__VA_ARGS__)
158161

162+
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
163+
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
164+
159165
// WMath prototypes
160166
long random(long);
161167
#endif /* __cplusplus */

cores/esp32/wiring_pulse.c

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
pulse.c - wiring pulseIn implementation for esp8266
3+
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
4+
This file is part of the esp8266 core for Arduino environment.
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
Lesser General Public License for more details.
13+
You should have received a copy of the GNU Lesser General Public
14+
License along with this library; if not, write to the Free Software
15+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16+
*/
17+
//#include <limits.h>
18+
#include "wiring_private.h"
19+
#include "pins_arduino.h"
20+
21+
22+
extern uint32_t xthal_get_ccount();
23+
24+
#define WAIT_FOR_PIN_STATE(state) \
25+
while (digitalRead(pin) != (state)) { \
26+
if (xthal_get_ccount() - start_cycle_count > timeout_cycles) { \
27+
return 0; \
28+
} \
29+
}
30+
31+
// max timeout is 27 seconds at 160MHz clock and 54 seconds at 80MHz clock
32+
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
33+
{
34+
const uint32_t max_timeout_us = clockCyclesToMicroseconds(UINT_MAX);
35+
if (timeout > max_timeout_us) {
36+
timeout = max_timeout_us;
37+
}
38+
const uint32_t timeout_cycles = microsecondsToClockCycles(timeout);
39+
const uint32_t start_cycle_count = xthal_get_ccount();
40+
WAIT_FOR_PIN_STATE(!state);
41+
WAIT_FOR_PIN_STATE(state);
42+
const uint32_t pulse_start_cycle_count = xthal_get_ccount();
43+
WAIT_FOR_PIN_STATE(!state);
44+
return clockCyclesToMicroseconds(xthal_get_ccount() - pulse_start_cycle_count);
45+
}
46+
47+
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout)
48+
{
49+
return pulseIn(pin, state, timeout);
50+
}

0 commit comments

Comments
 (0)