cancel
Showing results for 
Search instead for 
Did you mean: 

f103 spi slave unreliable

zhausq
Associate III
Posted on February 05, 2016 at 04:13

I am experiencing difficulty getting the spi slave to transmit data. Receiving data works perfectly and correctly, transmitting however doesn't.

Initialisation code:

GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//enable spi clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE);
//configure miso pin
GPIO_InitStructure.GPIO_Pin = SPI2_PIN_MISO;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(SPI2_PORT, &GPIO_InitStructure);
//configure mosi + sclk + nss pins
GPIO_InitStructure.GPIO_Pin = SPI2_PIN_MOSI | SPI2_PIN_SCLK | SPI2_PIN_NSS;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(SPI2_PORT, &GPIO_InitStructure);
//initialize spi
SPI_I2S_DeInit(SPI2);
SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, DISABLE);//interrupt disable
//
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//mode
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;//master/slave mode
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;//data size
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;// clock is high when idle
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;// data sampled at second edge
SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;//use hardware slave select pin
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
SPI_InitStructure.SPI_CRCPolynomial = 7;
//
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);//interrupt enable
SPI_Init(SPI2, &SPI_InitStructure);
//setup interrupt

NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//enable spi
SPI_Cmd(SPI2, ENABLE);

ISR code:

void SPI2_IRQHandler(void)
{
tmp = SPI2->DR;
SPI2->DR = 0x08;
}

The result of that code is that sometimes 0x08 gets transmitted, most of the time 0x10 gets transmitted and rarely other 2^n values get transmitted. Logic analyzer sample:

http://imgur.com/W3xAFGo

Does anybody have any hints as to what I'm doing wrong? #stm32-f1-spi-problem
3 REPLIES 3
denny2
Associate II
Posted on February 05, 2016 at 08:53

In the GPIO configuration, � Think the line

    

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

looks suspicious. I'd remove that and try to see what happens.

/D

zhausq
Associate III
Posted on February 05, 2016 at 12:44

Tried it, didn't have any effect.

zhausq
Associate III
Posted on February 06, 2016 at 00:04

Solved, wiring problem. Fell free to delete this thread.