cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F302R8 SPI problem

Emre Kiremitci
Associate II

Hello i am using 2 STM32F302R8 nucleo boards as 1 slave and 1 master.

Master sends values to slave but slave is not receiving values and not going to interrupt.

Master Code:

uint8_t x;
uint16_t d;
void rx(void){
	GPIOB->ODR&=~(1<<9);// reset PB9
	SPI2->CR1|=(1<<6);//start spi2
	while(!(SPI2->SR&(1<<1))){}//wait for tx buffer empty
	SPI2->DR=x;
	d=SPI2->SR;
	while(!(SPI2->SR&(1<<1))){}//wait for tx buffer enmpty
	GPIOB->ODR|=(1<<9);//set PB9
	SPI2->CR1&=~(1<<6);//stop spi2
	x++;
}
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  /* USER CODE BEGIN 2 */
  RCC->AHBENR|=(1<<18);//GPIOB clock
  RCC->APB1ENR|=(1<<14);//SPI2 clock
  GPIOB->MODER|=(2<<30)|(2<<28)|(2<<26)|(1<<18);//PB15 PB14 PB13 alternate function PB9 output
  GPIOB->OSPEEDR|=(3<<30)|(3<<28)|(3<<26);//PB15 PB14 PB13 high speed
  GPIOB->ODR|=(1<<9);//PB9 set
  GPIOB->AFR[1]|=(5<<28)|(5<<24)|(5<<20);//AF5 PB15 PB14 PB13 PB12
  //SPI2 CR1
  SPI2->CR1|=(1<<5);//Fpclk/32  36/32
  SPI2->CR1&=~((1<<1)|(1<<0));//cpol cpha 0 0
  SPI2->CR1&=~((1<<15)|(1<<10));// 2 undirectional full duplex
  SPI2->CR1&=~(1<<7);// msb first
  SPI2->CR1|=(1<<9)|(1<<8);//master ssm=1 ssi=1 prevent modf error
  SPI2->CR1|=(1<<2);//master mode
  //SPI2 CR2
  SPI2->CR2|=(1<<8)|(1<<9)|(1<<10);//8 bit data
  SPI2->CR2|=(1<<2);//not working multimaster
  SPI2->CR2&=(1<<4);//motorola spi2
  SPI2->CR2|=(1<<12);//RXNE event when fifo 8 bit or greater
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  rx();
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

Slave Code:

uint8_t rxdata,x,z;
uint16_t d;
void SPI2_IRQHandler(void){
	if(SPI2->SR&(1<<0)){
		x++;
		rxdata=SPI2->DR;
	}
}
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  /* USER CODE BEGIN 2 */
  RCC->AHBENR|=(1<<18);//GPIOB clock
  RCC->APB1ENR|=(1<<14);//SPI2 clock
  GPIOB->MODER|=(2<<30)|(2<<28)|(2<<26)|(2<<24);//PB15 PB14 PB13 PB12 alternate function
  GPIOB->OSPEEDR|=(3<<30)|(3<<28)|(3<<26)|(3<<24);//PB15 PB14 PB13 PB12 high speed
  GPIOB->AFR[1]|=(5<<28)|(5<<24)|(5<<20)|(5<<16);//AF5 PB15 PB14 PB13 PB12
  NVIC_EnableIRQ(SPI2_IRQn);
  //SPI2 CR1
  SPI2->CR1&=~((1<<1)|(1<<0));//cpol cpha 0 0
  SPI2->CR1&=~((1<<15)|(1<<10));// 2 undirexctional full duplex
  SPI2->CR1&=~(1<<7);// msb first
  SPI2->CR1&=~(1<<2);//slave
  //SPI2 CR2
  SPI2->CR2|=(1<<8)|(1<<9)|(1<<10);//8 bit data
  SPI2->CR2&=(1<<4);//motorola spi2
  SPI2->CR2|=(1<<12);//RXNE event when fifo 8 bit or greater
  SPI2->CR2|=(1<<6);//RXNEIE
  //ENABLE
  SPI2->CR1|=(1<<6);
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  d=SPI2->SR;
	  z++;
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

2 REPLIES 2

> Master sends values to slave

How do you know?

Did you observe the signals?

JW

Yes i used oscilloscope and i saw value