libjoybus
Joybus implementation for 32-bit MCUs
Loading...
Searching...
No Matches
bus.h
1
6
7#pragma once
8
9#include <stddef.h>
10#include <stdint.h>
11
12#include <joybus/target.h>
13
14struct joybus;
15
17#define JOYBUS_FREQ_NOMINAL 250000
18
20#define JOYBUS_FREQ_N64_CONSOLE 244141 // PIF-NUS @ 15.625MHz / 64
21
23#define JOYBUS_FREQ_N64_EXTJOY JOYBUS_FREQ_N64_CONSOLE // SECCLK @ 1.953125MHz / 8
24
26#define JOYBUS_FREQ_N64_CONTROLLER 250000 // CNT-NUS @ 2MHz / 8
27
29#define JOYBUS_FREQ_N64_VRU 250000 // VCI-NUS @ 4MHz / 16
30
32#define JOYBUS_FREQ_GCN_CONSOLE 202500 // Flipper @ 162MHz / 800
33
35#define JOYBUS_FREQ_GCN_CONTROLLER 250000 // CNT-DOL @ 4MHz / 16
36
38#define JOYBUS_FREQ_WAVEBIRD_RECEIVER 225000 // WCRX-DOL @ 28.8MHz / 128
39
41#define JOYBUS_FREQ_WII_CONSOLE 202500 // Hollywood @ 243MHz / 1200
42
44#define JOYBUS_FREQ_GCN_GBA_CABLE 262144 // CPU-AGB @ 16.777216MHz / 64
45
47#define JOYBUS_INTER_TRANSFER_DELAY_US 80
48
50#define JOYBUS_REPLY_TIMEOUT_US 64
51
53#define JOYBUS_BUS_IDLE_US 100
54
56#define JOYBUS_BLOCK_SIZE 64
57
59#define JOYBUS_PAK_BLOCK_SIZE 32
60
64#define JOYBUS(bus) ((struct joybus *)(bus))
65
76
88typedef void (*joybus_transfer_cb)(struct joybus *bus, int status, void *user_data);
89
90// API for a Joybus backend - internal use only
91struct joybus_api {
92 int (*enable)(struct joybus *bus);
93 int (*disable)(struct joybus *bus);
94 int (*transfer)(struct joybus *bus, const uint8_t *write_buf, uint8_t write_len, uint8_t *read_buf, uint8_t read_len,
95 joybus_transfer_cb callback, void *user_data);
96};
97
99 joybus_transfer_cb callback;
100 void *user_data;
101 uint8_t *response;
102 uint8_t arg;
103};
104
108struct joybus {
110 const struct joybus_api *api;
111
114
116 uint32_t freq;
117
120
121 uint8_t command_buffer[JOYBUS_BLOCK_SIZE];
122 uint8_t response_buffer[JOYBUS_BLOCK_SIZE];
123 struct joybus_host_op host_op;
124};
125
136static inline int joybus_enable(struct joybus *bus, enum joybus_mode mode)
137{
138 bus->mode = mode;
139 return bus->api->enable(bus);
140}
141
147static inline int joybus_disable(struct joybus *bus)
148{
149 return bus->api->disable(bus);
150}
151
168static inline int joybus_transfer(struct joybus *bus, const uint8_t *write_buf, uint8_t write_len, uint8_t *read_buf,
169 uint8_t read_len, joybus_transfer_cb callback, void *user_data)
170{
171 return bus->api->transfer(bus, write_buf, write_len, read_buf, read_len, callback, user_data);
172}
173
189int joybus_transfer_sync(struct joybus *bus, const uint8_t *write_buf, uint8_t write_len, uint8_t *read_buf,
190 uint8_t read_len);
191
199static inline int joybus_attach_target(struct joybus *bus, struct joybus_target *target)
200{
201 bus->target = target;
202 target->attached = true;
203
204 return 0;
205}
206
214static inline int joybus_detach_target(struct joybus *bus, struct joybus_target *target)
215{
216 bus->target = NULL;
217 target->attached = false;
218
219 return 0;
220}
221
222// Context for a blocking Joybus operation
224 volatile bool done;
225 volatile int status;
226};
227
228// Transfer completion callback that records the status into a joybus_sync_ctx.
229void joybus_sync_cb(struct joybus *bus, int status, void *user_data);
230
231// Busy-wait on a joybus_sync_ctx until the operation completes
232int joybus_sync(int start_status, struct joybus_sync_ctx *ctx);
233
joybus_mode
The role a Joybus instance plays on the bus.
Definition bus.h:69
void(* joybus_transfer_cb)(struct joybus *bus, int status, void *user_data)
Function type for transfer completion callbacks.
Definition bus.h:88
static int joybus_detach_target(struct joybus *bus, struct joybus_target *target)
Detach a target from the bus.
Definition bus.h:214
static int joybus_enable(struct joybus *bus, enum joybus_mode mode)
Enable the Joybus instance in the given mode.
Definition bus.h:136
#define JOYBUS_BLOCK_SIZE
Maximum size of a Joybus transfer, in bytes.
Definition bus.h:56
int joybus_transfer_sync(struct joybus *bus, const uint8_t *write_buf, uint8_t write_len, uint8_t *read_buf, uint8_t read_len)
Perform a synchronous "write then read" Joybus transfer.
Definition bus.c:24
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 callback, void *user_data)
Perform a Joybus "write then read" transfer.
Definition bus.h:168
static int joybus_disable(struct joybus *bus)
Disable the Joybus instance.
Definition bus.h:147
static int joybus_attach_target(struct joybus *bus, struct joybus_target *target)
Attach a target to handle commands received in target mode.
Definition bus.h:199
@ JOYBUS_MODE_HOST
Host mode, sends commands and reads responses from a target device.
Definition bus.h:71
@ JOYBUS_MODE_TARGET
Target mode, listens for commands from a host device and responds accordingly.
Definition bus.h:74
uint8_t status
Status flags.
Definition identify.h:4
Definition bus.h:91
Definition bus.h:98
Definition bus.h:223
Interface for a Joybus target, a device on the Joybus which responds to commands from a host.
Definition target.h:72
bool attached
Whether the target is currently attached to a bus.
Definition target.h:77
A Joybus instance.
Definition bus.h:108
struct joybus_target * target
The target device attached to this Joybus instance, if any.
Definition bus.h:119
enum joybus_mode mode
The operating mode of this Joybus instance (host or target).
Definition bus.h:113
uint32_t freq
The frequency of the bus, in Hz.
Definition bus.h:116
const struct joybus_api * api
The backend API implementation for this Joybus instance.
Definition bus.h:110