Started Webserver RNDIS host project.
[pub/USBasp.git] / Projects / TemperatureDataLogger / TempDataLogger.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 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 TemperatureDataLogger project. This file contains the main tasks of
34 * the project and is responsible for the initial application hardware configuration.
35 */
36
37 #include "TempDataLogger.h"
38
39 /** LUFA Mass Storage Class driver interface configuration and state information. This structure is
40 * passed to all Mass Storage Class driver functions, so that multiple instances of the same class
41 * within a device can be differentiated from one another.
42 */
43 USB_ClassInfo_MS_Device_t Disk_MS_Interface =
44 {
45 .Config =
46 {
47 .InterfaceNumber = 0,
48
49 .DataINEndpointNumber = MASS_STORAGE_IN_EPNUM,
50 .DataINEndpointSize = MASS_STORAGE_IO_EPSIZE,
51 .DataINEndpointDoubleBank = false,
52
53 .DataOUTEndpointNumber = MASS_STORAGE_OUT_EPNUM,
54 .DataOUTEndpointSize = MASS_STORAGE_IO_EPSIZE,
55 .DataOUTEndpointDoubleBank = false,
56
57 .TotalLUNs = 1,
58 },
59 };
60
61 /** FAT Fs structure to hold the internal state of the FAT driver for the dataflash contents. */
62 FATFS DiskFATState;
63
64 /** FAT Fs structure to hold a FAT file handle for the log data write destination. */
65 FIL TempLogFile;
66
67 /** Counter to count the number of 10 millisecond ticks that has elapsed since the last sample */
68 uint16_t CurrentLogTick;
69
70
71 ISR(TIMER1_COMPA_vect, ISR_BLOCK)
72 {
73 if (CurrentLogTick++ != LOG_INTERVAL_10MS)
74 return;
75
76 uint8_t LEDMask = LEDs_GetLEDs();
77
78 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
79
80 CurrentLogTick = 0;
81
82 if (USB_DeviceState == DEVICE_STATE_Unattached)
83 {
84 f_printf(&TempLogFile, "%d Degrees\r\n", Temperature_GetTemperature());
85 f_sync(&TempLogFile);
86 }
87
88 LEDs_SetAllLEDs(LEDMask);
89 }
90
91
92 /** Main program entry point. This routine contains the overall program flow, including initial
93 * setup of all components and the main program loop.
94 */
95 int main(void)
96 {
97 SetupHardware();
98
99 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
100
101 /* Mount and open the log file on the dataflash FAT partition */
102 f_mount(0, &DiskFATState);
103 f_open(&TempLogFile, LOG_FILENAME, FA_OPEN_ALWAYS | FA_WRITE);
104 f_lseek(&TempLogFile, TempLogFile.fsize);
105 f_printf(&TempLogFile, "===========================\r\n");
106
107 /* Discard the first sample from the temperature sensor, as it is generally incorrect */
108 uint8_t Dummy = Temperature_GetTemperature();
109 (void)Dummy;
110
111 for (;;)
112 {
113 MS_Device_USBTask(&Disk_MS_Interface);
114 USB_USBTask();
115 }
116 }
117
118 /** Configures the board hardware and chip peripherals for the demo's functionality. */
119 void SetupHardware(void)
120 {
121 /* Disable watchdog if enabled by bootloader/fuses */
122 MCUSR &= ~(1 << WDRF);
123 wdt_disable();
124
125 /* Disable clock division */
126 clock_prescale_set(clock_div_1);
127
128 /* Hardware Initialization */
129 LEDs_Init();
130 SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_SCK_LEAD_FALLING | SPI_SAMPLE_TRAILING | SPI_MODE_MASTER);
131 ADC_Init(ADC_REFERENCE_AVCC | ADC_FREE_RUNNING | ADC_PRESCALE_128);
132 Temperature_Init();
133 Dataflash_Init();
134 USB_Init();
135
136 /* 10ms interval timer configuration */
137 OCR1A = (((F_CPU / 1024) / 100) - 1);
138 TCCR1B = (1 << WGM12) | (1 << CS12) | (1 << CS10); // CTC mode, Fcpu/1024 speed
139 TIMSK1 = (1 << OCIE1A);
140
141 /* Clear Dataflash sector protections, if enabled */
142 DataflashManager_ResetDataflashProtections();
143 }
144
145 /** Event handler for the library USB Connection event. */
146 void EVENT_USB_Device_Connect(void)
147 {
148 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
149
150 /* Close the log file so that the host has exclusive filesystem access */
151 f_close(&TempLogFile);
152 }
153
154 /** Event handler for the library USB Disconnection event. */
155 void EVENT_USB_Device_Disconnect(void)
156 {
157 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
158
159 /* When disconnected from the host, re-open log file so we can resume logging */
160 f_mount(0, &DiskFATState);
161 f_open(&TempLogFile, LOG_FILENAME, FA_OPEN_ALWAYS | FA_WRITE);
162 f_lseek(&TempLogFile, TempLogFile.fsize);
163 f_printf(&TempLogFile, "===========================\r\n");
164 }
165
166 /** Event handler for the library USB Configuration Changed event. */
167 void EVENT_USB_Device_ConfigurationChanged(void)
168 {
169 LEDs_SetAllLEDs(LEDMASK_USB_READY);
170
171 if (!(MS_Device_ConfigureEndpoints(&Disk_MS_Interface)))
172 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
173 }
174
175 /** Event handler for the library USB Unhandled Control Request event. */
176 void EVENT_USB_Device_UnhandledControlRequest(void)
177 {
178 MS_Device_ProcessControlRequest(&Disk_MS_Interface);
179 }
180
181 /** Mass Storage class driver callback function the reception of SCSI commands from the host, which must be processed.
182 *
183 * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface configuration structure being referenced
184 */
185 bool CALLBACK_MS_Device_SCSICommandReceived(USB_ClassInfo_MS_Device_t* MSInterfaceInfo)
186 {
187 bool CommandSuccess;
188
189 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
190 CommandSuccess = SCSI_DecodeSCSICommand(MSInterfaceInfo);
191 LEDs_SetAllLEDs(LEDMASK_USB_READY);
192
193 return CommandSuccess;
194 }