Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XMega AVRISP2 #178

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7386afc
Endpoint function added
masterxq Dec 4, 2021
b2590d1
Ported a lot to xmega, compiling and USB detection works, tested pdi …
masterxq Dec 4, 2021
f5bd6bc
pdi reads successfull
Dec 5, 2021
2666376
pdi reads successfull
Dec 5, 2021
675ae0c
Merge branch 'xmega-avrisp2' of github.com:masterxq/lufa into xmega-a…
masterxq Dec 5, 2021
dc98859
Formular for baud rate for syncronous devices corrected, inverted clk…
masterxq Dec 5, 2021
4c4f1a0
Clearing correct irq flag. And waiting for the correct pin level
masterxq Dec 5, 2021
43835b2
void casting unused paramter, but should be checked!
masterxq Dec 5, 2021
8c706e2
Timeouts
masterxq Dec 5, 2021
d470313
Timeouts
masterxq Dec 5, 2021
2f71ea5
And more fixes for pin level
masterxq Dec 5, 2021
0fb1677
No more timeout
masterxq Dec 5, 2021
0a8c79f
Latest changes for testing
masterxq Dec 5, 2021
3fdc821
Smoother change of PDI TX/RX mode
masterxq Dec 20, 2021
ef1bebd
Fixed crital bug in length checks!
masterxq Dec 20, 2021
93e29ee
Removed IDE file
masterxq Dec 20, 2021
a6c90b8
Added importand define
masterxq Dec 20, 2021
d2f21b1
Removed another ide file
masterxq Dec 20, 2021
394c31f
Fixed SPI for XMega
masterxq Dec 20, 2021
4781eb7
Removed reset pin as it is the same as aux
masterxq Dec 22, 2021
e0eed18
Changed reset pin ctrl to aux pin ctrl
masterxq Dec 22, 2021
68d95e8
Avoid overflow
masterxq Dec 23, 2021
c75241c
Added untested Calibration function for XMega
masterxq Dec 23, 2021
18cec3c
Added hacked, but tested and working ADC init
masterxq Dec 23, 2021
18f9e67
Added hacked, but tested and working ADC init and measure
masterxq Dec 23, 2021
dc7bbcf
Added usart spi master and calculations
masterxq Dec 23, 2021
3078450
Added new univeral precompile defined lookup table for SPI settings f…
masterxq Dec 23, 2021
ed5ba7d
Updated configuration for to newly implented functionality and update…
masterxq Dec 23, 2021
0282577
Repaired Vtarget ADC
masterxq Jan 2, 2022
7b29be6
Merge branch 'abcminiuser:master' into xmega-avrisp2
masterxq Jan 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LUFA/Drivers/Peripheral/XMEGA/SerialSPI_XMEGA.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@

/* Private Interface - For use in library only: */
#if !defined(__DOXYGEN__)
#define SERIAL_SPI_UBBRVAL(Baud) ((Baud < (F_CPU / 2)) ? ((F_CPU / (2 * Baud)) - 1) : 0)
#define SERIAL_SPI_UBBRVAL(Baud) ((Baud < (F_CPU / 2)) ? ((F_CPU / (2ULL * Baud)) - 1) : 0)
#endif

/* Public Interface - May be used in end-application: */
Expand Down
13 changes: 13 additions & 0 deletions LUFA/Drivers/USB/Core/XMEGA/Endpoint_XMEGA.h
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,19 @@
return (USB_Endpoint_SelectedEndpoint & ENDPOINT_DIR_IN);
}

/** Sets the direction of the currently selected endpoint.
*
* \param[in] DirectionMask New endpoint direction, as a \c ENDPOINT_DIR_* mask.
*/
static inline void Endpoint_SetEndpointDirection(const uint8_t DirectionMask) ATTR_ALWAYS_INLINE;
static inline void Endpoint_SetEndpointDirection(const uint8_t DirectionMask)
{
//TODO: Needs to be completed!
(void) DirectionMask;
// UECFG0X = ((UECFG0X & ~(1 << EPDIR)) | (DirectionMask ? (1 << EPDIR) : 0));
//USB_Endpoint_SelectedEndpoint = (USB_Endpoint_SelectedEndpoint & ~ENDPOINT_DIR_IN) | DirectionMask;
}

/** Reads one byte from the currently selected endpoint's bank, for OUT direction endpoints.
*
* \ingroup Group_EndpointPrimitiveRW_XMEGA
Expand Down
13 changes: 12 additions & 1 deletion Projects/AVRISP-MKII/AVRISP-MKII.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,23 @@ void SetupHardware(void)

/* Disable clock division */
clock_prescale_set(clock_div_1);
#elif (ARCH == ARCH_XMEGA)
XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);

