这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 行业应用 » 汽车电子 » 【S32K3XX】STM适配 custom timer

共1条 1/1 1 跳转至

【S32K3XX】STM适配 custom timer

高工
2025-11-23 18:30:36     打赏

【简介】

在S32K3系列芯片的RTD的驱动中会有检测超时的逻辑,这个检测的timer可以指定用户自己实现的timer。在S32DS的驱动配置中也可以选择自定义的超时处理。以下是S32DS 的UART驱动配置,可以让用户自己来选择超时检出的timer 处理方法。

image.png

以下是os_if 接口中对timer 的接口函数。

/*FUNCTION**********************************************************************
 *
 * Function Name : OsIf_Init.
 * Description   : OsIf initialization.
 * @implements OsIf_Init_Activity
 * 
 *END**************************************************************************/
void OsIf_Init(const void* Config)
{
#if (STD_ON == OSIF_DEV_ERROR_DETECT)
    if (Config != NULL_PTR)
    {
    #if defined(USING_OS_AUTOSAROS)
        (void)Det_ReportError(OSIF_MODULE_ID, OSIF_DRIVER_INSTANCE, OSIF_SID_INIT, OSIF_E_INIT_FAILED);
    #else
        OSIF_DEV_ASSERT(FALSE);
    #endif /* defined(USING_OS_AUTOSAROS) */
    }
#else
    (void)Config;
#endif /* (STD_ON == OSIF_DEV_ERROR_DETECT) */

#if (OSIF_USE_SYSTEM_TIMER == STD_ON)
    OsIf_Timer_System_Init();
#endif /* (OSIF_USE_SYSTEM_TIMER == STD_ON) */
#if (OSIF_USE_CUSTOM_TIMER == STD_ON)
    OsIf_Timer_Custom_Init();
#endif /* (OSIF_USE_CUSTOM_TIMER == STD_ON) */
}

/*FUNCTION**********************************************************************
 *
 * Function Name : OsIf_GetCounter.
 * Description   : Get counter value.
 * @implements OsIf_GetCounter_Activity
 * 
 *END**************************************************************************/
uint32 OsIf_GetCounter(OsIf_CounterType SelectedCounter)
{
    uint32 u32Value = 0U;

    switch (SelectedCounter)
    {
        case OSIF_COUNTER_DUMMY:
            u32Value = OsIf_Timer_Dummy_GetCounter();
            break;
#if (OSIF_USE_SYSTEM_TIMER == STD_ON)
        case OSIF_COUNTER_SYSTEM:
            u32Value = OsIf_Timer_System_GetCounter();
            break;
#endif /* (OSIF_USE_SYSTEM_TIMER == STD_ON) */
#if (OSIF_USE_CUSTOM_TIMER == STD_ON)
        case OSIF_COUNTER_CUSTOM:
            u32Value = OsIf_Timer_Custom_GetCounter();
            break;
#endif /* (OSIF_USE_CUSTOM_TIMER == STD_ON) */
        default:
            /* impossible */
            break;
    }

    return u32Value;
}


/*FUNCTION**********************************************************************
 *
 * Function Name : OsIf_GetElapsed.
 * Description   : Get elapsed value.
 * @implements OsIf_GetElapsed_Activity
 * 
 *END**************************************************************************/
