This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

STM32F10x - SPI doesn't send any data

this is the program. this program build successfully but on simulation(SPI WINDOW SPI1_DR) it does not show any data please suggest....


#include "stm32f10x_lib.h"
#define BufferSize 10
void RCC_Configuration(void);
void GPIO_Configuration(void);
void SPI_Configuration(void);
ErrorStatus HSEStartUpStatus;
u8 TxIdx = 0, RxIdx = 0;
u8 SPI1_Buffer_Tx[BufferSize] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09};

u8 SPI1_Buffer_Rx[BufferSize];
int main(void)
{
    RCC_Configuration();
    GPIO_Configuration();
    /* Just delay some time */
    SPI_Configuration();
    /* Enable SPI1 */
    SPI_Cmd(SPI1, ENABLE);

        while(TxIdx < BufferSize)
        {

               SPI_I2S_SendData(SPI1, SPI1_Buffer_Tx[TxIdx]);
                TxIdx++;

        }
 }
void RCC_Configuration(void)
{

        RCC_DeInit();
        RCC_HSEConfig(RCC_HSE_ON);
        HSEStartUpStatus=RCC_WaitForHSEStartUp();
        if(HSEStartUpStatus == SUCCESS)
        {
                 RCC_HCLKConfig(RCC_SYSCLK_Div1);
                 RCC_PCLK2Config(RCC_HCLK_Div2);
                 RCC_PCLK1Config(RCC_HCLK_Div2);
                 RCC_PCLK1Config(RCC_HCLK_Div2);
                 RCC_PLLConfig(0x00010000, RCC_PLLMul_9);
                 RCC_PLLCmd(ENABLE);
                 while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {}
                 RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
                 while (RCC_GetSYSCLKSource() != 0x08) {}
        }
        /* Enable peripheral clocks ----------------------------------------------*/
    /* GPIOA and SPI1 clock enable */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |  RCC_APB2Periph_SPI1, ENABLE);

}
void GPIO_Configuration(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_DeInit(GPIOA);
        /* Configure SPI1 pins: SCK, MISO and MOSI -------------------------------*/
    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

}
void SPI_Configuration(void)
{
        SPI_InitTypeDef SPI_InitStructure;
        SPI_I2S_DeInit(SPI1);
        /* Initialize the SPI_Direction member */
 SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

  /* initialize the SPI_Mode member */
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

  /* initialize the SPI_DataSize member */
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

  /* Initialize the SPI_CPOL member */
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;

  /* Initialize the SPI_CPHA member */
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;

  /* Initialize the SPI_NSS member */
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

  /* Initialize the SPI_BaudRatePrescaler member */
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;

  /* Initialize the SPI_FirstBit member */
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;

  /* Initialize the SPI_CRCPolynomial member */
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(SPI1, &SPI_InitStructure);

}

Parents
  • Why such comments?

      /* Initialize the SPI_CPHA member */
      SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
    
      /* Initialize the SPI_NSS member */
      SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    
      /* Initialize the SPI_BaudRatePrescaler member */
      SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
    
      /* Initialize the SPI_FirstBit member */
      SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
    
      /* Initialize the SPI_CRCPolynomial member */   <== wouldn't a reader be able to figure this out hisemf?
      SPI_InitStructure.SPI_CRCPolynomial = 7;   <== your comment don't tell what '7' actually means..
      SPI_Init(SPI1, &SPI_InitStructure);
    

    If you add comments to a program, you shouldn't add them just to have comments. You should make sure that the comments actually adds some extra value. Normally by telling _why_ you do something.

    Telling that you initialize the SPI_NSS member is totally foolish since any coder who can't see that from the following source line really is too unskilled to be the target audience for your comments. And he/she would still not be helped from getting the information duplicated.

    If you do document your code based on "why", then you might actually figure out why your program doesn't work. When something doesn't work, it's normally because you needed to do A, but actually did B. Or you didn't do anything at all.

Reply
  • Why such comments?

      /* Initialize the SPI_CPHA member */
      SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
    
      /* Initialize the SPI_NSS member */
      SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    
      /* Initialize the SPI_BaudRatePrescaler member */
      SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
    
      /* Initialize the SPI_FirstBit member */
      SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
    
      /* Initialize the SPI_CRCPolynomial member */   <== wouldn't a reader be able to figure this out hisemf?
      SPI_InitStructure.SPI_CRCPolynomial = 7;   <== your comment don't tell what '7' actually means..
      SPI_Init(SPI1, &SPI_InitStructure);
    

    If you add comments to a program, you shouldn't add them just to have comments. You should make sure that the comments actually adds some extra value. Normally by telling _why_ you do something.

    Telling that you initialize the SPI_NSS member is totally foolish since any coder who can't see that from the following source line really is too unskilled to be the target audience for your comments. And he/she would still not be helped from getting the information duplicated.

    If you do document your code based on "why", then you might actually figure out why your program doesn't work. When something doesn't work, it's normally because you needed to do A, but actually did B. Or you didn't do anything at all.

Children