# Determine what keyboard we are building and setup the build environment. # # We support folders up to 5 levels deep below `keyboards/`. This file is # responsible for determining which folder is being used and doing the # corresponding environment setup. ifndef VERBOSE .SILENT: endif .DEFAULT_GOAL := all include common.mk # Set the filename for the final firmware binary KEYBOARD_FILESAFE := $(subst /,_,$(KEYBOARD)) TARGET ?= $(KEYBOARD_FILESAFE)_$(KEYMAP) KEYBOARD_OUTPUT := $(BUILD_DIR)/obj_$(KEYBOARD_FILESAFE) STM32_PATH := quantum/stm32 # Force expansion TARGET := $(TARGET) # For split boards we need to set a master half. MASTER ?= left ifdef master MASTER = $(master) endif ifeq ($(MASTER),right) OPT_DEFS += -DMASTER_IS_ON_RIGHT else ifneq ($(MASTER),left) $(error MASTER does not have a valid value(left/right)) endif endif ifdef SKIP_VERSION OPT_DEFS += -DSKIP_VERSION endif # Determine which subfolders exist. KEYBOARD_FOLDER_PATH_1 := $(KEYBOARD) KEYBOARD_FOLDER_PATH_2 := $(patsubst %/,%,$(dir $(KEYBOARD_FOLDER_PATH_1))) KEYBOARD_FOLDER_PATH_3 := $(patsubst %/,%,$(dir $(KEYBOARD_FOLDER_PATH_2))) KEYBOARD_FOLDER_PATH_4 := $(patsubst %/,%,$(dir $(KEYBOARD_FOLDER_PATH_3))) KEYBOARD_FOLDER_PATH_5 := $(patsubst %/,%,$(dir $(KEYBOARD_FOLDER_PATH_4))) KEYBOARD_FOLDER_1 := $(notdir $(KEYBOARD_FOLDER_PATH_1)) KEYBOARD_FOLDER_2 := $(notdir $(KEYBOARD_FOLDER_PATH_2)) KEYBOARD_FOLDER_3 := $(notdir $(KEYBOARD_FOLDER_PATH_3)) KEYBOARD_FOLDER_4 := $(notdir $(KEYBOARD_FOLDER_PATH_4)) KEYBOARD_FOLDER_5 := $(notdir $(KEYBOARD_FOLDER_PATH_5)) KEYBOARD_PATHS := KEYBOARD_PATH_1 := keyboards/$(KEYBOARD_FOLDER_PATH_1) KEYBOARD_PATH_2 := keyboards/$(KEYBOARD_FOLDER_PATH_2) KEYBOARD_PATH_3 := keyboards/$(KEYBOARD_FOLDER_PATH_3) KEYBOARD_PATH_4 := keyboards/$(KEYBOARD_FOLDER_PATH_4) KEYBOARD_PATH_5 := keyboards/$(KEYBOARD_FOLDER_PATH_5) ifneq ("$(wildcard $(KEYBOARD_PATH_5)/)","") KEYBOARD_PATHS += $(KEYBOARD_PATH_5) endif ifneq ("$(wildcard $(KEYBOARD_PATH_4)/)","") KEYBOARD_PATHS += $(KEYBOARD_PATH_4) endif ifneq ("$(wildcard $(KEYBOARD_PATH_3)/)","") KEYBOARD_PATHS += $(KEYBOARD_PATH_3) endif ifneq ("$(wildcard $(KEYBOARD_PATH_2)/)","") KEYBOARD_PATHS += $(KEYBOARD_PATH_2) endif ifneq ("$(wildcard $(KEYBOARD_PATH_1)/)","") KEYBOARD_PATHS += $(KEYBOARD_PATH_1) endif # Pull in rules.mk files from all our subfolders ifneq ("$(wildcard $(KEYBOARD_PATH_5)/rules.mk)","") include $(KEYBOARD_PATH_5)/rules.mk endif ifneq ("$(wildcard $(KEYBOARD_PATH_4)/rules.mk)","") include $(KEYBOARD_PATH_4)/rules.mk endif ifneq ("$(wildcard $(KEYBOARD_PATH_3)/rules.mk)","") include $(KEYBOARD_PATH_3)/rules.mk endif ifneq ("$(wildcard $(KEYBOARD_PATH_2)/rules.mk)","") include $(KEYBOARD_PATH_2)/rules.mk endif ifneq ("$(wildcard $(KEYBOARD_PATH_1)/rules.mk)","") include $(KEYBOARD_PATH_1)/rules.mk endif MAIN_KEYMAP_PATH_1 := $(KEYBOARD_PATH_1)/keymaps/$(KEYMAP) MAIN_KEYMAP_PATH_2 := $(KEYBOARD_PATH_2)/keymaps/$(KEYMAP) MAIN_KEYMAP_PATH_3 := $(KEYBOARD_PATH_3)/keymaps/$(KEYMAP) MAIN_KEYMAP_PATH_4 := $(KEYBOARD_PATH_4)/keymaps/$(KEYMAP) MAIN_KEYMAP_PATH_5 := $(KEYBOARD_PATH_5)/keymaps/$(KEYMAP) # Check for keymap.json first, so we can regenerate keymap.c include build_json.mk ifeq ("$(wildcard $(KEYMAP_PATH))", "") # Look through the possible keymap folders until we find a matching keymap.c ifneq ("$(wildcard $(MAIN_KEYMAP_PATH_5)/keymap.c)","") -include $(MAIN_KEYMAP_PATH_5)/rules.mk KEYMAP_C := $(MAIN_KEYMAP_PATH_5)/keymap.c KEYMAP_PATH := $(MAIN_KEYMAP_PATH_5) else ifneq ("$(wildcard $(MAIN_KEYMAP_PATH_4)/keymap.c)","") -include $(MAIN_KEYMAP_PATH_4)/rules.mk KEYMAP_C := $(MAIN_KEYMAP_PATH_4)/keymap.c KEYMAP_PATH := $(MAIN_KEYMAP_PATH_4) else ifneq ("$(wildcard $(MAIN_KEYMAP_PATH_3)/keymap.c)","") -include $(MAIN_KEYMAP_PATH_3)/rules.mk KEYMAP_C := $(MAIN_KEYMAP_PATH_3)/keymap.c KEYMAP_PATH := $(MAIN_KEYMAP_PATH_3) else ifneq ("$(wildcard $(MAIN_KEYMAP_PATH_2)/keymap.c)","") -include $(MAIN_KEYMAP_PATH_2)/rules.mk KEYMAP_C := $(MAIN_KEYMAP_PATH_2)/keymap.c KEYMAP_PATH := $(MAIN_KEYMAP_PATH_2) else ifneq ("$(wildcard $(MAIN_KEYMAP_PATH_1)/keymap.c)","") -include $(MAIN_KEYMAP_PATH_1)/rules.mk KEYMAP_C := $(MAIN_KEYMAP_PATH_1)/keymap.c KEYMAP_PATH := $(MAIN_KEYMAP_PATH_1) else ifneq ($(LAYOUTS),) # If we haven't found a keymap yet fall back to community layouts include build_layout.mk else $(error Could not find keymap) # this state should never be reached endif endif ifeq ($(strip $(CTPC)), yes) CONVERT_TO_PROTON_C=yes endif ifeq ($(strip $(CONVERT_TO_PROTON_C)), yes) TARGET := $(TARGET)_proton_c include $(STM32_PATH)/proton_c.mk OPT_DEFS += -DCONVERT_TO_PROTON_C endif ifneq ($(FORCE_LAYOUT),) TARGET := $(TARGET)_$(FORCE_LAYOUT) endif include quantum/mcu_selection.mk ifdef MCU_FAMILY OPT_DEFS += -DQMK_STM32 KEYBOARD_PATHS += $(STM32_PATH) endif # Find all the C source files to be compiled in subfolders. KEYBOARD_SRC := KEYBOARD_C_1 := $(KEYBOARD_PATH_1)/$(KEYBOARD_FOLDER_1).c KEYBOARD_C_2 := $(KEYBOARD_PATH_2)/$(KEYBOARD_FOLDER_2).c KEYBOARD_C_3 := $(KEYBOARD_PATH_3)/$(KEYBOARD_FOLDER_3).c KEYBOARD_C_4 := $(KEYBOARD_PATH_4)/$(KEYBOARD_FOLDER_4).c KEYBOARD_C_5 := $(KEYBOARD_PATH_5)/$(KEYBOARD_FOLDER_5).c ifneq ("$(wildcard $(KEYBOARD_C_5))","") KEYBOARD_SRC += $(KEYBOARD_C_5) endif ifneq ("$(wildcard $(KEYBOARD_C_4))","") KEYBOARD_SRC += $(KEYBOARD_C_4) endif ifneq ("$(wildcard $(KEYBOARD_C_3))","") KEYBOARD_SRC += $(KEYBOARD_C_3) endif ifneq ("$(wildcard $(KEYBOARD_C_2))","") KEYBOARD_SRC += $(KEYBOARD_C_2) endif ifneq ("$(wildcard $(KEYBOARD_C_1))","") KEYBOARD_SRC += $(KEYBOARD_C_1) endif # Generate KEYBOARD_name_subname for all levels of the keyboard folder KEYBOARD_FILESAFE_1 := $(subst .,,$(subst /,_,$(KEYBOARD_FOLDER_PATH_1))) KEYBOARD_FILESAFE_2 := $(subst .,,$(subst /,_,$(KEYBOARD_FOLDER_PATH_2))) KEYBOARD_FILESAFE_3 := $(subst .,,$(subst /,_,$(KEYBOARD_FOLDER_PATH_3))) KEYBOARD_FILESAFE_4 := $(subst .,,$(subst /,_,$(KEYBOARD_FOLDER_PATH_4))) KEYBOARD_FILESAFE_5 := $(subst .,,$(subst /,_,$(KEYBOARD_FOLDER_PATH_5))) ifneq ("$(wildcard $(KEYBOARD_PATH_5)/)","") OPT_DEFS += -DKEYBOARD_$(KEYBOARD_FILESAFE_5) endif ifneq ("$(wildcard $(KEYBOARD_PATH_4)/)","") OPT_DEFS += -DKEYBOARD_$(KEYBOARD_FILESAFE_4) endif ifneq ("$(wildcard $(KEYBOARD_PATH_3)/)","") OPT_DEFS += -DKEYBOARD_$(KEYBOARD_FILESAFE_3) endif ifneq ("$(wildcard $(KEYBOARD_PATH_2)/)","") OPT_DEFS += -DKEYBOARD_$(KEYBOARD_FILESAFE_2) endif ifneq ("$(wildcard $(KEYBOARD_PATH_1)/)","") OPT_DEFS += -DKEYBOARD_$(KEYBOARD_FILESAFE_1) endif # Setup the define for QMK_KEYBOARD_H. This is used inside of keymaps so # that the same keymap may be used on multiple keyboards. # # We grab the most top-level include file that we can. That file should # use #ifdef statements to include all the neccesary subfolder includes, # as described here: # # https://docs.qmk.fm/#/feature_layouts?id=tips-for-making-layouts-keyboard-agnostic # ifneq ("$(wildcard $(KEYBOARD_PATH_1)/$(KEYBOARD_FOLDER_1).h)","") QMK_KEYBOARD_H = $(KEYBOARD_FOLDER_1).h endif ifneq ("$(wildcard $(KEYBOARD_PATH_2)/$(KEYBOARD_FOLDER_2).h)","") QMK_KEYBOARD_H = $(KEYBOARD_FOLDER_2).h endif ifneq ("$(wildcard $(KEYBOARD_PATH_3)/$(KEYBOARD_FOLDER_3).h)","") QMK_KEYBOARD_H = $(KEYBOARD_FOLDER_3).h endif ifneq ("$(wildcard $(KEYBOARD_PATH_4)/$(KEYBOARD_FOLDER_4).h)","") QMK_KEYBOARD_H = $(KEYBOARD_FOLDER_4).h endif ifneq ("$(wildcard $(KEYBOARD_PATH_5)/$(KEYBOARD_FOLDER_5).h)","") QMK_KEYBOARD_H = $(KEYBOARD_FOLDER_5).h endif # Determine and set parameters based on the keyboard's processor family. # We can assume a ChibiOS target When MCU_FAMILY is defined since it's # not used for LUFA ifdef MCU_FAMILY PLATFORM=CHIBIOS PLATFORM_KEY=chibios FIRMWARE_FORMAT?=bin else ifdef ARM_ATSAM PLATFORM=ARM_ATSAM PLATFORM_KEY=arm_atsam FIRMWARE_FORMAT=bin else PLATFORM=AVR PLATFORM_KEY=avr FIRMWARE_FORMAT?=hex endif # Find all of the config.h files and add them to our CONFIG_H define. CONFIG_H := ifneq ("$(wildcard $(KEYBOARD_PATH_5)/config.h)","") CONFIG_H += $(KEYBOARD_PATH_5)/config.h endif ifneq ("$(wildcard $(KEYBOARD_PATH_4)/config.h)","") CONFIG_H += $(KEYBOARD_PATH_4)/config.h endif ifneq ("$(wildcard $(KEYBOARD_PATH_3)/config.h)","") CONFIG_H += $(KEYBOARD_PATH_3)/config.h endif ifneq ("$(wildcard $(KEYBOARD_PATH_2)/config.h)","") CONFIG_H += $(KEYBOARD_PATH_2)/config.h endif ifneq ("$(wildcard $(KEYBOARD_PATH_1)/config.h)","") CONFIG_H += $(KEYBOARD_PATH_1)/config.h endif POST_CONFIG_H := ifneq ("$(wildcard $(KEYBOARD_PATH_1)/post_config.h)","") POST_CONFIG_H += $(KEYBOARD_PATH_1)/post_config.h endif ifneq ("$(wildcard $(KEYBOARD_PATH_2)/post_config.h)","") POST_CONFIG_H += $(KEYBOARD_PATH_2)/post_config.h endif ifneq ("$(wildcard $(KEYBOARD_PATH_3)/post_config.h)","") POST_CONFIG_H += $(KEYBOARD_PATH_3)/post_config.h endif ifneq ("$(wildcard $(KEYBOARD_PATH_4)/post_config.h)","") POST_CONFIG_H += $(KEYBOARD_PATH_4)/post_config.h endif ifneq ("$(wildcard $(KEYBOARD_PATH_5)/post_config.h)","") POST_CONFIG_H += $(KEYBOARD_PATH_5)/post_config.h endif # Userspace setup and definitions ifeq ("$(USER_NAME)","") USER_NAME := $(KEYMAP) endif USER_PATH := users/$(USER_NAME) -include $(USER_PATH)/rules.mk ifneq ("$(wildcard $(USER_PATH)/config.h)","") CONFIG_H += $(USER_PATH)/config.h endif # Object files directory # To put object files in current directory, use a dot (.), do NOT make # this an empty or blank macro! KEYMAP_OUTPUT := $(BUILD_DIR)/obj_$(TARGET) ifneq ("$(wildcard $(KEYMAP_PATH)/config.h)","") CONFIG_H += $(KEYMAP_PATH)/config.h endif # project specific files SRC += $(KEYBOARD_SRC) \ $(KEYMAP_C) \ $(QUANTUM_SRC) # Optimize size but this may cause error "relocation truncated to fit" #EXTRALDFLAGS = -Wl,--relax # Search Path VPATH += $(KEYMAP_PATH) VPATH += $(USER_PATH) VPATH += $(KEYBOARD_PATHS) VPATH += $(COMMON_VPATH) include common_features.mk include $(TMK_PATH)/protocol.mk include $(TMK_PATH)/common.mk include bootloader.mk SRC += $(patsubst %.c,%.clib,$(LIB_SRC)) SRC += $(patsubst %.c,%.clib,$(QUANTUM_LIB_SRC)) SRC += $(TMK_COMMON_SRC) OPT_DEFS += $(TMK_COMMON_DEFS) EXTRALDFLAGS += $(TMK_COMMON_LDFLAGS) include $(TMK_PATH)/$(PLATFORM_KEY).mk ifneq ($(strip $(PROTOCOL)),) include $(TMK_PATH)/protocol/$(strip $(shell echo $(PROTOCOL) | tr '[:upper:]' '[:lower:]')).mk else include $(TMK_PATH)/protocol/$(PLATFORM_KEY).mk endif # TODO: remove this bodge? PROJECT_DEFS := $(OPT_DEFS) PROJECT_INC := $(VPATH) $(EXTRAINCDIRS) $(KEYBOARD_PATHS) PROJECT_CONFIG := $(CONFIG_H) ifeq ($(strip $(VISUALIZER_ENABLE)), yes) VISUALIZER_DIR = $(QUANTUM_DIR)/visualizer VISUALIZER_PATH = $(QUANTUM_PATH)/visualizer include $(VISUALIZER_PATH)/visualizer.mk endif CONFIG_H += $(POST_CONFIG_H) ALL_CONFIGS := $(PROJECT_CONFIG) $(CONFIG_H) OUTPUTS := $(KEYMAP_OUTPUT) $(KEYBOARD_OUTPUT) $(KEYMAP_OUTPUT)_SRC := $(SRC) $(KEYMAP_OUTPUT)_DEFS := $(OPT_DEFS) $(GFXDEFS) \ -DQMK_KEYBOARD=\"$(KEYBOARD)\" -DQMK_KEYBOARD_H=\"$(QMK_KEYBOARD_H)\" -DQMK_KEYBOARD_CONFIG_H=\"$(KEYBOARD_PATH_1)/config.h\" \ -DQMK_KEYMAP=\"$(KEYMAP)\" -DQMK_KEYMAP_H=\"$(KEYMAP).h\" -DQMK_KEYMAP_CONFIG_H=\"$(KEYMAP_PATH)/config.h\" \ -DQMK_SUBPROJECT -DQMK_SUBPROJECT_H -DQMK_SUBPROJECT_CONFIG_H $(KEYMAP_OUTPUT)_INC := $(VPATH) $(EXTRAINCDIRS) $(KEYMAP_OUTPUT)_CONFIG := $(CONFIG_H) $(KEYBOARD_OUTPUT)_SRC := $(CHIBISRC) $(GFXSRC) $(KEYBOARD_OUTPUT)_DEFS := $(PROJECT_DEFS) $(GFXDEFS) $(KEYBOARD_OUTPUT)_INC := $(PROJECT_INC) $(GFXINC) $(KEYBOARD_OUTPUT)_CONFIG := $(PROJECT_CONFIG) # Default target. all: build check-size build: elf cpfirmware check-size: build objs-size: build include show_options.mk include $(TMK_PATH)/rules.mk ef='#n270'>270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
/*
             LUFA Library
     Copyright (C) Dean Camera, 2011.

  dean [at] fourwalledcubicle [dot] com
           www.lufa-lib.org
*/

