AnsweredAssumed Answered

STM32F4 - Hanging on SPI Get Status

Question asked by roofie01 on Oct 11, 2015
Latest reply on Oct 12, 2015 by roofie01
Hi,

I'm trying to get a Micron M25P128 flash device going on SPI1. below is the initialization, and then the send function.
At this point I am sending just a read ID opcode, but it is hanging at the next line after the one commented with **asterisks. Both MOSI / MISO pulled high with 10K, CS is set low (confirmed).
In addition, not using the "NSS" pin (PA4) - it's being used for something else, and not sure if it's relevant. I'm using PD6 for CS.

Can anyone please point out something that might be obviously wrong?
 
Thank for any help you can provide!

void SPI1_Configuration(void)
{
    SPI_InitTypeDef SPI_InitStruct;
    GPIO_InitTypeDef GPIO_InitStructure;    

    SPI_I2S_DeInit(SPI1);
    
    // enable GPIO D clock for PD6 - CS
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
    
      /* Configure the chip select pin to PD6 */
    GPIO_InitStructure.GPIO_Pin = MEM_CS;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
    //set (PD6) CS high
    GPIO_SetPinHigh(GPIOD, MEM_CS);

    // enable peripheral clock - SPI Clock
    RCC_APB1PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

    // enable GPIO A clock for PA5/PA6/PA7 (SPI CLK/DATA)
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

    GPIO_InitStructure.GPIO_Pin = MEM_SCK  | MEM_MOSI;
    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_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = MEM_MISO;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    //observe the "GPIO_AF_SPI1" denotes ALTERNATE MAPPING fucntion
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1); //connecting pin 5 of port A to the SPI1 peripheral
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1); //connecting pin 6 of port A to the SPI1 peripheral
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1); //connecting pin 7 of port A to the SPI1 peripheral

    /* 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_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO lines
    //SPI_InitStruct.SPI_Direction = SPI_Direction_1Line_Tx; // set to TX only
    
    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 16 bits wide
    SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;        // clock is low when idle
    //SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;
    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_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
    SPI_Init(SPI1, &SPI_InitStruct);
    
    SPI_Cmd(SPI1, ENABLE); // enable SPI1
 }

int8_t SPI1_send(uint8_t data)
{
        GPIO_SetPinLow(GPIOD, MEM_CS);

        //  *******code hangs at next line ********
        while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);

       //data in is opCode 0x9f
        SPI_I2S_SendData(SPI1, data);

      .......etc...etc...etc

}

Outcomes