SPI newbie questions

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SPI newbie questions

Jump to solution
2,628 Views
Cram
Contributor III

Hi all and thxs in advance it's my first time working with SPI (and sorry for my english),

 

I must comunicate a HC08SH8 microcontroller and a magnetic rotary encoder (AS5040 it use a SSI interface http://www.austriamicrosystems.com/eng/Products/Magnetic-Encoders/Rotary-Encoders/AS5040).

 

If I understood correctly the encoder datasheet as soon as the /CS goes LOW and it has CLK signal it will transmit trought the data line the angular position of a magnet.

 

How I must configure the SPI as a Master or as a Slave? Now I configured the SH8 as a master and plugged the data ouput from the encoder to the MISO line (because I want to stablish a comunication), is it correct? Nothing work for the moment, I don't have CLK signal on SPICLK output so it must be a configuration problem.

 

I only need to read so I must wait (polling or IRQ) until fill the read-buffer isn't it?

 

another question how can I read 16 bit data-transmission? the encoder give me the position data un 10 bits + CRC + some magnet data.

 

I need some help!

Labels (1)
0 Kudos
Reply
1 Solution
1,251 Views
bigmac
Specialist III

Hello,

 

The required SPI module connections would be the MISO and SPICLK lines.  The CS line will need to be derived from a GPIO pin.  The SPI module would operate in master mode. 

 

Even though you have no interest in the MOSI signal (and it would remain unconnected), it is still necessary to send a (dummy) byte value for each byte that you wish to receive.  This is the only method of generating the clock pulses - I suspect that you are currently not doing this.  After sending each dummy byte, you will also need to wait until the SPRF flag becomes set before reading the SPID value.  Sending two bytes in succession will generate a total of 16 clock pulses.

 

The SSI protocol requires that the clock signal idles at high state, and that each bit state is sampled on the negative edge of clock signal, so as to fall in the middle of each bit period.  This will require a setting of CPHA = 0 and CPOL = 1.  Fig. 5 of the datasheet for the sensor shows that no valid data is present during the first negative edge (maybe always a high state).  The same diagram also shows that the final negative edge corresponds with the MagDEC bit, and that there is no clock pulse during the final EvenPAR bit, with the result that this bit is never read.

 

Therefore the contents of the two data bytes will shifted to the right by one bit.  This will need to be taken into account when processing the 10-bit position data.

 

Regards,

Mac

 

View solution in original post

0 Kudos
Reply
3 Replies
1,252 Views
bigmac
Specialist III

Hello,

 

The required SPI module connections would be the MISO and SPICLK lines.  The CS line will need to be derived from a GPIO pin.  The SPI module would operate in master mode. 

 

Even though you have no interest in the MOSI signal (and it would remain unconnected), it is still necessary to send a (dummy) byte value for each byte that you wish to receive.  This is the only method of generating the clock pulses - I suspect that you are currently not doing this.  After sending each dummy byte, you will also need to wait until the SPRF flag becomes set before reading the SPID value.  Sending two bytes in succession will generate a total of 16 clock pulses.

 

The SSI protocol requires that the clock signal idles at high state, and that each bit state is sampled on the negative edge of clock signal, so as to fall in the middle of each bit period.  This will require a setting of CPHA = 0 and CPOL = 1.  Fig. 5 of the datasheet for the sensor shows that no valid data is present during the first negative edge (maybe always a high state).  The same diagram also shows that the final negative edge corresponds with the MagDEC bit, and that there is no clock pulse during the final EvenPAR bit, with the result that this bit is never read.

 

Therefore the contents of the two data bytes will shifted to the right by one bit.  This will need to be taken into account when processing the 10-bit position data.

 

Regards,

Mac

 

0 Kudos
Reply
1,251 Views
Cram
Contributor III

Thx bigmac,

 

I'm not sending the dummy, I just start to write the code and I don't know how to do it.

 

So If I understand correctly I could be able to comunicate the both devices, but I'll need to send a first dummy message just to receive the first part of the message then generate a second dummy message and receive the last part of the message and then work with the message splited in two parts, isn't it?

 

Thxs for all

 

 

0 Kudos
Reply
1,251 Views
peg
Senior Contributor IV

Hello Cram,

 

The SPI only reads (along with generates clock pulses) when it sends.

If you just have data to read then the only way to do it is to do a dummy send.

A dummy send is when you just load anything into the SCID register.

This automatically sends the byte (as long as the SPI is setup correctly) which also reads a byte in (at the same time).

Do a search of this forum for SPI, this subject has been covered many times before.

 

0 Kudos
Reply