woshilee 发表于 2014-12-16 17:29:14

F072RB的SPI问题&深入浅出STM32-NUCLEO-072RB谁做的?

本帖最后由 woshilee 于 2014-12-17 14:33 编辑

void GPIO_Configuration(void)
{
    GPIO_InitTypeDef      GPIO_InitStructure;

    /* ConfigurePA5£¬PA7 in output nopull mode */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz ;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

         
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);


    GPIO_PinAFConfig(GPIOA,GPIO_PinSource4, GPIO_AF_0);
    GPIO_PinAFConfig(GPIOA,GPIO_PinSource5, GPIO_AF_0);
    GPIO_PinAFConfig(GPIOA,GPIO_PinSource6, GPIO_AF_0);
    GPIO_PinAFConfig(GPIOA,GPIO_PinSource7, GPIO_AF_0);
}
void RCC_Configuration(void)
{
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_GPIOA |RCC_AHBPeriph_GPIOC , ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
}
void SPI_Configuration(void)
{
    SPI_InitTypeDef SPI_InitStructure;
    SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_CPHA=SPI_CPHA_2Edge;
    SPI_InitStructure.SPI_CPOL=SPI_CPOL_High;
    SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_8 ;
    SPI_InitStructure.SPI_CRCPolynomial=7;
    SPI_InitStructure.SPI_DataSize=SPI_DataSize_8b;
    SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_Mode=SPI_Mode_Master;
    SPI_InitStructure.SPI_NSS=SPI_NSS_Soft ;
    SPI_Init(SPI1,&SPI_InitStructure);
    SPI_NSSInternalSoftwareConfig(SPI1,SPI_NSSInternalSoft_Set);
    SPI_SSOutputCmd(SPI1, ENABLE);
    SPI_Cmd(SPI1, ENABLE);
}

基本的配置就是这样,下面是我写的读写函数
uint8_t SPI2_ReadWriteByte(uint8_t TxData)
{               
      uint8_t RxData = 0;
      while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);         
      SPI_SendData8(SPI1, TxData);                                       
       while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
      RxData = SPI_ReceiveData8(SPI1);//SPI2                              
      return (uint8_t)RxData;                                    
}

每次都会死在红色的地方,这究竟是为什么呢?

我看了一下深入浅出STM32-NUCLEO-072RB的资料,有点问题,里边这样配置:
void SPI1_Init(void)
{
         GPIO_InitTypeDefGPIO_InitStructure;
         SPI_InitTypeDef   SPI_InitStructure;
         RCC_AHBPeriphClockCmd(SPI_GPIO_CLK,ENABLE);
         RCC_APB2PeriphClockCmd(SPI_CLK, ENABLE);
         
         GPIO_InitStructure.GPIO_Pin   = SPI_SCK_PIN|SPI_MISO_PIN|SPI_MOSI_PIN;
         GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF;
         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
         GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
         GPIO_Init(SPI_GPIO_PORT , &GPIO_InitStructure);
         GPIO_PinAFConfig(SPI_GPIO_PORT, SPI_SCK_SOURCE , SPI_SCK_AF);
         GPIO_PinAFConfig(SPI_GPIO_PORT, SPI_MISO_SOURCE, SPI_MISO_AF);
         GPIO_PinAFConfig(SPI_GPIO_PORT, SPI_MOSI_SOURCE, SPI_MOSI_AF);                        
          //SPI CONFIGURATION
         SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;         
      SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
      SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
      SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
      SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;   
      SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
      SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
      SPI_InitStructure.SPI_CRCPolynomial = 7;                                 
      SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
      SPI_Init(SPI1, &SPI_InitStructure);                                          
      SPI_NSSInternalSoftwareConfig(SPI1, SPI_NSSInternalSoft_Set);
      SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);
      SPI_Cmd(SPI1, ENABLE); /* SPI enable */
}


这是里边的定义:
#defineSPI_CLK                     RCC_APB2Periph_SPI1
#defineSPI_GPIO_CLK                  RCC_AHBPeriph_GPIOB
#defineSPI_GPIO_PORT               GPIOB                  /* GPIOB*/


#defineSPI_SCK_PIN                   GPIO_Pin_3               /* PB.3 */
#defineSPI_SCK_SOURCE                GPIO_PinSource3
#defineSPI_SCK_AF                  GPIO_AF_0


#defineSPI_MISO_PIN                  GPIO_Pin_4               /* PB.4 */
#defineSPI_MISO_SOURCE               GPIO_PinSource4
#defineSPI_MISO_AF                   GPIO_AF_0


#defineSPI_MOSI_PIN                  GPIO_Pin_5               /* PB.5 */
#defineSPI_MOSI_SOURCE               GPIO_PinSource5
#defineSPI_MOSI_AF                   GPIO_AF_0


SPI1对应的端口不是PB而是PA,只有重映射了才会这样,但是没找到是在哪里重映射的。
且不说这个问题,大家怎么看卡死在 while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);

大神现身吧






woshilee 发表于 2014-12-16 19:37:19

还是自己解决吧,因为少了SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);
这个函数是决定RXNE标志位产生条件的,详细的可以看我发的帖子

沐紫 发表于 2014-12-17 08:55:16

woshilee 发表于 2014-12-16 19:37
还是自己解决吧,因为少了SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);
这个函数是决定RXNE ...

视频是枫叶老师做的,他也在社区发了ppt课件https://www.stmcu.org.cn/module/forum/forum.php?mod=viewthread&tid=597508&highlight=%E6%B7%B1%E5%85%A5%E6%B5%85%E5%87%BA
页: [1]
查看完整版本: F072RB的SPI问题&深入浅出STM32-NUCLEO-072RB谁做的?