RMT peripheral and reading a pin

stoikos
Posts: 31
Joined: Mon Aug 06, 2018 4:38 am

RMT peripheral and reading a pin

Postby stoikos » Mon Aug 06, 2018 7:56 pm

This is what I am trying to achieve:

Code: Select all

do for 25 times:
        set PIN_OUT for output
        set PIN_IN for input
        send 1 microsecond pulse to PIN_OUT (HIGH for 1 microsecond, LOW for the next microsecond)
        immediately read  PIN_IN status (HIGH or LOW)
        store value

I think using RMT should do it but when I tried it didn't seem to work. Any suggestions or sample code?

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: RMT peripheral and reading a pin

Postby kolban » Wed Aug 08, 2018 5:21 am

If it were me, I'd probably look at some low-level bit banging ... some function similar to the following (pseudo code)

Code: Select all

int doPulse() {
   Disable interrupts;
   write PIN_OUT HIGH;
   wait 1uS
   write PIN_OUT_LOW;
   wait 1uS
   int val = read PIN_IN;
   Enable interrupts;
   return value;
}
while it certainly isn't nearly as elegant as leveraging an underlying hardware peripheral, it does somehow feel useful.

Another alternative that we might want to examine is potentially seeing if we could leverage the SPI protocol.

It almost feels like you have an output clock that is at 2MHz. Perhaps we could use SPI CLK and MISO?

I'd also look to clarify your specification. Do we read when PIN_OUT goes low or do we read 1uS after PIN_OUT goes low?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

stoikos
Posts: 31
Joined: Mon Aug 06, 2018 4:38 am

Re: RMT peripheral and reading a pin

Postby stoikos » Fri Aug 10, 2018 12:28 am

Hi Neil,
The code you wrote was my initial guess. When looking for 1us delay that only thing I found was ets_delay_us() which I believe it is not recommended. I could use I think esp_timer_get_time() but I have read some recommendations about using RMT. RMT is based on interrupts so disabling them is not a good idea. I played around it but I wasn't very successful. if using ets_delay_us() is ok I assume your code would work like a charm :)

keckert
Posts: 8
Joined: Tue Apr 03, 2018 8:44 pm

Re: RMT peripheral and reading a pin

Postby keckert » Tue Aug 14, 2018 4:33 pm

The RMT peripheral has an interrupt:
  • The RMT transmitter has finished transmitting the signal - rmt_set_tx_intr_en()
You could transmit your short sequence with the RMT and read your input pin with the interrupt service routine.
Sorry, I do not have example code.

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: RMT peripheral and reading a pin

Postby fly135 » Tue Aug 14, 2018 4:49 pm

Just for reference, I just put some code in the beginning of the 2nd stage bootloader that needed to delay. I created a loop that read the time and calculated a timeout and toggled a pin every loop and the loop was around 40usec. I modded it to just a loop count test and could achieve a pulse width of 1.4 usec. That's before anything even gets set up in the 2nd stage. So a pulse width of 1 usec with bit banging is not likely.

John A

Who is online

Users browsing this forum: No registered users and 319 guests