cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103C8 SPI Not Working

markbell9
Associate II
Posted on July 13, 2011 at 20:13

Hello --

I'm having no success in getting a clock signal out of the SPI1_SCK pin on our chip.    TIM1 is working as configured.   I've studied the manual,  download materials from the STM web site,  but no further success.

Here's what I'm doing in our system init function:

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

  /* PCLK1 = HCLK/2 */

  RCC_PCLK1Config(RCC_HCLK_Div2);

  /* PLLCLK = 8MHz * 9 = 72 MHz */

  RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

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

  {

  }

 }

        /* Enable peripheral clocks via the RCC_APB2ENR register */

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA

              | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC

              | RCC_APB2Periph_SPI1 | RCC_APB2Periph_AFIO

       | RCC_APB2Periph_TIM1, ENABLE);  

 

 /*------------------- Resources Initialization -----------------------------*/

 /* GPIO Configuration */

 GPIO_Config();

 /* Interrupt Configuration */

 InterruptConfig();

 /* Configure the systick */

 SysTick_Configuration();

 /*------------------- Drivers Initialization -------------------------------*/

 // Set up time base structure

 TimebaseInit();

 /* If HSE is not detected at program startup */

 if(HSEStartUpStatus == ERROR)

 {

  /* Generate NMI exception */

  SCB->ICSR |= SCB_ICSR_NMIPENDSET;

 }

 /* SPI Initialization */

 DAC_SPI_Init();

}

SPI Initialization is as follows:

    SPI_InitTypeDef SPI_InitStructure;                         

    SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;

    SPI_InitStructure.SPI_Mode =  SPI_Mode_Master;

    SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;

    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_2 ;

    SPI_InitStructure.SPI_FirstBit =  SPI_FirstBit_MSB;

    SPI_Init(SPI1, &SPI_InitStructure);

    SPI_NSSInternalSoftwareConfig(SPI1, SPI_NSSInternalSoft_Set);

    GPIO_WriteBit(GPIOB, GPIO_Pin_0, Bit_SET);    /* Pull /SYNC high */

Here's what I do to send data to the SPI:

SPI1->CR1 |= SPI_CR1_MSTR;

SPI1->CR1 |= SPI_CR1_SPE;

 

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

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

GPIO_WriteBit(GPIOB, GPIO_Pin_0, Bit_RESET);    /* Pull /Sync low */ 

SPI_I2S_SendData(SPI1, Test_Val);     /* Send 2nd tilt byte */

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

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

Nothing appears on the clock or data pin for the SPI.

Registers after the last line are as follows:

CR1:  0xCB45

CR2:  0x0

SR:    0x2  (TXE 1)

DR:  0x0

CRCPR:  0x544

RXCRCR: 0

TXCRCR : 0

Any help would be appreciated.

Thank you

#spi-problems-stm32
1 REPLY 1
Posted on July 13, 2011 at 20:33

Show GPIO configuration.

  /* Enable SPI1 */

  SPI_Cmd(SPI1, ENABLE);

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..