Oops - fix include directory for case-sensitive filesystems.
[pub/USBasp.git] / Projects / TemperatureDataLogger / TempDataLogger.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, 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 tick 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 CurrentLogTick = 0;
77
78 if (USB_DeviceState == DEVICE_STATE_Unattached)
79 {
80 f_printf(&TempLogFile, "%d Degrees\r\n", Temperature_GetTemperature());
81 f_sync(&TempLogFile);
82 }
83 }
84
85
86 /** Main program entry point. This routine contains the overall program flow, including initial
87 * setup of all components and the main program loop.
88 */
89 int main(void)
90 {
91 SetupHardware();
92
93 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
94
95 /* Mount and open the log file on the dataflash FAT partition */
96 f_mount(0, &DiskFATState);
97 f_open(&TempLogFile, LOG_FILENAME, FA_OPEN_ALWAYS | FA_WRITE);
98 f_lseek(&TempLogFile, TempLogFile.fsize);
99
100 /* Write out the log seperator line */
101 f_printf(&TempLogFile, "===========================\r\n");
102 Temperature_GetTemperature(); // Discard first temperature reading to ensure accuracy
103
104 for (;;)
105 {
106 MS_Device_USBTask(&Disk_MS_Interface);
107 USB_USBTask();
108 }
109 }
110
111 /** Configures the board hardware and chip peripherals for the demo's functionality. */
112 void SetupHardware(void)
113 {
114 /* Disable watchdog if enabled by bootloader/fuses */
115 MCUSR &= ~(1 << WDRF);
116 wdt_disable();
117
118 /* Disable clock division */
119 clock_prescale_set(clock_div_1);
120
121 /* Hardware Initialization */
122 LEDs_Init();
123 SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_SCK_LEAD_FALLING | SPI_SAMPLE_TRAILING | SPI_MODE_MASTER);
124 Dataflash_Init();
125 USB_Init();
126 ADC_Init(ADC_REFERENCE_AVCC | ADC_FREE_RUNNING | ADC_PRESCALE_128);
127 Temperature_Init();
128
129 /* 10ms interval timer configuration */
130 OCR1A = (((F_CPU / 1024) / 100) - 1);
131 TCCR1A = (1 << WGM01); // CTC mode
132 TCCR1B = (1 << CS12) | (1 << CS10); // Fcpu/1024 speed
133 TIMSK1 = (1 << OCIE1A);
134
135 /* Clear Dataflash sector protections, if enabled */
136 DataflashManager_ResetDataflashProtections();
137 }
138
139 /** Event handler for the library USB Connection event. */
140 void EVENT_USB_Device_Connect(void)
141 {
142 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
143
144 /* Close the log file so that the host has exclusive filesystem access */
145 f_close(&TempLogFile);
146 }
147
148 /** Event handler for the library USB Disconnection event. */
149 void EVENT_USB_Device_Disconnect(void)
150 {
151 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
152
153 /* When disconnected from the host, re-open log file so we can resume logging */
154 f_mount(0, &DiskFATState);
155 f_open(&TempLogFile, LOG_FILENAME, FA_OPEN_ALWAYS | FA_WRITE);
156 f_lseek(&TempLogFile, TempLogFile.fsize);
157 }
158
159 /** Event handler for the library USB Configuration Changed event. */
160 void EVENT_USB_Device_ConfigurationChanged(void)
161 {
162 LEDs_SetAllLEDs(LEDMASK_USB_READY);
163
164 if (!(MS_Device_ConfigureEndpoints(&Disk_MS_Interface)))
165 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
166 }
167
168 /** Event handler for the library USB Unhandled Control Request event. */
169 void EVENT_USB_Device_UnhandledControlRequest(void)
170 {
171 MS_Device_ProcessControlRequest(&Disk_MS_Interface);
172 }
173
174 /** Mass Storage class driver callback function the reception of SCSI commands from the host, which must be processed.
175 *
176 * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface configuration structure being referenced
177 */
178 bool CALLBACK_MS_Device_SCSICommandReceived(USB_ClassInfo_MS_Device_t* MSInterfaceInfo)
179 {
180 bool CommandSuccess;
181
182 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
183 CommandSuccess = SCSI_DecodeSCSICommand(MSInterfaceInfo);
184 LEDs_SetAllLEDs(LEDMASK_USB_READY);
185
186 return CommandSuccess;
187 }