AnsweredAssumed Answered

STM32L151 MISO AF config issue

Question asked by Alex_rcpilot on Feb 26, 2015
Latest reply on Mar 18, 2015 by STM32Cube-T
Hi all, I'm using L1 series for the very first time. 

The code is generated with CubeMX 4.6.0 and built with MDK 5.12.0. I've had Cube MX copy the device library into the project directory. 

My board carries an STM32L151C8T6, SPI2 has been configured with following code:

/* SPI2 init function */
void MX_SPI2_Init(void)
  hspi2.Instance = SPI2;
  hspi2.Init.Mode = SPI_MODE_MASTER;
  hspi2.Init.Direction = SPI_DIRECTION_2LINES;
  hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi2.Init.CLKPolarity = SPI_POLARITY_HIGH;
  hspi2.Init.CLKPhase = SPI_PHASE_2EDGE;
  hspi2.Init.NSS = SPI_NSS_SOFT;
  hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
  hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi2.Init.TIMode = SPI_TIMODE_DISABLED;

An ADXL345 has been connected to SPI2 and when address 0 is read, it returns device ID 0xE5. When I monitor MISO with my oscilloscope, it shows exactly the same value. MOSI also checks out, the address byte was sent correct. 

But my code keeps reading 0x00, I checked every step within the library source code and confirmed that DR didn't contain the correct value when being read. 

So I tweaked registers to find a combination which might offer some clue. I noticed that PB14(MISO) has been put into floating input mode, while SCLK and MOSI are in AFPP mode. 

Could't quite recall how exactly I set up MISO with F1 and F2 series. But since AFPP supposedly is only meant for outputs, I guess it makes sense.

Then I traced the code which initializes SPI2 pins

Version info says:
  * File Name          : stm32l1xx_hal_msp.c
  * Date               : 17/02/2015 00:41:04
  * Description        : This file provides code for the MSP Initialization 
  *                      and de-Initialization codes. 

Starting at Line 297:
    /**SPI2 GPIO Configuration    
    PB13     ------> SPI2_SCK
    PB14     ------> SPI2_MISO
    PB15     ------> SPI2_MOSI 
    GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_15;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_VERY_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_14;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

Guess I'm not supposed to modify that. 

Then at some point I changed that floating input mode into AFPP mode and all of a sudden I could read the correct value. 

So, does the library hold a bug or is it more to PB14 than just changing its mode? Thank you for your time.