3      Copyright (C) Dean Camera, 2010. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  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. 
  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 
  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. 
  37 #include "TempDataLogger.h" 
  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. 
  43 USB_ClassInfo_MS_Device_t Disk_MS_Interface 
= 
  49                                 .DataINEndpointNumber      
= MASS_STORAGE_IN_EPNUM
, 
  50                                 .DataINEndpointSize        
= MASS_STORAGE_IO_EPSIZE
, 
  51                                 .DataINEndpointDoubleBank  
= false, 
  53                                 .DataOUTEndpointNumber     
= MASS_STORAGE_OUT_EPNUM
, 
  54                                 .DataOUTEndpointSize       
= MASS_STORAGE_IO_EPSIZE
, 
  55                                 .DataOUTEndpointDoubleBank 
= false, 
  61 /** FAT Fs structure to hold the internal state of the FAT driver for the dataflash contents. */ 
  64 /** FAT Fs structure to hold a FAT file handle for the log data write destination. */ 
  67 /** Counter to count the number of 10 millisecond ticks that has elapsed since the last sample */ 
  68 uint16_t CurrentLogTick
; 
  71 ISR(TIMER1_COMPA_vect
, ISR_BLOCK
) 
  73         if (CurrentLogTick
++ != LOG_INTERVAL_10MS
) 
  76         uint8_t LEDMask 
= LEDs_GetLEDs(); 
  78         LEDs_SetAllLEDs(LEDMASK_USB_BUSY
); 
  82         if (USB_DeviceState 
== DEVICE_STATE_Unattached
) 
  84                 f_printf(&TempLogFile
, "%d Degrees\r\n", Temperature_GetTemperature()); 
  88         LEDs_SetAllLEDs(LEDMask
); 
  92 /** Main program entry point. This routine contains the overall program flow, including initial 
  93  *  setup of all components and the main program loop. 
  99         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 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"); 
 107         /* Discard the first sample from the temperature sensor, as it is generally incorrect */ 
 108         uint8_t Dummy 
= Temperature_GetTemperature(); 
 113                 MS_Device_USBTask(&Disk_MS_Interface
); 
 118 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
 119 void SetupHardware(void) 
 121         /* Disable watchdog if enabled by bootloader/fuses */ 
 122         MCUSR 
&= ~(1 << WDRF
); 
 125         /* Disable clock division */ 
 126         clock_prescale_set(clock_div_1
); 
 128         /* Hardware Initialization */ 
 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
); 
 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
); 
 141         /* Clear Dataflash sector protections, if enabled */ 
 142         DataflashManager_ResetDataflashProtections(); 
 145 /** Event handler for the library USB Connection event. */ 
 146 void EVENT_USB_Device_Connect(void) 
 148         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
 150         /* Close the log file so that the host has exclusive filesystem access */ 
 151         f_close(&TempLogFile
); 
 154 /** Event handler for the library USB Disconnection event. */ 
 155 void EVENT_USB_Device_Disconnect(void) 
 157         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 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"); 
 166 /** Event handler for the library USB Configuration Changed event. */ 
 167 void EVENT_USB_Device_ConfigurationChanged(void) 
 169         LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
 171         if (!(MS_Device_ConfigureEndpoints(&Disk_MS_Interface
))) 
 172           LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 175 /** Event handler for the library USB Unhandled Control Request event. */ 
 176 void EVENT_USB_Device_UnhandledControlRequest(void) 
 178         MS_Device_ProcessControlRequest(&Disk_MS_Interface
); 
 181 /** Mass Storage class driver callback function the reception of SCSI commands from the host, which must be processed. 
 183  *  \param[in] MSInterfaceInfo  Pointer to the Mass Storage class interface configuration structure being referenced 
 185 bool CALLBACK_MS_Device_SCSICommandReceived(USB_ClassInfo_MS_Device_t
* MSInterfaceInfo
) 
 189         LEDs_SetAllLEDs(LEDMASK_USB_BUSY
); 
 190         CommandSuccess 
= SCSI_DecodeSCSICommand(MSInterfaceInfo
); 
 191         LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
 193         return CommandSuccess
;