cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F3, SPI and L3GD20 communication problem

maciek
Associate II
Posted on July 12, 2014 at 13:25

Hay! That's me with a problem, again! I am trying to communicate my gyro sensor on my discovery board via SPI interface. Firstly, as always, i was trying to do this all by myself with reference manual, but i failed. I checked for some help on the web and i found few libraries with code for the sensor. I changed my code, but still, i can't handle the transmission. I have no access to osciloscope at the moment so i can't check if there are any signals. Can you take a look on my code and tell me if everything's ok?

spi.c
#include ''stm32f30x.h''
#include ''spi.h''
void
SPI_setup()
{
RCC->AHBENR |= RCC_AHBENR_GPIOEEN; 
// Clock for GPIOE
RCC->AHBENR |= RCC_AHBENR_GPIOAEN; 
// Clock for GPIOA
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; 
// Clock for SPI
// CS=PE3 pin configuration as output, push-pull, 50 MHz //
GPIOE->MODER |= GPIO_MODER_MODER15_0 
// LED PE15 as output
| GPIO_MODER_MODER3_0 
// PE3, CS pin as output
| GPIO_MODER_MODER14_0; 
// LED PE14 as output
GPIOE->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR3; 
// PE3 50 MHz speed
// END CS configuration
// MISO/MOSI/SCK=PA6/PA7/PA5 pin configuration as (MISO) input floating, (MOSI) alternate function pull-up, (SCK) alternate function pull-up
GPIOA->MODER |= GPIO_MODER_MODER5_1 
// PA5, SCK alternate function, push-pull
| GPIO_MODER_MODER7_1; 
// PA7, MOSI alternate function, push-pull
// PA6, MISO input floating (no pull-up, no pull down)
GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR5 
// PA5 50 MHz speed
| GPIO_OSPEEDER_OSPEEDR7; 
// PA7 50 MHz speed 
GPIOA->AFR[0] |= (GPIO_AFRL_AFRL5 & (5<<4*5)) 
// PA5, SCK AF5
| (GPIO_AFRL_AFRL7 & (5<<4*7)); 
// PA7, MOSI AF5
// END MISO/MOSI/SCK configuration
// SPI configuration
SPI1->CR1 |= SPI_CR1_BR_0
| SPI_CR1_BR_1 
// Prescaler: 16, 72MHz/16= 4.5 MHz
| SPI_CR1_MSTR 
// Master mode
| SPI_CR1_SSM 
// CS software management
| SPI_CR1_SSI;
SPI1->CR2 |=SPI_CR2_DS_2 
| SPI_CR2_DS_1
| SPI_CR2_DS_0; 
// 8 bit data size
SPI1->CR2 |= SPI_CR2_FRXTH; 
// RX interrupt is generated when FIFO is >= 8 bit
SPI1->CR1 |= SPI_CR1_SPE; 
// SPI enable
CS_HIGH;
}
uint8_t SPI_send(uint8_t 
byte
, uint8_t mode, uint8_t continous)
{
uint8_t rx_data;
if
(mode)
byte
|= 0x80;
CS_LOW;
while
(!(SPI1->SR & SPI_SR_TXE));
SPI1->DR = 
byte
;
while
(!(SPI1->SR & SPI_SR_RXNE));
rx_data=SPI1->DR;
CS_HIGH;
return
rx_data;
}
spi.h
#ifndef _SPI_H
#define _SPI_H
#define CS_LOW GPIOE->BSRR |= GPIO_BSRR_BR_3
#define CS_HIGH GPIOE->BSRR |= GPIO_BSRR_BS_3
#define RW 1
#define W 0
#define cont 1
#define ncont 0
void
SPI_setup(
void
); 
// Setups transmission
uint8_t SPI_send(uint8_t, uint8_t, uint8_t); 
// Byte: byte to send, mode: RW- write-read, W- only write, cont: continous transfer
#endif
main.c
#include ''stm32f30x.h''
#include ''spi.h''
#include ''usart_init.h''
#include <string.h>
void
delay (
int
i)
{
for
(;i>0;i--);
}
int
main ()
{
char
tab [8] = {0};
uint8_t a;
USART_setup();
SPI_setup();
while
(1)
{
a=SPI_send(0x0F, RW, ncont);
sprintf(tab, 
''%d''
, a);
USART_send(tab);
delay(10000000);
}
}

According to gyro's datasheet i should receive 0b11010100 data- all i get is 0.
1 REPLY 1
maciek
Associate II
Posted on July 12, 2014 at 20:27

Everything's fine now- i made a mistake in MISO configuration and i didnt send dummy byte while receiving msg.