PWM Interface

PWM Platform interface

PWM Overview

Pulse Width Modulation (PWM) is a powerful technique used to control the amount of power delivered to electrical devices, including motors, LEDs, and other components. PWM achieves this by varying the duty cycle of a periodic digital signal, effectively simulating an analog output.

PWM Modes

The following PWM modes are supported:

  • RS_PWM_MODE_ONE_SHOT : The PWM signal is generated once and stops after completing a single cycle.

  • RS_PWM_MODE_CONTINUOUS : The PWM signal is continuously generated, repeating the cycle indefinitely.

These modes allow flexibility in controlling the duration and repetition of the PWM signal based on the application requirements.

Application Interface

The below table captures the functions that are to be called from the application layer.

API Functions

Function

Description

rs_pwm_open

To open the pwm interface.

rs_pwm_configure_channel

To configure the pwm interface.

rs_pwm_start_channel

To start the pwm channel interface.

rs_pwm_stop_channel

To stop the pwm channel interface.

rs_pwm_on_channel

To on the pwm channel interface.

rs_pwm_off_channel

To off the pwm channel interface.

rs_pwm_set_channel_duty_cycle

To set the pwm channel dutycycle.

rs_pwm_set_channel_frequency

To set the pwm channel frequency.

rs_pwm_get_channel_config

To get the updated pwm channel config

rs_pwm_close

To close the pwm interface

Implementation Guide

Flint provide the PWM unit configuration and Channel configuration. Flint can have Digital Output Bridge to drive the PWM value.

  • Initialize the PWM unit

  • Configure the PWM channel under the unit

  • Register the callback function

  • Start the PWM conversion

  • Callback gets called where the next value can be updated

  • Stop the conversion once work is done

  • Un-Initialize the PWM unit

Error Code

  • Every API’s for the PWM returns some success or failure values. Please refer below section,

Documentation from the relevant header as follows:

PWM Specific APIs.

Defines macros and functions specific to PWM functionality

Author

Embien RAPIDSEA Team

Copyright

Embien Technologies India Pvt. Ltd.

Defines

RS_PWM_MODE_ONE_SHOT

Do it once as needed.

RS_PWM_MODE_CONTINUOUS

Convert continuously.

RS_PWM_OPERATION_MODE_BLOCKING

Blocking Mode operation.

RS_PWM_OPERATION_MODE_NON_BLOCKING

Non-Blocking Mode operation.

RS_PWM_ALIGNMENT_LEFT

PWM output aligned to left.

RS_PWM_ALIGNMENT_CENTER

PWM output aligned to center.

RS_PWM_TRIGGER_SOFTWARE

Triggered by software.

RS_PWM_TRIGGER_HARDWARE

Triggered by hardware.

RS_PWM_04_CHANNEL_INDEX

Index of the PWM Channel.

RS_PWM_22_CHANNEL_INDEX

Index of the PWM Channel.

RS_PWM_CHANNEL_DISABLE

disable output channel

RS_PWM_CHANNEL_ENABLE

enable output channel

RS_PWM_TRIGGER_COUNT_ENABLE

Triggered by software.

RS_PWM_TRIGGER_COUNT_DISABLE

Triggered by hardware.

RS_PWM_COUNTER_0

PWM counter.

RS_PWM_COUNTER_1

PWM counter.

RS_PWM_COUNTER_2

PWM counter.

RS_PWM_COUNTER_3

PWM counter.

Typedefs

typedef void (*rs_pwm_callback)(rs_handle_t handle, rs_ret_val_t reason, uint32_t u32_channel, void *ptr_args)

PWM callback function.

typedef struct tag_rs_pwm_config rs_pwm_config_t

Structure for setting up each PWM Unit configuration

typedef struct tag_rs_pwm_instance rs_pwm_instance_t

Contains information about PWM instance

typedef struct tag_rs_pwm_channel_config rs_pwm_channel_config_t

Structure for setting up each PWM Channel configuration

Functions

rs_handle_t rs_pwm_open(rs_pwm_instance_t *ptr_instance, rs_pwm_config_t *ptr_config)

Configures the PWM Unit.

This function opens the PWM Unit as per the given configuration

Parameters:
  • ptr_instance[in] - Pointer to the instance structure

  • ptr_config[in] - Pointer to the configuration structure

Returns:

0 on success or error code on failure

rs_ret_val_t rs_pwm_configure_channel(rs_handle_t handle, rs_pwm_channel_config_t *ptr_channel_config)

Configures the PWM channel.

