RA Flexible Software Package Documentation  Release v5.2.0

 
Three-Phase PWM (r_gpt_three_phase)

Functions

fsp_err_t R_GPT_THREE_PHASE_Open (three_phase_ctrl_t *const p_ctrl, three_phase_cfg_t const *const p_cfg)
 
fsp_err_t R_GPT_THREE_PHASE_Stop (three_phase_ctrl_t *const p_ctrl)
 
fsp_err_t R_GPT_THREE_PHASE_Start (three_phase_ctrl_t *const p_ctrl)
 
fsp_err_t R_GPT_THREE_PHASE_Reset (three_phase_ctrl_t *const p_ctrl)
 
fsp_err_t R_GPT_THREE_PHASE_DutyCycleSet (three_phase_ctrl_t *const p_ctrl, three_phase_duty_cycle_t *const p_duty_cycle)
 
fsp_err_t R_GPT_THREE_PHASE_CallbackSet (three_phase_ctrl_t *const p_ctrl, void(*p_callback)(timer_callback_args_t *), void const *const p_context, timer_callback_args_t *const p_callback_memory)
 
fsp_err_t R_GPT_THREE_PHASE_Close (three_phase_ctrl_t *const p_ctrl)
 

Detailed Description

Driver for 3-phase motor control using the GPT peripheral on RA MCUs. This module implements the Three-Phase Interface.

Overview

The General PWM Timer (GPT) Three-Phase driver provides basic functionality for synchronously starting and stopping three PWM channels for use in 3-phase motor control applications. A function is additionally provided to allow setting duty cycle values for all three channels, optionally with double-buffering.

Features

The GPT Three-Phase driver provides the following functions:

Configuration

Build Time Configurations for r_gpt_three_phase

The following build time configurations are defined in fsp_cfg/r_gpt_three_phase_cfg.h:

ConfigurationOptionsDefaultDescription
Parameter Checking
  • Default (BSP)
  • Enabled
  • Disabled
Default (BSP) If selected code for parameter checking is included in the build.

Configurations for Timers > Three-Phase PWM (r_gpt_three_phase)

This module can be added to the Stacks tab via New Stack > Timers > Three-Phase PWM (r_gpt_three_phase). Non-secure callable guard functions can be generated for this module by right clicking the module in the RA Configuration tool and checking the "Non-secure Callable" box.

ConfigurationOptionsDefaultDescription
General
NameName must be a valid C symbolg_three_phase0 Module name.
Mode
  • Triangle-Wave Symmetric PWM
  • Triangle-Wave Asymmetric PWM
  • Triangle-Wave Asymmetric PWM (Mode 3)
Triangle-Wave Symmetric PWM Mode selection.
Triangle-Wave Symmetric PWM: Generates symmetric PWM waveforms with duty cycle determined by compare match set during a crest interrupt and updated at the next trough.
Triangle-Wave Asymmetric PWM: Generates asymmetric PWM waveforms with duty cycle determined by compare match set during a crest/trough interrupt and updated at the next trough/crest.
PeriodValue must be a non-negative integer less than or equal to 0x4000000000015 Specify the timer period in units selected below. Setting the period to 0x100000000 raw counts results in the maximum period. Set the period to 0x100000000 raw counts for a free running timer or an input capture configuration. The period can be set up to 0x40000000000, which will use a divider of 1024 with the maximum period.

If the requested period cannot be achieved, the settings with the largest possible period that is less than or equal to the requested period are used. The theoretical calculated period is printed in a comment in the generated timer_cfg_t structures for each timer.
Period Unit
  • Raw Counts
  • Nanoseconds
  • Microseconds
  • Milliseconds
  • Seconds
  • Hertz
  • Kilohertz
Kilohertz Unit of the period specified above
GPT U-ChannelValue must be an integer greater than or equal to 00 Specify the GPT channel for U signal output.
GPT V-ChannelValue must be an integer greater than or equal to 01 Specify the GPT channel for V signal output.
GPT W-ChannelValue must be an integer greater than or equal to 02 Specify the GPT channel for W signal output.
Callback Channel
  • U-Channel
  • V-Channel
  • W-Channel
U-Channel Specify the GPT channel to set a callback for when using R_GPT_THREE_PHASE_CallbackSet.
Buffer Mode
  • Single Buffer
  • Double Buffer
Single Buffer When Double Buffer is selected the 'duty_buffer' array in three_phase_duty_cycle_t is used as a buffer for the 'duty' array. This allows setting the duty cycle for the next two crest/trough events in asymmetric mode with only one call to R_GPT_THREE_PHASE_DutyCycleSet.
GTIOCA Stop Level
  • Pin Level Low
  • Pin Level High
