Timer (PWM)
Some parts of the AHI Timer-related APIs are implemented for source code compatibility.
- This implementation is not intended to maintain full compatibility.
- Notes and considerations known at the time of writing are described here.
- Internal implementations may change without notice.
- For parameters mentioned in the explanations, only items requiring attention are described; for omitted details, refer to the original AHI library manual.
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 bybOutputEnable
invAHI_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 asbIntPeriodEnable == TRUE
. - Even if
bOutputEnable == TRUE
is set, if the pin is disabled byvAHI_TimerFineGrainDIOControl()
, it is treated asFALSE
. - 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 ID | 0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|---|
SMD PIN## | 12 | 16 | 13 | 15 | 19 |
AHI DIO## | 10 | 11 | 12 | 13 | 17 |
PIO## | 4 | 0 | 13 | 1 | 21 |
PWM channel | 4 | 0 | 2 | 1 | 9 |
When Using DO1,2 in Standard Mode
AHI TIMER ID | 0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|---|
SMD PIN## | 12 | 16 | 1 | 2 | 19 |
AHI DIO## | 10 | 11 | DO0 | DO1 | 17 |
PIO## | 4 | 0 | 0 | 5 | 21 |
PWM channel | 4 | 0 | 0 | N/A | 9 |
- TIMER_1 and TIMER_2 assignments conflict.
- The output pin for TIMER_3 does not have hardware timer functionality.
Secondary Assignment
AHI TIMER ID | 0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|---|
SMD PIN## | 6 | 7 | 8 | 9 | 10 |
AHI DIO## | 4 | 5 | 6 | 7 | 8 |
PIO## | 7 | 6 | 8 | 9 | 14 |
PWM channel | 7 | 6 | 8 | 9 | 1 |
When Using DO1,2 in Secondary Assignment
AHI TIMER ID | 0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|---|
SMD PIN## | 6 | 7 | 1 | 9 | 10 |
AHI DIO## | 4 | 5 | DO0 | DO1 | 8 |
PIO## | 7 | 6 | 0 | 5 | 14 |
PWM channel | 7 | 6 | 0 | N/A | 1 |
- 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.
- Calling these APIs creates processing objects for AHI compatibility. Behavior is undefined if you then call APIs defined in
- Define
PWM0_IRQHandler()
…PWM9_IRQHandler()
separately. Since these are specified withweak
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.
Related AHI APIs
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_???
).