cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4DISCOVERY SPI Interface with Gyro problem

randyao12345
Associate II
Posted on June 12, 2014 at 01:54

Hi ST,

I've run into a brick wall trying to interface my STM32F4 Discovery board to a SD740 sensor using SPI. 

I'm having trouble writing the SPI1_send function, and getting to work. (I hope my SPI1_init is correct).

Would appreciate if anyone can tell me how to pull the chip select register low, and how to receive incoming data. Working code as example would be highly appreciated :)

Thanks in advance.

/* 06/02/14

 * PA4 SPI SEL

 * PA5 SCLK

 * PA6 MISO

 * PA7 MOSI (output data here)

 * APB2 CLK 42Mhz?

 * At time of testing - SCLK was 124.8KHz

 * MOSI sends 10101010 at 121.9KHz

 * SEL pulls low when

Addr 70 check standby register last bit 0 -> normal bit 1-> standby

GND -> AMODE, STBY, GND

VCC -> 3.3V*/

//Retest 06/09/14 with SD740 Accelerometer

#define standbyaddr2 (0x80 | 70) //0b 1100 0110

#define datareg (0x80 | 0x00)

#include <stm32f4xx.h>

#include <stm32f4xx_spi.h>

#include <stm32f4xx_gpio.h>

#include <stm32f4xx_rcc.h>

#include <semihosting.h>

uint8_t received;

uint8_t i = 0;

void init_SPI1(void);

uint8_t SPI1_send(uint8_t data);

int main(void){

init_SPI1();

while(1){

received = SPI1_send(standbyaddr2);

printf(''\nValue: %d'',received);

}

}

void init_SPI1(void){

GPIO_InitTypeDef gpio1;

SPI_InitTypeDef SPI_InitStruct;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

gpio1.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5 | GPIO_Pin_4;

gpio1.GPIO_Mode = GPIO_Mode_AF;

gpio1.GPIO_OType = GPIO_OType_PP;

gpio1.GPIO_Speed = GPIO_Speed_50MHz;

gpio1.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_Init(GPIOA, &gpio1);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_SPI1); //Configure PA4 as SPI1_NSS Chip select

GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1); //Configure PA5 as SCLK - SCLK

GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1); //Configure PA6 as MISO - Master in Slave out

GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1); //Configure PA7 as MOSI - Master out Slave in -> output data here

SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO lines

SPI_InitStruct.SPI_Mode = SPI_Mode_Master;     // transmit in master mode, NSS pin has to be always high

SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // one packet of data is 8 bits wide

SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;        // clock is low when idle

SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;      // data sampled at first edge

SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set; // set the NSS management to internal and pull internal NSS high

SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128; // SPI frequency is APB2 frequency / 256 = ~121kHz

SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first

SPI_Init(SPI1, &SPI_InitStruct);

SPI_Cmd(SPI1, ENABLE); // enable SPI1

}

uint8_t SPI1_send(uint8_t data){

SPI1->DR = data; // write data to be transmitted to the SPI data register

while( !(SPI1->SR & SPI_I2S_FLAG_TXE) ); // wait until transmit complete

while( !(SPI1->SR & SPI_I2S_FLAG_RXNE) ); // wait until receive complete

while( SPI1->SR & SPI_I2S_FLAG_BSY ); // wait until SPI is not busy anymore

return SPI1->DR; // return received data from SPI data register

}
0 REPLIES 0