2015-03-13 11:06 AM
Hello all,
I faced problems with SPI2. First I interfaced a graphic LCD on SPI bus and could not get to working properly, though it works well on an Atmel 8 bit controller. Spent more than 3 weeks without much luck. Now I interfaced Spansion flash memory. This chip supports reading manufacturer and device identification. When queried it must return 0x15. On STM32F105 returns 02, whereas 0x15 (correct value) is returned on 8 bit controller. Checked clock polarity. Here is my SPI initialization code. Any suggestions please? Thank you. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_Init(GPIOB, &GPIO_InitStructure); SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; // SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_Init(SPI2, &SPI_InitStructure); // currently used SPI on this board SPI_Cmd(SPI2, ENABLE); #stm32f105-spi22015-03-13 12:06 PM
The F1 parts expect MISO pin to be configured in INPUT mode on the Master, not AF
2015-03-13 12:24 PM
This is for an F4, but these worked with the SPI NOR Flash I was testing
/* configure SPI2 in Mode 0
* CPOL = 0 --> clock is low when idle
* CPHA = 0 --> data is sampled at the first edge
*/
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO lines
SPI_InitStruct.SPI_Mode = SPI_Mode_Master; // transmit in master mode, NSS pin has to be always high
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // one packet of data is 8 bits wide
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low; // clock is low when idle
SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge; // data sampled at first edge
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set; // set the NSS management to internal and pull internal NSS high
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; // SPI frequency is APB2 frequency / 4
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
SPI_Init(SPI2, &SPI_InitStruct);
SPI_Cmd(SPI2, ENABLE); // enable SPI2
2015-03-13 06:01 PM
Hi clive1,
Thanks for the reply. Yes I tried all possible combinations. Here is the modified code. The funny thing is, we did not face much problems on an stm32f103. LCD does not have a read facility so only MOSI was used. The other difference is SPI2. In the f103 board we used SPI1. Is there a difference between the two? We are also using usart3 in UART mode. Please help resolve this once and for all. The board is a two layer board and UART1 and 3 work well. Thanks for your help.2015-03-13 06:05 PM
Sorry I think I missed to add the code. Here it is. This is also returning 02 instead of 0x15
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15; // SPI2 GPIO_Init(GPIOB, &GPIO_InitStructure); // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // MISO // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; // GPIO_Init(GPIOB, &GPIO_InitStructure); SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; // SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_Init(SPI2, &SPI_InitStructure); // currently used SPI on this board SPI_Cmd(SPI2, ENABLE);2015-03-13 07:16 PM
Hi Clive1,
Thanks for the help. The code is working now. The mistake was, waiting for TX complete and not RXNE. I thought I used RXNE hence did not take a closer look. I made MISO input with pull-up. Thank you once again.