uint32 OsIf_GetElapsed(uint32 * const CurrentRef,
                       OsIf_CounterType SelectedCounter
                      )
{
    uint32 u32Value = 0U;

    switch (SelectedCounter)
    {
        case OSIF_COUNTER_DUMMY:
            u32Value = OsIf_Timer_Dummy_GetElapsed(CurrentRef);
            break;
#if (OSIF_USE_SYSTEM_TIMER == STD_ON)
        case OSIF_COUNTER_SYSTEM:
            u32Value = OsIf_Timer_System_GetElapsed(CurrentRef);
            break;
#endif /* (OSIF_USE_SYSTEM_TIMER == STD_ON) */
#if (OSIF_USE_CUSTOM_TIMER == STD_ON)
        case OSIF_COUNTER_CUSTOM:
            u32Value = OsIf_Timer_Custom_GetElapsed(CurrentRef);
            break;
#endif /* (OSIF_USE_CUSTOM_TIMER == STD_ON) */
        default:
            /* impossible */
            break;
    }

    return u32Value;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : OsIf_SetTimerFrequency.
 * Description   : Set timer frequency.
 * @implements OsIf_SetTimerFrequency_Activity
 * 
 *END**************************************************************************/
/* @implements OsIf_SetTimerFrequency_Activity */
void OsIf_SetTimerFrequency(uint32 Freq, 
                            OsIf_CounterType SelectedCounter
                           )
{
    switch (SelectedCounter)
    {
        case OSIF_COUNTER_DUMMY:
            OsIf_Timer_Dummy_SetTimerFrequency(Freq);
            break;
#if (OSIF_USE_SYSTEM_TIMER == STD_ON)
        case OSIF_COUNTER_SYSTEM:
            OsIf_Timer_System_SetTimerFrequency(Freq);
            break;
#endif /* (OSIF_USE_SYSTEM_TIMER == STD_ON) */
#if (OSIF_USE_CUSTOM_TIMER == STD_ON)
        case OSIF_COUNTER_CUSTOM:
            OsIf_Timer_Custom_SetTimerFrequency(Freq);
            break;
#endif /* (OSIF_USE_CUSTOM_TIMER == STD_ON) */
        default:
            /* impossible */
            break;
    }
}

/*FUNCTION**********************************************************************
 *
 * Function Name : OsIf_MicrosToTicks.
 * Description   : Convert micro second to ticks.
 * @implements OsIf_MicrosToTicks_Activity
 * 
 *END**************************************************************************/
uint32 OsIf_MicrosToTicks(uint32 Micros,
                          OsIf_CounterType SelectedCounter
                         )
{
    uint32 u32Value = 0U;

    switch (SelectedCounter)
    {
        case OSIF_COUNTER_DUMMY:
            u32Value = OsIf_Timer_Dummy_MicrosToTicks(Micros);
            break;
#if (OSIF_USE_SYSTEM_TIMER == STD_ON)
        case OSIF_COUNTER_SYSTEM:
            u32Value = OsIf_Timer_System_MicrosToTicks(Micros);
            break;
#endif /* (OSIF_USE_SYSTEM_TIMER == STD_ON) */
#if (OSIF_USE_CUSTOM_TIMER == STD_ON)
        case OSIF_COUNTER_CUSTOM:
            u32Value = OsIf_Timer_Custom_MicrosToTicks(Micros);
            break;
#endif /* (OSIF_USE_CUSTOM_TIMER == STD_ON) */
        default:
            /* impossible */
            break;
    }

    return u32Value;
}


以上 os_if 的接口每个接口内都有 OSIF_USE_CUSTOM_TIMER  的分支并且定义好了函数的名称,我们只需要按照口实现对应的接口即可完成cuetom timer 的添加,接口定义说明如下:

/*!
 * @brief Initialize the custom timer.
 *
 * This function initialize the custom timer.
 */
void OsIf_Timer_Custom_Init(void);

/*!
 * @brief Get counter value from custom timer.
 *
 * This function get counter value from custom timer.
 *
 * @return Counter value
 */
uint32 OsIf_Timer_Custom_GetCounter(void);

/*!
 * @brief Get elapsed value from custom timer.
 *
 * This function get elapsed value from custom timer.
 *
 * @param[in] CurrentRef The pointer to current reference point
 * @return Elapsed value
 */
uint32 OsIf_Timer_Custom_GetElapsed(uint32 * const CurrentRef);

/*!
 * @brief Set custom timer frequency.
 *
 * This function set custom timer frequency.
 *
 * @param[in] Freq Frequency value
 */
void OsIf_Timer_Custom_SetTimerFrequency(uint32 Freq);

/*!
 * @brief Convert micro second to ticks based on custom timer frequency.
 *
 * This function Convert micro second to ticks based on custom timer frequency.
 *
 * @param[in] Micros Micro second
 * @return Ticks
 */
uint32 OsIf_Timer_Custom_MicrosToTicks(uint32 Micros);

【OsIf_Timer_Custom_Init】

/*!
 * @brief Initialize the custom timer.
 *
 * This function initialize the custom timer.
 */
void OsIf_Timer_Custom_Init(void)
{
    Stm_Ip_Init(0, &STM_0_InitConfig_PB);
    Stm_Ip_InitChannel(0,STM_0_ChannelConfig_PB);
    Stm_Ip_StartCounting(0,0,1000);
}

【OsIf_Timer_Custom_GetCounter】

/********************************************************************************************************
 *                                  Private Variable Definitions                                        *
 *******************************************************************************************************/
static uint32 customcounter = 0U;

/********************************************************************************************************
 *                                  Global Function Declarations                                        *
 *******************************************************************************************************/

void Custom_Timer_Callback(uint8 channel)
{
    customcounter++;
}

/*!
 * @brief Get counter value from custom timer.
 *
 * This function get counter value from custom timer.
 *
 * @return Counter value
 */
uint32 OsIf_Timer_Custom_GetCounter(void)
{
    return customcounter;
}

【OsIf_Timer_Custom_GetElapsed】

/*!
 * @brief Get elapsed value from custom timer.
 *
 * This function get elapsed value from custom timer.
 *
 * @param[in] CurrentRef The pointer to current reference point
 * @return Elapsed value
 */
uint32 OsIf_Timer_Custom_GetElapsed(uint32 * const CurrentRef)
{
    return (customcounter - *CurrentRef);
}

【OsIf_Timer_Custom_SetTimerFrequency】

频率固定不需要动态设定

/*!
 * @brief Set custom timer frequency.
 *
 * This function set custom timer frequency.
 *
 * @param[in] Freq Frequency value
 */
void OsIf_Timer_Custom_SetTimerFrequency(uint32 Freq)
{

}

【OsIf_Timer_Custom_MicrosToTicks】

/*!
 * @brief Convert micro second to ticks based on custom timer frequency.
 *
 * This function Convert micro second to ticks based on custom timer frequency.
 *
 * @param[in] Micros Micro second
 * @return Ticks
 */
uint32 OsIf_Timer_Custom_MicrosToTicks(uint32 Micros)
{
    return Micros;
}

在S32DS中开启该配置就可以打开custom timer 的配置项目。

image.png

适配好这些 我们就可以在驱动配置超时处理选择CUSTOM timer作为超时检测接口,本地的uart 配置修改为CUSTOM 。

image.png

修改后UART 的驱动的超时检测就被更新成我们新修改的时钟配置了。

image.png

RTD 添加使用其他的timer 方法也是类似,都是完成timer 的接口配置即可。





共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]