2014-01-11 10:43 PM
Hi, I just received my STM32F429 discovery board and I tried to implement basic SPI but I'm stumped as to why the logic analyzer output was different. Can anybody point out were I was wrong. Here is the code.
#include <stm32f4xx.h>
#include <stm32f4xx_gpio.h>
#include <stm32f4xx_rcc.h>
#include <stm32f4xx_spi.h>
void
spi1_init(
void
);
void
spiSend(uint8_t data);
uint8_t spiRead(
void
);
void
csInit(
void
);
uint8_t txBuffer[4] = {0x00, 0x01, 0x02, 0x03};
uint8_t rxBuffer[4];
void
Delay(__IO uint32_t nCount){
while
(nCount--)
{
}
}
int
main(
void
){
int
i,j;
csInit();
spi1_init();
for
(i = 0; i < 4; i++){
GPIO_WriteBit(GPIOE, GPIO_Pin_3, 0);
spiSend(txBuffer[i]);
rxBuffer[i] = spiRead();
GPIO_WriteBit(GPIOE, GPIO_Pin_3, 1);
}
while
(1){
}
}
void
csInit(
void
){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
/* Configure chip select pin to PE3 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_WriteBit(GPIOE, GPIO_Pin_3, 1);
/* Set chip select high */
}
void
spi1_init(
void
){
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/* Configure GPIO pins for SPI1
* PA5 = SCK
* PA6 = MISO
* PA7 = MOSI
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect SPI1 pins to SPI alternate function */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
/* Configure SPI1 */
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_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
void
spiSend(uint8_t data){
while
(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE));
SPI_I2S_SendData(SPI1, data);
}
uint8_t spiRead(
void
){
while
(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
return
SPI_I2S_ReceiveData(SPI1);
}
#ifdef USE_FULL_ASSERT
void
assert_failed(uint8_t * file, uint32_t line){
while
(1)
{
}
}
#endif
And here is the output of the logic analyzer.
Thank you very much!
#discovery #stm32f429 #spi
2014-01-12 04:24 AM
I might start by initializing PE3 properly. Tip: Don't enable line numbers unless you specifically use them to refer to something.
void csInit(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
/* Configure chip select pin to PE3 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_WriteBit(GPIOE, GPIO_Pin_3, 1); /* Set chip select high */
}
2014-01-12 08:20 AM
Thank you very much clive1! So now I properly configured the select pin. But I now got the following result from the logic analyzer...it seems the values that are sent are being doubled. What have I done wrong now?
Thank you.2014-01-12 09:23 AM
Perhaps because the analyzer chooses to use the falling edge of the clock, and not the rising edge?
2014-01-12 03:02 PM
I think the logic analyzer trigger as first clock pulse the 'glitch' generated between GPIO_Init() and GPIOA_Config() so the data are shifted left by 1 bit.
Try to set all used GPIO pins to 1 before to call GPIO_Init() either in csInit() and spi1Init() using GPIO_Setbits() or GPIO_Write() so when you initialize the port in output mode, the respective bits in ODR are already set.2014-01-18 07:27 AM
Hello
a.rocchegiani! I did what you suggested and still got the same output.
Would you think that using a different operating system might affect my output? Since I rolled my own toolchain, I'm beginning to think that I might have done something wrong in the configuration of the toolchain that I am using. I'll try to compile this code again using Keil and see if I still get the same output. Thanks for the tips and suggestions.