SPI over USART

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
luca
Posts: 3
Joined: Tue Jul 11, 2017 4:12 pm

SPI over USART

Post by luca »

Hi,
I'm developing on a STM32L073-Nucleo64 board with a STM32L073RZT6 on it.
For my purpose i need to use the SPI driver. The problem is that i need to use the following pins setup:
    PA1 = Chip Select
    PA2 = MOSI
    PA3 = MISO
    PA4 = Serial Clock
and according with the datasheet of STM32L073RZ those pins do not support any SPI driver with these specific settings.
I noticed that these pins can be used with the following setup:
    PA2 = USART2_TX
    PA3 = USART2_RX
    PA4 = USART2_CK
Looking on the reference manual seems like that i can use the USART like an SPI but i cannot figure out how i should proceed.
There is any example or any tips for understand, if it's possible, what i should do for use the SPI over the USART ?
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: SPI over USART

Post by steved »

Are you stuck with that pin assignment? Its generally best to look at what functions are available on each pin as a first step; sometimes you have to experiment with combinations of pins to get everything you want. The SPI peripheral is available on PA4..PA7, if you're able to reassign pins.
It's possible that you could use the USART, since it claims to support synchronous communication of the type you want. However there's no support for this in ChibiOS (that I've found, anyway, not that I've looked hard) so it will be up to you to program the USART at the register level. Also be aware that some synchronous modes do 'bit stuffing', which ensures there's a transition on the data line every n clock cycles.
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: SPI over USART

Post by Giovanni »

Hi,

There is no support for USARTs in SPI mode currently, it could be introduced but it would be a mess because the SPI driver would need to handle 2 peripheral types at the same time (because people would want to mix SPIs and USARTs in SPI mode).

Giovanni
luca
Posts: 3
Joined: Tue Jul 11, 2017 4:12 pm

Re: SPI over USART

Post by luca »

Hi,
thanks for the answers. So there is no way to use the SPI on PA1, PA2, PA3, PA4 pins?
I know that there are many pins that support the SPI but unfortunately I'm forced to use those pins.

Thanks in advance,
Luca.
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: SPI over USART

Post by steved »

luca wrote:So there is no way to use the SPI on PA1, PA2, PA3, PA4 pins?

You can't use the internal SPI.
Basically you'll have to write some code.

I had a closer look at the USART, and it should be possible to make that generate SPI-like signals - then have a look at the SPI and UART drivers to get some ideas on how to integrate your driver into your code (you don't have to make it part of ChibiOS; it can be just another module in your project).
Alternatively, you could write some 'bit-banging' code to generate the signals - perfectly adequate if you don't need the SPI to run very fast, and you have the processor capacity.
luca
Posts: 3
Joined: Tue Jul 11, 2017 4:12 pm

Re: SPI over USART

Post by luca »

Hi steved,
thanks for the answer. I will take a look to the SPI and UART drivers as you suggested hoping to find a solution (i'm really newbie to the ChibiOS and embedded world).

Thanks,
Luca.
Post Reply