// Start the 32MHz internal RC oscillator and start the DFLL to increase it to F_USB using the USB SOF as a reference
XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
sei();
#endif

/* Hardware Initialization */
LEDs_Init();
#if defined(RESET_TOGGLES_LIBUSB_COMPAT)
UpdateCurrentCompatibilityMode();
#endif

/* USB Stack Initialization */
USB_Init();
}
Expand All @@ -113,7 +122,9 @@ void EVENT_USB_Device_ConfigurationChanged(void)
ConfigSuccess &= Endpoint_ConfigureEndpoint(AVRISP_DATA_OUT_EPADDR, EP_TYPE_BULK, AVRISP_DATA_EPSIZE, 1);

/* Setup AVRISP Data IN endpoint if it is using a physically different endpoint */
#if (ARCH != ARCH_XMEGA)
if ((AVRISP_DATA_IN_EPADDR & ENDPOINT_EPNUM_MASK) != (AVRISP_DATA_OUT_EPADDR & ENDPOINT_EPNUM_MASK))
#endif
ConfigSuccess &= Endpoint_ConfigureEndpoint(AVRISP_DATA_IN_EPADDR, EP_TYPE_BULK, AVRISP_DATA_EPSIZE, 1);

/* Indicate endpoint configuration success or failure */
Expand Down
125 changes: 113 additions & 12 deletions Projects/AVRISP-MKII/Config/AppConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,128 @@

#ifndef _APP_CONFIG_H_
#define _APP_CONFIG_H_
#if (ARCH == ARCH_AVR8)
#define AUX_LINE_PORT PORTB
#define AUX_LINE_PIN PINB
#define AUX_LINE_DDR DDRB
#if (BOARD == BOARD_U2S)
#define AUX_LINE_MASK (1 << 0)
#else
#define AUX_LINE_MASK (1 << 4)
#endif
#define VTARGET_ADC_CHANNEL 2
#define VTARGET_REF_VOLTS 5
#define VTARGET_SCALE_FACTOR 1
#define VTARGET_USE_INTERNAL_REF
#elif (ARCH == ARCH_XMEGA)
/* Wiring in default config (REMAP SPI Enable or USART SPI Master)
* PORTC
* PIN
* 0 O---120Ohm---|
* O-- RESET (PDI/ISP PIN5)
* 1 O---120Ohm---|
*
* 2 O---180Ohm---O
* |
* 3 O---180Ohm---O
* |
* 4 x O-- MISO/DAT (PDI/ISP PIN1)
* |
* 5 O---120Ohm---|-- SCK (PDI/ISP PIN5)
* |
* 6 O---120Ohm---O
*
* 7 O---120Ohm------ MOSI (PDI/ISP PIN4)
*
*
* GND O---------------O--------- GND (PDI/ISP PIN6)
* |
* PA0 O----O-1.5KOhm--O
* | Scale theoritical 7.66667 with this Resistors, but we needed to adjust to 8.43333.
* O--10KOhm--O You can read voltage from target
* ? Or you can supply the target. We prefer to have addional Power OUT Pins for supply the target.
* VCC O---------???---O---------- VCC (PDI/ISP PIN2) (Dangerous! But can be helpfull, you can also leave it open or use other options)
*
* This allows to program PDI/ISP but only 3.3V!!!!!!
*/
//Remaping spi will swap SCK and MOSI (Same pinout as USART in SPI master mode)
#define REMAP_SPI

//Use USART interface in SPI master mode instead of the SPI module.
//Recommended it allows higher precision of the SPI CLK.
#define USART_SPI_MASTER

#define AUX_LINE_PORT PORTB
#define AUX_LINE_PIN PINB
#define AUX_LINE_DDR DDRB
#if (BOARD == BOARD_U2S)
#ifdef USART_SPI_MASTER
#define USART_SPI_REG USARTC1
#else
#define SPI_REG SPIC
#endif

#define SPI_PORT PORTC

#if defined(REMAP_SPI) || defined(USART_SPI_MASTER)
#define SPI_SCK_MASK (1 << 5)
#define SPI_SCK_CTRL PIN5CTRL

#define SPI_MOSI_MASK (1 << 7)
#define SPI_MOSI_CTRL PIN7CTRL
#else
#define SPI_SCK_MASK (1 << 7)
#define SPI_SCK_CTRL PIN7CTRL

#define SPI_MOSI_MASK (1 << 5)
#define SPI_MOSI_CTRL PIN5CTRL
#endif

