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_ULPT_H
8 #define R_ULPT_H
9 
10 /***********************************************************************************************************************
11  * Includes
12  **********************************************************************************************************************/
13 #include "bsp_api.h"
14 #include "r_ulpt_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 /*******************************************************************************************************************//**
25  * @addtogroup ULPT
26  * @{
27  **********************************************************************************************************************/
28 
29 /***********************************************************************************************************************
30  * Typedef definitions
31  **********************************************************************************************************************/
32 
33 /** Count source. */
34 typedef enum e_ulpt_clock
35 {
36     ULPT_CLOCK_LOCO     = 0x00U,       ///< LOCO count source, division by 1, 2, 4, 8, 16, 32, 64, 128.
37     ULPT_CLOCK_SUBCLOCK = 0x20U,       ///< Subclock count source, division by 1, 2, 4, 8, 16, 32, 64, 128.
38     ULPT_CLOCK_ULPTEVI  = 0x02U,       ///< Counts external events on ULPTEVI.
39 } ulpt_clock_t;
40 
41 /** Counter mode for event enable. */
42 typedef enum e_ulpt_enable_function
43 {
44     // R_ULPT0_ULPTIOC_TIOGT0_Msk    0X00 0000 0=Always count, 1 count external events (Not to be mized with nonzero TEECTL)
45     // R_ULPT0_ULPTISR_RCCPSEL2_Msk  0000 0X00 0=count low level, 1 count high level, only valid when TIOGT0=1 (Not to be mized with nonzero TEECTL)
46     // R_ULPT0_ULPTMR3_TEECTL_Msk    00XX 0000
47     // 0100 0000 Count external high,
48     // 0100 0100 Count external low
49     ULPT_ENABLE_FUNCTION_IGNORED     = 0x00U, ///< Always count external events, ignore ULPTEE.
50     ULPT_ENABLE_FUNCTION_ENABLE_LOW  = 0x40U, ///< Event counting is enabled while ULPTEE is low (event counting only).
51     ULPT_ENABLE_FUNCTION_ENABLE_HIGH = 0x44U, ///< Event counting is enabled while ULPTEE is high (event counting only).
52     ULPT_ENABLE_FUNCTION_START       = 0x20U, ///< Counting is started after ULPTEE.
53     ULPT_ENABLE_FUNCTION_RESTART     = 0x30U, ///< Counting is restarted after ULPTEE.
54 } ulpt_enable_function_t;
55 
56 /** Enable signal trigger edge for start and restart functions. */
57 typedef enum e_ulpt_trigger_edge
58 {
59     ULPT_TRIGGER_EDGE_RISING  = 0x00U, ///< Timer enable function occurs on the rising edge of ULPTEE.
60     ULPT_TRIGGER_EDGE_FALLING = 0x40U, ///< Timer enable function occurs on the falling edge of ULPTEE.
61     ULPT_TRIGGER_EDGE_BOTH    = 0x80U, ///< Timer enable function occurs on any edge of ULPTEE.
62 } ulpt_trigger_edge_t;
63 
64 /** Event signal pin. */
65 typedef enum e_ulpt_event_pin
66 {
67     ULPT_EVENT_PIN_RISING  = 0x00U,    ///< Event count occurs on the rising edge.
68     ULPT_EVENT_PIN_FALLING = 0x02U,    ///< Event count occurs on the falling edge.
69     ULPT_EVENT_PIN_BOTH    = 0x08U,    ///< Event count occurs on both edges.
70 } ulpt_event_pin_t;
71 
72 /** Output pins, used to select which duty cycle to update in R_ULPT_DutyCycleSet(). */
73 typedef enum e_ulpt_output_pin
74 {
75     ULPT_OUTPUT_PIN_ULPTOA = 0U,       ///< Compare match A output.
76     ULPT_OUTPUT_PIN_ULPTOB = 1U,       ///< Compare match B output.
77 } ulpt_output_pin_t;
78 
79 /** ULPTO pulse output pin. */
80 typedef enum e_ulpt_pulse_pin_cfg
81 {
82     ULPT_PULSE_PIN_CFG_DISABLED                 = 0x00U, ///< Output pin disabled.
83     ULPT_PULSE_PIN_CFG_ENABLED_START_LEVEL_LOW  = 0x01U, ///< Output pin Enabled Start Low
84     ULPT_PULSE_PIN_CFG_ENABLED_START_LEVEL_HIGH = 0x02U, ///< Output pin enabled Start Hig
85 } ulpt_pulse_pin_cfg_t;
86 
87 /** ULPT match output pin. */
88 typedef enum e_ulpt_match_pin_cfg
89 {
90     ULPT_MATCH_PIN_CFG_DISABLED         = 0x00U, ///< Match output disabled.
91     ULPT_MATCH_PIN_CFG_START_LEVEL_LOW  = 0x03U, ///< Match output enabled, starts low.
92     ULPT_MATCH_PIN_CFG_START_LEVEL_HIGH = 0x07U, ///< Match output enabled, starts high.
93 } ulpt_match_pin_cfg_t;
94 
95 /** Input filter, applied to ULPTEVI in event counter mode. The filter requires the signal to be at the same level for
96  *  3 successive reads at the specified filter frequency. */
97 typedef enum e_ulpt_ulptevi_filter
98 {
99     ULPT_ULPTEVI_FILTER_NONE         = 0x00U, ///< No filter
100     ULPT_ULPTEVI_FILTER_PCLKB        = 0x10U, ///< Filter at PCLKB
101     ULPT_ULPTEVI_FILTER_PCLKB_DIV_8  = 0x20U, ///< Filter at PCLKB / 8
102     ULPT_ULPTEVI_FILTER_PCLKB_DIV_32 = 0x30U, ///< Filter at PCLKB / 32
103 } ulpt_ulptevi_filter_t;
104 
105 /** Channel control block. DO NOT INITIALIZE.  Initialization occurs when @ref timer_api_t::open is called. */
106 typedef struct st_ulpt_instance_ctrl
107 {
108     uint32_t            open;                     // Whether or not the channel is open.
109     const timer_cfg_t * p_cfg;                    // Pointer to initial configuration.
110     R_ULPT0_Type      * p_reg;                    // Base register for this channel.
111     uint32_t            period;                   // Current timer period (counts).
112 
113     void (* p_callback)(timer_callback_args_t *); // Pointer to callback that is called when a timer_event_t occurs.
114     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.
115     void const            * p_context;            // Pointer to context to be passed into callback function
116 } ulpt_instance_ctrl_t;
117 
118 /** Optional ULPT extension data structure. */
119 typedef struct st_ulpt_extended_cfg
120 {
121     ulpt_clock_t count_source;               ///< ULPT channel clock source.
122 
123     /* Input pin settings */
124     ulpt_ulptevi_filter_t  ulptevi_filter;   ///< Input filter for ULTPEVI.
125     ulpt_enable_function_t enable_function;  ///< Counter function when ULPTEE is valid.
126     ulpt_trigger_edge_t    trigger_edge;     ///< Enable trigger edge (start and restart functions only).
127     ulpt_event_pin_t       event_pin;        ///< Event pin (event counting only).
128 
129     /* Output settings */
130     ulpt_pulse_pin_cfg_t ulpto;              ///< Pulse output pin.
131     union
132     {
133         uint8_t ulptoab_settings;            ///< Compare match output register.
134         struct
135         {
136             ulpt_match_pin_cfg_t ulptoa : 3; ///< Compare match A output pin.
137             uint8_t                     : 1;
138             ulpt_match_pin_cfg_t ulptob : 3; ///< Compare match B output pin.
139             uint8_t                     : 1;
140         } ulptoab_settings_b;
141     };
142 } ulpt_extended_cfg_t;
143 
144 /**********************************************************************************************************************
145  * Exported global variables
146  **********************************************************************************************************************/
147 
148 /** @cond INC_HEADER_DEFS_SEC */
149 /** Filled in Interface API structure for this Instance. */
150 extern const timer_api_t g_timer_on_ulpt;
151 
152 /** @endcond */
153 
154 fsp_err_t R_ULPT_Close(timer_ctrl_t * const p_ctrl);
155 fsp_err_t R_ULPT_PeriodSet(timer_ctrl_t * const p_ctrl, uint32_t const period_counts);
156 fsp_err_t R_ULPT_DutyCycleSet(timer_ctrl_t * const p_ctrl, uint32_t const duty_cycle_counts, uint32_t const pin);
157 fsp_err_t R_ULPT_Reset(timer_ctrl_t * const p_ctrl);
158 fsp_err_t R_ULPT_Start(timer_ctrl_t * const p_ctrl);
159 fsp_err_t R_ULPT_Enable(timer_ctrl_t * const p_ctrl);
160 fsp_err_t R_ULPT_Disable(timer_ctrl_t * const p_ctrl);
161 fsp_err_t R_ULPT_InfoGet(timer_ctrl_t * const p_ctrl, timer_info_t * const p_info);
162 fsp_err_t R_ULPT_StatusGet(timer_ctrl_t * const p_ctrl, timer_status_t * const p_status);
163 fsp_err_t R_ULPT_Stop(timer_ctrl_t * const p_ctrl);
164 fsp_err_t R_ULPT_Open(timer_ctrl_t * const p_ctrl, timer_cfg_t const * const p_cfg);
165 fsp_err_t R_ULPT_CallbackSet(timer_ctrl_t * const          p_api_ctrl,
166                              void (                      * p_callback)(timer_callback_args_t *),
167                              void const * const            p_context,
168                              timer_callback_args_t * const p_callback_memory);
169 fsp_err_t R_ULPT_CompareMatchSet(timer_ctrl_t * const        p_ctrl,
170                                  uint32_t const              compare_match_value,
171                                  timer_compare_match_t const match_channel);
172 
173 /*******************************************************************************************************************//**
174  * @} (end defgroup ULPT)
175  **********************************************************************************************************************/
176 
177 /* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */
178 
179 FSP_FOOTER
180 
181 #endif                                 // R_ULPT_H
182