void SWT_Init(void) { BaseType_t res; led = 0; /* red */ #if configSUPPORT_STATIC_ALLOCATION handle = xTimerCreateStatic( "swtimer", /* debug name of task */ pdMS_TO_TICKS(500), /* period */ pdTRUE, /* auto-reload */ NULL, /* no timer ID */ vTimerCallback, /* callback */ &xTimerBuffer ); #else /* configSUPPORT_DYNAMIC_ALLOCATION */ handle = xTimerCreate( "swtimer", /* debug name of task */ pdMS_TO_TICKS(500), /* period */ pdTRUE, /* auto-reload */ NULL, /* no timer ID */ vTimerCallback /* callback */ ); #endif if (handle==NULL) { for(;;); /* error creating timer */ } res = xTimerStart(handle, 0); if (res==pdFAIL) { for(;;); /* error starting timer */ } }
TimerHandle_t MPU_xTimerCreateStatic( const char * const pcTimerName, const TickType_t xTimerPeriodInTicks, const UBaseType_t uxAutoReload, void * const pvTimerID, TimerCallbackFunction_t pxCallbackFunction, StaticTimer_t *pxTimerBuffer ) { TimerHandle_t xReturn; BaseType_t xRunningPrivileged = xPortRaisePrivilege(); xReturn = xTimerCreateStatic( pcTimerName, xTimerPeriodInTicks, uxAutoReload, pvTimerID, pxCallbackFunction, pxTimerBuffer ); vPortResetPrivilege( xRunningPrivileged ); return xReturn; }
static void prvCreateAndDeleteStaticallyAllocatedTimers( void ) { TimerHandle_t xTimer; UBaseType_t uxVariableToIncrement; const TickType_t xTimerPeriod = pdMS_TO_TICKS( 20 ); BaseType_t xReturned; /* StaticTimer_t is a publicly accessible structure that has the same size and alignment requirements as the real timer structure. It is provided as a mechanism for applications to know the size of the timer structure (which is dependent on the architecture and configuration file settings) without breaking the strict data hiding policy by exposing the real timer internals. This StaticTimer_t variable is passed into the xTimerCreateStatic() function calls within this function. */ StaticTimer_t xTimerBuffer; /* Create the software time. xTimerCreateStatic() has an extra parameter than the normal xTimerCreate() API function. The parameter is a pointer to the StaticTimer_t structure that will hold the software timer structure. If the parameter is passed as NULL then the structure will be allocated dynamically, just as if xTimerCreate() had been called. */ xTimer = xTimerCreateStatic( "T1", /* Text name for the task. Helps debugging only. Not used by FreeRTOS. */ xTimerPeriod, /* The period of the timer in ticks. */ pdTRUE, /* This is an auto-reload timer. */ ( void * ) &uxVariableToIncrement, /* The variable incremented by the test is passed into the timer callback using the timer ID. */ prvTimerCallback, /* The function to execute when the timer expires. */ &xTimerBuffer ); /* The buffer that will hold the software timer structure. */ /* The timer handle should equal the static timer structure passed into the xTimerCreateStatic() function. */ configASSERT( xTimer == ( TimerHandle_t ) &xTimerBuffer ); /* Set the variable to 0, wait for a few timer periods to expire, then check the timer callback has incremented the variable to the expected value. */ uxVariableToIncrement = 0; /* This is a low priority so a block time should not be needed. */ xReturned = xTimerStart( xTimer, staticDONT_BLOCK ); if( xReturned != pdPASS ) { xErrorOccurred = pdTRUE; } vTaskDelay( xTimerPeriod * staticMAX_TIMER_CALLBACK_EXECUTIONS ); /* By now the timer should have expired staticMAX_TIMER_CALLBACK_EXECUTIONS times, and then stopped itself. */ if( uxVariableToIncrement != staticMAX_TIMER_CALLBACK_EXECUTIONS ) { xErrorOccurred = pdTRUE; } /* Finished with the timer, delete it. */ xReturned = xTimerDelete( xTimer, staticDONT_BLOCK ); /* Again, as this is a low priority task it is expected that the timer command will have been sent even without a block time being used. */ if( xReturned != pdPASS ) { xErrorOccurred = pdTRUE; } /* Just to show the check task that this task is still executing. */ uxCycleCounter++; /* Now do the same using a dynamically allocated software timer to ensure the delete function is working correctly in both the static and dynamic allocation cases. */ #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) { xTimer = xTimerCreate( "T1", /* Text name for the task. Helps debugging only. Not used by FreeRTOS. */ xTimerPeriod, /* The period of the timer in ticks. */ pdTRUE, /* This is an auto-reload timer. */ ( void * ) &uxVariableToIncrement, /* The variable incremented by the test is passed into the timer callback using the timer ID. */ prvTimerCallback ); /* The function to execute when the timer expires. */ configASSERT( xTimer != NULL ); uxVariableToIncrement = 0; xReturned = xTimerStart( xTimer, staticDONT_BLOCK ); if( xReturned != pdPASS ) { xErrorOccurred = pdTRUE; } vTaskDelay( xTimerPeriod * staticMAX_TIMER_CALLBACK_EXECUTIONS ); if( uxVariableToIncrement != staticMAX_TIMER_CALLBACK_EXECUTIONS ) { xErrorOccurred = pdTRUE; } xReturned = xTimerDelete( xTimer, staticDONT_BLOCK ); if( xReturned != pdPASS ) { xErrorOccurred = pdTRUE; } } #endif }