ShiboSoftwareDev/am69-pi5

A 85×56 mm 10-layer AM69A single-board computer carrier integrates 8 GiB LPDDR4, 32 GB eMMC, Pi-style expansion and multimedia/USB/Ethernet connectors, multi-rail PMIC power, STM32 boot/power control, USB-C PD, SD/fan protection, and HDI-controlled high-speed routing.

Version
0.0.8-engineering-draft-20261107-dqs19
License
unset
Stars
0

Use with the tscircuit CLI

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

tsci clone ShiboSoftwareDev/am69-pi5  # download the package files into ./am69-pi5
tsci add ShiboSoftwareDev/am69-pi5    # add it to your project as @tsci/ShiboSoftwareDev.am69-pi5

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/boot-controller/ec-client.py

#!/usr/bin/env python3
"""AM69 EC UART client. The BSP must first assign MCU_UART0 to this device."""
import argparse
import binascii
import os
import select
import struct
import termios
import time


def exchange(fd, command, value=0):
    sequence = int(time.monotonic_ns()) & 255
    payload = struct.pack('<BBH', command, sequence, value)
    frame = b'\xa5\x5a' + payload + struct.pack('<H', binascii.crc_hqx(payload, 0xffff))
    termios.tcflush(fd, termios.TCIFLUSH)
    os.write(fd, frame)
    received = bytearray()
    deadline = time.monotonic() + 1
    while time.monotonic() < deadline:
        if not select.select([fd], [], [], max(0, deadline-time.monotonic()))[0]:
            break
        received.extend(os.read(fd, 64))
        while len(received) >= 8:
            if received[:2] != b'\xa5\x5a':
                del received[0]
                continue
            response = bytes(received[:8])
            del received[:8]
            crc, = struct.unpack('<H', response[6:])
            if crc != binascii.crc_hqx(response[2:6], 0xffff) or response[3] != sequence:
                continue
            if response[2] != command | 0x80:
                raise RuntimeError('EC rejected command')
            return struct.unpack('<H', response[4:6])[0]
    raise TimeoutError('No valid EC response')


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('device', help='BSP-assigned MCU_UART0 device, e.g. /dev/ttySx')
    parser.add_argument('command', choices=['status', 'rpm', 'fan'])
    parser.add_argument('duty', type=int, nargs='?')
    parser.add_argument('--keepalive', action='store_true', help='Repeat fan command every two seconds')
    args = parser.parse_args()
    if args.command == 'fan' and (args.duty is None or not 0 <= args.duty <= 100):
        parser.error('fan requires duty from 0 to 100')
    if args.keepalive and args.command != 'fan':
        parser.error('--keepalive requires fan')
    fd = os.open(args.device, os.O_RDWR | os.O_NOCTTY)
    saved = termios.tcgetattr(fd)
    raw = termios.tcgetattr(fd)
    raw[0] = raw[1] = raw[3] = 0
    raw[2] = termios.CS8 | termios.CREAD | termios.CLOCAL
    raw[4] = raw[5] = termios.B115200
    raw[6][termios.VMIN] = 0
    raw[6][termios.VTIME] = 0
    termios.tcsetattr(fd, termios.TCSANOW, raw)
    try:
        while True:
            answer = exchange(fd, {'fan': 1, 'status': 2, 'rpm': 5}[args.command], args.duty or 0)
            print(answer, flush=True)
            if not args.keepalive:
                break
            time.sleep(2)
    finally:
        # A missing client heartbeat also restores full speed after five seconds.
        try:
            if args.keepalive:
                exchange(fd, 1, 100)
        finally:
            termios.tcsetattr(fd, termios.TCSANOW, saved)
            os.close(fd)


if __name__ == '__main__':
    main()