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:

ISCON) != SnIR:

ISCON) {
}
W5100.writeSnIR(s,SnIR:

ISCON); // clear the interrupt bit
Serial.print("\r\nDisconnected.");
}