Параметр обратного вызова в программном таймере freeRTOS stm32 - PullRequest
0 голосов
/ 13 февраля 2019

Я пытаюсь использовать программный таймер с интеграцией freeRTOS cubeMx (в основном это freeRTOS с почти прозрачным слоем выше).Я думал, что смогу передать указатель на структуру в качестве параметра таймера и получить его в качестве параметра в функции обратного вызова.Примерно так:

typedef struct{
    uint8_t a;
    uint8_t b;
    uint8_t c;
}T;

T t = {1, 2, 3};

osTimerDef(myTimer01, Callback01);
myTimer01Handle = osTimerCreate(osTimer(myTimer01), osTimerPeriodic, (void*) &t);
osTimerStart(myTimer01Handle, 5000);

обратный вызов:

void Callback01(void const * argument)
{
    T* a = argument;
}

К сожалению, аргумент не указывает на тот же адрес, что и & t.Когда я смотрю на код freeRTOS, оказывается, что библиотека передает структуру «Timer_t», приведенную как void *, в функцию обратного вызова (см. Конец кода ниже):

static void prvProcessExpiredTimer( const TickType_t xNextExpireTime, const TickType_t xTimeNow )
{
BaseType_t xResult;
Timer_t * const pxTimer = ( Timer_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );

    /* Remove the timer from the list of active timers.  A check has already
    been performed to ensure the list is not empty. */
    ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
    traceTIMER_EXPIRED( pxTimer );

    /* If the timer is an auto reload timer then calculate the next
    expiry time and re-insert the timer in the list of active timers. */
    if( pxTimer->uxAutoReload == ( UBaseType_t ) pdTRUE )
    {
        /* The timer is inserted into a list using a time relative to anything
        other than the current time.  It will therefore be inserted into the
        correct list relative to the time this task thinks it is now. */
        if( prvInsertTimerInActiveList( pxTimer, ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ), xTimeNow, xNextExpireTime ) != pdFALSE )
        {
            /* The timer expired before it was added to the active timer
            list.  Reload it now.  */
            xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START_DONT_TRACE, xNextExpireTime, NULL, tmrNO_DELAY );
            configASSERT( xResult );
            ( void ) xResult;
        }
        else
        {
            mtCOVERAGE_TEST_MARKER();
        }
    }
    else
    {
        mtCOVERAGE_TEST_MARKER();
    }

    /* Call the timer callback. */
    pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
}

Структура:

typedef struct tmrTimerControl
{
    const char              *pcTimerName;       /*<< Text name.  This is not used by the kernel, it is included simply to make debugging easier. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
    ListItem_t              xTimerListItem;     /*<< Standard linked list item as used by all kernel features for event management. */
    TickType_t              xTimerPeriodInTicks;/*<< How quickly and often the timer expires. */
    UBaseType_t             uxAutoReload;       /*<< Set to pdTRUE if the timer should be automatically restarted once expired.  Set to pdFALSE if the timer is, in effect, a one-shot timer. */
    void                    *pvTimerID;         /*<< An ID to identify the timer.  This allows the timer to be identified when the same callback is used for multiple timers. */
    TimerCallbackFunction_t pxCallbackFunction; /*<< The function that will be called when the timer expires. */
} xTIMER;

typedef xTIMER Timer_t;

Эта структура содержит указатель данных, который я передал, когда создал таймер.Он хранится в pvTimerId.

Это означает, что я должен привести параметр обратного вызова к Timer_t, чтобы иметь доступ к pvTimerId.Примерно так:

void Callback01(void const * argument)
{
    T* a =((Timer_t*)argument)->pvTimerID;
}

НО эта структура Timer_t не является публичной.Я не очень понимаю, почему обратный вызов вызывается с этой структурой в качестве параметра и, кроме того, приводится как const void * ...

Как мне поступить?

1 Ответ

0 голосов
/ 15 февраля 2019

Учитывая, что вызов функции osTimerCreate в вашей версии cmsis действительно сохраняет параметр аргумента для pvTimerID структуры Timer_t, вы можете использовать pvTimerGetTimerID , чтобы получить переданные данные обратно:

void Callback01(void const * argument)
{
    T* data = (T*)pvTimerGetTimerID((TimerHandle_t)argument);
}
...