AnsweredAssumed Answered

Why is my SPI-slave unable to generate any interrupts?

Question asked by arnold_w on Feb 5, 2016
Latest reply on Feb 5, 2016 by wolff.roger
I am working with the ST-Link Discovery development board and I'm trying to get the SPI-bus working properly. I have configured SPI1 as my master and SPI2 as my slave and I connected wires in between them. I have verfied with a logic analyzer that my master is working great. However, my slave seems hopeless, no matter what I do I don't get any interrupts. Can someone please help me?

void initializeSpi2controlPins(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
 
    /***************************************************
    *****************  SPI MISO  ***********************
    ***************************************************/
    GPIO_InitStructure.Pin       = GPIO_PIN_2;
    GPIO_InitStructure.Mode      = GPIO_MODE_AF_PP;
    GPIO_InitStructure.Speed     = GPIO_SPEED_FAST;
    GPIO_InitStructure.Pull      = GPIO_NOPULL;
    GPIO_InitStructure.Alternate = GPIO_AF5_SPI2;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
 
    /***************************************************
    *****************  SPI MOSI  ***********************
    ***************************************************/
    GPIO_InitStructure.Pin       = GPIO_PIN_3;
    GPIO_InitStructure.Mode      = GPIO_MODE_AF_OD;
    GPIO_InitStructure.Speed     = GPIO_SPEED_FAST;
    GPIO_InitStructure.Pull      = GPIO_NOPULL;
    GPIO_InitStructure.Alternate = GPIO_AF5_SPI2;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
 
    /***************************************************
    *****************  SPI CLK  ************************
    ***************************************************/
    GPIO_InitStructure.Pin       = GPIO_PIN_10;
    GPIO_InitStructure.Mode      = GPIO_MODE_AF_OD;
    GPIO_InitStructure.Speed     = GPIO_SPEED_FAST;
    GPIO_InitStructure.Pull      = GPIO_NOPULL;
    GPIO_InitStructure.Alternate = GPIO_AF5_SPI2;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
 
    /***************************************************
    *****************  SPI NSS  ************************
    ***************************************************/
    GPIO_InitStructure.Pin       = GPIO_PIN_9;
    GPIO_InitStructure.Mode      = GPIO_MODE_AF_OD;
    GPIO_InitStructure.Speed     = GPIO_SPEED_FAST;
    GPIO_InitStructure.Pull      = GPIO_NOPULL;
    GPIO_InitStructure.Alternate = GPIO_AF5_SPI2;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
}
 
 
void enableSpi2module(void)
{
    SPI_HandleTypeDef hspi;
    hspi.Instance               = SPI2;
    hspi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
    hspi.Init.Direction         = SPI_DIRECTION_2LINES;
    hspi.Init.Mode              = SPI_MODE_SLAVE;
    hspi.Init.DataSize          = SPI_DATASIZE_16BIT;
    hspi.Init.NSS               = SPI_NSS_HARD_INPUT;
    hspi.Init.FirstBit          = SPI_FIRSTBIT_MSB;
    hspi.Init.CLKPhase          = SPI_PHASE_2EDGE;
    hspi.Init.CLKPolarity       = SPI_POLARITY_HIGH;
    hspi.Init.CRCCalculation    = SPI_CRCCALCULATION_DISABLE;
    hspi.Init.TIMode            = SPI_TIMODE_DISABLE;
 
    RCC->AHB3ENR |= RCC_APB1ENR_SPI2EN;           // Enable clock for SPI 2
 
    if (HAL_SPI_Init(&hspi) != HAL_OK)
    {
        INV_UART4_TransmitNullTerminatedString((uint8_t*)"\r\nSPI2. Initialization Error");
        INV_UART4_WaitForTransmitToFinish();
    }
     __HAL_SPI_ENABLE(&hspi);
 
    __HAL_SPI_ENABLE_IT(&hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
    HAL_NVIC_EnableIRQ(SPI2_IRQn);                // Enable SPI bus interrupts
    SPI2->CR1 |= SPI_CR1_SPE;
    SPI2->CR2 |= SPI_CR2_RXNEIE;
    SPI2->CR2 |= SPI_CR2_ERRIE;
    SPI2->CR2 |= SPI_CR2_TXEIE;
}
 
 
void SPI2_IRQHandler(void)
{
    INV_UART4_TransmitNullTerminatedString((uint8_t*)"\r\nSPI2_IRQHandler.");
    INV_UART4_WaitForTransmitToFinish();
}

Outcomes