Skip to content

Commit 0274418

Browse files
committed
Update examples
1 parent 2d4f1df commit 0274418

5 files changed

Lines changed: 95 additions & 47 deletions

File tree

examples/gecko/gcn-host/libjoybus_example.slcp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ quality: production
55
component:
66
- id: emlib
77
- id: dmadrv
8+
- id: sleeptimer
89
- id: udelay

examples/gecko/gcn-host/main.c

Lines changed: 75 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,76 @@
22
#include "em_cmu.h"
33
#include "em_gpio.h"
44

5+
#include "sl_sleeptimer.h"
56
#include "sl_udelay.h"
67

78
#include <joybus/joybus.h>
89
#include <joybus/backend/gecko.h>
910

10-
#define JOYBUS_DATA_PORT gpioPortD
11-
#define JOYBUS_DATA_PIN 3
12-
#define JOYBUS_TIMER TIMER0
13-
#define JOYBUS_USART USART0
11+
#define JOYBUS_PORT gpioPortD
12+
#define JOYBUS_PIN 3
13+
#define JOYBUS_TIMER TIMER0
14+
#define JOYBUS_USART USART0
1415

15-
#define LED_PORT gpioPortA
16-
#define LED_PIN 4
16+
#define LED_PORT gpioPortA
17+
#define LED_PIN 4
1718

19+
#define JOYBUS_POLL_INTERVAL_MS 15
20+
21+
// Joybus instance
1822
struct joybus_gecko gecko_bus;
1923
struct joybus *bus = JOYBUS(&gecko_bus);
2024

21-
static uint8_t response[JOYBUS_BLOCK_SIZE] = {0};
25+
// Buffer for Joybus responses
26+
static uint8_t joybus_response[JOYBUS_BLOCK_SIZE] = {0};
2227

23-
static uint16_t last_buttons = 0;
24-
static uint16_t down_buttons = 0;
25-
static uint16_t up_buttons = 0;
28+
// Polling mode
29+
enum { POLL_MODE_IDENTIFY, POLL_MODE_READ };
30+
static uint8_t poll_mode = POLL_MODE_IDENTIFY;
2631

27-
static void identify_cb(struct joybus *bus, int result, void *user_data)
32+
void joybus_identify_cb(struct joybus *bus, int result, void *user_data)
2833
{
34+
// Stay in identify mode on any Joybus error
35+
if (result < 0)
36+
return;
37+
38+
// Check it's a GameCube controller
39+
uint16_t type = joybus_id_get_type(joybus_response);
40+
if (!(type & JOYBUS_ID_GCN_DEVICE))
41+
return;
42+
43+
// Check we've received data if it's a wireless controller
44+
if ((type & JOYBUS_ID_GCN_WIRELESS) && !(type & JOYBUS_ID_GCN_WIRELESS_RECEIVED))
45+
return;
46+
47+
// Check it's a standard controller
48+
if (!(type & JOYBUS_ID_GCN_STANDARD))
49+
return;
50+
51+
// Move to polling for input
52+
poll_mode = POLL_MODE_READ;
2953
}
3054

31-
static void read_cb(struct joybus *bus, int result, void *user_data)
55+
void joybus_read_cb(struct joybus *bus, int result, void *user_data)
3256
{
33-
uint16_t buttons = ((response[1] << 8) | response[0]) & JOYBUS_GCN_BUTTON_MASK;
57+
// Switch back to identify mode on any Joybus error
58+
if (result < 0) {
59+
poll_mode = POLL_MODE_IDENTIFY;
60+
return;
61+
}
3462

35-
down_buttons = (buttons ^ last_buttons) & buttons;
36-
up_buttons = (buttons ^ last_buttons) & last_buttons;
63+
// Extract button states
64+
uint16_t buttons = ((joybus_response[1] << 8) | joybus_response[0]) & JOYBUS_GCN_BUTTON_MASK;
3765

38-
last_buttons = buttons;
66+
// Light the LED while the A button is held
67+
if (buttons & JOYBUS_GCN_BUTTON_A) {
68+
GPIO_PinOutSet(LED_PORT, LED_PIN);
69+
} else {
70+
GPIO_PinOutClear(LED_PORT, LED_PIN);
71+
}
3972
}
4073

74+
// Initialize system clocks
4175
static void clock_init()
4276
{
4377
// HFXO initialization
@@ -57,6 +91,20 @@ static void clock_init()
5791
CMU_ClockSelectSet(cmuClock_EM01GRPACLK, cmuSelect_HFXO);
5892
}
5993

