#include "TempDataLogger.h"
USB_ClassInfo_MS_Device_t Disk_MS_Interface =
{
.Config =
{
.InterfaceNumber = INTERFACE_ID_MassStorage,
.DataINEndpoint =
{
.Address = MASS_STORAGE_IN_EPADDR,
.Size = MASS_STORAGE_IO_EPSIZE,
.Banks = 1,
},
.DataOUTEndpoint =
{
.Address = MASS_STORAGE_OUT_EPADDR,
.Size = MASS_STORAGE_IO_EPSIZE,
.Banks = 1,
},
.TotalLUNs = 1,
},
};
static uint8_t PrevHIDReportBuffer[GENERIC_REPORT_SIZE];
USB_ClassInfo_HID_Device_t Generic_HID_Interface =
{
.Config =
{
.InterfaceNumber = INTERFACE_ID_HID,
.ReportINEndpoint =
{
.Address = GENERIC_IN_EPADDR,
.Size = GENERIC_EPSIZE,
.Banks = 1,
},
.PrevReportINBuffer = PrevHIDReportBuffer,
.PrevReportINBufferSize = sizeof(PrevHIDReportBuffer),
},
};
static uint8_t EEMEM LoggingInterval500MS_EEPROM = DEFAULT_LOG_INTERVAL;
static uint8_t LoggingInterval500MS_SRAM;
static uint16_t CurrentLoggingTicks;
static FATFS DiskFATState;
static FIL TempLogFile;
ISR(TIMER1_COMPA_vect, ISR_BLOCK)
{
RTC_Tick500ms();
if (++CurrentLoggingTicks < LoggingInterval500MS_SRAM)
return;
CurrentLoggingTicks = 0;
uint8_t LEDMask = LEDs_GetLEDs();
LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
if (USB_DeviceState == DEVICE_STATE_Unattached)
{
TimeDate_t CurrentTimeDate;
RTC_GetTimeDate(&CurrentTimeDate);
char LineBuffer[100];
uint16_t BytesWritten;
BytesWritten = snprintf(LineBuffer, sizeof(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);
}
int main(void)
{
SetupHardware();
LoggingInterval500MS_SRAM = eeprom_read_byte(&LoggingInterval500MS_EEPROM);
if (LoggingInterval500MS_SRAM == 0xFF)
LoggingInterval500MS_SRAM = DEFAULT_LOG_INTERVAL;
OpenLogFile();
LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
GlobalInterruptEnable();
for (;;)
{
MS_Device_USBTask(&Disk_MS_Interface);
HID_Device_USBTask(&Generic_HID_Interface);
USB_USBTask();
}
}
void OpenLogFile(void)
{
char LogFileName[16];
TimeDate_t CurrentTimeDate;
RTC_GetTimeDate(&CurrentTimeDate);
snprintf(LogFileName, sizeof(LogFileName), "%02d%02d%02d.csv", CurrentTimeDate.Day, CurrentTimeDate.Month, CurrentTimeDate.Year);
f_mount(0, &DiskFATState);
f_open(&TempLogFile, LogFileName, FA_OPEN_ALWAYS | FA_WRITE);
f_lseek(&TempLogFile, TempLogFile.fsize);
}
void CloseLogFile(void)
{
f_sync(&TempLogFile);
f_close(&TempLogFile);
}
void SetupHardware(void)
{
#if (ARCH == ARCH_AVR8)
MCUSR &= ~(1 << WDRF);
wdt_disable();
clock_prescale_set(clock_div_1);
#endif
LEDs_Init();
ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_128);
Temperature_Init();
Dataflash_Init();
USB_Init();
TWI_Init(TWI_BIT_PRESCALE_4, TWI_BITLENGTH_FROM_FREQ(4, 50000));
RTC_Init();
OCR1A = (((F_CPU / 256) / 2) - 1);
TCCR1B = (1 << WGM12) | (1 << CS12);
TIMSK1 = (1 << OCIE1A);
if (!(DataflashManager_CheckDataflashOperation()))
{
LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
for(;;);
}
DataflashManager_ResetDataflashProtections();
}
void EVENT_USB_Device_Connect(void)
{
LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
CloseLogFile();
}
void EVENT_USB_Device_Disconnect(void)
{
LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
OpenLogFile();
}
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);
}
void EVENT_USB_Device_ControlRequest(void)
{
MS_Device_ProcessControlRequest(&Disk_MS_Interface);
HID_Device_ProcessControlRequest(&Generic_HID_Interface);
}
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;
}
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;
RTC_GetTimeDate(&ReportParams->TimeDate);
ReportParams->LogInterval500MS = LoggingInterval500MS_SRAM;
*ReportSize = sizeof(Device_Report_t);
return true;
}
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;
RTC_SetTimeDate(&ReportParams->TimeDate);
if (LoggingInterval500MS_SRAM != ReportParams->LogInterval500MS)
{
LoggingInterval500MS_SRAM = ReportParams->LogInterval500MS;
eeprom_update_byte(&LoggingInterval500MS_EEPROM, LoggingInterval500MS_SRAM);
}
}