/*
  Copyright 2011  Dean Camera (dean [at] fourwalledcubicle [dot] com)

  Permission to use, copy, modify, distribute, and sell this
  software and its documentation for any purpose is hereby granted
  without fee, provided that the above copyright notice appear in
  all copies and that both that the copyright notice and this
  permission notice and warranty disclaimer appear in supporting
  documentation, and that the name of the author not be used in
  advertising or publicity pertaining to distribution of the
  software without specific, written prior permission.

  The author disclaim all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

/** \file
 *
 *  Main source file for the TemperatureDataLogger project. This file contains the main tasks of
 *  the project and is responsible for the initial application hardware configuration.
 */

#include "TempDataLogger.h"

/** LUFA Mass Storage Class driver interface configuration and state information. This structure is
 *  passed to all Mass Storage Class driver functions, so that multiple instances of the same class
 *  within a device can be differentiated from one another.
 */
USB_ClassInfo_MS_Device_t Disk_MS_Interface =
	{
		.Config =
			{
				.InterfaceNumber           = 0,

				.DataINEndpointNumber      = MASS_STORAGE_IN_EPNUM,
				.DataINEndpointSize        = MASS_STORAGE_IO_EPSIZE,
				.DataINEndpointDoubleBank  = false,

				.DataOUTEndpointNumber     = MASS_STORAGE_OUT_EPNUM,
				.DataOUTEndpointSize       = MASS_STORAGE_IO_EPSIZE,
				.DataOUTEndpointDoubleBank = false,

				.TotalLUNs                 = 1,
			},
	};

