AnsweredAssumed Answered

stm32f0 spi l3gd20

Question asked by wnu71 on Aug 21, 2015
Hay!I'm new in programmin stm32 and I have a problem with gyro on board stm32f0. I get only 0 or 255 walue. I don't know what is wrong, maybe configuration spi ? 
Someone know what is a problem ?

This is my code:

#include "stm32f0xx_conf.h"
#include <stdint.h>
#include <stdio.h>
#include "STM32F0xx.h"


#define CS_LOW GPIOA->BSRR |= GPIO_BSRR_BR_3
#define CS_HIGH GPIOA->BSRR |= GPIO_BSRR_BS_3


#define RW 1
#define W 0


#define cont 1
#define ncont 0
#define DUMMY_BYTE      0xFF




void SPI_conf(void);
void Port_Init(void);
void USART1_Init(void);


void TIM14_Init(uint32_t PRESCALER, uint32_t ARR);
void TIM14_IRQHandler(void);


void delay_ms(uint32_t delay);
void delay(uint32_t t);


void send_byte(char dana);
void send(char data[8]);
void send_num(uint8_t cyfra);
uint8_t SPI_send(uint8_t byte, uint8_t mode, uint8_t continous);






volatile static uint32_t ms;


int main(void)
{
    Port_Init();
    USART1_Init();
    TIM14_Init(480,100);
    SPI_conf();


  //  char tab [5] = {0};
    uint8_t a;
    uint8_t b;
    GPIOC->ODR |= (1UL<<7);
   delay_ms(500);
    GPIOC->ODR &= ~(1UL<<7);
    send("Start");


  while(1)
  {
        a=SPI_send(0x28, RW, ncont);
        b=SPI_send(0x29, RW, ncont);
       // sprintf(tab, "%d", a);
        send_byte('x');
        //send(tab);
        send_num(a);
        send_num(b);
        a=SPI_send(0x2A, RW, ncont);
        b=SPI_send(0x2B, RW, ncont);
        //sprintf(tab, "%d", a);
        send_byte('y');
        //send(tab);
        send_num(a);
        send_num(b);
        a=SPI_send(0x2C, RW, ncont);
        b=SPI_send(0x2D, RW, ncont);
        //sprintf(tab, "%d", a);
        send_byte('z');
        //send(tab);
        send_num(a);
        send_num(b);


        delay_ms(1000);
  }
}


