cancel
Showing results for 
Search instead for 
Did you mean: 

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

arnold_w
Senior II
Posted on February 05, 2016 at 11:16

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();
}

4 REPLIES 4
arnold_w
Senior II
Posted on February 05, 2016 at 12:09

Maybe I should add that I do make sure to enable the clocks to the pins, I do that in a different file:

void RCC_Configuration(void)
{
RCC->APB1ENR |= RCC_APB1ENR_UART4EN; // Enable clock for UART 4
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; // Enable clock for GPIO port A
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; // Enable clock for GPIO port B
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; // Enable clock for GPIO port C
RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN; // Enable clock for GPIO port D
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOEEN; // Enable clock for GPIO port E
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOFEN; // Enable clock for GPIO port F
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOGEN; // Enable clock for GPIO port G
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOHEN; // Enable clock for GPIO port H
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOIEN; // Enable clock for GPIO port I
}

arnold_w
Senior II
Posted on February 05, 2016 at 12:38

I tried to dump all SPI2-registers in the end of the enableSpi2module function and all I read are zeros(!):

SPI2->CR1: 0x0

SPI2->CR2: 0x0

SPI2->CRCPR: 0x0

SPI2->DR: 0x0

SPI2->I2SCFGR: 0x0

SPI2->I2SPR: 0x0

SPI2->RXCRCR: 0x0

SPI2->SR: 0x0

SPI2->TXCRCR: 0x0

Anybody got a theory on what's going on here?
arnold_w
Senior II
Posted on February 05, 2016 at 12:59

I found the problem. I was turning on the wrong clock:

RCC->AHB3ENR |= RCC_APB1ENR_SPI2EN; // Enable clock for SPI 2

should have been

RCC->APB1ENR |= RCC_APB1ENR_SPI2EN; // Enable clock for SPI 2

re.wolff9
Senior
Posted on February 05, 2016 at 17:41

Thats why you should keep an eye on the numbers in the define and the register. 

Your symptoms are typical for ''clock not enabled''. So for the future, it may help you to know that if all (or many) registers read as zero, you might have forgotten to enable the clock.