MustafaMulla29/rp2040-bldc-motor-controller

Hardware assembly metadata and PCB geometry for a controller board with a snap-off rotary-encoder section, plus four parallel 150 µF/50 V bulk electrolytic capacitors filtering the motor supply.

Version
2.0.40
License
unset
Stars
0

Use with the tscircuit CLI

tsci is the tscircuit CLI. Install it with npm install -g tscircuit.

tsci clone MustafaMulla29/rp2040-bldc-motor-controller  # download the package files into ./rp2040-bldc-motor-controller
tsci add MustafaMulla29/rp2040-bldc-motor-controller    # add it to your project as @tsci/MustafaMulla29.rp2040-bldc-motor-controller

In a cloned or new (tsci init) project: tsci dev starts a local preview, tsci build outputs Circuit JSON, and tsci push publishes to tscircuit.com.

firmware/oled_ssd1309.py

"""SSD1309 for the isolated-power hardware revision (TCA9538, I2C address 0x70).

P0 is an ACTIVE-LOW power request through Q16. Input or HIGH means OFF.
The hardware supervisor owns RESET. Firmware never releases it or bypasses it.
Do not use this driver on the original TCA9536/0x41 board.
"""
try:
    from time import sleep_ms
except ImportError:
    from time import sleep
    def sleep_ms(ms): sleep(ms / 1000)

GPIO_ADDRESS = 0x70
GPIO_CONFIGURATION = 0xfe  # P0 output, grounded P1..P7 MUST remain inputs
POWER_OFF = 0xff
POWER_ON = 0xfe
STARTUP_WAIT_MS = 1500
DISCHARGE_WAIT_MS = 700  # analytical allowance; physical waveforms still required


def power_off(i2c, address=0x3c, delay_ms=sleep_ms):
    """Request DISPLAY OFF, then disable boost even if the panel does not ACK.

    Software cleanup needs a working bus. The supervisor separately handles
    abrupt 3.3 V failure; a successful write is not a rail-voltage measurement.
    """
    try:
        i2c.writeto(address, b'\x00\xae')
    finally:
        i2c.writeto(GPIO_ADDRESS, bytes((1, POWER_OFF)))
        delay_ms(DISCHARGE_WAIT_MS)


class Display:
    def __init__(self, i2c, address=0x3c, delay_ms=sleep_ms, power_ready=None):
        self.i2c, self.address, self.delay = i2c, address, delay_ms
        self.power_ready = power_ready
        self.buffer = bytearray(1024)
        self.enabled = False
        self.closed = False
        try:
            # Allow the reservoir and two supervisors to settle. A missing ACK
            # afterwards fails closed; this delay does not override power-good.
            delay_ms(STARTUP_WAIT_MS)
            power_off(i2c, address, delay_ms)
            # Defaults and preloaded HIGH are OFF through Q16, including a reset
            # occurring between these writes. Never configure P1..P7 as outputs.
            self.gpio(1, POWER_OFF)
            self.gpio(3, GPIO_CONFIGURATION)
            self.require_power()
            self.command(0xfd, 0x12, 0xae, 0xd5, 0xa0, 0xa8, 0x3f,
                         0xd3, 0, 0x40, 0xa1, 0xc8, 0xda, 0x12,
                         0x81, 0xbf, 0xd9, 0x25, 0xdb, 0x34, 0xa4, 0xa6)
            self.show()
            self.require_power()
            self.gpio(1, POWER_ON)
            self.enabled = True
            delay_ms(150)  # converter settling plus panel's 100 ms VCC interval
            self.require_power()
            self.command(0xaf)
        except BaseException:
            try:
                self.off()
            except BaseException as error:
                print("OLED shutdown failed:", error)
            raise

    def gpio(self, register, value):
        self.i2c.writeto(GPIO_ADDRESS, bytes((register, value)))

    def require_hardware(self):
        if self.closed:
            raise OSError('OLED is off; create a new Display to initialize again')
        configuration = self.i2c.readfrom_mem(GPIO_ADDRESS, 3, 1)[0]
        output = self.i2c.readfrom_mem(GPIO_ADDRESS, 1, 1)[0]
        if configuration != GPIO_CONFIGURATION or (output & 1) != (0 if self.enabled else 1):
            raise OSError('OLED hardware reset/power state changed; fresh initialization required')

    def require_power(self):
        if self.power_ready is None or not self.power_ready():
            raise OSError('OLED requires a verified 3 A USB-PD contract')
        self.require_hardware()

    def command(self, *values):
        self.i2c.writeto(self.address, b'\x00' + bytes(values))

    def show(self):
        self.require_hardware()
        for page in range(8):
            self.command(0xb0 + page, 0x00, 0x10)
            self.i2c.writeto(self.address, b'\x40' + self.buffer[page*128:(page+1)*128])

    def fill(self, on):
        self.buffer[:] = bytes((0xff if on else 0,)) * 1024

    def off(self):
        self.closed = True
        self.enabled = False
        power_off(self.i2c, self.address, self.delay)