This function configures/reconfigures the channel as per the given configuration

Parameters:
  • handle[in] - Handle to the PWM unit

  • ptr_channel_config[in] - Pointer to the channel config

Returns:

0 on success or error code on failure

rs_ret_val_t rs_pwm_start_channel(rs_handle_t handle, uint32_t u32_channel)

Starts the conversion for the given channel.

This function initiates the conversion operation for the given PWM channel

Parameters:
  • handle[in] - Handle to the PWM unit

  • u32_channel[in] - Channel number

Returns:

0 on success or error code on failure

rs_ret_val_t rs_pwm_stop_channel(rs_handle_t handle, uint32_t u32_channel)

Stops the conversion for the given channel.

This function stops the conversion operation for the given PWM channel

Parameters:
  • handle[in] - Handle to the PWM unit

  • u32_channel[in] - Channel number

Returns:

0 on success or error code on failure

rs_ret_val_t rs_pwm_on_channel(rs_handle_t handle, uint32_t u32_channel)

To on the PWM channel.

This function to on the PWM channel

Parameters:
  • handle[in] - Handle to the PWM unit

  • u32_channel[in] - Channel number of the PWM

Returns:

0 on success or error code on failure

rs_ret_val_t rs_pwm_off_channel(rs_handle_t handle, uint32_t u32_channel)

To off the PWM channel.

This function to off the PWM channel

Parameters:
  • handle[in] - Handle to the PWM unit

  • u32_channel[in] - Channel number of the PWM

Returns:

0 on success or error code on failure

rs_ret_val_t rs_pwm_set_channel_frequency(rs_handle_t handle, uint32_t u32_channel, uint32_t u32_frequency)

Set the PWM frequency of a unit on a given channel.

This function Set the PWM frequency of a unit on a given channel.

Parameters:
  • handle[in] - Handle to the PWM unit

  • u32_channel[in] - Channel number

  • u32_frequency[in] - Set a frequency of an given channel number

Returns:

0 on success or error code on failure

rs_ret_val_t rs_pwm_set_channel_duty_cycle(rs_handle_t handle, uint32_t u32_channel, uint32_t u32_duty_cycle)

Set the PWM duty cycle of a unit on a given channel.

This function Set the PWM duty cycle of a unit on a given channel.

Parameters:
  • handle[in] - Handle to the PWM unit

  • u32_channel[in] - Channel number

  • u32_duty_cycle[in] - set a duty cycle of an given channel number.

Returns:

0 on success or error code on failure

rs_ret_val_t rs_pwm_get_channel_config(rs_handle_t handle, uint32_t u32_channel, rs_pwm_channel_config_t *ptr_channel_config)

To get PWM configuration.

This function gets the channel configuration

Parameters:
  • handle[in] - Handle to the PWM unit

  • u32_channel[in] - Channel number

  • ptr_channel_config[in] - Pointer to the channel config

Returns:

0 on success or error code on failure

rs_ret_val_t rs_pwm_close(rs_handle_t handle)

Close the PWM Unit.

This function close the PWM Unit and releases the handle

Parameters:

handle[in] - Handle to the PWM Unit

Returns:

0 on success or error code on failure

struct tag_rs_pwm_config
#include <rs_pwm.h>

Structure for setting up each PWM Unit configuration

Public Members

uint8_t unit

Index to PWM Unit

uint8_t mode

Operation mode - One-shot, continuous etc

uint8_t alignment

Data alignment - edge or center

uint8_t oper_mode

Interrupt, Polling etc

uint8_t trigger

Trigger source

uint32_t output_freq

PWM Output Frequency

struct tag_rs_pwm_instance
#include <rs_pwm.h>

Contains information about PWM instance

Public Members

rs_pwm_config_t *ptr_config

Pointer to the PWM configuration

rs_pwm_callback *ptr_cb

Pointer to the callback function

void *ptr_arg

Pointer to the callback function argument

void *ptr_values

Buffer to store the input values in continuous interrupt mode

rs_handle_t hal_handle

HAL PWM handle

struct tag_rs_pwm_channel_config
#include <rs_pwm.h>

Structure for setting up each PWM Channel configuration

Public Members

uint8_t channel

Channel number inside the unit, Starting from 0 index

uint8_t group

Grouping of channel, if supported

uint8_t trigger

Trigger source

uint32_t duty_cycle

Duty cycle

uint32_t output_freq

PWM Output Frequency

uint16_t start_delay

Start delay

uint8_t counter

Counter

uint8_t channel_mode

channel mode enable or disable