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_AGT_H
8 #define R_AGT_H
9 
10 /***********************************************************************************************************************
11  * Includes
12  **********************************************************************************************************************/
13 #include "bsp_api.h"
14 #include "r_agt_cfg.h"
15 #include "r_timer_api.h"
16 
17 /* Common macro for FSP header files. There is also a corresponding FSP_FOOTER macro at the end of this file. */
18 FSP_HEADER
19 
20 /***********************************************************************************************************************
21  * Macro definitions
22  **********************************************************************************************************************/
23 
24 /* Leading zeroes removed to avoid coding standards violation. */
25 
26 /** Maximum number of clock counts for standard AGT peripheral. */
27 #define AGT_MAX_CLOCK_COUNTS_16BIT    (UINT16_MAX)
28 
29 /** Maximum number of clock counts for AGTW peripheral. */
30 #define AGT_MAX_CLOCK_COUNTS_32BIT    (UINT32_MAX)
31 
32 /** Maximum period value allowed for standard AGT peripheral. */
33 #define AGT_MAX_PERIOD_16BIT          (UINT16_MAX + 1U)
34 
35 /** Maximum period valud allowed for AGTW peripheral. */
36 #define AGT_MAX_PERIOD_32BIT          (UINT32_MAX)
37 
38 /*******************************************************************************************************************//**
39  * @addtogroup AGT
40  * @{
41  **********************************************************************************************************************/
42 
43 /***********************************************************************************************************************
44  * Typedef definitions
45  **********************************************************************************************************************/
46 
47 /** Count source */
48 typedef enum e_agt_clock
49 {
50     AGT_CLOCK_PCLKB         = 0x00,    ///< PCLKB count source, division by 1, 2, or 8 allowed
51     AGT_CLOCK_LOCO          = 0x40,    ///< LOCO count source, division by 1, 2, 4, 8, 16, 32, 64, or 128 allowed
52     AGT_CLOCK_AGT_UNDERFLOW = 0x50,    ///< Underflow event signal from next lowest AGT channel, division must be 1
53     AGT_CLOCK_SUBCLOCK      = 0x60,    ///< Subclock count source, division by 1, 2, 4, 8, 16, 32, 64, or 128 allowed
54     AGT_CLOCK_P402          = 0x92,    ///< Counts events on P402, events are counted in deep software standby mode
55     AGT_CLOCK_P403          = 0x93,    ///< Counts events on P403, events are counted in deep software standby mode
56     AGT_CLOCK_P404          = 0x91,    ///< Counts events on P404, events are counted in deep software standby mode
57     AGT_CLOCK_AGTIO         = 0x80,    ///< Counts events on AGTIOn, events are not counted in software standby modes
58 } agt_clock_t;
59 
60 /** Enable pin for event counting mode. */
61 typedef enum e_agt_measure
62 {
63     AGT_MEASURE_DISABLED               = 1U,    ///< AGT used as a counter
64     AGT_MEASURE_PULSE_WIDTH_LOW_LEVEL  = 3U,    ///< AGT used to measure low level pulse width
65     AGT_MEASURE_PULSE_WIDTH_HIGH_LEVEL = 0x13U, ///< AGT used to measure high level pulse width
66     AGT_MEASURE_PULSE_PERIOD           = 4U,    ///< AGT used to measure pulse period
67 } agt_measure_t;
68 
69 /** Input filter, applies AGTIO in pulse period measurement, pulse width measurement, or event counter mode. The filter
70  * requires the signal to be at the same level for 3 successive reads at the specified filter frequency. */
71 typedef enum e_agt_agtio_filter
72 {
73     AGT_AGTIO_FILTER_NONE         = 0x00U, ///< No filter
74     AGT_AGTIO_FILTER_PCLKB        = 0x10U, ///< Filter at PCLKB
75     AGT_AGTIO_FILTER_PCLKB_DIV_8  = 0x20U, ///< Filter at PCLKB / 8
76     AGT_AGTIO_FILTER_PCLKB_DIV_32 = 0x30U, ///< Filter at PCLKB / 32
77 } agt_agtio_filter_t;
78 
79 /** Enable pin for event counting mode. */
80 typedef enum e_agt_enable_pin
81 {
82     AGT_ENABLE_PIN_NOT_USED    = 0U,    ///< AGTEE/AGTWEE is not used
83     AGT_ENABLE_PIN_ACTIVE_LOW  = 0x40U, ///< Events are only counted when AGTEE/AGTWEE is low
84     AGT_ENABLE_PIN_ACTIVE_HIGH = 0x44U, ///< Events are only counted when AGTEE/AGTWEE is high
85 } agt_enable_pin_t;
86 
87 /** Trigger edge for pulse period measurement mode and event counting mode. */
88 typedef enum e_agt_trigger_edge
89 {
90     AGT_TRIGGER_EDGE_RISING  = 0U,     ///< Measurement starts or events are counted on rising edge
91     AGT_TRIGGER_EDGE_FALLING = 1U,     ///< Measurement starts or events are counted on falling edge
92     AGT_TRIGGER_EDGE_BOTH    = 8U,     ///< Events are counted on both edges (n/a for pulse period mode)
93 } agt_trigger_edge_t;
94 
95 /** Output pins, used to select which duty cycle to update in R_AGT_DutyCycleSet(). */
96 typedef enum e_agt_output_pin
97 {
98     AGT_OUTPUT_PIN_AGTOA = 0,          ///< AGTOA
99     AGT_OUTPUT_PIN_AGTOB = 1,          ///< AGTOB
100 } agt_output_pin_t;
101 
102 /** Level of AGT pin */
103 typedef enum e_agt_pin_cfg
104 {
105     AGT_PIN_CFG_DISABLED         = 0,  ///< Not used as output pin
106     AGT_PIN_CFG_START_LEVEL_LOW  = 3,  ///< Pin level low
107     AGT_PIN_CFG_START_LEVEL_HIGH = 7,  ///< Pin level high
108 } agt_pin_cfg_t;
109 
110 /** Counter type to determine regsiter size */
111 typedef enum e_agt_counter_bit_width
112 {
113     AGT_COUNTER_BIT_WIDTH_DEFAULT = 0, ///< Legacy
114     AGT_COUNTER_BIT_WIDTH_16      = 1, ///< AGT
115     AGT_COUNTER_BIT_WIDTH_32      = 2, ///< AGTW
116 } agt_counter_bit_width_t;
117 
118 /** Channel control block. DO NOT INITIALIZE.  Initialization occurs when @ref timer_api_t::open is called. */
119 typedef struct st_agt_instance_ctrl
120 {
121     uint32_t            open;                     // Whether or not channel is open
122     const timer_cfg_t * p_cfg;                    // Pointer to initial configurations
123     R_AGTX0_Type      * p_reg;                    // Base register for this channel
124     bool                is_agtw;                  // Whether or not this channel is agtw, otherwise it is agt
125     uint32_t            period;                   // Current timer period (counts)
126 
127     void (* p_callback)(timer_callback_args_t *); // Pointer to callback that is called when a timer_event_t occurs.
128     timer_callback_args_t * p_callback_memory;    // Pointer to non-secure memory that can be used to pass arguments to a callback in non-secure memory.
129     void const            * p_context;            // Pointer to context to be passed into callback function
130 } agt_instance_ctrl_t;
131 
132 /** Optional AGT extension data structure.*/
133 typedef struct st_agt_extended_cfg
134 {
135     agt_clock_t count_source;          ///< AGT channel clock source. Valid values are: AGT_CLOCK_PCLKB, AGT_CLOCK_LOCO, AGT_CLOCK_FSUB
136 
137     /* Output pin settings. */
138     union
139     {
140         uint8_t agtoab_settings;
141 
142         struct
143         {
144             agt_pin_cfg_t agtoa : 3;           ///< Configure AGTOA/AGTWOA pin
145             uint8_t             : 1;
146             agt_pin_cfg_t agtob : 3;           ///< Configure AGTOB/AGTWOB pin
147         } agtoab_settings_b;
148     };
149     agt_pin_cfg_t agto : 3;                    ///< Configure AGTO pin @note AGTIO polarity is opposite AGTO
150 
151     /* Input pin settings. */
152     agt_measure_t           measurement_mode;  ///< Measurement mode
153     agt_agtio_filter_t      agtio_filter;      ///< Input filter for AGTIO
154     agt_enable_pin_t        enable_pin;        ///< Enable pin (event counting only)
155     agt_trigger_edge_t      trigger_edge;      ///< Trigger edge to start pulse period measurement or count external event
156     agt_counter_bit_width_t counter_bit_width; ///< Counter bit width
157 } agt_extended_cfg_t;
158 
159 /**********************************************************************************************************************
160  * Exported global variables
161  **********************************************************************************************************************/
162 
163 /** @cond INC_HEADER_DEFS_SEC */
164 /** Filled in Interface API structure for this Instance. */
165 extern const timer_api_t g_timer_on_agt;
166 
167 /** @endcond */
168 
169 fsp_err_t R_AGT_Close(timer_ctrl_t * const p_ctrl);
170 fsp_err_t R_AGT_PeriodSet(timer_ctrl_t * const p_ctrl, uint32_t const period_counts);
171 fsp_err_t R_AGT_DutyCycleSet(timer_ctrl_t * const p_ctrl, uint32_t const duty_cycle_counts, uint32_t const pin);
172 fsp_err_t R_AGT_Reset(timer_ctrl_t * const p_ctrl);
173 fsp_err_t R_AGT_Start(timer_ctrl_t * const p_ctrl);
174 fsp_err_t R_AGT_Enable(timer_ctrl_t * const p_ctrl);
175 fsp_err_t R_AGT_Disable(timer_ctrl_t * const p_ctrl);
176 fsp_err_t R_AGT_InfoGet(timer_ctrl_t * const p_ctrl, timer_info_t * const p_info);
177 fsp_err_t R_AGT_StatusGet(timer_ctrl_t * const p_ctrl, timer_status_t * const p_status);
178 fsp_err_t R_AGT_Stop(timer_ctrl_t * const p_ctrl);
179 fsp_err_t R_AGT_Open(timer_ctrl_t * const p_ctrl, timer_cfg_t const * const p_cfg);
180 fsp_err_t R_AGT_CallbackSet(timer_ctrl_t * const          p_api_ctrl,
181                             void (                      * p_callback)(timer_callback_args_t *),
182                             void const * const            p_context,
183                             timer_callback_args_t * const p_callback_memory);
184 fsp_err_t R_AGT_CompareMatchSet(timer_ctrl_t * const        p_ctrl,
185                                 uint32_t const              compare_match_value,
186                                 timer_compare_match_t const match_channel);
187 
188 /*******************************************************************************************************************//**
189  * @} (end defgroup AGT)
190  **********************************************************************************************************************/
191 
192 /* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */
193 
194 FSP_FOOTER
195 
196 #endif                                 // R_AGT_H
197