X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/d1e52660368d34d693131f6aff3c8fd8584162e5..189d0c7e669b6a3d07ed35c1eaa0bfc8cbc7a729:/Projects/Magstripe/Magstripe.c?ds=inline diff --git a/Projects/Magstripe/Magstripe.c b/Projects/Magstripe/Magstripe.c index 5cd107e9a..1963973b4 100644 --- a/Projects/Magstripe/Magstripe.c +++ b/Projects/Magstripe/Magstripe.c @@ -29,18 +29,44 @@ this software. */ +/** \file + * + * Main source file for the MagStripe reader program. This file contains the main tasks of + * the project and is responsible for the initial application hardware configuration. + */ + #include "Magstripe.h" +/** Bit buffers to hold the read bits for each of the three magnetic card tracks before they are transmitted + * to the host as keyboard presses. + */ BitBuffer_t TrackDataBuffers[3]; -USB_ClassInfo_HID_t Keyboard_HID_Interface = +/** LUFA HID Class driver interface configuration and state information. This structure is + * passed to all HID Class driver functions, so that multiple instances of the same class + * within a device can be differentiated from one another. + */ +USB_ClassInfo_HID_Device_t Keyboard_HID_Interface = { - .InterfaceNumber = 0, - - .ReportINEndpointNumber = KEYBOARD_EPNUM, - .ReportINEndpointSize = KEYBOARD_EPSIZE, + .Config = + { + .InterfaceNumber = 0, + + .ReportINEndpointNumber = KEYBOARD_EPNUM, + .ReportINEndpointSize = KEYBOARD_EPSIZE, + + .ReportINBufferSize = sizeof(USB_KeyboardReport_Data_t), + }, + + .State = + { + // Leave all state values to their defaults + } }; +/** Main program entry point. This routine contains the overall program flow, including initial + * setup of all components and the main program loop. + */ int main(void) { SetupHardware(); @@ -53,11 +79,12 @@ int main(void) if (Magstripe_GetStatus() & MAG_CARDPRESENT) ReadMagstripeData(); - USB_HID_USBTask(&Keyboard_HID_Interface); + HID_Device_USBTask(&Keyboard_HID_Interface); USB_USBTask(); } } +/** Configures the board hardware and chip peripherals for the demo's functionality. */ void SetupHardware(void) { /* Disable watchdog if enabled by bootloader/fuses */ @@ -70,8 +97,17 @@ void SetupHardware(void) /* Hardware Initialization */ Magstripe_Init(); USB_Init(); + + /* Millisecond timer initialization, with output compare interrupt enabled for the idle timing */ + OCR0A = ((F_CPU / 64) / 1000); + TCCR0A = (1 << WGM01); + TCCR0B = ((1 << CS01) | (1 << CS00)); + TIMSK0 = (1 << OCIE0A); } +/** Determines if a card has been inserted, and if so reads in each track's contents into the bit buffers + * until they are read out to the host as a series of keyboard presses. + */ void ReadMagstripeData(void) { /* Arrays to hold the buffer pointers, clock and data bit masks for the separate card tracks */ @@ -103,22 +139,34 @@ void ReadMagstripeData(void) } } +/** Event handler for the library USB Configuration Changed event. */ void EVENT_USB_ConfigurationChanged(void) { - USB_HID_ConfigureEndpoints(&Keyboard_HID_Interface); + HID_Device_ConfigureEndpoints(&Keyboard_HID_Interface); } +/** Event handler for the library USB Unhandled Control Packet event. */ void EVENT_USB_UnhandledControlPacket(void) { - USB_HID_ProcessControlPacket(&Keyboard_HID_Interface); + HID_Device_ProcessControlPacket(&Keyboard_HID_Interface); } -void EVENT_USB_StartOfFrame(void) +/** Timer 0 CTC ISR, firing once each millisecond to keep track of elapsed idle time in the HID interface. */ +ISR(TIMER0_COMPA_vect, ISR_BLOCK) { - USB_HID_RegisterStartOfFrame(&Keyboard_HID_Interface); + if (Keyboard_HID_Interface.State.IdleMSRemaining) + Keyboard_HID_Interface.State.IdleMSRemaining--; } -uint16_t CALLBACK_USB_HID_CreateNextHIDReport(USB_ClassInfo_HID_t* HIDInterfaceInfo, void* ReportData) +/** HID Class driver callback function for the creation of a HID report for the host. + * + * \param HIDInterfaceInfo Pointer to the HID interface structure for the HID interface being referenced + * \param ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID + * \param ReportData Pointer to the preallocated report buffer where the created report should be stored + * + * \return Number of bytes in the created report + */ +uint16_t CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* HIDInterfaceInfo, uint8_t* ReportID, void* ReportData) { static bool IsKeyReleaseReport; static bool IsNewlineReport; @@ -159,7 +207,15 @@ uint16_t CALLBACK_USB_HID_CreateNextHIDReport(USB_ClassInfo_HID_t* HIDInterfaceI return sizeof(USB_KeyboardReport_Data_t); } -void CALLBACK_USB_HID_ProcessReceivedHIDReport(USB_ClassInfo_HID_t* HIDInterfaceInfo, void* ReportData, uint16_t ReportSize) +/** HID Class driver callback function for the processing of a received HID report from the host. + * + * \param HIDInterfaceInfo Pointer to the HID interface structure for the HID interface being referenced + * \param ReportID Report ID of the received report from the host + * \param ReportData Pointer to the report buffer where the received report is stored + * \param ReportSize Size in bytes of the report received from the host + */ +void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* HIDInterfaceInfo, uint8_t ReportID, + void* ReportData, uint16_t ReportSize) { // Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports }