cancel
Showing results for 
Search instead for 
Did you mean: 

Cannot activate 8 bit spi

fallos
Associate
Posted on May 29, 2013 at 23:42

Hi. I configured my STM32F3-Discovery SPI link to 8 bit data frame but no matter if I send 1 or 2 bytes through it, oscilloscope anyway registers 2 bytes being sent.

here is the code:

uint8_t readbyte(void){
GPIO_WriteBit(GPIOD, GPIO_Pin_8, Bit_RESET);
while((SPI1->SR & SPI_I2S_FLAG_TXE) == RESET);
SPI2->DR = 0xDE;
while((SPI1->SR & SPI_I2S_FLAG_RXNE) == RESET);
GPIO_WriteBit(GPIOD, GPIO_Pin_8, Bit_SET);
return SPI1->DR;
}
...
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_256;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(&SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
...
while (1){
uint8_t u8 = readbyte();
Delay(1);
}

This happens when I send ''0xDE'' value:

http://i.imgur.com/jyfaRQc.png

This happens when I send ''0xDEAD'' value: http://i.imgur.com/LY3VrfR.png As you can see it sends 16 bits of data regardless of the SPI_DataSize setting. I think this may be a bug of stdperiph lib? How do I fix this?
2 REPLIES 2
Posted on May 30, 2013 at 01:30

I think this may be a bug of stdperiph lib? How do I fix this?

As convenient as that might be, I suspect it relates to user error and SPI2 vs SPI1, decide which interface you're talking too.

uint8_t readbyte(void){
GPIO_WriteBit(GPIOD, GPIO_Pin_8, Bit_RESET);
while((SPI1->SR & SPI_I2S_FLAG_TXE) == RESET);
SPI2->DR = 0xDE; // << 
SPI2
? REALLY
while((SPI1->SR & SPI_I2S_FLAG_RXNE) == RESET);
GPIO_WriteBit(GPIOD, GPIO_Pin_8, Bit_SET);
return SPI1->DR;
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
fallospenis
Associate II
Posted on May 30, 2013 at 05:16

Thank you for your answer.

Ha-ha! No, of course I have same SPI2 in all my code. I dont know why I decided to change all this code to SPI1 just to post it here. I found a way to write exactly 8 bits:

GPIO_WriteBit(GPIOD, GPIO_Pin_8, Bit_RESET);
int i;
while((SPI2->SR & SPI_I2S_FLAG_TXE) == RESET);
uint32_t spixbase = 0x00;
spixbase = (uint32_t)SPI2;
spixbase += 0x0C;
*(__IO uint8_t *) spixbase = 0xDE; //this is 1 byte I wanna send
//while((SPI2->SR & SPI_I2S_FLAG_RXNE) == RESET);
for(i = 0; i < 
350
; i++);
GPIO_WriteBit(GPIOD, GPIO_Pin_8, Bit_SET);
return SPI2->DR;

But that waywhile((SPI2->SR & SPI_I2S_FLAG_RXNE) == RESET); does not work (infinite looping) and I made a for loop to skip some clocks. It works now~ Is there any way to use flag_rxne with this code? Usual SPI2->DR = x sets this flag. PS This website for some reason does not allow me to login under this username for a while ago (and thats because the second account was made) but now there is all right.