cancel
Showing results for 
Search instead for 
Did you mean: 

SPI problem in stm32f10x

parisa
Senior
Posted on January 06, 2017 at 17:40

Hello

Here is my code

#include <stdio.h>

#include <string.h>

#include 'stm32f10x_gpio.h'

#include 'stm32f10x_rcc.h'

#include 'stm32f10x_usart.h'

#include 'stm32f10x_spi.h'

#define SPI_FLASH           SPI3

#define SPI_CLK             RCC_APB1Periph_SPI3

#define SPI_GPIO                GPIOC

#define SPI_GPIO_CLK        RCC_APB2Periph_GPIOC

#define SPI_PIN_SCK         GPIO_Pin_10

#define SPI_PIN_MISO        GPIO_Pin_11

#define SPI_PIN_MOSI        GPIO_Pin_12

void SPIConfig(void){

SPI_InitTypeDef  SPI_InitStructure;

  GPIO_InitTypeDef GPIO_InitStructure;

  /* Enable SPI and GPIO clocks */

  RCC_APB2PeriphClockCmd( SPI_GPIO_CLK , ENABLE);

  GPIO_PinRemapConfig(GPIO_Remap_SPI3, ENABLE);

  /* Configure SPI pins: SCK, MISO and MOSI */

  GPIO_InitStructure.GPIO_Pin = SPI_PIN_SCK | SPI_PIN_MISO | SPI_PIN_MOSI;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(SPI_GPIO, &GPIO_InitStructure);

    

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

  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;

  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;

  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

  SPI_InitStructure.SPI_CRCPolynomial = 7;

  SPI_Init(SPI3, &SPI_InitStructure);

  /* Enable the SPI  */

  SPI_Cmd(SPI3, ENABLE);

}

    

void delay(){

    int i,j;

    for(i=0;i<10000;i++)

    for(j=0;j<250;j++);

}

int main(){        

SPIConfig();            

    while(1){            

    delay();delay();

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

    SPI_I2S_SendData(SPI3,0xff);    

    }

}

But unfortunately I can't see anything on CLK pin and  after I added 'while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET);' line to my program it stops in this line.

what is my mistake?

Thanks in advamce

1 ACCEPTED SOLUTION

Accepted Solutions
Posted on January 10, 2017 at 07:34

MOSI means 'Master Out Slave In', i.e. on device configured as master it is output, on device configured as slave it is input.

Vice versa, MISO means 'Master In Slave Out', i.e. on device configured as slave it is output, on device configured as master it is input.

You always connect output to input.

JW

PS. You seem to have forgotten to enable the SPI clock in RCC.

View solution in original post

2 REPLIES 2
parisa
Senior
Posted on January 10, 2017 at 07:24

Hi,

In addition I want to know why do we connect both MOSI together and MISO together too? in Full duplex mode?

why can't we use this wiring?

MISO->MOSI

MOSI->MISO

0690X00000605weQAA.png

Thanks

Posted on January 10, 2017 at 07:34

MOSI means 'Master Out Slave In', i.e. on device configured as master it is output, on device configured as slave it is input.

Vice versa, MISO means 'Master In Slave Out', i.e. on device configured as slave it is output, on device configured as master it is input.

You always connect output to input.

JW

PS. You seem to have forgotten to enable the SPI clock in RCC.