From 0862964c82c0c398c9f6ed3e6f867b6325aa64c0 Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Tue, 7 Sep 2021 07:20:06 +0200 Subject: [PATCH 01/20] Add cmake project for LUFA MMU-80 --- lib/AddLufa.cmake | 11 +++++++++++ lib/CMakeLists.txt | 1 + 2 files changed, 12 insertions(+) create mode 100644 lib/AddLufa.cmake diff --git a/lib/AddLufa.cmake b/lib/AddLufa.cmake new file mode 100644 index 0000000..52ff12f --- /dev/null +++ b/lib/AddLufa.cmake @@ -0,0 +1,11 @@ +add_library( + LUFA + lufa/LUFA/Common/Drivers/USB/Class/Device/CDCClassDevice.c + lufa/LUFA/Drivers/USB/Core/AVR8/Device_AVR8.c + lufa/LUFA/Drivers/USB/Core/AVR8/USBController_AVR8.c + lufa/LUFA/Drivers/USB/Core/AVR8/USBInterrupt_AVR8.c + lufa/LUFA/Drivers/USB/Core/ConfigDescriptors.c + lufa/LUFA/Drivers/USB/Core/Events.c + ) + +# target_compile_features(LUFA PUBLIC cxx_std_14) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 09159f6..13893f0 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,3 +1,4 @@ if(NOT CMAKE_CROSSCOMPILING) add_subdirectory(Catch2) endif() +include(AddLufa.cmake) From b4ecf39ce5c8ac79cb34f85264832bb4bfd16fdc Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Mon, 20 Sep 2021 11:31:51 +0200 Subject: [PATCH 02/20] Fix LUFA build --- lib/AddLufa.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/AddLufa.cmake b/lib/AddLufa.cmake index 52ff12f..3f0926f 100644 --- a/lib/AddLufa.cmake +++ b/lib/AddLufa.cmake @@ -1,6 +1,6 @@ add_library( LUFA - lufa/LUFA/Common/Drivers/USB/Class/Device/CDCClassDevice.c + lufa/LUFA/Drivers/USB/Class/Device/CDCClassDevice.c lufa/LUFA/Drivers/USB/Core/AVR8/Device_AVR8.c lufa/LUFA/Drivers/USB/Core/AVR8/USBController_AVR8.c lufa/LUFA/Drivers/USB/Core/AVR8/USBInterrupt_AVR8.c From c2411619dd4f68a7ee4cb9248981e9e3481100a1 Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Mon, 20 Sep 2021 11:32:38 +0200 Subject: [PATCH 03/20] Set LUFA F_USB --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 87d70f5..025c022 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -91,7 +91,7 @@ if(CMAKE_CROSSCOMPILING) endif() # mcu related settings - set(MCU_FLAGS -mmcu=atmega32u4 -DF_CPU=16000000L) + set(MCU_FLAGS -mmcu=atmega32u4 -DF_CPU=16000000L -DF_USB=F_CPU) add_compile_options(${MCU_FLAGS}) add_link_options(${MCU_FLAGS}) From 44b160c2c13df75b3adae2c9c50205c08f0d0b00 Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Mon, 20 Sep 2021 11:32:51 +0200 Subject: [PATCH 04/20] Fix warnings --- CMakeLists.txt | 3 ++- lib/AddLufa.cmake | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 025c022..77f51ad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -100,7 +100,8 @@ if(CMAKE_CROSSCOMPILING) add_link_options(-Wl,--gc-sections) # disable exceptions and related metadata - add_compile_options(-fno-exceptions -fno-unwind-tables -fno-rtti) + add_compile_options(-fno-exceptions -fno-unwind-tables) + add_compile_options($<$:-fno-rtti>) add_link_options(-Wl,--defsym,__exidx_start=0,--defsym,__exidx_end=0) else() if(CMAKE_BUILD_TYPE STREQUAL "Debug") diff --git a/lib/AddLufa.cmake b/lib/AddLufa.cmake index 3f0926f..ec381d5 100644 --- a/lib/AddLufa.cmake +++ b/lib/AddLufa.cmake @@ -8,4 +8,4 @@ add_library( lufa/LUFA/Drivers/USB/Core/Events.c ) -# target_compile_features(LUFA PUBLIC cxx_std_14) +target_compile_features(LUFA PUBLIC c_std_11) From 21ad1539417332820bf5bf544248ef59f4b3c072 Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Mon, 20 Sep 2021 12:09:25 +0200 Subject: [PATCH 05/20] Add compile options --- CMakeLists.txt | 2 +- lib/AddLufa.cmake | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 77f51ad..714da3c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -91,7 +91,7 @@ if(CMAKE_CROSSCOMPILING) endif() # mcu related settings - set(MCU_FLAGS -mmcu=atmega32u4 -DF_CPU=16000000L -DF_USB=F_CPU) + set(MCU_FLAGS -mmcu=atmega32u4 -DF_CPU=16000000L) add_compile_options(${MCU_FLAGS}) add_link_options(${MCU_FLAGS}) diff --git a/lib/AddLufa.cmake b/lib/AddLufa.cmake index ec381d5..e162ec2 100644 --- a/lib/AddLufa.cmake +++ b/lib/AddLufa.cmake @@ -6,6 +6,23 @@ add_library( lufa/LUFA/Drivers/USB/Core/AVR8/USBInterrupt_AVR8.c lufa/LUFA/Drivers/USB/Core/ConfigDescriptors.c lufa/LUFA/Drivers/USB/Core/Events.c - ) +) -target_compile_features(LUFA PUBLIC c_std_11) +target_compile_definitions(LUFA PUBLIC + -D USB_DEVICE_ONLY + -D DEVICE_STATE_AS_GPIOR=0 + -D ORDERED_EP_CONFIG + -D FIXED_CONTROL_ENDPOINT_SIZE=8 + -D FIXED_NUM_CONFIGURATIONS=1 + -D USE_RAM_DESCRIPTORS +# -D USE_STATIC_OPTIONS= "(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)" + -D NO_INTERNAL_SERIAL + -D NO_DEVICE_SELF_POWER + -D NO_DEVICE_REMOTE_WAKEUP + -D NO_SOF_EVENTS + -D F_USB=F_CPU + -D DEVICE_VID=0x2C99 + -D DEVICE_PID=0x0003 #could also be 0x0004. TBD. +) + +target_compile_features(LUFA PUBLIC c_std_99) From 26fd53251b62983ef6bcf4ea2b9f482a52cead6b Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Mon, 20 Sep 2021 14:26:56 +0200 Subject: [PATCH 06/20] Checkpoint --- CMakeLists.txt | 2 +- lib/AddLufa.cmake | 24 ++--- lib/Descriptors.c | 245 +++++++++++++++++++++++++++++++++++++++++++++ lib/Descriptors.h | 110 ++++++++++++++++++++ lib/lufa_config.h | 16 +++ src/CMakeLists.txt | 2 + src/main.cpp | 105 +++++++++++++++++++ 7 files changed, 487 insertions(+), 17 deletions(-) create mode 100644 lib/Descriptors.c create mode 100644 lib/Descriptors.h create mode 100644 lib/lufa_config.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 714da3c..c5beb42 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -159,7 +159,7 @@ endif() # add_link_dependency(firmware "${LINKER_SCRIPT}") -target_include_directories(firmware PRIVATE src) +target_include_directories(firmware PRIVATE src lib) target_compile_options(firmware PRIVATE -Wdouble-promotion) diff --git a/lib/AddLufa.cmake b/lib/AddLufa.cmake index e162ec2..d9e1664 100644 --- a/lib/AddLufa.cmake +++ b/lib/AddLufa.cmake @@ -4,25 +4,17 @@ add_library( lufa/LUFA/Drivers/USB/Core/AVR8/Device_AVR8.c lufa/LUFA/Drivers/USB/Core/AVR8/USBController_AVR8.c lufa/LUFA/Drivers/USB/Core/AVR8/USBInterrupt_AVR8.c + lufa/LUFA/Drivers/USB/Core/AVR8/Endpoint_AVR8.c lufa/LUFA/Drivers/USB/Core/ConfigDescriptors.c lufa/LUFA/Drivers/USB/Core/Events.c + lufa/LUFA/Drivers/USB/Core/DeviceStandardReq.c + lufa/LUFA/Drivers/USB/Core/USBTask.c + lufa/LUFA/CodeTemplates/DeviceTemplate/Descriptors.c + lufa/LUFA/Drivers/USB/Core/AVR8/EndpointStream_AVR8.c + Descriptors.c ) -target_compile_definitions(LUFA PUBLIC - -D USB_DEVICE_ONLY - -D DEVICE_STATE_AS_GPIOR=0 - -D ORDERED_EP_CONFIG - -D FIXED_CONTROL_ENDPOINT_SIZE=8 - -D FIXED_NUM_CONFIGURATIONS=1 - -D USE_RAM_DESCRIPTORS -# -D USE_STATIC_OPTIONS= "(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)" - -D NO_INTERNAL_SERIAL - -D NO_DEVICE_SELF_POWER - -D NO_DEVICE_REMOTE_WAKEUP - -D NO_SOF_EVENTS - -D F_USB=F_CPU - -D DEVICE_VID=0x2C99 - -D DEVICE_PID=0x0003 #could also be 0x0004. TBD. -) +target_include_directories(LUFA PRIVATE . lufa) +target_compile_options(LUFA PRIVATE -include lufa_config.h) target_compile_features(LUFA PUBLIC c_std_99) diff --git a/lib/Descriptors.c b/lib/Descriptors.c new file mode 100644 index 0000000..5ef99b9 --- /dev/null +++ b/lib/Descriptors.c @@ -0,0 +1,245 @@ +/* + 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 + * + * 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. + */ + +#include "Descriptors.h" + + +/** 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}, + + .USBSpecification = VERSION_BCD(1,1,0), + .Class = CDC_CSCP_CDCClass, + .SubClass = CDC_CSCP_NoSpecificSubclass, + .Protocol = CDC_CSCP_NoSpecificProtocol, + + .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE, + + .VendorID = 0x03EB, + .ProductID = 0x2044, + .ReleaseNumber = VERSION_BCD(0,0,1), + + .ManufacturerStrIndex = STRING_ID_Manufacturer, + .ProductStrIndex = STRING_ID_Product, + .SerialNumStrIndex = USE_INTERNAL_SERIAL, + + .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 = + { + .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration}, + + .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t), + .TotalInterfaces = 2, + + .ConfigurationNumber = 1, + .ConfigurationStrIndex = NO_DESCRIPTOR, + + .ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED), + + .MaxPowerConsumption = USB_CONFIG_POWER_MA(100) + }, + + .CDC_CCI_Interface = + { + .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, + + .InterfaceNumber = INTERFACE_ID_CDC_CCI, + .AlternateSetting = 0, + + .TotalEndpoints = 1, + + .Class = CDC_CSCP_CDCClass, + .SubClass = CDC_CSCP_ACMSubclass, + .Protocol = CDC_CSCP_ATCommandProtocol, + + .InterfaceStrIndex = NO_DESCRIPTOR + }, + + .CDC_Functional_Header = + { + .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalHeader_t), .Type = CDC_DTYPE_CSInterface}, + .Subtype = CDC_DSUBTYPE_CSInterface_Header, + + .CDCSpecification = VERSION_BCD(1,1,0), + }, + + .CDC_Functional_ACM = + { + .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalACM_t), .Type = CDC_DTYPE_CSInterface}, + .Subtype = CDC_DSUBTYPE_CSInterface_ACM, + + .Capabilities = 0x06, + }, + + .CDC_Functional_Union = + { + .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalUnion_t), .Type = CDC_DTYPE_CSInterface}, + .Subtype = CDC_DSUBTYPE_CSInterface_Union, + + .MasterInterfaceNumber = INTERFACE_ID_CDC_CCI, + .SlaveInterfaceNumber = INTERFACE_ID_CDC_DCI, + }, + + .CDC_NotificationEndpoint = + { + .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, + + .EndpointAddress = CDC_NOTIFICATION_EPADDR, + .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), + .EndpointSize = CDC_NOTIFICATION_EPSIZE, + .PollingIntervalMS = 0xFF + }, + + .CDC_DCI_Interface = + { + .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, + + .InterfaceNumber = INTERFACE_ID_CDC_DCI, + .AlternateSetting = 0, + + .TotalEndpoints = 2, + + .Class = CDC_CSCP_CDCDataClass, + .SubClass = CDC_CSCP_NoDataSubclass, + .Protocol = CDC_CSCP_NoDataProtocol, + + .InterfaceStrIndex = NO_DESCRIPTOR + }, + + .CDC_DataOutEndpoint = + { + .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, + + .EndpointAddress = CDC_RX_EPADDR, + .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), + .EndpointSize = CDC_TXRX_EPSIZE, + .PollingIntervalMS = 0x05 + }, + + .CDC_DataInEndpoint = + { + .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, + + .EndpointAddress = CDC_TX_EPADDR, + .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), + .EndpointSize = CDC_TXRX_EPSIZE, + .PollingIntervalMS = 0x05 + } +}; + +/** 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) +{ + const uint8_t DescriptorType = (wValue >> 8); + const uint8_t DescriptorNumber = (wValue & 0xFF); + + const void* Address = NULL; + uint16_t Size = NO_DESCRIPTOR; + + switch (DescriptorType) + { + case DTYPE_Device: + 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) + { + case STRING_ID_Language: + 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; + } + + *DescriptorAddress = Address; + return Size; +} + diff --git a/lib/Descriptors.h b/lib/Descriptors.h new file mode 100644 index 0000000..0325c7d --- /dev/null +++ b/lib/Descriptors.h @@ -0,0 +1,110 @@ +/* + 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 + + #include "lufa/LUFA/Drivers/USB/USB.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 + diff --git a/lib/lufa_config.h b/lib/lufa_config.h new file mode 100644 index 0000000..a08266e --- /dev/null +++ b/lib/lufa_config.h @@ -0,0 +1,16 @@ +#pragma once + +#define USB_DEVICE_ONLY +#define DEVICE_STATE_AS_GPIOR 0 +#define ORDERED_EP_CONFIG +#define FIXED_CONTROL_ENDPOINT_SIZE 8 +#define FIXED_NUM_CONFIGURATIONS 1 +#define USE_FLASH_DESCRIPTORS +#define USE_STATIC_OPTIONS (USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL) +#define NO_INTERNAL_SERIAL +#define NO_DEVICE_SELF_POWER +#define NO_DEVICE_REMOTE_WAKEUP +#define NO_SOF_EVENTS +#define F_USB F_CPU +#define DEVICE_VID 0x2C99 +#define DEVICE_PID 0x0004 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9fb39e8..fce3c5c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,6 +3,8 @@ target_sources( PRIVATE main.cpp ) +target_link_libraries(firmware LUFA) + set_property( SOURCE src/version.c APPEND diff --git a/src/main.cpp b/src/main.cpp index 2544f4d..9893103 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,12 @@ #include "hal/usart.h" #include "hal/watchdog.h" +extern "C" { +#include "lufa_config.h" +#include "Descriptors.h" +#include "lufa/LUFA/Drivers/USB/USB.h" +} + #include "pins.h" #include #include @@ -35,6 +41,100 @@ #include "panic.h" +extern "C" { + +/** 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 = INTERFACE_ID_CDC_CCI, + .DataINEndpoint = + { + .Address = CDC_TX_EPADDR, + .Size = CDC_TXRX_EPSIZE, + .Type = EP_TYPE_BULK, + .Banks = 1, + }, + .DataOUTEndpoint = + { + .Address = CDC_RX_EPADDR, + .Size = CDC_TXRX_EPSIZE, + .Type = EP_TYPE_BULK, + .Banks = 1, + }, + .NotificationEndpoint = + { + .Address = CDC_NOTIFICATION_EPADDR, + .Size = CDC_NOTIFICATION_EPSIZE, + .Type = EP_TYPE_INTERRUPT, + .Banks = 1, + }, + }, + }; + +void testFunc(uint8_t i) { + char str[30]; + sprintf_P(str, PSTR("testFunc(%hu)\n"), i); + hal::usart::usart1.puts(str); +} + +/** Event handler for the library USB Connection event. */ +void EVENT_USB_Device_Connect(void) +{ + hal::usart::usart1.puts("EVENT_USB_Device_Connect\n"); +} + +/** Event handler for the library USB Disconnection event. */ +void EVENT_USB_Device_Disconnect(void) +{ + hal::usart::usart1.puts("EVENT_USB_Device_Disconnect\n"); +} + +/** 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); + char str1[] = "ready"; + char str0[] = "error"; + hal::usart::usart1.puts("EVENT_USB_Device_ConfigurationChanged:"); + hal::usart::usart1.puts(ConfigSuccess ? str1 : str0); +} + +/** Event handler for the library USB Control Request reception event. */ +void EVENT_USB_Device_ControlRequest(void) +{ + hal::usart::usart1.puts("EVENT_USB_Device_ControlRequest\n"); + CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface); +} + +/** CDC class driver callback function the processing of changes to the virtual + * control lines sent from the host.. + * + * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced + */ +void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t *const CDCInterfaceInfo) +{ + /* You can get changes to the virtual CDC lines in this callback; a common + use-case is to use the Data Terminal Ready (DTR) flag to enable and + disable CDC communications in your application when set to avoid the + application blocking while waiting for a host to become ready and read + in the pending data from the USB endpoints. + */ + bool HostReady = (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR) != 0; + + (void)HostReady; +} + +} + /// Global instance of the protocol codec static mp::Protocol protocol; @@ -141,6 +241,8 @@ void setup() { ml::leds.SetMode(0, ml::Color::green, ml::Mode::on); ml::leds.Step(); + USB_Init(); + /// Turn off all leds for (uint8_t i = 0; i < config::toolCount; i++) { ml::leds.SetMode(i, ml::Color::green, ml::Mode::off); @@ -367,6 +469,9 @@ void loop() { ms::selector.Step(); mui::userInput.Step(); currentCommand->Step(); + + CDC_Device_USBTask(&VirtualSerial_CDC_Interface); + USB_USBTask(); hal::watchdog::Reset(); } From 895671aeb28c7981e54425ffb967f84118c75007 Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Mon, 20 Sep 2021 15:14:37 +0200 Subject: [PATCH 07/20] Fix descriptors --- lib/AddLufa.cmake | 1 - lib/Descriptors.c | 8 ++++---- src/main.cpp | 20 +++++++++++++------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/AddLufa.cmake b/lib/AddLufa.cmake index d9e1664..84be75a 100644 --- a/lib/AddLufa.cmake +++ b/lib/AddLufa.cmake @@ -9,7 +9,6 @@ add_library( lufa/LUFA/Drivers/USB/Core/Events.c lufa/LUFA/Drivers/USB/Core/DeviceStandardReq.c lufa/LUFA/Drivers/USB/Core/USBTask.c - lufa/LUFA/CodeTemplates/DeviceTemplate/Descriptors.c lufa/LUFA/Drivers/USB/Core/AVR8/EndpointStream_AVR8.c Descriptors.c ) diff --git a/lib/Descriptors.c b/lib/Descriptors.c index 5ef99b9..e5c111e 100644 --- a/lib/Descriptors.c +++ b/lib/Descriptors.c @@ -54,8 +54,8 @@ const USB_Descriptor_Device_t PROGMEM DeviceDescriptor = .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE, - .VendorID = 0x03EB, - .ProductID = 0x2044, + .VendorID = 0x2C99, + .ProductID = 0x0004, .ReleaseNumber = VERSION_BCD(0,0,1), .ManufacturerStrIndex = STRING_ID_Manufacturer, @@ -185,13 +185,13 @@ const USB_Descriptor_String_t PROGMEM LanguageString = USB_STRING_DESCRIPTOR_ARR * 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"); +const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(L"Prusa3D"); /** 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"); +const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"Original Prusa i3 MK3 Multi Material 2.0 upgrade"); /** 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 diff --git a/src/main.cpp b/src/main.cpp index 9893103..1e43b60 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -76,11 +76,17 @@ USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface = }, }; -void testFunc(uint8_t i) { - char str[30]; - sprintf_P(str, PSTR("testFunc(%hu)\n"), i); - hal::usart::usart1.puts(str); -} +// void testFunc1(uint8_t i) { +// char str[30]; +// sprintf_P(str, PSTR("testFunc1(%hu)\n"), i); +// hal::usart::usart1.puts(str); +// } + +// void testFunc2(uint8_t i) { +// char str[30]; +// sprintf_P(str, PSTR("testFunc2(%hu)\n"), i); +// hal::usart::usart1.puts(str); +// } /** Event handler for the library USB Connection event. */ void EVENT_USB_Device_Connect(void) @@ -102,8 +108,8 @@ void EVENT_USB_Device_ConfigurationChanged(void) ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface); // LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR); - char str1[] = "ready"; - char str0[] = "error"; + char str1[] = "ready\n"; + char str0[] = "error\n"; hal::usart::usart1.puts("EVENT_USB_Device_ConfigurationChanged:"); hal::usart::usart1.puts(ConfigSuccess ? str1 : str0); } From 9611db9f53bb53ac838ddf7734261e465fe21487 Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Mon, 20 Sep 2021 16:07:15 +0200 Subject: [PATCH 08/20] =?UTF-8?q?Fix=20formatting=20and=20=F0=9F=90=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Descriptors.c | 237 ++++++++++++++++++++-------------------------- src/main.cpp | 95 +++++++++---------- 2 files changed, 151 insertions(+), 181 deletions(-) diff --git a/lib/Descriptors.c b/lib/Descriptors.c index e5c111e..fc87c9f 100644 --- a/lib/Descriptors.c +++ b/lib/Descriptors.c @@ -37,32 +37,30 @@ #include "Descriptors.h" - /** 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}, +const USB_Descriptor_Device_t PROGMEM DeviceDescriptor = { + .Header = { .Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device }, - .USBSpecification = VERSION_BCD(1,1,0), - .Class = CDC_CSCP_CDCClass, - .SubClass = CDC_CSCP_NoSpecificSubclass, - .Protocol = CDC_CSCP_NoSpecificProtocol, + .USBSpecification = VERSION_BCD(1, 1, 0), + .Class = CDC_CSCP_CDCClass, + .SubClass = CDC_CSCP_NoSpecificSubclass, + .Protocol = CDC_CSCP_NoSpecificProtocol, - .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE, + .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE, - .VendorID = 0x2C99, - .ProductID = 0x0004, - .ReleaseNumber = VERSION_BCD(0,0,1), + .VendorID = 0x2C99, + .ProductID = 0x0004, + .ReleaseNumber = VERSION_BCD(0, 0, 1), - .ManufacturerStrIndex = STRING_ID_Manufacturer, - .ProductStrIndex = STRING_ID_Product, - .SerialNumStrIndex = USE_INTERNAL_SERIAL, + .ManufacturerStrIndex = STRING_ID_Manufacturer, + .ProductStrIndex = STRING_ID_Product, + .SerialNumStrIndex = USE_INTERNAL_SERIAL, - .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS + .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS }; /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage @@ -70,109 +68,88 @@ const USB_Descriptor_Device_t PROGMEM DeviceDescriptor = * 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 = - { - .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration}, +const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor = { + .Config = { + .Header = { .Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration }, - .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t), - .TotalInterfaces = 2, + .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t), + .TotalInterfaces = 2, - .ConfigurationNumber = 1, - .ConfigurationStrIndex = NO_DESCRIPTOR, + .ConfigurationNumber = 1, + .ConfigurationStrIndex = NO_DESCRIPTOR, - .ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED), + .ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED), - .MaxPowerConsumption = USB_CONFIG_POWER_MA(100) - }, + .MaxPowerConsumption = USB_CONFIG_POWER_MA(100) }, - .CDC_CCI_Interface = - { - .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, + .CDC_CCI_Interface = { .Header = { .Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface }, - .InterfaceNumber = INTERFACE_ID_CDC_CCI, - .AlternateSetting = 0, + .InterfaceNumber = INTERFACE_ID_CDC_CCI, + .AlternateSetting = 0, - .TotalEndpoints = 1, + .TotalEndpoints = 1, - .Class = CDC_CSCP_CDCClass, - .SubClass = CDC_CSCP_ACMSubclass, - .Protocol = CDC_CSCP_ATCommandProtocol, + .Class = CDC_CSCP_CDCClass, + .SubClass = CDC_CSCP_ACMSubclass, + .Protocol = CDC_CSCP_ATCommandProtocol, - .InterfaceStrIndex = NO_DESCRIPTOR - }, + .InterfaceStrIndex = NO_DESCRIPTOR }, - .CDC_Functional_Header = - { - .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalHeader_t), .Type = CDC_DTYPE_CSInterface}, - .Subtype = CDC_DSUBTYPE_CSInterface_Header, + .CDC_Functional_Header = { + .Header = { .Size = sizeof(USB_CDC_Descriptor_FunctionalHeader_t), .Type = CDC_DTYPE_CSInterface }, + .Subtype = CDC_DSUBTYPE_CSInterface_Header, - .CDCSpecification = VERSION_BCD(1,1,0), - }, + .CDCSpecification = VERSION_BCD(1, 1, 0), + }, - .CDC_Functional_ACM = - { - .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalACM_t), .Type = CDC_DTYPE_CSInterface}, - .Subtype = CDC_DSUBTYPE_CSInterface_ACM, + .CDC_Functional_ACM = { + .Header = { .Size = sizeof(USB_CDC_Descriptor_FunctionalACM_t), .Type = CDC_DTYPE_CSInterface }, + .Subtype = CDC_DSUBTYPE_CSInterface_ACM, - .Capabilities = 0x06, - }, + .Capabilities = 0x06, + }, - .CDC_Functional_Union = - { - .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalUnion_t), .Type = CDC_DTYPE_CSInterface}, - .Subtype = CDC_DSUBTYPE_CSInterface_Union, + .CDC_Functional_Union = { + .Header = { .Size = sizeof(USB_CDC_Descriptor_FunctionalUnion_t), .Type = CDC_DTYPE_CSInterface }, + .Subtype = CDC_DSUBTYPE_CSInterface_Union, - .MasterInterfaceNumber = INTERFACE_ID_CDC_CCI, - .SlaveInterfaceNumber = INTERFACE_ID_CDC_DCI, - }, + .MasterInterfaceNumber = INTERFACE_ID_CDC_CCI, + .SlaveInterfaceNumber = INTERFACE_ID_CDC_DCI, + }, - .CDC_NotificationEndpoint = - { - .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, + .CDC_NotificationEndpoint = { .Header = { .Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint }, - .EndpointAddress = CDC_NOTIFICATION_EPADDR, - .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), - .EndpointSize = CDC_NOTIFICATION_EPSIZE, - .PollingIntervalMS = 0xFF - }, + .EndpointAddress = CDC_NOTIFICATION_EPADDR, + .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), + .EndpointSize = CDC_NOTIFICATION_EPSIZE, + .PollingIntervalMS = 0xFF }, - .CDC_DCI_Interface = - { - .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, + .CDC_DCI_Interface = { .Header = { .Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface }, - .InterfaceNumber = INTERFACE_ID_CDC_DCI, - .AlternateSetting = 0, + .InterfaceNumber = INTERFACE_ID_CDC_DCI, + .AlternateSetting = 0, - .TotalEndpoints = 2, + .TotalEndpoints = 2, - .Class = CDC_CSCP_CDCDataClass, - .SubClass = CDC_CSCP_NoDataSubclass, - .Protocol = CDC_CSCP_NoDataProtocol, + .Class = CDC_CSCP_CDCDataClass, + .SubClass = CDC_CSCP_NoDataSubclass, + .Protocol = CDC_CSCP_NoDataProtocol, - .InterfaceStrIndex = NO_DESCRIPTOR - }, + .InterfaceStrIndex = NO_DESCRIPTOR }, - .CDC_DataOutEndpoint = - { - .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, + .CDC_DataOutEndpoint = { .Header = { .Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint }, - .EndpointAddress = CDC_RX_EPADDR, - .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), - .EndpointSize = CDC_TXRX_EPSIZE, - .PollingIntervalMS = 0x05 - }, + .EndpointAddress = CDC_RX_EPADDR, + .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), + .EndpointSize = CDC_TXRX_EPSIZE, + .PollingIntervalMS = 0x05 }, - .CDC_DataInEndpoint = - { - .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, + .CDC_DataInEndpoint = { .Header = { .Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint }, - .EndpointAddress = CDC_TX_EPADDR, - .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), - .EndpointSize = CDC_TXRX_EPSIZE, - .PollingIntervalMS = 0x05 - } + .EndpointAddress = CDC_TX_EPADDR, + .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), + .EndpointSize = CDC_TXRX_EPSIZE, + .PollingIntervalMS = 0x05 } }; /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests @@ -191,7 +168,7 @@ const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR * 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"Original Prusa i3 MK3 Multi Material 2.0 upgrade"); +const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"Original Prusa i3 MK3 Multi Material 2.0 upgrade 🐑"); /** 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 @@ -200,46 +177,42 @@ const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"Or * USB host. */ uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, - const uint16_t wIndex, - const void** const DescriptorAddress) -{ - const uint8_t DescriptorType = (wValue >> 8); - const uint8_t DescriptorNumber = (wValue & 0xFF); + const uint16_t wIndex, + const void **const DescriptorAddress) { + const uint8_t DescriptorType = (wValue >> 8); + const uint8_t DescriptorNumber = (wValue & 0xFF); - const void* Address = NULL; - uint16_t Size = NO_DESCRIPTOR; + const void *Address = NULL; + uint16_t Size = NO_DESCRIPTOR; - switch (DescriptorType) - { - case DTYPE_Device: - 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) - { - case STRING_ID_Language: - 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; - } + switch (DescriptorType) { + case DTYPE_Device: + 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) { + case STRING_ID_Language: + 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; - } + break; + } - *DescriptorAddress = Address; - return Size; + *DescriptorAddress = Address; + return Size; } - diff --git a/src/main.cpp b/src/main.cpp index 1e43b60..46dbe82 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,34 +47,29 @@ extern "C" { * 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 = INTERFACE_ID_CDC_CCI, - .DataINEndpoint = - { - .Address = CDC_TX_EPADDR, - .Size = CDC_TXRX_EPSIZE, - .Type = EP_TYPE_BULK, - .Banks = 1, - }, - .DataOUTEndpoint = - { - .Address = CDC_RX_EPADDR, - .Size = CDC_TXRX_EPSIZE, - .Type = EP_TYPE_BULK, - .Banks = 1, - }, - .NotificationEndpoint = - { - .Address = CDC_NOTIFICATION_EPADDR, - .Size = CDC_NOTIFICATION_EPSIZE, - .Type = EP_TYPE_INTERRUPT, - .Banks = 1, - }, - }, - }; +USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface = { + .Config = { + .ControlInterfaceNumber = INTERFACE_ID_CDC_CCI, + .DataINEndpoint = { + .Address = CDC_TX_EPADDR, + .Size = CDC_TXRX_EPSIZE, + .Type = EP_TYPE_BULK, + .Banks = 1, + }, + .DataOUTEndpoint = { + .Address = CDC_RX_EPADDR, + .Size = CDC_TXRX_EPSIZE, + .Type = EP_TYPE_BULK, + .Banks = 1, + }, + .NotificationEndpoint = { + .Address = CDC_NOTIFICATION_EPADDR, + .Size = CDC_NOTIFICATION_EPSIZE, + .Type = EP_TYPE_INTERRUPT, + .Banks = 1, + }, + }, +}; // void testFunc1(uint8_t i) { // char str[30]; @@ -89,25 +84,22 @@ USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface = // } /** Event handler for the library USB Connection event. */ -void EVENT_USB_Device_Connect(void) -{ - hal::usart::usart1.puts("EVENT_USB_Device_Connect\n"); +void EVENT_USB_Device_Connect(void) { + hal::usart::usart1.puts("EVENT_USB_Device_Connect\n"); } /** Event handler for the library USB Disconnection event. */ -void EVENT_USB_Device_Disconnect(void) -{ - hal::usart::usart1.puts("EVENT_USB_Device_Disconnect\n"); +void EVENT_USB_Device_Disconnect(void) { + hal::usart::usart1.puts("EVENT_USB_Device_Disconnect\n"); } /** Event handler for the library USB Configuration Changed event. */ -void EVENT_USB_Device_ConfigurationChanged(void) -{ - bool ConfigSuccess = true; +void EVENT_USB_Device_ConfigurationChanged(void) { + bool ConfigSuccess = true; - ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface); + ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface); - // LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR); + // LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR); char str1[] = "ready\n"; char str0[] = "error\n"; hal::usart::usart1.puts("EVENT_USB_Device_ConfigurationChanged:"); @@ -115,10 +107,9 @@ void EVENT_USB_Device_ConfigurationChanged(void) } /** Event handler for the library USB Control Request reception event. */ -void EVENT_USB_Device_ControlRequest(void) -{ +void EVENT_USB_Device_ControlRequest(void) { hal::usart::usart1.puts("EVENT_USB_Device_ControlRequest\n"); - CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface); + CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface); } /** CDC class driver callback function the processing of changes to the virtual @@ -126,19 +117,25 @@ void EVENT_USB_Device_ControlRequest(void) * * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced */ -void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t *const CDCInterfaceInfo) -{ - /* You can get changes to the virtual CDC lines in this callback; a common +void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t *const CDCInterfaceInfo) { + hal::usart::usart1.puts("EVENT_CDC_Device_ControLineStateChanged\n"); + /* You can get changes to the virtual CDC lines in this callback; a common use-case is to use the Data Terminal Ready (DTR) flag to enable and disable CDC communications in your application when set to avoid the application blocking while waiting for a host to become ready and read in the pending data from the USB endpoints. */ - bool HostReady = (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR) != 0; - - (void)HostReady; + bool HostReady = (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR) != 0; + char str[50]; + sprintf_P(str, PSTR("DTR:%hu\n"), HostReady); + hal::usart::usart1.puts(str); } +void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t *const CDCInterfaceInfo) { + char str[50]; + sprintf_P(str, PSTR("baud:%lu\n"), CDCInterfaceInfo->State.LineEncoding.BaudRateBPS); + hal::usart::usart1.puts(str); +} } /// Global instance of the protocol codec @@ -475,7 +472,7 @@ void loop() { ms::selector.Step(); mui::userInput.Step(); currentCommand->Step(); - + CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); From a8b1725dfe51054aec8d0c52474ca18fe719b663 Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Mon, 20 Sep 2021 16:43:02 +0200 Subject: [PATCH 09/20] Line encoding update --- src/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 46dbe82..9279cc1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -118,13 +118,13 @@ void EVENT_USB_Device_ControlRequest(void) { * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced */ void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t *const CDCInterfaceInfo) { - hal::usart::usart1.puts("EVENT_CDC_Device_ControLineStateChanged\n"); /* You can get changes to the virtual CDC lines in this callback; a common use-case is to use the Data Terminal Ready (DTR) flag to enable and disable CDC communications in your application when set to avoid the application blocking while waiting for a host to become ready and read in the pending data from the USB endpoints. */ + hal::usart::usart1.puts("EVENT_CDC_Device_ControLineStateChanged "); bool HostReady = (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR) != 0; char str[50]; sprintf_P(str, PSTR("DTR:%hu\n"), HostReady); @@ -132,6 +132,7 @@ void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t *const C } void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t *const CDCInterfaceInfo) { + hal::usart::usart1.puts("EVENT_CDC_Device_LineEncodingChanged "); char str[50]; sprintf_P(str, PSTR("baud:%lu\n"), CDCInterfaceInfo->State.LineEncoding.BaudRateBPS); hal::usart::usart1.puts(str); From 29018671b5a98ccffd95a2f133b9b367556f3c9c Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Mon, 20 Sep 2021 17:26:17 +0200 Subject: [PATCH 10/20] Reduce verbosity --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 9279cc1..d1e340a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -108,7 +108,7 @@ void EVENT_USB_Device_ConfigurationChanged(void) { /** Event handler for the library USB Control Request reception event. */ void EVENT_USB_Device_ControlRequest(void) { - hal::usart::usart1.puts("EVENT_USB_Device_ControlRequest\n"); + // hal::usart::usart1.puts("EVENT_USB_Device_ControlRequest\n"); CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface); } From 6346dd3989c7a722fe627f4ecedd1d6e85a8a750 Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Mon, 20 Sep 2021 17:26:59 +0200 Subject: [PATCH 11/20] USB reset initial --- src/main.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index d1e340a..9cbf362 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -136,6 +136,10 @@ void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t *const CDCI char str[50]; sprintf_P(str, PSTR("baud:%lu\n"), CDCInterfaceInfo->State.LineEncoding.BaudRateBPS); hal::usart::usart1.puts(str); + if (CDCInterfaceInfo->State.LineEncoding.BaudRateBPS == 1200) { + *(uint16_t *)0x0800U = 0x7777; + hal::cpu::Reset(); + } } } From 9f0f7bc33b8d97cfff7e74392d24096dad894485 Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Mon, 20 Sep 2021 18:18:52 +0200 Subject: [PATCH 12/20] Fix limits.h naming conflict --- src/hal/circular_buffer.h | 2 +- src/{limits.h => intlimits.h} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/{limits.h => intlimits.h} (100%) diff --git a/src/hal/circular_buffer.h b/src/hal/circular_buffer.h index 4330e15..8f99003 100644 --- a/src/hal/circular_buffer.h +++ b/src/hal/circular_buffer.h @@ -1,6 +1,6 @@ #pragma once #include -#include "../limits.h" +#include "../intlimits.h" /// A generic circular index class which can be used to build circular buffers /// Can hold up to size elements diff --git a/src/limits.h b/src/intlimits.h similarity index 100% rename from src/limits.h rename to src/intlimits.h From 2b9c73dbd5631bafce538e26d415eb92effbb2bd Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Mon, 20 Sep 2021 19:00:54 +0200 Subject: [PATCH 13/20] Make the config more similar to the demo --- lib/lufa_config.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/lufa_config.h b/lib/lufa_config.h index a08266e..2ae4f65 100644 --- a/lib/lufa_config.h +++ b/lib/lufa_config.h @@ -2,15 +2,16 @@ #define USB_DEVICE_ONLY #define DEVICE_STATE_AS_GPIOR 0 -#define ORDERED_EP_CONFIG +// #define ORDERED_EP_CONFIG #define FIXED_CONTROL_ENDPOINT_SIZE 8 #define FIXED_NUM_CONFIGURATIONS 1 +#define INTERRUPT_CONTROL_ENDPOINT #define USE_FLASH_DESCRIPTORS #define USE_STATIC_OPTIONS (USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL) #define NO_INTERNAL_SERIAL #define NO_DEVICE_SELF_POWER #define NO_DEVICE_REMOTE_WAKEUP -#define NO_SOF_EVENTS +// #define NO_SOF_EVENTS #define F_USB F_CPU #define DEVICE_VID 0x2C99 #define DEVICE_PID 0x0004 From 4f6ea165153809b35541700a80e44789d186609c Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Mon, 20 Sep 2021 19:01:08 +0200 Subject: [PATCH 14/20] Functional USB reset --- src/hal/avr/cpu.cpp | 17 ++++++++++++++++ src/hal/cpu.h | 3 +++ src/hal/watchdog.h | 4 ++++ src/main.cpp | 47 +++++++++++++++++++++++++-------------------- 4 files changed, 50 insertions(+), 21 deletions(-) diff --git a/src/hal/avr/cpu.cpp b/src/hal/avr/cpu.cpp index 7bef056..db9d93f 100644 --- a/src/hal/avr/cpu.cpp +++ b/src/hal/avr/cpu.cpp @@ -2,9 +2,17 @@ #include #include "../watchdog.h" +#include "lufa_config.h" +#include "Descriptors.h" +#include "lufa/LUFA/Drivers/USB/USB.h" + +#include "../usart.h" + namespace hal { namespace cpu { +bool resetPending = false; + void Init() { } @@ -15,5 +23,14 @@ void Reset() { ; //endless loop while waiting for the watchdog to reset } +void Step() { + if (resetPending) { + hal::usart::usart1.puts("resetPending\n"); + USB_Detach(); + for (;;) + ; //endless loop while waiting for the watchdog to reset + } +} + } // namespace CPU } // namespace hal diff --git a/src/hal/cpu.h b/src/hal/cpu.h index ace6a9e..bdf0472 100644 --- a/src/hal/cpu.h +++ b/src/hal/cpu.h @@ -10,9 +10,12 @@ namespace cpu { #define F_CPU (16000000ul) #endif +extern bool resetPending; + /// CPU init routines (not really necessary for the AVR) void Init(); void Reset(); +void Step(); } // namespace cpu } // namespace hal diff --git a/src/hal/watchdog.h b/src/hal/watchdog.h index a560c14..2a2459e 100644 --- a/src/hal/watchdog.h +++ b/src/hal/watchdog.h @@ -37,8 +37,12 @@ public: uint8_t prescalerBits = 0; uint32_t ticks = 1; switch (timeout) { + case 250: + prescalerBits = 4; + break; case 8000: prescalerBits = 9; + break; } configuration config = { prescalerBits, static_cast(ticks - 1) }; diff --git a/src/main.cpp b/src/main.cpp index 9cbf362..476c15e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -100,10 +100,10 @@ void EVENT_USB_Device_ConfigurationChanged(void) { ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface); // LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR); - char str1[] = "ready\n"; - char str0[] = "error\n"; - hal::usart::usart1.puts("EVENT_USB_Device_ConfigurationChanged:"); - hal::usart::usart1.puts(ConfigSuccess ? str1 : str0); + // char str1[] = "ready\n"; + // char str0[] = "error\n"; + // hal::usart::usart1.puts("EVENT_USB_Device_ConfigurationChanged:"); + // hal::usart::usart1.puts(ConfigSuccess ? str1 : str0); } /** Event handler for the library USB Control Request reception event. */ @@ -118,27 +118,28 @@ void EVENT_USB_Device_ControlRequest(void) { * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced */ void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t *const CDCInterfaceInfo) { - /* You can get changes to the virtual CDC lines in this callback; a common - use-case is to use the Data Terminal Ready (DTR) flag to enable and - disable CDC communications in your application when set to avoid the - application blocking while waiting for a host to become ready and read - in the pending data from the USB endpoints. - */ - hal::usart::usart1.puts("EVENT_CDC_Device_ControLineStateChanged "); - bool HostReady = (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR) != 0; - char str[50]; - sprintf_P(str, PSTR("DTR:%hu\n"), HostReady); - hal::usart::usart1.puts(str); + // Printing to serial from here will make Windows commit suicide when opening the port + + // hal::usart::usart1.puts("EVENT_CDC_Device_ControLineStateChanged "); + // bool HostReady = (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR) != 0; + // char str[50]; + // sprintf_P(str, PSTR("DTR:%hu\n"), HostReady); + // hal::usart::usart1.puts(str); } void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t *const CDCInterfaceInfo) { - hal::usart::usart1.puts("EVENT_CDC_Device_LineEncodingChanged "); - char str[50]; - sprintf_P(str, PSTR("baud:%lu\n"), CDCInterfaceInfo->State.LineEncoding.BaudRateBPS); - hal::usart::usart1.puts(str); + // Printing to serial from here will make Windows commit suicide when opening the port + + // hal::usart::usart1.puts("EVENT_CDC_Device_LineEncodingChanged "); + // char str[50]; + // sprintf_P(str, PSTR("baud:%lu\n"), CDCInterfaceInfo->State.LineEncoding.BaudRateBPS); + // hal::usart::usart1.puts(str); + if (CDCInterfaceInfo->State.LineEncoding.BaudRateBPS == 1200) { - *(uint16_t *)0x0800U = 0x7777; - hal::cpu::Reset(); + // *(uint16_t *)0x0800U = 0x7777; //old bootloader? + *(uint16_t *)(RAMEND-1) = 0x7777; + hal::cpu::resetPending = true; + hal::watchdog::Enable(hal::watchdog::configuration::compute(250)); } } } @@ -251,6 +252,8 @@ void setup() { USB_Init(); + _delay_ms(100); + /// Turn off all leds for (uint8_t i = 0; i < config::toolCount; i++) { ml::leds.SetMode(i, ml::Color::green, ml::Mode::off); @@ -477,7 +480,9 @@ void loop() { ms::selector.Step(); mui::userInput.Step(); currentCommand->Step(); + hal::cpu::Step(); + CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); From c2d705e612a53050339f7228889c7ada06141085 Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Mon, 20 Sep 2021 19:10:27 +0200 Subject: [PATCH 15/20] Fix unit tests --- lib/CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 13893f0..17bbcac 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,4 +1,5 @@ if(NOT CMAKE_CROSSCOMPILING) add_subdirectory(Catch2) +else() + include(AddLufa.cmake) endif() -include(AddLufa.cmake) From b6d630d1c990893af4013f7f775c84bf29d69ec5 Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Thu, 23 Sep 2021 13:06:36 +0200 Subject: [PATCH 16/20] Clean up the code + introduce USB CDC module ... and tune some more namespaces --- src/hal/adc.h | 2 + src/hal/watchdog.h | 2 + src/main.cpp | 137 +++---------------------------------- src/modules/CMakeLists.txt | 3 +- src/modules/usb_cdc.cpp | 129 ++++++++++++++++++++++++++++++++++ src/modules/usb_cdc.h | 26 +++++++ 6 files changed, 171 insertions(+), 128 deletions(-) create mode 100644 src/modules/usb_cdc.cpp create mode 100644 src/modules/usb_cdc.h diff --git a/src/hal/adc.h b/src/hal/adc.h index bacbbfa..5ea8040 100644 --- a/src/hal/adc.h +++ b/src/hal/adc.h @@ -12,3 +12,5 @@ uint16_t ReadADC(uint8_t channel); } // namespace adc } // namespace hal + +namespace ha = hal::adc; diff --git a/src/hal/watchdog.h b/src/hal/watchdog.h index 2a2459e..8fe8f83 100644 --- a/src/hal/watchdog.h +++ b/src/hal/watchdog.h @@ -57,3 +57,5 @@ void Reset(); } // namespace watchdog } // namespace hal + +namespace hwd = hal::watchdog; diff --git a/src/main.cpp b/src/main.cpp index 476c15e..e3edae6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,12 +6,6 @@ #include "hal/usart.h" #include "hal/watchdog.h" -extern "C" { -#include "lufa_config.h" -#include "Descriptors.h" -#include "lufa/LUFA/Drivers/USB/USB.h" -} - #include "pins.h" #include #include @@ -27,6 +21,7 @@ extern "C" { #include "modules/user_input.h" #include "modules/timebase.h" #include "modules/motion.h" +#include "modules/usb_cdc.h" #include "logic/command_base.h" #include "logic/cut_filament.h" @@ -38,112 +33,8 @@ extern "C" { #include "logic/unload_filament.h" #include "version.h" - #include "panic.h" -extern "C" { - -/** 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 = INTERFACE_ID_CDC_CCI, - .DataINEndpoint = { - .Address = CDC_TX_EPADDR, - .Size = CDC_TXRX_EPSIZE, - .Type = EP_TYPE_BULK, - .Banks = 1, - }, - .DataOUTEndpoint = { - .Address = CDC_RX_EPADDR, - .Size = CDC_TXRX_EPSIZE, - .Type = EP_TYPE_BULK, - .Banks = 1, - }, - .NotificationEndpoint = { - .Address = CDC_NOTIFICATION_EPADDR, - .Size = CDC_NOTIFICATION_EPSIZE, - .Type = EP_TYPE_INTERRUPT, - .Banks = 1, - }, - }, -}; - -// void testFunc1(uint8_t i) { -// char str[30]; -// sprintf_P(str, PSTR("testFunc1(%hu)\n"), i); -// hal::usart::usart1.puts(str); -// } - -// void testFunc2(uint8_t i) { -// char str[30]; -// sprintf_P(str, PSTR("testFunc2(%hu)\n"), i); -// hal::usart::usart1.puts(str); -// } - -/** Event handler for the library USB Connection event. */ -void EVENT_USB_Device_Connect(void) { - hal::usart::usart1.puts("EVENT_USB_Device_Connect\n"); -} - -/** Event handler for the library USB Disconnection event. */ -void EVENT_USB_Device_Disconnect(void) { - hal::usart::usart1.puts("EVENT_USB_Device_Disconnect\n"); -} - -/** 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); - // char str1[] = "ready\n"; - // char str0[] = "error\n"; - // hal::usart::usart1.puts("EVENT_USB_Device_ConfigurationChanged:"); - // hal::usart::usart1.puts(ConfigSuccess ? str1 : str0); -} - -/** Event handler for the library USB Control Request reception event. */ -void EVENT_USB_Device_ControlRequest(void) { - // hal::usart::usart1.puts("EVENT_USB_Device_ControlRequest\n"); - CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface); -} - -/** CDC class driver callback function the processing of changes to the virtual - * control lines sent from the host.. - * - * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced - */ -void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t *const CDCInterfaceInfo) { - // Printing to serial from here will make Windows commit suicide when opening the port - - // hal::usart::usart1.puts("EVENT_CDC_Device_ControLineStateChanged "); - // bool HostReady = (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR) != 0; - // char str[50]; - // sprintf_P(str, PSTR("DTR:%hu\n"), HostReady); - // hal::usart::usart1.puts(str); -} - -void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t *const CDCInterfaceInfo) { - // Printing to serial from here will make Windows commit suicide when opening the port - - // hal::usart::usart1.puts("EVENT_CDC_Device_LineEncodingChanged "); - // char str[50]; - // sprintf_P(str, PSTR("baud:%lu\n"), CDCInterfaceInfo->State.LineEncoding.BaudRateBPS); - // hal::usart::usart1.puts(str); - - if (CDCInterfaceInfo->State.LineEncoding.BaudRateBPS == 1200) { - // *(uint16_t *)0x0800U = 0x7777; //old bootloader? - *(uint16_t *)(RAMEND-1) = 0x7777; - hal::cpu::resetPending = true; - hal::watchdog::Enable(hal::watchdog::configuration::compute(250)); - } -} -} - /// Global instance of the protocol codec static mp::Protocol protocol; @@ -200,24 +91,19 @@ void TmpPlayground() { /// Called before entering the loop() function /// Green LEDs signalize the progress of initialization. If anything goes wrong we shall turn on a red LED void setup() { - using namespace hal; - - cpu::Init(); + hal::cpu::Init(); mt::timebase.Init(); - watchdog::Enable(watchdog::configuration::compute(8000)); //set 8s timeout + hwd::Enable(hwd::configuration::compute(8000)); //set 8s timeout mg::globals.Init(); - // watchdog init - - shr16::shr16.Init(); + hal::shr16::shr16.Init(); ml::leds.SetMode(4, ml::Color::green, ml::Mode::on); ml::leds.Step(); - // @@TODO if the shift register doesn't work we really can't signalize anything, only internal variables will be accessible if the UART works - + // if the shift register doesn't work we really can't signalize anything, only internal variables will be accessible if the UART works hu::USART::USART_InitTypeDef usart_conf = { .rx_pin = USART_RX, .tx_pin = USART_TX, @@ -229,7 +115,7 @@ void setup() { // @@TODO if both shift register and the UART are dead, we are sitting ducks :( - spi::SPI_InitTypeDef spi_conf = { + hal::spi::SPI_InitTypeDef spi_conf = { .miso_pin = TMC2130_SPI_MISO_PIN, .mosi_pin = TMC2130_SPI_MOSI_PIN, .sck_pin = TMC2130_SPI_SCK_PIN, @@ -238,7 +124,7 @@ void setup() { .cpha = 1, .cpol = 1, }; - spi::Init(SPI0, &spi_conf); + hal::spi::Init(SPI0, &spi_conf); ml::leds.SetMode(2, ml::Color::green, ml::Mode::on); ml::leds.Step(); @@ -246,11 +132,11 @@ void setup() { ml::leds.SetMode(1, ml::Color::green, ml::Mode::on); ml::leds.Step(); - adc::Init(); + ha::Init(); ml::leds.SetMode(0, ml::Color::green, ml::Mode::on); ml::leds.Step(); - USB_Init(); + mu::cdc.Init(); _delay_ms(100); @@ -481,10 +367,7 @@ void loop() { mui::userInput.Step(); currentCommand->Step(); hal::cpu::Step(); - - CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); - CDC_Device_USBTask(&VirtualSerial_CDC_Interface); - USB_USBTask(); + mu::cdc.Step(); hal::watchdog::Reset(); } diff --git a/src/modules/CMakeLists.txt b/src/modules/CMakeLists.txt index 139f1e0..47ff979 100644 --- a/src/modules/CMakeLists.txt +++ b/src/modules/CMakeLists.txt @@ -11,9 +11,10 @@ target_sources( motion.cpp movable_base.cpp permanent_storage.cpp + pulse_gen.cpp selector.cpp speed_table.cpp timebase.cpp + usb_cdc.cpp user_input.cpp - pulse_gen.cpp ) diff --git a/src/modules/usb_cdc.cpp b/src/modules/usb_cdc.cpp new file mode 100644 index 0000000..052ed66 --- /dev/null +++ b/src/modules/usb_cdc.cpp @@ -0,0 +1,129 @@ +#include "usb_cdc.h" +#include "../hal/cpu.h" +#include "../hal/usart.h" +#include "../hal/watchdog.h" + +extern "C" { +#include "lufa_config.h" +#include "Descriptors.h" +#include "lufa/LUFA/Drivers/USB/USB.h" + +/** 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 = INTERFACE_ID_CDC_CCI, + .DataINEndpoint = { + .Address = CDC_TX_EPADDR, + .Size = CDC_TXRX_EPSIZE, + .Type = EP_TYPE_BULK, + .Banks = 1, + }, + .DataOUTEndpoint = { + .Address = CDC_RX_EPADDR, + .Size = CDC_TXRX_EPSIZE, + .Type = EP_TYPE_BULK, + .Banks = 1, + }, + .NotificationEndpoint = { + .Address = CDC_NOTIFICATION_EPADDR, + .Size = CDC_NOTIFICATION_EPSIZE, + .Type = EP_TYPE_INTERRUPT, + .Banks = 1, + }, + }, +}; + +// void testFunc1(uint8_t i) { +// char str[30]; +// sprintf_P(str, PSTR("testFunc1(%hu)\n"), i); +// hal::usart::usart1.puts(str); +// } + +// void testFunc2(uint8_t i) { +// char str[30]; +// sprintf_P(str, PSTR("testFunc2(%hu)\n"), i); +// hal::usart::usart1.puts(str); +// } + +/** Event handler for the library USB Connection event. */ +void EVENT_USB_Device_Connect(void) { + hal::usart::usart1.puts("EVENT_USB_Device_Connect\n"); +} + +/** Event handler for the library USB Disconnection event. */ +void EVENT_USB_Device_Disconnect(void) { + hal::usart::usart1.puts("EVENT_USB_Device_Disconnect\n"); +} + +/** 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); + // char str1[] = "ready\n"; + // char str0[] = "error\n"; + // hal::usart::usart1.puts("EVENT_USB_Device_ConfigurationChanged:"); + // hal::usart::usart1.puts(ConfigSuccess ? str1 : str0); +} + +/** Event handler for the library USB Control Request reception event. */ +void EVENT_USB_Device_ControlRequest(void) { + // hal::usart::usart1.puts("EVENT_USB_Device_ControlRequest\n"); + CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface); +} + +/** CDC class driver callback function the processing of changes to the virtual + * control lines sent from the host.. + * + * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced + */ +void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t *const CDCInterfaceInfo) { + // Printing to serial from here will make Windows commit suicide when opening the port + + // hal::usart::usart1.puts("EVENT_CDC_Device_ControLineStateChanged "); + // bool HostReady = (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR) != 0; + // char str[50]; + // sprintf_P(str, PSTR("DTR:%hu\n"), HostReady); + // hal::usart::usart1.puts(str); +} + +void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t *const CDCInterfaceInfo) { + // Printing to serial from here will make Windows commit suicide when opening the port + + // hal::usart::usart1.puts("EVENT_CDC_Device_LineEncodingChanged "); + // char str[50]; + // sprintf_P(str, PSTR("baud:%lu\n"), CDCInterfaceInfo->State.LineEncoding.BaudRateBPS); + // hal::usart::usart1.puts(str); + + if (CDCInterfaceInfo->State.LineEncoding.BaudRateBPS == 1200) { + // *(uint16_t *)0x0800U = 0x7777; //old bootloader? + *(uint16_t *)(RAMEND - 1) = 0x7777; + hal::cpu::resetPending = true; + hal::watchdog::Enable(hal::watchdog::configuration::compute(250)); + } +} +} + +namespace modules { +namespace usb { + +CDC cdc; + +void CDC::Init() { + USB_Init(); +} + +void CDC::Step() { + CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); + CDC_Device_USBTask(&VirtualSerial_CDC_Interface); + USB_USBTask(); +} + +} // namespace usb + +} // namespace modules diff --git a/src/modules/usb_cdc.h b/src/modules/usb_cdc.h new file mode 100644 index 0000000..a64e254 --- /dev/null +++ b/src/modules/usb_cdc.h @@ -0,0 +1,26 @@ +#pragma once +#include + +namespace modules { + +/// The usb namespace provides all necessary facilities related to the USB interface. +namespace usb { + +class CDC { +public: + constexpr inline CDC() {} + + void Init(); + + void Step(); + +private: +}; + +/// The one and only instance of Selector in the FW +extern CDC cdc; + +} // namespace usb +} // namespace modules + +namespace mu = modules::usb; From cddc16dc2e1b8dd16e42641cba5bffd534db0e02 Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Thu, 23 Sep 2021 13:14:28 +0200 Subject: [PATCH 17/20] Tune USB CDC class + doc --- src/modules/usb_cdc.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/modules/usb_cdc.h b/src/modules/usb_cdc.h index a64e254..9ffe938 100644 --- a/src/modules/usb_cdc.h +++ b/src/modules/usb_cdc.h @@ -8,16 +8,16 @@ namespace usb { class CDC { public: - constexpr inline CDC() {} + constexpr inline CDC() = default; + /// Calls USB_Init from the LUFA library void Init(); + /// Calls USB_USBTask from the LUFA library - basically takes care about the runtime of USB CDC operation void Step(); - -private: }; -/// The one and only instance of Selector in the FW +/// The one and only instance of USB CDC in the FW extern CDC cdc; } // namespace usb From e35ae30470542940f578fb004aabd0ec1aae2c05 Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Thu, 23 Sep 2021 14:24:07 +0300 Subject: [PATCH 18/20] Remove some debug code --- src/modules/usb_cdc.cpp | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/modules/usb_cdc.cpp b/src/modules/usb_cdc.cpp index 052ed66..913ae66 100644 --- a/src/modules/usb_cdc.cpp +++ b/src/modules/usb_cdc.cpp @@ -36,18 +36,6 @@ USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface = { }, }; -// void testFunc1(uint8_t i) { -// char str[30]; -// sprintf_P(str, PSTR("testFunc1(%hu)\n"), i); -// hal::usart::usart1.puts(str); -// } - -// void testFunc2(uint8_t i) { -// char str[30]; -// sprintf_P(str, PSTR("testFunc2(%hu)\n"), i); -// hal::usart::usart1.puts(str); -// } - /** Event handler for the library USB Connection event. */ void EVENT_USB_Device_Connect(void) { hal::usart::usart1.puts("EVENT_USB_Device_Connect\n"); From 10e08021fbe95ea91808a4a2a21930f728434bb2 Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Thu, 23 Sep 2021 13:40:15 +0200 Subject: [PATCH 19/20] Another fix format --- lib/Descriptors.h | 95 ++++++++++++++++++++++------------------------ src/CMakeLists.txt | 17 ++++----- 2 files changed, 53 insertions(+), 59 deletions(-) diff --git a/lib/Descriptors.h b/lib/Descriptors.h index 0325c7d..bbf506f 100644 --- a/lib/Descriptors.h +++ b/lib/Descriptors.h @@ -36,75 +36,72 @@ #ifndef _DESCRIPTORS_H_ #define _DESCRIPTORS_H_ - /* Includes: */ - #include +/* Includes: */ +#include - #include "lufa/LUFA/Drivers/USB/USB.h" +#include "lufa/LUFA/Drivers/USB/USB.h" - /* Macros: */ - /** Endpoint address of the CDC device-to-host notification IN endpoint. */ - #define CDC_NOTIFICATION_EPADDR (ENDPOINT_DIR_IN | 2) +/* 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 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) +/** 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 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 +/** 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 +/* 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; +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 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; + // 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 +/** 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 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 +/** 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 */ - }; +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); +/* 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 - diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fce3c5c..e8bf73c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,7 +1,4 @@ -target_sources( - firmware - PRIVATE main.cpp -) +target_sources(firmware PRIVATE main.cpp) target_link_libraries(firmware LUFA) @@ -9,12 +6,12 @@ set_property( SOURCE src/version.c APPEND PROPERTY COMPILE_DEFINITIONS - FW_BUILD_NUMBER=${BUILD_NUMBER} - FW_VERSION_FULL=${PROJECT_VERSION_FULL} - FW_VERSION=${PROJECT_VERSION} - FW_VERSION_SUFFIX=${PROJECT_VERSION_SUFFIX} - FW_VERSION_SUFFIX_SHORT=${PROJECT_VERSION_SUFFIX_SHORT} -) + FW_BUILD_NUMBER=${BUILD_NUMBER} + FW_VERSION_FULL=${PROJECT_VERSION_FULL} + FW_VERSION=${PROJECT_VERSION} + FW_VERSION_SUFFIX=${PROJECT_VERSION_SUFFIX} + FW_VERSION_SUFFIX_SHORT=${PROJECT_VERSION_SUFFIX_SHORT} + ) add_subdirectory(hal) add_subdirectory(logic) From 57a3cfa109e48044361ff9fe5ed24d76521df0e2 Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Thu, 23 Sep 2021 13:43:18 +0200 Subject: [PATCH 20/20] Fix formatting --- lib/AddLufa.cmake | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/AddLufa.cmake b/lib/AddLufa.cmake index 84be75a..dab513b 100644 --- a/lib/AddLufa.cmake +++ b/lib/AddLufa.cmake @@ -1,17 +1,17 @@ add_library( LUFA + Descriptors.c lufa/LUFA/Drivers/USB/Class/Device/CDCClassDevice.c lufa/LUFA/Drivers/USB/Core/AVR8/Device_AVR8.c + lufa/LUFA/Drivers/USB/Core/AVR8/Endpoint_AVR8.c + lufa/LUFA/Drivers/USB/Core/AVR8/EndpointStream_AVR8.c lufa/LUFA/Drivers/USB/Core/AVR8/USBController_AVR8.c lufa/LUFA/Drivers/USB/Core/AVR8/USBInterrupt_AVR8.c - lufa/LUFA/Drivers/USB/Core/AVR8/Endpoint_AVR8.c lufa/LUFA/Drivers/USB/Core/ConfigDescriptors.c - lufa/LUFA/Drivers/USB/Core/Events.c lufa/LUFA/Drivers/USB/Core/DeviceStandardReq.c + lufa/LUFA/Drivers/USB/Core/Events.c lufa/LUFA/Drivers/USB/Core/USBTask.c - lufa/LUFA/Drivers/USB/Core/AVR8/EndpointStream_AVR8.c - Descriptors.c -) + ) target_include_directories(LUFA PRIVATE . lufa) target_compile_options(LUFA PRIVATE -include lufa_config.h)