Hi,
Tl; dr:
How do I use SPI on the nano every by using registers instead of SPI.transfer()?
SOLVED:
SPI0_DATA is correct. My initialization or something else was wrong. I now just use Arduino's SPI.begin(), SPI.beginTransaction and SPI.endransaction and only do the data-part via the SPI0_DATA register.
I'm trying to use SPI (in master mode) on a Nano Every by using the SPI registers directly.
The reason want to do this instead of simply using SPI.transfer() is that I want to use the time the transfer takes to do other stuff.
So i want to put my byte into the SPI data register, do other stuff and periodically check for the flag that signals that the transfer is completed (and then send the next byte and so on).
(My SPI runs at 1 Mhz and I have to receive 48 bytes every 1 ms. If I do nothing while the bytes are getting shifted out I use a lot of time I could use for processing. Of course I could use a faster Arduino, but I would prefer the nano every.)
I used the examples in the spi manual from Microchip and tried to change the port to E, as I think this is the port used for spi by default on the nano every (see pinout).
I use this to configure the spi:
static void SPI0_init(void)
{
PORTE.DIR |= PIN0_bm; /* Set MOSI pin direction to output */
PORTE.DIR &= ~PIN1_bm; /* Set MISO pin direction to input */
PORTE.DIR |= PIN2_bm; /* Set SCK pin direction to output */
PORTE.DIR |= PIN3_bm; /* Set SS pin direction to output */
SPI0_CTRLA = SPI_CLK2X_bm /* Enable double-speed */
| SPI_DORD_bm /* LSB is transmitted first */
| SPI_ENABLE_bm /* Enable module */
| SPI_MASTER_bm /* SPI module in Master mode */
| SPI_PRESC_DIV16_gc; /* System Clock divided by 16 */
}
... and this, to transfer data (i connected MOSI and MISO on the board to get the byte back I send):
static uint8_t SPI0_exchangeData(uint8_t data)
{
SPI0_DATA = data;
while (!(SPI0.INTFLAGS & SPI_IF_bm)) /* waits until data is exchanged*/
{
;
}
return SPI0_DATA;
}
Before I transfer I set my SS pin to low and I set it to high aferwards.
However, I don't receive my byte back, I just get 0xFF.
I then tried to
SPI0_DATA = data;
data_r = SPI0_DATA;
and then check, if data and data_r are the same. They are not. So I', guessing SPI0_DATA perhaps isn't the right register for the SPI data.
I looked into the SPI.h and SPI.cpp files provided by arduino. They use SPDR as spi data register. However, if I use "SPDR" arduino studio tells me it was not declared in this scope.
What are the correct registers? Is something else the problem?