Before logging an issue, please update to the latest release of Visual Micro from the Downloads Page.

When Logging a Support Issue in the Forum, please ensure you have also:-

  • Enabled vMicro > Compiler > Show Build Properties
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
  • Click the Reply button and attach as .txt file OR
  • Click here to Email us with the file attached, and a link to your post
Support requests without the output above may be impossible to answer, so please help us to help you
 
Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 8 Replies) Debugging Arduino UNO + Ethernet Shield. When Using Timer1 program hangs (Read 13819 times)
Peter Pen
Newbies
*
Offline


Posts: 7
Joined: Nov 20th, 2014
Debugging Arduino UNO + Ethernet Shield. When Using Timer1 program hangs
Nov 20th, 2014 at 11:57am
Print Post  
Hello, dear community, 

I write a program for Arduino UNO + Ethernet Shield. In my setup I run a TCP Client  which connects to a TCP Server. Server responds with one byte of information after connection establishment.  I use the Timer1 to measure how long i took for the server to respond after connection establishement. I need to use timer for maximal precision (counting ticks) 

I use Atmel Studio 6.2 + Visual Micro (Trial version) to debug my application. In my config I have chosen: 
Project Type: Arduino Sketch with C++ Class Example
Processor: ATmega328P
Board: Arduino Ethernet (there is either Arduino Uno or Arduino Ethernet possible to choose in the IDE, tried both, when switching Reload Tool Chain was done)
Project Properties->(Micro Debug): Full
Tools->Visual Micro checked: Automatic Debugging
Breakpoint Manager: Jump to Breakpoints
  
I have tried 2 types of debugging: 

1) Doing a live debug, i.e. I build project and start debugging right away. In this case if I don't use timer, then Client-Server communication works the way it should. However, my program hangs when I am attempting to set the TIMSK Register to enable the counter.
  
2) Debugging with a Simulator by opening an .elf file in IDE, switching from "No Tool" to "Simulator".  I am able to set the TIMSK and jump to my Interrupt Service Routine, when the timer overflows. However networking doesn't work. I track that with help of Wireshark.

I would very appreciate any help!

Here are some questions that I could not answer:
1) Which exactly board do I need to choose: Arduino UNO or Arduino Ethernet. In my case they are both combined.
2) Is the Simulator for those boards are designed to perform networking

But the most important for me is to answer why debugging with timer doesn't work. Here is a code snippet of my program:
ISR(TIMER1_OVF_vect){      sreg = SREG;

     digitalWrite(ledPin, digitalRead(ledPin)^ 1);      // invert the bit value on the ledPin      overflowCounter++;
     TIMSK1 &= ~(1 << TOIE1);      //disable interrupt on timer1
SREG = sreg; }

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
 
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);    
  
           //+Serial.println("\r\nSetup...");
           //+Serial.print("\r\nInitializing the w5100 chip and open a TCP socket...");
           W5100.init(); //
           W5100.writeSnMR(s, SnMR::TCP); // set type of socket in Mode Register

           W5100.setMACAddress(smac); // set own mac address
           W5100.setIPAddress(src_ip_addr); // set own ip address
           W5100.writeSnPORT(s, src_port); // set own port (for each socket different)
           W5100.setSubnetMask(subnet_mask);
           W5100.setGatewayIp(gateway_ip);

           W5100.writeSnDHAR(s, dst_mac); // set destination mac address
           W5100.writeSnDIPR(s, dst_ip_addr); // set destination ip
           W5100.writeSnDPORT(s, dst_port); // set destination port

           W5100.writeRMSR(0x03); // RX Memory Size Register. First two bits set to 1, this corresponds to assigning all 8 kByte of RX memory for the socket 0
           W5100.writeTMSR(0x03); // TX Memory Size Register. First two bits set to 1, this corresponds to assigning all 8 kByte of TX memory for the socket 0

           W5100.writeIMR(0x01); // Interrupt Mask Register is set to enable interrupts on socket 0.

           W5100.execCmdSn(s, Sock_OPEN);
                 Serial.print("\r\nDone opening socket");
                 Serial.print("\r\n");

           memset(data, 0, PAYLOAD_SIZE);
           strcpy(data, "A");
           len = strlen(data);
           
           cli();
           TCCR1B = 0;
           TCCR1A = 0;
           TCNT1 = 65300;
           // prescaler
           TCCR1B |= (1 << CS10);      // Time/Counter Control Register B no prescaler needed, since we count amount of ticks
           sei();
}

