cancel
Showing results for 
Search instead for 
Did you mean: 

Setting up SPI-bus

reckan
Associate II
Posted on April 11, 2011 at 10:56

Hey, Im trying to set up an SPI-bus between the STM32VLDISCOVERY and an external flash memory. I want the STM32-processor to be master and the flash-memory to be slave.

What ive tried to far is simply to set up the GPIOs and also trying to send simple data but i get nothing. Im using an oscilloscope to read Pin7 on PortA on the STM32board. Obviously im doing something wrong but i dont really know what. Probably its some code missing?

This is what i have so far:

&sharpinclude ''stddef.h''

&sharpinclude ''stm32f10x.h''

&sharpinclude ''stm32f10x_gpio.h''

&sharpinclude <stm32f10x_spi.h>

&sharpinclude ''string.h''

&sharpdefine SPI_COMMS           SPI1

&sharpdefine PORT_SPI_COMMS      GPIOA

&sharpdefine PIN_SPI_COMMS_NCS   GPIO_Pin_4

&sharpdefine PIN_SPI_COMMS_SCK   GPIO_Pin_5

&sharpdefine PIN_SPI_COMMS_MISO  GPIO_Pin_6

&sharpdefine PIN_SPI_COMMS_MOSI  GPIO_Pin_7

void GPIO_Configuration(void);

void SPI_Configuration(void);

void RCC_Configuration(void);

int main(void)

{

    GPIO_Configuration();

    SPI_Configuration();

    RCC_Configuration();

     

    for(;;){

          SPI_I2S_SendData(SPI1,0xAAAA);

   

      }

}

void SPI_Configuration(void)

{

    SPI_InitTypeDef SPI_InitStructure;

    /* SPI_MASTER 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_High;

      SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;

      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(SPI_COMMS, &SPI_InitStructure);

    /* Enable SPI_MASTER TXE interrupt */

      SPI_I2S_ITConfig(SPI_COMMS, SPI_I2S_IT_TXE, ENABLE);

    /* Enable SPI_MASTER */

      SPI_Cmd(SPI_COMMS, ENABLE);

}

void GPIO_Configuration(void)

{

    GPIO_InitTypeDef GPIO_InitStructure;

    // Enable SPI1 Pins Software Remapping //

    //GPIO_PinRemapConfig(GPIO_Remap_SPI1, ENABLE);

    // Configure SPI_MASTER pins: SCK, MISO and MOSI //

    GPIO_InitStructure.GPIO_Pin = PIN_SPI_COMMS_SCK | PIN_SPI_COMMS_MISO | PIN_SPI_COMMS_MOSI;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_Init(PORT_SPI_COMMS, &GPIO_InitStructure);

    GPIO_Init(PORT_SPI_COMMS, &GPIO_InitStructure);

}

void RCC_Configuration(void)

{

    // Enable GPIO clock //

    RCC_APB2PeriphClockCmd(PORT_SPI_COMMS, ENABLE);

}

What am i missing?

Thanks in advance for your help!!

//Fredrik

#spi
4 REPLIES 4
donald2
Associate II
Posted on April 11, 2011 at 18:06

The GPIO pin control registers won't latch new values until it has an active clock.

You don't enable the clocks until after you try to write the GPIO registers.

Move the RCC setup so that it is done first.

This is such a common oversight that the run-time environment that I wrote does this as the first step, with a weak binding so an application can override the settings if it's especially power-sensitive.  I don't expect this to ever to be overridden, since such an application would just explicitly turn off everything it could after configuration.

/* The default settings enable all of the the timers and GPIO ports. */

unsigned int __attribute__((weak))

#if 0

_RCC_APB1ENR = APB1ENR_TIM3EN,

_RCC_APB2ENR = APB2ENR_IOPDEN | APB2ENR_IOPCEN | APB2ENR_IOPBEN

    | APB2ENR_IOPAEN | APB2ENR_AFIOEN;

#else

_RCC_APB1ENR = 0x7fffffff,

_RCC_APB2ENR = 0x003fffff;

#endif

/* This routine is the entry point after reset.

 * We configure a few essential registers, then leap into main();

 */

void __attribute__((naked, used, noreturn))

_crt0(void)

{

    /* Enable peripheral clocks.  This bumps the power use up slightly,

     * but developers benefit from having peripherals work by default

     * rather than mysteriously not respond.

     * If the program is power sensitive it can override the weak bindings.

     */

    APB1ENR =_RCC_APB1ENR;

    APB2ENR = _RCC_APB2ENR;

reckan
Associate II
Posted on April 12, 2011 at 14:01

Hello and thanks for your help!

I did change the order and now the RCC config is done before the GPIO. But it didn't work for me. I still dont see any signs of datatransfer. Do i need the rest of your code? And where do i put that exactly?

Again, thanks for your time and help!

//Fredrik

reckan
Associate II
Posted on April 14, 2011 at 14:11

I got it working. Thanks for your help

rasimoguzturk
Associate III
Posted on October 10, 2012 at 20:35

hi andersson can you put the complete code for me,i am also working on that.

thanks