Timer end time
응답
19. 7. 30 오후 12:07

Hey everyone. I wrote the following code to get the accelerometer data. I used a timer function there. Caan someone please tell me how to write it in a way so the xdk only gives the accelerometer data for 5 seconds or any other fixed time.

#include "XdkAppInfo.h"

#undef BCDS_MODULE_ID  // [i] Module ID define before including Basics package

#define BCDS_MODULE_ID XDK_APP_MODULE_ID_APP_CONTROLLER

#include "AppController.h"

#include <stdio.h>

#include "BCDS_CmdProcessor.h"

#include "FreeRTOS.h"

#include "XdkSensorHandle.h"

#include "timers.h"

 

/* --------------------------------------------------------------------------- |

 * HANDLES ******************************************************************* |

 * -------------------------------------------------------------------------- */

 

static CmdProcessor_T * AppCmdProcessor;

xTimerHandle accelerometerHandle = NULL;

 

/* --------------------------------------------------------------------------- |

 * EXECUTING FUNCTIONS ******************************************************* |

 * -------------------------------------------------------------------------- */

 

 static void readAccelerometer(xTimerHandle xTimer)

 {

     (void) xTimer;

 

     Retcode_T returnValue = RETCODE_FAILURE;

 

     /* read and print BMA280 accelerometer data */

 

     Accelerometer_XyzData_T bma280 = {INT32_C(0), INT32_C(0), INT32_C(0)};

     memset(&bma280, 0, sizeof(CalibratedAccel_XyzMps2Data_T));

 

     returnValue = Accelerometer_readXyzGValue(xdkAccelerometers_BMA280_Handle,&bma280);

 

     if (RETCODE_OK == returnValue) {

         printf("BMA280 Acceleration Data - M/S2 %f m/s2 %f m/s2 %f m/s2\n\r",

             (float) bma280.xAxisData, (float) bma280.yAxisData, (float) bma280.zAxisData);

     }

 }

 

static void initAccelerometer(void)

{

    Retcode_T returnValue = RETCODE_FAILURE;

    Retcode_T returnBandwidthValue = RETCODE_FAILURE;

    Retcode_T returnRangeValue = RETCODE_FAILURE;

 

    /* initialize accelerometer */

 

    returnValue = Accelerometer_init(xdkAccelerometers_BMA280_Handle);

 

    if ( RETCODE_OK != returnValue) {

        printf("BMA280 Accelerometer initialization failed\n\r");

    }

 

    returnBandwidthValue = Accelerometer_setBandwidth(xdkAccelerometers_BMA280_Handle,ACCELEROMETER_BMA280_BANDWIDTH_125HZ);

 

    if (RETCODE_OK != returnBandwidthValue) {

        printf("Configuring bandwidth failed \n\r");

    }

    returnRangeValue = Accelerometer_setRange(xdkAccelerometers_BMA280_Handle,ACCELEROMETER_BMA280_RANGE_2G);

 

    if (RETCODE_OK != returnRangeValue) {

        printf("Configuring range failed \n\r");

    }

}

 

 

