MustafaMulla29/rp2040-bldc-motor-controller-new
A USB‑C PD–powered RP2040 BLDC motor controller PCB with 3.3 V logic, MCT8329A sensorless commutation, three MOSFET half-bridges, protected power switching, USB data, controls, and motor connectors.
- Version
- 0.3.82
- License
- unset
- Stars
- 0
firmware/provision_pd.py
"""Run once from MicroPython REPL, powered by a 5 V-only USB host/supply.
Set SW4 OFF and disconnect the motor. Do not connect a PD-capable source until this
script succeeds and verify_pd_boot.py passes after complete power removal.
"""
from machine import Pin, ADC, I2C
from pd_nvm import NVM
Pin(2, Pin.OUT, value=0)
Pin(3, Pin.OUT, value=0)
bus = I2C(1, sda=Pin(22), scl=Pin(23), freq=100_000)
def millivolts(channel):
return ADC(channel).read_u16() * 3300 * 235 // (65535 * 15)
if not 4400 <= millivolts(26) <= 5500 or millivolts(27) > 1000:
raise OSError("Use 5 V-only USB power, with SW4 OFF")
nvm = NVM(bus)
# Preserve a per-device recovery copy before the first write; never overwrite it.
try:
with open("pd-nvm-original.bin", "rb") as backup:
if len(backup.read()) != 40:
raise OSError("Existing NVM backup is incomplete")
except OSError as error:
# ENOENT is the only acceptable reason for a missing backup.
if error.args[0] != 2:
raise
with open("pd-nvm-original.bin", "wb") as backup:
backup.write(nvm.read())
import os
os.sync()
changed = nvm.provision_9v()
print("NVM programmed and read back." if changed else "NVM already correct; no write needed.")
print("Remove ALL power. Reconnect 5 V-only USB, then run verify_pd_boot.py before power_manager.py.")