Make sure that long reads are aborted early if the connection times out while waiting...
[pub/USBasp.git] / Projects / RelayBoard / RelayBoard.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 OBinou (obconseil [at] gmail [dot] com)
11 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12
13 Permission to use, copy, modify, distribute, and sell this
14 software and its documentation for any purpose is hereby granted
15 without fee, provided that the above copyright notice appear in
16 all copies and that both that the copyright notice and this
17 permission notice and warranty disclaimer appear in supporting
18 documentation, and that the name of the author not be used in
19 advertising or publicity pertaining to distribution of the
20 software without specific, written prior permission.
21
22 The author disclaim all warranties with regard to this
23 software, including all implied warranties of merchantability
24 and fitness. In no event shall the author be liable for any
25 special, indirect or consequential damages or any damages
26 whatsoever resulting from loss of use, data or profits, whether
27 in an action of contract, negligence or other tortious action,
28 arising out of or in connection with the use or performance of
29 this software.
30 */
31
32 /** \file
33 *
34 * Main source file for the RelayBoard program. This file contains the main tasks of
35 * the project and is responsible for the initial application hardware configuration.
36 */
37
38 #include "RelayBoard.h"
39
40
41 /** Main program entry point. This routine contains the overall program flow, including initial
42 * setup of all components and the main program loop.
43 */
44 int main(void)
45 {
46 SetupHardware();
47
48 sei();
49
50 for (;;)
51 USB_USBTask();
52 }
53
54 /** Configures the board hardware and chip peripherals for the project's functionality. */
55 void SetupHardware(void)
56 {
57 /* Disable watchdog if enabled by bootloader/fuses */
58 MCUSR &= ~(1 << WDRF);
59 wdt_disable();
60
61 /* Disable clock division */
62 clock_prescale_set(clock_div_1);
63
64 /* Hardware Initialization */
65 USB_Init();
66
67 /* Initialize Relays */
68 DDRC |= ALL_RELAYS;
69 PORTC &= ~ALL_RELAYS;
70 }
71
72
73 /** Event handler for the library USB Configuration Changed event. */
74 void EVENT_USB_Device_ConfigurationChanged(void)
75 {
76 USB_Device_EnableSOFEvents();
77 }
78
79 /** Event handler for the library USB Unhandled Control Packet event. */
80 void EVENT_USB_Device_UnhandledControlRequest(void)
81 {
82 const uint8_t SerialNumber[5] = { 0, 0, 0, 0, 1 };
83 uint8_t ControlData[2] = { 0, 0 };
84
85 switch (USB_ControlRequest.bRequest)
86 {
87 case 0x09:
88 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
89 {
90 LEDs_ToggleLEDs(LEDS_LED1);
91
92 Endpoint_ClearSETUP();
93
94 Endpoint_Read_Control_Stream_LE(ControlData, sizeof(ControlData));
95 Endpoint_ClearIN();
96
97 switch (USB_ControlRequest.wValue)
98 {
99 case 0x303:
100 if (ControlData[1]) PORTC &= ~RELAY1; else PORTC |= RELAY1;
101 break;
102 case 0x306:
103 if (ControlData[1]) PORTC &= ~RELAY2; else PORTC |= RELAY2;
104 break;
105 case 0x309:
106 if (ControlData[1]) PORTC &= ~RELAY3; else PORTC |= RELAY3;
107 break;
108 case 0x30c:
109 if (ControlData[1]) PORTC &= ~RELAY4; else PORTC |= RELAY4;
110 break;
111 }
112 }
113
114 break;
115 case 0x01:
116 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
117 {
118 LEDs_ToggleLEDs(LEDS_LED1);
119
120 Endpoint_ClearSETUP();
121
122 switch (USB_ControlRequest.wValue)
123 {
124 case 0x301:
125 Endpoint_Write_Control_Stream_LE(SerialNumber, sizeof(SerialNumber));
126 break;
127 case 0x303:
128 ControlData[1] = (PORTC & RELAY1) ? 2 : 3;
129 break;
130 case 0x306:
131 ControlData[1] = (PORTC & RELAY2) ? 2 : 3;
132 break;
133 case 0x309:
134 ControlData[1] = (PORTC & RELAY3) ? 2 : 3;
135 break;
136 case 0x30c:
137 ControlData[1] = (PORTC & RELAY4) ? 2 : 3;
138 break;
139 }
140
141 if (ControlData[1])
142 Endpoint_Write_Control_Stream_LE(ControlData, sizeof(ControlData));
143
144 Endpoint_ClearOUT();
145 }
146
147 break;
148 }
149 }