cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G030K8 SPI1 issue : initialise and reach IRQ but always read and send 0

fgroleau
Associate II

Hi,

I'm trying to set my setup my SPI1 as slave full duplex on my STM32G030K8 and i reach the HAL_SPI_TxRxCpltCallback when receiving message, but I always read value 0 in my rxbuffer when my master is spamming 16 @ 1MHz at a 1 sec intervall. Also, my slave should be answering 196 base on the value place in the txbuff, but nothing seem to get out;

 

void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef * hspi)
{
	__disable_irq();

	switch(spi_s.state)
	{
		case REG:
			spi_s.reg = spi_s.rx_buff[1];
			if(spi_s.rx_buff[0] & WRITE_MASK)
			{
				spi_s.state = VALUE;
			}
			else
			{
				manage_reg_read(spi_s.reg);
			}
			break;

		case VALUE:
			manage_reg_write(spi_s.reg);
			break;
	}
	if(HAL_SPI_TransmitReceive_IT(&hspi1, (uint8_t*)spi_s.tx_buff, (uint8_t*)spi_s.rx_buff, SPI_BUFF_SIZE))
	{
		Error_Handler();
	}
	__enable_irq();
}
void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
{
	__disable_irq();

	spi_s.reg = 0;
	spi_s.state = REG;
	__HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
	hspi1.Instance->CR1 |= (1<<SPI_CR1_SSI_Pos);
	//HAL_SPI_TransmitReceive_IT(&hspi1, (uint8_t*)spi_s.tx_buff,  (uint8_t*)spi_s.rx_buff, sizeof(uint16_t));
	__enable_irq();
}

void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
{
	__disable_irq();
	switch(GPIO_Pin)
	{
		case CS_Pin:
			HAL_SPIEx_FlushRxFifo(&hspi1);
			hspi1.Instance->CR1 &=(~SPI_CR1_SSI_Msk);
			break;
		case PIR_Pin:
		case Button_Pin:
			init_clock();
			init_GPIO();
			while( HAL_GPIO_ReadPin(Button_GPIO_Port, Button_Pin));
			break;
	}
	__HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
	__enable_irq();
}

void init_SPI()
{
	 GPIO_InitTypeDef GPIO_InitStruct = {0};

	 for(uint8_t i = 0; i < SPI_BUFF_SIZE;i++)
	 {
		 spi_s.rx_buff[i] = 0;
		 spi_s.tx_buff[i] = 0;

	 }
	 spi_s.reg = 0;
	 spi_s.state = REG;

	 __HAL_RCC_GPIOB_CLK_ENABLE();
	 __HAL_RCC_GPIOA_CLK_ENABLE();

	 GPIOA->MODER |= (0x02<<GPIO_MODER_MODE2_Pos)|((0x02<<GPIO_MODER_MODE5_Pos))|((0x02<<GPIO_MODER_MODE6_Pos));
	 GPIOA->OSPEEDR |= (11<<GPIO_OSPEEDR_OSPEED2_Pos)|(11<<GPIO_OSPEEDR_OSPEED5_Pos)|(11<<GPIO_OSPEEDR_OSPEED6_Pos);
	 GPIOA->AFR[0] &= (~GPIO_AFRL_AFSEL2_Msk)&(~GPIO_AFRL_AFSEL5_Msk)&(~GPIO_AFRL_AFSEL6_Msk);

	GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
	GPIO_InitStruct.Pin =	SCK_Pin;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
	HAL_GPIO_Init(SCK_GPIO_Port, &GPIO_InitStruct);

	 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	 GPIO_InitStruct.Pin = INT_Pin;
	 GPIO_InitStruct.Pull = GPIO_NOPULL;
	 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	 HAL_GPIO_Init(INT_GPIO_Port, &GPIO_InitStruct);


	  /*Configure GPIO pin : CS_Pin */
	  GPIO_InitStruct.Pin = CS_Pin;
	  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
	  GPIO_InitStruct.Pull = GPIO_NOPULL;
	  HAL_GPIO_Init(CS_GPIO_Port, &GPIO_InitStruct);


	__HAL_RCC_SPI1_CLK_ENABLE();
	hspi1.Instance = SPI1;
	hspi1.Init.Mode = SPI_MODE_SLAVE;
	hspi1.Init.Direction = SPI_DIRECTION_2LINES;
	hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
	hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
	hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
	hspi1.Init.NSS = SPI_NSS_SOFT;
	hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
	hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
	hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
	hspi1.Init.CRCPolynomial = 7;
	hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
	hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
	if (HAL_SPI_Init(&hspi1) != HAL_OK)
	{
		Error_Handler();
	}

// cs int
  HAL_NVIC_SetPriority(EXTI2_3_IRQn, IRQ_SPI_PRIORITY, 0);
  HAL_NVIC_EnableIRQ(EXTI2_3_IRQn);

  HAL_NVIC_SetPriority(SPI1_IRQn, IRQ_SPI_PRIORITY, 0);
  HAL_NVIC_EnableIRQ(SPI1_IRQn);

	if(HAL_SPI_TransmitReceive_IT(&hspi1, (uint8_t*)spi_s.tx_buff,  (uint8_t*)spi_s.rx_buff, SPI_BUFF_SIZE))
	{
		Error_Handler();
	}
}

 

Blue: SCK Yellow: Mosi

451096102_2393086501034679_7389975685350709849_n.jpg

Yellow: MISO     Blue:SCK

451695031_2879324295574843_8713842410652025399_n.jpg

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
fgroleau
Associate II

Sorry for the delay it seem like it  was a hardware problem, i change the chip and now it work

View solution in original post

2 REPLIES 2

Incorrect MISO/MOSI setup in GPIO?

Read out and check/post content of relevant GPIO registers.

JW

fgroleau
Associate II

Sorry for the delay it seem like it  was a hardware problem, i change the chip and now it work