Search code examples
embeddedstm32stm32f0

what is the best way to design a shift register with stm32


I am using a STM32F031K6, clocked at 40MHz, and I want to design a program which acts as a looping shift register - an external trigger is used to clock it, the values in the shift register left shift every time a rising/falling edge is received. the output is one pin either high or low.

I need to make the time between the clocking edge and the output less than 0.5uS, or failing that as quick as possible. The values of the shift register can be changed and the length can also be changed, but for now I'm just starting with a byte like 11000010 .

I initially thought to implement this with an external interrupt but it was suggested there may be a better way to implement it

any help much appreciated


Solution

  • You might use the SPI peripheral of the STM32F0 for your task. When configured in slave mode, each time an external clock edge is detected on the SCK signal, the MISO will be set to the next bit of a value loaded into an internal shift register via the SPI data register.

    Check out the chapter on the Serial peripheral interface (SPI) in STM32F0 reference manual. Especially have a look at the sections addressing the following keywords:

    • General description: SPI block diagram
    • Slave Mode (Master selection: Slave configuration)
    • Simplex communication: Transmit-only mode (RXONLY=0)
    • Slave select (NSS) pin management: Software NSS management (SSM = 1)
    • Data frame format (data size can be set from 4-bit up to 16-bit length)
    • Configuration of SPI

    The SPI unit is highly configurable, e.g. regarding the polarity of clock signal. Since it is an independent hardware unit, it should be able to handle your 0.5us reaction time requirement. The MCU firmware needs to set up the SPI unit and then provide new data to the SPI unit, each time the Tx buffer empty flag (TXE) is set. This can also be done by interrupt (TXEIE) or even using a DMA channel (TXDMAEN) with a circular buffer. In the latter case the "shift register functionality" runs completely independent of the MCU core (after setup).