cancel
Showing results for 
Search instead for 
Did you mean: 

SPI Problem(NO SCK Signal when receiving)

andykim
Associate II
Posted on May 15, 2013 at 05:08

Hi, everyone.

I am trying to control a slave device by SPI Communication.

But I have a problem with SPI, SCK is not output when receiving a data. Tx is OK.

Why does this problem happen?

I hope you help me to slove this problem.

The belowing is my code

void bsp_init_rcc(void)

 /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration -----------------------------*/  

 /* RCC system reset(for debug purpose) */

 RCC_DeInit();

 

 /* Enable HSE */

 RCC_HSEConfig(RCC_HSE_ON);

 

 /* Wait till HSE is ready */

 HSEStartUpStatus = RCC_WaitForHSEStartUp();

 

 ..................................................

 /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);

       

 /* Enable peripheral clocks --------------------------------------------------*/

 /* SPI1 clock enable */

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

 

 /* SPI2 Periph clock enable */

 RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

}

void bsp_init_gpio(void)

{

 GPIO_InitTypeDef GPIO_InitStructure;

 

  /* Configure SPI1 pins: SCK, MISO and MOSI ---------------------------------*/

 /* Confugure NSS, SCK and MOSI pins as Alternate Function Push Pull */

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

 GPIO_Init(GPIOA, &GPIO_InitStructure);

 

 /* Confugure MISO pin as Input Floating  */

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

 GPIO_Init(GPIOA, &GPIO_InitStructure);

 /* Confugure NSS pin as Output  */

 GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4 ;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

 GPIO_Init(GPIOA, &GPIO_InitStructure); 

}

void spi_init(void)

{

  /* SPI1 configuration ------------------------------------------------------*/

  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;

  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;

  SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;

  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;

  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

  SPI_InitStructure.SPI_CRCPolynomial = 7;

  SPI_Init(SPI1, &SPI_InitStructure);

 

  /* Enable SPI1 CRC calculation */

  SPI_CalculateCRC(SPI1, ENABLE);

 SPI_SSOutputCmd(SPI1, ENABLE);

 /* Enable SPI1 */

  SPI_Cmd(SPI1, ENABLE);

 

 SPI_NSS_High();

}

void SPI_tx_data(uint8_t data)

{

  /* Wait for SPI1 Tx buffer empty */

  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);

 SPI_NSS_Low();

  SPI_I2S_SendData(SPI1, data);

 while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET) ;

 SPI_NSS_High();

}

uint8_t SPI_rx_data(void)

{

 uint8_t data;

 /* Wait for SPI1 data reception */

 while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);

 

 SPI_NSS_Low();

  data = SPI_I2S_ReceiveData(SPI1);

 SPI_NSS_High();

 

 return data;

} Please help me

2 REPLIES 2
Posted on May 15, 2013 at 06:21

Why does this problem happen?

Because the bus is symmetrical, as a Master device it clocks out on one edge, and back on the other. ReceiveData just reads a holding register, it does not clock. You'll need to send a junk/dummy byte to generate the clocks to load the receive register.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
andykim
Associate II
Posted on May 15, 2013 at 07:11

Hi clive1

Do you mean the folllowing coding?

uint8_t SPI_rx_data(void)

{

 uint8_t data;

 SPI_NSS_Low();

  SPI_I2S_SendData(SPI1, 0x00);

 while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET) ;

 SPI_NSS_High();

 

 /* Wait for SPI1 data reception */

 while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);

  data = SPI_I2S_ReceiveData(SPI1);

 return data;

} By a dummy byte tx, I can see a data output from the slave device.

But I cann't read the recevied data at the data register of the STM32F103, which is received from the slave.

How can I do this?

Thanks clive1.