AnsweredAssumed Answered

SPI protocol STM32F303

Question asked by kakarla.krishna on Apr 4, 2015
Latest reply on Apr 13, 2015 by kakarla.krishna
Hi,
I am a newbie in the field of micro-controllers and I'm having trouble programming the GPIOA parts as SPI1. Could someone please tell me where I could be going wrong? I would greatly appreciate if someone points me towards SPI implementation examples. The routines I've written so far are given below. I am trying to send just a byte of data and I am using a logic analyzer to check if there's a clock on SCK pin but I don't see anything happen. I also tried toggling an LED by sending data continuously but even this fails. 

void spi_initialization(void)
{
     SPI_I2S_DeInit(SPI1);
     SPI_InitStructure.SPI_Direction= SPI_Direction_1Line_Tx;
     SPI_InitStructure.SPI_Mode= SPI_Mode_Master;
     SPI_InitStructure.SPI_DataSize= SPI_DataSize_8b;
     SPI_InitStructure.SPI_CPOL=SPI_CPHA_2Edge;
     SPI_InitStructure.SPI_CPHA= SPI_CPOL_High;
     SPI_InitStructure.SPI_NSS=SPI_NSS_Soft ;
     SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_2;
     SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB;
     SPI_InitStructure.SPI_CRCPolynomial=7;
     SPI_Init(SPI1,&SPI_InitStructure);
     SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);
      /* Enable SPI1 CRC calculation */   
  SPI_CalculateCRC(SPI1, ENABLE);  
     //SPI_BiDirectionalLineConfig(SPI1, SPI_Direction_Tx);
      
     SPI_Cmd(SPI1, ENABLE);
     
     
}


void send_byte(uint8_t val)
{
    GPIO_ResetBits(GPIOA, GPIO_Pin_4); // CS low
    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); //wait buffer empty
    SPI_SendData8(SPI1, val);
    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET); //wait finish sending
    GPIO_SetBits(GPIOA, GPIO_Pin_4); // CS high
}

void RCC_config()
{
          
     
          //System clock set as HSI
          RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);
     
          // clock freq is 4MHz
          RCC_HCLKConfig(RCC_SYSCLK_Div2); 
       // CLock freq divided by 2 to get 2MHz again on APB2 peripherals for SPI1
          //RCC_PCLK2Config(RCC_HCLK_Div1);
     
       RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
          RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE);
       RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
     
          RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
}

void GPIO_config()
{
     // Setting the pins for alternate mode SPI configura MOSI, SS, SCK
     //GPIO_PinAFConfig(GPIOA, GPIO_PinSource4|GPIO_PinSource5|GPIO_PinSource7, GPIO_AF_5);
     GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
     
          // Set GPIO pin for reset of LCD
     
     GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_15;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOE, &GPIO_InitStructure);


     
     
     // Set GPIO pin as NO PULL for RS functionality
     
     GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_15;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
     
}


int main(void)
{
     
     
     // Initialize clocks
     RCC_config();
     GPIO_config();
     spi_initialization();
     send_byte(0xFF);


}

 

Outcomes