-
Notifications
You must be signed in to change notification settings - Fork 5
update simavr #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thanks! While the TWI patch seems to be unnecessary nowadays, there actually seems to be a new bug either in simavr or avr-tester - if you run avr-tester's testsuite (so basically
Of course, if you'd like to take a look and debug, feel free as well 😄 |
Wow, super weird bug.
So it looks more like a simavr bug. What I don't understand though, is why the simavr examples that use spi seem to run ok. |
Ah I think I get it, reading after writing is always returning the last byte sent, it's your rot13 that's sending those extra |
Ah, I think the test might be wrong after all! -- that test simulates an SPI-master, so actively waiting for incoming data: ... is wrong, because that's something an SPI-slave would do. It just happened to work previously, because simavr had a separate buffer for sent and incoming data, which seems not to be how the hardware actually works (i.e. that test wouldn't work on an actual AVR). I'll adjust the test. Edit: or maaaybe it's because the irq handler within avr-tester doesn't call |
Reading as a master is definitely a valid use case. I've got an old project that uses full duplex SPI - shifting data in and out at the same time. The process is:
The result will be the byte shifted in while the byte you wrote was shifted out. Hmm if everything was working the same as the hardware, I think I would expect the first byte read by the firmware in that test to be 0, because the harness only sets up the send buffer after "Ready!" has been received. Because there was nothing to be read into the avr before then, SPDR would still be 0. Perhaps switching to slave mode would make it a more realistic scenario - since it's job is to respond to bytes sent to it, slave mode would probably be more appropriate. I think there's a timing issue somewhere too, it seems simavr clears SPIF too early after SPDR is written to, and perhaps avt-tester doesn't leave enough time before writing the next byte - adjusting the increment in I think the process in simavr is a bit wrong, and wouldn't support duplex SPI at all. It seems like in master mode it should be raising an irq when a byte is sent, but also another to collect the next byte shifted in, or maybe that can be done in one? |
Yes, this seems about right - instead of using This way instead of keeping this hack: ... we'd raise ... similarly to TWI: |
On a second thought, it might be actually easier and better to adjust the SPIF behavior - like you said, it seems to be cleared too early, without simavr waiting for the response to actually appear (which is to say that it probably expects the SPI interrupt to be synchronous and raise the input interrupt at the same time it sends the output interrupt). |
Oh yeah I think you're on to something, the way you have TWI working looks like the way It'd need to be handled. I wonder if it would just work without any modification to simavr? |
maybe instead of needing a specific slave device, you could just send from the tx buffer whenever there was something in it - possibly defaulting to 0 is there isn't. |
Patryk27/avr-tester#8 It works! It probably needs some refactoring though to make it clearer what's going on - and I'm not sure how this would impact when the AVR is in slave mode |
Nice! Looks like a good way forward - though, like you mention, probably doesn't compose well in AVR in slave mode as the Maybe a good idea would be to have |
I've added support for spi slave mode, with a test... it doesn't work 😭 The irqs I'm getting back from Here simavr raises an output irq whenever a byte is received in slave mode, but it sends the new byte value, when I would I'd love if you could have a look and let me know what you think. We were wrong about it being Also, |
I see, thanks! I'll take a look on Friday 🙂 |
Yeah, it seems simavr doesn't provide any way to get the modified SPDR out of it - this: ... doesn't work as expected, because this call: ... is sort of asynchronous - it doesn't execute the interrupt handler right there, it just registers that the interrupt needs to be run some time later in the future (most likely on the next CPU tick), so then: ... raises interrupt with the same value that was just written into the register. The response-value is available later, over: ... but since simavr doesn't raise the interrupt when AVR is a slave: ... the write to SPDR from within the interrupt handler is effectively discarded. Considering this, and the fact that avr-hal doesn't allow you to use AVR as slave anyway, I think it's safe not to implement it and stick to just the AVR-as-master scenario. |
Great I'm glad we reached an agreed way forward. I've redone the necessary changes as Patryk27/avr-tester#9 and updated this PR removing the TWI patch. I'll leave version bumping to you if that's ok. Thanks for all your input on this, and thanks for creating this great project, it's been really useful for me :) |
Great, I'm glad you've found the project useful as well 🙂 |
Updates simavr to the latest (personally I wanted buserror/simavr#550 available)
Redid the twi patch - not totally sure how necessary this is now?