Fixed LowLevel Keyboard demo not saving the issued report only after it has been...
[pub/USBasp.git] / Projects / Incomplete / StandaloneProgrammer / StandaloneProgrammer.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 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 *
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.
35 */
36
37 #define INCLUDE_FROM_STANDALONEPROG_C
38 #include "StandaloneProgrammer.h"
39
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.
43 */
44 USB_ClassInfo_MS_Device_t Disk_MS_Interface =
45 {
46 .Config =
47 {
48 .InterfaceNumber = 0,
49
50 .DataINEndpointNumber = MASS_STORAGE_IN_EPNUM,
51 .DataINEndpointSize = MASS_STORAGE_IO_EPSIZE,
52 .DataINEndpointDoubleBank = false,
53
54 .DataOUTEndpointNumber = MASS_STORAGE_OUT_EPNUM,
55 .DataOUTEndpointSize = MASS_STORAGE_IO_EPSIZE,
56 .DataOUTEndpointDoubleBank = false,
57
58 .TotalLUNs = 1,
59 },
60 };
61
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.
65 */
66 USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
67 {
68 .Config =
69 {
70 .ControlInterfaceNumber = 0,
71
72 .DataINEndpointNumber = CDC_TX_EPNUM,
73 .DataINEndpointSize = CDC_TXRX_EPSIZE,
74 .DataINEndpointDoubleBank = false,
75
76 .DataOUTEndpointNumber = CDC_RX_EPNUM,
77 .DataOUTEndpointSize = CDC_TXRX_EPSIZE,
78 .DataOUTEndpointDoubleBank = false,
79
80 .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM,
81 .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE,
82 .NotificationEndpointDoubleBank = false,
83 },
84 };
85
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
88 */
89 FILE USBSerialStream;
90
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);
93
94 /** Petite FAT Fs structure to hold the internal state of the FAT driver for the dataflash contents. */
95 FATFS DataflashData;
96
97
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.
100 */
101 static int Dataflash_getchar(FILE* Stream)
102 {
103 char ReadByte;
104 WORD ByteWasRead;
105
106 if (pf_read(&ReadByte, 1, &ByteWasRead) != FR_OK)
107 return _FDEV_ERR;
108
109 return (ByteWasRead ? ReadByte : _FDEV_EOF);
110 }
111
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.
114 */
115 void Programmer_Task(void)
116 {
117 static bool HasAttempted = false;
118
119 if (Buttons_GetStatus() & BUTTONS_BUTTON1)
120 {
121 if (!(HasAttempted))
122 HasAttempted = true;
123 else
124 return;
125
126 fputs("==== PROGRAMMING CYCLE STARTED ====\r\n", &USBSerialStream);
127
128 fputs("Reading Configuration File...\r\n", &USBSerialStream);
129
130 if (!(ProgrammerConfig_ProcessConfiguration()))
131 goto EndOfProgCycle;
132
133 EndOfProgCycle:
134 fputs("==== PROGRAMMING CYCLE FINISHED ====\r\n", &USBSerialStream);
135 }
136 else
137 {
138 HasAttempted = false;
139 }
140 }
141
142 /** Main program entry point. This routine contains the overall program flow, including initial
143 * setup of all components and the main program loop.
144 */
145 int main(void)
146 {
147 SetupHardware();
148
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);
151
152 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
153
154 for (;;)
155 {
156 Programmer_Task();
157
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);
161
162 CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
163 MS_Device_USBTask(&Disk_MS_Interface);
164 USB_USBTask();
165 }
166 }
167
168 /** Configures the board hardware and chip peripherals for the demo's functionality. */
169 void SetupHardware(void)
170 {
171 /* Disable watchdog if enabled by bootloader/fuses */
172 MCUSR &= ~(1 << WDRF);
173 wdt_disable();
174
175 /* Disable clock division */
176 clock_prescale_set(clock_div_1);
177
178 /* Hardware Initialization */
179 LEDs_Init();
180 SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_SCK_LEAD_FALLING | SPI_SAMPLE_TRAILING | SPI_MODE_MASTER);
181 Dataflash_Init();
182 USB_Init();
183 Buttons_Init();
184 pf_mount(&DataflashData);
185
186 /* Clear Dataflash sector protections, if enabled */
187 DataflashManager_ResetDataflashProtections();
188 }
189
190 /** Event handler for the library USB Connection event. */
191 void EVENT_USB_Device_Connect(void)
192 {
193 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
194 }
195
196 /** Event handler for the library USB Disconnection event. */
197 void EVENT_USB_Device_Disconnect(void)
198 {
199 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
200 }
201
202 /** Event handler for the library USB Configuration Changed event. */
203 void EVENT_USB_Device_ConfigurationChanged(void)
204 {
205 LEDs_SetAllLEDs(LEDMASK_USB_READY);
206
207 if (!(MS_Device_ConfigureEndpoints(&Disk_MS_Interface)))
208 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
209
210 if (!(CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface)))
211 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
212 }
213
214 /** Event handler for the library USB Unhandled Control Request event. */
215 void EVENT_USB_Device_UnhandledControlRequest(void)
216 {
217 MS_Device_ProcessControlRequest(&Disk_MS_Interface);
218 CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
219 }
220
221 /** Mass Storage class driver callback function the reception of SCSI commands from the host, which must be processed.
222 *
223 * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface configuration structure being referenced
224 */
225 bool CALLBACK_MS_Device_SCSICommandReceived(USB_ClassInfo_MS_Device_t* MSInterfaceInfo)
226 {
227 bool CommandSuccess;
228
229 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
230 CommandSuccess = SCSI_DecodeSCSICommand(MSInterfaceInfo);
231 LEDs_SetAllLEDs(LEDMASK_USB_READY);
232
233 return CommandSuccess;
234 }