Atmel Studio chip Simulator - Page 1
Forum section: electronics / general

author: dan3460
author_rank: Frequent Contributor
timestamp: December 27, 2016, 05:43:38 pm
content: I was playing with TWI and I was looking at the simulator to see what was happening. The code hangs while waiting for a command to finish. I have something similar to this:Code:WTCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);while (!(TWCR & (1<<TWINT)));This hangs on the while. Any work around this?

author: kiran_shrestha
author_rank: Regular Contributor
timestamp: December 28, 2016, 02:58:32 am
content: I think this is because you dont have slave present.

author: dan3460
author_rank: Frequent Contributor
timestamp: December 28, 2016, 03:10:22 am
content: Do you know how to simulate a slave?

author: ajb
author_rank: Super Contributor
timestamp: December 29, 2016, 12:49:21 am
content: When you see a construct like this:Code:while (!(TWCR & (1<<TWINT)));it means the software is waiting for something to happen in the hardware, or possibly in an ISR. In this case, the program will stick on this line until the TWINT bit in the TWCR register is set. Looks like TWINT is an interrupt flag, and you're expecting it to be set once the START condition has been asserted on the TWI bus. Except with the simulator, there's no hardware to to complete that task, nor is there any bus to assert that condition on. So the simulator, left to its own devices, is just going to sit there forever.There are two solutions: you can set a breakpoint on the loop and manually set TWINT (you can edit TWCR in the watch window) before resuming, or you can just comment out the while loop. The former saves you the trouble of remembering to uncomment that line before moving to a real chip, but the latter will be far less aggravating if you're going to be doing a lot of simulating. You could potentially put the while loop in an #if/#endif block I suppose.Generally, simulation isn't really useful for peripheral drivers, though, so if those aren't the focus of your simulation efforts you could also just stub them out for now and fill them in once you're running on actual hardware.

author: dan3460
author_rank: Frequent Contributor
timestamp: December 29, 2016, 12:32:56 pm
content: Thanks for the answer, I suspected that something like you are describing was happening. I did try to change the TWINT bit on the register window but didn't work, the simulator will change it automatically back to 0 and would not end the while statement.It seems weird to me that you will go to the trouble of simulating the behavior of a whole bunch of registers and not allowed for such basic flag to be set.You can probably comment out the statements but I think will be a hassle and I can bet that the software will hang on the chip because I forgot to un-comment on line and I will be pulling my hear out.Don't know if the #if endif# will work either as you will have to make a compilation for the simulator and another for the real chip.On the software that I'm working on there is extensive use of the TWI bus, I have several devices attached to it, would have been nice to be able to run the software on the simulator first. I know that the simulator allows for inputs to be written on a file, I think I have not read/try it yet, to server as stimuli for the simulator.Thanks