2
\$\begingroup\$

I am working on stm32f103rb nucleo board and want to send data and receive data using stm32f103 in slave mode. I can get data from the master but I can not send data to the master. I do not know what caused the problem.(initializing or interrup or flag ?) How do I make an arrangement?

#include "stm32f10x.h"                  /*Device header*/
#define rxBufferSize 4
#define txBufferSize 5
#define SPI_SLAVE_PIN_NSS 12
#define SPI_SLAVE_PIN_SCK 5
#define SPI_SLAVE_PIN_MISO 6
#define SPI_SLAVE_PIN_MOSI 7



uint8_t txSize=5;
 uint8_t SPI_SLAVE_Buffer_Rx[rxBufferSize],RxIdx=0,TxIdx=0;
 uint8_t SPIz_Buffer_Tx[txBufferSize] = {'s','e','l','a','m'};

/*****************************************************************************/
/*                           FUNCTIONS                                       */
/*****************************************************************************/

/**
 * @brief  Initializes the SPI
 * @param  None
 * @retval None
 */
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);


int main(void)
{

    RCC_Configuration();

    NVIC_Configuration();

    GPIO_Configuration(); 

    SPI_InitTypeDef SPI_InitStructure;

   /* SPI_SLAVE 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_Low;                                                                         /* NOTE: Clock polarity low */

    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;                                                                       /* NOTE: Clock phase 2 edges */

    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;                                                                          /* NOTE: Chip select is controlled by software */

    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;

    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;                                                                 /* NOTE: MSB First */

    SPI_InitStructure.SPI_CRCPolynomial = 7;                                                                           /* NOTE: Default CRC calculation polynomial value */


    SPI_Init(SPI1, &SPI_InitStructure);

    SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE);

 //       SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, ENABLE);

/* Enable SPI_SLAVE */
 /* SPI_I2S_ClearITPendingBit(SPI1, SPI_I2S_IT_RXNE);
  SPI_I2S_ClearITPendingBit(SPI1, SPI_I2S_IT_TXE);*/

    SPI_Cmd(SPI1, ENABLE);

    while(1) {


}

}
void RCC_Configuration(void)
{  
   // RCC_PCLK2Config(RCC_HCLK_Div2); 

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_SPI1, ENABLE);

}

void GPIO_Configuration(void)
{
 GPIO_InitTypeDef GPIO_InitStructure;


  /* Configure SCK and MOSI pins as Input Floating */

      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_InitStructure.GPIO_Pin =SPI_SLAVE_PIN_SCK | SPI_SLAVE_PIN_MOSI  ; 
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
      GPIO_Init(GPIOA, &GPIO_InitStructure);


      GPIO_InitStructure.GPIO_Pin =  SPI_SLAVE_PIN_MISO     
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
      GPIO_Init(GPIOA, &GPIO_InitStructure);





}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

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





}
void SPI1_IRQHandler(void)
{
/*  if (SPI_I2S_GetITStatus(SPI1, SPI_I2S_FLAG_TXE) == SET) {

      SPI_I2S_SendData(SPI1, (uint8_t)SPIz_Buffer_Tx[TxIdx++]);

      if(TxIdx == txBufferSize)
         TxIdx=0;
      }
*/
   if (SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)== SET)
        SPI_SLAVE_Buffer_Rx[RxIdx++] = SPI_I2S_ReceiveData(SPI1);
    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);

   if(RxIdx==rxBufferSize)
        RxIdx=0;

      //  SPI1_Transmit(SPIz_Buffer_Tx,SPI_SLAVE_Buffer_Rx,4);
 /* if (SPI_I2S_GetITStatus(SPI1, SPI_I2S_IT_TXE) != RESET)
  {
    //Send SPI1 data
    SPI_I2S_SendData(SPI1, SPIz_Buffer_Tx[TxIdx++]);//just an idea here as how send needs to be done

    //clear interrupt bit
  }
*/
/*  if (SPI_I2S_GetITStatus(SPI1, SPI_I2S_IT_RXNE) != RESET)
  {
    //Receive SPI1 data
   SPI_SLAVE_Buffer_Rx[RxIdx++] = SPI_I2S_ReceiveData(SPI1);


    if(RxIdx==rxBufferSize)
        RxIdx=0;//just an idea here as how receive needs to be done

    //clear interrupt bit
  }     */
}

When I watch the line from the logic analyzer, I can see that the miso line is always in high level.

enter image description here

\$\endgroup\$
1
\$\begingroup\$

I lost about 2 weeks to solve this problem. But I am happy to solve the problem.

Let's settle the problem...

The condition causing the problem; It is a usb logic analyzer that I use to monitor MOSI, MISO and SCK lines. Yes it just caused a usb logic analyzer problem.

I do not know why.

After removing the usb logic analyzer from the line, the problem was solved.

you can look at this repository for those who want to examine the source code.

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.