State information for class drivers is now zeroed out during enumeration (both in...
[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[TOTAL_TRACKS];
44
45 /** Pointer to the current track buffer being sent to the host. */
46 BitBuffer_t* CurrentTrackBuffer = &TrackDataBuffers[TOTAL_TRACKS];
47
48 /** LUFA HID Class driver interface configuration and state information. This structure is
49 * passed to all HID Class driver functions, so that multiple instances of the same class
50 * within a device can be differentiated from one another.
51 */
52 USB_ClassInfo_HID_Device_t Keyboard_HID_Interface =
53 {
54 .Config =
55 {
56 .InterfaceNumber = 0,
57
58 .ReportINEndpointNumber = KEYBOARD_EPNUM,
59 .ReportINEndpointSize = KEYBOARD_EPSIZE,
60
61 .ReportINBufferSize = sizeof(USB_KeyboardReport_Data_t),
62 },
63 };
64
65 /** Main program entry point. This routine contains the overall program flow, including initial
66 * setup of all components and the main program loop.
67 */
68 int main(void)
69 {
70 SetupHardware();
71
72 for (uint8_t Buffer = 0; Buffer < TOTAL_TRACKS; Buffer++)
73 BitBuffer_Init(&TrackDataBuffers[Buffer]);
74
75 for (;;)
76 {
77 if (Magstripe_GetStatus() & MAG_CARDPRESENT)
78 ReadMagstripeData();
79
80 HID_Device_USBTask(&Keyboard_HID_Interface);
81 USB_USBTask();
82 }
83 }
84
85 /** Configures the board hardware and chip peripherals for the demo's functionality. */
86 void SetupHardware(void)
87 {
88 /* Disable watchdog if enabled by bootloader/fuses */
89 MCUSR &= ~(1 << WDRF);
90 wdt_disable();
91
92 /* Disable clock division */
93 clock_prescale_set(clock_div_1);
94
95 /* Hardware Initialization */
96 Magstripe_Init();
97 USB_Init();
98
99 /* Millisecond timer initialization, with output compare interrupt enabled for the idle timing */
100 OCR0A = ((F_CPU / 64) / 1000);
101 TCCR0A = (1 << WGM01);
102 TCCR0B = ((1 << CS01) | (1 << CS00));
103 TIMSK0 = (1 << OCIE0A);
104 }
105
106 /** Determines if a card has been inserted, and if so reads in each track's contents into the bit buffers
107 * until they are read out to the host as a series of keyboard presses.
108 */
109 void ReadMagstripeData(void)
110 {
111 /* Arrays to hold the buffer pointers, clock and data bit masks for the separate card tracks */
112 const struct
113 {
114 uint8_t ClockMask;
115 uint8_t DataMask;
116 } TrackInfo[] = {{MAG_T1_CLOCK, MAG_T1_DATA},
117 {MAG_T2_CLOCK, MAG_T2_DATA},
118 {MAG_T3_CLOCK, MAG_T3_DATA}};
119
120 uint8_t Magstripe_Prev = 0;
121 uint8_t Magstripe_LCL = Magstripe_GetStatus();
122
123 while (Magstripe_LCL & MAG_CARDPRESENT)
124 {
125 for (uint8_t Track = 0; Track < TOTAL_TRACKS; Track++)
126 {
127 bool DataPinLevel = ((Magstripe_LCL & TrackInfo[Track].DataMask) != 0);
128 bool ClockPinLevel = ((Magstripe_LCL & TrackInfo[Track].ClockMask) != 0);
129 bool ClockLevelChanged = (((Magstripe_LCL ^ Magstripe_Prev) & TrackInfo[Track].ClockMask) != 0);
130
131 /* Sample data on rising clock edges from the card reader */
132 if (ClockPinLevel && ClockLevelChanged)
133 BitBuffer_StoreNextBit(&TrackDataBuffers[Track], DataPinLevel);
134 }
135
136 Magstripe_Prev = Magstripe_LCL;
137 Magstripe_LCL = Magstripe_GetStatus();
138 }
139
140 CurrentTrackBuffer = &TrackDataBuffers[0];
141 }
142
143 /** Event handler for the library USB Configuration Changed event. */
144 void EVENT_USB_ConfigurationChanged(void)
145 {
146 HID_Device_ConfigureEndpoints(&Keyboard_HID_Interface);
147 }
148
149 /** Event handler for the library USB Unhandled Control Packet event. */
150 void EVENT_USB_UnhandledControlPacket(void)
151 {
152 HID_Device_ProcessControlPacket(&Keyboard_HID_Interface);
153 }
154
155 /** Timer 0 CTC ISR, firing once each millisecond to keep track of elapsed idle time in the HID interface. */
156 ISR(TIMER0_COMPA_vect, ISR_BLOCK)
157 {
158 if (Keyboard_HID_Interface.State.IdleMSRemaining)
159 Keyboard_HID_Interface.State.IdleMSRemaining--;
160 }
161
162 /** HID Class driver callback function for the creation of a HID report for the host.
163 *
164 * \param[in] HIDInterfaceInfo Pointer to the HID interface structure for the HID interface being referenced
165 * \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
166 * \param[out] ReportData Pointer to the preallocated report buffer where the created report should be stored
167 *
168 * \return Number of bytes in the created report
169 */
170 uint16_t CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo, uint8_t* const ReportID, void* ReportData)
171 {
172 USB_KeyboardReport_Data_t* KeyboardReport = (USB_KeyboardReport_Data_t*)ReportData;
173
174 static bool IsKeyReleaseReport;
175
176 /* Key reports must be interleaved with key release reports, or repeated keys will be ignored */
177 IsKeyReleaseReport = !IsKeyReleaseReport;
178
179 if ((IsKeyReleaseReport) || (CurrentTrackBuffer == &TrackDataBuffers[TOTAL_TRACKS]))
180 {
181 /* No more data to send, or key release report between key presses */
182 KeyboardReport->KeyCode = KEY_NONE;
183 }
184 else if (!(CurrentTrackBuffer->Elements))
185 {
186 /* End of current track, send an enter press and change to the next track's buffer */
187 KeyboardReport->KeyCode = KEY_ENTER;
188 CurrentTrackBuffer++;
189 }
190 else
191 {
192 /* Still data in the current track; convert next bit to a 1 or 0 keypress */
193 KeyboardReport->KeyCode = BitBuffer_GetNextBit(CurrentTrackBuffer) ? KEY_1 : KEY_0;
194 }
195
196 return sizeof(USB_KeyboardReport_Data_t);
197 }
198
199 /** HID Class driver callback function for the processing of a received HID report from the host.
200 *
201 * \param[in] HIDInterfaceInfo Pointer to the HID interface structure for the HID interface being referenced
202 * \param[in] ReportID Report ID of the received report from the host
203 * \param[in] ReportData Pointer to the report buffer where the received report is stored
204 * \param[in] ReportSize Size in bytes of the report received from the host
205 */
206 void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo, const uint8_t ReportID,
207 const void* ReportData, const uint16_t ReportSize)
208 {
209 // Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports
210 }