#define SPI_MISO_MASK (1 << 6)
#define SPI_MISO_CTRL PIN6CTRL



#define RESCUE_PORT PORTD
#define RESCUE_TIMER TCD0
#define RESCUE_PIN_MASK (1 << 0)
#define RESCUE_TIMER_CMP_EN TC0_CCAEN_bm //A if pin 0, B if pin 1...
#define RESCUE_TIMER_CMP_REG CCA

#define PDI_USART USARTC0
#define PDI_PORT PORTC
#define PDI_RX_MASK (1 << 2)
#define PDI_RX_CTRL PIN2CTRL

#define PDI_TX_MASK (1 << 3)
#define PDI_TX_CTRL PIN3CTRL

#define PDI_XCK_MASK (1 << 1)
#define PDI_XCK_CTRL PIN1CTRL

#define AUX_LINE_PORT PORTC
#define AUX_LINE_MASK (1 << 0)
#else
#define AUX_LINE_MASK (1 << 4)
#endif
#define AUX_LINE_CTRL PIN0CTRL


#define DELAY_TIMER TCC0
#define DELAY_TIMER_OVF_vect TCC0_OVF_vect
#define SW_SPI_TIMER TCC1
#define SW_SPI_TIMER_CCA_vect TCC1_CCA_vect
#define SW_SPI_TIMER_CCB_vect TCC1_CCB_vect
#define SW_SPI_TIMER_OVF_vect TCC1_OVF_vect
#define SW_SPI_PIN_IRQ_vect PORTC_INT0_vect
#define VTARGET_ADC_CHANNEL 1 //Hardcoded allways 1 Connected to PA1
#define VTARGET_REF_VOLTS 1.00 //Hardcoded allways 1.00
#define VTARGET_SCALE_FACTOR 6.8 //GND<->R1<-ADC->R2<->SIGNAL | SCALE = (R1+R2)/R1
#endif
#define ENABLE_ISP_PROTOCOL
#define ENABLE_XPROG_PROTOCOL

#define VTARGET_ADC_CHANNEL 2
#define VTARGET_REF_VOLTS 5
#define VTARGET_SCALE_FACTOR 1
// #define VTARGET_USE_INTERNAL_REF
#define NO_VTARGET_DETECT


// #define NO_VTARGET_DETECT
// #define XCK_RESCUE_CLOCK_ENABLE
// #define INVERTED_ISP_MISO

// #define LIBUSB_DRIVER_COMPAT
// #define RESET_TOGGLES_LIBUSB_COMPAT
// #define FIRMWARE_VERSION_MINOR 0x11

#endif
33 changes: 33 additions & 0 deletions Projects/AVRISP-MKII/Config/LUFAConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,39 @@
// #define NO_AUTO_VBUS_MANAGEMENT
// #define INVERTED_VBUS_ENABLE_LINE

#elif (ARCH == ARCH_XMEGA)

/* Non-USB Related Configuration Tokens: */
// #define DISABLE_TERMINAL_CODES
#define ORDERED_EP_CONFIG
/* USB Class Driver Related Tokens: */
// #define HID_HOST_BOOT_PROTOCOL_ONLY
// #define HID_STATETABLE_STACK_DEPTH {Insert Value Here}
// #define HID_USAGE_STACK_DEPTH {Insert Value Here}
// #define HID_MAX_COLLECTIONS {Insert Value Here}
// #define HID_MAX_REPORTITEMS {Insert Value Here}
// #define HID_MAX_REPORT_IDS {Insert Value Here}
// #define NO_CLASS_DRIVER_AUTOFLUSH

/* General USB Driver Related Tokens: */
#define USE_STATIC_OPTIONS (USB_DEVICE_OPT_FULLSPEED | USB_OPT_RC32MCLKSRC | USB_OPT_BUSEVENT_PRIHIGH)
// #define USB_STREAM_TIMEOUT_MS {Insert Value Here}
// #define NO_LIMITED_CONTROLLER_CONNECT
#define NO_SOF_EVENTS

/* USB Device Mode Driver Related Tokens: */
// #define USE_RAM_DESCRIPTORS
#define USE_FLASH_DESCRIPTORS
// #define USE_EEPROM_DESCRIPTORS
#define NO_INTERNAL_SERIAL
#define FIXED_CONTROL_ENDPOINT_SIZE 16
#define DEVICE_STATE_AS_GPIOR 0
#define FIXED_NUM_CONFIGURATIONS 1
// #define CONTROL_ONLY_DEVICE
#define MAX_ENDPOINT_INDEX 4
#define NO_DEVICE_REMOTE_WAKEUP
#define NO_DEVICE_SELF_POWER

#else