Pin Level Low Select the behavior of the output pin when the timer is stopped.
GTIOCB Stop Level
  • Pin Level Low
  • Pin Level High
Pin Level Low Select the behavior of the output pin when the timer is stopped.
Extra Features
Extra Features > Dead Time
Dead Time Count Up (Raw Counts)Must be a valid non-negative integer with a maximum configurable value of 4294967295 (0xffffffff).0 Select the dead time to apply during up counting. This value also applies during down counting for the GPT32 and GPT16 variants.
Dead Time Count Down (Raw Counts) (GPTE/GPTEH only)Must be a valid non-negative integer with a maximum configurable value of 4294967295 (0xffffffff).0 Select the dead time to apply during down counting. This value only applies to the GPT32E and GPT32EH variants.

Clock Configuration

Please refer to the Timer, General PWM (r_gpt) section for more information.

Pin Configuration

Please refer to the Timer, General PWM (r_gpt) section for more information.

Usage Notes

Warning
Be sure the GTIOCA/B stop level and dead time values are set appropriately for your application before attempting to drive a motor. Failure to do so may result in damage to the motor drive circuitry and/or the motor itself if the timer is stopped by software.

Initial Setup

The following should be configured once the GPT Three-Phase module has been added to a project:

  1. Set "Pin Output Support" in one of the GPT submodules to "Enabled with Extra Features"
  2. Configure common settings in the GPT Three-Phase module properties
  3. Set the crest and trough interrupt priority and callback function in one of the three GPT submodules (if desired)
  4. Set the "Extra Features -> Output Disable" settings in each GPT submodule as needed for your application
Note
Because all three modules are operated synchronously with the same period interrupts only need to be enabled in one of the three GPT modules.

Buffer Modes

There are two buffering modes available for duty cycle values - single- and double-buffered. In single buffer mode only the values specified in the duty array element of three_phase_duty_cycle_t are used by R_GPT_THREE_PHASE_DutyCycleSet. At the next trough or crest event the output duty cycle will be internally updated to the set values.

In double buffer mode the duty_buffer array values are used as buffer values for the duty elements. Once passed to R_GPT_THREE_PHASE_DutyCycleSet, the next trough or crest event will update the output duty cycle to the values specified in duty as before. However, at the following crest or trough event the output duty cycle will be updated to the values in duty_buffer. This allows the duty cycle for both sides of an asymmetric PWM waveform to be set at only one trough or crest event per period instead of at every event.

Examples

GPT Three-Phase Basic Example

This is a basic example of minimal use of the GPT Three-Phase module in an application. The duty cycle is updated at every timer trough with the previously loaded buffer value, then the duty cycle buffer is reloaded in the trough interrupt callback.

