2015-10-06 04:24 PM
I am having trouble getting SPI2 to function on my STM32F407 board. My issue is that I am seeing data on the MOSI pin with a logic analyzer but there is no clock activity (it is low). My processor is clocked at 168MHz and APB1 is set 42MHz. Below is the function that I am using to configure the module.
void initOLED_Spi(void)
{
SPI_InitTypeDef OLED_SpiInitStruct;
GPIO_InitTypeDef SpiGPIO_InitStructure;
//CONFIGURE THE GPIO PINS B12, B13, B14, AND B15 FOR SPI
/* B12 = CS = SSD1306_CS_PIN
* B13 = SCK = SSD1306_CLK_PIN
* B14 = MISO = SSD1306_MISO_PIN
* B15 = MOSI = SSD1306_MOSI_PIN
*/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
//Enable the SPI2 Peripherial clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
//CONFIGURE THE CS LINE
GPIO_StructInit(&SpiGPIO_InitStructure);
SpiGPIO_InitStructure.GPIO_Pin = SSD1306_CS_PIN;
SpiGPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
SpiGPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
SpiGPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
SpiGPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init( GPIOB, &SpiGPIO_InitStructure);
GPIO_SetBits(GPIOB, SSD1306_CS_PIN);
//CONFIGURE SCK AND MOSI
GPIO_StructInit(&SpiGPIO_InitStructure);
SpiGPIO_InitStructure.GPIO_Pin = (SSD1306_CLK_PIN | SSD1306_MOSI_PIN);
SpiGPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
SpiGPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
SpiGPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
SpiGPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init( GPIOB, &SpiGPIO_InitStructure);
GPIO_StructInit(&SpiGPIO_InitStructure);
SpiGPIO_InitStructure.GPIO_Pin = SSD1306_MISO_PIN;
SpiGPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
//SpiGPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
SpiGPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
SpiGPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init( GPIOB, &SpiGPIO_InitStructure);
//SET THE GPIO TO SPI ALTERNATE
GPIO_PinAFConfig(GPIOB, (GPIO_PinSource15 | GPIO_PinSource13 | GPIO_PinSource14), GPIO_AF_SPI2);
//SETUP THE PORT
SPI_StructInit( &OLED_SpiInitStruct);
OLED_SpiInitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
OLED_SpiInitStruct.SPI_Mode = SPI_Mode_Master;
OLED_SpiInitStruct.SPI_DataSize = SPI_DataSize_8b;
OLED_SpiInitStruct.SPI_CPOL = SPI_CPOL_High;
OLED_SpiInitStruct.SPI_CPHA = SPI_CPHA_2Edge;
OLED_SpiInitStruct.SPI_NSS = SPI_NSS_Soft;
OLED_SpiInitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
OLED_SpiInitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init( SPI2, &OLED_SpiInitStruct);
SPI_Cmd(SPI2, ENABLE);
}
Then I am using the following code to try and generate data on the SPI port:
GPIO_ResetBits(GPIOB, SSD1306_CS_PIN);
SPI_I2S_SendData(SPI2 , 0xAA);
GPIO_SetBits(GPIOB, SSD1306_CS_PIN);
For what it is worth I am using CooCox and I am using their libraries as I was having issues with the HAL library and Keil / CooCox.
Thanks!
#selective-cut-n-paste
2015-10-06 04:39 PM
//SET THE GPIO TO SPI ALTERNATE
GPIO_PinAFConfig(GPIOB, (GPIO_PinSource15 | GPIO_PinSource13 | GPIO_PinSource14), GPIO_AF_SPI2);
It doesn't work like that, it's an index not a bit mask. Enumerate each pin individually.
2015-10-06 04:54 PM
Good catch but that is not the problem. I changed to to
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2); GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2); GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2);but the clock is still not being generated. I tried it before and also after declaring the GPIO for the SPI bus.2015-10-06 06:02 PM
May be it's with stuff I can't see. It's much easier if you can provide something that's stand-alone.
There's some unnecessary stuff in the GPIO initialization, and I'd probably put a check prior to the SendData, waiting for TXE, clearing the RXNE, and then waiting for RXNE on the back-end, and then loop the send so I could see things on a scope. If you looped the current code, it probably wouldn't get to send anything.