Added Dataflash operational checks and aborts to all projects using the Dataflash...
[pub/USBasp.git] / Projects / Incomplete / StandaloneProgrammer / StandaloneProgrammer.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all 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 project 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 #if defined(USB_CAN_BE_BOTH)
61 /** Event to handle mode changes in the library, to clear the FAT library's drive state structure when transitioning
62 * between modes. This ensures that the library always works with current disk data.
63 */
64 void EVENT_USB_UIDChange(void)
65 {
66 pf_mount(&DiskFATState);
67 }
68 #endif
69
70 /** Task to determine if the user is wishes to start the programming sequence, and if so executes the
71 * required functions to program the attached target (if any) with the files loaded to the Dataflash.
72 */
73 void Programmer_Task(void)
74 {
75 static bool HasAttempted = false;
76
77 if (Buttons_GetStatus() & BUTTONS_BUTTON1)
78 {
79 if (!(HasAttempted))
80 HasAttempted = true;
81 else
82 return;
83
84 puts("==== PROGRAMMING CYCLE STARTED ====\r\n");
85
86 #if defined(USB_CAN_BE_BOTH)
87 printf("Using %s Drive...\r\n", (USB_CurrentMode == USB_MODE_Host) ? "External" : "Internal");
88 #endif
89
90 puts("Reading Configuration File...\r\n");
91 if (!(ProgrammerConfig_ProcessConfiguration()))
92 goto EndOfProgCycle;
93
94 EndOfProgCycle:
95 puts("==== PROGRAMMING CYCLE FINISHED ====\r\n");
96 }
97 else
98 {
99 HasAttempted = false;
100 }
101 }
102
103 /** Main program entry point. This routine contains the overall program flow, including initial
104 * setup of all components and the main program loop.
105 */
106 int main(void)
107 {
108 SetupHardware();
109
110 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
111 sei();
112
113 for (;;)
114 {
115 Programmer_Task();
116
117 #if defined(USB_CAN_BE_HOST)
118 if (USB_CurrentMode == USB_MODE_Host)
119 DiskHost_USBTask();
120 #endif
121
122 #if defined(USB_CAN_BE_DEVICE)
123 if (USB_CurrentMode == USB_MODE_Device)
124 DiskDevice_USBTask();
125 #endif
126
127 USB_USBTask();
128 }
129 }
130
131 /** Configures the board hardware and chip peripherals for the demo's functionality. */
132 void SetupHardware(void)
133 {
134 /* Disable watchdog if enabled by bootloader/fuses */
135 MCUSR &= ~(1 << WDRF);
136 wdt_disable();
137
138 /* Disable clock division */
139 clock_prescale_set(clock_div_1);
140
141 /* Hardware Initialization */
142 #if defined(USB_CAN_BE_BOTH)
143 USB_Init(USB_MODE_UID);
144 #else
145 USB_Init();
146 #endif
147
148 LEDs_Init();
149 SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_SCK_LEAD_FALLING | SPI_SAMPLE_TRAILING | SPI_MODE_MASTER);
150 Dataflash_Init();
151 Buttons_Init();
152 Serial_Init(9600, true);
153
154 /* Create a stdio stream for the serial port for stdin and stdout */
155 Serial_CreateStream(NULL);
156
157 #if defined(USB_CAN_BE_DEVICE)
158 /* Check if the Dataflash is working, abort if not */
159 if (!(DataflashManager_CheckDataflashOperation()))
160 {
161 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
162 for(;;);
163 }
164
165 /* Clear Dataflash sector protections, if enabled */
166 DataflashManager_ResetDataflashProtections();
167 #endif
168 }
169