STM32F407 discovery SPI Haberleşme Sorunu

Başlatan emiter, 11 Nisan 2020, 00:58:56

emiter

Merhaba
Elimde 2 adet  STM32F407 discovery kiti var ve bunları SPI ile fullduplex haberleştirmeye çalışıyorum. Birisi master ve birisi slave. Masterda Nss pinini Gpio output ile kontrol ediyorum. Slave de ise alternate function olarak tanımlı. Ne yaptıysam bir türlü karşılıklı verileri alıp veremedim doğru şekilde. Her türlü tavsiyeye açığım.  Irq handlerlar %95 aynı olduğundan sadece masterın Irq handler kodunu koydum ve her ikisininde main kodunu ekledim.

Not: Master ve slavede Spi'yı init ederken Spi_enable bitini set edersem hemen kesmeye gidiyor. Bu nedenle masterda 4 saniye gecikmeyle main içinde spi'ı enable ediyorum. Aynı anda slavede lowa çektiği için spi enable biti mainde set olup kesmeye gidiyor.

/**************************/

void Slave_SPI_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);

	SPI_InitTypeDef SPI_InitStruct;
	NVIC_InitTypeDef NVIC_InitStruct1;
	GPIO_InitTypeDef GPIO_InitStruct1;
	
	GPIO_InitStruct1.GPIO_Pin = SPI_NSS_PIN | SPI_MISO_PIN | SPI_CLOCK_PIN  |SPI_MOSI_PIN ;
	GPIO_InitStruct1.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStruct1.GPIO_Speed = GPIO_Speed_50MHz; //
	GPIO_InitStruct1.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStruct1.GPIO_PuPd = GPIO_PuPd_DOWN; //GPIO_PuPd_NOPULL  GPIO_PuPd_UP
	GPIO_Init(GPIOA, &GPIO_InitStruct1);

	GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_SPI1);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource5 ,GPIO_AF_SPI1);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource6 ,GPIO_AF_SPI1);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource7 ,GPIO_AF_SPI1);

	SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	SPI_InitStruct.SPI_Mode = SPI_Mode_Slave;
	SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
	SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
	SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
	SPI_InitStruct.SPI_NSS = SPI_NSS_Soft; 
	SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; 
	SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
	SPI_Init(SPI1, &SPI_InitStruct );

	SPI_NSSInternalSoftwareConfig(SPI1,SPI_NSSInternalSoft_Reset);  /*   SSI RESET  */

	SPI_I2S_ITConfig(SPI1,SPI_I2S_IT_TXE,ENABLE);
	SPI_I2S_ITConfig(SPI1,SPI_I2S_IT_RXNE,ENABLE);

	NVIC_InitStruct1.NVIC_IRQChannel = SPI1_IRQn;
	NVIC_InitStruct1.NVIC_IRQChannelPreemptionPriority = 0x00;
	NVIC_InitStruct1.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct1);
}

/*********************************/
int main(void) //SLAVE
{
	Nem_Ve_Sicaklik_Degeri[0]=0x0A;
	Nem_Ve_Sicaklik_Degeri[1]=0x09;
	Nem_Ve_Sicaklik_Degeri[2]=0x08;
	Nem_Ve_Sicaklik_Degeri[3]=0x07;
	Nem_Ve_Sicaklik_Degeri[4]=0x06;
	Donanim_Init();
	while (1)
	{
			if(  0==   (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4)) && data_transferi_basladi !=1 )
			{
					 SPI_Cmd(SPI1,ENABLE);
			}
        }
			
}

