2014-03-03 92 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