Control fan pwm from CDC

This commit is contained in:
Olaf Rempel 2021-02-13 21:17:34 +01:00
parent b9c11c6860
commit 7181dc2308
5 changed files with 224 additions and 342 deletions

View File

@ -1,109 +0,0 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2021.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
Copyright 2021 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 disclaims 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_
/* Includes: */
#include <LUFA/Drivers/USB/USB.h>
#include <avr/pgmspace.h>
/* Macros: */
/** Endpoint address of the CDC device-to-host notification IN endpoint. */
#define CDC_NOTIFICATION_EPADDR (ENDPOINT_DIR_IN | 2)
/** Endpoint address of the CDC device-to-host data IN endpoint. */
#define CDC_TX_EPADDR (ENDPOINT_DIR_IN | 3)
/** Endpoint address of the CDC host-to-device data OUT endpoint. */
#define CDC_RX_EPADDR (ENDPOINT_DIR_OUT | 4)
/** Size in bytes of the CDC device-to-host notification IN endpoint. */
#define CDC_NOTIFICATION_EPSIZE 8
/** Size in bytes of the CDC data IN and OUT endpoints. */
#define CDC_TXRX_EPSIZE 16
/* Type Defines: */
/** Type define for the device configuration descriptor structure. This must be defined in the
* application code, as the configuration descriptor contains several sub-descriptors which
* vary between devices, and which describe the device's usage to the host.
*/
typedef struct
{
USB_Descriptor_Configuration_Header_t Config;
// CDC Control Interface
USB_Descriptor_Interface_t CDC_CCI_Interface;
USB_CDC_Descriptor_FunctionalHeader_t CDC_Functional_Header;
USB_CDC_Descriptor_FunctionalACM_t CDC_Functional_ACM;
USB_CDC_Descriptor_FunctionalUnion_t CDC_Functional_Union;
USB_Descriptor_Endpoint_t CDC_NotificationEndpoint;
// CDC Data Interface
USB_Descriptor_Interface_t CDC_DCI_Interface;
USB_Descriptor_Endpoint_t CDC_DataOutEndpoint;
USB_Descriptor_Endpoint_t CDC_DataInEndpoint;
} USB_Descriptor_Configuration_t;
/** Enum for the device interface descriptor IDs within the device. Each interface descriptor
* should have a unique ID index associated with it, which can be used to refer to the
* interface from other descriptors.
*/
enum InterfaceDescriptors_t
{
INTERFACE_ID_CDC_CCI = 0, /**< CDC CCI interface descriptor ID */
INTERFACE_ID_CDC_DCI = 1, /**< CDC DCI interface descriptor ID */
};
/** Enum for the device string descriptor IDs within the device. Each string descriptor should
* have a unique ID index associated with it, which can be used to refer to the string from
* other descriptors.
*/
enum StringDescriptors_t
{
STRING_ID_Language = 0, /**< Supported Languages string descriptor ID (must be zero) */
STRING_ID_Manufacturer = 1, /**< Manufacturer string ID */
STRING_ID_Product = 2, /**< Product string ID */
};
/* Function Prototypes: */
uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
const uint16_t wIndex,
const void** const DescriptorAddress)
ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(3);
#endif

View File

@ -55,7 +55,7 @@
// #define NO_CLASS_DRIVER_AUTOFLUSH
/* General USB Driver Related Tokens: */
// #define ORDERED_EP_CONFIG
#define ORDERED_EP_CONFIG
#define USE_STATIC_OPTIONS (USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)
#define USB_DEVICE_ONLY
// #define USB_HOST_ONLY
@ -72,7 +72,7 @@
// #define DEVICE_STATE_AS_GPIOR {Insert Value Here}
#define FIXED_NUM_CONFIGURATIONS 1
// #define CONTROL_ONLY_DEVICE
// #define INTERRUPT_CONTROL_ENDPOINT
#define INTERRUPT_CONTROL_ENDPOINT
// #define NO_DEVICE_REMOTE_WAKEUP
// #define NO_DEVICE_SELF_POWER

219
main.c
View File

