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