AnsweredAssumed Answered

STM32 SPI Setup

Question asked by cooper.ian on May 2, 2013
Latest reply on Nov 26, 2013 by Clive One
Hi all,

New to the stm32 range and so far all is well. I am having a couple of problems setting up SPI on my stm32vldiscovery board and was hoping to get some help. I'm sure I must be missing something very small but can't seem to find it. I have probed portB with a scope but havn't got any visible output.

I have a couple of theories on what may be the problem, perhaps you can help.
The lines:
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2);
Will not compile, I get an error: GPIO_AF_SPI2 is undeclared. To compile I have been commenting these out.

I have not setup any clock preferences?

Thanks again, please see attached code below. (apologies for messy code, still just getting bits working!)
Ian


/**
******************************************************************************
  * @file    main.c
  * @author  icooper
  * @version V1.0.0
  * @date    29/04//2013
  * @brief   Main program body.
******************************************************************************
  * File Description:
  * Just messing around with some io
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_spi.h"
#include "stdint.h"

GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
SPI_InitTypeDef SPI_InitStructure;

void Delay(uint32_t nTime);

int main(void)
{
    //Enable Peripheral Clocks, GPIOC, GPIOA, USART1, Alternative Function(AF), SPI2
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC |
                           RCC_APB2Periph_GPIOA |
                           RCC_APB2Periph_GPIOB |
                           //RCC_APB2Periph_USART1 |
                           RCC_APB2Periph_AFIO ,
                           ENABLE);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2 , ENABLE);


    //Configure Pins for CS for SPI
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //GPIO_Pin is a bit vector
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Output Push Pull
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Set Speed
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    //confugre spi - CLK PA5, MOSI PA7
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Alternative function - push pull
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Set Speed
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2);

    //Configure SysTickTimer
    if    (SysTick_Config(SystemCoreClock / 1000))
    while(1);


    SPI_Cmd(SPI2, DISABLE);
    //configure spi
    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_High;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_Init(SPI2, &SPI_InitStructure);
    SPI_Cmd(SPI2, ENABLE);

  while (1)
  {
      Delay(50); //wait 50ms
      sendbyte_spi_lcd('c');
      Delay(50); //wait 50ms
  }
}

void sendbyte_spi_lcd(unsigned char byte){
    GPIO_WriteBit(GPIOA, GPIO_Pin_4, 0);
    GPIO_WriteBit(GPIOC, GPIO_Pin_8, 1);
    SPI_I2S_SendData(SPI2, byte);
    //while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET);
}

//Delay loop
static __IO uint32_t TimingDelay;

void Delay(uint32_t nTime){
    TimingDelay = nTime;
    while(TimingDelay != 0);
}
void SysTick_Handler(void){
    if (TimingDelay != 0x00)
        TimingDelay--;
}

Outcomes