Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By penatenjoe
#58803 You know you have to add a yield() or delay() to avoid watchdog resets, right?
Also, I think someone pointed out in a different thread that every now and then the CPU is busy with wifi which may make you miss a couple of clock ticks.
See also http://www.esp8266.com/viewtopic.php?f=32&t=11426
User avatar
By JimDrew
#71542
Code: Select all#define CLOCK 0x0010 // GPIO4 bit mask
#define DATA 0x0020 // GPIO5 bit mask
#define GPIO_IN ((volatile uint32_t*) 0x60000318)

void loop() {
  uint32_t data;
  while((*GPIO_IN & CLOCK) == 0) { }     
  // rising edge
  data = (*GPIO_IN & DATA);
  // wait for falling edge
  while((*GPIO_IN & CLOCK) == 1) {}
}


I was looking for some info on accessing the pins directly and I see that this code example won't work. The reason is that the values being checked here are masks. When you AND (&) GPIO_IN with CLOCK (or DATA), you are going to get either 0x0000 or the mask value (0x0010 for CLOCK and 0x0020 for DATA) as the result. Comparing for 0 is fine, but comparing for 1 is not. You should be comparing for !=0 instead of 1 because the result will never be 1 in this case (because CLOCK is 0x0010, not 0x0001 which would work). ie.. (0xFFFF & 0x0010) = 0x0010, (0x0000 & 0x0010) = 0x0000.