4
votes

I am currently working on the STM32L476RG Nucleo board and I am trying to communicate with the SPI2 bus.

It seems that I am sending data with the MOSI pin but I don't have anything on the SCK pin.

Here are my initialisation code and sending data code:

In the main.c:

/Function that initializes the SPI/

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_LOW;
  hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
  hspi2.Init.NSS = SPI_NSS_SOFT;
  hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi2.Init.TIMode = SPI_TIMODE_DISABLED;
  hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
  hspi2.Init.CRCPolynomial = 7;
  hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  hspi2.Init.NSSPMode = SPI_NSS_PULSE_DISABLED;
  HAL_SPI_Init(&hspi2);
}

/Function that sends data via SPI to the slave/

void ADAS1000_SetRegisterValue(unsigned char regAddress,
                               unsigned long regVal)
{

    unsigned char writeCmd[4] = {0, 0, 0, 0};

    writeCmd[0] = 0x80 + regAddress;    // Write bit and register address.
    writeCmd[1] = (unsigned char)((regVal & 0xFF0000) >> 16);
    writeCmd[2] = (unsigned char)((regVal & 0x00FF00) >> 8);
    writeCmd[3] = (unsigned char)((regVal & 0x0000FF) >> 0);

    HAL_SPI_Transmit(&hspi2, &(writeCmd[0]), (uint16_t) sizeof(writeCmd[0]), 50);
    HAL_Delay(500);
    HAL_SPI_Transmit(&hspi2, &(writeCmd[1]), (uint16_t) sizeof(writeCmd[1]), 50);
    HAL_Delay(500);
    HAL_SPI_Transmit(&hspi2, &(writeCmd[2]), (uint16_t) sizeof(writeCmd[2]), 50);
    HAL_Delay(500);
    HAL_SPI_Transmit(&hspi2, &(writeCmd[3]), (uint16_t) sizeof(writeCmd[3]), 50);
    HAL_Delay(500);
}

In the hal_msp.c :

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{

  GPIO_InitTypeDef GPIO_InitStruct;
  if(hspi->Instance==SPI2)
  {
    /* Peripheral clock enable */
    __SPI2_CLK_ENABLE();

    /**SPI2 GPIO Configuration
    PC2     ------> SPI2_MISO
    PC3     ------> SPI2_MOSI
    PB10     ------> SPI2_SCK
    PB12     ------> SPI2_NSS
    */
    GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_12;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  }

If you have any ideas or advice, thanks for helping!

EDIT

void MX_GPIO_Init(void) 
{ 
   /* GPIO Ports Clock Enable */ 
   __GPIOC_CLK_ENABLE(); 
   __GPIOA_CLK_ENABLE(); 
   __GPIOB_CLK_ENABLE(); 
} 
1
Did you enable the clock to SPI2 before you try to configure it? You say you enabled the clock to the GPIOs, but did you do so before you tried to configure them? You might also consider temporarily setting the pin in question as an ordinary GPIO output and slowly togging it in software, to verify that you have correctly located it for probing, that it is not shorted to something or damaged, etc. Where are you picking up the signal? Your board brings out PB13 as the labeled SPI2 SCK, but it does bring out PB10 as Arduino-style D6 (either pin can be configured for this function) - Chris Stratton
Hi Chris! Thanks for helping! I did enable SPI2 and GPIOs clock before configuring them and I already checked that my SCK pin is working as an ordinary GPIO I just tried to use PB10 as the SPI2 SCK, but it still does not work... I am really confused, I don't know what to do :/ - Tetris
I've had problems with STM32 SPI implementations using the NSS pin as Alt function. I've always had to set the pin as out and manually assert the pin low before doing any SPI operations. This was the case with STM32F1, F2, and F4. Not sure if this is the case for the L series. - rost0031
Is there an external pull up resistor on the CLK line? - Koorosh Hajiani
Are you testing unconnected? What are you communicating with? could it be that the other device is also configured in master mode and thus trying to drive the clock line at the same time? - tony_felloni

1 Answers

0
votes

This might be the problem:

// [...]
hspi2.Init.NSS = SPI_NSS_SOFT;
// [...]

Without checking the definition of your HAL macros, I believe that this configures the SPI not to drive NSS - but to have this done by software. This means that you are responsible to assert the NSS pin manually before putting data into the SPI peripheral.

This in turn may cause the SPI slave side not to respond, which may appear like the SCK signals weren't reaching the slave at all.