Add host mode USB Class driver stubs, add beginnings of a CDC host class driver.
[pub/USBasp.git] / Projects / Magstripe / Magstripe.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Denver Gingerich (denver [at] ossguy [dot] com)
11 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12
13 Permission to use, copy, modify, and distribute this software
14 and its documentation for any purpose and without fee is hereby
15 granted, provided that the above copyright notice appear in all
16 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 MagStripe reader program. This file contains the main tasks of
35 * the project and is responsible for the initial application hardware configuration.
36 */
37
38 #include "Magstripe.h"
39
40 /** Bit buffers to hold the read bits for each of the three magnetic card tracks before they are transmitted
41 * to the host as keyboard presses.
42 */
43 BitBuffer_t TrackDataBuffers[3];
44
45 /** LUFA HID Class driver interface configuration and state information. This structure is
46 * passed to all HID Class driver functions, so that multiple instances of the same class
47 * within a device can be differentiated from one another.
48 */
49 USB_ClassInfo_HID_Device_t Keyboard_HID_Interface =
50 {
51 .InterfaceNumber = 0,
52
53 .ReportINEndpointNumber = KEYBOARD_EPNUM,
54 .ReportINEndpointSize = KEYBOARD_EPSIZE,
55
56 .ReportINBufferSize = sizeof(USB_KeyboardReport_Data_t),
57 };
58
59 /** Main program entry point. This routine contains the overall program flow, including initial
60 * setup of all components and the main program loop.
61 */
62 int main(void)
63 {
64 SetupHardware();
65
66 for (uint8_t Buffer = 0; Buffer < 3; Buffer++)
67 BitBuffer_Init(&TrackDataBuffers[Buffer]);
68
69 for (;;)
70 {
71 if (Magstripe_GetStatus() & MAG_CARDPRESENT)
72 ReadMagstripeData();
73
74 USB_HID_USBTask(&Keyboard_HID_Interface);
75 USB_USBTask();
76 }
77 }
78
79 /** Configures the board hardware and chip peripherals for the demo's functionality. */
80 void SetupHardware(void)
81 {
82 /* Disable watchdog if enabled by bootloader/fuses */
83 MCUSR &= ~(1 << WDRF);
84 wdt_disable();
85
86 /* Disable clock division */
87 clock_prescale_set(clock_div_1);
88
89 /* Hardware Initialization */
90 Magstripe_Init();
91 USB_Init();
92
93 /* Millisecond timer initialization, with output compare interrupt enabled for the idle timing */
94 OCR0A = ((F_CPU / 64) / 1000);
95 TCCR0A = (1 << WGM01);
96 TCCR0B = ((1 << CS01) | (1 << CS00));
97 TIMSK0 = (1 << OCIE0A);
98 }
99
100 /** Determines if a card has been inserted, and if so reads in each track's contents into the bit buffers
101 * until they are read out to the host as a series of keyboard presses.
102 */
103 void ReadMagstripeData(void)
104 {
105 /* Arrays to hold the buffer pointers, clock and data bit masks for the separate card tracks */
106 const struct
107 {
108 uint8_t ClockMask;
109 uint8_t DataMask;
110 } TrackInfo[] = {{MAG_T1_CLOCK, MAG_T1_DATA},
111 {MAG_T2_CLOCK, MAG_T2_DATA},
112 {MAG_T3_CLOCK, MAG_T3_DATA}};
113
114 uint8_t Magstripe_Prev = 0;
115 uint8_t Magstripe_LCL = Magstripe_GetStatus();
116
117 while (Magstripe_LCL & MAG_CARDPRESENT)
118 {
119 for (uint8_t Track = 0; Track < 3; Track++)
120 {
121 bool DataPinLevel = ((Magstripe_LCL & TrackInfo[Track].DataMask) != 0);
122 bool ClockPinLevel = ((Magstripe_LCL & TrackInfo[Track].ClockMask) != 0);
123 bool ClockLevelChanged = (((Magstripe_LCL ^ Magstripe_Prev) & TrackInfo[Track].ClockMask) != 0);
124
125 if (ClockPinLevel && ClockLevelChanged)
126 BitBuffer_StoreNextBit(&TrackDataBuffers[Track], DataPinLevel);
127 }
128
129 Magstripe_Prev = Magstripe_LCL;
130 Magstripe_LCL = Magstripe_GetStatus();
131 }
132 }
133
134 /** Event handler for the library USB Configuration Changed event. */
135 void EVENT_USB_ConfigurationChanged(void)
136 {
137 USB_HID_ConfigureEndpoints(&Keyboard_HID_Interface);
138 }
139
140 /** Event handler for the library USB Unhandled Control Packet event. */
141 void EVENT_USB_UnhandledControlPacket(void)
142 {
143 USB_HID_ProcessControlPacket(&Keyboard_HID_Interface);
144 }
145
146 /** Timer 0 CTC ISR, firing once each millisecond to keep track of elapsed idle time in the HID interface. */
147 ISR(TIMER0_COMPA_vect, ISR_BLOCK)
148 {
149 if (Keyboard_HID_Interface.IdleMSRemaining)
150 Keyboard_HID_Interface.IdleMSRemaining--;
151 }
152
153 /** HID Class driver callback function for the creation of a HID report for the host.
154 *
155 * \param HIDInterfaceInfo Pointer to the HID interface structure for the HID interface being referenced
156 * \param ReportData Pointer to the preallocated report buffer where the created report should be stored
157 *
158 * \return Number of bytes in the created report
159 */
160 uint16_t CALLBACK_USB_HID_CreateNextHIDReport(USB_ClassInfo_HID_Device_t* HIDInterfaceInfo, uint8_t* ReportID, void* ReportData)
161 {
162 static bool IsKeyReleaseReport;
163 static bool IsNewlineReport;
164
165 BitBuffer_t* Buffer = NULL;
166 USB_KeyboardReport_Data_t* KeyboardReport = (USB_KeyboardReport_Data_t*)ReportData;
167
168 /* Key reports must be interleaved with 0 Key Code reports to release the keys, or repeated keys will be ignored */
169 IsKeyReleaseReport = !IsKeyReleaseReport;
170
171 if (IsKeyReleaseReport)
172 {
173 KeyboardReport->KeyCode = 0;
174 }
175 else if (IsNewlineReport)
176 {
177 IsNewlineReport = false;
178 KeyboardReport->KeyCode = KEY_ENTER;
179 }
180 else
181 {
182 if (TrackDataBuffers[0].Elements)
183 Buffer = &TrackDataBuffers[0];
184 else if (TrackDataBuffers[1].Elements)
185 Buffer = &TrackDataBuffers[1];
186 else if (TrackDataBuffers[2].Elements)
187 Buffer = &TrackDataBuffers[2];
188 else
189 return 0;
190
191 KeyboardReport->KeyCode = BitBuffer_GetNextBit(Buffer) ? KEY_1 : KEY_0;
192
193 /* If buffer now empty, next report must be a newline to seperate track data */
194 if (!(Buffer->Elements))
195 IsNewlineReport = true;
196 }
197
198 return sizeof(USB_KeyboardReport_Data_t);
199 }
200
201 /** HID Class driver callback function for the processing of a received HID report from the host.
202 *
203 * \param HIDInterfaceInfo Pointer to the HID interface structure for the HID interface being referenced
204 * \param ReportData Pointer to the report buffer where the received report is stored
205 * \param ReportSize Size in bytes of the report received from the host
206 */
207 void CALLBACK_USB_HID_ProcessReceivedHIDReport(USB_ClassInfo_HID_Device_t* HIDInterfaceInfo, uint8_t ReportID,
208 void* ReportData, uint16_t ReportSize)
209 {
210 // Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports
211 }