94+
// Poll Joybus for data
95+
static void poll_task(sl_sleeptimer_timer_handle_t *handle, void *data)
96+
{
97+
switch (poll_mode) {
98+
case POLL_MODE_IDENTIFY:
99+
joybus_identify(bus, joybus_response, joybus_identify_cb, NULL);
100+
break;
101+
102+
case POLL_MODE_READ:
103+
joybus_gcn_read(bus, JOYBUS_GCN_ANALOG_MODE_3, JOYBUS_GCN_MOTOR_STOP, joybus_response, joybus_read_cb, NULL);
104+
break;
105+
}
106+
}
107+
60108
int main()
61109
{
62110
// Initialize chip and clocks
@@ -67,27 +115,19 @@ int main()
67115
CMU_ClockEnable(cmuClock_GPIO, true);
68116
GPIO_PinModeSet(LED_PORT, LED_PIN, gpioModePushPull, 0);
69117

70-
// Initialize and enable the Joybus
71-
joybus_gecko_init(&gecko_bus, JOYBUS_DATA_PORT, JOYBUS_DATA_PIN, JOYBUS_TIMER, JOYBUS_USART);
118+
// Initialize Joybus
119+
joybus_gecko_init(&gecko_bus, JOYBUS_PORT, JOYBUS_PIN, JOYBUS_TIMER, JOYBUS_USART);
72120
joybus_enable(bus);
73-
sl_udelay_wait(100000);
74-
75-
// Ask any connected target for their ID
76-
joybus_identify(bus, response, identify_cb, NULL);
77-
sl_udelay_wait(100000);
78121

79-
while (1) {
80-
// Read the controller state
81-
joybus_gcn_read(bus, JOYBUS_GCN_ANALOG_MODE_3, JOYBUS_GCN_MOTOR_STOP, response, read_cb, NULL);
122+
// Poll for Joybus data at regular intervals
123+
sl_sleeptimer_timer_handle_t poll_timer;
124+
sl_sleeptimer_init();
125+
sl_sleeptimer_start_periodic_timer(&poll_timer, sl_sleeptimer_ms_to_tick(JOYBUS_POLL_INTERVAL_MS), poll_task, bus, 0,
126+
SL_SLEEPTIMER_NO_HIGH_PRECISION_HF_CLOCKS_REQUIRED_FLAG);
82127

83-
// Toggle LED on PA4 based on A button state
84-
if (last_buttons & JOYBUS_GCN_BUTTON_A) {
85-
GPIO_PinOutSet(LED_PORT, LED_PIN);
86-
} else {
87-
GPIO_PinOutClear(LED_PORT, LED_PIN);
88-
}
128+
// No work to do in main loop
129+
while (true)
130+
;
89131

90-
// Wait a bit before reading again
91-
sl_udelay_wait(10000);
92-
}
132+
return 0;
93133
}

examples/gecko/gcn-target/main.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@
1515
#define BTN_PORT gpioPortC
1616
#define BTN_PIN 7
1717

18+
// Joybus instance
1819
struct joybus_gecko gecko_bus;
1920
struct joybus *bus = JOYBUS(&gecko_bus);
2021

22+
// GameCube controller target instance
2123
static struct joybus_gc_controller gc_controller;
2224

23-
static void clock_init()
25+
// Initialize system
26+
static void system_init()
2427
{
28+
// Chip errata
29+
CHIP_Init();
30+
2531
// HFXO initialization
2632
CMU_HFXOInit_TypeDef hfxoInit = CMU_HFXOINIT_DEFAULT;
2733
hfxoInit.ctuneXoAna = 121;
@@ -39,15 +45,18 @@ static void clock_init()
3945
CMU_ClockSelectSet(cmuClock_EM01GRPACLK, cmuSelect_HFXO);
4046
}
4147

42-
int main()
48+
// Initialize GPIO for button
49+
static void gpio_init()
4350
{
44-
// Initialize chip and clocks
45-
CHIP_Init();
46-
clock_init();
47-
48-
// Set up GPIO for button
4951
CMU_ClockEnable(cmuClock_GPIO, true);
5052
GPIO_PinModeSet(BTN_PORT, BTN_PIN, gpioModeInput, 1);
53+
}
54+
55+
int main()
56+
{
57+
// Initialize system
58+
system_init();
59+
gpio_init();
5160

5261
// Initialize the Joybus
5362
joybus_gecko_init(&gecko_bus, JOYBUS_DATA_PORT, JOYBUS_DATA_PIN, JOYBUS_TIMER, JOYBUS_USART);
@@ -59,9 +68,6 @@ int main()
5968
// Register the target on the bus
6069
joybus_target_register(bus, JOYBUS_TARGET(&gc_controller));
6170

62-
// Wait a bit before starting
63-
sl_udelay_wait(100000);
64-
6571
while (1) {
6672
// Clear previous button state
6773
gc_controller.input.buttons &= ~JOYBUS_GCN_BUTTON_MASK;

examples/pico-sdk/gcn-host/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ int main()
9292
joybus_rp2xxx_init(&rp2xxx_bus, JOYBUS_GPIO, pio0);
9393
joybus_enable(bus);
9494

95-
// Poll for Joybus data and send HID reports at regular intervals
95+
// Poll for Joybus data at regular intervals
9696
struct repeating_timer poll_timer;
9797
add_repeating_timer_ms(JOYBUS_POLL_INTERVAL_MS, poll_task, NULL, &poll_timer);
9898

src/backend/rp2xxx/joybus.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ static inline void target_byte_received(struct joybus *bus)
225225

226226
data->state = BUS_STATE_TARGET_TX;
227227
} else if (rc > 0) {
228-
// More bytes expected, set a timeout for the next byte
228+
// More bytes expected
229+
// Set a timeout for the next byte
229230
data->rx_timeout_alarm = add_alarm_in_us(JOYBUS_REPLY_TIMEOUT_US, target_rx_timeout, bus, true);
230231
} else {
231232
// Error handling command, or command not supported, switch back to idle/read mode

0 commit comments

Comments
 (0)