This section introduces the Pulse-Width Modulation(PWM) APIs including terms and acronyms, supported features, software architecture, details on how to use this driver, PWM function groups, enums, structures and functions.
More...
This section introduces the Pulse-Width Modulation(PWM) APIs including terms and acronyms, supported features, software architecture, details on how to use this driver, PWM function groups, enums, structures and functions.
Terms and acronyms
| Terms | Details |
| PWM | Pulse-Width Modulation. PWM is a modulation technique used to encode a message into a pulsing signal. For more information, please refer to PWM in Wikipedia . |
Supported features
- Support polling mode.
The PWM generates the fixed waveform at the specified frequency and duty. The application can query current frequency and duty of the polling mode. The PWM does not support interrupt mode.
Software architecture of PWM
- PWM polling mode architecture.
Call hal_pwm_init() function to initialize the PWM source clock, then call hal_pwm_set_frequency() function to set the PWM frequency and get the total counter of the hardware. Total counter is PWM counter internal value at specified frequency. The duty cycle is calculated as the product of application's duty ratio and total counter. Duty ratio is the ratio of duty counter over the total counter.
Call hal_pwm_set_duty_cycle() function to set the PWM duty cycle. Call hal_pwm_start() function to trigger PWM execution.
Call hal_pwm_get_frequency() and hal_pwm_get_duty_cycle() functions if the current frequency and duty cycle need to be retrieved.
Call hal_pwm_get_running_status() function to get the PWM status, either busy or idle. Call hal_pwm_stop() function to stop the PWM execution.
Polling mode architecture is similar to the polling mode architecture in HAL overview. See HAL Driver Model for polling mode architecture.
How to use this driver
- Using PWM in polling mode.
Calculate the PWM frequency and duty cycle at fixed source clock. Assuming the source clock is 32kHz and the applicaton needs a waveform at 200Hz frequency and 50% duty ratio.The calculations of division clock, total count and duty cycle are based on the formuals as shown below.
- division_clock = source_clock/division
- total_count = division_clock/frequency
- duty_cycle = (total_count*duty_ratio)/100
- Set count to PWM_COUNT register
- Set duty_cycle to PWM_THRES register
This function deinitializes the PWM hardware.
- Parameters
-
| [in] | pwm_channel | is the PWM channel number. For more details about the parameter, please refer to hal_pwm_channel_t. |
- Returns
- To indicate whether this function call is successful or not. If the return value is HAL_PWM_STATUS_OK, the operation completed successfully. If the return value is HAL_PWM_STATUS_INVALID_PARAMETER, a wrong parameter is given. The parameter needs to be verified.
- Example
- For a sample code, please refer to How to use this driver.
- See also
- hal_pwm_init()
This function gets the current duty cycle of the PWM.
- Parameters
-
| [in] | pwm_channel | is the PWM channel number. For more details about the parameter, please refer to hal_pwm_channel_t. |
| [out] | *duty_cycle | is PWM hardware duty cycle, which is calculated as a product of application's duty ratio and hardware 's total count. |
- Returns
- To indicate whether this function call is successful or not. If the return value is HAL_PWM_STATUS_OK, the operation completed successfully. If the return value is HAL_PWM_STATUS_INVALID_PARAMETER, a wrong parameter is given. The parameter needs to be verified.
- Example
- For a sample code, please refer to How to use this driver.
- See also
- hal_pwm_set_duty_cycle()
This function gets current frequency of the PWM, the unit of frequency is Hz.
- Parameters
-
| [in] | pwm_channel | is the PWM channel number. For more details about the parameter, please refer to hal_pwm_channel_t. |
| [out] | frequency | is PWM output frequency. |
- Returns
- To indicate whether this function call is successful or not. If the return value is HAL_PWM_STATUS_OK, the operation completed successfully. If the return value is HAL_PWM_STATUS_INVALID_PARAMETER, a wrong parameter is given. The parameter needs to be verified.
- Example
- For a sample code, please refer to How to use this driver.
- See also
- hal_pwm_set_frequency()
This function gets the current status of PWM.
- Parameters
-
| [in] | pwm_channel | is the PWM channel number. For more details about the parameter, please refer to hal_pwm_channel_t. |
| [out] | running_status | is PWM busy or idle status, For details about this parameter, please refer to hal_pwm_running_status_t . |
- Returns
- To indicate whether this function call is successful or not. If the return value is HAL_PWM_STATUS_OK, the operation completed successfully. If the return value is HAL_PWM_STATUS_INVALID_PARAMETER, a wrong parameter is given. The parameter needs to be verified.
- Example
- For a sample code, please refer to How to use this driver.
This function initializes the PWM hardware source clock.
- Parameters
-
| [in] | pwm_channel | is the PWM channel number. For more details about the parameter, please refer to hal_pwm_channel_t. |
| [in] | source_clock | is the PWM source clock. For more details about the parameter, please refer to hal_pwm_source_clock_t. |
- Returns
- To indicate whether this function call is successful or not. If the return value is HAL_PWM_STATUS_OK, the operation completed successfully. If the return value is HAL_PWM_STATUS_INVALID_PARAMETER, a wrong parameter is given. The parameter needs to be verified.
- Example
- For a sample code, please refer to How to use this driver.
- Note
- There are some limitations about the minimum frequency that we can set if we choose the related source clock list in hal_pwm_source_clock_t, and the caculation formula is as follows:
The minimum frequency = ( the chosen source clock) / ( 0X1FFF * advanced_config )
For example:
if we choose the clock of HAL_PWM_CLOCK_32KHZ, the minimum frequency that we can set is 0.5 Hz;
if we choose the clock of HAL_PWM_CLOCK_13MHZ, the minimum frequency that we can set is 199 Hz;
- Warning
- For this chip, we can config the PWM channel and the related clock, which means different PWM channels can use the different clock.
- See also
- hal_pwm_deinit()
This function sets the PWM advanced configuration.
- Parameters
-
| [in] | pwm_channel | is the PWM channel number. For more details about the parameter, please refer to hal_pwm_channel_t. |
| [in] | advanced_config | is PWM source clock division value, For more details about this parameter, please refer to hal_pwm_advanced_config_t. |
- Returns
- To indicate whether this function call is successful or not. If the return value is HAL_PWM_STATUS_OK, the operation completed successfully. If the return value is HAL_PWM_STATUS_INVALID_PARAMETER, a wrong parameter is given. The parameter needs to be verified.
- Example
- For a sample code, please refer to How to use this driver.
This function sets the PWM duty cycle.
- Parameters
-
| [in] | pwm_channel | is the PWM channel number. For more details about the parameter, please refer to hal_pwm_channel_t. |
| [in] | duty_cycle | is the PWM hardware duty cycle, which is calculated as a product of application's duty ratio and hardware 's total count. |
- Returns
- To indicate whether this function call is successful or not. If the return value is HAL_PWM_STATUS_OK, the operation completed successfully. If the return value is HAL_PWM_STATUS_INVALID_PARAMETER, a wrong parameter is given. The parameter needs to be verified.
- Example
- For a sample code, please refer to How to use this driver.
- See also
- hal_pwm_get_duty_cycle()
This function sets the PWM frequency and retrieves total count of the PWM hardware at the specified frequency.
- Parameters
-
| [in] | pwm_channel | is the PWM channel number. For more details about the parameter, please refer to hal_pwm_channel_t. |
| [in] | frequency | is the PWM output frequency. |
| [out] | total_count | is PWM hardware total count, the value of this parameter varies based on the given PWM frequency. |
- Returns
- To indicate whether this function call is successful or not. If the return value is HAL_PWM_STATUS_OK, the operation completed successfully. If the return value is HAL_PWM_STATUS_INVALID_PARAMETER, a wrong parameter is given. The parameter needs to be verified.
- Example
- For a sample code, please refer to How to use this driver.
This function starts the PWM execution.
- Parameters
-
| [in] | pwm_channel | is the PWM channel number. For more details about the parameter, please refer to hal_pwm_channel_t. |
- Returns
- To indicate whether this function call is successful or not. If the return value is HAL_PWM_STATUS_OK, the operation completed successfully. If the return value is HAL_PWM_STATUS_INVALID_PARAMETER, a wrong parameter is given. The parameter needs to be verified.
- Example
- For a sample code, please refer to How to use this driver.
- See also
- hal_pwm_stop()
This function stops the PWM execution.
- Parameters
-
| [in] | pwm_channel | is PWM channel number. For more details about the parameter, please refer to hal_pwm_channel_t. |
- Returns
- To indicate whether this function call is successful or not. If the return value is HAL_PWM_STATUS_OK, the operation completed successfully. If the return value is HAL_PWM_STATUS_INVALID_PARAMETER, a wrong parameter is given. The parameter needs to be verified.
- Example
- For a sample code, please refer to How to use this driver.
- See also
- hal_pwm_start()