Skip to content

Commit

Permalink
USBasp conditional compilation added.
Browse files Browse the repository at this point in the history
By default compiles a FW with MS 2.0 BOS Descriptor support. No TPI, no HID UART / SerialWrite support.

To add TPI functionality, run make ( or edit the Makefile ) with TPI variable not empty. i.e. make TPI=On main.hex .

To add HID UART and SerialWrite, run make ( or edit the Makefile ) with HIDUART variable not empty. i.e. make HIDUART=On main.hex .

Added for testing PDI with stock windows avrdude ( builded with MSVC ).
  • Loading branch information
dioannidis committed Aug 16, 2023
1 parent f1748a9 commit e944dd4
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 14 deletions.
16 changes: 15 additions & 1 deletion firmware/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ TARGET ?= atmega8
HFUSE=0xC9
LFUSE=0xDF
CLOCK=12000000
TPI=
HIDUART=

# ISP=ponyser PORT=/dev/ttyS1
# ISP=stk500 PORT=/dev/ttyS1
Expand All @@ -34,12 +36,24 @@ help:
@echo " LFUSE=${LFUSE}"
@echo " HFUSE=${HFUSE}"
@echo " CLOCK=${CLOCK}"
@echo " TPI=$(if ifeq (${TPI},),Off,On)"
@echo " HIDUART=$(if ifeq (${HIDUART},),Off,On)"
@echo " ISP=${ISP}"
@echo " PORT=${PORT}"

COMPILE = avr-gcc -Wall -Wextra -fno-move-loop-invariants -fno-tree-scev-cprop -fno-inline-small-functions -Os -Iusbdrv -I. -mmcu=$(TARGET) -DF_CPU=$(CLOCK) # -DDEBUG_LEVEL=1

OBJECTS = usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o isp.o clock.o tpi.o main.o uart.o serialnumber.o
OBJECTS = usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o isp.o clock.o main.o

ifneq ($(TPI),)
COMPILE += -D__TPI__
OBJECTS += tpi.o
endif

ifneq ($(HIDUART),)
COMPILE += -D__HIDUART__
OBJECTS += uart.o serialnumber.o
endif

.c.o:
$(COMPILE) -c $< -o $@
Expand Down
69 changes: 57 additions & 12 deletions firmware/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,20 @@
#include "oddebug.h"
#include "usbasp.h"
#include "usbdrv.h"
#include "uart.h"
#include "usb_descriptors.h"
#include "isp.h"
#include "clock.h"

#ifdef __TPI__
#include "tpi.h"
#include "tpi_defs.h"
#endif

#ifdef __HIDUART__
#include "uart.h"
#include "serialnumber.h"
#endif


#if F_CPU == 12000000L
#define CAP_CLOCK USBASP_CAP_12MHZ_CLOCK
Expand All @@ -46,18 +53,26 @@ static uchar featureReport[8] = {
0, /* Prescaler Low byte */
0, /* Prescaler High byte */
0, /* Bitmask Parity, StopBit and DataBit */
0, /* Reserved */
USBASP_CAP_0_TPI | USBASP_CAP_HIDUART | USBASP_CAP_SNHIDUPDATE, /* Device Capabilities */
0, /* Reserved */
0
#ifdef __TPI__
| USBASP_CAP_0_TPI
#endif
#ifdef __HIDUART__
| USBASP_CAP_HIDUART | USBASP_CAP_SNHIDUPDATE
#endif
, /* Device Capabilities */
CAP_CLOCK, /* Device Crystal */
0, /* Reserved */
0 /* Reserved */
};

static uchar replyBuffer[8];
#ifdef __HIDUART__
static uchar interruptBuffer[8];
static uchar monitorBuffer[8];

static uchar uart_state = UART_STATE_DISABLED;
static uchar monitorBuffer[8];
#endif

