|
| 1 | +extern "C" |
| 2 | +{ |
| 3 | +#include "board.h" |
| 4 | +#include "fsl_gpio.h" |
| 5 | +#include "pin_mux.h" |
| 6 | + |
| 7 | +#if defined(FSL_FEATURE_SOC_PORT_COUNT) && (FSL_FEATURE_SOC_PORT_COUNT) |
| 8 | +#include "fsl_port.h" |
| 9 | +#endif |
| 10 | +} |
| 11 | +#include "high_resolution_mouse.hpp" |
| 12 | +#include "port/nxp/mcux_mac.hpp" |
| 13 | +#include "usb/df/class/hid.hpp" |
| 14 | +#include "usb/df/device.hpp" |
| 15 | + |
| 16 | +auto& mac() |
| 17 | +{ |
| 18 | + static std::array<uint8_t, 128> buffer; |
| 19 | + static auto mac{usb::df::nxp::mcux_mac::khci()}; |
| 20 | + mac.set_control_buffer(buffer); |
| 21 | + return mac; |
| 22 | +} |
| 23 | + |
| 24 | +extern "C" void USB0_IRQHandler(void) |
| 25 | +{ |
| 26 | + mac().handle_irq(); |
| 27 | + SDK_ISR_EXIT_BARRIER; |
| 28 | +} |
| 29 | + |
| 30 | +extern "C" void BOARD_InitBootPins(void); |
| 31 | +extern "C" void BOARD_InitHardware(void) |
| 32 | +{ |
| 33 | + BOARD_InitBootClocks(); |
| 34 | + BOARD_InitBootPins(); |
| 35 | + BOARD_InitLEDsPins(); |
| 36 | + BOARD_InitBUTTONsPins(); |
| 37 | +#if (defined(FSL_FEATURE_PORT_HAS_NO_INTERRUPT) && FSL_FEATURE_PORT_HAS_NO_INTERRUPT) || \ |
| 38 | + (!defined(FSL_FEATURE_SOC_PORT_COUNT)) |
| 39 | + GPIO_SetPinInterruptConfig(BOARD_SW2_GPIO, BOARD_SW2_GPIO_PIN, kGPIO_InterruptEitherEdge); |
| 40 | +#else |
| 41 | + PORT_SetPinInterruptConfig(BOARD_SW2_PORT, BOARD_SW2_GPIO_PIN, kPORT_InterruptEitherEdge); |
| 42 | +#endif |
| 43 | + EnableIRQ(BOARD_SW2_IRQ); |
| 44 | + // BOARD_InitDebugConsole(); |
| 45 | +} |
| 46 | + |
| 47 | +extern "C" void USB_DeviceClockInit(void) |
| 48 | +{ |
| 49 | +#if ((defined FSL_FEATURE_USB_KHCI_IRC48M_MODULE_CLOCK_ENABLED) && \ |
| 50 | + (FSL_FEATURE_USB_KHCI_IRC48M_MODULE_CLOCK_ENABLED)) |
| 51 | + CLOCK_EnableUsbfs0Clock(kCLOCK_UsbSrcIrc48M, 48000000U); |
| 52 | +#else |
| 53 | + CLOCK_EnableUsbfs0Clock(kCLOCK_UsbSrcPll0, CLOCK_GetFreq(kCLOCK_PllFllSelClk)); |
| 54 | +#endif /* FSL_FEATURE_USB_KHCI_IRC48M_MODULE_CLOCK_ENABLED */ |
| 55 | +} |
| 56 | + |
| 57 | +auto& mouse_app() |
| 58 | +{ |
| 59 | + static high_resolution_mouse<> m( |
| 60 | + [](const high_resolution_mouse<>::resolution_multiplier_report& report) |
| 61 | + { GPIO_PinWrite(BOARD_LED_RED_GPIO, BOARD_LED_RED_GPIO_PIN, report.resolutions == 0); }); |
| 62 | + return m; |
| 63 | +} |
| 64 | + |
| 65 | +static high_resolution_mouse<>::mouse_report mouse_report{}; |
| 66 | + |
| 67 | +auto& device() |
| 68 | +{ |
| 69 | + static constexpr usb::product_info prinfo{0x1FC9, "NXP", 0x0091, "c2usb hid-mouse", |
| 70 | + usb::version("1.0")}; |
| 71 | + static usb::df::device_instance<usb::speed::FULL> device{mac(), prinfo}; |
| 72 | + return device; |
| 73 | +} |
| 74 | + |
| 75 | +extern "C" void BOARD_SW2_IRQ_HANDLER(void) |
| 76 | +{ |
| 77 | +#if (defined(FSL_FEATURE_PORT_HAS_NO_INTERRUPT) && FSL_FEATURE_PORT_HAS_NO_INTERRUPT) || \ |
| 78 | + (!defined(FSL_FEATURE_SOC_PORT_COUNT)) |
| 79 | + /* Clear external interrupt flag. */ |
| 80 | + GPIO_GpioClearInterruptFlags(BOARD_SW2_GPIO, 1U << BOARD_SW2_GPIO_PIN); |
| 81 | +#else |
| 82 | + /* Clear external interrupt flag. */ |
| 83 | + GPIO_PortClearInterruptFlags(BOARD_SW2_GPIO, 1U << BOARD_SW2_GPIO_PIN); |
| 84 | +#endif |
| 85 | + bool pressed = GPIO_PinRead(BOARD_SW2_GPIO, BOARD_SW2_GPIO_PIN) == 0; |
| 86 | + if (pressed) |
| 87 | + { |
| 88 | + SysTick_Config(MSEC_TO_COUNT((mouse_app().multiplier_report().high_resolution() ? 10 : 100), |
| 89 | + SystemCoreClock)); |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + SysTick->CTRL &= ~_VAL2FLD(SysTick_CTRL_ENABLE, 1); |
| 94 | + } |
| 95 | + switch (device().power_state()) |
| 96 | + { |
| 97 | + case usb::power::state::L2_SUSPEND: |
| 98 | + if (pressed) |
| 99 | + { |
| 100 | + device().remote_wakeup(); |
| 101 | + } |
| 102 | + break; |
| 103 | + case usb::power::state::L0_ON: |
| 104 | + mouse_report.wheel_y = pressed ? -1 : 0; |
| 105 | + mouse_app().send(mouse_report); |
| 106 | + break; |
| 107 | + default: |
| 108 | + break; |
| 109 | + } |
| 110 | + SDK_ISR_EXIT_BARRIER; |
| 111 | +} |
| 112 | + |
| 113 | +extern "C" void SysTick_Handler(void) |
| 114 | +{ |
| 115 | + if (device().power_state() != usb::power::state::L0_ON) |
| 116 | + { |
| 117 | + return; |
| 118 | + } |
| 119 | + mouse_app().send(mouse_report); |
| 120 | + SDK_ISR_EXIT_BARRIER; |
| 121 | +} |
| 122 | + |
| 123 | +int main(void) |
| 124 | +{ |
| 125 | + BOARD_InitHardware(); |
| 126 | + USB_DeviceClockInit(); |
| 127 | + |
| 128 | + static usb::df::hid::function usb_mouse{mouse_app()}; |
| 129 | + static const auto single_config = usb::df::config::make_config( |
| 130 | + usb::df::config::header(usb::df::config::power::bus(100, true), "hid mouse"), |
| 131 | + usb::df::hid::config(usb_mouse, usb::speed::FULL, usb::endpoint::address(0x81), 1)); |
| 132 | + device().set_config(single_config); |
| 133 | + device().open(); |
| 134 | + device().set_power_event_delegate( |
| 135 | + [](usb::df::device& dev, usb::df::device::event ev) |
| 136 | + { |
| 137 | + GPIO_PinWrite(BOARD_LED_GREEN_GPIO, BOARD_LED_GREEN_GPIO_PIN, |
| 138 | + dev.power_state() != usb::power::state::L0_ON); |
| 139 | + }); |
| 140 | + |
| 141 | + while (true) |
| 142 | + { |
| 143 | + __WFI(); |
| 144 | + } |
| 145 | +} |
0 commit comments