Skip to content

Commit d20c5b8

Browse files
Hieu NguyenKhiemNguyenT
authored andcommitted
hal: renesas: rza: Initial PWM support for RZ/A series
Add MTU FSP HAL driver to support PWM driver for RZ/A series Signed-off-by: Hieu Nguyen <[email protected]> Signed-off-by: Nhut Nguyen <[email protected]>
1 parent ad1ae61 commit d20c5b8

File tree

4 files changed

+2197
-0
lines changed

4 files changed

+2197
-0
lines changed

drivers/rz/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,6 @@ else()
8282
zephyr_library_sources_ifdef(CONFIG_USE_RZ_FSP_SCI_UART
8383
fsp/src/${SOC_SERIES_PREFIX}/r_sci_uart/r_sci_uart.c)
8484
endif()
85+
86+
zephyr_library_sources_ifdef(CONFIG_USE_RZ_FSP_MTU
87+
fsp/src/${SOC_SERIES_PREFIX}/r_mtu3/r_mtu3.c)
Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
/*
2+
* Copyright (c) 2020 - 2024 Renesas Electronics Corporation and/or its affiliates
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#ifndef R_MTU3_H
8+
#define R_MTU3_H
9+
10+
/*******************************************************************************************************************//**
11+
* @addtogroup MTU3
12+
* @{
13+
**********************************************************************************************************************/
14+
15+
/***********************************************************************************************************************
16+
* Includes
17+
**********************************************************************************************************************/
18+
#include "bsp_api.h"
19+
#include "r_timer_api.h"
20+
21+
/* Common macro for FSP header files. There is also a corresponding FSP_FOOTER macro at the end of this file. */
22+
FSP_HEADER
23+
24+
/***********************************************************************************************************************
25+
* Macro definitions
26+
**********************************************************************************************************************/
27+
28+
/***********************************************************************************************************************
29+
* Typedef definitions
30+
**********************************************************************************************************************/
31+
32+
/** I/O Level Select */
33+
typedef enum e_mtu3_io_pin_level
34+
{
35+
MTU3_IO_PIN_LEVEL_NO_OUTPUT = 0, ///< Output prohibited
36+
MTU3_IO_PIN_LEVEL_INITIAL_LOW_COMPARE_LOW = 1, ///< Initial output is low. Low output at compare match.
37+
MTU3_IO_PIN_LEVEL_INITIAL_LOW_COMPARE_HIGH = 2, ///< Initial output is low. High output at compare match.
38+
MTU3_IO_PIN_LEVEL_INITIAL_LOW_COMPARE_TOGGLE = 3, ///< Initial output is low. Toggle output at compare match.
39+
MTU3_IO_PIN_LEVEL_INITIAL_HIGH_COMPARE_LOW = 5, ///< Initial output is high. Low output at compare match.
40+
MTU3_IO_PIN_LEVEL_INITIAL_HIGH_COMPARE_HIGH = 6, ///< Initial output is high. High output at compare match.
41+
MTU3_IO_PIN_LEVEL_INITIAL_HIGH_COMPARE_TOGGLE = 7, ///< Initial output is high. Toggle output at compare match.
42+
MTU3_IO_PIN_LEVEL_INPUT_RISING_EDGE = 8, ///< Input capture at rising edge.
43+
MTU3_IO_PIN_LEVEL_INPUT_FALLING_EDGE = 9, ///< Input capture at falling edge.
44+
MTU3_IO_PIN_LEVEL_INPUT_BOTH_EDGE = 10, ///< Input capture at both edges.
45+
} mtu3_io_pin_level_t;
46+
47+
/** Clock Edge Select */
48+
typedef enum e_mtu3_clock_edge
49+
{
50+
MTU3_CLOCK_EDGE_RISING = 0x0, ///< Count at rising edge
51+
MTU3_CLOCK_EDGE_FALLING = 0x1, ///< Count at falling edge
52+
MTU3_CLOCK_EDGE_BOTH = 0x2, ///< Count at both edges
53+
} mtu3_clock_edge_t;
54+
55+
/** Time Prescaler Select */
56+
typedef enum e_mtu3_clock_div
57+
{
58+
MTU3_CLOCK_DIV_1 = 0x0, ///< CLOCK divided by 1 (common ch)
59+
MTU3_CLOCK_DIV_4 = 0x1, ///< CLOCK divided by 4 (common ch)
60+
MTU3_CLOCK_DIV_16 = 0x2, ///< CLOCK divided by 16 (common ch)
61+
MTU3_CLOCK_DIV_64 = 0x3, ///< CLOCK divided by 64 (common ch)
62+
MTU3_CLOCK_DIV_2 = 0x8, ///< CLOCK divided by 2 (common ch)
63+
MTU3_CLOCK_DIV_8 = 0x10, ///< CLOCK divided by 8 (common ch)
64+
MTU3_CLOCK_DIV_32 = 0x18, ///< CLOCK divided by 32 (common ch)
65+
66+
MTU3_CLOCK_DIV_MTCLKA_CH_0 = 0x4, ///< External clock: counts on MTCLKA pin input (ch0)
67+
MTU3_CLOCK_DIV_MTCLKB_CH_0 = 0x5, ///< External clock: counts on MTCLKB pin input (ch0)
68+
MTU3_CLOCK_DIV_MTCLKC_CH_0 = 0x6, ///< External clock: counts on MTCLKC pin input (ch0)
69+
MTU3_CLOCK_DIV_MTCLKD_CH_0 = 0x7, ///< External clock: counts on MTCLKD pin input (ch0)
70+
MTU3_CLOCK_DIV_256_CH_0 = 0x20, ///< CLOCK divided by 256 (ch0)
71+
MTU3_CLOCK_DIV_1024_CH_0 = 0x28, ///< CLOCK divided by 1024 (ch0)
72+
MTU3_CLOCK_DIV_MTIOC1A_CH_0 = 0x38, ///< External clock: counts on MTIOC1A pin input (ch0)
73+
74+
MTU3_CLOCK_DIV_MTCLKA_CH_1 = 0x4, ///< External clock: counts on MTCLKA pin input (ch1)
75+
MTU3_CLOCK_DIV_MTCLKB_CH_1 = 0x5, ///< External clock: counts on MTCLKB pin input (ch1)
76+
MTU3_CLOCK_DIV_256_CH_1 = 0x6, ///< CLOCK divided by 256 (ch1)
77+
MTU3_CLOCK_DIV_TCNT_CH1 = 0x7, ///< Overflow/underflow of MTU2.TCNT
78+
MTU3_CLOCK_DIV_1024_CH_1 = 0x20, ///< CLOCK divided by 1024 (ch1)
79+
80+
MTU3_CLOCK_DIV_MTCLKA_CH_2 = 0x4, ///< External clock: counts on MTCLKA pin input (ch2)
81+
MTU3_CLOCK_DIV_MTCLKB_CH_2 = 0x5, ///< External clock: counts on MTCLKB pin input (ch2)
82+
MTU3_CLOCK_DIV_MTCLKC_CH_2 = 0x6, ///< External clock: counts on MTCLKC pin input (ch2)
83+
MTU3_CLOCK_DIV_1024_CH_2 = 0x7, ///< CLOCK divided by 1024 (ch2)
84+
MTU3_CLOCK_DIV_256_CH_2 = 0x20, ///< CLOCK divided by 256 (ch2)
85+
86+
MTU3_CLOCK_DIV_256_CH_3_4_6_7_8 = 0x4, ///< CLOCK divided by 256 (ch3-4, 6-8)
87+
MTU3_CLOCK_DIV_1024_CH_3_4_6_7_8 = 0x5, ///< CLOCK divided by 1024 (ch3-4, 6-8)
88+
MTU3_CLOCK_DIV_MTCLKA_CH_3_4_6_7_8 = 0x6, ///< External clock: counts on MTCLKA pin input (ch3-4, 6-8)
89+
MTU3_CLOCK_DIV_MTCLKB_CH_3_4_6_7_8 = 0x7, ///< External clock: counts on MTCLKB pin input (ch3-4, 6-8)
90+
} mtu3_clock_div_t;
91+
92+
/** Counter Clear Source Select */
93+
typedef enum e_mtu3_tcnt_clear
94+
{
95+
MTU3_TCNT_CLEAR_DISABLE = 0x0, ///< TCNT clearing disabled
96+
MTU3_TCNT_CLEAR_TGRA = 0x1, ///< TCNT cleared by TGRA compare match/input capture
97+
MTU3_TCNT_CLEAR_TGRB = 0x2, ///< TCNT cleared by TGRB compare match/input capture
98+
} mtu3_tcnt_clear_t;
99+
100+
/** Level of MTU3 pin */
101+
typedef enum e_mtu3_io_pin
102+
{
103+
MTU3_IO_PIN_MTIOCA = 0, ///< MTIOCA
104+
MTU3_IO_PIN_MTIOCB = 1, ///< MTIOCB
105+
MTU3_IO_PIN_MTIOCA_AND_MTIOCB = 2, ///< MTIOCA and MTIOCB
106+
} mtu3_io_pin_t;
107+
108+
/** Configurations for output pins. */
109+
typedef struct st_mtu3_output_pin
110+
{
111+
mtu3_io_pin_level_t output_pin_level_a; ///< I/O Control A
112+
mtu3_io_pin_level_t output_pin_level_b; ///< I/O Control B
113+
} mtu3_output_pin_t;
114+
115+
/** Disables or enables the noise filter for input from the MTIOCnA pin */
116+
typedef enum e_mtu3_noise_filter
117+
{
118+
MTU3_NOISE_FILTER_DISABLE = 0U, ///< The noise filter for the MTIOC pin is disabled
119+
MTU3_NOISE_FILTER_A_ENABLE = 1U, ///< The noise filter for the MTIOCA pin is enabled
120+
MTU3_NOISE_FILTER_B_ENABLE = 2U, ///< The noise filter for the MTIOCB pin is enabled
121+
} mtu3_noise_filter_t;
122+
123+
/** Disables or enables the noise filter for the external clock input pins of the MTU */
124+
typedef enum e_mtu3_noise_filter_external
125+
{
126+
MTU3_NOISE_FILTER_MTCLK_DISABLE = 0U, ///< The noise filter for the MTCLK pin is disabled
127+
MTU3_NOISE_FILTER_MTCLK_A_ENABLE = 1U, ///< The noise filter for the MTCLKA pin is enabled.
128+
MTU3_NOISE_FILTER_MTCLK_B_ENABLE = 2U, ///< The noise filter for the MTCLKB pin is enabled.
129+
MTU3_NOISE_FILTER_MTCLK_C_ENABLE = 4U, ///< The noise filter for the MTCLKC pin is enabled.
130+
MTU3_NOISE_FILTER_MTCLK_D_ENABLE = 8U, ///< The noise filter for the MTCLKD pin is enabled.
131+
} mtu3_noise_filter_mtclk_t;
132+
133+
typedef enum e_mtu3_noise_filter_clock
134+
{
135+
MTU3_NOISE_FILTER_CLOCK_DIV_1 = 0U, ///< CLK/1 - fast sampling
136+
MTU3_NOISE_FILTER_CLOCK_DIV_8 = 1U, ///< CLK/8
137+
MTU3_NOISE_FILTER_CLOCK_DIV_32 = 2U, ///< CLK/32 - slow sampling
138+
MTU3_NOISE_FILTER_CLOCK_SOURCE = 3U, ///< Clock source for counting
139+
} mtu3_noise_filter_clock_t;
140+
141+
typedef enum e_mtu3_noise_filter_external_clock
142+
{
143+
MTU3_NOISE_FILTER_EXTERNAL_CLOCK_DIV_1 = 0U, ///< CLK/1 - fast sampling
144+
MTU3_NOISE_FILTER_EXTERNAL_CLOCK_DIV_2 = 1U, ///< CLK/2
145+
MTU3_NOISE_FILTER_EXTERNAL_CLOCK_DIV_8 = 2U, ///< CLK/8
146+
MTU3_NOISE_FILTER_EXTERNAL_CLOCK_DIV_32 = 3U, ///< CLK/32 - slow sampling
147+
} mtu3_noise_filter_external_clock_t;
148+
149+
/** Interrupt Skipping Function Select */
150+
typedef enum e_mtu3_interrupt_skip_mode
151+
{
152+
MTU3_INTERRUPT_SKIP_MODE_1 = 0U, ///< Setting the TITCR1A or TITCR1B register enables
153+
MTU3_INTERRUPT_SKIP_MODE_2 = 1U, ///< Setting the TITCR2A or TITCR2B register enables
154+
} mtu3_interrupt_skip_mode_t;
155+
156+
/** Number of interrupts to skip between events */
157+
typedef enum e_mtu3_interrupt_skip_count
158+
{
159+
MTU3_INTERRUPT_SKIP_COUNT_0 = 0U, ///< Do not skip interrupts
160+
MTU3_INTERRUPT_SKIP_COUNT_1, ///< Skip one interrupt
161+
MTU3_INTERRUPT_SKIP_COUNT_2, ///< Skip two interrupts
162+
MTU3_INTERRUPT_SKIP_COUNT_3, ///< Skip three interrupts
163+
MTU3_INTERRUPT_SKIP_COUNT_4, ///< Skip four interrupts
164+
MTU3_INTERRUPT_SKIP_COUNT_5, ///< Skip five interrupts
165+
MTU3_INTERRUPT_SKIP_COUNT_6, ///< Skip six interrupts
166+
MTU3_INTERRUPT_SKIP_COUNT_7, ///< Skip seven interrupts
167+
} mtu3_interrupt_skip_count_t;
168+
169+
/** Trigger options to start A/D conversion. */
170+
typedef enum e_mtu3_adc_compare_match
171+
{
172+
MTU3_ADC_COMPARE_MATCH_ADC_A = 0x1U, ///< Set A/D conversion start request value for MTU3 A/D converter start request A
173+
MTU3_ADC_COMPARE_MATCH_ADC_B = 0x2U, ///< Set A/D conversion start request value for MTU3 A/D converter start request B
174+
} mtu3_adc_compare_match_t;
175+
176+
typedef enum e_mtu3_adc_tgra_compare_match
177+
{
178+
MTU3_ADC_TGRA_COMPARE_MATCH_DISABLE = 0U, ///< A/D Converter Activation by TGRA Input Capture/Compare Match Disable
179+
MTU3_ADC_TGRA_COMPARE_MATCH_ENABLE = 1U, ///< A/D Converter Activation by TGRA Input Capture/Compare Match Enable
180+
} mtu3_adc_activation_tgra_compare_match_t;
181+
182+
/** Channel control block. DO NOT INITIALIZE. Initialization occurs when @ref timer_api_t::open is called. */
183+
typedef struct st_mtu3_instance_ctrl
184+
{
185+
uint32_t open; ///< Whether or not channel is open
186+
const timer_cfg_t * p_cfg; ///< Pointer to initial configurations
187+
void * p_reg; ///< Base register for this channel
188+
R_MTU_Type * p_reg_com; ///< Base register for this channel(common ch)
189+
void * p_reg_nf; ///< Base register for this channel(noise fileter)
190+
uint32_t channel_mask; ///< Channel bitmask
191+
192+
void (* p_callback)(timer_callback_args_t *); ///< Pointer to callback
193+
timer_callback_args_t * p_callback_memory; ///< Pointer to optional callback argument memory
194+
void const * p_context; ///< Pointer to context to be passed into callback function
195+
} mtu3_instance_ctrl_t;
196+
197+
/** MTU3 extension for advanced PWM features. */
198+
typedef struct st_mtu3_extended_pwm_cfg
199+
{
200+
mtu3_interrupt_skip_mode_t interrupt_skip_mode_a; ///< Selects interrupt skipping function 1 or 2(TIMTRA)
201+
mtu3_interrupt_skip_mode_t interrupt_skip_mode_b; ///< Selects interrupt skipping function 1 or 2(TIMTRB)
202+
uint16_t adc_a_compare_match; ///< Timer A/D Converter Start Request Cycle A (MTU4 or MTU7)
203+
uint16_t adc_b_compare_match; ///< Timer A/D Converter Start Request Cycle B (MTU4 or MTU7)
204+
mtu3_interrupt_skip_count_t interrupt_skip_count_tciv4; ///< TCIV4 Interrupt Skipping Count Setting(TITCR1A)
205+
mtu3_interrupt_skip_count_t interrupt_skip_count_tgia3; ///< TGIA3 Interrupt Skipping Count Setting(TITCR1A)
206+
mtu3_interrupt_skip_count_t interrupt_skip_count_tciv7; ///< TCIV7 Interrupt Skipping Count Setting(TITCR1B)
207+
mtu3_interrupt_skip_count_t interrupt_skip_count_tgia6; ///< TGIA6 Interrupt Skipping Count Setting(TITCR1B)
208+
mtu3_interrupt_skip_count_t interrupt_skip_count_tgr4an_bn; ///< TRG4AN/TRG4BN Interrupt Skipping Count Setting(TITCR2A)
209+
mtu3_interrupt_skip_count_t interrupt_skip_count_tgr7an_bn; ///< TRG7AN/TRG7BN Interrupt Skipping Count Setting(TITCR2B)
210+
} mtu3_extended_pwm_cfg_t;
211+
212+
/** The MTU3 extension constitutes a unique feature of MTU3. */
213+
typedef struct st_mtu3_extended_cfg
214+
{
215+
uint32_t tgra_val; ///< Capture/Compare match A register
216+
uint32_t tgrb_val; ///< Capture/Compare match B register
217+
uint32_t tgrc_val; ///< Capture/Compare match C register (Does not exist in MTU ch1-2)
218+
uint32_t tgrd_val; ///< Capture/Compare match D register (Does not exist in MTU ch1-2)
219+
mtu3_clock_div_t mtu3_clk_div; ///< Time Prescaler Select
220+
mtu3_clock_edge_t clk_edge; ///< Clock Edge Select
221+
mtu3_tcnt_clear_t mtu3_clear; ///< Counter Clear Source Select
222+
mtu3_output_pin_t mtioc_ctrl_setting; ///< I/O Control A, B
223+
224+
/* Debounce filter for MTIOCxA or MTIOCxB input signal pin. */
225+
mtu3_noise_filter_t noise_filter_mtioc_setting;
226+
mtu3_noise_filter_clock_t noise_filter_mtioc_clk;
227+
228+
/* Debounce filter for MTCLKx input signal pin. */
229+
mtu3_noise_filter_mtclk_t noise_filter_mtclk_setting;
230+
mtu3_noise_filter_external_clock_t noise_filter_mtclk_clk;
231+
232+
/* A/D Converter Activation by TGRA Input Capture/Compare Match. */
233+
mtu3_adc_activation_tgra_compare_match_t adc_activation_setting;
234+
235+
/* Used for other than MTU5 */
236+
uint8_t capture_a_ipl; ///< Capture/Compare match A interrupt priority
237+
uint8_t capture_b_ipl; ///< Capture/Compare match B interrupt priority
238+
IRQn_Type capture_a_irq; ///< Capture/Compare match A interrupt
239+
IRQn_Type capture_b_irq; ///< Capture/Compare match B interrupt
240+
241+
mtu3_extended_pwm_cfg_t const * p_pwm_cfg; ///< Advanced PWM features, optional
242+
} mtu3_extended_cfg_t;
243+
244+
/**********************************************************************************************************************
245+
* Exported global variables
246+
**********************************************************************************************************************/
247+
248+
/** @cond INC_HEADER_DEFS_SEC */
249+
/** Filled in Interface API structure for this Instance. */
250+
extern const timer_api_t g_timer_on_mtu3;
251+
252+
/** @endcond */
253+
254+
/***********************************************************************************************************************
255+
* Public APIs
256+
**********************************************************************************************************************/
257+
fsp_err_t R_MTU3_Open(timer_ctrl_t * const p_ctrl, timer_cfg_t const * const p_cfg);
258+
fsp_err_t R_MTU3_Stop(timer_ctrl_t * const p_ctrl);
259+
fsp_err_t R_MTU3_Start(timer_ctrl_t * const p_ctrl);
260+
fsp_err_t R_MTU3_Reset(timer_ctrl_t * const p_ctrl);
261+
fsp_err_t R_MTU3_PeriodSet(timer_ctrl_t * const p_ctrl, uint32_t const period_counts);
262+
fsp_err_t R_MTU3_DutyCycleSet(timer_ctrl_t * const p_ctrl, uint32_t const duty_cycle_counts, uint32_t const pin);
263+
fsp_err_t R_MTU3_CompareMatchSet(timer_ctrl_t * const p_ctrl,
264+
uint32_t const compare_match_value,
265+
timer_compare_match_t const match_channel);
266+
fsp_err_t R_MTU3_InfoGet(timer_ctrl_t * const p_ctrl, timer_info_t * const p_info);
267+
fsp_err_t R_MTU3_StatusGet(timer_ctrl_t * const p_ctrl, timer_status_t * const p_status);
268+
fsp_err_t R_MTU3_CounterSet(timer_ctrl_t * const p_ctrl, uint32_t counter);
269+
fsp_err_t R_MTU3_OutputEnable(timer_ctrl_t * const p_ctrl, mtu3_output_pin_t pin_level);
270+
fsp_err_t R_MTU3_OutputDisable(timer_ctrl_t * const p_ctrl, mtu3_io_pin_t pin);
271+
fsp_err_t R_MTU3_Enable(timer_ctrl_t * const p_ctrl);
272+
fsp_err_t R_MTU3_Disable(timer_ctrl_t * const p_ctrl);
273+
fsp_err_t R_MTU3_AdcTriggerSet(timer_ctrl_t * const p_ctrl,
274+
mtu3_adc_compare_match_t which_compare_match,
275+
uint16_t compare_match_value);
276+
fsp_err_t R_MTU3_CallbackSet(timer_ctrl_t * const p_api_ctrl,
277+
void ( * p_callback)(timer_callback_args_t *),
278+
void const * const p_context,
279+
timer_callback_args_t * const p_callback_memory);
280+
fsp_err_t R_MTU3_Close(timer_ctrl_t * const p_ctrl);
281+
282+
/*******************************************************************************************************************//**
283+
* @} (end defgroup MTU3)
284+
**********************************************************************************************************************/
285+
286+
/* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */
287+
FSP_FOOTER
288+
289+
#endif

0 commit comments

Comments
 (0)