@ -1,61 +1,71 @@
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/power.h>
/***************************************************************************
* Copyright (C) 02/2021 by Olaf Rempel *
* razzor@kopf-tisch.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; version 2 of the License, *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <avr/interrupt.h>
#include <string.h>
#include <avr/io.h>
#include <avr/power.h>
#include "Descriptors.h"
#include "event.h"
#include "ledfade.h"
#include "pwm.h"
#include "timer.h"
#include "usbcdc.h"
#include <LUFA/Drivers/USB/USB.h>
#include <LUFA/Platform/Platform.h>
void SetupHardware(void);
void CDC_Task(void);
void EVENT_USB_Device_Connect(void);
void EVENT_USB_Device_Disconnect(void);
void EVENT_USB_Device_ConfigurationChanged(void);
void EVENT_USB_Device_ControlRequest(void);
/* *********************************************************************** */
/** Contains the current baud rate and other settings of the virtual serial port. While this demo does not use
* the physical USART and thus does not use these settings, they must still be retained and returned to the host
* upon request or the host will assume the device is non-functional.
*
* These values are set by the host via a class-specific request, however they are not required to be used accurately.
* It is possible to completely ignore these value or use other settings as the host is completely unaware of the physical
* serial link characteristics and instead sends and receives data in endpoint streams.
*/
static CDC_LineEncoding_t LineEncoding = { .BaudRateBPS = 0,
.CharFormat = CDC_LINEENCODING_OneStopBit,
.ParityType = CDC_PARITY_None,
.DataBits = 8 };
/* *************************************************************************
* disable_wdt_timer
* ************************************************************************* */
void disable_wdt_timer(void) __attribute__((naked, section(".init3")));
void disable_wdt_timer(void)
{
MCUSR = 0;
WDTCSR = (1<<WDCE) | (1<<WDE);
WDTCSR = (0<<WDE);
} /* disable_wdt_timer */
/** Main program entry point. This routine contains the overall program flow, including initial
* setup of all components and the main program loop.
*/
/* *************************************************************************
* main
* ************************************************************************* */
int main(void)
{
SetupHardware();
DDRD = (1<<PORTD5);
PORTD = (1<<PORTD5);
/* Disable clock division */
clock_prescale_set(clock_div_1);
usbcdc_init();
timer_init();
pwm_init();
GlobalInterruptEnable();
sei();
// fade LEDs to max, set fan PWM to 50%
event_queue(EVENT_TYPE_LEDFADE_COMMAND, EVENT_NUM_LED_CH0, EVENT_VALUE_LEDFADE_MAX);
event_queue(EVENT_TYPE_LEDFADE_COMMAND, EVENT_NUM_LED_CH1, EVENT_VALUE_LEDFADE_MAX);
event_queue(EVENT_TYPE_PWM_VALUE, EVENT_NUM_PWM_CH2, 0x8000);
for (;;)
{
// CDC_Task();
// USB_USBTask();
usbcdc_task();
timer_check(0);
@ -80,9 +90,18 @@ int main(void)
case EVENT_TYPE_LEDFADE_COMMAND:
case EVENT_TYPE_LEDFADE_VALUE:
ledfade_event_handler(p_event);
if (p_event->num == EVENT_NUM_LED_CH0)
{
PORTD &= ~(1<<PORTD5);
}
break;
case EVENT_TYPE_LEDFADE_STATUS:
usbcdc_event_handler(p_event);
if (p_event->num == EVENT_NUM_LED_CH0)
{
PORTD |= (1<<PORTD5);
}
break;
default:
@ -92,135 +111,3 @@ int main(void)
event_clear(p_event);
}
}
/** 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);
// USB_Init();
}
/** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
* starts the library USB task to begin the enumeration and USB management process.
*/
void EVENT_USB_Device_Connect(void)
{
}
/** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
* the status LEDs and stops the USB management and CDC management tasks.
*/
void EVENT_USB_Device_Disconnect(void)
{
}
/** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
* of the USB device after enumeration - the device endpoints are configured and the CDC management task started.
*/
void EVENT_USB_Device_ConfigurationChanged(void)
{
/* Setup CDC Data Endpoints */
Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPADDR, EP_TYPE_INTERRUPT, CDC_NOTIFICATION_EPSIZE, 1);
Endpoint_ConfigureEndpoint(CDC_TX_EPADDR, EP_TYPE_BULK, CDC_TXRX_EPSIZE, 1);
Endpoint_ConfigureEndpoint(CDC_RX_EPADDR, EP_TYPE_BULK, CDC_TXRX_EPSIZE, 1);
/* Reset line encoding baud rate so that the host knows to send new values */
LineEncoding.BaudRateBPS = 0;
}
/** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
* the device from the USB host before passing along unhandled control requests to the library for processing
* internally.
*/
void EVENT_USB_Device_ControlRequest(void)
{
/* Process CDC specific control requests */
switch (USB_ControlRequest.bRequest)
{
case CDC_REQ_GetLineEncoding:
if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
{
Endpoint_ClearSETUP();
/* Write the line coding data to the control endpoint */
Endpoint_Write_Control_Stream_LE(&LineEncoding, sizeof(CDC_LineEncoding_t));
Endpoint_ClearOUT();
}
break;
case CDC_REQ_SetLineEncoding:
if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
{
Endpoint_ClearSETUP();
/* Read the line coding data in from the host into the global struct */
Endpoint_Read_Control_Stream_LE(&LineEncoding, sizeof(CDC_LineEncoding_t));
Endpoint_ClearIN();
}
break;
case CDC_REQ_SetControlLineState:
if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
{
Endpoint_ClearSETUP();
Endpoint_ClearStatusStage();
}
break;
}
}
/** Function to manage CDC data transmission and reception to and from the host. */
void CDC_Task(void)
{
char* ReportString = NULL;
static bool ActionSent = false;
/* Device must be connected and configured for the task to run */
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
ActionSent = false;
/* Flag management - Only allow one string to be sent per action */
if ((ReportString != NULL) && (ActionSent == false) && LineEncoding.BaudRateBPS)
{
ActionSent = true;
/* Select the Serial Tx Endpoint */
Endpoint_SelectEndpoint(CDC_TX_EPADDR);
/* Write the String to the Endpoint */
Endpoint_Write_Stream_LE(ReportString, strlen(ReportString), NULL);
/* Remember if the packet to send completely fills the endpoint */
bool IsFull = (Endpoint_BytesInEndpoint() == CDC_TXRX_EPSIZE);
/* Finalize the stream transfer to send the last packet */
Endpoint_ClearIN();
/* If the last packet filled the endpoint, send an empty packet to release the buffer on
* the receiver (otherwise all data will be cached until a non-full packet is received) */
if (IsFull)
{
/* Wait until the endpoint is ready for another packet */
Endpoint_WaitUntilReady();
/* Send an empty packet to ensure that the host does not buffer data sent to it */
Endpoint_ClearIN();
}
}
/* Select the Serial Rx Endpoint */
Endpoint_SelectEndpoint(CDC_RX_EPADDR);
/* Throw away any received data from the host */
if (Endpoint_IsOUTReceived())
Endpoint_ClearOUT();
}