/** Buffer to hold the previously generated HID report, for comparison purposes inside the HID class driver. */
static uint8_t PrevHIDReportBuffer[GENERIC_REPORT_SIZE];

/** LUFA HID Class driver interface configuration and state information. This structure is
 *  passed to all HID Class driver functions, so that multiple instances of the same class
 *  within a device can be differentiated from one another.
 */
USB_ClassInfo_HID_Device_t Generic_HID_Interface =
	{
		.Config =
			{
				.InterfaceNumber              = 1,

				.ReportINEndpointNumber       = GENERIC_IN_EPNUM,
				.ReportINEndpointSize         = GENERIC_EPSIZE,
				.ReportINEndpointDoubleBank   = false,

				.PrevReportINBuffer           = PrevHIDReportBuffer,
				.PrevReportINBufferSize       = sizeof(PrevHIDReportBuffer),
			},
	};

/** Non-volatile Logging Interval value in EEPROM, stored as a number of 500ms ticks */
static uint8_t EEMEM LoggingInterval500MS_EEPROM = DEFAULT_LOG_INTERVAL;

/** SRAM Logging Interval value fetched from EEPROM, stored as a number of 500ms ticks */
static uint8_t LoggingInterval500MS_SRAM;

/** Total number of 500ms logging ticks elapsed since the last log value was recorded */
static uint16_t CurrentLoggingTicks;

