NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » SPI: Checking SPIF while in an interrupt Handler
September 16, 2012 by jlaskowski |
I am writing some SPI code where the MCU is the master and a CC3000 WiFi chip is the slave. The code responds to a CC3000 interrupt request to initiate the first SPI write. Can I do an SPI write while in an interrupt handler? I don't have the SPI interrupt turned on, so I'm just waiting for the SPIF flag to get set in a loop:
I noticed that writing to the UART for logging with HyperTerminal doesn't work during this interrupt (although writing to the LCD does), so I'm wondering if all SPI work has to be done outside of an interrupt handler. |
---|---|
September 16, 2012 by jlaskowski |
Here's my config code:
|
September 16, 2012 by jlaskowski |
Re-reading my original post, I realize I didn't mention the specific problem I'm having. The problem is that SPIF is not being set after writing to SPDR. |
September 16, 2012 by jlaskowski |
It seems if when I set these bits this way, the Master wasn't actually being set, but auto-cleared:
However when I set them like this, it stayed set:
The ATMega168 doc says it will auto-clear the MSTR if you try to set it when SS is set as input and it's low. That should not be the case, since I set SS as output before this. Not to mention, it doesn't indicate it should make a difference whether you set the bits in SPCR in two instructions or in one. |
September 16, 2012 by Noter |
In general it's a good idea to do as little as possible in an interrupt routine and give control back as soon as possible. One thing to keep in mind is that usually other interrupts will be blocked while you're in an interrupt routine. Maybe reviewing this working example of spi master/slave will help you solve your problem ... Master/Slave SPI |
September 17, 2012 by jlaskowski |
I actually got it to work using an example of SPI for ATMega168 online. As I mentioned, what I found seems like it might be a bug in the ATMega168 chip. I think I set the MSTR bit correctly both ways as I've shown above, but only one works. The document does say this: This bit selects Master SPI mode when written to one, and Slave SPI mode when written logic zero. If SS is configured as an input and is driven low while MSTR is set, MSTR will be cleared, and SPIF in SPSR will become set. The user will then have to set MSTR to re-enable SPI Master mode. So there are times when MSTR is forced low, however, as you can see, I set SS as an output, not an input:
|
September 17, 2012 by Noter |
When you enable SPI without setting MSTR 1st, it is in slave mode and that configures SS as an input. If SS is not held high then when you set MSTR it immediately goes back to slave mode. Simple to test, wire SS high and I expect your MSTR will take effect as you expect. |
September 17, 2012 by Noter |
18.3.1 Slave Mode When the SPI is configured as a Slave, the Slave Select (SS) pin is always input ... |
Please log in to post a reply.
Did you know that NerdKits has been featured in the MIT Undergraduate Research Journal? Learn more...
|