AnsweredAssumed Answered

SPI NSS Hardware control

Question asked by kim.andy.002 on Apr 30, 2013
Latest reply on Apr 30, 2013 by waclawek.jan

Hello,

I have a problem with SPI communication in case of NSS Hardware control.
NSS of SPI Signal is always low.
I know that it is low when SPI is enable, and high when SPI is disable.
But it is always low. why?

The following is my program related with SPI.
==============================================================
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();

 if (HSEStartUpStatus == SUCCESS)
 {
  /* Enable Prefetch Buffer */
  FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  /* Flash 2 wait state */
  FLASH_SetLatency(FLASH_Latency_2);

  /* HCLK = SYSCLK */
  RCC_HCLKConfig(RCC_SYSCLK_Div1);

  /* PCLK2 = HCLK */
  RCC_PCLK2Config(RCC_HCLK_Div1);
  /* PCLK2 = HCLK/2 */
//  RCC_PCLK2Config(RCC_HCLK_Div2);

  /* PCLK1 = HCLK/2 */
  RCC_PCLK1Config(RCC_HCLK_Div2);

#if defined( STM32F10X_CL )
  /* Configure PLLs *********************************************************/
  /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  RCC_PREDIV2Config(RCC_PREDIV2_Div5);
  RCC_PLL2Config(RCC_PLL2Mul_8);

  /* Enable PLL2 */
  RCC_PLL2Cmd(ENABLE);

  /* Wait till PLL2 is ready */
  while (RCC_GetFlagStatus(RCC_FLAG_PLL2RDY) == RESET)
  {}

 #if defined (HSE_8MHZ)
  /* PLL configuration: PLLCLK = (PLL2 / 5) * 9 = 72 MHz */
  RCC_PREDIV1Config(RCC_PREDIV1_Source_PLL2, RCC_PREDIV1_Div5);  
  RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_9);
 #endif
#else

 #if defined (HSE_12MHZ)
  /* PLLCLK = 12MHz * 6 = 72 MHz */
  RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_6);
 #elif defined( HSE_16MHZ )
  /* PLLCLK = 16MHz /2 * 9 = 72 MHz */
  RCC_PLLConfig(RCC_PLLSource_HSE_Div2, RCC_PLLMul_9);
 #else  // 8MHZ  
  /* PLLCLK = 8MHz * 9 = 72 MHz */
  RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
 #endif
#endif

  /* Enable PLL */
  RCC_PLLCmd(ENABLE);

  /* Wait till PLL is ready */
  while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  {
  }

  /* Select PLL as system clock source */
  RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  /* Wait till PLL is used as system clock source */
  while(RCC_GetSYSCLKSource() != 0x08)
  {
  }
  }
  else
  { /* If HSE fails to start-up, the application will have wrong clock configuration.
     User can add here some code to deal with this error */   

  /* Go to infinite loop */
  while (1)
  {
  }
 }
 
 /* enable the PWR clock */
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

 /* 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);

 /* USART1 Periph clock enable */
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

 /* TIM1 Periph clock enable */
 // RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

 /* TIM2 clock enable */
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); 

 Set_USBClock();

   /* 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 the GPIO ports( leds ) */
  GPIO_InitStructure.GPIO_Pin =  LED_USER_PIN;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(LED_USER_PORT, &GPIO_InitStructure); 

  /* GPIOA Configuration:TIM2 Channel1, 2, 3 and 4 in Output */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 
  GPIO_Init(GPIOA, &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_4 | 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);
}

void spi_test(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);

  /* Transfer procedure */
  /* Wait for SPI1 Tx buffer empty */
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);

  /* Send SPI2 data */
  SPI_I2S_SendData(SPI1, SPI1_Buffer_Tx[TxIdx++]);
}
==============================================================

Is there any error for NSS Control?

Outcomes