Your spi_send function is a loop which writes characters one at a time to the SPI port in software. This might work with a very slow SCK, but there's still the fundamental problem that you don't want your CPU to be sitting in that tight loop all the time waiting for SCK.
In most devices, what happens when you write a byte to the SPI interface is:
- if the Tx shift register is empty, the byte is immediately transferred to it. If not, the byte is held in a temporary (holding) register until it is.
- each edge on SCK causes the shift register to advance one bit, and the bit that's shifted out appears on MISO
- as soon as the last bit has been shifted out, the contents of the holding register are transferred to the shift register
- at this point, the holding register is empty, and you must ensure it is written with the next byte before the shift register also becomes empty.
This all means that you must ensure the holding register is updated each and every time it becomes empty, otherwise the SPI interface will underrun (ie. there's nothing new to copy into the shift register at the moment it empties) and you'll get duplicated characters transmitted.
The problem with this is you have absolutely no idea when the holding register becomes empty, unless you're sitting in a tight loop polling it. The CPU literally cannot do anything else - not even service an interrupt - in case it misses that critical moment when the 8th SCK has been received and there's just a few instructions' worth of time to write to the holding register again.
This is where DMA comes in. Once a DMA channel is set up, the SPI interface itself can indicate to the DMA controller each time the holding register has become empty, and this causes a new byte to be transferred without the CPU itself being involved at all.
So you don't need spi_send. Just delete it.
To reiterate:
- set up SPI port in slave mode
- make voltage measurement, translate into whatever string of digits you'll transmit to SPI
- set up a DMA channel to write those digits to the SPI Tx register, and enable it.
- go off and do whatever other things the CPU needs to do.
What you'll see, if you check the DMA controller's registers, is that exactly one byte gets transferred immediately; this is the write of the first byte into the holding register. There's a hardware link between the SPI interface and the DMA controller which tells it not to write any further bytes until the holding register becomes empty. Once SCK starts toggling, the DMA controller will keep transferring a byte at a time to the SPI port.
Don't forget that you'll need some way for the STM32 to indicate to the RPi that there is actually valid data ready to transfer. In a system that needs to be reliable, the RPi needs to know when the SPI interface is ready to transfer data, and when it isn't. You don't want an SPI read to occur while you're busy updating the measurement or reconfiguring the DMA controller. Typically this is done with a separate GPIO, configured to be a 'data ready' output. This is polled by the master, or used to generate an interrupt.