Minor documentation improvements.
[pub/USBasp.git] / Bootloaders / MassStorage / BootloaderMassStorage.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2013.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2013 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 disclaims 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 Mass Storage class bootloader. This file contains the complete bootloader logic.
34 */
35
36 #include "BootloaderMassStorage.h"
37
38 /** LUFA Mass Storage Class driver interface configuration and state information. This structure is
39 * passed to all Mass Storage Class driver functions, so that multiple instances of the same class
40 * within a device can be differentiated from one another.
41 */
42 USB_ClassInfo_MS_Device_t Disk_MS_Interface =
43 {
44 .Config =
45 {
46 .InterfaceNumber = 0,
47 .DataINEndpoint =
48 {
49 .Address = MASS_STORAGE_IN_EPADDR,
50 .Size = MASS_STORAGE_IO_EPSIZE,
51 .Banks = 1,
52 },
53 .DataOUTEndpoint =
54 {
55 .Address = MASS_STORAGE_OUT_EPADDR,
56 .Size = MASS_STORAGE_IO_EPSIZE,
57 .Banks = 1,
58 },
59 .TotalLUNs = 1,
60 },
61 };
62
63
64 void Application_Jump_Check(void)
65 {
66 bool JumpToApplication = false;
67
68 #if (BOARD == BOARD_LEONARDO)
69 /* Enable pull-up on the IO13 pin so we can use it to select the mode */
70 PORTC |= (1 << 7);
71 Delay_MS(10);
72 JumpToApplication |= ((PINC & (1 << 7)) != 0);
73 PORTC &= ~(1 << 7);
74 #endif
75
76 if (JumpToApplication)
77 {
78 // cppcheck-suppress constStatement
79 ((void (*)(void))0x0000)();
80 }
81 }
82
83 /** Main program entry point. This routine configures the hardware required by the application, then
84 * enters a loop to run the application tasks in sequence.
85 */
86 int main(void)
87 {
88 SetupHardware();
89
90 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
91 GlobalInterruptEnable();
92
93 for (;;)
94 {
95 MS_Device_USBTask(&Disk_MS_Interface);
96 USB_USBTask();
97 }
98 }
99
100 /** Configures the board hardware and chip peripherals for the demo's functionality. */
101 static void SetupHardware(void)
102 {
103 /* Disable watchdog if enabled by bootloader/fuses */
104 MCUSR &= ~(1 << WDRF);
105 wdt_disable();
106
107 /* Disable clock division */
108 clock_prescale_set(clock_div_1);
109
110 /* Relocate the interrupt vector table to the bootloader section */
111 MCUCR = (1 << IVCE);
112 MCUCR = (1 << IVSEL);
113
114 /* Hardware Initialization */
115 LEDs_Init();
116 USB_Init();
117
118 /* Bootloader active LED toggle timer initialization */
119 TIMSK1 = (1 << TOIE1);
120 TCCR1B = ((1 << CS11) | (1 << CS10));
121 }
122
123 /** ISR to periodically toggle the LEDs on the board to indicate that the bootloader is active. */
124 ISR(TIMER1_OVF_vect, ISR_BLOCK)
125 {
126 LEDs_ToggleLEDs(LEDS_LED1 | LEDS_LED2);
127 }
128
129 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs. */
130 void EVENT_USB_Device_Connect(void)
131 {
132 /* Indicate USB enumerating */
133 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
134 }
135
136 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
137 * the status LEDs and stops the Mass Storage management task.
138 */
139 void EVENT_USB_Device_Disconnect(void)
140 {
141 /* Indicate USB not ready */
142 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
143 }
144
145 /** Event handler for the library USB Configuration Changed event. */
146 void EVENT_USB_Device_ConfigurationChanged(void)
147 {
148 bool ConfigSuccess = true;
149
150 /* Setup Mass Storage Data Endpoints */
151 ConfigSuccess &= MS_Device_ConfigureEndpoints(&Disk_MS_Interface);
152
153 /* Indicate endpoint configuration success or failure */
154 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
155 }
156
157 /** Event handler for the library USB Control Request reception event. */
158 void EVENT_USB_Device_ControlRequest(void)
159 {
160 MS_Device_ProcessControlRequest(&Disk_MS_Interface);
161 }
162
163 /** Mass Storage class driver callback function the reception of SCSI commands from the host, which must be processed.
164 *
165 * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface configuration structure being referenced
166 */
167 bool CALLBACK_MS_Device_SCSICommandReceived(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
168 {
169 bool CommandSuccess;
170
171 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
172 CommandSuccess = SCSI_DecodeSCSICommand(MSInterfaceInfo);
173 LEDs_SetAllLEDs(LEDMASK_USB_READY);
174
175 return CommandSuccess;
176 }