Skip to content

Commit 6f1613a

Browse files
committed
Added flexible RAM/PROGMEM descriptor
1 parent 7fab825 commit 6f1613a

File tree

6 files changed

+73
-16
lines changed

6 files changed

+73
-16
lines changed

avr/bootloaders/HoodLoader2/Config/AppConfig.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ along with Hoodloader2. If not, see <http://www.gnu.org/licenses/>.
6363
#define _APP_CONFIG_H_
6464

6565
// #define NO_BLOCK_SUPPORT
66+
#ifdef __AVR_ATmega32U4__
6667
#define NO_EEPROM_BYTE_SUPPORT
68+
#endif
6769
#define NO_FLASH_BYTE_SUPPORT
6870
// #define NO_LOCK_BYTE_WRITE_SUPPORT
6971

avr/bootloaders/HoodLoader2/Config/LUFAConfig.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ along with Hoodloader2. If not, see <http://www.gnu.org/licenses/>.
6464

6565
#if (ARCH == ARCH_AVR8)
6666

67+
// Pull in RAMEND definitions
68+
#include "avr/io.h"
69+
6770
/* Non-USB Related Configuration Tokens: */
6871
// #define DISABLE_TERMINAL_CODES
6972

@@ -86,8 +89,13 @@ along with Hoodloader2. If not, see <http://www.gnu.org/licenses/>.
8689
#define NO_SOF_EVENTS
8790

8891
/* USB Device Mode Driver Related Tokens: */
89-
// #define USE_RAM_DESCRIPTORS
92+
// Only use RAM Descriptors if we have enough ram
93+
#if ((RAMEND - RAMSTART) >= 512)
94+
#define USE_RAM_DESCRIPTORS
95+
#else
9096
#define USE_FLASH_DESCRIPTORS
97+
#endif
98+
9199
// #define USE_EEPROM_DESCRIPTORS
92100
#define NO_INTERNAL_SERIAL
93101
#define FIXED_CONTROL_ENDPOINT_SIZE 8

avr/bootloaders/HoodLoader2/Descriptors.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,21 @@ along with Hoodloader2. If not, see <http://www.gnu.org/licenses/>.
6565
#define ARDUINO_MEGA_PID 0x0042 // R3 (0010 R1)
6666
#define ARDUINO_MEGA_ADK_PID 0x0044 // R3 (003F R1)
6767

68+
// Only use RAM Descriptors if we have enough ram
69+
#ifdef USE_RAM_DESCRIPTORS
70+
#define DESCRIPTOR_PROGMEM
71+
#define STRING_PROGMEM(x) (x)
72+
#else // PROGMEM descriptors
73+
#define DESCRIPTOR_PROGMEM PROGMEM
74+
#define STRING_PROGMEM(x) pgm_read_byte(x)
75+
#endif
76+
6877
/** Device descriptor structure. This descriptor, located in SRAM memory, describes the overall
6978
* device characteristics, including the supported USB version, control endpoint size and the
7079
* number of device configurations. The descriptor is read out by the USB host when the enumeration
7180
* process begins.
7281
*/
73-
const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
82+
const USB_Descriptor_Device_t DESCRIPTOR_PROGMEM DeviceDescriptor =
7483
{
7584
.Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
7685

@@ -98,7 +107,7 @@ const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
98107
* and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
99108
* a configuration so that the host may correctly communicate with the USB device.
100109
*/
101-
const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
110+
const USB_Descriptor_Configuration_t DESCRIPTOR_PROGMEM ConfigurationDescriptor =
102111
{
103112
.Config =
104113
{
@@ -207,26 +216,26 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
207216
* the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
208217
* via the language ID table available at USB.org what languages the device supports for its string descriptors.
209218
*/
210-
const USB_Descriptor_String_t PROGMEM LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
219+
const USB_Descriptor_String_t DESCRIPTOR_PROGMEM LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
211220

212221
/** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
213222
* form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
214223
* Descriptor.
215224
*/
216-
const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(L"NicoHood");
225+
const USB_Descriptor_String_t DESCRIPTOR_PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(L"NicoHood");
217226

218227
/** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
219228
* and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
220229
* Descriptor.
221230
*/
222231
#if (PRODUCTID == ARDUINO_UNO_PID)
223-
const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"HoodLoader2 Uno");
232+
const USB_Descriptor_String_t DESCRIPTOR_PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"HoodLoader2 Uno");
224233
#elif (PRODUCTID == ARDUINO_MEGA_PID)
225-
const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"HoodLoader2 Mega");
234+
const USB_Descriptor_String_t DESCRIPTOR_PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"HoodLoader2 Mega");
226235
#elif (PRODUCTID == ARDUINO_ADK_PID)
227-
const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"HoodLoader2 ADK");
236+
const USB_Descriptor_String_t DESCRIPTOR_PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"HoodLoader2 ADK");
228237
#else
229-
const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"HoodLoader2 Lufa");
238+
const USB_Descriptor_String_t DESCRIPTOR_PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"HoodLoader2 Lufa");
230239
#endif
231240

