1515#include <zephyr/input/input.h>
1616#include <zephyr/sys/util.h>
1717
18- #include <zephyr/usb/usb_device.h>
1918#include <zephyr/usb/usbd.h>
20- #include <zephyr/usb/class/usb_hid .h>
19+ #include <zephyr/usb/class/usbd_hid .h>
2120
2221#include <zephyr/logging/log.h>
2322LOG_MODULE_REGISTER (main , LOG_LEVEL_INF );
2423
2524static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET (DT_ALIAS (led0 ), gpios );
2625static const uint8_t hid_report_desc [] = HID_MOUSE_REPORT_DESC (2 );
27- static enum usb_dc_status_code usb_status ;
2826
2927#define MOUSE_BTN_LEFT 0
3028#define MOUSE_BTN_RIGHT 1
@@ -38,22 +36,7 @@ enum mouse_report_idx {
3836};
3937
4038K_MSGQ_DEFINE (mouse_msgq , MOUSE_REPORT_COUNT , 2 , 1 );
41- static K_SEM_DEFINE (ep_write_sem , 0 , 1 ) ;
42-
43- static inline void status_cb (enum usb_dc_status_code status , const uint8_t * param )
44- {
45- usb_status = status ;
46- }
47-
48- static ALWAYS_INLINE void rwup_if_suspended (void )
49- {
50- if (IS_ENABLED (CONFIG_USB_DEVICE_REMOTE_WAKEUP )) {
51- if (usb_status == USB_DC_SUSPEND ) {
52- usb_wakeup_request ();
53- return ;
54- }
55- }
56- }
39+ static bool mouse_ready ;
5740
5841static void input_cb (struct input_event * evt , void * user_data )
5942{
@@ -63,11 +46,9 @@ static void input_cb(struct input_event *evt, void *user_data)
6346
6447 switch (evt -> code ) {
6548 case INPUT_KEY_0 :
66- rwup_if_suspended ();
6749 WRITE_BIT (tmp [MOUSE_BTN_REPORT_IDX ], MOUSE_BTN_LEFT , evt -> value );
6850 break ;
6951 case INPUT_KEY_1 :
70- rwup_if_suspended ();
7152 WRITE_BIT (tmp [MOUSE_BTN_REPORT_IDX ], MOUSE_BTN_RIGHT , evt -> value );
7253 break ;
7354 case INPUT_KEY_2 :
@@ -99,42 +80,30 @@ static void input_cb(struct input_event *evt, void *user_data)
9980
10081INPUT_CALLBACK_DEFINE (NULL , input_cb , NULL );
10182
102- #if defined(CONFIG_USB_DEVICE_STACK_NEXT )
103- static int enable_usb_device_next (void )
83+ static void mouse_iface_ready (const struct device * dev , const bool ready )
10484{
105- struct usbd_context * sample_usbd ;
106- int err ;
107-
108- sample_usbd = sample_usbd_init_device (NULL );
109- if (sample_usbd == NULL ) {
110- LOG_ERR ("Failed to initialize USB device" );
111- return - ENODEV ;
112- }
113-
114- err = usbd_enable (sample_usbd );
115- if (err ) {
116- LOG_ERR ("Failed to enable device support" );
117- return err ;
118- }
119-
120- LOG_DBG ("USB device support enabled" );
121-
122- return 0 ;
85+ LOG_INF ("HID device %s interface is %s" ,
86+ dev -> name , ready ? "ready" : "not ready" );
87+ mouse_ready = ready ;
12388}
124- #endif /* defined(CONFIG_USB_DEVICE_STACK_NEXT) */
12589
126- static void int_in_ready_cb (const struct device * dev )
90+ static int mouse_get_report (const struct device * dev ,
91+ const uint8_t type , const uint8_t id , const uint16_t len ,
92+ uint8_t * const buf )
12793{
128- ARG_UNUSED (dev );
129- k_sem_give (& ep_write_sem );
94+ LOG_WRN ("Get Report not implemented, Type %u ID %u" , type , id );
95+
96+ return 0 ;
13097}
13198
132- static const struct hid_ops ops = {
133- .int_in_ready = int_in_ready_cb ,
99+ struct hid_device_ops mouse_ops = {
100+ .iface_ready = mouse_iface_ready ,
101+ .get_report = mouse_get_report ,
134102};
135103
136104int main (void )
137105{
106+ struct usbd_context * sample_usbd ;
138107 const struct device * hid_dev ;
139108 int ret ;
140109
@@ -143,51 +112,58 @@ int main(void)
143112 return 0 ;
144113 }
145114
146- #if defined(CONFIG_USB_DEVICE_STACK_NEXT )
147- hid_dev = DEVICE_DT_GET_ONE (zephyr_hid_device );
148- #else
149- hid_dev = device_get_binding ("HID_0" );
150- #endif
151- if (hid_dev == NULL ) {
152- LOG_ERR ("Cannot get USB HID Device" );
153- return 0 ;
154- }
155-
156115 ret = gpio_pin_configure_dt (& led0 , GPIO_OUTPUT );
157- if (ret < 0 ) {
116+ if (ret != 0 ) {
158117 LOG_ERR ("Failed to configure the LED pin, error: %d" , ret );
159118 return 0 ;
160119 }
161120
162- usb_hid_register_device (hid_dev ,
163- hid_report_desc , sizeof (hid_report_desc ),
164- & ops );
121+ hid_dev = DEVICE_DT_GET_ONE (zephyr_hid_device );
122+ if (!device_is_ready (hid_dev )) {
123+ LOG_ERR ("HID Device is not ready" );
124+ return - EIO ;
125+ }
165126
166- usb_hid_init (hid_dev );
127+ ret = hid_device_register (hid_dev ,
128+ hid_report_desc , sizeof (hid_report_desc ),
129+ & mouse_ops );
130+ if (ret != 0 ) {
131+ LOG_ERR ("Failed to register HID Device, %d" , ret );
132+ return ret ;
133+ }
167134
168- #if defined(CONFIG_USB_DEVICE_STACK_NEXT )
169- ret = enable_usb_device_next ();
170- #else
171- ret = usb_enable (status_cb );
172- #endif
135+ sample_usbd = sample_usbd_init_device (NULL );
136+ if (sample_usbd == NULL ) {
137+ LOG_ERR ("Failed to initialize USB device" );
138+ return - ENODEV ;
139+ }
140+
141+ ret = usbd_enable (sample_usbd );
173142 if (ret != 0 ) {
174- LOG_ERR ("Failed to enable USB " );
175- return 0 ;
143+ LOG_ERR ("Failed to enable device support " );
144+ return ret ;
176145 }
177146
147+ LOG_DBG ("USB device support enabled" );
148+
178149 while (true) {
179150 UDC_STATIC_BUF_DEFINE (report , MOUSE_REPORT_COUNT );
180151
181152 k_msgq_get (& mouse_msgq , & report , K_FOREVER );
182153
183- ret = hid_int_ep_write (hid_dev , report , MOUSE_REPORT_COUNT , NULL );
154+ if (!mouse_ready ) {
155+ LOG_INF ("USB HID device is not ready" );
156+ continue ;
157+ }
158+
159+ ret = hid_device_submit_report (hid_dev , MOUSE_REPORT_COUNT , report );
184160 if (ret ) {
185- LOG_ERR ("HID write error, %d" , ret );
161+ LOG_ERR ("HID submit report error, %d" , ret );
186162 } else {
187- k_sem_take (& ep_write_sem , K_FOREVER );
188163 /* Toggle LED on sent report */
189164 (void )gpio_pin_toggle (led0 .port , led0 .pin );
190165 }
191166 }
167+
192168 return 0 ;
193169}
0 commit comments