-
Hello, I'm wondering if I should be able to write digital IO at high speeds on the ESP32. I'm trying to drive a stepper, and am using 4 output pins to generate the waveform. I need to switch at 2ms intervals for this, but it isn't working. When I look at the output on a scope, I see that the timing is actually between 2-5ms. Even looping at 50ms intervals still isn't perfect. At around 100ms intervals, timing looks fine. I ran my code on the adorable little Seeed Studio MCU, and also on the Rust devboard to rule out a bad device. They both performed about the same. I'll include the full code below. Any ideas on what could be causing the issue here? Is there some way to guarantee realtime execution? import * as ds from "@devicescript/core"
import { GPIOMode } from "@devicescript/core"
import "@devicescript/gpio"
import { pins } from "@dsboard/seeed_xiao_esp32c3"
import { schedule } from '@devicescript/runtime'
const minStepInterval = 2
const p0 = pins.A0_D0
const p1 = pins.A1_D1
const p2 = pins.A2_D2
const p3 = pins.A3_D3
await p0.setMode(GPIOMode.Output)
await p1.setMode(GPIOMode.Output)
await p2.setMode(GPIOMode.Output)
await p3.setMode(GPIOMode.Output)
const oneStep = async (step: number) => {
switch (step) {
case 0:
await p0.write(1)
await p1.write(0)
await p2.write(0)
await p3.write(0)
break;
case 1:
await p0.write(0)
await p1.write(1)
await p2.write(0)
await p3.write(0)
break;
case 2:
await p0.write(0)
await p1.write(0)
await p2.write(1)
await p3.write(0)
break;
case 3:
await p0.write(0)
await p1.write(0)
await p2.write(0)
await p3.write(1)
break;
default:
break;
}
}
schedule(async ({ counter, elapsed, delta }) => {
await oneStep(counter % 4)
}, { interval: minStepInterval }) |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
Unfortunately, DeviceScript is not designed for real-time bit-banging :/ Would PWM work for you (assuming you can turn it on/off with a few ms accuracy)? We currently only expose it as services implemented in C (lightbulb, buzzer, motor, servo) but could probably expose it directly? |
Beta Was this translation helpful? Give feedback.
-
Have you tried digitalWrite functions? https://microsoft.github.io/devicescript/developer/drivers/digital-io/wire#digitalwrite |
Beta Was this translation helpful? Give feedback.
-
Thanks all! For my case, I'll be able to move to a component that uses SPI rather than trying to bang it out manually. I also reread the documentation and realized that GC is going to cause pauses anyway, so no way to do realtime. |
Beta Was this translation helpful? Give feedback.
-
@ertw keep us posted on your progress! |
Beta Was this translation helpful? Give feedback.
Unfotunately these will suffer from the same timing issues.