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/mcf8329.py

"""Read-only MCF8329A profile verification, TI SLLSFQ7 sections 7.6.2/7.7.

Use SoftI2C primitives: TI requires 100 us BETWEEN bytes. A normal bulk
readfrom_mem transaction cannot provide this. No EEPROM or tuning writes.
"""

# TI SLLSFQ7 section 7.7: algorithm, fault, internal-algorithm and hardware
# registers together occupy all 24 even addresses. Their FIELDS differ from MCT.
REGISTER_NAMES = (
    "ISD_CONFIG", "REV_DRIVE_CONFIG", "MOTOR_STARTUP1", "MOTOR_STARTUP2",
    "CLOSED_LOOP1", "CLOSED_LOOP2", "CLOSED_LOOP3", "CLOSED_LOOP4",
    "FAULT_CONFIG1", "FAULT_CONFIG2", "REF_PROFILES1", "REF_PROFILES2",
    "REF_PROFILES3", "REF_PROFILES4", "REF_PROFILES5", "REF_PROFILES6",
    "INT_ALGO_1", "INT_ALGO_2", "PIN_CONFIG", "DEVICE_CONFIG1",
    "DEVICE_CONFIG2", "PERI_CONFIG1", "GD_CONFIG1", "GD_CONFIG2",
)
CONFIG_REGISTERS = tuple(range(0x80, 0xB0, 2))
PWM_HZ = 20000  # SPEED command carrier, not the motor bridge switching rate.


class Mcf8329:
    def __init__(self, bus, clock, address=0x60):
        if not 0x08 <= address <= 0x77:
            raise ValueError("Invalid MCF I2C address")
        self.bus, self.clock, self.address = bus, clock, address

    def _byte(self, value):
        ack = self.bus.write(bytes((value,)))
        self.clock.sleep_us(100)
        if ack != 1:
            raise OSError("MCF I2C NACK")

    def read_register(self, register):
        if register not in CONFIG_REGISTERS:
            raise ValueError("Only MCF configuration reads are supported")
        # Read=bit23, CRC off, 32-bit DLEN=01, section/page=0.
        control = (0x900000 | register).to_bytes(3, "big")
        data = bytearray(4)
        one = bytearray(1)
        self.bus.start()
        try:
            self._byte(self.address << 1)
            for byte in control:
                self._byte(byte)
            # The target can NACK the read address while preparing its data.
            # Retry that address only, bounded, as TI specifies in 7.6.2.3.
            for attempt in range(3):
                self.bus.start()
                ack = self.bus.write(bytes(((self.address << 1) | 1,)))
                self.clock.sleep_us(100)
                if ack == 1:
                    break
            else:
                raise OSError("MCF read-ready timeout")
            for i in range(4):
                self.bus.readinto(one, i == 3)
                data[i] = one[0]
                self.clock.sleep_us(100)
            return int.from_bytes(data, "little")
        finally:
            self.bus.stop()


class ProfileCheck:
    def __init__(self, reader, registers):
        self.reader, self.registers = reader, registers
        self.index = 0

    def _check(self, address):
        if self.reader.read_register(address) != self.registers[address]:
            raise OSError("MCF profile mismatch at 0x%02X" % address)

    def verify_all(self):
        for address in CONFIG_REGISTERS:
            self._check(address)
        self.index = 0

    def verify_next(self):
        self._check(CONFIG_REGISTERS[self.index])
        self.index = (self.index + 1) % len(CONFIG_REGISTERS)