/* --------------------------------------------------------------------------- |

 * BOOTING- AND SETUP FUNCTIONS ********************************************** |

 * -------------------------------------------------------------------------- */

 

 static void AppControllerEnable(void * param1, uint32_t param2)

 {

     BCDS_UNUSED(param1);

     BCDS_UNUSED(param2);

 

     uint32_t timerBlockTime = UINT32_MAX;

 

     xTimerStart(accelerometerHandle,timerBlockTime);

 }

 

 

 static void AppControllerSetup(void * param1, uint32_t param2)

 {

     BCDS_UNUSED(param1);

     BCDS_UNUSED(param2);

     Retcode_T retcode = RETCODE_OK;

 

     uint32_t oneMilisecondDelay = UINT32_C(1);

     uint32_t timerAutoReloadOn = UINT32_C(1);

 

     initAccelerometer();

 

     /* Creation and start of the timer task */

     accelerometerHandle = xTimerCreate((const char *) "readAcclerometer", oneMilisecondDelay,timerAutoReloadOn, NULL, readAccelerometer);

 

     retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerEnable, NULL, UINT32_C(0));

     if (RETCODE_OK != retcode)

     {

        printf("AppControllerSetup : Failed \r\n");

        Retcode_RaiseError(retcode);

        assert(0);

 

        /* To provide LED indication for the user */

     }

 }

 

 

 void AppController_Init(void * cmdProcessorHandle, uint32_t param2)

 {

     BCDS_UNUSED(param2);

 

     Retcode_T retcode = RETCODE_OK;

 

     if (cmdProcessorHandle == NULL)

     {

         printf("AppController_Init : Command processor handle is NULL \r\n");

         retcode = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_NULL_POINTER);

     }

     else

     {

         AppCmdProcessor = (CmdProcessor_T *) cmdProcessorHandle;

         retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerSetup, NULL, UINT32_C(0));

     }

 

     if (RETCODE_OK != retcode)

     {

         Retcode_RaiseError(retcode);

         assert(0); /* To provide LED indication for the user */

     }

 }

 

0 (0 투표)
RE: Timer end time
응답
19. 7. 30 오후 2:00 as a reply to Md Zakaria RAhman.

Hello Md Zakaria RAhman,

I quite did not get your requirement.

  1. Are you interested to sample accelerometer sensor data for every 5 seconds once ?  (or)
  2. Sample data in x sample rate till 5 seconds ?

 

0 (0 투표)
RE: Timer end time
응답
19. 7. 30 오후 7:21 as a reply to Padmapriya G.

Sample data till 5 seconds

thnx :)

0 (0 투표)
RE: Timer end time
응답
19. 7. 31 오전 7:21 as a reply to Md Zakaria RAhman.

Hello Md Zakaria RAhman,

Then you can assign "readAccelerometer" to timer function and configure the timer period to 5 seconds as

pdMS_TO_TICKS(5000)
0 (0 투표)
RE: Timer end time
응답
19. 7. 31 오전 8:57 as a reply to Padmapriya G.

I used the following timer in my code 

accelerometerHandle = xTimerCreate((const char *) "readAcclerometer", oneMilisecondDelay,timerAutoReloadOn, NULL, readAccelerometer);

can you please tell me where to add 

pdMS_TO_TICKS(5000) in the above code
0 (0 투표)
RE: Timer end time
응답
19. 8. 7 오전 8:20 as a reply to Md Zakaria RAhman.

Hello,

accelerometerHandle = xTimerCreate((const char *) "readAcclerometer", pdMS_TO_TICKS(5000) ,timerAutoReloadOn, NULL, readAccelerometer);

 

0 (0 투표)
RE: Timer end time
응답
19. 8. 13 오후 2:38 as a reply to Padmapriya G.

Hi, thank you for your response. I used a task delay function of 5 sec then called the function xTimerStop. So the timer function runs for 5 seconds and then stops. The problem is it only works when I set the time period of the timer function to 3ms or more. But I want to get the accelerometer data for every miliseconds. But when I change the time period of the timer function to 1ms the timer never stops. 

Thank you 

0 (0 투표)
RE: Timer end time
응답
19. 8. 14 오후 1:35 as a reply to Md Zakaria RAhman.
I was having the same problem. What you can do is suspend the task that the timer is calling. Like the following.

xTimerStart(applicationTimer,TIMERBLOCKTIME);
vTaskDelay(5000);
xTimerStop(applicationTimer,TIMERBLOCKTIME);
vTaskSuspend(readAccelerometer);

N.B. 
readAccelerometer is the function that the timer task is calling. 
0 (0 투표)
RE: Timer end time
응답
19. 8. 15 오전 8:49 as a reply to Ankit Chanda.
Thank you, I tried doing that but still the timer does not stop 
0 (0 투표)