2012-10-08 07:06 AM
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 #spi2012-10-08 11:31 AM
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.2012-10-09 03:29 AM
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.
2012-10-09 04:42 AM
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.2012-10-09 07:53 AM
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.
2012-10-18 06:53 AM
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?2013-03-19 07:24 AM
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=false2013-03-19 08:58 AM
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.