User Tools

Site Tools


esp32:direct_pin_data_copy

copy data from input pin to output pin - example ws8212 data stream

fast way to copy read/write register values

void loop() {
  int v1 = 0;
  portDISABLE_INTERRUPTS();
  for(;;) {
    auto v1 = (GPIO_REG_READ(GPIO_IN_REG) & BIT(25));
    if (v1) {
       GPIO_REG_WRITE(GPIO_OUT_W1TS_REG, BIT(27)); 
       //cnt3 = 0;
    }
    else { 
      GPIO_REG_WRITE(GPIO_OUT_W1TC_REG, BIT(27)); 
      //cnt3 ++;
    }
    cnt3 ++;
    if (cnt3 % 1000000 == 0) {
      //Serial.println("loop");
      portENABLE_INTERRUPTS();
      vTaskDelay(10);
      portDISABLE_INTERRUPTS();
    }    
  }
}

speed is like 3.5 times faster

  • 07:31:40.620 → register based read : 53
  • 07:31:40.620 → digitalRead based read : 172
  auto t1 = micros();
  int v1;
  for (int i=0;i<1000;i++)
    v1 = (GPIO_REG_READ(GPIO_IN_REG) & BIT(25));
  auto t2 = micros();
  auto t3 = micros();
  for (int i=0;i<1000;i++)
    v1 = digitalRead(25);
  auto t4 = micros();
  Serial.printf("register based read : %d\n",t2-t1);
  Serial.printf("digitalRead based read : %d\n",t4-t3);  
esp32/direct_pin_data_copy.txt · Last modified: by aistis