User Tools

Site Tools


esp32:direct_pin_data_copy

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
esp32:direct_pin_data_copy [2019/09/26 20:47] – aistisesp32:direct_pin_data_copy [2019/09/27 07:34] (current) – aistis
Line 1: Line 1:
 copy data from input pin to output pin - example ws8212 data stream copy data from input pin to output pin - example ws8212 data stream
 +
 +fast way to copy read/write register values
  
 <code cpp> <code cpp>
 void loop() { void loop() {
- +  int v1 = 0;
-   +
-  uint64_t cnt = 0; +
-  register uint32_t cnt2 = 0; +
-  //disableCore0WDT(); +
-  //disableCore1WDT();+
   portDISABLE_INTERRUPTS();   portDISABLE_INTERRUPTS();
-  //disableCore0WDT(); +  for(;;) { 
-  //disableCore1WDT(); +    auto v1 = (GPIO_REG_READ(GPIO_IN_REG) & BIT(25)); 
-  gpio_num_t pin = (gpio_num_t)(25 & 0x1F); +    if (v1) { 
-  int state=0; +       GPIO_REG_WRITE(GPIO_OUT_W1TS_REG, BIT(27));  
-  register int v1 = digitalRead(25); +       //cnt3 = 0;
-  register int p1 = digitalRead(25); +
-  for (;;) { +
-    v1 = (GPIO_REG_READ(GPIO_IN_REG)  >> pin) & 1U; +
-    if (v1 != p1) +
-    { +
-      digitalWrite(27, v1); +
-      p1 = v1;+
     }     }
-    cnt2 ++; +    else {  
-    if (cnt2 % 1000000 == 0) {+      GPIO_REG_WRITE(GPIO_OUT_W1TC_REG, BIT(27));  
 +      //cnt3 ++; 
 +    } 
 +    cnt3 ++; 
 +    if (cnt3 % 1000000 == 0) {
       //Serial.println("loop");       //Serial.println("loop");
       portENABLE_INTERRUPTS();       portENABLE_INTERRUPTS();
 +      vTaskDelay(10);
       portDISABLE_INTERRUPTS();       portDISABLE_INTERRUPTS();
-    }+    }    
   }   }
 } }
 +</code>
 +
 +
 +speed is like 3.5 times faster
 +
 +  * 07:31:40.620 -> register based read : 53
 +  * 07:31:40.620 -> digitalRead based read : 172
 +
 +<code>
 +  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);  
 </code> </code>
esp32/direct_pin_data_copy.1569520078.txt.gz · Last modified: by aistis