void gpt_callback (timer_callback_args_t * p_args)
{
fsp_err_t err;
if (TIMER_EVENT_TROUGH == p_args->event)
{
/* Update duty cycle values (example) */
duty_cycle.duty[THREE_PHASE_CHANNEL_U] = get_duty_counts(THREE_PHASE_CHANNEL_U);
duty_cycle.duty[THREE_PHASE_CHANNEL_V] = get_duty_counts(THREE_PHASE_CHANNEL_V);
duty_cycle.duty[THREE_PHASE_CHANNEL_W] = get_duty_counts(THREE_PHASE_CHANNEL_W);
/* Update duty cycle values */
err = R_GPT_THREE_PHASE_DutyCycleSet(&g_gpt_three_phase_ctrl, &duty_cycle);
assert(FSP_SUCCESS == err);
}
else
{
/* Handle crest event. */
}
}
void gpt_three_phase_basic_example (void)
{
fsp_err_t err = FSP_SUCCESS;
/* Initializes the module. */
err = R_GPT_THREE_PHASE_Open(&g_gpt_three_phase_ctrl, &g_gpt_three_phase_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
/* Start the timer. */
(void) R_GPT_THREE_PHASE_Start(&g_gpt_three_phase_ctrl);
}

Data Structures

struct  gpt_three_phase_instance_ctrl_t
 

Data Structure Documentation

◆ gpt_three_phase_instance_ctrl_t

struct gpt_three_phase_instance_ctrl_t

Channel control block. DO NOT INITIALIZE. Initialization occurs when three_phase_api_t::open is called.

Function Documentation

◆ R_GPT_THREE_PHASE_Open()

fsp_err_t R_GPT_THREE_PHASE_Open ( three_phase_ctrl_t *const  p_ctrl,
three_phase_cfg_t const *const  p_cfg 
)

Initializes the 3-phase timer module (and associated timers) and applies configurations. Implements three_phase_api_t::open.

Example:

/* Initializes the module. */
err = R_GPT_THREE_PHASE_Open(&g_gpt_three_phase_ctrl, &g_gpt_three_phase_cfg);
Return values
FSP_SUCCESSInitialization was successful.
FSP_ERR_ASSERTIONA required input pointer is NULL.
FSP_ERR_ALREADY_OPENModule is already open.

◆ R_GPT_THREE_PHASE_Stop()

fsp_err_t R_GPT_THREE_PHASE_Stop ( three_phase_ctrl_t *const  p_ctrl)

Stops all timers synchronously. Implements three_phase_api_t::stop.

Return values
FSP_SUCCESSTimers successfully stopped.
FSP_ERR_ASSERTIONp_ctrl was NULL.
FSP_ERR_NOT_OPENThe instance is not opened.

◆ R_GPT_THREE_PHASE_Start()

fsp_err_t R_GPT_THREE_PHASE_Start ( three_phase_ctrl_t *const  p_ctrl)

Starts all timers synchronously. Implements three_phase_api_t::start.

Example:

/* Start the timer. */
(void) R_GPT_THREE_PHASE_Start(&g_gpt_three_phase_ctrl);
Return values
FSP_SUCCESSTimers successfully started.
FSP_ERR_ASSERTIONp_ctrl was NULL.
FSP_ERR_NOT_OPENThe instance is not opened.

◆ R_GPT_THREE_PHASE_Reset()

fsp_err_t R_GPT_THREE_PHASE_Reset ( three_phase_ctrl_t *const  p_ctrl)

Resets the counter values to 0. Implements three_phase_api_t::reset.

Return values
FSP_SUCCESSCounters were reset successfully.
FSP_ERR_ASSERTIONp_ctrl was NULL.
FSP_ERR_NOT_OPENThe instance is not opened.

◆ R_GPT_THREE_PHASE_DutyCycleSet()

fsp_err_t R_GPT_THREE_PHASE_DutyCycleSet ( three_phase_ctrl_t *const  p_ctrl,
three_phase_duty_cycle_t *const  p_duty_cycle 
)

Sets duty cycle for all three timers. Implements three_phase_api_t::dutyCycleSet.

In symmetric PWM mode duty cycle values are reflected after the next trough. In asymmetric PWM mode values are reflected at the next trough OR crest, whichever comes first.

When double-buffering is enabled the values in three_phase_duty_cycle_t::duty_buffer are set to the double-buffer registers. When values are reflected the first time the single buffer values (three_phase_duty_cycle_t::duty) are used. On the second reflection the duty_buffer values are used. In asymmetric PWM mode this enables both count-up and count-down PWM values to be set at trough (or crest) exclusively.

Note
It is recommended to call this function in a high-priority callback to ensure that it is not interrupted and that no GPT events occur during setting that would result in a duty cycle buffer load operation.

Example:

/* Update duty cycle values */
err = R_GPT_THREE_PHASE_DutyCycleSet(&g_gpt_three_phase_ctrl, &duty_cycle);
assert(FSP_SUCCESS == err);
Return values
FSP_SUCCESSDuty cycle updated successfully.
FSP_ERR_ASSERTIONp_ctrl was NULL
FSP_ERR_NOT_OPENThe instance is not opened.
FSP_ERR_INVALID_ARGUMENTOne or more duty cycle count values was outside the range 0..(period - 1).

◆ R_GPT_THREE_PHASE_CallbackSet()

fsp_err_t R_GPT_THREE_PHASE_CallbackSet ( three_phase_ctrl_t *const  p_ctrl,
void(*)(timer_callback_args_t *)  p_callback,
void const *const  p_context,
timer_callback_args_t *const  p_callback_memory 
)

Updates the user callback for the GPT U-channel with the option to provide memory for the callback argument structure. Implements three_phase_api_t::callbackSet.

Return values
FSP_SUCCESSCallback updated successfully.
FSP_ERR_ASSERTIONA required pointer is NULL.
FSP_ERR_NOT_OPENThe control block has not been opened.

◆ R_GPT_THREE_PHASE_Close()

fsp_err_t R_GPT_THREE_PHASE_Close ( three_phase_ctrl_t *const  p_ctrl)

Stops counters, disables output pins, and clears internal driver data. Implements three_phase_api_t::close.

Return values
FSP_SUCCESSSuccessful close.
FSP_ERR_ASSERTIONp_ctrl was NULL.
FSP_ERR_NOT_OPENThe instance is not opened.