cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F217 SPI3 always reads zeros

eckhard
Associate
Posted on April 28, 2015 at 11:25

SPI3 is configured as master readonly.

When the SPI is enabled then the clock can be monitored by an oscilloscope.

The Interrupt-handler stops SPI3 after having received 3 bytes. However the data are always Zero regardless of MISO  is 1 or 0.

void SPI3_readonly_Config(void) { // SPI3 is Master readonly

  /* available SPI3-Pins:

 

  SPI3_SCK: PB3 (JTAG-conflict) use:  PC10

  SPI3_MISO PB4 (JTAG-conflict) use:  PC11

  SPI3_MOSI PB5 (JTAG-conflict) use:  PC12, not used because readonly

  SPI3_NSS      PA15, not used

  */

  GPIO_InitTypeDef GPIO_InitStructure;

  SPI_InitTypeDef  SPI_InitStructure;

  NVIC_InitTypeDef NVIC_InitStructure;

 

  /* Enable the SPI clock */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);

 

  /* Enable GPIOC clock, do not use MOSI and NSS */

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

  /* Connect SPI pins to AF6, do not use MOSI */ 

  GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_SPI3);

  GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_SPI3);

 

  /* SPI SCK pin configuration */

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

  GPIO_Init(GPIOC, &GPIO_InitStructure);

  /* SPI  MOSI pin configuration not used, because readonly */

  /* SPI  MISO pin configuration as input */

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;

  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_11;

  GPIO_Init(GPIOC, &GPIO_InitStructure);

  /* NSS pin not used */

 

  /* SPI configuration ----------------------------------------*/

  SPI_I2S_DeInit(SPI3);

  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_RxOnly;

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

  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

  SPI_InitStructure.SPI_CRCPolynomial = 0;

  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

  SPI_Init(SPI3, &SPI_InitStructure);

 

  /* Configure the SPI interrupt */

  NVIC_InitStructure.NVIC_IRQChannel = SPI3_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

  /* Enable SPI_MASTER RXNE interrupt */

  SPI_I2S_ITConfig(SPI3,SPI_I2S_IT_RXNE, ENABLE);

  // SPI_Cmd(SPI3, ENABLE);  do not enable SPI here

}

uint8_t SPI3values[3]={0};

uint32_t SPIindex = 0;

/* SPI3 Interrupt Handler */

void SPI3_IRQHandler(void) {  // receive 3 bytes

if (SPI_I2S_GetITStatus(SPI3, SPI_I2S_IT_RXNE) == SET) {

  SPI3values[SPIindex] = SPI3->DR; // content of array is always zeros

  if(SPIindex > 1) {

    /* disable SPI */

    SPI3->CR1 &= 0xFFBF;  // clear SPE Bit

    ....

    /* stop SPI, disable SPI clock */

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, DISABLE);

    SPIindex = 0;

    } else SPIindex++;

  }

}

The clock configuration seems to be successful but what is wrong with the MISO-configuration?

2 REPLIES 2
Posted on April 28, 2015 at 13:42

The clock configuration seems to be successful but what is wrong with the MISO-configuration?

ALL the pins for the peripheral need to be in the GPIO AF Mode

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
eckhard
Associate
Posted on April 28, 2015 at 14:20

Yes, that makes sense. Now it works, thank you very much.