Hi, I’ve been programming my Photon to act as a Slave, everything works fine when I poll the data in a loop as follows:
if (SPI_I2S_GetITStatus((SPI_TypeDef *) SPI1_BASE, SPI_I2S_IT_RXNE) == SET)
{
RGB.color(0, 0, 255);
uint16_t byte = SPI_I2S_ReceiveData((SPI_TypeDef *) SPI1_BASE);
Serial.println(byte);
if(byte == (uint16_t) 0x15){
RGB.color(0, 255, 0);
}
}
But once I setup the NVIC for the SPI interrupt and receive data on the photon it starts flashing red in an odd patern before it resets itself. Does anyone know what might be the cause?
void HAL_SPI_Begin(HAL_SPI_Interface spi, uint16_t pin)
{
STM32_Pin_Info* PIN_MAP = HAL_Pin_Map();
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable SPI Clock */
*spiMap[spi]->SPI_RCC_APBRegister |= spiMap[spi]->SPI_RCC_APBClockEnable;
/* Connect SPI pins to AF */
GPIO_PinAFConfig(PIN_MAP[spiMap[spi]->SPI_SCK_Pin].gpio_peripheral, PIN_MAP[spiMap[spi]->SPI_SCK_Pin].gpio_pin_source, spiMap[spi]->SPI_AF_Mapping);
GPIO_PinAFConfig(PIN_MAP[spiMap[spi]->SPI_MISO_Pin].gpio_peripheral, PIN_MAP[spiMap[spi]->SPI_MISO_Pin].gpio_pin_source, spiMap[spi]->SPI_AF_Mapping);
GPIO_PinAFConfig(PIN_MAP[spiMap[spi]->SPI_MOSI_Pin].gpio_peripheral, PIN_MAP[spiMap[spi]->SPI_MOSI_Pin].gpio_pin_source, spiMap[spi]->SPI_AF_Mapping);
HAL_Pin_Mode(spiMap[spi]->SPI_SCK_Pin, AF_OUTPUT_PUSHPULL);
HAL_Pin_Mode(spiMap[spi]->SPI_MISO_Pin, AF_OUTPUT_PUSHPULL);
HAL_Pin_Mode(spiMap[spi]->SPI_MOSI_Pin, AF_OUTPUT_PUSHPULL);
// HAL_Pin_Mode(pin, OUTPUT);
//HAL_GPIO_Write(pin, Bit_SET);//HIGH
/* SPI configuration */
spiMap[spi]->SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
spiMap[spi]->SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
spiMap[spi]->SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
// if(spiMap[spi]->SPI_Data_Mode_Set != true)
//{
//Default: SPI_MODE3
spiMap[spi]->SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
spiMap[spi]->SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
//}
spiMap[spi]->SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
if(spiMap[spi]->SPI_Clock_Divider_Set != true)
{
/* Defaults to 15Mbit/s on SPI1, SPI2 and SPI3 */
if(spi == HAL_SPI_INTERFACE1)
{
spiMap[spi]->SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;//60/4=15
}
else
{
spiMap[spi]->SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;//30/2=15
}
}
// if(spiMap[spi]->SPI_Bit_Order_Set != true)
// {
//Default: MSBFIRST
spiMap[spi]->SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
//}
spiMap[spi]->SPI_InitStructure.SPI_CRCPolynomial = 7;
/* Configure the SPI interrupt priority */
NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 14;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
SPI_I2S_DeInit(SPI1);
spiMap[spi]->SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
SPI_Init(spiMap[spi]->SPI_Peripheral, &spiMap[spi]->SPI_InitStructure);
SPI_I2S_ITConfig(spiMap[spi]->SPI_Peripheral, SPI_I2S_IT_RXNE, ENABLE);
SPI_Cmd(spiMap[spi]->SPI_Peripheral, ENABLE);
spiMap[spi]->SPI_Enabled = true;
}
Works fine without this part of the code:
/* Configure the SPI interrupt priority */
NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 14;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
the SPI1_IRQHandler(void) function is empty.
EDIT: The same exact thing happens when I try to use SPI3 with interrupts
EDIT2: After some debugging I have found that the Photon ends up in a Hardfault handler. Any thoughts?
Regards