Skip to content

Commit c02bd1f

Browse files
committed
HM01B0 PIO driver: restart state machine in vsync handler
Helps #19, but does not fully fix it If a desync occurs, the state machine's ISR would have a few extra bits in it, caused all subsequent frames to be corrupted Restarting the state machine clears the ISR
1 parent 7bdeb99 commit c02bd1f

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

drivers/camera/hm01b0_pio.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,20 @@ def soft_reset(self):
240240
"""
241241
self.writeRegister(self.SW_RESET, self.HIMAX_RESET)
242242

243+
def setMode(self, mode):
244+
"""
245+
Sets the operating mode of the HM01B0 sensor.
246+
Args:
247+
mode (int): The mode to set, e.g., MODE_STREAMING.
248+
"""
249+
self.writeRegister(self.MODE_SELECT, mode)
250+
251+
def trigger(self):
252+
self.writeRegister(self.MODE_SELECT, self.HIMAX_MODE_STREAMING_NFRAMES)
253+
254+
def set_n_frames(self, n_frames):
255+
self.writeRegister(self.PMU_AUTOSLEEP_FRAMECNT, n_frames)
256+
243257
def send_init(self):
244258
"""
245259
Initializes the HM01B0 sensor with default settings.
@@ -248,9 +262,6 @@ def send_init(self):
248262
for reg, value in self.INIT_COMMANDS:
249263
self.writeRegister(reg, value)
250264
sleep_us(1000)
251-
252-
# Ensure the sensor is in streaming mode
253-
# self.writeRegister(self.MODE_SELECT, self.HIMAX_MODE_STREAMING)
254265

255266
def readRegister(self, reg, nbytes=1):
256267
self.i2c.writeto(self.i2c_address, bytes([reg >> 8, reg & 0xFF]))
@@ -323,7 +334,10 @@ def _vsync_handler(self):
323334
# Disable DMA before reconfiguring it
324335
self.dma.active(False)
325336

326-
# Ensure PIO RX FIFO is empty
337+
# Reset state machine to ensure ISR is cleared
338+
self.sm.restart()
339+
340+
# Ensure PIO RX FIFO is empty (it's not emptied by `sm.restart()`)
327341
while self.sm.rx_fifo() > 0:
328342
self.sm.get()
329343

0 commit comments

Comments
 (0)