2014-03-03 55 views
0

我正在尝试配置SPI以使用ST7565 GLCD库,该库已给出here。目前,我正在尝试使用SPI1来实现这一点。当在main()中调用下面给出的init函数时,它会导致程序在assert_failed函数中循环。STM32F4 SPI配置SPI_Init导致assert_failed循环

void init_SPI1(void){ 

    GPIO_InitTypeDef GPIO_InitStruct; 
    SPI_InitTypeDef SPI_InitStruct; 

    // enable clock for used IO pins 
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); 

    /* configure pins used by SPI1 
    * PA5 = SCK 
    * PA6 = MISO 
    * PA7 = MOSI 
    */ 
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5; 
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; 
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; 
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; 
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; 
    GPIO_Init(GPIOA, &GPIO_InitStruct); 

    // connect SPI1 pins to SPI alternate function 
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1); 
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1); 
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1); 

    // enable clock for used IO pins 
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); 

    /* Configure the chip select pin 
     in this case we will use PE7 */ 
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7; 
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; 
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; 
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; 
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; 
    GPIO_Init(GPIOE, &GPIO_InitStruct); 

    GPIOE->BSRRL |= GPIO_Pin_7; // set PE7 high 

    // enable peripheral clock 
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); 

    /* configure SPI1 in Mode 0 
    * CPOL = 0 --> clock is low when idle 
    * CPHA = 0 --> data is sampled at the first edge 
    */ 
    SPI_InitStruct.SPI_Direction = SPI_Direction_1Line_Tx; // set to full duplex mode, seperate MOSI and MISO lines 
    SPI_InitStruct.SPI_Mode = SPI_Mode_Master;  // transmit in master mode, NSS pin has to be always high 
    SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // one packet of data is 8 bits wide 
    SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;  // clock is low when idle 
    SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;  // data sampled at first edge 
    SPI_InitStruct.SPI_NSS = SPI_NSS_Soft; // set the NSS management to internal and pull internal NSS high 
    SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; // SPI frequency is APB2 frequency/4 
    SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first 

    SPI_Init(SPI1, &SPI_InitStruct); 
    SPI_Cmd(SPI1, ENABLE); // enable SPI1 
} 

我注意到,程序进无限循环assert_failed函数内,当它到达SPI_Init()行:

SPI_Init(SPI1, &SPI_InitStruct); 

的assert_failed功能(默认固件库)低于:

void assert_failed(uint8_t* file, uint32_t line) 
{ 
    /* User can add his own implementation to report the file name and line number, 
    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 

    /* Infinite loop */ 
    while (1) 
    { 

    } 
} 

我不知道它是什么意思,它在assert_failed函数中循环。这是与SPI配置有关的问题吗?我需要指导来了解问题并生成解决方案。任何帮助将非常感激。提前致谢。

编辑:我的SPI_Init函数内检查了stm32f4xx_spi.c

void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct) 
{ 
    uint16_t tmpreg = 0; 

    /* check the parameters */ 
    assert_param(IS_SPI_ALL_PERIPH(SPIx)); 

    /* Check the SPI parameters */ 
    assert_param(IS_SPI_DIRECTION_MODE(SPI_InitStruct->SPI_Direction)); 
    assert_param(IS_SPI_MODE(SPI_InitStruct->SPI_Mode)); 
    assert_param(IS_SPI_DATASIZE(SPI_InitStruct->SPI_DataSize)); 
    assert_param(IS_SPI_CPOL(SPI_InitStruct->SPI_CPOL)); 
    assert_param(IS_SPI_CPHA(SPI_InitStruct->SPI_CPHA)); 
    assert_param(IS_SPI_NSS(SPI_InitStruct->SPI_NSS)); 
    assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_InitStruct->SPI_BaudRatePrescaler)); 
    assert_param(IS_SPI_FIRST_BIT(SPI_InitStruct->SPI_FirstBit)); 
    assert_param(IS_SPI_CRC_POLYNOMIAL(SPI_InitStruct->SPI_CRCPolynomial)); 

由于库已被锁定,我不能让里面输入任何东西在观看现场直播调试。 (我正在使用IAR EWARM)

回答

0

它在assert()中循环,因为断言失败,所以循环停止进一步执行。

只需加紧堆栈,以便可以看到外围库中哪个断言失败。该库对其参数进行了相当广泛的验证,因此可能在您的某个调用中出现错误。

UPDATE看起来你永远不会初始化CRCPolynomial字段,但它被声明。我建议添加一个SPI_StructInit()的调用,以确保在初始化init结构之前,根据应用程序的意愿开始设置它。

+0

请检查我的编辑。 – mozcelikors

+0

我会检查一下。 – mozcelikors