/** FAT Fs structure to hold the internal state of the FAT driver for the Dataflash contents. */
static FATFS DiskFATState;

/** FAT Fs structure to hold a FAT file handle for the log data write destination. */
static FIL TempLogFile;


/** ISR to handle the 500ms ticks for sampling and data logging */
ISR(TIMER1_COMPA_vect, ISR_BLOCK)
{
	uint8_t LEDMask = LEDs_GetLEDs();

	/* Check to see if the logging interval has expired */
	if (CurrentLoggingTicks++ < LoggingInterval500MS_SRAM)
	  return;

	/* Reset log tick counter to prepare for next logging interval */
	CurrentLoggingTicks = 0;

	LEDs_SetAllLEDs(LEDMASK_USB_BUSY);

	/* Only log when not connected to a USB host */
	if (USB_DeviceState == DEVICE_STATE_Unattached)
	{
		TimeDate_t CurrentTimeDate;
		DS1307_GetTimeDate(&CurrentTimeDate);

		char     LineBuffer[100];
		uint16_t BytesWritten;

		BytesWritten = sprintf(LineBuffer, "%02d/%02d/20%02d, %02d:%02d:%02d, %d Degrees\r\n",
		                       CurrentTimeDate.Day, CurrentTimeDate.Month, CurrentTimeDate.Year,
		                       CurrentTimeDate.Hour, CurrentTimeDate.Minute, CurrentTimeDate.Second,
		                       Temperature_GetTemperature());

		f_write(&TempLogFile, LineBuffer, BytesWritten, &BytesWritten);
		f_sync(&TempLogFile);
	}

	LEDs_SetAllLEDs(LEDMask);
}

