cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f205rg spi configuration

838445810
Associate II
Posted on July 21, 2014 at 11:47

hi,all

I'm working on a SPI interface on stm32f205rg. I'm newly using this chip. I do the configuration as I do on stm32f103 and of cause refer to example of stm32f2. It's just not working. I configure SPI1 worked as a slave. And as I can see in debug, the SPI1->DR is not assigned when sending data. While the CLK from the Master is all right. The code is as followed. Please see if I miss anything or wrong configured. Many thanks.

#include <
string.h
>
#include <
stdbool.h
>
#include ''stm32f2xx.h''
/* Private constants -----------------------------------------------------------*/
#define SPI_BUF_SZ 128
/* Private typedef -------------------------------------------------------------*/
/* Private macro --------------------------------------------------------------*/
#define TIP_SPI SPI1
#define TIP_SPI_CLK_PIN GPIO_Pin_3
#define TIP_SPI_MISO_PIN GPIO_Pin_4
#define TIP_SPI_MOSI_PIN GPIO_Pin_5
#define TIP_SPI_PORT GPIOB
#define TIP_SPI_NSS_PIN GPIO_Pin_4
#define TIP_SPI_NSS_PORT GPIOA
#define SLV2MST_CS GPIO_ResetBits(TIP_SPI_NSS_PORT,TIP_SPI_NSS_PIN)
#define SLV2MST_NCS GPIO_SetBits(TIP_SPI_NSS_PORT,TIP_SPI_NSS_PIN)
#define SLV2MST_IS_CS GPIO_ReadOutputDataBit(TIP_SPI_NSS_PORT,TIP_SPI_NSS_PIN)==Bit_RESET
/* Private variable -------------------------------------------------------------*/
/* Private functions prototype ----------------------------------------------------*/
/* Private functions ------------------------------------------------------------*/
uint8_t TIPSPI_BUF[128] = {0};
uint8_t dma_tx_available = 1;
uint8_t spi_rx_flag = 0;
void GpioInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* configure GPIO*/
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB, ENABLE);
/* Use Alternate Functions for SPI pins */
GPIO_PinAFConfig( TIP_SPI_PORT, TIP_SPI_CLK_PIN, GPIO_AF_SPI1 );
GPIO_PinAFConfig( TIP_SPI_PORT, TIP_SPI_MISO_PIN, GPIO_AF_SPI1 );
GPIO_PinAFConfig( TIP_SPI_PORT, TIP_SPI_MOSI_PIN, GPIO_AF_SPI1 );
/* Setup SPI pin types */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; 
GPIO_InitStructure.GPIO_Pin = TIP_SPI_CLK_PIN | TIP_SPI_MISO_PIN | TIP_SPI_MOSI_PIN;
GPIO_Init( TIP_SPI_PORT, &GPIO_InitStructure );
/* Setup NSS pin type*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 
GPIO_InitStructure.GPIO_Pin = TIP_SPI_NSS_PIN;
GPIO_Init( TIP_SPI_NSS_PORT, &GPIO_InitStructure ); 
SLV2MST_NCS; 
}
void SpiInit(void)
{
SPI_InitTypeDef SPI_InitStructure;
GpioInit();
NvicInit();
/* configure SPI*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE );
SPI_I2S_DeInit(TIP_SPI);
SPI_Cmd(TIP_SPI, DISABLE);
SPI_StructInit(&SPI_InitStructure);
/*!< SPI configuration */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(TIP_SPI, &SPI_InitStructure);
/* Enable the SPI peripheral */
SPI_I2S_DMACmd(TIP_SPI,SPI_I2S_DMAReq_Rx,ENABLE);
SPI_Cmd(TIP_SPI, ENABLE); /* enable SPI1 module */ 
}
uint16_t SpiSend(uint8_t* buf, uint16_t len)
{
SLV2MST_CS;
while(len--)
{
while(SPI_I2S_GetFlagStatus(TIP_SPI, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(TIP_SPI, *buf++);
}
SLV2MST_NCS;
}
/* Private function prototypes -----------------------------------------------*/
int main(void)
{
SpiInit();
for(;;)
{
SpiSend(''blah blah!'',10);
Spi_delay_ms(10);
}
}

#stm32f205-spi-slave
2 REPLIES 2
Posted on July 21, 2014 at 17:39

Yeah, wouldn't the Chip Select be an INPUT on a slave device, and wouldn't you need to send data back to the master in response to data being shifted IN?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
838445810
Associate II
Posted on July 22, 2014 at 05:19

hi,clive1

thanks for your quick reply.

Chip Select, I assume you mean

TIP_SPI_NSS_PIN in my code.

This a legacy macro. Sorry for its ambiguity.

Actually, 

TIP_SPI_NSS_PIN is a output pin for triggering the Master's EXTI to receive data by sending dummy bytes as well as providing the SCLK.

As I do in

SpiInit(),

 SPI_NSS_Soft mode is configured, so the NSS Pin can be use as GPIO.