3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
33 * Main source file for the Standalone Programmer project. This file contains the main tasks of
34 * the demo and is responsible for the initial application hardware configuration.
37 #define INCLUDE_FROM_STANDALONEPROG_C
38 #include "StandaloneProgrammer.h"
40 /** LUFA Mass Storage Class driver interface configuration and state information. This structure is
41 * passed to all Mass Storage Class driver functions, so that multiple instances of the same class
42 * within a device can be differentiated from one another.
44 USB_ClassInfo_MS_Device_t Disk_MS_Interface
=
50 .DataINEndpointNumber
= MASS_STORAGE_IN_EPNUM
,
51 .DataINEndpointSize
= MASS_STORAGE_IO_EPSIZE
,
52 .DataINEndpointDoubleBank
= false,
54 .DataOUTEndpointNumber
= MASS_STORAGE_OUT_EPNUM
,
55 .DataOUTEndpointSize
= MASS_STORAGE_IO_EPSIZE
,
56 .DataOUTEndpointDoubleBank
= false,
62 /** LUFA CDC Class driver interface configuration and state information. This structure is
63 * passed to all CDC Class driver functions, so that multiple instances of the same class
64 * within a device can be differentiated from one another.
66 USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface
=
70 .ControlInterfaceNumber
= 0,
72 .DataINEndpointNumber
= CDC_TX_EPNUM
,
73 .DataINEndpointSize
= CDC_TXRX_EPSIZE
,
74 .DataINEndpointDoubleBank
= false,
76 .DataOUTEndpointNumber
= CDC_RX_EPNUM
,
77 .DataOUTEndpointSize
= CDC_TXRX_EPSIZE
,
78 .DataOUTEndpointDoubleBank
= false,
80 .NotificationEndpointNumber
= CDC_NOTIFICATION_EPNUM
,
81 .NotificationEndpointSize
= CDC_NOTIFICATION_EPSIZE
,
82 .NotificationEndpointDoubleBank
= false,
86 /** Standard file stream for the CDC interface when set up, so that the virtual CDC COM port can be
87 * used like any regular character stream in the C APIs
91 /** Standard file stream for the currently open file on the dataflash disk. */
92 FILE DataflashStream
= FDEV_SETUP_STREAM(NULL
, Dataflash_getchar
, _FDEV_SETUP_READ
);
94 /** Petite FAT Fs structure to hold the internal state of the FAT driver for the dataflash contents. */
98 /** Stream character fetching routine for the FAT driver so that characters from the currently open file can be
99 * read in sequence when applied to a stdio stream.
101 static int Dataflash_getchar(FILE* Stream
)
106 if (pf_read(&ReadByte
, 1, &ByteWasRead
) != FR_OK
)
109 return (ByteWasRead ? ReadByte
: _FDEV_EOF
);
112 /** Task to determine if the user is wishes to start the programming sequence, and if so executes the
113 * required functions to program the attached target (if any) with the files loaded to the dataflash.
115 void Programmer_Task(void)
117 static bool HasAttempted
= false;
119 if (Buttons_GetStatus() & BUTTONS_BUTTON1
)
126 fputs("==== PROGRAMMING CYCLE STARTED ====\r\n", &USBSerialStream
);
128 fputs("Reading Configuration File...\r\n", &USBSerialStream
);
130 if (!(ProgrammerConfig_ProcessConfiguration()))
134 fputs("==== PROGRAMMING CYCLE FINISHED ====\r\n", &USBSerialStream
);
138 HasAttempted
= false;
142 /** Main program entry point. This routine contains the overall program flow, including initial
143 * setup of all components and the main program loop.
149 /* Create a regular character stream for the interface so that it can be used with the stdio.h functions */
150 CDC_Device_CreateStream(&VirtualSerial_CDC_Interface
, &USBSerialStream
);
152 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
158 /* Must throw away unused bytes from the host, or it will lock up while waiting for the device */
159 while (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface
))
160 CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface
);
162 CDC_Device_USBTask(&VirtualSerial_CDC_Interface
);
163 MS_Device_USBTask(&Disk_MS_Interface
);
168 /** Configures the board hardware and chip peripherals for the demo's functionality. */
169 void SetupHardware(void)
171 /* Disable watchdog if enabled by bootloader/fuses */
172 MCUSR
&= ~(1 << WDRF
);
175 /* Disable clock division */
176 clock_prescale_set(clock_div_1
);
178 /* Hardware Initialization */
180 SPI_Init(SPI_SPEED_FCPU_DIV_2
| SPI_SCK_LEAD_FALLING
| SPI_SAMPLE_TRAILING
| SPI_MODE_MASTER
);
184 pf_mount(&DataflashData
);
186 /* Clear Dataflash sector protections, if enabled */
187 DataflashManager_ResetDataflashProtections();
190 /** Event handler for the library USB Connection event. */
191 void EVENT_USB_Device_Connect(void)
193 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
196 /** Event handler for the library USB Disconnection event. */
197 void EVENT_USB_Device_Disconnect(void)
199 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
202 /** Event handler for the library USB Configuration Changed event. */
203 void EVENT_USB_Device_ConfigurationChanged(void)
205 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
207 if (!(MS_Device_ConfigureEndpoints(&Disk_MS_Interface
)))
208 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
210 if (!(CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface
)))
211 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
214 /** Event handler for the library USB Unhandled Control Request event. */
215 void EVENT_USB_Device_UnhandledControlRequest(void)
217 MS_Device_ProcessControlRequest(&Disk_MS_Interface
);
218 CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface
);
221 /** Mass Storage class driver callback function the reception of SCSI commands from the host, which must be processed.
223 * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface configuration structure being referenced
225 bool CALLBACK_MS_Device_SCSICommandReceived(USB_ClassInfo_MS_Device_t
* MSInterfaceInfo
)
229 LEDs_SetAllLEDs(LEDMASK_USB_BUSY
);
230 CommandSuccess
= SCSI_DecodeSCSICommand(MSInterfaceInfo
);
231 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
233 return CommandSuccess
;