Go Down

Topic: [ ATMega328p ] Implementing ISR breaks / freezes program (Read 521 times) previous topic - next topic

Smokehead

Hi,

i'm trying to program an ATMega328P using avrdude on a breadboard.

When i implement an ISR for timer-0-overflow the program crashes and won't start anymore. It seems like just implementing an ISR breaks the whole program.

I'm trying the following:

Code: [Select]

#include <avr/io.h>
#include <avr/interrupt.h>

// T = 0.5s with 16 MHz Frequency
constexpr uint16_t ovf_max_count = 31250;

volatile uint16_t ovf_count = 0;

int main()
{
    // Configure Pin 5 of Port-D to Output
    // Set Pin 5 of Port-D to high
    DDRD |= (1 << PD5);
    PORTD |= (1 << PD5);

    // Enabling Timer 0 without prescaler
    // Enable Timer 0 OVF-Interrupt
    // Setting Timer 0 Count to 0

    TIMSK0 |= (1 << TOIE0);
    TCCR0B |= (1 << CS00);

    TCNT0 = 0;

    // Enable interrupts
    sei();

    for(;;)
    {
    }
}

// Implementing this breaks the program and the LED on pin 5 doesn't light up.
// When not implemented the LED is on and it seems to work. When testing
// with delay in the main loop it works. When trying to use it this way with
// interrupts it breaks the whole program.

ISR(TIMER0_OVF_vect)
{
    if(++ovf_count >= ovf_max_count)
    {
         PORTD ^= (1 << PD5);
         ovf_count = 0;
    }
}


It doesn't depend on this interrupt in special. Every ISR i'm implementing breaks the program. The TIMER0_OVF_vect as well as any other like PCINT0 etc. I don't have any idea left which could cause this problem.

I'm using an ATMega328P-PU with an 16 MHz Crystal oscillator on a breadboard. Capacitor-Values are 22 pF.

Thanks for your answers or advices!

Cheers,

Smokehead

ron_sutherland

#1
Dec 14, 2018, 07:30 pm Last Edit: Dec 14, 2018, 07:31 pm by ron_sutherland
Wiring uses that ISR

https://github.com/arduino/ArduinoCore-avr/blob/b7c607663fecc232e598f2c0acf419ceb0b7078c/cores/arduino/wiring.c#L45

so that we can have a millis function.

https://www.arduino.cc/reference/en/language/functions/time/millis/

I bet the compiler is not very pleased with having two references to the interrupt.
I use an R-Pi Zero on an RPUpi shield to have a tool-chain at the network edge.

DrAzzy

Multiple ISRs for the same vector should prevent compile from succeeding, so it's weird that it even builds....
ATtiny core for 841+1634+828 and x313/x4/x5/x61/x7/x8 series Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts (some assembled), mosfets and awesome prototyping board in my store http://tindie.com/stores/DrAzzy

Smokehead

I'm not using anything from the Arduino-API. I'm not even using the arduino boards anymore. So i don't think it does have to do anything with any Arduino APIs anymore. I switched to programming via ISP on a breadboard with the testing setup. Programs without ISRs work as intended but as soon as i add an ISR it doesn't do anything. Not even the LED is on.

I'm using avr-gcc 8.2 on ArchLinux 4.19.8 with avrdude using an pavolu avr programmer v2.1.

Cheers,

Smokehead

ron_sutherland

That is some bleeding edge stuff, not even Microchip is using it that fresh. You are going to have to figure out how to fix your own problems if you want to play the game at that level. The 8.x updates do look interesting, but I think I will wait for Debian and/or Microchip before I start working with it.

https://packages.debian.org/sid/gcc-avr
I use an R-Pi Zero on an RPUpi shield to have a tool-chain at the network edge.

Smokehead

I'm playing around with Templatemetaprogramming using Templatestructures for Hardwareabstruction without producing the same overhead like classic C++ classes. There i need a lot of constexpr and stuff like this. Older compilers aren't capable of these things. I need to have a new one for using the C++ 17 features.

For sinple I/O and Pin toggling or even configuring some Regusters it works fine. But when i start using simple things like this it breaks everything. Then i've tried a simple.program.like this one above and couldn't get it running.

But thanks for the hint! I'll try using an older compiler for testing the ISR code to see if this coukd be the problem.

If i'd post the disassembler binary, could any one help me find an possible error?

Cheers,

Smokehead

Smokehead

Update:

It seems like the microcontroller is running into the following ISR. After implementing this it seems to do exactly this. It puts on the LED connected to pin PC0. I'm slightly confused. I don't have any other interrupt enabled nor do i have called `sei()` (just for testing purposes).

Code: (Bad Interrupt) [Select]

ISR(BADISR_vect)
{
    PORTC |= (1 << PC0);
}


I don't know whats happening there. Do Fuses maybe have any influence to interrupt handling? The external crystal oscillator at 16MHz seems to run without any issues. When using the _delay_ms(500) with 500 ms as parameter it toggles the LED 2 times a second which would be correct. I do not have any idea what could cause this behaviour.

Cheers,

Smokehead

Smajdalf