232241
/** This function is called by the library when in device mode, and must be overridden (see LUFA library "USB Descriptors"
@@ -259,17 +268,17 @@ uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
259268
if (DescriptorNumber == STRING_ID_Language)
260269
{
261270
Address = &LanguageString;
262-
Size = pgm_read_byte(&LanguageString.Header.Size);
271+
Size = STRING_PROGMEM(&LanguageString.Header.Size);
263272
}
264273
else if (DescriptorNumber == STRING_ID_Manufacturer)
265274
{
266275
Address = &ManufacturerString;
267-
Size = pgm_read_byte(&ManufacturerString.Header.Size);
276+
Size = STRING_PROGMEM(&ManufacturerString.Header.Size);
268277
}
269278
else if (DescriptorNumber == STRING_ID_Product)
270279
{
271280
Address = &ProductString;
272-
Size = pgm_read_byte(&ProductString.Header.Size);
281+
Size = STRING_PROGMEM(&ProductString.Header.Size);
273282
}
274283

275284
break;

avr/bootloaders/HoodLoader2/Descriptors.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ along with Hoodloader2. If not, see <http://www.gnu.org/licenses/>.
6060
#include <LUFA/Drivers/USB/USB.h>
6161

6262
#include "Config/AppConfig.h"
63+
#include "Config/LUFAConfig.h"
6364

6465
/* Macros: */
6566
#if defined(__AVR_AT90USB1287__)

avr/bootloaders/HoodLoader2/HoodLoader2.c

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,45 @@ void EVENT_USB_Device_ControlRequest(void)
280280
{
281281
Endpoint_ClearSETUP();
282282

283-
// Read the line coding data in from the host into the global struct
284-
Endpoint_Read_Control_Stream_LE(&LineEncoding, sizeof(CDC_LineEncoding_t));
283+
// Read the line coding data in from the host into the global struct (made inline)
284+
//Endpoint_Read_Control_Stream_LE(&LineEncoding, sizeof(CDC_LineEncoding_t));
285+
286+
uint8_t Length = sizeof(CDC_LineEncoding_t);
287+
uint8_t* DataStream = (uint8_t*)&LineEncoding;
288+
289+
bool skip = false;
290+
while (Length)
291+
{
292+
uint8_t USB_DeviceState_LCL = USB_DeviceState;
293+
294+
if ((USB_DeviceState_LCL == DEVICE_STATE_Unattached) || (USB_DeviceState_LCL == DEVICE_STATE_Suspended) || (Endpoint_IsSETUPReceived())){
295+
skip = true;
296+
break;
297+
}
298+
299+
if (Endpoint_IsOUTReceived())
300+
{
301+
while (Length && Endpoint_BytesInEndpoint())
302+
{
303+
*DataStream = Endpoint_Read_8();
304+
DataStream++;
305+
Length--;
306+
}
307+
308+
Endpoint_ClearOUT();
309+
}
310+
}
311+
312+
if (!skip)
313+
while (!(Endpoint_IsINReady()))
314+
{
315+
uint8_t USB_DeviceState_LCL = USB_DeviceState;
316+
317+
if ((USB_DeviceState_LCL == DEVICE_STATE_Unattached) || (USB_DeviceState_LCL == DEVICE_STATE_Suspended))
318+
break;
319+
}
320+
321+
// end of inline Endpoint_Read_Control_Stream_LE
285322

286323
Endpoint_ClearIN();
287324

avr/bootloaders/HoodLoader2/makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ TARGET = HoodLoader2
2121
SRC = $(TARGET).c Descriptors.c BootloaderAPI.c BootloaderAPITable.S $(LUFA_SRC_USB)
2222
LUFA_PATH = ../../../tools/lufa-LUFA-140928/LUFA
2323
REGS = -ffixed-r2 -ffixed-r3 -ffixed-r4 -ffixed-r5
24-
CC_FLAGS = -DUSE_LUFA_CONFIG_HEADER -IConfig/ $(HOODLOADER2_OPTS) -DBOOT_START_ADDR=$(BOOT_START_OFFSET) $(REGS) -flto
25-
LD_FLAGS = -Wl,--section-start=.text=$(BOOT_START_OFFSET) $(BOOT_API_LD_FLAGS),--section-start=.data=0x800280
24+
CC_FLAGS = -DUSE_LUFA_CONFIG_HEADER -IConfig/ $(HOODLOADER2_OPTS) -DBOOT_START_ADDR=$(BOOT_START_OFFSET) $(REGS) -flto -fuse-linker-plugin
25+
LD_FLAGS = -Wl,--section-start=.text=$(BOOT_START_OFFSET) $(BOOT_API_LD_FLAGS),--section-start=.data=0x800280 -flto -fuse-linker-plugin
2626

2727
#define LUFA_VID 0x03EB
2828
#define LUFA_PID 0x204A

0 commit comments

Comments
 (0)