Skip to main content
prad90
Associate
April 4, 2015
Question

SPI protocol STM32F303

  • April 4, 2015
  • 3 replies
  • 887 views
Posted on April 04, 2015 at 08:49

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

}

 

#spi-protocol-stm32f303
This topic has been closed for replies.

3 replies

Amel NASRI
ST Technical Moderator
April 6, 2015
Posted on April 06, 2015 at 11:25

I would greatly appreciate if someone points me towards SPI implementation examples.

Check the ones under STM32F30x_DSP_StdPeriph_Lib_V1.2.0\Projects\STM32F30x_StdPeriph_Examples\SPI.

STSW-STM32108 to be downloaded from this page: www.st.com/web/en/catalog/t

ools/PF258144.

-Mayla-

To give better visibility on the answered topics, please click on "Best Answer" on the reply which solved your issue or answered your question.
Tesla DeLorean
Guru
April 6, 2015
Posted on April 06, 2015 at 15:08

GPIO_PinAFConfig(GPIOA, GPIO_PinSource4|GPIO_PinSource5|GPIO_PinSource7, GPIO_AF_5);

You have to config the AF settings for each pin individually, you can't OR them together like this because PinSource in an Index.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
prad90
prad90Author
Associate
April 13, 2015
Posted on April 13, 2015 at 03:48

Thanks for the help! I was finally able to debug the problem.