View File

@ -1,48 +1,69 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2021.
/***************************************************************************
* Copyright (C) 02/2021 by Olaf Rempel *
* razzor@kopf-tisch.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; version 2 of the License, *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <avr/pgmspace.h>
#include <LUFA/Drivers/USB/USB.h>
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
#include "event.h"
#include "ledfade.h"
#include "pwm.h"
#include "usbcdc.h"
/*
Copyright 2021 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.
#define CDC_NOTIFICATION_EPADDR (ENDPOINT_DIR_IN | 2)
#define CDC_TX_EPADDR (ENDPOINT_DIR_IN | 3)
#define CDC_RX_EPADDR (ENDPOINT_DIR_OUT | 4)
#define CDC_NOTIFICATION_EPSIZE 8
#define CDC_TXRX_EPSIZE 16
The author disclaims 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.
*/
typedef struct
{
USB_Descriptor_Configuration_Header_t Config;
/** \file
*
* USB Device Descriptors, for library use when in USB device mode. Descriptors are special
* computer-readable structures which the host requests upon device enumeration, to determine
* the device's capabilities and functions.
*/
// CDC Control Interface
USB_Descriptor_Interface_t CDC_CCI_Interface;
USB_CDC_Descriptor_FunctionalHeader_t CDC_Functional_Header;
USB_CDC_Descriptor_FunctionalACM_t CDC_Functional_ACM;
USB_CDC_Descriptor_FunctionalUnion_t CDC_Functional_Union;
USB_Descriptor_Endpoint_t CDC_NotificationEndpoint;
#include "Descriptors.h"
// CDC Data Interface
USB_Descriptor_Interface_t CDC_DCI_Interface;
USB_Descriptor_Endpoint_t CDC_DataOutEndpoint;
USB_Descriptor_Endpoint_t CDC_DataInEndpoint;
} USB_Descriptor_Configuration_t;
enum InterfaceDescriptors_t
{
INTERFACE_ID_CDC_CCI = 0,
INTERFACE_ID_CDC_DCI = 1,
};
enum StringDescriptors_t
{
STRING_ID_Language = 0,
STRING_ID_Manufacturer = 1,
STRING_ID_Product = 2,
};
/* *********************************************************************** */
/** 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
* process begins.
*/
const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
{
.Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
@ -60,11 +81,6 @@ const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
.NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
};
/** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
* of the device in one of its supported configurations, including information about any device interfaces
* and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
* a configuration so that the host may correctly communicate with the USB device.
*/
const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
{
.Config =
@ -152,30 +168,10 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
}
};
/** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
* the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
* via the language ID table available at USB.org what languages the device supports for its string descriptors.
*/
const USB_Descriptor_String_t PROGMEM LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
/** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
* form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
* Descriptor.
*/
const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(L"LUFA Library");
/** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
* and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
* Descriptor.
*/
const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"LUFA CDC Demo");
/** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
* documentation) by the application code so that the address and size of a requested descriptor can be given
* to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
* is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
* USB host.
*/
uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
const uint16_t wIndex,
const void** const DescriptorAddress)
@ -192,10 +188,12 @@ uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
Address = &DeviceDescriptor;
Size = sizeof(USB_Descriptor_Device_t);
break;
case DTYPE_Configuration:
Address = &ConfigurationDescriptor;
Size = sizeof(USB_Descriptor_Configuration_t);
break;
case DTYPE_String:
switch (DescriptorNumber)
{
@ -203,16 +201,17 @@ uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
Address = &LanguageString;
Size = pgm_read_byte(&LanguageString.Header.Size);
break;
case STRING_ID_Manufacturer:
Address = &ManufacturerString;
Size = pgm_read_byte(&ManufacturerString.Header.Size);
break;
case STRING_ID_Product:
Address = &ProductString;
Size = pgm_read_byte(&ProductString.Header.Size);
break;
}
break;
}
@ -220,3 +219,93 @@ uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
return Size;
}
USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
{
.Config =
{
.ControlInterfaceNumber = INTERFACE_ID_CDC_CCI,
.DataINEndpoint =
{
.Address = CDC_TX_EPADDR,
.Size = CDC_TXRX_EPSIZE,
.Banks = 1,
},
.DataOUTEndpoint =
{
.Address = CDC_RX_EPADDR,
.Size = CDC_TXRX_EPSIZE,
.Banks = 1,
},
.NotificationEndpoint =
{
.Address = CDC_NOTIFICATION_EPADDR,
.Size = CDC_NOTIFICATION_EPSIZE,
.Banks = 1,
},
},
};
/*
void EVENT_USB_Device_Connect(void)
{
}
void EVENT_USB_Device_Disconnect(void)
{
}
*/
void EVENT_USB_Device_ConfigurationChanged(void)
{
CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
}
void EVENT_USB_Device_ControlRequest(void)
{
CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
}
/*
void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
{
}
void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
{
}
*/
void usbcdc_event_handler(event_entry_t * p_event)
{
if ((p_event->type == EVENT_TYPE_LEDFADE_STATUS) &&
(p_event->num == EVENT_NUM_LED_CH0)
)
{
if (USB_DeviceState == DEVICE_STATE_Configured)
{
CDC_Device_SendByte(&VirtualSerial_CDC_Interface, '!');
}
}
}
void usbcdc_task(void)
{
CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
USB_USBTask();
if (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface))
{
uint8_t cmd = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
uint16_t value = 0;
if ((cmd >= '1') && (cmd <= '8'))
{
value = ((cmd - '0') * 0x2000) -1;
}
event_queue(EVENT_TYPE_PWM_VALUE,
EVENT_NUM_PWM_CH2,
value);
}
}
void usbcdc_init(void)
{
USB_Init();
}

15
usbcdc.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef USBCDC_H_SEEN
#define USBCDC_H_SEEN
#include "event.h"
/* *********************************************************************** */
void usbcdc_event_handler (event_entry_t * p_event);
void usbcdc_task(void);
void usbcdc_init(void);
/* *********************************************************************** */
#endif /* USBCDC_H_SEEN */