Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | /* * Copyright (c) 2018 qianfan Zhao * Copyright (c) 2018 Nordic Semiconductor ASA * * SPDX-License-Identifier: Apache-2.0 */ #include <zephyr/zephyr.h> #include <zephyr/device.h> #include <zephyr/drivers/gpio.h> #include <zephyr/usb/usb_device.h> #include <zephyr/usb/class/usb_hid.h> #define LOG_LEVEL LOG_LEVEL_DBG LOG_MODULE_REGISTER(main); /* * Devicetree node identifiers for the buttons and LED this sample * supports. */ #define SW0_NODE DT_ALIAS(sw0) #define SW1_NODE DT_ALIAS(sw1) #define SW2_NODE DT_ALIAS(sw2) #define SW3_NODE DT_ALIAS(sw3) #define LED0_NODE DT_ALIAS(led0) /* * Button sw0 and LED led0 are required. */ #if !DT_NODE_EXISTS(SW0_NODE) #error "Unsupported board: sw0 devicetree alias is not defined" #endif #if !DT_NODE_EXISTS(LED0_NODE) #error "Unsupported board: led0 devicetree alias is not defined" #endif /* * Helper macro for initializing a gpio_dt_spec from the devicetree * with fallback values when the nodes are missing. */ #define GPIO_SPEC(node_id) GPIO_DT_SPEC_GET_OR(node_id, gpios, {0}) /* * Create gpio_dt_spec structures from the devicetree. */ static const struct gpio_dt_spec sw0 = GPIO_SPEC(SW0_NODE), sw1 = GPIO_SPEC(SW1_NODE), sw2 = GPIO_SPEC(SW2_NODE), sw3 = GPIO_SPEC(SW3_NODE), led0 = GPIO_SPEC(LED0_NODE); static const uint8_t hid_report_desc[] = HID_MOUSE_REPORT_DESC(2); static uint8_t def_val[4]; static volatile uint8_t status[4]; static K_SEM_DEFINE(sem, 0, 1); /* starts off "not available" */ static struct gpio_callback callback[4]; static enum usb_dc_status_code usb_status; #define MOUSE_BTN_REPORT_POS 0 #define MOUSE_X_REPORT_POS 1 #define MOUSE_Y_REPORT_POS 2 #define MOUSE_BTN_LEFT BIT(0) #define MOUSE_BTN_RIGHT BIT(1) #define MOUSE_BTN_MIDDLE BIT(2) static void status_cb(enum usb_dc_status_code status, const uint8_t *param) { usb_status = status; } static void left_button(const struct device *gpio, struct gpio_callback *cb, uint32_t pins) { int ret; uint8_t state = status[MOUSE_BTN_REPORT_POS]; if (IS_ENABLED(CONFIG_USB_DEVICE_REMOTE_WAKEUP)) { if (usb_status == USB_DC_SUSPEND) { usb_wakeup_request(); return; } } ret = gpio_pin_get(gpio, sw0.pin); if (ret < 0) { LOG_ERR("Failed to get the state of port %s pin %u, error: %d", gpio->name, sw0.pin, ret); return; } if (def_val[0] != (uint8_t)ret) { state |= MOUSE_BTN_LEFT; } else { state &= ~MOUSE_BTN_LEFT; } if (status[MOUSE_BTN_REPORT_POS] != state) { status[MOUSE_BTN_REPORT_POS] = state; k_sem_give(&sem); } } static void right_button(const struct device *gpio, struct gpio_callback *cb, uint32_t pins) { int ret; uint8_t state = status[MOUSE_BTN_REPORT_POS]; if (IS_ENABLED(CONFIG_USB_DEVICE_REMOTE_WAKEUP)) { if (usb_status == USB_DC_SUSPEND) { usb_wakeup_request(); return; } } ret = gpio_pin_get(gpio, sw1.pin); if (ret < 0) { LOG_ERR("Failed to get the state of port %s pin %u, error: %d", gpio->name, sw1.pin, ret); return; } if (def_val[1] != (uint8_t)ret) { state |= MOUSE_BTN_RIGHT; } else { state &= ~MOUSE_BTN_RIGHT; } if (status[MOUSE_BTN_REPORT_POS] != state) { status[MOUSE_BTN_REPORT_POS] = state; k_sem_give(&sem); } } static void x_move(const struct device *gpio, struct gpio_callback *cb, uint32_t pins) { int ret; uint8_t state = status[MOUSE_X_REPORT_POS]; ret = gpio_pin_get(gpio, sw2.pin); if (ret < 0) { LOG_ERR("Failed to get the state of port %s pin %u, error: %d", gpio->name, sw2.pin, ret); return; } if (def_val[2] != (uint8_t)ret) { state += 10U; } if (status[MOUSE_X_REPORT_POS] != state) { status[MOUSE_X_REPORT_POS] = state; k_sem_give(&sem); } } static void y_move(const struct device *gpio, struct gpio_callback *cb, uint32_t pins) { int ret; uint8_t state = status[MOUSE_Y_REPORT_POS]; ret = gpio_pin_get(gpio, sw3.pin); if (ret < 0) { LOG_ERR("Failed to get the state of port %s pin %u, error: %d", gpio->name, sw3.pin, ret); return; } if (def_val[3] != (uint8_t)ret) { state += 10U; } if (status[MOUSE_Y_REPORT_POS] != state) { status[MOUSE_Y_REPORT_POS] = state; k_sem_give(&sem); } } int callbacks_configure(const struct gpio_dt_spec *spec, gpio_callback_handler_t handler, struct gpio_callback *callback, uint8_t *val) { const struct device *gpio = spec->port; gpio_pin_t pin = spec->pin; int ret; if (gpio == NULL) { /* Optional GPIO is missing. */ return 0; } if (!device_is_ready(gpio)) { LOG_ERR("GPIO port %s is not ready", gpio->name); return -ENODEV; } ret = gpio_pin_configure_dt(spec, GPIO_INPUT); if (ret < 0) { LOG_ERR("Failed to configure port %s pin %u, error: %d", gpio->name, pin, ret); return ret; } ret = gpio_pin_get(gpio, pin); if (ret < 0) { LOG_ERR("Failed to get the state of port %s pin %u, error: %d", gpio->name, pin, ret); return ret; } *val = (uint8_t)ret; gpio_init_callback(callback, handler, BIT(pin)); ret = gpio_add_callback(gpio, callback); if (ret < 0) { LOG_ERR("Failed to add the callback for port %s pin %u, " "error: %d", gpio->name, pin, ret); return ret; } ret = gpio_pin_interrupt_configure_dt(spec, GPIO_INT_EDGE_BOTH); if (ret < 0) { LOG_ERR("Failed to configure interrupt for port %s pin %u, " "error: %d", gpio->name, pin, ret); return ret; } return 0; } void main(void) { int ret; uint8_t report[4] = { 0x00 }; const struct device *hid_dev; if (!device_is_ready(led0.port)) { LOG_ERR("LED device %s is not ready", led0.port->name); return; } hid_dev = device_get_binding("HID_0"); if (hid_dev == NULL) { LOG_ERR("Cannot get USB HID Device"); return; } ret = gpio_pin_configure_dt(&led0, GPIO_OUTPUT); if (ret < 0) { LOG_ERR("Failed to configure the LED pin, error: %d", ret); return; } if (callbacks_configure(&sw0, &left_button, &callback[0], &def_val[0])) { LOG_ERR("Failed configuring left button callback."); return; } if (callbacks_configure(&sw1, &right_button, &callback[1], &def_val[1])) { LOG_ERR("Failed configuring right button callback."); return; } if (callbacks_configure(&sw2, &x_move, &callback[2], &def_val[2])) { LOG_ERR("Failed configuring X axis movement callback."); return; } if (callbacks_configure(&sw3, &y_move, &callback[3], &def_val[3])) { LOG_ERR("Failed configuring Y axis movement callback."); return; } usb_hid_register_device(hid_dev, hid_report_desc, sizeof(hid_report_desc), NULL); usb_hid_init(hid_dev); ret = usb_enable(status_cb); if (ret != 0) { LOG_ERR("Failed to enable USB"); return; } while (true) { k_sem_take(&sem, K_FOREVER); report[MOUSE_BTN_REPORT_POS] = status[MOUSE_BTN_REPORT_POS]; report[MOUSE_X_REPORT_POS] = status[MOUSE_X_REPORT_POS]; status[MOUSE_X_REPORT_POS] = 0U; report[MOUSE_Y_REPORT_POS] = status[MOUSE_Y_REPORT_POS]; status[MOUSE_Y_REPORT_POS] = 0U; ret = hid_int_ep_write(hid_dev, report, sizeof(report), NULL); if (ret) { LOG_ERR("HID write error, %d", ret); } /* Toggle LED on sent report */ ret = gpio_pin_toggle(led0.port, led0.pin); if (ret < 0) { LOG_ERR("Failed to toggle the LED pin, error: %d", ret); } } } |