Reputation: 75
I'm working on spi communicate between stm32f0308-discovery and jetson tx2. Jetson is master and stm32 should be slave. (idk how but if it is possible stm32 may be master too.) My problem is I'm new for stm32 and I don't know how an I make stm32 to slave. Can someone show me a way for stm32 spi slave ? Thanks in advance.
Upvotes: 1
Views: 2268
Reputation: 121
You can use stm32cubeMX for this purpose. in the config of the SPI peripheral, you will find option to select master or slave mode
Upvotes: 1
Reputation: 36
Yes, you can use the STM32 as SPI slave.
If you are new with STM32, I recommend to use STM32CubeIDE and its code generation from .ioc file. It will make the low level initialization for SPI. All you have to use is this two function:
HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
If your code has no blocking function in the main while cycle (e.g.:HAL_Delay()), then you can use the SPI of the slave STM32 without interrupt or DMA.
Important: If your slave device has to answer for the master (not just read from it), you have to set the size of the protocol the same as the size of the command bytes (.ioc -> Basic parameters ->Data size). In this way, your HAL_SPI_Receive() function will return with the command bytes, and you can process it before calling HAL_SPI_Transmit().
SPI Echo example ( 1 byte command, 1 byte data / I set the slave select pin manually / If you need to separate reading and writing data, and need to implement setpoint readback, you can improve this code with FromMaster/ToMaster arrays) :
void SPI_Slave_Process(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, SPI_HandleTypeDef *hspi, uint8_t* FromMaster, uint8_t* ToMaster) {
static uint8_t RxData;
static uint8_t TxData;
while(!(HAL_GPIO_ReadPin(GPIOx, GPIO_Pin))) {
HAL_SPI_Receive(hspi, &RxData, 1, 2);
TxData = RxData;
HAL_SPI_Transmit(hspi, &TxData, 1, 2);
}
}
Upvotes: 1
Reputation: 33
Yes. You can make STM32 as the slave. The only thing you need to do is CLEAR the MSTR Bit in The Control Register on the peripheral. You perhaps then can load some values in the SPI Data Register & then can read them from your other board.
Upvotes: 0
Reputation: 134
You can select SPI mode when configuring the SPI_InitTypeDef structure. You need to set the SPI_Mode to Slave as follows:
SPI_InitDef.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitDef.SPI_Mode = SPI_Mode_Slave; // <-- This is it
SPI_InitDef.SPI_DataSize = SPI_DataSize_8b; // 8-bit transactions
SPI_InitDef.SPI_FirstBit = SPI_FirstBit_MSB; // set it to match Master conf
SPI_InitDef.SPI_CPOL = SPI_CPOL_Low; // set it to match Master conf
SPI_InitDef.SPI_CPHA = SPI_CPHA_2Edge; // set it to match Master conf
SPI_InitDef.SPI_NSS = SPI_NSS_Hard; // use hardware SS
An example tutorial using blue pill boards can be found here
Upvotes: 1
Reputation: 2718
You can start by reading the reference manual of your product family. Then, you can find examples of SPI peripheral configuration source code in STM32Cube software packages.
If you are new to STM32 and new to the microcontroller ecosystem, I'm afraid you will need some training. But there are plenty of resources online.
Upvotes: 1