cancel
Showing results for 
Search instead for 
Did you mean: 

STM32VLDiscovery SPI2 - how to enable?

gigamegawatts
Associate
Posted on June 03, 2014 at 01:32

Hi,

I'm new to STM32 development.  I'm working my way through Professor Geoffrey Brown's ''Discovering the STM32 Microcontroller'', using an STM32VL Discovery board, and have run into a snag in the SPI section.

After not getting SPI2 to work at all, with various examples I have tried.  The same examples work when I use SPI1. 

I'm wondering if the problem is with my code to enable the clock for SPI2.

Here's the relevant section of my SPI initialization code, with the SPI2 clock enabling code highlighted...

if (SPIx == SPI1)

{

    // A5 = SCK, , A7 = MOSI

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);

    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_5 | GPIO_Pin_7;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;

    GPIO_Init(GPIOA, &GPIO_InitStructure);

    

    // A6 = MISO

    GPIO_StructInit(&GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

    GPIO_Init(GPIOA, &GPIO_InitStructure);

} else  if (SPIx == SPI2) {

    // B13 = SCK, B15 = MOSI

   

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB1Periph_SPI2, ENABLE);

    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_13 | GPIO_Pin_15;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;

    GPIO_Init(GPIOB, &GPIO_InitStructure);

    

    // B14 = MISO

    GPIO_StructInit(&GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

    GPIO_Init(GPIOB, &GPIO_InitStructure);

}

Does that look correct?  If so, are there any special considerations when using SPI2 on the STM32VL Discovery board -- extra steps that aren't required when using SPI1?

Thanks,

Dan

#stm32-stm32vldiscovery-stm32f100
2 REPLIES 2
Posted on June 03, 2014 at 04:08

SPI1 and SPI2 are on different APB

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
gigamegawatts
Associate
Posted on June 03, 2014 at 17:06

Clive,

Thanks!  I must have looked at that line of code 100 times without noticing the ''ABP1''.

Dan.