#error Unsupported architecture for this LUFA configuration file.
Expand Down
53 changes: 53 additions & 0 deletions Projects/AVRISP-MKII/Lib/ISP/ISPProtocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,26 @@ static volatile uint8_t ISPProtocol_ResponseTogglesRemaining;


/** ISR to toggle MOSI pin when TIMER1 overflows */
#if ARCH == ARCH_AVR8
ISR(TIMER1_OVF_vect, ISR_BLOCK)
{
PINB |= (1 << PB2); // toggle PB2 (MOSI) by writing 1 to its bit in PINB
ISPProtocol_HalfCyclesRemaining--;
}
#elif (ARCH == ARCH_XMEGA)
ISR(SW_SPI_TIMER_CCA_vect, ISR_BLOCK)
{
SPI_PORT.OUTTGL = SPI_MOSI_MASK;
ISPProtocol_HalfCyclesRemaining--;
}
#endif

/** ISR to listen for toggles on MISO pin */
#if ARCH == ARCH_AVR8
ISR(PCINT0_vect, ISR_BLOCK)
#elif (ARCH == ARCH_XMEGA)
ISR(SW_SPI_PIN_IRQ_vect, ISR_BLOCK)
#endif
{
ISPProtocol_ResponseTogglesRemaining--;
}
Expand Down Expand Up @@ -420,6 +432,8 @@ void ISPProtocol_Calibrate(void)
Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);

//TODO: Learn it and implement it...
#if (ARCH == ARCH_AVR8)
/* Enable pull-up on MISO and release ~RESET */
DDRB = ~(1 << PB3);
PORTB |= ( (1 << PB4) | (1 << PB3) );
Expand Down Expand Up @@ -455,7 +469,46 @@ void ISPProtocol_Calibrate(void)
/* Check if device responded with a success message or if we timed out */
if (ISPProtocol_ResponseTogglesRemaining)
ResponseStatus = STATUS_CMD_TOUT;
#elif (ARCH == ARCH_XMEGA)
//TODO: This needs to be tested
/* Enable pull-up on MISO and release ~RESET */
SPI_PORT.SPI_MISO_CTRL = PORT_OPC_PULLUP_gc;
SPI_PORT.DIRCLR = SPI_MISO_MASK;
AUX_LINE_PORT.DIRCLR = AUX_LINE_MASK;

/* Set up MISO pin (PCINT3) to listen for toggles */
SPI_PORT.INT0MASK = SPI_MISO_MASK;

/* Set up timer that fires at a rate of 65536 Hz - this will drive the MOSI toggle */
SW_SPI_TIMER.CCA = ISPPROTOCOL_CALIB_TICKS - 1;
SW_SPI_TIMER.PER = ISPPROTOCOL_CALIB_TICKS - 1;
SW_SPI_TIMER.CTRLB = TC_WGMODE_FRQ_gc; // set for fast PWM, TOP = OCR1A
SW_SPI_TIMER.CTRLA = TC_CLKSEL_DIV1_gc; // ... and no clock prescaling
SW_SPI_TIMER.CNT = 0;

/* Initialize counter variables */
ISPProtocol_HalfCyclesRemaining = ISPPROTOCOL_CALIB_HALF_CYCLE_LIMIT;
ISPProtocol_ResponseTogglesRemaining = ISPPROTOCOL_CALIB_SUCCESS_TOGGLE_NUM;

/* Turn on interrupts */
SW_SPI_TIMER.INTCTRLB = TC_CCAINTLVL_MED_gc; // enable interrupts for PCINT7:0 (don't touch setting for PCINT12:8)
SPI_PORT.INT0MASK |= PORT_INT0LVL_MED_gc; // enable T1 OVF interrupt (and no other T1 interrupts)

/* Turn on global interrupts for the following block, restoring current state at end */
NONATOMIC_BLOCK(NONATOMIC_RESTORESTATE)
{
/* Let device do its calibration, wait for response on MISO */
while (ISPProtocol_HalfCyclesRemaining && ISPProtocol_ResponseTogglesRemaining);

/* Disable timer and pin change interrupts */
SW_SPI_TIMER.INTCTRLB &= ~TC_CCAINTLVL_MED_gc;
SPI_PORT.INT0MASK &= ~PORT_INT0LVL_gm;
}

/* Check if device responded with a success message or if we timed out */
if (ISPProtocol_ResponseTogglesRemaining)
ResponseStatus = STATUS_CMD_TOUT;
#endif
/* Report back to PC via USB */
Endpoint_Write_8(CMD_OSCCAL);
Endpoint_Write_8(ResponseStatus);
Expand Down
Loading