]> git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/Demos/Device/LowLevel/BulkVendor/BulkVendor.c
Merge commit '20b787fc1284176834cbe7ca2134e4b36bec5828'
[tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / Demos / Device / LowLevel / BulkVendor / BulkVendor.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaims all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 *
33 * Main source file for the Bulk Vendor demo. This file contains the main tasks of the demo and
34 * is responsible for the initial application hardware configuration.
35 */
36
37 #define INCLUDE_FROM_BULKVENDOR_C
38 #include "BulkVendor.h"
39
40
41 /** Main program entry point. This routine configures the hardware required by the application, then
42 * enters a loop to run the application tasks in sequence.
43 */
44 int main(void)
45 {
46 SetupHardware();
47
48 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
49 GlobalInterruptEnable();
50
51 for (;;)
52 {
53 USB_USBTask();
54
55 uint8_t ReceivedData[VENDOR_IO_EPSIZE];
56 memset(ReceivedData, 0x00, sizeof(ReceivedData));
57
58 Endpoint_SelectEndpoint(VENDOR_OUT_EPADDR);
59 if (Endpoint_IsOUTReceived())
60 {
61 Endpoint_Read_Stream_LE(ReceivedData, VENDOR_IO_EPSIZE, NULL);
62 Endpoint_ClearOUT();
63
64 Endpoint_SelectEndpoint(VENDOR_IN_EPADDR);
65 Endpoint_Write_Stream_LE(ReceivedData, VENDOR_IO_EPSIZE, NULL);
66 Endpoint_ClearIN();
67 }
68 }
69 }
70
71 /** Configures the board hardware and chip peripherals for the demo's functionality. */
72 void SetupHardware(void)
73 {
74 #if (ARCH == ARCH_AVR8)
75 /* Disable watchdog if enabled by bootloader/fuses */
76 MCUSR &= ~(1 << WDRF);
77 wdt_disable();
78
79 /* Disable clock division */
80 clock_prescale_set(clock_div_1);
81 #elif (ARCH == ARCH_XMEGA)
82 /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
83 XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
84 XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
85
86 /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
87 XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
88 XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
89
90 PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
91 #endif
92
93 /* Hardware Initialization */
94 LEDs_Init();
95 USB_Init();
96 }
97
98 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs. */
99 void EVENT_USB_Device_Connect(void)
100 {
101 /* Indicate USB enumerating */
102 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
103 }
104
105 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
106 * the status LEDs.
107 */
108 void EVENT_USB_Device_Disconnect(void)
109 {
110 /* Indicate USB not ready */
111 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
112 }
113
114 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
115 * of the USB device after enumeration - the device endpoints are configured.
116 */
117 void EVENT_USB_Device_ConfigurationChanged(void)
118 {
119 bool ConfigSuccess = true;
120
121 /* Setup Vendor Data Endpoints */
122 ConfigSuccess &= Endpoint_ConfigureEndpoint(VENDOR_IN_EPADDR, EP_TYPE_BULK, VENDOR_IO_EPSIZE, 1);
123 ConfigSuccess &= Endpoint_ConfigureEndpoint(VENDOR_OUT_EPADDR, EP_TYPE_BULK, VENDOR_IO_EPSIZE, 1);
124
125 /* Indicate endpoint configuration success or failure */
126 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
127 }
128
129 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
130 * the device from the USB host before passing along unhandled control requests to the library for processing
131 * internally.
132 */
133 void EVENT_USB_Device_ControlRequest(void)
134 {
135 // Process vendor specific control requests here
136 }
Imprint / Impressum