/* LUFA Library Copyright (C) Dean Camera, 2010. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* 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 * * Main source file for the VirtualSerial demo. This file contains the main tasks of * the demo and is responsible for the initial application hardware configuration. */ #include "VirtualSerial.h" #include #include #include #include static int running = true; static bool auto_mode = false; static volatile bool temp_valid = false; static volatile double current_temp = 0.0f; static volatile double target_temp = 0.0f; static volatile uint16_t temp_read_count = 0; static volatile bool compressor_running = false; float EEMEM eeprom_target_temperature = 4.0f; #define HYSTERESIS (0.5) void CheckACMStatus(void); void handle_command(const char *buf); /** LUFA CDC Class driver interface configuration and state information. This structure is * passed to all CDC Class driver functions, so that multiple instances of the same class * within a device can be differentiated from one another. */ USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface = { .Config = { .ControlInterfaceNumber = 0, .DataINEndpointNumber = CDC_TX_EPNUM, .DataINEndpointSize = CDC_TXRX_EPSIZE, .DataINEndpointDoubleBank = false, .DataOUTEndpointNumber = CDC_RX_EPNUM, .DataOUTEndpointSize = CDC_TXRX_EPSIZE, .DataOUTEndpointDoubleBank = false, .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM, .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE, .NotificationEndpointDoubleBank = false, }, }; /** Standard file stream for the CDC interface when set up, so that the virtual CDC COM port can be * used like any regular character stream in the C APIs */ static FILE USBSerialStream; #define BOARD_REV 2 #if (BOARD_REV == 1) #define LM74_SIO PD4 #define LM74_SIO_PIN PIND #define LM74_SIO_DDR DDRD #define LM74_SCL PD5 #define LM74_SCL_PORT PORTD #define LM74_SCL_DDR DDRD #define LM74_CS PD3 #define LM74_CS_PORT PORTD #define LM74_CS_DDR DDRD #define LM74_DBG PD6 #define LM74_DBG_PORT PORTD #define LM74_DBG_DDR DDRD #elif (BOARD_REV == 2) #define LM74_SIO PD0 #define LM74_SIO_PIN PIND #define LM74_SIO_DDR DDRD #define LM74_SCL PC2 #define LM74_SCL_PORT PORTC #define LM74_SCL_DDR DDRC #define LM74_CS PD1 #define LM74_CS_PORT PORTD #define LM74_CS_DDR DDRD #else #error BOARD_REV has to be defined as 'A' or 'B'. #endif /** * Data is clocked out on the falling edge of the serial clock (SC), while data is clocked in on the rising edge of SC. */ static int lm74_transaction(double *out) { uint16_t tmp = 0; LM74_CS_PORT &= ~_BV(LM74_CS); for(int i = 0; i < 16; i++) { tmp <<= 1; LM74_SCL_PORT |= _BV(LM74_SCL); if (LM74_SIO_PIN & (1 << LM74_SIO)) tmp |= 1; LM74_SCL_PORT &= ~_BV(LM74_SCL); } LM74_CS_PORT |= _BV(LM74_CS); /* Replay the data for debugging PORTD &= ~_BV(LM74_CS); for(int i = 0; i < 16; i++) { if(tmp & (0x8000 >> i)) { PORTD |= (1 << LM74_DBG); } else { PORTD &= ~(1 << LM74_DBG); } PORTD |= _BV(LM74_SCL); PORTD &= ~_BV(LM74_SCL); } PORTD |= _BV(LM74_CS); */ // Check for invalid reading if((tmp & (1 << 2)) == 0) { return 1; } // Dump the three last bits tmp >>= 3; *out = ((double)tmp) * 0.0625; return 0; } static void lm74_init(void) { LM74_SCL_PORT |= (1 << LM74_SCL); LM74_SCL_DDR |= (1 << LM74_SCL); LM74_CS_PORT |= (1 << LM74_CS); LM74_CS_DDR |= (1 << LM74_CS); LM74_SIO_DDR &= ~(1 << LM74_SIO); #ifdef LM74_DBG_PORT LM74_DBG_PORT |= (1 << LM74_DBG); LM74_DBG_DDR |= (1 << LM74_DBG); #endif } static void temp_timer_setup(void) { OCR1A = 16000; OCR1A = 128; // Disable all "compare output mode" pins, set CTC mode (Clear Timer on Compare) TCCR1A = (1 << WGM12); // Select clk_IO as source, divived by 256 TCCR1B = (1 << CS12); // Enable interrupt TIMSK1 = (1 << OCIE1A); } /** * Compressor */ static void compressor_init(void) { #if (BOARD_REV == 1) DDRB |= (1 << PB4) | (1 << PB0); PORTB |= 1 << PB4; #elif (BOARD_REV == 2) DDRC |= 1 << PC5; PORTC |= 1 << PC5; #endif } static void start_compressor(void) { compressor_running = true; #if (BOARD_REV == 1) PORTB |= 1 << PB4; #elif (BOARD_REV == 2) PORTC |= 1 << PC5; #endif } static void stop_compressor(void) { compressor_running = false; #if (BOARD_REV == 1) PORTB &= ~(1 << PB4); PORTB &= ~(1 << PB0); #elif (BOARD_REV == 2) PORTC &= ~(1 << PC5); #endif } /** 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(); lm74_init(); compressor_init(); target_temp = eeprom_read_float(&eeprom_target_temperature); temp_timer_setup(); /* Create a regular character stream for the interface so that it can be used with the stdio.h functions */ CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream); // LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); sei(); int last_temp_read_count = temp_read_count; while(running) { wdt_reset(); // _delay_ms(250); // PORTB ^= 1 << PB0; // Toggle external LED // CheckPinStatus(); CheckACMStatus(); if(auto_mode && temp_read_count != last_temp_read_count) { last_temp_read_count = temp_read_count; handle_command("status"); } CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); } USB_Detach(); /* Enable the watchdog and force a timeout to reset the AVR */ wdt_enable(WDTO_250MS); for (;;); } void handle_command(const char *buf) { if (strcmp("reboot", buf) == 0 || strcmp("reset", buf) == 0) { fputs("Rebooting!\r\n", &USBSerialStream); running = false; } if (strcmp("on", buf) == 0) { start_compressor(); } else if (strcmp("off", buf) == 0) { stop_compressor(); } else if (strcmp("auto", buf) == 0) { auto_mode = !auto_mode; } else if (strncmp("set ", buf, 4) == 0) { double d; errno = 0; d = strtod(&buf[4], NULL); if(errno != 0) { fprintf(&USBSerialStream, "Invalid temperature\r\n"); return; } target_temp = d; eeprom_update_float(&eeprom_target_temperature, (float)target_temp); fprintf(&USBSerialStream, "Target temperature set to: %f\r\n", target_temp); } else if (strcmp("status", buf) == 0) { if(temp_valid) { fprintf(&USBSerialStream, "Temp: %f\r\n", current_temp); } else { fprintf(&USBSerialStream, "Temp: Invalid reading\r\n"); } fprintf(&USBSerialStream, "Temp-Count: %u\r\n", temp_read_count); fprintf(&USBSerialStream, "Compressor: %s\r\n", compressor_running ? "running" : "off"); fprintf(&USBSerialStream, "Target-Temp: %f\r\n", target_temp); } } void CheckACMStatus() { static char buf[32]; static uint8_t len = 0; int c; while((c = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface)) > 0) { putc(c, &USBSerialStream); if (c == '\r') { putc('\n', &USBSerialStream); buf[len++] = '\0'; handle_command(buf); len = 0; continue; } buf[len++] = c; if (len == sizeof(buf)) { len = 0; continue; } } } /** 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); /* Enable the watchdog timer. */ wdt_enable(WDTO_1S); /* Disable clock division */ clock_prescale_set(clock_div_1); /* Hardware Initialization */ USB_Init(); } /** Checks for changes in the position of the board joystick, sending strings to the host upon each change. */ void CheckPinStatus(void) { char* ReportString = NULL; static bool ActionSent = false; if (!(PINB & (1 << PB6))) { ReportString = "Hello, World!\r\n"; ActionSent = false; } if (!(PINB & (1 << PB7))) { ReportString = "Rebooting!\r\n"; ActionSent = false; running = false; } if ((ReportString != NULL) && (ActionSent == false)) { ActionSent = true; /* Write the string to the virtual COM port via the created character stream */ fputs(ReportString, &USBSerialStream); _delay_ms(100); /* Alternatively, without the stream: */ // CDC_Device_SendString(&VirtualSerial_CDC_Interface, ReportString, strlen(ReportString)); } } /** Event handler for the library USB Connection event. */ void EVENT_USB_Device_Connect(void) { // LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING); } /** Event handler for the library USB Disconnection event. */ void EVENT_USB_Device_Disconnect(void) { // LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); } /** Event handler for the library USB Configuration Changed event. */ void EVENT_USB_Device_ConfigurationChanged(void) { bool ConfigSuccess = true; ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_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) { CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface); } ISR(TIMER1_COMPA_vect) { double t = 0; temp_read_count++; int i; for(i = 0; i < 10; i++) { if(lm74_transaction(&t) == 0) { break; } } if(i == 10) { temp_valid = false; return; } temp_valid = true; current_temp = t; /** * target = 30, hysteresis = 0.5 * * target = * */ if(current_temp < (target_temp - HYSTERESIS)) { stop_compressor(); } if(current_temp > (target_temp + HYSTERESIS)) { start_compressor(); } }