![]() |
SyterKit 0.4.0.x
SyterKit is a bare-metal framework
|
System PWM (Pulse Width Modulation) driver for Allwinner (sunxi) platforms. More...
#include <io.h>#include <stdarg.h>#include <stdbool.h>#include <stddef.h>#include <stdint.h>#include <types.h>#include <timer.h>#include <common.h>#include <log.h>#include <sys-clk.h>#include <sys-pwm.h>
Macros | |
| #define | PRESCALE_MAX 256 |
| #define | sunxi_pwm_do_div(n, base) |
| Perform division with rounding for PWM calculations. | |
Functions | |
| static void | sunxi_pwm_reg_set (uint32_t reg, uint32_t reg_shift, uint32_t reg_width, uint32_t data) |
| Set the value of a specific field in a PWM register. | |
| static void | sunxi_pwm_gpio_init (sunxi_pwm_t *pwm, int channel) |
| Initialize the GPIO for a specific PWM channel. | |
| static void | sunxi_pwm_clk_init (sunxi_pwm_t *pwm) |
| Initialize the PWM clock by configuring the bus and peripheral clocks. | |
| static void | sunxi_pwm_clk_deinit (sunxi_pwm_t *pwm) |
| Deinitialize the PWM clock and gate/reset control. | |
| static void | sunxi_pwm_enable_controller (sunxi_pwm_t *pwm, int channel) |
| Enable the PWM controller for a specific channel. | |
| static void | sunxi_pwm_disable_controller (sunxi_pwm_t *pwm, int channel) |
| Disable the PWM controller for a specific channel. | |
| static void | sunxi_pwm_set_porality (sunxi_pwm_t *pwm, int channel, sunxi_pwm_polarity_t polarity) |
| Set the polarity of a specific PWM channel. | |
| static uint32_t | sunxi_pwm_get_pccr_reg_offset (uint32_t channel) |
| Get the PCC register offset for a given PWM channel. | |
| static uint32_t | sunxi_pwm_get_pdzcr_reg_offset (uint32_t channel) |
| Get the register offset for the PWM dead zone control register (PDZCR) for a specific channel. | |
| static int | sunxi_pwm_set_config_single (sunxi_pwm_t *pwm, int channel, sunxi_pwm_config_t *config) |
| Configures a single PWM channel with the given configuration parameters. | |
| static int | sunxi_pwm_set_config_bind (sunxi_pwm_t *pwm, int channel, sunxi_pwm_config_t *config) |
| Configure and bind a PWM channel with specific settings. | |
| static int | sunxi_pwm_release_single (sunxi_pwm_t *pwm, int channel) |
| Release a single PWM channel. | |
| static int | sunxi_pwm_release_bind (sunxi_pwm_t *pwm, int channel) |
| Release a bound PWM channel. | |
| void | sunxi_pwm_init (sunxi_pwm_t *pwm) |
| Initialize the PWM instance. | |
| void | sunxi_pwm_deinit (sunxi_pwm_t *pwm) |
| Deinitialize the PWM instance. | |
| int | sunxi_pwm_set_config (sunxi_pwm_t *pwm, int channel, sunxi_pwm_config_t *config) |
| Set configuration for a PWM channel. | |
| int | sunxi_pwm_release (sunxi_pwm_t *pwm, int channel) |
| Release the PWM channel. | |
Variables | |
| static const uint32_t | pre_scal [][2] |
| Pre-scaler values for clock division. | |
System PWM (Pulse Width Modulation) driver for Allwinner (sunxi) platforms.
This file implements PWM functionality for Allwinner SoCs, supporting multiple channels with configurable frequency, duty cycle, polarity, and dead zone timing. It provides both single-channel and dual-channel binding modes, with support for pulse mode operations. The driver handles various clock sources including APB and oscillator clocks, and provides fine-grained control over PWM output parameters.
| #define PRESCALE_MAX 256 |
| #define sunxi_pwm_do_div | ( | n, | |
| base | |||
| ) |
Perform division with rounding for PWM calculations.
This macro performs a division operation on a given number n by base, and applies rounding to the result. The division is done by casting both operands to uint64_t to avoid overflow for large values. If the remainder is greater than half of base, the quotient is incremented by 1, implementing rounding to the nearest integer.
| n | The numerator, which will be updated with the quotient. |
| base | The divisor. |
|
inlinestatic |
Deinitialize the PWM clock and gate/reset control.
This function disables the clock and resets the PWM controller by clearing the appropriate bits in the clock gate and reset registers. It handles both bus and standalone clocks, depending on the configuration of the PWM structure.
| pwm | Pointer to the PWM controller structure. |
|
inlinestatic |
Initialize the PWM clock by configuring the bus and peripheral clocks.
This function initializes the PWM clock by enabling the necessary bus and peripheral clocks. It handles the clock reset and gate control for both the bus and peripheral clocks. If the PWM uses a bus clock, it will configure the bus clock by setting the reset bit and opening the clock gate. Similarly, if the PWM uses a separate clock, the function will configure it in the same manner.
| pwm | Pointer to the PWM controller structure. |
udelay(10)) to ensure proper clock stabilization after gating the clock. | void sunxi_pwm_deinit | ( | sunxi_pwm_t * | pwm | ) |
Deinitialize the PWM instance.
This function deinitializes the PWM instance by deactivating the clocks and re-initializing the GPIOs for each channel. It also marks the PWM as uninitialized (status set to false).
| pwm | Pointer to the PWM instance structure. |
|
inlinestatic |
Disable the PWM controller for a specific channel.
This function disables the PWM controller for the given channel by clearing the corresponding bit in the PWM controller's "PWM_PER" register.
| pwm | Pointer to the PWM controller structure. |
| channel | The PWM channel number (0-15) to disable. |
|
inlinestatic |
Enable the PWM controller for a specific channel.
This function enables the PWM controller for the given channel by setting the corresponding bit in the PWM controller's "PWM_PER" register.
| pwm | Pointer to the PWM controller structure. |
| channel | The PWM channel number (0-15) to enable. |
Get the PCC register offset for a given PWM channel.
This function returns the corresponding PWM PCCR register offset based on the input channel. The channels are mapped to the register offsets in a predefined array. If the provided channel is out of the valid range, the function returns the offset for channel 0.
| channel | The PWM channel number (0-15). |
Get the register offset for the PWM dead zone control register (PDZCR) for a specific channel.
This function maps each PWM channel to its corresponding PDZCR register offset. The function returns the register offset based on the given channel number. The mapping for channels is as follows:
| channel | The PWM channel number (0-15). |
|
inlinestatic |
Initialize the GPIO for a specific PWM channel.
This function initializes the GPIO pins associated with a given PWM channel. If the channel is bound to another channel (via a binding mode), the GPIO for the bound channel is also initialized. The initialization process involves configuring the pin and its multiplexing function as defined in the PWM controller structure.
| pwm | Pointer to the PWM controller structure. |
| channel | The PWM channel number (0-15) whose GPIO needs to be initialized. |
| void sunxi_pwm_init | ( | sunxi_pwm_t * | pwm | ) |
Initialize the PWM instance.
This function initializes the PWM instance by setting up the necessary clocks and marking the PWM as initialized (status set to true).
| pwm | Pointer to the PWM instance structure. |
|
inlinestatic |
Set the value of a specific field in a PWM register.
This function modifies a specific field in the PWM register by shifting and masking the data. It reads the current register value, applies the mask and shift to set the field value, and writes the modified value back to the register.
| reg | The base address of the register. |
| reg_shift | The bit position to shift the data for the specific field. |
| reg_width | The width (size) of the field to be modified (in bits). |
| data | The data to be written to the register field. |
| int sunxi_pwm_release | ( | sunxi_pwm_t * | pwm, |
| int | channel | ||
| ) |
Release the PWM channel.
This function releases the specified PWM channel. It checks if the PWM is initialized, validates the channel index, and then either releases a bound channel or a single channel based on the current channel mode.
| pwm | Pointer to the PWM instance structure. |
| channel | The PWM channel to release (0-based index). |
|
static |
Release a bound PWM channel.
This function handles the release of a bound PWM channel. It disables the clock for both the primary and bound channels, resets the clock source to the oscillator, and clears the corresponding dead zone control register for the first channel in the binding.
| pwm | Pointer to the PWM instance structure. |
| channel | The primary PWM channel to release (0-based index). |
|
static |
Release a single PWM channel.
This function handles the release of a single PWM channel. It disables the PWM controller for the specified channel, closes the clock gate, and resets the clock source to the oscillator.
| pwm | Pointer to the PWM instance structure. |
| channel | The PWM channel to release (0-based index). |
| int sunxi_pwm_set_config | ( | sunxi_pwm_t * | pwm, |
| int | channel, | ||
| sunxi_pwm_config_t * | config | ||
| ) |
Set configuration for a PWM channel.
This function sets the configuration for the specified PWM channel. It checks if the PWM is initialized, validates the channel index, and then applies the configuration either for a bound channel or a single channel based on the current channel mode.
| pwm | Pointer to the PWM instance structure. |
| channel | The PWM channel to configure (0-based index). |
| config | Pointer to the PWM configuration structure containing the settings. |
|
static |
Configure and bind a PWM channel with specific settings.
This function configures the PWM settings for the specified channel, binds it with another channel, and sets up various parameters such as clock source, duty cycle, dead time, active cycles, and period cycles. It also handles the configuration for pulse mode and GPIO initialization for the PWM output.
| pwm | Pointer to the PWM instance structure. |
| channel | The PWM channel to configure (0-based index). |
| config | Pointer to the PWM configuration structure. |
|
static |
Configures a single PWM channel with the given configuration parameters.
This function sets up the PWM configuration for a specific channel. It configures the clock source, the period, duty cycle, polarity, and other related settings based on the provided configuration. It also adjusts the frequency and duty cycle to ensure they are within valid ranges and applies the necessary register settings.
| pwm | Pointer to the sunxi_pwm_t structure representing the PWM controller. |
| channel | The PWM channel number to configure. |
| config | Pointer to a sunxi_pwm_config_t structure containing the PWM configuration parameters. |
period_ns in the configuration should not be zero and must be greater than duty_ns.
|
inlinestatic |
Set the polarity of a specific PWM channel.
This function sets the polarity for the specified PWM channel. The polarity determines the active state of the PWM signal (high or low). If the polarity is set to 1, the PWM signal is active high; if set to 0, the PWM signal is active low.
| pwm | Pointer to the PWM controller structure. |
| channel | The PWM channel number (0-15) to configure. |
| polarity | The desired polarity: 1 for active high, 0 for active low. |
|
static |
Pre-scaler values for clock division.
This constant array maps register values (reg_val) to corresponding clock pre-divider values (clk_pre_div). The array is used to configure the pre-scaler for clock division in PWM or similar hardware modules.
The first element in each pair is the register value, and the second element is the associated clock division factor.