Commit dd995683 authored by Dean Camera's avatar Dean Camera
Browse files

Enhance TemperatureDatalogger project -- add RTC capabilities so that data is...

Enhance TemperatureDatalogger project -- add RTC capabilities so that data is logged along with the current time and date. Make logging interval configurable, set by a C# PC host application.
parent 03ee87b3
This diff is collapsed.
......@@ -17,6 +17,8 @@
* -# Add ability to get number of bytes not written with pipe/endpoint write routines after an error
* -# Add standardized descriptor names to class driver structures
* -# Correct mishandling of error cases in Mass Storage demos
* -# FIX BROKEN RNDIS HOST CLASS DRIVER (BLOCKING)
* -# TEST AND CORRECT TPI PROGRAMMING SUPPORT IN THE AVRISP-MKII PROJECT (BLOCKING)
* - Documentation/Support
* -# Remake AVRStudio project files
* -# Add detailed overviews of how each demo works
......@@ -32,6 +34,7 @@
* -# Finish MIDI class Bootloader
* -# Finish SideShow demo
* -# Finish StandaloneProgrammer project
* -# Finish Webserver project
* - Ports
* -# AVR32 UC3B series microcontrollers
* -# Atmel ARM7 series microcontrollers
......
......@@ -265,7 +265,7 @@
* 0x2063
* </td>
* <td>
* <i>Currently Unallocated</i>
* Mass Storage/HID Interface Datalogger Project
* </td>
* </tr>
*
......
......@@ -197,6 +197,7 @@ CDEFS += -DAUX_LINE_MASK="(1 << 4)"
CDEFS += -DVTARGET_ADC_CHANNEL=2
CDEFS += -DENABLE_ISP_PROTOCOL
CDEFS += -DENABLE_XPROG_PROTOCOL
#CDEFS += -DXPROG_VIA_HARDWARE_USART
# Place -D or -U options here for ASM sources
......
......@@ -49,6 +49,32 @@
#warning USE_INTERNAL_SERIAL is not available on this AVR - please manually construct a device serial descriptor.
#endif
/** HID class report descriptor. This is a special descriptor constructed with values from the
* USBIF HID class specification to describe the reports and capabilities of the HID device. This
* descriptor is parsed by the host and its contents used to determine what data (and in what encoding)
* the device will send, and what it may be sent back from the host. Refer to the HID specification for
* more details on HID report descriptors.
*/
USB_Descriptor_HIDReport_Datatype_t PROGMEM GenericReport[] =
{
0x06, 0x9c, 0xff, /* Usage Page (Vendor Defined) */
0x09, 0x01, /* Usage (Vendor Defined) */
0xa1, 0x01, /* Collection (Vendor Defined) */
0x09, 0x02, /* Usage (Vendor Defined) */
0x75, 0x08, /* Report Size (8) */
0x95, GENERIC_REPORT_SIZE, /* Report Count (GENERIC_REPORT_SIZE) */
0x15, 0x80, /* Logical Minimum (-128) */
0x25, 0x7F, /* Logical Maximum (127) */
0x81, 0x02, /* Input (Data, Variable, Absolute) */
0x09, 0x03, /* Usage (Vendor Defined) */
0x75, 0x08, /* Report Size (8) */
0x95, GENERIC_REPORT_SIZE, /* Report Count (GENERIC_REPORT_SIZE) */
0x15, 0x00, /* Logical Minimum (0) */
0x25, 0xff, /* Logical Maximum (255) */
0x91, 0x02, /* Output (Data, Variable, Absolute) */
0xc0 /* End Collection */
};
/** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
* device characteristics, including the supported USB version, control endpoint size and the
* number of device configurations. The descriptor is read out by the USB host when the enumeration
......@@ -66,7 +92,7 @@ USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
.Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
.VendorID = 0x03EB,
.ProductID = 0x2045,
.ProductID = 0x2063,
.ReleaseNumber = 0x0000,
.ManufacturerStrIndex = 0x01,
......@@ -88,7 +114,7 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
.Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
.TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
.TotalInterfaces = 1,
.TotalInterfaces = 2,
.ConfigurationNumber = 1,
.ConfigurationStrIndex = NO_DESCRIPTOR,
......@@ -98,7 +124,7 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
.MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
},
.Interface =
.MSInterface =
{
.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
......@@ -114,7 +140,7 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
.InterfaceStrIndex = NO_DESCRIPTOR
},
.DataInEndpoint =
.MSDataInEndpoint =
{
.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
......@@ -124,7 +150,7 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
.PollingIntervalMS = 0x00
},
.DataOutEndpoint =
.MSDataOutEndpoint =
{
.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
......@@ -132,7 +158,44 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
.Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = MASS_STORAGE_IO_EPSIZE,
.PollingIntervalMS = 0x00
}
},
.HIDInterface =
{
.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
.InterfaceNumber = 1,
.AlternateSetting = 0,
.TotalEndpoints = 1,
.Class = 0x03,
.SubClass = 0x00,
.Protocol = HID_NON_BOOT_PROTOCOL,
.InterfaceStrIndex = NO_DESCRIPTOR
},
.HIDInfo =
{
.Header = {.Size = sizeof(USB_HID_Descriptor_t), .Type = DTYPE_HID},
.HIDSpec = VERSION_BCD(01.11),
.CountryCode = 0x00,
.TotalReportDescriptors = 1,
.HIDReportType = DTYPE_Report,
.HIDReportLength = sizeof(GenericReport)
},
.HIDDataInEndpoint =
{
.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
.EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_IN | GENERIC_IN_EPNUM),
.Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = GENERIC_EPSIZE,
.PollingIntervalMS = 0x0A
},
};
/** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
......@@ -163,9 +226,9 @@ USB_Descriptor_String_t PROGMEM ManufacturerString =
*/
USB_Descriptor_String_t PROGMEM ProductString =
{
.Header = {.Size = USB_STRING_LEN(22), .Type = DTYPE_String},
.Header = {.Size = USB_STRING_LEN(10), .Type = DTYPE_String},
.UnicodeString = L"LUFA Mass Storage Demo"
.UnicodeString = L"Datalogger"
};
/** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
......@@ -209,6 +272,14 @@ uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, const uint8_t wIndex,
break;
}
break;
case DTYPE_HID:
Address = (void*)&ConfigurationDescriptor.HIDInfo;
Size = sizeof(USB_HID_Descriptor_t);
break;
case DTYPE_Report:
Address = (void*)&GenericReport;
Size = sizeof(GenericReport);
break;
}
......
......@@ -5,33 +5,6 @@
dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com
*/
/*
Copyright 2010 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
*
* Header file for Descriptors.c.
*/
#ifndef _DESCRIPTORS_H_
#define _DESCRIPTORS_H_
......@@ -41,6 +14,9 @@
#include <LUFA/Drivers/USB/USB.h>
#include <LUFA/Drivers/USB/Class/MassStorage.h>
#include <LUFA/Drivers/USB/Class/HID.h>
#include "TempDataLogger.h"
/* Macros: */
/** Endpoint number of the Mass Storage device-to-host data IN endpoint. */
......@@ -51,6 +27,15 @@
/** Size in bytes of the Mass Storage data endpoints. */
#define MASS_STORAGE_IO_EPSIZE 64
/** Endpoint number of the Generic HID reporting IN endpoint. */
#define GENERIC_IN_EPNUM 1
/** Size in bytes of the Generic HID reporting endpoint. */
#define GENERIC_EPSIZE 16
/** Size in bytes of the Generic HID reports (including report ID byte). */
#define GENERIC_REPORT_SIZE sizeof(Device_Report_t)
/* Type Defines: */
/** Type define for the device configuration descriptor structure. This must be defined in the
......@@ -60,9 +45,12 @@
typedef struct
{
USB_Descriptor_Configuration_Header_t Config;
USB_Descriptor_Interface_t Interface;
USB_Descriptor_Endpoint_t DataInEndpoint;
USB_Descriptor_Endpoint_t DataOutEndpoint;
USB_Descriptor_Interface_t MSInterface;
USB_Descriptor_Endpoint_t MSDataInEndpoint;
USB_Descriptor_Endpoint_t MSDataOutEndpoint;
USB_Descriptor_Interface_t HIDInterface;
USB_HID_Descriptor_t HIDInfo;
USB_Descriptor_Endpoint_t HIDDataInEndpoint;
} USB_Descriptor_Configuration_t;
/* Function Prototypes: */
......
/*
Copyright (C) Dean Camera, 2010.
dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com
*/
#include "DS1307.h"
void DS1307_Init(void)
{
// Nothing to initialize
}
void DS1307_SetDate(uint8_t Day, uint8_t Month, uint8_t Year)
{
#if defined(DUMMY_RTC)
return;
#endif
DS1307_DateRegs_t CurrentRTCDate;
CurrentRTCDate.Byte1.TenDay = (Day / 10);
CurrentRTCDate.Byte1.Day = (Day % 10);
CurrentRTCDate.Byte2.TenMonth = (Month / 10);
CurrentRTCDate.Byte2.Month = (Month % 10);
CurrentRTCDate.Byte3.TenYear = (Year / 10);
CurrentRTCDate.Byte3.Year = (Year % 10);
if (TWI_StartTransmission(DS1307_ADDRESS_WRITE))
{
TWI_SendByte(DS1307_DATEREG_START);
TWI_SendByte(CurrentRTCDate.Byte1.IntVal);
TWI_SendByte(CurrentRTCDate.Byte2.IntVal);
TWI_SendByte(CurrentRTCDate.Byte3.IntVal);
TWI_StopTransmission();
}
}
void DS1307_SetTime(uint8_t Hour, uint8_t Minute, uint8_t Second)
{
#if defined(DUMMY_RTC)
return;
#endif
DS1307_TimeRegs_t CurrentRTCTime;
CurrentRTCTime.Byte1.TenSec = (Second / 10);
CurrentRTCTime.Byte1.Sec = (Second % 10);
CurrentRTCTime.Byte1.CH = false;
CurrentRTCTime.Byte2.TenMin = (Minute / 10);
CurrentRTCTime.Byte2.Min = (Minute % 10);
CurrentRTCTime.Byte3.TenHour = (Hour / 10);
CurrentRTCTime.Byte3.Hour = (Hour % 10);
CurrentRTCTime.Byte3.TwelveHourMode = false;
if (TWI_StartTransmission(DS1307_ADDRESS_WRITE))
{
TWI_SendByte(DS1307_TIMEREG_START);
TWI_SendByte(CurrentRTCTime.Byte1.IntVal);
TWI_SendByte(CurrentRTCTime.Byte2.IntVal);
TWI_SendByte(CurrentRTCTime.Byte3.IntVal);
TWI_StopTransmission();
}
}
void DS1307_GetDate(uint8_t* Day, uint8_t* Month, uint8_t* Year)
{
#if defined(DUMMY_RTC)
*Day = 1;
*Month = 1;
*Year = 1;
return;
#endif
if (TWI_StartTransmission(DS1307_ADDRESS_WRITE))
{
TWI_SendByte(DS1307_DATEREG_START);
TWI_StopTransmission();
}
DS1307_DateRegs_t CurrentRTCDate;
if (TWI_StartTransmission(DS1307_ADDRESS_READ))
{
TWI_ReceiveByte(&CurrentRTCDate.Byte1.IntVal, false);
TWI_ReceiveByte(&CurrentRTCDate.Byte2.IntVal, false);
TWI_ReceiveByte(&CurrentRTCDate.Byte3.IntVal, true);
TWI_StopTransmission();
}
*Day = (CurrentRTCDate.Byte1.TenDay * 10) + CurrentRTCDate.Byte1.Day;
*Month = (CurrentRTCDate.Byte2.TenMonth * 10) + CurrentRTCDate.Byte2.Month;
*Year = (CurrentRTCDate.Byte3.TenYear * 10) + CurrentRTCDate.Byte3.Year;
}
void DS1307_GetTime(uint8_t* Hour, uint8_t* Minute, uint8_t* Second)
{
#if defined(DUMMY_RTC)
*Hour = 1;
*Minute = 1;
*Second = 1;
return;
#endif
if (TWI_StartTransmission(DS1307_ADDRESS_WRITE))
{
TWI_SendByte(DS1307_TIMEREG_START);
TWI_StopTransmission();
}
DS1307_TimeRegs_t CurrentRTCTime;
if (TWI_StartTransmission(DS1307_ADDRESS_READ))
{
TWI_ReceiveByte(&CurrentRTCTime.Byte1.IntVal, false);
TWI_ReceiveByte(&CurrentRTCTime.Byte2.IntVal, false);
TWI_ReceiveByte(&CurrentRTCTime.Byte3.IntVal, true);
TWI_StopTransmission();
}
*Second = (CurrentRTCTime.Byte1.TenSec * 10) + CurrentRTCTime.Byte1.Sec;
*Minute = (CurrentRTCTime.Byte2.TenMin * 10) + CurrentRTCTime.Byte2.Min;
*Hour = (CurrentRTCTime.Byte3.TenHour * 10) + CurrentRTCTime.Byte3.Hour;
}
/*
Copyright (C) Dean Camera, 2010.
dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com
*/
#ifndef _DS1307_H_
#define _DS1307_H_
/* Includes: */
#include <avr/io.h>
#include <LUFA/Drivers/Peripheral/TWI.h>
/* Type Defines: */
typedef struct
{
union
{
struct
{
unsigned int Sec : 4;
unsigned int TenSec : 3;
unsigned int CH : 1;
};
uint8_t IntVal;
} Byte1;
union
{
struct
{
unsigned int Min : 4;
unsigned int TenMin : 3;
unsigned int _RESERVED : 1;
};
uint8_t IntVal;
} Byte2;
union
{
struct
{
unsigned int Hour : 4;
unsigned int TenHour : 2;
unsigned int TwelveHourMode : 1;
unsigned int _RESERVED : 1;
};
uint8_t IntVal;
} Byte3;
} DS1307_TimeRegs_t;
typedef struct
{
union
{
struct
{
unsigned int Day : 4;
unsigned int TenDay : 2;
unsigned int _RESERVED : 2;
};
uint8_t IntVal;
} Byte1;
union
{
struct
{
unsigned int Month : 4;
unsigned int TenMonth : 1;
unsigned int _RESERVED : 3;
};
uint8_t IntVal;
} Byte2;
union
{
struct
{
unsigned int Year : 4;
unsigned int TenYear : 4;
};
uint8_t IntVal;
} Byte3;
} DS1307_DateRegs_t;
/* Macros: */
#define DS1307_TIMEREG_START 0x00
#define DS1307_DATEREG_START 0x04
#define DS1307_ADDRESS_READ 0b11010001
#define DS1307_ADDRESS_WRITE 0b11010000
/* Function Prototypes: */
void DS1307_Init(void);
void DS1307_SetDate(uint8_t Day, uint8_t Month, uint8_t Year);
void DS1307_SetTime(uint8_t Hour, uint8_t Minute, uint8_t Second);
void DS1307_GetDate(uint8_t* Day, uint8_t* Month, uint8_t* Year);
void DS1307_GetTime(uint8_t* Hour, uint8_t* Minute, uint8_t* Second);
#endif
......@@ -83,5 +83,17 @@ DRESULT disk_ioctl (
DWORD get_fattime (void)
{
return (1UL << 25) | (1UL << 21) | (1UL << 16) | (1UL << 11) | (1UL << 5) | (1UL << 0);
uint8_t Day, Month, Year;
uint8_t Hour, Minute, Second;
DS1307_GetDate(&Day, &Month, &Year);
DS1307_GetTime(&Hour, &Minute, &Second);
return ((DWORD)(20 + Year) << 25) |
((DWORD)Month << 21) |
((DWORD)Day << 16) |
((DWORD)Hour << 11) |
((DWORD)Minute << 5) |
(((DWORD)Second >> 1) << 0);
}
......@@ -36,7 +36,7 @@
/ 3: f_lseek is removed in addition to level 2. */
#define _USE_STRFUNC 1 /* 0, 1 or 2 */
#define _USE_STRFUNC 0 /* 0, 1 or 2 */
/* To enable string functions, set _USE_STRFUNC to 1 or 2. */
......
......@@ -58,52 +58,93 @@ USB_ClassInfo_MS_Device_t Disk_MS_Interface =
},
};
/** Buffer to hold the previously generated HID report, for comparison purposes inside the HID class driver. */
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 */
uint8_t EEMEM LoggingInterval500MS_EEPROM;
/** SRAM Logging Interval value fetched from EEPROM, stored as a number of 500ms ticks */
uint8_t LoggingInterval500MS_SRAM;
/** Total number of 500ms logging ticks elapsed since the last log value was recorded */
uint16_t CurrentLoggingTicks;
/** FAT Fs structure to hold the internal state of the FAT driver for the dataflash contents. */
FATFS DiskFATState;
/** FAT Fs structure to hold a FAT file handle for the log data write destination. */
FIL TempLogFile;
/** Counter to count the number of 10 millisecond ticks that has elapsed since the last sample */
uint16_t CurrentLogTick;
ISR(TIMER1_COMPA_vect, ISR_BLOCK)
{
if (CurrentLogTick++ != LOG_INTERVAL_10MS)
return;
uint8_t LEDMask = LEDs_GetLEDs();
/* Check to see if the logging interval has expired */
if (CurrentLoggingTicks++ < LoggingInterval500MS_SRAM)
return;
LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
CurrentLogTick = 0;
/* Reset log tick counter to prepare for next logging interval */
CurrentLoggingTicks = 0;
if (USB_DeviceState == DEVICE_STATE_Unattached)
{
f_printf(&TempLogFile, "%d Degrees\r\n", Temperature_GetTemperature());
uint8_t Day, Month, Year;
uint8_t Hour, Minute, Second;
DS1307_GetDate(&Day, &Month, &Year);
DS1307_GetTime(&Hour, &Minute, &Second);
char LineBuffer[100];
uint16_t BytesWritten;
BytesWritten = sprintf(LineBuffer, "%02d/%02d/20%04d, %02d:%02d:%02d, %d Degrees\r\n",
Day, Month, Year, Hour, Minute, Second, Temperature_GetTemperature());
f_write(&TempLogFile,