// the loop routine runs over and over again forever:
void loop() {
            // wait for a second
     
           if(measurementsCounter == 1000)
           while(true){}                  // endless loop after desired amount of measurements achieved

           W5100.execCmdSn(s, Sock_CONNECT);

           while ((W5100.readSnSR(s) & SnSR::ESTABLISHED) != SnSR::ESTABLISHED) { // check Status Register to find out if the connection was established

           }
           TCNT1 = 0; // reset counter. Note that the timer counts always, whether interrupt occurs decides bit in TIMSK
           TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt. Note we OR the current state of the TIMSK, so once set it is always set, if manually not changed

           while ((W5100.readSnIR(s) & SnIR::RECV) != SnIR::RECV) {}
           restTicks = TCNT1; // save rest ticks
           TIMSK1 &= ~(1 << TOIE1); // disable timer1.
           W5100.writeSnIR(s,SnIR::RECV);      // clear the interrupt bit

           time = (overflowCounter * 65535 + restTicks) * 62.5;
           measurementsCounter++;

           overflowCounter = 0;      // clear overflowCounter

           Serial.print("\r\nTerminate Connection");
           W5100.execCmdSn(s, Sock_DISCON);

           while ((W5100.readSnIR(s) & SnIR:CheesyISCON) != SnIR:CheesyISCON) {
           }
           W5100.writeSnIR(s,SnIR:CheesyISCON);      // clear the interrupt bit
                 Serial.print("\r\nDisconnected.");

}
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12076
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Debugging Arduino UNO + Ethernet Shield. When Using Timer1 program hangs
Reply #1 - Nov 20th, 2014 at 12:10pm
Print Post  
Hi,

Thanks for the information. Maybe some information here will help.

1.
Reload Tool Chain is only required if you add new hardware definitions to the arduino ide sources.

2.
You should select whichever board the manufacturer of your board recommends. Maybe you can provide a link so we can see which board you have.

3.
You have not said where your break points are located in your code. This might be important because you are overriding the timer

4.
Visual Micro uses the millis() function to determine when a timed breakpoint should happen.

This is an example of a 250ms timed breakpoint ...

Code
Select All
long last_millis;
if ( (millis()-last_millis)>250 )
 



5.
Visual Micro uses Serial to display breakpoint messages. Do you own serial print statements work okay in your project?

Thanks
  
Back to top
WWW  
IP Logged
 
Peter Pen
Newbies
*
Offline


Posts: 7
Joined: Nov 20th, 2014
Re: Debugging Arduino UNO + Ethernet Shield. When Using Timer1 program hangs
Reply #2 - Nov 20th, 2014 at 12:28pm
Print Post  
Hello, @Tim Leek,

thanks for such a quick reply. Here is a link to my UNO board :
http://www.pollin.de/shop/dt/NzE4OTgxOTk-/Bausaetze_Module/Entwicklerboards/ARDU...

and to ethernet shield:

http://www.pollin.de/shop/dt/MTA4OTgxOTk-/Bausaetze_Module/Entwicklerboards/ARDU...



I have tried to use breakpoints at different lines. In particular, i placed one against 

---> TIMSK1 |= (1 << TOIE1);

when breakpoint is hit, and I resume, then program hangs forever. As far as I know debugging uses Timer0, and I use Timer1, so debugging should not collide with my timing? Isn't that right?

> Visual Micro uses the millis() function to determine when a timed breakpoint should happen.
Are all breakpoints are timerd breakpoints?

In live debug modus (when flashing the board) Serial.print() works just fine. 

Thanks for help!

Best regards



ISR(TIMER1_OVF_vect){      sreg = SREG;

     digitalWrite(ledPin, digitalRead(ledPin)^ 1);      // invert the bit value on the ledPin      overflowCounter++;
     TIMSK1 &= ~(1 << TOIE1);      //disable interrupt on timer1
SREG = sreg; }

and
  
Back to top
 
IP Logged
 
Peter Pen
Newbies
*
Offline


Posts: 7
Joined: Nov 20th, 2014
Re: Debugging Arduino UNO + Ethernet Shield. When Using Timer1 program hangs
Reply #3 - Nov 20th, 2014 at 12:30pm
Print Post  
sorry for pasting the ISR at the end, didn't want it.
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12076
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Debugging Arduino UNO + Ethernet Shield. When Using Timer1 program hangs
Reply #4 - Nov 20th, 2014 at 12:48pm
Print Post  
Sorry ignore my comment about millis and timer. I thought were using using timed breakpoints but I now see you were simply saying you had a breakpoint in your own timer function.

Question. does this work?

Code (C++)
Select All
 TIMSK1 |= (1 << TOIE1);Serial.println("Timer function here"); 




