AnsweredAssumed Answered

SPI Master and Slave Configuration Problem

Question asked by prabhakar.sandesh on Dec 12, 2013
Latest reply on Dec 13, 2013 by prabhakar.sandesh
Im configuring SPI1 as master and slave in two same boards (STM32F103RB) for peripheral testing.
And i configured board-1 as master which is transmitting continuously,
board-2 as slave which only receives data,. Interrupt is generating properly but im not able to receive the correct data, anybody help me with sample code,
 here the code...


/*----------------------------------------*/
/*------master code(board-1)-------------*/
/*----------------------------------------*/
/*----------Includes---------*/
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_nvic.h"
#include "stm32f10x_systick.h"
#include "stm32f10x_spi.h"
#include "stm32f10x_tim.h"

#define SPI_COMMS           SPI1
#define PORT_SPI_COMMS      GPIOA
#define PIN_SPI_COMMS_NCS   GPIO_Pin_4
#define PIN_SPI_COMMS_SCK   GPIO_Pin_5
#define PIN_SPI_COMMS_MISO  GPIO_Pin_6
#define PIN_SPI_COMMS_MOSI  GPIO_Pin_7

#define __NOP                             __nop

void sar_RCC_Configuration(void);
void sar_SPI1_Configuration(void);
void GPIO_Configuration(void);
void SPI1_send(void);
void NVIC_Configuration(void);

int main()
{
     sar_RCC_Configuration();
     GPIO_Configuration();
     sar_SPI1_Configuration();
     NVIC_Configuration();
     while(1);
}

/*-----------RCC_CLOCK_CONFIGURATION--------------------------*/
void sar_RCC_Configuration(void)
{
     RCC_DeInit();
     RCC_ClearFlag();
     /*enable HSE clock[RCC->CR-----WRITE BIT NO's 16 TO 1]*/
     RCC_HSEConfig(RCC_HSE_ON);
     if ((RCC->CR & RCC_HSE_ON))
     {
          /*Wait for HSERDY = 1 
          (External high speed clock is ready to use)[BIT NO 17 in RCC->CR register]*/
          while ((RCC->CR & RCC_FLAG_HSERDY)==0 ); 
          /*used to flash code to hardware,system operating speed is 72MHz,
          prefetch buffer is enabled to flash the Firm Ware and
          so Flash Latency is selected to two wait states->if 48 MHz < SYSCLK „T 72 MHz */
          FLASH->ACR =  0x12 ;
          /*We used 8MHz external crystal and operating system frequency in 72MHz
           by multiplying 8MHz 9 times to make it 72MHz[RCC->CFGR-----WRITE BIT NO's 21,20,19,18 TO 0,1,1,1]*/
          RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);
          /*PLL clock 1 is selected for APB1 bus because its operating speed is 36MHz hence 
          High speed clock(72MHz) is divided by 2 to get 36MHz[RCC->CFGR-----WRITE BIT NO's 10,9,8 TO 1,0,0]*/
          RCC_PCLK1Config(RCC_HCLK_Div2);
          /*PLL clock 2 is selected for APB2 bus because its operating speed is 72MHz hence 
          High speed clock(72MHz) is not divided[RCC->CFGR-----WRITE BIT NO's 13,12,11 TO 0,0,0]*/
          RCC_PCLK2Config(RCC_HCLK_Div1);
          /*ADC clock is selected for ADC bus because its maximum operating speed is 12MHz hence 
          PLL clock(72MHz) is divided by 6 to get 12MHz[RCC->CFGR-----WRITE BIT NO's 15,14 TO 1,0]*/
          RCC_ADCCLKConfig(RCC_PCLK2_Div6);
          /*if above configuration is done then select SYSTEM clock ,maximum operating speed of system is 72MHz
          ,so select the PLL clock which gives 72MHz[RCC->CFGR-----WRITE BIT NO's 3,2 TO 1,0] */
          RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
          /*enable PLL clock[RCC->CR-----WRITE BIT NO's 24 TO 1]*/
          RCC_PLLCmd(ENABLE);
          while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);
          while(RCC_WaitForHSEStartUp()==ERROR);
     }                                      
}


void sar_SPI1_Configuration(void)
{
     SPI_InitTypeDef SPI_InitStructure;
     
     // Enable SPI1 clock //
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
     /* SPI_MASTER configuration -------------------------------------------------*/
     SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
     SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
     SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
     SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
     SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
     SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
     SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
     SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
     SPI_InitStructure.SPI_CRCPolynomial = 7;
     SPI_Init(SPI_COMMS, &SPI_InitStructure);
     
     /* Enable SPI_MASTER TXE interrupt */
     SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, ENABLE);
     
     /* Enable SPI_MASTER */
     SPI_Cmd(SPI1, ENABLE);
}


void GPIO_Configuration(void)
{
     GPIO_InitTypeDef GPIO_InitStructure;
     
     // Enable GPIO clock //
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
     // Enable SPI1 Pins Software Remapping //
     //GPIO_PinRemapConfig(GPIO_Remap_SPI1, ENABLE);
     
     // Configure SPI_MASTER pins: SCK, MISO and MOSI //
     
     GPIO_InitStructure.GPIO_Pin = PIN_SPI_COMMS_SCK | PIN_SPI_COMMS_MISO | PIN_SPI_COMMS_MOSI;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
     GPIO_Init(PORT_SPI_COMMS, &GPIO_InitStructure);
     
     GPIO_Init(PORT_SPI_COMMS, &GPIO_InitStructure);
}


void NVIC_Configuration(void)
{
     NVIC_InitTypeDef NVIC_InitStruct;
     
     NVIC_InitStruct.NVIC_IRQChannel=SPI1_IRQChannel;
     NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
     NVIC_Init(&NVIC_InitStruct);
}