/** Main program entry point. This routine contains the overall program flow, including initial
 *  setup of all components and the main program loop.
 */
int main(void)
{
	SetupHardware();

	/* Fetch logging interval from EEPROM */
	LoggingInterval500MS_SRAM = eeprom_read_byte(&LoggingInterval500MS_EEPROM);

	/* Check if the logging interval is invalid (0xFF) indicating that the EEPROM is blank */
	if (LoggingInterval500MS_SRAM == 0xFF)
	  LoggingInterval500MS_SRAM = DEFAULT_LOG_INTERVAL;

	/* Mount and open the log file on the Dataflash FAT partition */
	OpenLogFile();

	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
	sei();

	for (;;)
	{
		MS_Device_USBTask(&Disk_MS_Interface);
		HID_Device_USBTask(&Generic_HID_Interface);
		USB_USBTask();
	}
}

/** Opens the log file on the Dataflash's FAT formatted partition according to the current date */
void OpenLogFile(void)
{
	char LogFileName[12];

	/* Get the current date for the filename as "DDMMYY.csv" */
	TimeDate_t CurrentTimeDate;
	DS1307_GetTimeDate(&CurrentTimeDate);
	sprintf(LogFileName, "%02d%02d%02d.csv", CurrentTimeDate.Day, CurrentTimeDate.Month, CurrentTimeDate.Year);

	/* Mount the storage device, open the file */
	f_mount(0, &DiskFATState);
	f_open(&TempLogFile, LogFileName, FA_OPEN_ALWAYS | FA_WRITE);
	f_lseek(&TempLogFile, TempLogFile.fsize);
}