void SPI_conf(void)
{
    RCC->APB2ENR |= RCC_APB2ENR_SPI1EN;// zegar dla spi
//    RCC->AHBENR |= RCC_AHBENR_GPIOAEN;// zegar dla gpio


    GPIOA->MODER |= (GPIO_MODER_MODER4_0 | GPIO_MODER_MODER5_1 | GPIO_MODER_MODER6_1 | GPIO_MODER_MODER7_1); //GPIO jako fun alternatywna CS(PA4)-output SCK(PA5)-alter, MISO(PA6)-alter, MOSI(PA7)-alter
    GPIOA->OSPEEDR |= (GPIO_OSPEEDR_OSPEEDR4 |GPIO_OSPEEDR_OSPEEDR5| GPIO_OSPEEDR_OSPEEDR6 | GPIO_OSPEEDR_OSPEEDR7);
    GPIOA->AFR[0] &= ~(GPIO_AFRL_AFR5 | GPIO_AFRL_AFR6 | GPIO_AFRL_AFR7); // funkcja alternatywna jako SPI1


    //spi config
    SPI1->CR1 |= (SPI_CR1_BR_0 |SPI_CR1_BR_1 | SPI_CR1_MSTR );//  f/8 // TRYB MASTER
    SPI1->CR2 |= (SPI_CR2_DS_0 |SPI_CR2_DS_1 |SPI_CR2_DS_2); // 8 BITÓW
    SPI1->CR1 |= (SPI_CR1_SSM | SPI_CR1_SSI);


    //SPI1->CR2 |= (SPI_CR2_TXEIE | SPI_CR2_RXNEIE );
    SPI1->CR2 |= SPI_CR2_FRXTH;


    //SPI1->CRCPR |= 7; //SPI_CRCPR_CRCPOLY
    SPI1->CR1 |=SPI_CR1_SPE;
   // CS_HIGH;
}
void USART1_Init(void)
{
    // PORTY USART
    RCC-> AHBENR |=RCC_AHBENR_GPIOAEN;
    GPIOA->MODER |= (GPIO_MODER_MODER9_1 | GPIO_MODER_MODER10_1);
    GPIOA->OTYPER |= (GPIO_OTYPER_OT_9 | GPIO_OTYPER_OT_10);
    //GPIOA->PUPDR|=(GPIO_PUPDR_PUPDR10 |GPIO_PUPDR_PUPDR9);
    GPIOA->OSPEEDR |= (GPIO_OSPEEDR_OSPEEDR9_0 | GPIO_OSPEEDR_OSPEEDR10_0);
    //GPIOA-> AFR[1]|= (GPIO_AFRH_AFR9 | GPIO_AFRH_AFR10);
    GPIOA->AFR[1] |= 1<<4 | 1<<8; //alternate function AF1 for GPIO PA9 PA10 (USART TX RX)


    //KONFIGURACJA
    RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
    USART1->BRR = 48000000/9600;
    USART1->CR1 |= USART_CR1_UE;
    USART1->CR1 |= (USART_CR1_TE  | USART_CR1_RE | USART_CR1_RXNEIE | USART_CR1_CMIE);
    //USART1->CR1 |= USART_CR1_M;


    //USART1->CR1 |= USART_CR1_PS;
    //USART1->CR2 |= USART_CR2_STOP_0;
    NVIC_EnableIRQ(USART1_IRQn);


}
void Port_Init(void)
{
    RCC->AHBENR |= RCC_AHBENR_GPIOCEN;
    GPIOC->MODER |= (GPIO_MODER_MODER7_0);
    GPIOC->OSPEEDR |= (GPIO_OSPEEDR_OSPEEDR7_1 );


    GPIOC->MODER |= (GPIO_MODER_MODER6_0);
    GPIOC->OSPEEDR |= (GPIO_OSPEEDR_OSPEEDR6_1 );


    GPIOC->MODER |= (GPIO_MODER_MODER8_0);
    GPIOC->OSPEEDR |= (GPIO_OSPEEDR_OSPEEDR8_1 );


    GPIOC->MODER |= (GPIO_MODER_MODER9_0);
    GPIOC->OSPEEDR |= (GPIO_OSPEEDR_OSPEEDR9_1 );




    RCC->AHBENR |=RCC_AHBENR_GPIOAEN;
    //GPIOA->MODER |=(GPIO_MODER_MODER1_1);
    GPIOA-> OSPEEDR |= (GPIO_OSPEEDR_OSPEEDR1_1);
    GPIOA-> PUPDR |=(GPIO_PUPDR_PUPDR1_0);
}
void TIM14_Init(uint32_t PRESCALER, uint32_t ARR)
{
    RCC->APB1ENR |= RCC_APB1ENR_TIM14EN;
    TIM14->PSC = PRESCALER - 1; //PRESKALER
    TIM14->ARR = ARR - 1;
    TIM14->DIER |= TIM_DIER_UIE;
    TIM14->CR1 |= TIM_CR1_ARPE | TIM_CR1_CEN;


}


void TIM14_IRQHandler (void)
{
    TIM14->SR = ~TIM_SR_UIF;
    ms++;
}
/*void delay(uint32_t t)
{   for(t;t>0;t--);
}*/
void send_byte(char dana)
{
   while(!(USART1->ISR & USART_FLAG_TXE));
    USART1->TDR = dana;
}
void send(char data[8])
{  uint8_t i=0;
    while(i<8){
    send_byte(data[i]);
    i++;
    }
    i=0;
}
void delay_ms(uint32_t delay)
{
    NVIC_EnableIRQ(TIM14_IRQn);
    ms = 0;
    while (ms < delay);
    NVIC_DisableIRQ(TIM14_IRQn);
}
void send_num(uint8_t cyfra)
{
   while(!(USART1->ISR & USART_FLAG_TXE));
    USART1->TDR = cyfra;
}
void USART1_IRQHandler(void)
{
    if ((USART1->ISR & USART_ISR_CMF) == USART_ISR_CMF)
        {
          USART1->ICR |= USART_ICR_CMCF;     //clear Compare Match Flag (CMF)
          GPIOC->ODR ^= GPIO_ODR_9;                    //non atomic toggle bit C9
        }
     USART1->TDR = USART1->RDR;     //echo CMF sign
}


uint8_t SPI_send(uint8_t byte, uint8_t mode, uint8_t continous)
{
    uint8_t rx_data;


    if (mode)
        //byte |= 0x80;


    CS_LOW;
    /*volatile uint8_t * const ptr = (volatile uint8_t *)&SPI1->DR;
    while(!(SPI1->SR & SPI_SR_TXE));
   *ptr = rx_data;
    while(!(SPI1->SR & SPI_SR_RXNE));


    return *ptr;
    */
    while(!(SPI1->SR & SPI_SR_TXE));
    SPI1->DR =DUMMY_BYTE ;
    SPI1->DR = byte;
    while(!(SPI1->SR & SPI_SR_RXNE));
    rx_data=SPI1->DR;


    CS_HIGH;


    return rx_data;




}


Outcomes