/*****************************************/
void Master_SPI_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);

	SPI_InitTypeDef SPI_InitStruct;
	NVIC_InitTypeDef NVIC_InitStruct1;
	GPIO_InitTypeDef GPIO_InitStruct1;
	GPIO_InitTypeDef GPIO_InitStruct2;
	
	GPIO_InitStruct1.GPIO_Pin = SPI_MISO_PIN |SPI_CLOCK_PIN  |SPI_MOSI_PIN ;
	GPIO_InitStruct1.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStruct1.GPIO_Speed = GPIO_Speed_50MHz; //
	GPIO_InitStruct1.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStruct1.GPIO_PuPd = GPIO_PuPd_DOWN; 
	GPIO_Init(GPIOA, &GPIO_InitStruct1);

	//GPIO_PinAFConfig(GPIOA, GPIO_PinSource4 ,GPIO_AF_SPI1);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource5 ,GPIO_AF_SPI1);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource6 ,GPIO_AF_SPI1);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource7 ,GPIO_AF_SPI1);


	GPIO_InitStruct2.GPIO_Pin=SPI_NSS_PIN;
	GPIO_InitStruct2.GPIO_Mode=GPIO_Mode_OUT;
	GPIO_InitStruct2.GPIO_OType=GPIO_OType_PP;
	GPIO_InitStruct2.GPIO_PuPd=GPIO_PuPd_NOPULL;
	GPIO_Init(GPIOA,&GPIO_InitStruct2);

	SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
	SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
	SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
	SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
	SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;  
	SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
	SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
	SPI_Init(SPI1, &SPI_InitStruct );

	SPI1 -> CR2 |= 0x00000004; //SSOE SET /

	SPI_I2S_ITConfig(SPI1,SPI_I2S_IT_TXE,ENABLE);
	SPI_I2S_ITConfig(SPI1,SPI_I2S_IT_RXNE,ENABLE);

	NVIC_InitStruct1.NVIC_IRQChannel = SPI1_IRQn;
	NVIC_InitStruct1.NVIC_IRQChannelPreemptionPriority = 0x00;
	NVIC_InitStruct1.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct1);

	SPI_NSS_OUT_ON;    //NSS PİNİ HIGH YAPILIYOR
}

/****************************************/
int main(void)  // MASTER 
{
	  Nem_Ve_Sicaklik_Degeri[0]=0x02;
	  Nem_Ve_Sicaklik_Degeri[1]=0x03;
	  Nem_Ve_Sicaklik_Degeri[2]=0x04;
	  Nem_Ve_Sicaklik_Degeri[3]=0x05;
	  Nem_Ve_Sicaklik_Degeri[4]=0x06;
  Donanim_Init();
  while (1)
  {
     if(data_transferi_basladi != 1)
	  {
		  SPI_NSS_OUT_OFF; // NSS pini LOW'a çekiliyor.
		 SPI_Cmd(SPI1,ENABLE);
          }
 }

}

/*********************************************/
void SPI1_IRQHandler(void)
{
	if (data_transferi_basladi ==0 )
	{
		SPI_DATA_REGISTER = Nem_Ve_Sicaklik_Degeri[dizi_sirasi];
		dizi_sirasi++;
		data_transferi_basladi=1;
	}
	if(dizi_sirasi_receiver < 4)
	{
		while( (SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)) == 0)
		{}
		SPI_DATA_REGISTER = Nem_Ve_Sicaklik_Degeri[dizi_sirasi];
		dizi_sirasi++;
		while( (SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)) == 0 )
		{}
		veri_kaydedici[dizi_sirasi_receiver]=SPI_DATA_REGISTER;
		dizi_sirasi_receiver++;
	}
	else if(dizi_sirasi_receiver  == 4)
	{
		while((SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)) == 0)
		{}
		veri_kaydedici[dizi_sirasi_receiver]=SPI_DATA_REGISTER;
		dizi_sirasi_receiver++;
	}
	else if (dizi_sirasi_receiver == 5)
	{
		while((SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)) == 0)
		{}
		while((SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_BSY)) == 1 )
		{}
		GPIOA->ODR |= (GPIO_Pin_4 );  // NSS PİNİ LOW'a çekiliyor.
		SPI_Cmd(SPI1,DISABLE);
		dizi_sirasi_receiver=0;
		dizi_sirasi=0;
	}
}