AnsweredAssumed Answered

SPI CPHA not setting correctly

Question asked by naphtali on Sep 26, 2014
Latest reply on Sep 29, 2014 by naphtali
I am seeing some strange behavior in my SPI port.

I expect that it will be set up as CPOL = 0, CPHA = 0 and for the bus to output 0xA5.

What I see is that not only is MOSI transitioning on the wrong edges, but the data appears shifted.

I have inserted my code and a scope capture of the output. Red is CS, Yellow is CLK, and Green is MOSI.


Any ideas as to what is going on here?

-------------------------------------------Code-------------------------------------------------
    GPIO_InitTypeDef GPIO_InitStructure;
    SPI_InitTypeDef  SPI_InitStructure;


    // Enable GPIO clk
    RCC_AHB1PeriphClockCmd(EEPROM_MOSI_AHB1Periph_GPIOx, ENABLE);
    RCC_AHB1PeriphClockCmd(EEPROM_MISO_AHB1Periph_GPIOx, ENABLE);
    RCC_AHB1PeriphClockCmd(EEPROM_SCLK_AHB1Periph_GPIOx, ENABLE);
    RCC_AHB1PeriphClockCmd(EEPROM_CS_AHB1Periph_GPIOx,   ENABLE);


    // Connect Pins to AF
    GPIO_PinAFConfig(EEPROM_SCLK_GPIOx, EEPROM_SCLK_SOURCE, EEPROM_AF);
    GPIO_PinAFConfig(EEPROM_MOSI_GPIOx, EEPROM_MOSI_SOURCE, EEPROM_AF);
    GPIO_PinAFConfig(EEPROM_MISO_GPIOx, EEPROM_MISO_SOURCE, EEPROM_AF);


    // Pin Configuration
    GPIO_StructInit(&GPIO_InitStructure);
    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_DOWN;
    GPIO_InitStructure.GPIO_Pin   = EEPROM_SCLK_PIN_x;
    GPIO_Init(EEPROM_SCLK_GPIOx, &GPIO_InitStructure);


    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;


    GPIO_InitStructure.GPIO_Pin   = EEPROM_MOSI_PIN_x;
    GPIO_Init(EEPROM_MOSI_GPIOx, &GPIO_InitStructure);


    GPIO_InitStructure.GPIO_Pin   = EEPROM_MISO_PIN_x;
    GPIO_Init(EEPROM_MISO_GPIOx, &GPIO_InitStructure);


    GPIO_SetBits(EEPROM_CS_GPIOx, EEPROM_CS_PIN_x);
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
    GPIO_InitStructure.GPIO_Pin   = EEPROM_CS_PIN_x;
    GPIO_Init(EEPROM_CS_GPIOx, &GPIO_InitStructure);




    // Init SPI peripheral


    // Enable SPI clk
    EEPROM_CLK_INIT(EEPROM_CLK, ENABLE);


    SPI_I2S_DeInit(EEPROM_SPIx);


    SPI_StructInit(&SPI_InitStructure);
    SPI_InitStructure.SPI_Direction         = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode              = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize          = SPI_DataSize_8b;
    // CPHA = 0 CPOL = 0
    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_8;
    SPI_InitStructure.SPI_FirstBit          = SPI_FirstBit_MSB;
    SPI_Init(EEPROM_SPIx,&SPI_InitStructure);


    // Set so that the SPI Master does not think someone is trying to take over the bus (Sets SSI bit)
    SPI_NSSInternalSoftwareConfig(EEPROM_SPIx,SPI_NSSInternalSoft_Set);


    eeprom_csd();
    SPI_Cmd(EEPROM_SPIx,ENABLE);
    SPI_I2S_SendData(EEPROM_SPIx,0xA5);


-------------------------------------------Code-------------------------------------------------

SPI_Error0.png

Outcomes