Skip to main content
jipeng
Associate II
March 18, 2015
Question

stm32f3discovery read gyro acc mag data take long time

  • March 18, 2015
  • 3 replies
  • 784 views
Posted on March 18, 2015 at 05:47

I have a stm32f3discovery board,I want read the sensor's data, I use the driver in demo, but it take about 700 mseconds to get it, is this normal? but i want to handle these data under 20Hz.

      seconds0 = millis();

          seconds = seconds0 - seconds1;

          if (seconds >= 5) {

           sprintf(output, ''seconds is %d'', seconds);

           uartPrint(output);

            GyroReadAngRate(gyro);

            CompassReadMag(mag);

            CompassReadAcc(acc);

          seconds1 = seconds0;

          }
    This topic has been closed for replies.

    3 replies

    AvaTar
    Senior III
    March 18, 2015
    Posted on March 18, 2015 at 11:48

      seconds0 = millis();

     

    This is a semantic contradiction.  Don't know this millis() function.

      seconds = seconds0 - seconds1;

    Are you sure 'seconds1' is initialised at this point ?

      uartPrint(output);

     

    Is this call blocking ?

    I want read the sensor's data, I use the driver in demo, but it take about 700 mseconds to get it, is this normal ?

     

    No.

    Your loop my run with this cycle time, but the sensor read-out is definitely faster.

    Either add GPIO toggles and use a scope, or configure a timer unit to measure the runtime of those calls (only sensor read funtions, NOT including opaque stuff like uartPrint(). Or use the runtime measurement facilities of your IDE/Debugger, if present.

    jipeng
    jipengAuthor
    Associate II
    March 25, 2015
    Posted on March 25, 2015 at 02:58

    thx, I checked my code,

                GyroReadAngRate(gyro1);

                CompassReadMag(mag1);

                CompassReadAcc(acc1);

    this three line run in 5 milliseconds.

    and

                  sprintf(output, ''e0:%f e1:%f e2:%f\r\n'', euler[0], euler[1], euler[2]);

                  uartPrint(output);

    run in about 40 milliseconds.

    how to improve usart's performance.below is my usart code:

    /*

     * usart.c

     *

     *  Created on: 2014å¹´8月12æ—¥

     *      Author: sunjipeng

     */

    #include ''stdio.h''

    #include ''usart.h''

    #include ''stm32f30x_usart.h''

    void USART1_Configuration(void)

    {

        /* Enable GPIO clock */

          RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

          /* Enable USART clock */

          RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

      USART_InitTypeDef USART_InitStructure;

      GPIO_InitTypeDef GPIO_InitStructure;

      /************************************************************************************/

      /*for usart1*/

      /* Configure USART Tx as alternate function push-pull */

      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

    //  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

      GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

      GPIO_Init(GPIOA, &GPIO_InitStructure);

    //  GPIO_Init(GPIOB, &GPIO_InitStructure);

      /* Configure USART Rx as alternate function push-pull */

      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

    //  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;

      GPIO_Init(GPIOA, &GPIO_InitStructure);

    //  GPIO_Init(GPIOB, &GPIO_InitStructure);

      /* Connect PA9 to USART1_Tx */

      GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);

    //  GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_7);

      /* Connect PA10 to USART1_Rx */

      GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);

    //  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_7);

      /* USART resources configuration (Clock, GPIO pins and USART registers) ----*/

      /* USART configured as follow:

            - BaudRate = 115200 baud

            - Word Length = 8 Bits

            - One Stop Bit

            - No parity

            - Hardware flow control disabled (RTS and CTS signals)

            - Receive and transmit enabled

      */

      USART_InitStructure.USART_BaudRate = 9600;

      USART_InitStructure.USART_WordLength = USART_WordLength_8b;

      USART_InitStructure.USART_StopBits = USART_StopBits_1;

      USART_InitStructure.USART_Parity = USART_Parity_No;

      USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

      USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

      /* USART configuration */

      USART_Init(USART1, &USART_InitStructure);

      /* Enable USART */

      USART_Cmd(USART1, ENABLE);

    }

    int USART1_PutChar(char ch)

    {

        USART_SendData(USART1, ch); // Send 'I'

        while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait for Empty

        return ch;

    }

    //�定义fputc函数

    int fputc(int ch, FILE *f)

    {

        USART1_PutChar(ch);

      return ch;

    }

    void uartPrint(char *str)

    {

        while (*str){

            USART1_PutChar(*(str++));

    //        do {

    //            USART_SendData(USART1, *(str++));

    //        }while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait for Empty

    //        // Send 'I'

        }

    }

    AvaTar
    Senior III
    March 25, 2015
    Posted on March 25, 2015 at 08:52

        sprintf(output, ''e0:%f e1:%f e2:%f\r\n'', euler[0], euler[1], euler[2]);

     

        uartPrint(output);

     

    run in about 40 milliseconds.

     

    how to improve usart's performance.below is my usart code:

     

    By not busy-waiting on individual characters, as done here:

        USART_SendData(USART1, ch); // Send 'I'

     

        while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait for Empty

     

    You could use the TXE interrupt instead, or feed the UART per DMA.

    However, this does not eliminate your problem. You still produce your send data in a much faster rate than you can send it, at least with your current baudrate setting (9600).