I also suggest setting project property ThrottleEnabled = False

Switching off the throttle will prevent Visual Micro from altering timings with pauses. The throttle is for new users who put unconditional breakpoints in the loop() causing thousands of debug messages to be sent to the pc

Thanks
« Last Edit: Nov 20th, 2014 at 12:50pm by Tim@Visual Micro »  
Back to top
WWW  
IP Logged
 
Peter Pen
Newbies
*
Offline


Posts: 7
Joined: Nov 20th, 2014
Re: Debugging Arduino UNO + Ethernet Shield. When Using Timer1 program hangs
Reply #5 - Nov 20th, 2014 at 1:20pm
Print Post  
@Tim Leek, thanks for investing your time helping me!

Throttling is now disabled.
It doesn't work with suggested line:

--breakpoint-->            TIMSK1 |= (1 << TOIE1);Serial.println("Timer function here"); 

also doesn't go over setting TIMSK1 when I split the instructions:
                                TIMSK1 |= (1 << TOIE1);
--breakpoint-->            Serial.println("Timer function here"); 

if I comment out the first line, the breakpoint is hit and message is printed out in Serial window:
                                //TIMSK1 |= (1 << TOIE1);
--breakpoint-->            Serial.println("Timer function here"); 

What can be the reason?

I also have tried to swith from ATmega328P Arduino Ethernet to  ATmega328P Arduino UNO. The effect is the same.


  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12076
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Debugging Arduino UNO + Ethernet Shield. When Using Timer1 program hangs
Reply #6 - Nov 20th, 2014 at 1:28pm
Print Post  
Hi,

Thanks

If you upload without any breakpoints does Serial.println("Timer function here"); print a message?

Thanks
  
Back to top
WWW  
IP Logged
 
Peter Pen
Newbies
*
Offline


Posts: 7
Joined: Nov 20th, 2014
Re: Debugging Arduino UNO + Ethernet Shield. When Using Timer1 program hangs
Reply #7 - Nov 20th, 2014 at 4:03pm
Print Post  
Hi, @Tim Leek, 

thanks again for helping. It was a good Idea to check if things work in debug mode, but without breakpoints. And it in fact has worked. The problem has been found as I have tried to figure out which breakpoints have caused the problem. 

The evil breakpoint that I didn't mention to you was one single sitting inside my ISR. I didn't mention it to you, because it was not reached by the code at all, as enabling the overflow in TIMSK1 had to occur first and this was all the time not the case. Sorry for not mentioning I thought it is irrelevant one. Please see the code:

Code (C++)
Select All
ISR(TIMER1_OVF_vect){

-bpoint->	sreg = SREG;
		digitalWrite(ledPin, digitalRead(ledPin)^ 1);	// invert the bit value on the ledPin 	overflowCounter++;
		TIMSK1 &= ~(1 << TOIE1);	//disable interrupt on timer1
		SREG = sreg;
}