/** Closes the open data log file on the Dataflash's FAT formatted partition */
void CloseLogFile(void)
{
	/* Sync any data waiting to be written, unmount the storage device */
	f_sync(&TempLogFile);
	f_close(&TempLogFile);
}

/** Configures the board hardware and chip peripherals for the demo's functionality. */
void SetupHardware(void)
{
	/* Disable watchdog if enabled by bootloader/fuses */
	MCUSR &= ~(1 << WDRF);
	wdt_disable();

	/* Disable clock division */
	clock_prescale_set(clock_div_1);

	/* Hardware Initialization */
	LEDs_Init();
	SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_SCK_LEAD_FALLING | SPI_SAMPLE_TRAILING | SPI_MODE_MASTER);
	ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_128);
	Temperature_Init();
	Dataflash_Init();
	USB_Init();
	TWI_Init(TWI_BIT_PRESCALE_4, (F_CPU / 4 / 50000) / 2);

	/* 500ms logging interval timer configuration */
	OCR1A   = (((F_CPU / 1024) / 2) - 1);
	TCCR1B  = (1 << WGM12) | (1 << CS12) | (1 << CS10);
	TIMSK1  = (1 << OCIE1A);

	/* Clear Dataflash sector protections, if enabled */
	DataflashManager_ResetDataflashProtections();
}

