I'm using UART1 on the ESP8266 to generate a precise timed pulse on IO2 / D4 in hardware.
In my code I need to know if the pulse is finished or not before I accidentally start a new one.
I found that when using IO2 with UART, the pin level cannot be read by GPI / digitalRead().
Also, despite esp8266_peri.h defining that U1S should reflect the pin level, it does not work...
//UART 1 Registers
#define U1F ESP8266_REG(0xF00) //UART FIFO
#define U1S ESP8266_REG(0xF1C) //STATUS
//UART STATUS Registers Bits
#define USTX 31 //TX PIN Level
This is my test code:
#include <Arduino.h>
#include <Ticker.h>
Ticker tick;
// U1 TX: GPIO_02 / D4
#define P_DEBUG 14 // D5
#define DEBUG_HIGH GPOS = 1<<P_DEBUG
#define DEBUG_LOW GPOC = 1<<P_DEBUG
void ICACHE_RAM_ATTR send() {
DEBUG_HIGH;
DEBUG_LOW;
U1F = 0x80;
}
void setup() {
Serial1.begin(9600);
pinMode(P_DEBUG,OUTPUT);
U1S |= 0x01 << USTXC;
U1D = 10*100;
tick.attach(0.01, send);
}
void loop() {
if(U1S & (1<<USTX)) DEBUG_HIGH;
else DEBUG_LOW;
if(GPI & (1<<2)) DEBUG_HIGH;
else DEBUG_LOW;
}
// What I get:
// IO_02: ‾‾‾‾|_______________|‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾|___
// DEBUG: ____|_________________________________|___
// What I expect: Debug HIGH/PULSING on either IO_02 LOW or HIGH
Am I missing something?
Is there a way to find out when IO2 is HIGH again after the LOW pulse?
Thx!