cancel
Showing results for 
Search instead for 
Did you mean: 

SPI pins initialization STM32L-discovery

jurriaan
Associate II
Posted on October 08, 2012 at 16:06

It seems I can't correctly initialize the SPI pins on the STM32L-discovery board. I have very little experience with programming 32bit devices so I am probably missing something obvious. I hooked up my oscilloscope and the pins all go and stay high. My configuration code:

//Enable clock of the SPI module

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

  /* Enable SCK, MOSI and MISO GPIO clocks */

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_SPI1);

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_SPI1);

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

  /* SPI SCK pin configuration */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* SPI  MOSI pin configuration */

  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_12;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* SPI MISO pin configuration */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  // Configure the SPI module

  SPI_InitTypeDef  SPI_InitStructure;

  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

  SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;

  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;

  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;

  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

  SPI_InitStructure.SPI_CRCPolynomial = 7;

  SPI_Init(SPI1, &SPI_InitStructure);

  SPI_Cmd(SPI1, ENABLE);           /* Enable the SPI  */

  /* Configure the Priority Group to 1 bit */

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

  /* Configure the SPI interrupt priority */

  NVIC_InitTypeDef NVIC_InitStructure;

  NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

#tigers-rock #spi
7 REPLIES 7
sgrunza
Associate II
Posted on October 08, 2012 at 20:31

I have very similar code for the STM32F0Discovery and it also doesn't work.  I'm about to give up on the built-in SPI and write my own bit-bang version.

ST support (via my local distributor) hasn't been of any help with the issue.  Seems like the standard response for problems encountered with the STM32 parts.
jurriaan
Associate II
Posted on October 09, 2012 at 12:29

I don't really believe that you are not getting any help on purpose, it's probably a communication problem. That being said, I really need to use the built in SPI module, I'm doing a research project for my university. I need to finish this project in order to graduate and the SPI interface is only one of a couple built in modules I need to use. I have been working on the code for a time now and I can't find the problem.

crt2
Associate II
Posted on October 09, 2012 at 13:42

I went fast through SPI examples in FWLib and I have noticed that NVIC configuration is always before GPIO so that might be an issue. Another thing I've noticed is that they are set to tables while you have it as IRQ. Also maybe you should consider putting this NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  away as if its 0, then it is not really needed imo.

Otherwise why dont you try unchanged examples as they should work and go from there? I always find some nice hints in FWLib's examples.

Posted on October 09, 2012 at 16:53

And presumably you'd want to the using SPI_I2S_SendData() to get some signalling on the bus, and a chip select to the slave device if so required.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jurriaan
Associate II
Posted on October 18, 2012 at 15:53

Thank you for the help everybody. I have managed to get a SPI signal for the board, looks good on my scope. Somehow my MCP23S08 chip doesn't react. I have checked the wiring and I am guessing that maybe the clock and MOSI pin sync are incorrect. I think these two lines control this:

  SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;

  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;

Can you guide me in the right direction?

elazhar
Associate II
Posted on March 19, 2013 at 15:24

please help me it's urgent !! ican't communicate with my device ( ksz8851 snl), im using the stm32f103 microcontroller . When i debug im stuck in the loop while (dev_id != CHIP_ID_8851_16) in step 1 of ksz8851's initialization .i think that the issue is caused by the initialization of SPI but i think that i configurate it well. I've attached my source code !!!!!!!

________________

Attachments :

problem.txt : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hzjc&d=%2Fa%2F0X0000000bQd%2Fs.Wovt2jwtBGv1LE3vnXnF46sEaTuylpoQstonsI4hg&asPdf=false
Posted on March 19, 2013 at 16:58

please help me it's urgent !! ican't communicate with my device ( ksz8851 snl), im using the stm32f103 microcontroller . When i debug im stuck in the loop while (dev_id != CHIP_ID_8851_16) in step 1 of ksz8851's initialization .i think that the issue is caused by the initialization of SPI but i think that i configurate it well. I've attached my source code !!!!!!!

 

Seems off topic. Well I would guess that you aren't initializing the GPIO clocks properly but in your urgency you failed to attach the include files defining some of the constants you are using.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..