AnsweredAssumed Answered

stm32f3discovery - l3gd20 gyroscope send constant values

Question asked by tatara.leszek on Sep 8, 2015
Latest reply on Sep 8, 2015 by tatara.leszek
Hello,
I have problem with l3gd20 gyroscope, which is part of stm32f3disco. I try to communicate with it using built in stm32f3 uC. I wrote my library for using SPI and this gyroscope. Theoretically SPI is working correctly, because I receive proper value of WHO_AM_I register, but when I try to read measurement registers (OUT_X_L etc.), the values of register dont change - they are constant even when I'm moving the board. They can only change when I turn off and turn on the power, but  then I am reading new constant values. I was trying to run my application on three different stm32f3 disco - the results were the same.
I'm doing simple configuration of l3gd20 - I write 
CTRL_REG1_PD | CTRL_REG1_XEN | CTRL_REG1_YEN | CTRL_REG1_ZEN
to CTRL_REG1.
I really don't know where is the problem - do I configure it wrong? Or is my configuration of SPI partially wrong? Below is source code of my SPI configuration. I would be grateful for your help.
01.void l3gd20_conf() {
02.    /* włącz zegary */
03.    RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
04.    RCC->AHBENR |= RCC_AHBENR_GPIOEEN;
05.    RCC->APB2ENR |= RCC_APB2ENR_SPI1EN;
06. 
07. 
08.    /* ustaw alternatywne funkcje dla GPIO - SCK, MISO, MOSI */
09.    GPIOA->MODER |= GPIO_MODER_MODER5_1 | GPIO_MODER_MODER6_1 | GPIO_MODER_MODER7_1;
10.    GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR5 | GPIO_OSPEEDER_OSPEEDR6 | GPIO_OSPEEDER_OSPEEDR7;
11.    GPIOA->AFR[0] |= GPIO_AFRL_AF5_5 | GPIO_AFRL_AF5_6 | GPIO_AFRL_AF5_7;
12. 
13.    /* ustaw PE3 jako CS - GPIO output */
14.    GPIOE->MODER |= GPIO_MODER_MODER3_0;
15.    GPIOE->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR3;
16.    GPIOE->ODR |= GPIO_ODR_3;
17. 
18.    /* ustaw SPI MASTER 2,25 MHz; ramka 8 bitow */
19.    SPI1->CR1 |= SPI_CR1_SSM | SPI_CR1_SSI | SPI_CR1_MSTR |SPI_CR1_BR_1 | SPI_CR1_BR_0;
20.    SPI1->CR2 |= SPI_CR2_FRXTH;
21.    /* wlacz SPI */
22.    SPI1->CR1 |= SPI_CR1_SPE;
23.}
24. 
25.void l3gd20_send_byte(uint8_t data) {
26.    volatile uint8_t * const ptr = (volatile uint8_t *)&SPI1->DR;
27.    while(!(SPI1->SR & SPI_SR_TXE));
28.    *ptr = data;
29.}
30./*
31.uint8_t l3gd20_transfer_byte(uint8_t mode, uint8_t addr, uint8_t data) {
32.    uint8_t buf;
33.     
34.    CS_LOW();
35.    if (mode == 1) {
36.        addr |= 0x80;
37.        l3gd20_send_byte(addr);
38.        l3gd20_send_byte(DUMMY_BYTE);
39.        while (!(SPI1->SR & SPI_SR_RXNE));
40.        buf = (uint8_t)SPI1->DR;
41.        return buf;
42.    } else {
43.        l3gd20_send_byte(addr);
44.        l3gd20_send_byte(data);
45.        return 0;
46.        }
47.    CS_HIGH();
48.}*/
49.uint8_t l3gd20_transfer_byte(uint8_t val) {
50.     
51.    /*while (!(SPI1->SR & SPI_SR_TXE));
52.    SPI1->DR = val;
53.    while (!(SPI1->SR & SPI_SR_RXNE));
54.    return (uint8_t)SPI1->DR;*/
55.    volatile uint8_t * const ptr = (volatile uint8_t *)&SPI1->DR;
56.    while(!(SPI1->SR & SPI_SR_TXE));
57.    *ptr = val;
58.    while(!(SPI1->SR & SPI_SR_RXNE));
59.    return *ptr;
60.}
61. 
62.void l3gd20_write_byte(uint8_t addr, uint8_t val) {
63.    uint8_t d;
64.    CS_LOW();
65.    d = l3gd20_transfer_byte(addr);
66.    d = l3gd20_transfer_byte(val);
67.    CS_HIGH();
68.}
69. 
70.uint8_t l3gd20_read_byte(uint8_t addr) {
71.    volatile uint8_t val;
72.    addr |= R_BIT;
73.    CS_LOW();
74.    val = l3gd20_transfer_byte(addr);
75.    val = l3gd20_transfer_byte(DUMMY_BYTE);
76.    CS_HIGH();
77.    return val;
78.}

Outcomes