libjoybus
Joybus implementation for 32-bit MCUs
Loading...
Searching...
No Matches
bus.h
1
5
6#pragma once
7
8#include <stdint.h>
9
10#include <joybus/target.h>
11
12struct joybus;
13
15#define JOYBUS_FREQ_GCC 250000
16
18#define JOYBUS_FREQ_WAVEBIRD 225000
19
21#define JOYBUS_FREQ_CONSOLE 200000
22
24#define JOYBUS_BLOCK_SIZE 64
25
27#define JOYBUS_INTER_TRANSFER_DELAY_US 20
28
30#define JOYBUS_REPLY_TIMEOUT_US 100
31
35#define JOYBUS(bus) ((struct joybus *)(bus))
36
44typedef void (*joybus_transfer_cb_t)(struct joybus *bus, int result, void *user_data);
45
46// API for a Joybus backend - internal use only
47struct joybus_api {
48 int (*enable)(struct joybus *bus);
49 int (*disable)(struct joybus *bus);
50 int (*transfer)(struct joybus *bus, const uint8_t *write_buf, uint8_t write_len, uint8_t *read_buf, uint8_t read_len,
51 joybus_transfer_cb_t callback, void *user_data);
52 int (*target_register)(struct joybus *bus, struct joybus_target *target);
53 int (*target_unregister)(struct joybus *bus, struct joybus_target *target);
54};
55
59struct joybus {
60 const struct joybus_api *api;
61
62 struct joybus_target *target;
63 uint8_t command_buffer[JOYBUS_BLOCK_SIZE];
64};
65
71static inline int joybus_enable(struct joybus *bus)
72{
73 return bus->api->enable(bus);
74}
75
81static inline int joybus_disable(struct joybus *bus)
82{
83 return bus->api->disable(bus);
84}
85
102static inline int joybus_transfer(struct joybus *bus, const uint8_t *write_buf, uint8_t write_len, uint8_t *read_buf,
103 uint8_t read_len, joybus_transfer_cb_t callback, void *user_data)
104{
105 return bus->api->transfer(bus, write_buf, write_len, read_buf, read_len, callback, user_data);
106}
107
115static inline int joybus_target_register(struct joybus *bus, struct joybus_target *target)
116{
117 return bus->api->target_register(bus, target);
118}
119
127static inline int joybus_target_unregister(struct joybus *bus, struct joybus_target *target)
128{
129 return bus->api->target_unregister(bus, target);
130}
131
static int joybus_enable(struct joybus *bus)
Enable the Joybus instance.
Definition bus.h:71
static int joybus_transfer(struct joybus *bus, const uint8_t *write_buf, uint8_t write_len, uint8_t *read_buf, uint8_t read_len, joybus_transfer_cb_t callback, void *user_data)
Perform a Joybus "write then read" transfer.
Definition bus.h:102
#define JOYBUS_BLOCK_SIZE
Maximum size of a Joybus transfer, in bytes.
Definition bus.h:24
void(* joybus_transfer_cb_t)(struct joybus *bus, int result, void *user_data)
Function type for transfer completion callbacks.
Definition bus.h:44
static int joybus_target_unregister(struct joybus *bus, struct joybus_target *target)
Unregister a Joybus target.
Definition bus.h:127
static int joybus_disable(struct joybus *bus)
Disable the Joybus instance.
Definition bus.h:81
static int joybus_target_register(struct joybus *bus, struct joybus_target *target)
Enable Joybus "target" mode, and register a target to handle commands.
Definition bus.h:115
Definition bus.h:47
A Joybus target, a device on the Joybus that can respond to commands.
Definition target.h:45
A Joybus instance.
Definition bus.h:59