Are the interrupt vectors properly defined in your tools? AFAIK ISR with misspelled name still compiles and you get only warning, the ISR is not attached to any interrupt and probably is not included to the binary at all. If you don't have the interrupt names defined (or the names are slightly different) it would explain what you see.
Banned forever by Coding Badly for "trolling". Good luck with such attitude.

Smokehead

I now have a completely different behaviour.

I've looked at the disassembled code for some time and wrote another simple program. It seems like the first one (created with CMake) did something different than the one with a simple Makefile.

The one i've build using a Makefile i've wrote included all interrupt vectors pointing to __bad_interrupt except the one i've defined. The one built with cmake didn't include this one but had some other sections in it. It seems like i'm going to use Makefiles instead of CMake for future MCU-projects.. CMake does some strange things.. I think it did something to the compiler options.

Now i've got the ISRs running but it seems that i've broken something on my programmer or board itself.

When i'm flashing the chip it's much slower than before and i get the following output:

Code: (avrdude output) [Select]

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.07s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "app.elf"
avrdude: writing flash (244 bytes):

Writing |                                                    | 0% 0.00savrdude: stk500v2_ReceiveMessage(): timeout
Writing | #########################                          | 50% 3.02savrdude: stk500v2_ReceiveMessage(): timeout
Writing | ################################################## | 100% 6.04s

avrdude: 244 bytes of flash written
avrdude: verifying flash memory against app.elf:
avrdude: load data flash data from input file app.elf:
avrdude: input file app.elf contains 244 bytes
avrdude: reading on-chip flash data:

Reading |                                                    | 0% 0.00savrdude: stk500v2_ReceiveMessage(): timeout
Reading | ################################################## | 100% 5.95s

avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x0000
         0x80 != 0x0c
avrdude: verification error; content mismatch

avrdude: safemode: Fuses OK (E:FF, H:D9, L:D6)

avrdude done.  Thank you.



I don't know what happened there. I did not change anything on the board itself nor on the programmer nor on the wiring.

Just reading from the flash using avrdude doesn't work either. I've tried that on three ATMega328P chips. The programs run as intended but it needs longer to upload and throws errors. Does anyone have an idea? Should i open another thread for this one?

For the basic-questions:

Yes, i'm using a pullup-resistor (10 k) on the reset pin as well as a 100nF capacitor between VCC and GND.

Like i said, the programs are working but i get these verification errors.

Thanks!

Cheers,

Smokehead

ron_sutherland

#9
Dec 15, 2018, 08:00 pm Last Edit: Dec 15, 2018, 08:00 pm by ron_sutherland
I have seen a verification error because I was doing something wrong but I don't recall what it was.

For reference, I have some Makefiles that load with an Arduino Uno using the example ISP sketch (avrdude -c stk500v1).

https://github.com/epccs/RPUadpt/blob/master/Remote/Makefile

with the optiboot loader (avrdude -c arduino)

https://github.com/epccs/RPUno/blob/master/Adc/Makefile

and again with xboot loader (avrdude -c avr109)

https://github.com/epccs/Irrigate7/blob/master/Adc/Makefile
I use an R-Pi Zero on an RPUpi shield to have a tool-chain at the network edge.

Smokehead

As far as i know i do not need a bootloader because im flashing the program with an ISP. I'm using a Polulu AVR Programmer v2.1 which as far as i know emulates the stk500v2.

That's how i start flashing:

Code: [Select]

avrdude -v -c stk500 -p atmega328p -P /dev/ttyACM0 -U flash:w:app.hex:i


I'm curious what caused the problem because i'm working with this command and it worked. Something happened today which broke my setup. Should i upload my schematic? Maybe there are some capacities or mistakes i'm not aware of.

Cheers,

Smokehead

Smokehead

Here it is.. Maybe it will show you my setup and leads to a solution.

Cheers,
Smokehead

ron_sutherland

#12
Dec 15, 2018, 09:33 pm Last Edit: Dec 15, 2018, 09:57 pm by ron_sutherland
The AVCC needs power, and I use a bypass for AVCC, VCC, and AREF (one on VCC works, but ... well it is probably my OCD).



with avrdude version 6.3 on Ubuntu 18.04, the programmer options I see are

Code: [Select]
  stk500           = Atmel STK500
  stk500hvsp       = Atmel STK500 V2 in high-voltage serial programming mode
  stk500pp         = Atmel STK500 V2 in parallel programming mode
  stk500v1         = Atmel STK500 Version 1.x firmware
  stk500v2         = Atmel STK500 Version 2.x firmware


I have no clue if the Polulu programmer would be using the original stk500 or one of the newer v1 and v2.
I use an R-Pi Zero on an RPUpi shield to have a tool-chain at the network edge.

Smokehead

Actually i've connected them to the supply voltage but i didn't put them in the schematic. Well, i've messed up the in/output labels as well. Had to be quick, you know...

I'll try different settings. Thanks fir the hint!

Cheers,
Smokehead

westfw

It sounds like you could be getting the wrong startup code, with vectors in the wrong place.
What EXACTLY does your build command end up looking like (the gcc command that make or cmake produces)?  And what version of avr-libc do you have?  (and/or post the disassembly of your test program...)


Go Up