#ifndef PROJECT_DRV8316_H
#define PROJECT_DRV8316_H
#include <stdbool.h>
#include <stdint.h>
/* HAL: MSB first, CPOL=0 CPHA=1, initially 500 kHz. Exactly 16 clocks
 * under CS low; CS high >=1 us between frames. Separate MCU 3V3 required.
 * Call from serialized foreground only. No ISR or concurrent SPI access. */
typedef struct {
    void *ctx;
    bool (*transfer16)(void *, uint16_t tx, uint16_t *rx);
    void (*set_off)(void *, bool off); /* true releases open-drain DRVOFF */
    void (*set_awake)(void *, bool awake); /* false pulls nSLEEP low */
    void (*delay_us)(void *, uint32_t us);
} drv8316_hal;
typedef enum {
    DRV_OK = 0, DRV_INVALID, DRV_IO, DRV_DEVICE_FAULT, DRV_READBACK
} drv8316_result;
typedef struct {
    uint8_t status[3];
    uint8_t last_header;
    uint8_t last_address;
    bool configured; /* Configuration verified, NOT permission to run motor. */
} drv8316_report;
bool drv8316_frame(bool read, uint8_t address, uint8_t data, uint16_t *frame);
/* Explicit startup/reset operation. Keeps power stage OFF on every path.
 * No motor enable function: current limits, speed/position sensing, hardware
 * break and bus-energy management must be integrated before operation. */
drv8316_result drv8316_configure_off(const drv8316_hal *, drv8316_report *);
/* Any error asserts DRVOFF and sleeps the device; reconfiguration required. */
drv8316_result drv8316_poll_faults(const drv8316_hal *, drv8316_report *);
#endif
