e8a863f54d7f93802708a6c5b8c7307f625687eb
[pub/lufa.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 /** Standard file stream for the currently open file on the disk. */
41 FILE DiskStream = FDEV_SETUP_STREAM(NULL, Disk_getchar, _FDEV_SETUP_READ);
42
43 /** Petite FAT Fs structure to hold the internal state of the FAT driver for the dataflash contents. */
44 FATFS DiskFATState;
45
46 /** Stream character fetching routine for the FAT driver so that characters from the currently open file can be
47 * read in sequence when applied to a stdio stream.
48 */
49 static int Disk_getchar(FILE* Stream)
50 {
51 char ReadByte;
52 WORD ByteWasRead;
53
54 if (pf_read(&ReadByte, 1, &ByteWasRead) != FR_OK)
55 return _FDEV_ERR;
56
57 return (ByteWasRead ? ReadByte : _FDEV_EOF);
58 }
59
60 /** Task to determine if the user is wishes to start the programming sequence, and if so executes the
61 * required functions to program the attached target (if any) with the files loaded to the dataflash.
62 */
63 void Programmer_Task(void)
64 {
65 static bool HasAttempted = false;
66
67 if (Buttons_GetStatus() & BUTTONS_BUTTON1)
68 {
69 if (!(HasAttempted))
70 HasAttempted = true;
71 else
72 return;
73
74 puts("==== PROGRAMMING CYCLE STARTED ====\r\n");
75
76 #if defined(USB_CAN_BE_BOTH)
77 printf("Using %s Drive...\r\n", (USB_CurrentMode == USB_MODE_HOST) ? "External" : "Internal");
78 #endif
79
80 puts("Reading Configuration File...\r\n");
81
82 if (!(ProgrammerConfig_ProcessConfiguration()))
83 goto EndOfProgCycle;
84
85 EndOfProgCycle:
86 puts("==== PROGRAMMING CYCLE FINISHED ====\r\n");
87 }
88 else
89 {
90 HasAttempted = false;
91 }
92 }
93
94 /** Main program entry point. This routine contains the overall program flow, including initial
95 * setup of all components and the main program loop.
96 */
97 int main(void)
98 {
99 SetupHardware();
100
101 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
102
103 for (;;)
104 {
105 Programmer_Task();
106
107 if (USB_CurrentMode == USB_MODE_HOST)
108 {
109 #if defined(USB_CAN_BE_HOST)
110 DiskHost_USBTask();
111 #endif
112 }
113 else
114 {
115 #if defined(USB_CAN_BE_DEVICE)
116 DiskDevice_USBTask();
117 #endif
118 }
119
120 USB_USBTask();
121 }
122 }
123
124 /** Configures the board hardware and chip peripherals for the demo's functionality. */
125 void SetupHardware(void)
126 {
127 /* Disable watchdog if enabled by bootloader/fuses */
128 MCUSR &= ~(1 << WDRF);
129 wdt_disable();
130
131 /* Disable clock division */
132 clock_prescale_set(clock_div_1);
133
134 /* Hardware Initialization */
135 #if defined(USB_CAN_BE_BOTH)
136 USB_Init(USB_MODE_UID);
137 #else
138 USB_Init();
139 #endif
140
141 LEDs_Init();
142 SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_SCK_LEAD_FALLING | SPI_SAMPLE_TRAILING | SPI_MODE_MASTER);
143 Dataflash_Init();
144 Buttons_Init();
145 SerialStream_Init(9600, true);
146
147 #if defined(USB_CAN_BE_DEVICE)
148 /* Clear Dataflash sector protections, if enabled */
149 DataflashManager_ResetDataflashProtections();
150 #endif
151 }