void setup() {

		Serial.begin(115200);
		pinMode(ledPin, OUTPUT);

		W5100.execCmdSn(s, Sock_OPEN);
		Serial.print("\r\nDone opening socket");
		Serial.print("\r\n");

		cli();
		TCCR1B = 0;
		TCCR1A = 0;
		TCNT1 = 65300;
		// prescaler
		TCCR1B |= (1 << CS10);
		sei();
void loop() {

		if(measurementsCounter == 1000)
		while(true){}			// endless loop after desired amount of measurements achieved

		W5100.execCmdSn(s, Sock_CONNECT);

		while ((W5100.readSnSR(s) & SnSR::ESTABLISHED) != SnSR::ESTABLISHED) { // check Status Register to find out if the connection was established

		}
		Serial.print("\r\nConnection established\r\n");
		TCNT1 = 0; // reset counter. Note that the timer counts always, whether interrupt occurs decides bit in TIMSK
		TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt. Note we OR the current state of the TIMSK, so once set it is always set, if manually not changed
		Serial.println("Timer function here");
		while ((W5100.readSnIR(s) & SnIR::RECV) != SnIR::RECV) {}
		restTicks = TCNT1; // save rest ticks
		TIMSK1 &= ~(1 << TOIE1); // disable timer1.
		W5100.writeSnIR(s,SnIR::RECV);	// clear the interrupt bit
-brpoint->	Serial.print("\r\nReceived packet\r\n");
 



But now I am confused, because when I remove the breakpoint from the ISR then the code runs just fine and prints me the following (please see the code that prints this at the end of the post):
Code
Select All
Port open

Done opening socket
ArduinoTCPClient Paused
Connection established
Timer function here
Received packet


 



However, when breakpoint in ISR is enabled, then the program hangs at the point when TIMSK1 is set. I get printed  only:
Code
Select All
Port open


Done opening socket

Co
 


or 
Code
Select All
Port open


Done opening so
 


or 
Code
Select All
Port open


Done opening socket
 



So the message is not getting printed completely. I thought that the TOV event on Timer1 takes place and breaks something. The initial state of my timer was 65300 so I thought that could be possible. But then I set it to zero (updated in the code), but nothing has changed, the printed messages were incomplete as previously.

What could be the reasons for that? Being able to set breakpoint in ISR is desired for me.

Thanks


  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12076
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Debugging Arduino UNO + Ethernet Shield. When Using Timer1 program hangs
Reply #8 - Nov 20th, 2014 at 4:11pm
Print Post  
Hi,

It is not possible do do much at all in an ISR. The best way is to save state as volatile variable then report on it back in normal code.

I suspect that a serial.print() in an ISR will also fail.

Thanks
  
Back to top
WWW  
IP Logged
 
Gilles Plante
Full Member
***
Offline


Posts: 156
Location: Québec, Canada
Joined: Oct 31st, 2013
Re: Debugging Arduino UNO + Ethernet Shield. When Using Timer1 program hangs
Reply #9 - Nov 20th, 2014 at 4:38pm
Print Post  
Quote:
I suspect that a serial.print() in an ISR will also fail.

I confirm Smiley It's quite impossible to debug from within the ISR. Using a variable you read once the ISR terminates is one way, but things is you are not synchronized because your can't control how much later the test code will run.

Maybe setting the state of unused parallel pins could do it, but then you need a scope or a logic analyzer.

I suspect that serial functions need interrupts to work, so writing to serial in an ISR would mess the things.
  
Back to top
 
IP Logged
 
Peter Pen
Newbies
*
Offline


Posts: 7
Joined: Nov 20th, 2014
Re: Debugging Arduino UNO + Ethernet Shield. When Using Timer1 program hangs
Reply #10 - Nov 20th, 2014 at 5:18pm
Print Post  
I know that ISRs are for doing a short job, and I was not going to do serial communication within the ISR. As you see in the code above I do Serial output inside my loop() function. And I am curious why doesn't the serial output was incomplete. 

I generally don't understand how does the Serial output relate to the Timer and my ISR for my case. I would be grateful for the clarification! 

P.S.: I was previousely writing assembly code for ATmega8 Asuro in Atmel Studio (older version). And there I was able to enter the ISR and track what is going on there. Same thing I was expecting to do here.

Thanks for all your replies.
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12076
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Debugging Arduino UNO + Ethernet Shield. When Using Timer1 program hangs
Reply #11 - Nov 20th, 2014 at 5:43pm
Print Post  
I'm no expert. Gilles knows more but I can say that the debugger uses serial because there is no other way with an Arduino unless you hack the reset on the board and buy a hardware programmer.

  
Back to top
WWW  
IP Logged
 
Gilles Plante
Full Member
***
Offline


Posts: 156
Location: Québec, Canada
Joined: Oct 31st, 2013
Re: Debugging Arduino UNO + Ethernet Shield. When Using Timer1 program hangs
Reply #12 - Nov 20th, 2014 at 7:28pm
Print Post  
I never dig into serial code to see how it operates, but on occasion I had serial.print() instructions that never send anything to the window event though I know the program came across those instructions.

I recall Tim telling the serial buffers are small, could it be some data was erased before the instructions were executed ? It's one of those unsolved mysteries  Undecided.
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12076
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Debugging Arduino UNO + Ethernet Shield. When Using Timer1 program hangs
Reply #13 - Nov 20th, 2014 at 7:43pm
Print Post  
I don't think the buffers are connected to this. The serial and many other arduino functions can not be used in an ISR

For your own serial outside of an ISR you probably need to println() to force the data to show but this is all a discussion for another thread  Smiley
« Last Edit: Nov 20th, 2014 at 7:44pm by Tim@Visual Micro »  
Back to top
WWW  
IP Logged
 
Peter Pen
Newbies
*
Offline


Posts: 7
Joined: Nov 20th, 2014
Re: Debugging Arduino UNO + Ethernet Shield. When Using Timer1 program hangs
Reply #14 - Nov 20th, 2014 at 8:46pm
Print Post  
@Tim Leek and @Gilles Plante, 

thank you very much for sharing your thoughts. It also was breaking my head trying to understand why I don't get messages printed. In fact it was because I was using Serial.print() instead of Serial.println().

Cheers
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint