cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L151 SPI only sending zeros.

kkarasti
Associate II
Posted on June 13, 2014 at 18:32

I'm trying to get SPI2 up and running to talk to another chip and I'm having some problems. I can see my SCK and MOSI traces on my scope but my DR register always has zero. I've even tried this right after SPI config:

SPI2->DR = 1;

TXE bit is high when I try this call.

I guess I'm lost at what to look at next.
7 REPLIES 7
Posted on June 13, 2014 at 19:57

Yeah, and I have no idea how your code is configuring this.

Observe that what you read from DR is inbound data (MISO for a Master), it's not the same register you write to DR for the outbound data (MOSI). It is not a memory cell, and looking at it and SR in a debug interface will interfere with it's operation.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
kkarasti
Associate II
Posted on June 13, 2014 at 20:29

I'm trying to port an app from an STM32F103 to STM32L151 and when I go into debug on the working STM32F103, I can see the DR register change.

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

  SPI_I2S_DeInit(SPI2);

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

  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;

  //SPI_InitStructure.SPI_CRCPolynomial = 7;

  SPI_Init(SPI2, &SPI_InitStructure);

/* Enable the SPI peripheral */

  SPI_Cmd(SPI2, ENABLE);

stm322399
Senior
Posted on June 13, 2014 at 20:37

It's a bit hard to understand what is the exact nature of your problem.

Anyway, one noticeable difference between 103 and 151 is that the MISO line must be configured as AF_SPI on 151 whereas it was a simple input on 103. This might help ...

Posted on June 13, 2014 at 22:51

ie Mapping AF functionality to pins, here for example SPI1 PA7

GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1); // SPI1 MOSI
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
kkarasti
Associate II
Posted on June 14, 2014 at 02:53

Here's my GPIO config.

/* Configure I/O for Chip select */

GPIO_InitStructure.GPIO_Pin = CR95HF_SPI_NSS_PIN;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; 

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;

GPIO_Init(CR95HF_SPI_NSS_GPIO_PORT, &GPIO_InitStructure);

GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2); // SCK

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2); // MOSI

  //GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2); // MISO

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;

  /* SPI SCK pin configuration */

  GPIO_InitStructure.GPIO_Pin = CR95HF_SPI_SCK_PIN;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* SPI  MOSI pin configuration */

  GPIO_InitStructure.GPIO_Pin =  CR95HF_SPI_MOSI_PIN;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* SPI MISO pin configuration */

  GPIO_InitStructure.GPIO_Pin = CR95HF_SPI_MISO_PIN;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

// SPI_NSS  = High Level  

GPIO_SetBits(CR95HF_SPI_NSS_GPIO_PORT, CR95HF_SPI_NSS_PIN); 

Maybe something is wrong here but what I see as my first problem is the only data going out on MOSI is 0x00. If I don't know if a valid command is going to the second chip, how can I troubleshoot the MISO line? In other words, I don't think anything will come in on MISO if the data sent isn't valid.

kkarasti
Associate II
Posted on June 16, 2014 at 03:29

OK thanks for clearing that up. Should the MISO line be an input or output? Because the scope trace doesn't look right. My brain tells me input, but one of the examples has it as an output, like the other two lines. This is what I have, which is like the example.

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2); // SCK

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2); // MOSI

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2); // MISO

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;

  /* SPI SCK pin configuration */

  GPIO_InitStructure.GPIO_Pin = CR95HF_SPI_SCK_PIN;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* SPI  MOSI pin configuration */

  GPIO_InitStructure.GPIO_Pin =  CR95HF_SPI_MOSI_PIN;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

/* SPI MISO pin configuration */

  GPIO_InitStructure.GPIO_Pin = CR95HF_SPI_MISO_PIN;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

/* Configure I/O for Chip select */

GPIO_InitStructure.GPIO_Pin = CR95HF_SPI_NSS_PIN;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; 

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;

GPIO_Init(CR95HF_SPI_NSS_GPIO_PORT, &GPIO_InitStructure);

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

  SPI_I2S_DeInit(SPI2);

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

  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

  //SPI_InitStructure.SPI_CRCPolynomial = 7;

  SPI_Init(SPI2, &SPI_InitStructure);

kkarasti
Associate II
Posted on June 16, 2014 at 17:06

Turns out I had a hardware problem. Corrected that and boom, works!