/** Event handler for the library USB Connection event. */
void EVENT_USB_Device_Connect(void)
{
	LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);

	/* Close the log file so that the host has exclusive filesystem access */
	CloseLogFile();
}

/** Event handler for the library USB Disconnection event. */
void EVENT_USB_Device_Disconnect(void)
{
	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);

	/* Mount and open the log file on the Dataflash FAT partition */
	OpenLogFile();
}

/** Event handler for the library USB Configuration Changed event. */
void EVENT_USB_Device_ConfigurationChanged(void)
{
	bool ConfigSuccess = true;

	ConfigSuccess &= HID_Device_ConfigureEndpoints(&Generic_HID_Interface);
	ConfigSuccess &= MS_Device_ConfigureEndpoints(&Disk_MS_Interface);

	LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
}

/** Event handler for the library USB Control Request reception event. */
void EVENT_USB_Device_ControlRequest(void)
{
	MS_Device_ProcessControlRequest(&Disk_MS_Interface);
	HID_Device_ProcessControlRequest(&Generic_HID_Interface);
}

/** Mass Storage class driver callback function the reception of SCSI commands from the host, which must be processed.
 *
 *  \param[in] MSInterfaceInfo  Pointer to the Mass Storage class interface configuration structure being referenced
 */
bool CALLBACK_MS_Device_SCSICommandReceived(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
{
	bool CommandSuccess;

	LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
	CommandSuccess = SCSI_DecodeSCSICommand(MSInterfaceInfo);
	LEDs_SetAllLEDs(LEDMASK_USB_READY);

	return CommandSuccess;
}

/** HID class driver callback function for the creation of HID reports to the host.
 *
 *  \param[in]     HIDInterfaceInfo  Pointer to the HID class interface configuration structure being referenced
 *  \param[in,out] ReportID    Report ID requested by the host if non-zero, otherwise callback should set to the
 *                             generated report ID
 *  \param[in]     ReportType  Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
 *  \param[out]    ReportData  Pointer to a buffer where the created report should be stored
 *  \param[out]    ReportSize  Number of bytes written in the report (or zero if no report is to be sent
 *
 *  \return Boolean true to force the sending of the report, false to let the library determine if it needs to be sent
 */
bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
                                         uint8_t* const ReportID,
                                         const uint8_t ReportType,
                                         void* ReportData,
                                         uint16_t* const ReportSize)
{
	Device_Report_t* ReportParams = (Device_Report_t*)ReportData;

	DS1307_GetTimeDate(&ReportParams->TimeDate);

	ReportParams->LogInterval500MS = LoggingInterval500MS_SRAM;

	*ReportSize = sizeof(Device_Report_t);
	return true;
}

/** HID class driver callback function for the processing of HID reports from the host.
 *
 *  \param[in] HIDInterfaceInfo  Pointer to the HID class interface configuration structure being referenced
 *  \param[in] ReportID    Report ID of the received report from the host
 *  \param[in] ReportType  The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
 *  \param[in] ReportData  Pointer to a buffer where the received report has been stored
 *  \param[in] ReportSize  Size in bytes of the received HID report
 */
void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
                                          const uint8_t ReportID,
                                          const uint8_t ReportType,
                                          const void* ReportData,
                                          const uint16_t ReportSize)
{
	Device_Report_t* ReportParams = (Device_Report_t*)ReportData;
	
	DS1307_SetTimeDate(&ReportParams->TimeDate);

	/* If the logging interval has changed from its current value, write it to EEPROM */
	if (LoggingInterval500MS_SRAM != ReportParams->LogInterval500MS)
	{
		LoggingInterval500MS_SRAM = ReportParams->LogInterval500MS;
		eeprom_update_byte(&LoggingInterval500MS_EEPROM, LoggingInterval500MS_SRAM);
	}
}