2016-02-04 07:13 PM
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:
Does anybody have any hints as to what I'm doing wrong?
#stm32-f1-spi-problem
2016-02-04 11:53 PM
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. /D2016-02-05 03:44 AM
Tried it, didn't have any effect.
2016-02-05 03:04 PM
Solved, wiring problem. Fell free to delete this thread.