Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years, 9 months ago.
SPI dialog with AS5047D
Hi all.
Here is my problem : I try to communicate using SPI between a Nucleo F446RE and a AS5047D (absolute position encoder).
It works pretty well with ABI signals but I try to use SPI for test purpose (I must check that SPI is fully functionnal on the component).
the AS5047D is configured for a 16bits Big Endian Mode 1 communication. But I don't see how to set Big Endian communication on SPI as a word is stored as Little endian in my Nucleo.
Thanks to those that will take time to answer my question.
1 Answer
5 years, 3 months ago.
Hi,
There are two ways to communicate with the sensor. The application can split 16 bit command to 2 x byte command and combine the 2 byte answer to one 16 bit lenght variable. The other way is to set SPI to use 16 bits. It might work (Endian is correct) or not. format (int bits, int mode=0) where bits can be 4 -16 / spi.write.
First one could be something like this: uint16_t SPI_operation(uint16_t command16) { uint8_t command[2]; uint8_t answer[2]; uint16_t answer16; command[0] = (uint8_t)(command16>>8); command[1] = (uint8_t)(command16 & 0x00ff); spi.write(command[0]); spi.write(command[1]); answer[0] = spi.write(0x00); answer[0] = spi.write(0x00); answer16 = answer[0]; answer16 = (answer16<<8) + answer[1]; return answer16; }
In general look at https://os.mbed.com/docs/mbed-os/v5.14/apis/spi.html#spi-hello-world
Regards, Pekka
Thank you for your answer, but I've already found an answer to my last question : There was a mistake in STM32 lib. I've solve it (it's in an other thread by me). And for Endianess, I've created a fonction that swap bit order... It's stupid as microcontroler can be easily configure to do it itself, but it was faster than looking in the whole SPI API lib... But I'm still building a mbed lib for the AS5047D.
posted by 26 Nov 2019
I don't know about the specifics here, but sometimes you are stuck with a particular byte order on a communication channel. A workaround is to use a union for byte access to a larger word which allows you to reshuffle things as they go out or come in.
At this point data1.data will equal 0x00FF
posted by Graham S. 07 Jun 2017Thank you for that, but in one byte no structure is able to swap bit order... Even bit field... I realy need to know how to set Endianess in SPI.
posted by H A 07 Jun 2017Looking at SPI_API.h I saw that mode integrate both CPOL, CPHA and shift direction without any more info... Any Ideas ?
Either I've found a :
extract of spi.h from mbed lib
How I may use it to set my data direction ?
Looking at SCK / MOSI / MISO with a scope I see : SS Falling down a 16 periods clock + 1x16 bits frame (0xFFFF) then before SS goes high another 16 periods clock + 16 bits frame (0x0000) And then SS goes high.
But my code says :
extract of my code
where SPIG is an instance of SPI and SPI_READ_ANGLE is defined as 0xFFFF (I've tried to put a 0xFFFFU directly but it change nothing).
So where does the second frame came from as my SPI is define as 16 bits wide ?
Holly God ! I really beleive that SPI library is a crap...
posted by H A 07 Jun 2017