The SDK provides a driver for the Pulse Width Modulator (PWM) of JN518x devices.
The function PWM_Init() initializes the PWM module with specified configurations, the function PWM_GetDefaultConfig() could help to get the default configurations. The initialization function configures the module to use the specified clock for PWM operation.
The function PWM_SetupPwm() sets up the PWM channel for PWM output. The function can set up PWM signal property the channel. The PWM has 10 channels: 0 to 9. Each channel has its own period, compare match value, and polarity specified. The settings are applied to the specified channel requesting PWM output. The period and compare match are 16-bit values. At the compare match value, within the period, the PWM output toggles. The period value is loaded to downcounter, which decrements to 0. Once it reaches 0, it reloads the count and starts the signal out again, until the PWM channel is stopped. The function also sets up the channel output level after the channel is disabled. The 11th channel (ChannelAll) is a special channel which outputs the same output signals on other 10 channels (0 to 9) when it is set up and enabled.
The function PWM_ReadPeriodValue() reads the current period (downcounter value) for the PWM channel. The function PWM_ReadCompareValue() reads the compare match value for the PWM channel.
The function PWM_StartTimer() can be used to start the PWM channel. The function PWM_StopTimer() can be used to stop the PWM channel.
Provide functions to get and clear the PWM status.
Provide functions to enable/disable PWM interrupts and get current enabled interrupts.
Typical use case
PWM output
Configures PWM channel to output PWM signal.
int main(void)
{
uint32_t pwmClockFrq;
uint32_t pwmChan0Clk;
BOARD_InitHardware();
PRINTF("PWM driver example\n");
pwmConfig.
clk_sel = kPWM_Osc32Mclk;
if (
PWM_Init(BOARD_PWM_BASEADDR, &pwmConfig) != kStatus_Success)
{
PRINTF("PWM initialization failed\n");
return 1;
}
{
PRINTF("PWM chan0 setup failed\n");
return 1;
}
while (1U)
{
;
}
}
|
enum | pwm_channels_t {
kPWM_Pwm0 = 0x0,
kPWM_Pwm1,
kPWM_Pwm2,
kPWM_Pwm3,
kPWM_Pwm4,
kPWM_Pwm5,
kPWM_Pwm6,
kPWM_Pwm7,
kPWM_Pwm8,
kPWM_Pwm9,
kPWM_PwmAll
} |
| PWM channel selection values. More...
|
|
enum | pwm_polarity_control_t {
kPWM_SetHighOnMatchLowOnPeriod = 0x0,
kPWM_SetLowOnMatchHighOnPeriod
} |
| PWM channel polarity control values. More...
|
|
enum | pwm_dis_output_level_t {
kPWM_SetLow = 0x0,
kPWM_SetHigh
} |
| PWM channel disable output level values. More...
|
|
enum | pwm_interrupt_enable_t {
kPWM_InterruptDisabled = 0x0,
kPWM_InterruptEnabled
} |
| PWM channel interrupt enable flags. More...
|
|
enum | pwm_interrupt_status_t {
kPWM_NoInterrupt = 0x0,
kPWM_InterruptPendig
} |
| PWM channel interrupt status flags. More...
|
|
uint16_t pwm_setup_t::prescaler_val |
Enumerator |
---|
kPWM_Pwm0 |
Channel 0.
|
kPWM_Pwm1 |
Channel 1.
|
kPWM_Pwm2 |
Channel 2.
|
kPWM_Pwm3 |
Channel 3.
|
kPWM_Pwm4 |
Channel 4.
|
kPWM_Pwm5 |
Channel 5.
|
kPWM_Pwm6 |
Channel 6.
|
kPWM_Pwm7 |
Channel 7.
|
kPWM_Pwm8 |
Channel 8.
|
kPWM_Pwm9 |
Channel 9.
|
kPWM_PwmAll |
Channel 10 - All the channels will output same output programmed in this channel.
|
Enumerator |
---|
kPWM_SetHighOnMatchLowOnPeriod |
Set high on compare match, set low at end of PWM period.
|
kPWM_SetLowOnMatchHighOnPeriod |
Set low on compare match, set high at end of PWM period.
|
Enumerator |
---|
kPWM_SetLow |
Set to Low level.
|
kPWM_SetHigh |
Set to High level.
|
Enumerator |
---|
kPWM_InterruptDisabled |
PWM channel interrupt disabled.
|
kPWM_InterruptEnabled |
PWM channel interrupt enabled.
|
Enumerator |
---|
kPWM_NoInterrupt |
PWM channel interrupt not occurred.
|
kPWM_InterruptPendig |
PWM channel interrupt pending.
|
The default values are:
* userConfig->clk_sel = kPWM_Osc32Mclk;
*
- Parameters
-
userConfig | Pointer to user's PWM config structure. |
Call this API to ungate the PWM clock and configure the PWM HW.
- Note
- This API should be called at the beginning of the application to use the PWM driver, or any operation to the PWM module could cause hard fault because PWM module clock is not enabled. The configuration structure can be filled by user from scratch, or be set with default values by PWM_GetDefaultConfig(). After calling this API, the application can configure PWM channels to generate PWM outputs. Example:
- Parameters
-
base | PWM base address |
userConfig | pointer to user configuration structure |
- Returns
- kStatus_Success - Success
-
kStatus_InvalidArgument - Invalid input parameter
void PWM_Deinit |
( |
PWM_Type * |
base | ) |
|
The function initializes the PWM channel according to the parameters passed in by the user. The function sets up the PWM compare match register & period registers.
- Parameters
-
base | PWM base address |
pwm_chan | PWM channel select value |
pwmSetup | Pointer to PWM user setup structure |
- Returns
- kStatus_Success - Success
-
kStatus_InvalidArgument - Invalid input parameter
static void PWM_EnableInterrupts |
( |
PWM_Type * |
base, |
|
|
pwm_channels_t |
pwm_chan |
|
) |
| |
|
inlinestatic |
This function enables the interrupt for the specified PWM channel.
- Parameters
-
base | PWM base address |
pwm_chan | PWM channel select value |
static void PWM_DisableInterrupts |
( |
PWM_Type * |
base, |
|
|
pwm_channels_t |
pwm_chan |
|
) |
| |
|
inlinestatic |
This function disables the interrupt for the specified PWM channel.
- Parameters
-
base | PWM base address |
pwm_chan | PWM channel select value |
static uint32_t PWM_GetEnabledInterrupts |
( |
PWM_Type * |
base, |
|
|
pwm_channels_t |
pwm_chan |
|
) |
| |
|
inlinestatic |
- Parameters
-
base | PWM base address |
pwm_chan | PWM channel select value |
- Returns
- PWM interrupt enabled status. This is the one of the values specified in enumeration pwm_interrupt_enable_t
uint32_t PWM_GetStatusFlags |
( |
PWM_Type * |
base, |
|
|
pwm_channels_t |
pwm_chan |
|
) |
| |
- Parameters
-
base | PWM base address |
pwm_chan | PWM channel select value |
- Returns
- The status flags. This is the one of the value of members of the enumeration pwm_interrupt_status_t
void PWM_ClearStatusFlags |
( |
PWM_Type * |
base, |
|
|
pwm_channels_t |
pwm_chan |
|
) |
| |
- Parameters
-
base | PWM base address |
pwm_chan | PWM channel select value |
static void PWM_StartTimer |
( |
PWM_Type * |
base, |
|
|
pwm_channels_t |
pwm_chan |
|
) |
| |
|
inlinestatic |
The API will start PWM channel output on the pin. Before calling this API, make sure that the PWM channel is set up using PWM_SetupPwm() API.
- Parameters
-
base | PWM base address |
pwm_chan | PWM channel select value |
static void PWM_StopTimer |
( |
PWM_Type * |
base, |
|
|
pwm_channels_t |
pwm_chan |
|
) |
| |
|
inlinestatic |
The API will stop PWM channel output on the pin.
- Parameters
-
base | PWM base address |
pwm_chan | PWM channel select value |
uint16_t PWM_ReadPeriodValue |
( |
PWM_Type * |
base, |
|
|
pwm_channels_t |
pwm_chan |
|
) |
| |
The API will read the current period value set for the PWM channel.
- Parameters
-
base | PWM base address |
pwm_chan | PWM channel select value |
- Returns
- 16-bit period value
uint16_t PWM_ReadCompareValue |
( |
PWM_Type * |
base, |
|
|
pwm_channels_t |
pwm_chan |
|
) |
| |
The API will read the compare match value set for the PWM channel.
- Parameters
-
base | PWM base address |
pwm_chan | PWM channel select value |
- Returns
- 16-bit period value