The S32 SDK provides a Peripheral Abstraction Layer for the PWM mode.
The PWM PAL driver allows to generate PWM signals. It was designed to be portable across all platforms and IPs which support PWM features.
How to integrate PWM in your application
Unlike the other drivers, PWM PAL modules need to include a configuration file named pwm_pal_cfg.h, which allows the user to specify which IPSs are used and how many resources are allocated for each of them (state structures). The following code example shows how to configure one instance for each available SPI IPs.
#ifndef PWM_PAL_cfg_H
#define PWM_PAL_cfg_H
#define PWM_OVER_FTM
#define PWM_OVER_EMIOS
#define PWM_OVER_ETIMER
#define NO_OF_FTM_INSTS_FOR_PWM 1U
#define NO_OF_EMIOS_INSTS_FOR_PWM 1U
#define NO_OF_ETIMER_INSTS_FOR_PWM 1U
#endif
The following table contains the matching between platforms and available IPs
IP/MCU | S32K116 | S32K142 | S32K144 | S32K146 | S32K148 | MPC5748G | MPC5746C | MPC5744P |
FTM | YES | YES | YES | YES | YES | NO | NO | NO |
eMIOS | NO | NO | NO | NO | NO | YES | YES | NO |
ETIMER | NO | NO | NO | NO | NO | NO | NO | YES |
In order to use the PWM PAL driver it must be first initialized it using function PWM_Init. Once initialized, it cannot be initialized again for the same SPI module instance until it is de-initialized, using PWM_Denit. Different SPI module instances can work independently of each other.
After initialization the duty cycle and pwm period can be updated with these functions: PWM_UpdateDuty and PWM_UpdatePeriod. The measurement unit for duty and period is clock ticks, so the application should be aware about the clock frequency of the timebase used by PWM channel.
Due to hardware limitation period changing for a specific channel can change the period for other channels if they share the same timebase. Also, for FTM all channels must have the same period and type.
Important Notes
- The driver enables the interrupts for the corresponding module, but any interrupt priority setting must be done by the application.
- Due to different hardware features is necessary to use different timebase configuration on each platform and some features are available only on some peripherals. To be sure that your applications doesn't try to use unsupported features check return status of called functions and activate DEV_ERROR_DETECT.
Example code
The following code explains how to define the configuration structures and how to initialize and update duty cycle on S32K1xx platforms with FTM.
pwm_ftm_timebase_t pwm_pal1Timebase =
{
};
{
{
.period = 5000,
.duty = 2500,
.insertDeadtime = false,
.deadtime = 0,
.enableComplementaryChannel = false,
.timebase = &pwm_pal1Timebase
},
{
.channel = 1,
.period = 5000,
.duty = 1000,
.insertDeadtime = false,
.deadtime = 0,
.enableComplementaryChannel = false,
.timebase = &pwm_pal1Timebase
},
{
.channel = 2,
.period = 5000,
.duty = 100,
.insertDeadtime = false,
.deadtime = 0,
.enableComplementaryChannel = false,
.timebase = &pwm_pal1Timebase
},
};
{
.numberOfPwmChannels = 3
};
{
.instIdx = 0,
};
bool increaseDutyCycle = true;
uint16_t dutyCycle = 0;
PWM_Init(&pwm_pal1Instance, &pwm_pal1Config);
while(1)
{
if (increaseDutyCycle == false)
{
dutyCycle--;
if (dutyCycle < 1)
increaseDutyCycle = true;
}
else
{
dutyCycle++;
if (dutyCycle > 5000)
increaseDutyCycle = false;
}
}
<p>
The following code explains how to define the configuration structures and how to initialize and update duty cycle on MPC574X platforms with eMIOS.
</p>
@code
pwm_emios_timebase_t BUS_A_Timebase =
{
.name = BUS_A,
.internalPrescaler = EMIOS_CLOCK_DIVID_BY_1,
};
{
{
.period = 5000,
.duty = 2500,
.timebase = &BUS_A_Timebase
},
};
{
.numberOfPwmChannels = 1,
};
{
.instIdx = 0,
};
bool increaseDutyCycle = true;
uint16_t dutyCycle = 0;
PWM_Init(&pwm_pal1Instance, &pwm_pal1Config);
while(1)
{
if (increaseDutyCycle == false)
{
dutyCycle--;
if (dutyCycle < 1)
increaseDutyCycle = true;
}
else
{
dutyCycle++;
if (dutyCycle > 5000)
increaseDutyCycle = false;
}
}
|
struct | pwm_channel_t |
| This structure includes the configuration for each channel Implements : pwm_channel_t_Class. More...
|
|
struct | pwm_global_config_t |
| This structure is the configuration for initialization of PWM channels. Implements : pwm_global_config_t_Class. More...
|
|
Defines the channel types Implements : pwm_channel_type_t_Class.
Enumerator |
---|
PWM_EDGE_ALIGNED |
Counter used by this type of channel is in up counting mode and the edge is aligned to PWM period
|
PWM_CENTER_ALIGNED |
Counter used by this type of channel is in up-down counting mode and the duty is inserted in center of PWM period
|
Definition at line 64 of file pwm_pal.h.
Defines the polarity of complementary pwm channels relative to main channel Implements : pwm_complementarty_mode_t_Class.
Enumerator |
---|
PWM_DUPLICATED |
Complementary channel is the same as main channel
|
PWM_INVERTED |
Complementary channel is inverted relative to main channel
|
Definition at line 84 of file pwm_pal.h.
Defines the polarity of pwm channels Implements : pwm_polarity_t_Class.
Enumerator |
---|
PWM_ACTIVE_HIGH |
Polarity is active high
|
PWM_ACTIVE_LOW |
Polarity is active low
|
Definition at line 74 of file pwm_pal.h.
Uninitialised PWM instance.
- Parameters
-
[in] | instance | The name of the instance |
- Returns
- Error or success status returned by API
Definition at line 723 of file pwm_pal.c.
Initialize PWM channels based on config parameter.
- Parameters
-
[in] | instance | The name of the instance |
[in] | config | The configuration structure used to initialize PWM modules |
- Returns
- Error or success status returned by API
Definition at line 134 of file pwm_pal.c.
status_t PWM_OverwriteOutputChannels |
( |
const pwm_instance_t *const |
instance, |
|
|
uint32_t |
channelsMask, |
|
|
uint32_t |
channelsValues |
|
) |
| |
This function change the output value for some channels. channelsMask select which channels will be overwrite, each bit filed representing one channel: 1 - channel is controlled by channelsValues, 0 - channel is controlled by pwm. channelsValues select output values to be write on corresponding channel.
- Parameters
-
[in] | instance | The name of the instance |
[in] | channelsMask | The name mask used to select which channel is overwrite |
[in] | channelsValues | The name overwrite values for all channels |
- Returns
- Error or success status returned by API
Definition at line 679 of file pwm_pal.c.
Update duty cycle. The measurement unit for duty is clock ticks.
- Parameters
-
[in] | instance | The name of the instance |
[in] | channel | The channel which is update |
[in] | duty | The duty cycle measured in ticks |
- Returns
- Error or success status returned by API
Definition at line 516 of file pwm_pal.c.
Update period for specific a specific channel. This function changes period for all channels which shares the timebase with targeted channel.
- Parameters
-
[in] | instance | The name of the instance |
[in] | channel | The channel which is update |
[in] | period | The period measured in ticks |
- Returns
- Error or success status returned by API
Definition at line 583 of file pwm_pal.c.