void SPI1_IRQHandler(void)
{
     while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET); 
          SPI_I2S_SendData(SPI1,0x01);
     while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_BSY)==SET);
}




/*----------------------------------------*/
/*------slave code(board-1)-------------*/
/*----------------------------------------*/
/*----------Includes--------------*/
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_nvic.h"
#include "stm32f10x_systick.h"
#include "stm32f10x_spi.h"
#include "stm32f10x_tim.h"

#define SPI_COMMS           SPI1
#define PORT_SPI_COMMS      GPIOA
#define PIN_SPI_COMMS_NCS   GPIO_Pin_4
#define PIN_SPI_COMMS_SCK   GPIO_Pin_5
#define PIN_SPI_COMMS_MISO  GPIO_Pin_6
#define PIN_SPI_COMMS_MOSI  GPIO_Pin_7

#define __NOP                             __nop

void sar_RCC_Configuration(void);
void sar_SPI1_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);

int main()
{
     sar_RCC_Configuration();
     GPIO_Configuration();
     sar_SPI1_Configuration();
     NVIC_Configuration();
     while(1);
}
/*-----------RCC_CLOCK_CONFIGURATION--------------------------*/
void sar_RCC_Configuration(void)
{
     RCC_DeInit();
     RCC_ClearFlag();
     /*enable HSE clock[RCC->CR-----WRITE BIT NO's 16 TO 1]*/
     RCC_HSEConfig(RCC_HSE_ON);
     if ((RCC->CR & RCC_HSE_ON))
     {
          /*Wait for HSERDY = 1 
          (External high speed clock is ready to use)[BIT NO 17 in RCC->CR register]*/
          while ((RCC->CR & RCC_FLAG_HSERDY)==0 ); 
          /*used to flash code to hardware,system operating speed is 72MHz,
          prefetch buffer is enabled to flash the Firm Ware and
          so Flash Latency is selected to two wait states->if 48 MHz < SYSCLK „T 72 MHz */
          FLASH->ACR =  0x12 ;
          /*We used 8MHz external crystal and operating system frequency in 72MHz
           by multiplying 8MHz 9 times to make it 72MHz[RCC->CFGR-----WRITE BIT NO's 21,20,19,18 TO 0,1,1,1]*/
          RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);
          /*PLL clock 1 is selected for APB1 bus because its operating speed is 36MHz hence 
          High speed clock(72MHz) is divided by 2 to get 36MHz[RCC->CFGR-----WRITE BIT NO's 10,9,8 TO 1,0,0]*/
          RCC_PCLK1Config(RCC_HCLK_Div2);
          /*PLL clock 2 is selected for APB2 bus because its operating speed is 72MHz hence 
          High speed clock(72MHz) is not divided[RCC->CFGR-----WRITE BIT NO's 13,12,11 TO 0,0,0]*/
          RCC_PCLK2Config(RCC_HCLK_Div1);
          /*ADC clock is selected for ADC bus because its maximum operating speed is 12MHz hence 
          PLL clock(72MHz) is divided by 6 to get 12MHz[RCC->CFGR-----WRITE BIT NO's 15,14 TO 1,0]*/
          RCC_ADCCLKConfig(RCC_PCLK2_Div6);
          /*if above configuration is done then select SYSTEM clock ,maximum operating speed of system is 72MHz
          ,so select the PLL clock which gives 72MHz[RCC->CFGR-----WRITE BIT NO's 3,2 TO 1,0] */
          RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
          /*enable PLL clock[RCC->CR-----WRITE BIT NO's 24 TO 1]*/
          RCC_PLLCmd(ENABLE);
          while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);
          while(RCC_WaitForHSEStartUp()==ERROR);
     }                                      
}


void sar_SPI1_Configuration(void)
{
     SPI_InitTypeDef SPI_InitStructure;
     
     // Enable SPI1 clock //
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
     /* SPI_MASTER configuration -------------------------------------------------*/
     SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
     SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
     SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
     SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
     SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
     SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
     SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
     SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
     SPI_InitStructure.SPI_CRCPolynomial = 7;
     SPI_Init(SPI_COMMS, &SPI_InitStructure);
     
     /* Enable SPI_MASTER TXE interrupt */
     SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE);
     
     /* Enable SPI_MASTER */
     SPI_Cmd(SPI1, ENABLE);
}


void GPIO_Configuration(void)
{
     GPIO_InitTypeDef GPIO_InitStructure;
     
     // Enable GPIO clock //
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
     // Enable SPI1 Pins Software Remapping //
     //GPIO_PinRemapConfig(GPIO_Remap_SPI1, ENABLE);
     
     // Configure SPI_MASTER pins: SCK, MISO and MOSI //
     
     GPIO_InitStructure.GPIO_Pin = PIN_SPI_COMMS_SCK | PIN_SPI_COMMS_MISO | PIN_SPI_COMMS_MOSI;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
     GPIO_Init(PORT_SPI_COMMS, &GPIO_InitStructure);
     
     GPIO_Init(PORT_SPI_COMMS, &GPIO_InitStructure);
}


void NVIC_Configuration(void)
{
     NVIC_InitTypeDef NVIC_InitStruct;
     
     NVIC_InitStruct.NVIC_IRQChannel=SPI1_IRQChannel;
     NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
     NVIC_Init(&NVIC_InitStruct);
}


void SPI1_IRQHandler(void)
{
u8 temp=0;
     while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)==RESET); 
          temp = SPI_I2S_ReceiveData(SPI1);
     while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_BSY)==SET);
}

Outcomes