ATMega328p flashing with ISP

I flashed some code onto an Arduino Uno, using Atmel Studio with an stk500 programmer.

This code is toggling the SPI lines every 50ms.

I believe this is preventing the programmer from communicating with the ATMega328p.

I cannot read the device ID and I cannot program the chip.

Any advice?

I accidentally did this with two of them.

To be able to serial program the chip you need an enabled reset pin, enabled serial programming, and a working system clock. If you got some of the fuse settings wrong you could have disabled one or more of those.

The only way to recover is with Hight Voltage Parallel Programming. You can do that with an Arduino UNO and a HVRecoveryShield: Arduino-based AVR High Voltage Programmer | MightyOhm

All fuse settings are correct and I can confirm that the code is executing (using a scope on the lines).

Reset pin also confirmed working.

Any way other than parallel?
One of these chips is on a custom board, not an Arduino, that doesn't have all the pins broken out.

Scradam:
All fuse settings are correct and I can confirm that the code is executing (using a scope on the lines).

If your fuse settings are correct and your sketch is running, what's the problem?

I can no longer flash it with updated code.

I believe the stk500 programmer cannot communicate due to spi lines being in use. I assumed that the programmer would use the reset line to avoid this issue.

Scradam:
This code is toggling the SPI lines every 50ms.

I believe this is preventing the programmer from communicating with the ATMega328p.

Nope.

The first thing the programmer will do when it wants to upload code is pull the RESET line low and hold it there. Your program won't be running.

That's what I would have thought!

So.. why would it be that as soon as I flash the code onto another arduino, it becomes bricked also?

OK... Perhaps you should spell out exactly what steps you take. include source code of the sketch you upload and what hardware you use when.

My code simply toggles the spi lines repeatedly.

// Set MOSI, SCK, SS as Output
DDRB |= (1<<PIN_MOSI)|(1<<PIN_SCK)|(1<<PIN_SS);

for (;;) {
	PORTB &= ~(1<<PIN_MOSI);  // drive PB3 low
	PORTB &= ~(1<<PIN_SCK);  // drive PB5 low
	delay_ms(50);
	PORTB |= (1<<PIN_MOSI);   // drive PB3 high
	PORTB |= (1<<PIN_SCK);   // drive PB5 high
	delay_ms(50);
}

I’m using Atmel Studio with a Pololu USB AVR programmer (stk500) to program the ATMega328p.

I’ve also tried burning the Arduino bootloader with the Arduino IDE and I get a ‘not in sync’ error.

The LED on the Arduino is flashing and a scope shows that the code is executing, but I can no longer program the devices.

What "Programmer" do you select when using your Pololu USB AVR programmer to burn the bootloader?

The Pololu USB AVR programmer says that it does not provide 5V to the target device. How are you applying power to the Arduino UNO while you try to burn the bootloader?

When your scope is showing the program running does it stop running when you hold down the Reset button?

If you connect the scope to the Reset line does the line go LOW when you try to burn the bootloader?

FWIW, I burnt your program into a m168 using Arduino (after a few corrections), and ArduinoISP as the programmer, and I did not have any problems uploading subsequent programs the same way.

Do you have fuses defined in the Atmel Studio project? I'm not clear on exactly how studio handles fuse programming, but it's possible that it sets them (or the lock bits) with each upload, perhaps to a value that is not fully compatible with the next upload (ie internal 1MHz clock, which might not run SPI fast enough for subsequent uploads.)