static uchar prog_state = PROG_STATE_IDLE;
uchar prog_sck = USBASP_ISP_SCK_AUTO;
Expand Down Expand Up @@ -111,8 +126,9 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) {

if (data[1] == USBASP_FUNC_CONNECT) {

#ifdef __HIDUART__
uart_state = uart_disable(); // make it not interfere.

#endif
/* set SCK speed */
ispSetSCKOption(prog_sck);

Expand All @@ -130,7 +146,9 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) {
UART was open and it was interrupted by an ISP connect command.
Re enable the UART */

#ifdef __HIDUART__
uart_state = uart_config(featureReport);
#endif

} else if (data[1] == USBASP_FUNC_TRANSMIT) {
replyBuffer[0] = ispTransmit(data[2]);
Expand Down Expand Up @@ -196,6 +214,8 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) {
replyBuffer[0] = 0;
len = 1;

#ifdef __TPI__

} else if (data[1] == USBASP_FUNC_TPI_CONNECT) {
tpi_dly_cnt = data[2] | (data[3] << 8);

Expand Down Expand Up @@ -250,6 +270,8 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) {
prog_state = PROG_STATE_TPI_WRITE;
len = USB_NO_MSG; /* multiple out */

#endif

} else if(data[1] == USBASP_FUNC_GETCAPABILITIES) {
replyBuffer[0] = featureReport[4];
replyBuffer[1] = featureReport[5];
Expand Down Expand Up @@ -320,10 +342,16 @@ uchar usbFunctionRead(uchar *data, uchar len) {

/* check if programmer is in correct read state */
if ((prog_state != PROG_STATE_READFLASH) && (prog_state
!= PROG_STATE_READEEPROM) && (prog_state != PROG_STATE_TPI_READ)) {
!= PROG_STATE_READEEPROM)
#ifdef __TPI__
&& (prog_state != PROG_STATE_TPI_READ)
#endif
) {
return 0xff;
}

#ifdef __TPI__

/* fill packet TPI mode */
if(prog_state == PROG_STATE_TPI_READ)
{
Expand All @@ -332,6 +360,8 @@ uchar usbFunctionRead(uchar *data, uchar len) {
return len;
}

#endif

/* fill packet ISP mode */
if((prog_state == PROG_STATE_READFLASH) || (prog_state == PROG_STATE_READEEPROM)) {
for (i = 0; i < len; i++) {
Expand Down Expand Up @@ -362,11 +392,16 @@ uchar usbFunctionWrite(uchar *data, uchar len) {

/* check if programmer is in correct write state */
if ((prog_state != PROG_STATE_WRITEFLASH) && (prog_state
!= PROG_STATE_WRITEEEPROM) && (prog_state != PROG_STATE_TPI_WRITE)
!= PROG_STATE_WRITEEEPROM)
#ifdef __TPI__
&& (prog_state != PROG_STATE_TPI_WRITE)
#endif
&& (prog_state != PROG_STATE_SET_REPORT)) {
return 0xff;
}

#ifdef __TPI__

if (prog_state == PROG_STATE_TPI_WRITE)
{
tpi_write_block(prog_address, data, len);
Expand All @@ -380,6 +415,8 @@ uchar usbFunctionWrite(uchar *data, uchar len) {
return 0;
}

#endif

if((prog_state == PROG_STATE_WRITEFLASH) || (prog_state == PROG_STATE_WRITEEEPROM)) {
for (i = 0; i < len; i++) {

Expand Down Expand Up @@ -442,15 +479,16 @@ uchar usbFunctionWrite(uchar *data, uchar len) {
/* To enable the UART the baud needs to be non zero.
Meaning that to disable the UART communication send a set feature report
with the prescaler ( first 2 bytes ) zeroed. */

#ifdef __HIDUART__
uart_state = uart_config(featureReport);

}
break;
case 1: {

serialNumberWrite(data);


#endif

}
break;
default:
Expand Down Expand Up @@ -483,6 +521,8 @@ uchar usbFunctionWrite(uchar *data, uchar len) {
*
*/

#ifdef __HIDUART__

/* Host to device. Endpoint 1 Output */
void usbFunctionWriteOut(uchar *data, uchar len){

Expand Down Expand Up @@ -593,6 +633,8 @@ void HID_EP_3_IN(){
usbSetInterrupt3(monitorBuffer, sizeof(monitorBuffer));
}

#endif

int main(void) {

/* enable debug if DEBUG_LEVEL > 0 */
Expand All @@ -616,6 +658,7 @@ int main(void) {
sei();
for (;;) {

#ifdef __HIDUART__
/* Enable transmit interrupt if tx buffer has data
and the transmit interrupt is disabled. */
if(!(USBASPUART_UCSRB & (1<<USBASPUART_UDRIE))
Expand All @@ -630,16 +673,18 @@ int main(void) {
usbEnableAllRequests();
}
}

#endif
usbPoll();

#ifdef __HIDUART__
if (usbInterruptIsReady()) {
HID_EP_1_IN();
}

if (usbInterruptIsReady3()) {
HID_EP_3_IN();
}
#endif

}

Expand Down
94 changes: 94 additions & 0 deletions firmware/usb_descriptors.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ PROGMEM const char usbDescriptorDevice[] = {
1, /* number of configurations */
};

#ifdef __HIDUART__

PROGMEM const char usbDescriptorConfiguration[] = {
9, /* sizeof(usbDescrConfig): length of descriptor in bytes */
USBDESCR_CONFIG, /* descriptor type */
Expand Down Expand Up @@ -300,4 +302,96 @@ PROGMEM const char usbDescriptorHidReport[] = {
0xC0 // END_COLLECTION
};

#else

PROGMEM const char usbDescriptorConfiguration[] = {
9, /* sizeof(usbDescrConfig): length of descriptor in bytes */
USBDESCR_CONFIG, /* descriptor type */
0x12,
0, /* total length of data returned (including inlined descriptors) */
1, /* number of interfaces in this configuration */
1, /* index of this configuration */
0, /* configuration name string index */
#if USB_CFG_IS_SELF_POWERED
(1 << 7) | USBATTR_SELFPOWER, /* attributes */
#else
(1 << 7), /* attributes */
#endif
USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */

/* interface descriptor follows inline: */

9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
USBDESCR_INTERFACE, /* descriptor type */
0, /* index of this interface */
0, /* alternate setting for this interface */
0, /* endpoints excl 0: number of endpoint descriptors to follow */
0xFF, /* USB_CFG_INTERFACE_CLASS */
0, /* USB_CFG_INTERFACE_SUBCLASS */
0, /* USB_CFG_INTERFACE_PROTOCOL */
2 /* string index for interface */

};

/* BOS Descriptor */
PROGMEM const char BOS_DESCRIPTOR[] = {

/* BOS Descriptor Header */
0x05, /* Size of descriptor */
USBDESCR_BOS, /* Descriptor type */
0x21, 0x00, /* Length of this descriptor and all of its sub descriptors */
0x01, /* The number of separate device capability descriptors in the BOS */

/* Device Capability Descriptor - Platform */
0x1C, /* Length */
USBDESCR_DEVICE_CAPABILITY_TYPE, /* Descriptor Type */
USBDESCR_DEVICE_CAPABILITY_PLATFORM, /* Device Capability Type */
0x00, /* Reserved */
0xDF, 0x60, 0xDD, 0xD8, 0x89, 0x45, 0xC7, 0x4C, /* MS OS 2.0 Platform Capability */
0x9C, 0xD2, 0x65, 0x9D, 0x9E, 0x64, 0x8A, 0x9F, /* {D8DD60DF-4589-4CC7-9CD2-659D9E648A9F} */
0x00, 0x00, 0x03, 0x06, /* Windows Version - Windows 8.1 or later */
0x9E, 0x00, /* Size of MS OS 2.0 Descriptor set */
VENDOR_CODE, /* Vendor Request Code */
0x00 /* Alternate Enumeration support - 0 No support */

};

/* Microsft OS 2.0 Descriptor Set */
PROGMEM const char MS_2_0_OS_DESCRIPTOR_SET[] = {

/* MS OS 2.0 Descriptor Set Header */
0x0A, 0x00, /* Size of descriptor */
MS_OS_20_SET_HEADER_DESCRIPTOR, /* Descriptor Type */
0x00, 0x00, 0x03, 0x06, /* Windows Version - Windows 8.1 or later */
0x9E, 0x00, /* Size of MS OS 2.0 Descriptor set */

/* MS OS 2.0 Compatible ID Descriptor */
0x14, 0x00, /* Size of descriptor */
MS_OS_20_FEATURE_COMPATIBLE_ID, /* Descriptor Type */
'W','I','N','U','S','B', 0x00, 0x00, /* Windows string Compatible ID */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Windows string SubCompatible ID */

/* MS OS 2.0 Registry Property Descriptor */
0x80, 0x00, /* Size of descriptor */
MS_OS_20_FEATURE_REG_PROPERTY, /* Descriptor Type */
MS_OS_20_REG_PROPERTY_REG_SZ, /* The type of registry property */
0x28, 0x00, /* The length of the property name */
'D',0x00,'e',0x00,'v',0x00,'i',0x00,'c',0x00, /* The name of the property name */
'e',0x00,'I',0x00,'n',0x00,'t',0x00,'e',0x00, /* -//- */
'r',0x00,'f',0x00,'a',0x00,'c',0x00,'e',0x00, /* -//- */
'G',0x00,'U',0x00,'I',0x00,'D',0x00,0x00,0x00, /* -//- */
0x4e, 0x00, /* The length of property data */
'{',0x00,'A',0x00,'D',0x00,'5',0x00,'7',0x00, /* Property data */
'D',0x00,'3',0x00,'B',0x00,'9',0x00,'-',0x00, /* -//- */
'1',0x00,'1',0x00,'6',0x00,'6',0x00,'-',0x00, /* -//- */
'4',0x00,'3',0x00,'F',0x00,'8',0x00,'-',0x00, /* -//- */
'8',0x00,'7',0x00,'9',0x00,'0',0x00,'-',0x00, /* -//- */
'0',0x00,'B',0x00,'E',0x00,'1',0x00,'4',0x00, /* -//- */
'D',0x00,'D',0x00,'C',0x00,'7',0x00,'5',0x00, /* -//- */
'0',0x00,'4',0x00,'}',0x00,0x00,0x00 /* -//- */

};

#endif

#endif

0 comments on commit e944dd4

Please sign in to comment.