0
\$\begingroup\$

I configure my STM to receive data from SPI. The data/CLK are available on MOSI, CLK pins (checked by oscilloscope) but I received 0 value. what is my mistake?

void SPIConfiguration(void)
{
  SPI_InitTypeDef  SPI_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3,ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
  GPIO_PinRemapConfig(GPIO_Remap_SPI3,DISABLE);
  GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
  GPIO_PinRemapConfig(GPIO_Remap_SPI1,DISABLE);


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5| GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  SPI_InitStructure.SPI_Direction =SPI_Direction_1Line_Rx;
  SPI_InitStructure.SPI_Mode = SPI_Mode_Slave ;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(SPI1, &SPI_InitStructure);
  SPI_Cmd(SPI1, ENABLE);
  SPI_SSOutputCmd(SPI1,DISABLE);
  SPI_I2S_ITConfig(SPI1,SPI_I2S_IT_RXNE,ENABLE);

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);   
  NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

void SPI1_IRQHandler(void)
{   
    if (SPI_I2S_GetITStatus(SPI1,SPI_I2S_IT_RXNE)== SET) 
    {
        SendTest=SPI_I2S_ReceiveData(SPI1);                         
    }

    if (SPI_I2S_GetITStatus(SPI1, SPI_I2S_IT_TXE)== SET )  
    {
         SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, DISABLE);
    }                   
}

My transmitter is another STM which its SPI frequency Bus (36/16=2Mhz) and configured in Master mode:


    SPI_InitStructure.SPI_Direction =SPI_Direction_1Line_Tx;//SPI_Direction_2Lines_FullDuplex;//
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; 
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(SPI2, &SPI_InitStructure);
  SPI_Cmd(SPI2, ENABLE);
 SPI_SSOutputCmd(SPI2,DISABLE);

I connected CLK->CLK, MOSI->MOSI , and SendTest datatype isvolatile unsigned long SendTest;

\$\endgroup\$
  • 1
    \$\begingroup\$ What is SendTest and how is it defined? \$\endgroup\$ – Ron Beyer Feb 14 at 22:07
  • 1
    \$\begingroup\$ IS your STM32 device the SPI Master or the SPI Slave? What is the other device it's talking to? \$\endgroup\$ – brhans Feb 14 at 23:38
  • \$\begingroup\$ Thank you, I updated my post, my receiver is a Slave and my transmitter is a Master device. \$\endgroup\$ – Parisa Feb 15 at 3:43
  • \$\begingroup\$ What about MISO line? \$\endgroup\$ – smajli Feb 15 at 22:18
  • \$\begingroup\$ I connected them together, First I didn't configure MISO and it didn't work. Second I configure pins of MISO but it doesn't work again( it works fine under full-duplex communication but for SPI_Direction_1Line_Tx/SPI_Direction_1Line_RX it has a problem. For the 1line communication, it is necessary to all pins have been connected? (SCK,MOSI,MISO) \$\endgroup\$ – Parisa Feb 16 at 3:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.