This is the multi-page printable view of this section. Click here to print...

Return to the regular view of this page

As of 2025-09-10

AHI Functions and Explanations for Timer (PWM)

Explanations of the TWENET library and AHI functions related to Timer (PWM)
    This section provides explanations of the TWENET library and AHI functions related to Timer (PWM).

    Timer (PWM)

    Some parts of the AHI Timer-related APIs are implemented for source code compatibility.

    AHI APIについて

    vAHI_TimerFineGrainDIOControl()

    void vAHI_TimerFineGrainDIOControl(uint8 u8BitMask)

    Specifies the bitmask to disable output pins. Only the bits corresponding to TIMER_0 … TIMER_4 are valid.

    • Call this function before executing vAHI_TimerEnable().
    • Even if output is enabled in the parameters of vAHI_TimerEnable(), pins set to be disabled by this function will be treated as output-disabled.

    vAHI_TimerAssignPIOPin_MW()

    void vAHI_TimerAssignPIOPin_MW(
    		uint8 u8_timer_id,
    		uint8 u8_pin_id)
    

    This function is specific to TWELITE GOLD.

    Specifies the PIO number of the pin to be used with the specified timer device (u8_timer_id).

    Each PIO number is linked to a specific PWM channel. Some PIO numbers do not support PWM. Depending on how you specify them, it is possible to create conflicting settings, such as using the same PWM channel multiple times. Refer to the pin assignment table for mapping between PIO numbers and PWM channels.

    • u8_timer_id specifies the target timer device (E_AHI_DEVICE_TIMER0-4).
    • Settings in vAHI_TimerFineGrainDIOControl() are also valid, but output enable/disable is controlled by bOutputEnable in vAHI_TimerEnable().
    • Even if output is disabled and only timer interrupts are used, specify the pin number corresponding to the PWM channel to be used.

    This is a custom function of this library.

    vAHI_TimerEnable()

    void vAHI_TimerEnable(
        uint8       u8_timer_id,
        uint8       u8Prescale,
        bool_t      bIntRiseEnable,
        bool_t      bIntPeriodEnable,
        bool_t      bOutputEnable)
    

    Enables the timer.

    • If bIntRiseEnable == TRUE, it is treated as bIntPeriodEnable == TRUE.
    • Even if bOutputEnable == TRUE is set, if the pin is disabled by vAHI_TimerFineGrainDIOControl(), it is treated as FALSE.
    • The behavior is undefined if this function is called again for a timer ID that has already been enabled.
    When Hardware Resource Conflicts Occur

    Due to hardware differences from TWELITE BLUE/RED, not all timer devices can be used in the same way. Refer to the pin assignment section described later for details.

    • If PWM output is enabled, software-based PWM output emulation will be performed.
    • If PWM output is disabled, the purpose is limited to timer interrupts, so the available PWM3 or PWM5 will be used without pin output.
    • No API or procedure is provided to check whether the above assignment has been made.

    vAHI_TimerDisable()

    void  vAHI_TimerDisable(uint8 u8_timer_id)

    Disables the timer.

    • The output port is reverted to input (the pull-up setting is not changed).

    vAHI_TimerDisable()

    void  vAHI_TimerDisable(uint8 u8_timer_id)

    Disables the timer.

    • The output port is reverted to input (the pull-up setting is not changed).

    vAHI_TimerConfigure()

    void vAHI_TimerConfigure(
    		uint8       u8_timer_id,
    		bool_t      bInvertPwmOutput,
    		bool_t      bGateDisable)

    Modifies the timer settings.

    • Configures bInvertPwmOutput.
    • bGateDisable is ignored.

    vAHI_TimerStartRepeat()

    void  vAHI_TimerStartRepeat(
    		uint8       u8_timer_id,
    	    uint16      u16Hi,
    	    uint16      u16Lo)

    Starts the timer.

    • This function can also be called while the timer is running and is used to change the PWM duty cycle settings.

    vAHI_TimerStop()

    void  vAHI_TimerStop(uint8 u8_timer_id)

    Stops the timer.

    vAHI_TimerSetLocation()

    void vAHI_TimerSetLocation(
    		uint8 u8Timer,
    		bool_t bLocation,
    		bool_t bLocationOverridePWM3andPWM2)

    Changes the pin assignment.

    • Call this function before executing vAHI_TimerEnable().

    vAHI_TimerXRegisterCallback

    PUBLIC void  vAHI_Timer0RegisterCallback(
        PR_HWINT_APPCALLBACK        prTimer0Callback);
    PUBLIC void  vAHI_Timer1RegisterCallback(
        PR_HWINT_APPCALLBACK        prTimer1Callback);
    PUBLIC void vAHI_Timer2RegisterCallback(
        PR_HWINT_APPCALLBACK        prTimer2Callback);
    PUBLIC void vAHI_Timer3RegisterCallback(
        PR_HWINT_APPCALLBACK        prTimer3Callback);
    PUBLIC void vAHI_Timer4RegisterCallback(
        PR_HWINT_APPCALLBACK        prTimer4Callback);

    The interrupt handler registration functions are not implemented. The interrupt handler is invoked via AppQAPI.

    If you want to define a custom interrupt handler, implement the WEAK-defined handler functions below.
    Refer to the implementation in AHIcmpt_TImers.cpp.
    At a minimum, you need to call PWM_ClearStatusFlags(PWM, kPWM_PwmX);.

    WEAK void PWM0_DriverIRQHandler(void); // MONO WIRELESS, DEFINED AS WEAK FUNCTION.
    WEAK void PWM1_DriverIRQHandler(void);
    WEAK void PWM2_DriverIRQHandler(void);
    WEAK void PWM3_DriverIRQHandler(void);
    WEAK void PWM4_DriverIRQHandler(void);
    WEAK void PWM5_DriverIRQHandler(void);
    WEAK void PWM6_DriverIRQHandler(void);
    WEAK void PWM7_DriverIRQHandler(void);
    WEAK void PWM8_DriverIRQHandler(void);
    WEAK void PWM9_DriverIRQHandler(void);

    Other Functions

    void vAHI_TimerClockSelect(
        uint8       u8Timer,
        bool_t      bExternalClock,
        bool_t      bInvertClock);
    void vAHI_TimerStartSingleShot(
        uint8       u8Timer,
        uint16      u16Hi,
        uint16      u16Lo);

    These are either undefined functions or dummy functions that do nothing.

    About Output Pins

    • Output pins are initialized for PWM use when calling vAHI_TimerEnabled().
    • Output pins are reset to input ports (without changing pull-up settings) when calling vAHI_TimerDisable().

    About Interrupts

    Interrupts at intermediate output transition points within a cycle are not supported. Only interrupts for each full cycle are supported.

    Interrupt Handlers

    Interrupt handlers are only supported in the standard TWENET callbacks cbToCoNet_u8HwInt() and cbToCoNet_vHwEvent(). Additional handlers cannot be registered.

    If you want to define individual handlers, define the PWMx_IRQHandler() functions separately. In this case, events will not be reported to TWENET from the defined handler.

    Pin Assignment

    Pin assignment can be configured individually for each TIMER_ID, but due to hardware differences in TWELITE BLACK, assignments may be difficult in some cases.

    Standard

    AHI TIMER ID01234
    SMD PIN##1216131519
    AHI DIO##1011121317
    PIO##4013121
    PWM channel40219

    When Using DO1,2 in Standard Mode

    AHI TIMER ID01234
    SMD PIN##12161219
    AHI DIO##1011DO0DO117
    PIO##400521
    PWM channel400N/A9
    • TIMER_1 and TIMER_2 assignments conflict.
    • The output pin for TIMER_3 does not have hardware timer functionality.

    Secondary Assignment

    AHI TIMER ID01234
    SMD PIN##678910
    AHI DIO##45678
    PIO##768914
    PWM channel76891

    When Using DO1,2 in Secondary Assignment

    AHI TIMER ID01234
    SMD PIN##671910
    AHI DIO##45DO0DO18
    PIO##760514
    PWM channel760N/A1
    • The output pin for TIMER_3 does not have hardware timer functionality.

    Others

    Behavior When Pin Assignment Conflicts Occur

    • If a pin configured for PWM output has a conflict (e.g., the pin does not support PWM), software emulation will be used.
      • The software emulation controls the output under the system timer (SysTick) interrupt, using 16 clocks per cycle. If running at 1 kHz, the PWM period is 1000/16 ≈ 62 Hz.
    • If a pin without PWM output configured has a conflict (e.g., duplicate PWM device numbers), the available PWM3 or PWM5 is assigned. PWM interrupts will operate even without pin output.

    When Not Using This Library’s PWM Features

    • Do not call AHI timer-related APIs (?AHI_Timer???).
      • Calling these APIs creates processing objects for AHI compatibility. Behavior is undefined if you then call APIs defined in fsl_pwm.h in this state.
    • Define PWM0_IRQHandler()PWM9_IRQHandler() separately. Since these are specified with weak linkage, your application’s non-weak definitions will take precedence at link time.

    Behavior on Wake from Sleep

    • For RAM-retention sleep, the PWM output state immediately before sleep is restored. For RAM-off sleep, normal DIO initialization is performed.
    void vAHI_TimerFineGrainDIOControl(
        uint8 u8BitMask);
    PUBLIC void vAHI_TimerConfigure(
        uint8       u8Timer,
        bool_t      bInvertPwmOutput,
        bool_t      bGateDisable);
    PUBLIC void  vAHI_TimerEnable(
        uint8       u8Timer,
        uint8       u8Prescale,
        bool_t      bIntRiseEnable,
        bool_t      bIntPeriodEnable,
        bool_t      bOutputEnable);
    PUBLIC void  vAHI_TimerDisable(
        uint8       u8Timer);
    PUBLIC void  vAHI_TimerClockSelect(
        uint8       u8Timer,
        bool_t      bExternalClock,
        bool_t      bInvertClock);
    PUBLIC void  vAHI_TimerStartRepeat(
        uint8       u8Timer,
        uint16      u16Hi,
        uint16      u16Lo);
    PUBLIC void  vAHI_TimerStop(
        uint8 u8_timer_id);
    PUBLIC void vAHI_TimerSetLocation(
        uint8 u8Timer,
        bool_t bLocation,
        bool_t bLocationOverridePWM3andPWM2);

    ※ Some function names are redefined by macros.
    ※ The timer library in utils.h also uses the above APIs.

    Example Handler Definitions

    Minimal definitions including the essential steps.

    void PWM0_IRQHandler(void) { PWM_ClearStatusFlags(PWM, kPWM_Pwm0); }
    void PWM1_IRQHandler(void) { PWM_ClearStatusFlags(PWM, kPWM_Pwm1); }
    void PWM2_IRQHandler(void) { PWM_ClearStatusFlags(PWM, kPWM_Pwm2); }
    void PWM3_IRQHandler(void) { PWM_ClearStatusFlags(PWM, kPWM_Pwm3); }
    void PWM4_IRQHandler(void) { PWM_ClearStatusFlags(PWM, kPWM_Pwm4); }
    void PWM5_IRQHandler(void) { PWM_ClearStatusFlags(PWM, kPWM_Pwm5); }
    void PWM6_IRQHandler(void) { PWM_ClearStatusFlags(PWM, kPWM_Pwm6); }
    void PWM7_IRQHandler(void) { PWM_ClearStatusFlags(PWM, kPWM_Pwm7); }
    void PWM8_IRQHandler(void) { PWM_ClearStatusFlags(PWM, kPWM_Pwm8); }
    void PWM9_IRQHandler(void) { PWM_ClearStatusFlags(PWM, kPWM_Pwm9); }
    • When you define these functions, interrupts/events will not be forwarded to TWENET.

    About This Library’s Implementation

    • The main implementation code is in AHIcmpt_Timer.cpp.
    • The above code references _periph_pwm.hpp and _periph_pwm.cpp. These are class libraries that consolidate procedures using the platform drivers (driver/fs_???).