Question
SPI2 won't work
Posted on May 03, 2012 at 12:08
Hi,
I've been trying to solve this problem for quite a while now. I 've get throuth various forum or example projet but nothing do the trick . My stm32f107 is connected by spi with another microchip (blackfin 537). I've extract my configuration code from various sample ST project :void
RCC_Configuration(
void
)
{
/* PCLK2 = HCLK/2 */
RCC_PCLK2Config(RCC_HCLK_Div2);
/* Enable peripheral clocks --------------------------------------------------*/
/* GPIOB clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* SPI2 TIM4 Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2 | RCC_APB1Periph_TIM4, ENABLE);
// I use tim4 for delay purpose and it works fine
}
void
SPI_Configuration(SPI_InitTypeDef * SPI_InitStructure)
{
/* SPI2 Config -------------------------------------------------------------*/
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_256;
SPI_InitStructure->SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure->SPI_CRCPolynomial = 7;
}
void
SPI_Initialisation(SPI_InitTypeDef SPI_InitStructure){
SPI_I2S_DeInit(SPI2);
/* Set SPI2 SS high*/
GPIO_SetBits(GPIOB, GPIO_Pin_12);
/* Init SPI2 */
SPI_Init(SPI2, &SPI_InitStructure);
/* Enable SPI2 */
SPI_Cmd(SPI2, ENABLE);
}
void
GPIO_Configuration(
void
)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure SPI2 pins: custom SS ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//CLK,MOSI
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//MISO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void
NVIC_Configuration(
void
)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
/* Configure and enable SPI2 interrupt -------------------------------------*/
NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
Here's the spi configuration that i need:
-master
-8bit
-CPOL = 1
-CPHA = 1
-nss was a big problem (as it doesn't act as a true chip select) but i managed to manualy make it high or low)
-high prescaler possible for debugging
-little endian
-CRC not needed
here's my transmission function :
u8 SPI_Manager_Transmit(u8* ToSend ,u8* ToReceive , u16 length, SPI_Manager * me){
u8 ret = 0;
/*[there is some code here related to my app]*/
// Enable SPI2 Transmit interrupts
SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, ENABLE);
// Enable SPI2 Receive interrupts
SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);
//loop until reception and send are complet
while
(*(me->toTransfert) > *(me->transfered) || *(me->toReceive) > *(me->received))
{
//blocking loop
}
return
ret;
}
I use interupt to send and receive so here's the SPI2 interupt handler :
void
SPI2_IRQHandler(
void
)
{
if
(SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_RXNE) == SET)
{
/* Read one byte from the receive data register */
RxBuffer[RxCounter++] = SPI_I2S_ReceiveData(SPI2);
if
(RxCounter == NbrOfDataToRead)
{
/* Disable the SPI2 Receive interrupt */
SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, DISABLE);
}
}
if
(SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_TXE) == SET)
{
/* Write one byte to the transmit data register */
SPI_I2S_SendData(SPI2, TxBuffer[TxCounter++]);
if
(TxCounter == NbrOfDataToTransfer)
{
/* Disable the SPI2 Transmit interrupt */
SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, DISABLE);
}
}
}
I think i didn't forget anything related to SPI2.
Currently the chip select and the clock are ok. I can see them on an ocilloscope. But there is no sign of MOSI. MISO is constantly at +3V,don't know where that come from, maybe the other microchip ?
I think I've could have misconfigured GPIOs or maybe killed some (they may have been damaged by a > 5V current)
Any help would be welcomed, I'm new to all this so I could have done common mistakes.