yyHHyyYyyHHh 发表于 2022-10-28 15:28

SPI例程失败,没有信号

本帖最后由 yyHHyyYyyHHh 于 2022-10-28 16:34 编辑

运行n32g43x_EVAL\examples\SPI\CRC这个例程。
将修改SPI1的SCK、MOSI和MISO三个引脚,修改为PB3、PB5和PB4(这三个引脚复用确定为SPI1)。
其余没有修改,部分程序如下:
void GPIO_Configuration(void)
{
    GPIO_InitType GPIO_InitStructure;

    GPIO_InitStruct(&GPIO_InitStructure);

    GPIO_InitStruct(&GPIO_InitStructure);
    /* Configure SPI1 pins: SCK, MISO and MOSI ---------------------------------*/
    /* Confugure SCK and MOSI pins as Alternate Function Push Pull */
    GPIO_InitStructure.Pin      = GPIO_PIN_3 | GPIO_PIN_5;
    GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Alternate = GPIO_AF0_SPI1;
    GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
    /* Confugure MISO pin as Input Floating*/
    GPIO_InitStructure.Pin       = GPIO_PIN_4;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Input;
    GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);

    /* Configure SPI2 pins: SCK, MISO and MOSI ---------------------------------*/
    /* Confugure SCK and MOSI pins as Input Floating */
    GPIO_InitStructure.Pin      = GPIO_PIN_13 | GPIO_PIN_15;
    GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Input;
    GPIO_InitStructure.GPIO_Alternate = GPIO_AF0_SPI2;
    GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
    /* Confugure MISO pin as Alternate Function Push Pull */
    GPIO_InitStructure.Pin       = GPIO_PIN_14;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
}


void RCC_Configuration(void)
{
    /* PCLK2 = HCLK/2 */
    RCC_ConfigPclk2(RCC_HCLK_DIV2);

    /* Enable peripheral clocks --------------------------------------------------*/
    /* GPIOA and GPIOB clock enable */
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO , ENABLE);

    /* SPI1 and SPI2 Periph clock enable */
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_SPI1 | RCC_APB2_PERIPH_SPI2, ENABLE);
}


int main(void)
{
    /*!< At this stage the microcontroller clock setting is already configured,
      this is done through SystemInit() function which is called from startup
      file (startup_n32g43x.s) before to branch to application main.
      To reconfigure the default setting of SystemInit() function, refer to
      system_n32g43x.c file
      */

    /* System clocks configuration ---------------------------------------------*/
    RCC_Configuration();

    /* GPIO configuration ------------------------------------------------------*/
    GPIO_Configuration();

    /* SPI1 configuration ------------------------------------------------------*/
    SPI_InitStructure.DataDirection = SPI_DIR_DOUBLELINE_FULLDUPLEX;
    SPI_InitStructure.SpiMode       = SPI_MODE_MASTER;
    SPI_InitStructure.DataLen       = SPI_DATA_SIZE_16BITS;
    SPI_InitStructure.CLKPOL      = SPI_CLKPOL_LOW;
    SPI_InitStructure.CLKPHA      = SPI_CLKPHA_SECOND_EDGE;
    SPI_InitStructure.NSS         = SPI_NSS_SOFT;
    SPI_InitStructure.BaudRatePres= SPI_BR_PRESCALER_8;
    SPI_InitStructure.FirstBit      = SPI_FB_MSB;
    SPI_InitStructure.CRCPoly       = 7;
    SPI_Init(SPI1, &SPI_InitStructure);

    /* SPI2 configuration ------------------------------------------------------*/
    SPI_InitStructure.SpiMode = SPI_MODE_SLAVE;
    SPI_Init(SPI2, &SPI_InitStructure);

    /* Enable SPI1 CRC calculation */
    SPI_EnableCalculateCrc(SPI1, ENABLE);
    /* Enable SPI2 CRC calculation */
    SPI_EnableCalculateCrc(SPI2, ENABLE);

    /* Enable SPI1 */
    SPI_Enable(SPI1, ENABLE);
    /* Enable SPI2 */
    SPI_Enable(SPI2, ENABLE);

    /* Transfer procedure */
    /* Transfer procedure */
    while (TxIdx < BufferSize - 1)
    {
      /* Wait for SPI1 Tx buffer empty */
      while (SPI_I2S_GetStatus(SPI1, SPI_I2S_TE_FLAG) == RESET)
            ;
                              while(1)
                              {
                                        /* Send SPI2 data */
                                        SPI_I2S_TransmitData(SPI2, SPI2_Buffer_Tx);
                                        /* Send SPI1 data */
                                        SPI_I2S_TransmitData(SPI1, SPI1_Buffer_Tx);
                                        if(TxIdx == 32)
                                        {
                                                TxIdx = 0;
                                        }
                              }
      /* Wait for SPI2 data reception */
      while (SPI_I2S_GetStatus(SPI2, SPI_I2S_RNE_FLAG) == RESET)
            ;
      /* Read SPI2 received data */
      SPI2_Buffer_Rx = SPI_I2S_ReceiveData(SPI2);
      /* Wait for SPI1 data reception */
      while (SPI_I2S_GetStatus(SPI1, SPI_I2S_RNE_FLAG) == RESET)
            ;
      /* Read SPI1 received data */
      SPI1_Buffer_Rx = SPI_I2S_ReceiveData(SPI1);
    }


      .....
      .....
      .....

}
我用示波器看了SPI1的SCK是没有时序信号的,是我哪里设置错了么?
谢谢


yyHHyyYyyHHh 发表于 2022-10-28 15:29

我用while(1)让SPI1一直发数据,方便用示波器看信号。

jdz8888 发表于 2022-10-28 21:59

没有配置复用吧

qintian0303 发表于 2022-10-29 08:48

SPI还是比较容易的

cooldog123pp 发表于 2022-10-29 18:50

官方没有对应的例程么,感觉用国产MCU大家都遇到而来好多问题,不管是大品牌还是小品牌。

yyHHyyYyyHHh 发表于 2022-10-31 09:35

jdz8888 发表于 2022-10-28 21:59
没有配置复用吧

GPIO_InitStructure.GPIO_Alternate = GPIO_AF0_SPI1;
这个不是配置复用么?是这里有问题么?
谢谢

yyHHyyYyyHHh 发表于 2022-10-31 10:21

cooldog123pp 发表于 2022-10-29 18:50
官方没有对应的例程么,感觉用国产MCU大家都遇到而来好多问题,不管是大品牌还是小品牌。 ...

我用的就是官方给的例程,只是把SPI1使用的引脚改了,其他的什么也没动。
这三个引脚确认支持SPI1复用的

sy12138 发表于 2022-10-31 11:07

PB3PB4默认是jtag引脚,使用前需要先使用ConfigPinRemap函数复用为普通GPIO,再使能SPI GPIO
页: [1]
查看完整版本: SPI例程失败,没有信号