Skip to content

Commit 0ba53e5

Browse files
Minh TangKhiemNguyenT
authored andcommitted
hal: renesas: ra: Add support FSP LVD driver for RA
Add FSP LVD module hal support for RA MCUs Signed-off-by: Minh Tang <[email protected]>
1 parent 1c84b05 commit 0ba53e5

File tree

5 files changed

+1666
-0
lines changed

5 files changed

+1666
-0
lines changed

drivers/ra/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ zephyr_library_sources_ifdef(CONFIG_USE_RA_FSP_WDT
8585
fsp/src/r_wdt/r_wdt.c)
8686
zephyr_library_sources_ifdef(CONFIG_USE_RA_FSP_ACMPHS
8787
fsp/src/r_acmphs/r_acmphs.c)
88+
zephyr_library_sources_ifdef(CONFIG_USE_RA_FSP_LVD
89+
fsp/src/r_lvd/r_lvd.c)
8890
zephyr_library_sources_ifdef(CONFIG_USE_RA_FSP_ELC
8991
fsp/src/r_elc/r_elc.c)
9092
zephyr_library_sources_ifdef(CONFIG_USE_RA_FSP_I3C

drivers/ra/fsp/inc/api/r_lvd_api.h

Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
/*
2+
* Copyright (c) 2020 - 2025 Renesas Electronics Corporation and/or its affiliates
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
/*******************************************************************************************************************//**
8+
* @ingroup RENESAS_MONITORING_INTERFACES
9+
* @defgroup LVD_API Low Voltage Detection Interface
10+
* @brief Interface for Low Voltage Detection
11+
*
12+
* @section LVD_API_SUMMARY Summary
13+
* The LVD driver provides functions for configuring the LVD voltage monitors and detectors.
14+
*
15+
*
16+
* @{
17+
**********************************************************************************************************************/
18+
#ifndef R_LVD_API_H
19+
#define R_LVD_API_H
20+
21+
/***********************************************************************************************************************
22+
* Includes
23+
**********************************************************************************************************************/
24+
25+
/** Register definitions, common services, and error codes. */
26+
#include "bsp_api.h"
27+
28+
/* Common macro for FSP header files. There is also a corresponding FSP_FOOTER macro at the end of this file. */
29+
FSP_HEADER
30+
31+
/**********************************************************************************************************************
32+
* Macro definitions
33+
**********************************************************************************************************************/
34+
35+
/**********************************************************************************************************************
36+
* Typedef definitions
37+
**********************************************************************************************************************/
38+
39+
#ifndef BSP_OVERRIDE_LVD_PERIPHERAL_T
40+
41+
/** Voltage detection level */
42+
43+
/** The thresholds supported by each MCU are in the MCU User's Manual as well as
44+
* in the r_lvd module description on the stack tab of the RA project. */
45+
typedef enum
46+
{
47+
LVD_THRESHOLD_MONITOR_1_LEVEL_4_29V = 0x00UL, ///< 4.29V
48+
LVD_THRESHOLD_MONITOR_1_LEVEL_4_16V = 0x01UL, ///< 4.16V
49+
LVD_THRESHOLD_MONITOR_1_LEVEL_4_14V = 0x01UL, ///< 4.14V
50+
LVD_THRESHOLD_MONITOR_1_LEVEL_4_03V = 0x02UL, ///< 4.03V
51+
LVD_THRESHOLD_MONITOR_1_LEVEL_4_02V = 0x02UL, ///< 4.02V
52+
LVD_THRESHOLD_MONITOR_1_LEVEL_3_86V = 0x03UL, ///< 3.86V
53+
LVD_THRESHOLD_MONITOR_1_LEVEL_3_84V = 0x03UL, ///< 3.84V
54+
LVD_THRESHOLD_MONITOR_1_LEVEL_3_10V = 0x04UL, ///< 3.10V
55+
LVD_THRESHOLD_MONITOR_1_LEVEL_3_00V = 0x05UL, ///< 3.00V
56+
LVD_THRESHOLD_MONITOR_1_LEVEL_2_90V = 0x06UL, ///< 2.90V
57+
LVD_THRESHOLD_MONITOR_1_LEVEL_2_79V = 0x07UL, ///< 2.79V
58+
LVD_THRESHOLD_MONITOR_1_LEVEL_2_68V = 0x08UL, ///< 2.68V
59+
LVD_THRESHOLD_MONITOR_1_LEVEL_2_58V = 0x09UL, ///< 2.58V
60+
LVD_THRESHOLD_MONITOR_1_LEVEL_2_48V = 0x0AUL, ///< 2.48V
61+
LVD_THRESHOLD_MONITOR_1_LEVEL_2_20V = 0x0BUL, ///< 2.20V
62+
LVD_THRESHOLD_MONITOR_1_LEVEL_1_96V = 0x0CUL, ///< 1.96V
63+
LVD_THRESHOLD_MONITOR_1_LEVEL_1_86V = 0x0DUL, ///< 1.86V
64+
LVD_THRESHOLD_MONITOR_1_LEVEL_1_75V = 0x0EUL, ///< 1.75V
65+
LVD_THRESHOLD_MONITOR_1_LEVEL_1_65V = 0x0FUL, ///< 1.65V
66+
LVD_THRESHOLD_MONITOR_1_LEVEL_2_99V = 0x11UL, ///< 2.99V
67+
LVD_THRESHOLD_MONITOR_1_LEVEL_2_92V = 0x12UL, ///< 2.92V
68+
LVD_THRESHOLD_MONITOR_1_LEVEL_2_85V = 0x13UL, ///< 2.85V
69+
LVD_THRESHOLD_MONITOR_2_LEVEL_4_31V = 0x00UL, ///< 4.31V
70+
LVD_THRESHOLD_MONITOR_2_LEVEL_4_29V = 0x00UL, ///< 4.29V
71+
LVD_THRESHOLD_MONITOR_2_LEVEL_4_17V = 0x01UL, ///< 4.17V
72+
LVD_THRESHOLD_MONITOR_2_LEVEL_4_14V = 0x01UL, ///< 4.14V
73+
LVD_THRESHOLD_MONITOR_2_LEVEL_4_03V = 0x02UL, ///< 4.03V
74+
LVD_THRESHOLD_MONITOR_2_LEVEL_4_02V = 0x02UL, ///< 4.02V
75+
LVD_THRESHOLD_MONITOR_2_LEVEL_3_84V = 0x03UL, ///< 3.84V
76+
LVD_THRESHOLD_MONITOR_2_LEVEL_2_99V = 0x05UL, ///< 2.99V
77+
LVD_THRESHOLD_MONITOR_2_LEVEL_2_92V = 0x06UL, ///< 2.92V
78+
LVD_THRESHOLD_MONITOR_2_LEVEL_2_85V = 0x07UL, ///< 2.85V
79+
80+
LVD_THRESHOLD_EXLVDVBAT_LEVEL_3_1V = 0x06UL, ///< 3.1V
81+
LVD_THRESHOLD_EXLVDVBAT_LEVEL_2_9V = 0x05UL, ///< 2.9V
82+
LVD_THRESHOLD_EXLVDVBAT_LEVEL_2_8V = 0x04UL, ///< 2.8V
83+
LVD_THRESHOLD_EXLVDVBAT_LEVEL_2_7V = 0x03UL, ///< 2.7V
84+
LVD_THRESHOLD_EXLVDVBAT_LEVEL_2_6V = 0x02UL, ///< 2.6V
85+
LVD_THRESHOLD_EXLVDVBAT_LEVEL_2_4V = 0x01UL, ///< 2.4V
86+
LVD_THRESHOLD_EXLVDVBAT_LEVEL_2_2V = 0x00UL, ///< 2.2V
87+
88+
LVD_THRESHOLD_LVDVRTC_LEVEL_2_8V = 0x03UL, ///< 2.8V
89+
LVD_THRESHOLD_LVDVRTC_LEVEL_2_6V = 0x02UL, ///< 2.6V
90+
LVD_THRESHOLD_LVDVRTC_LEVEL_2_4V = 0x01UL, ///< 2.4V
91+
LVD_THRESHOLD_LVDVRTC_LEVEL_2_2V = 0x00UL, ///< 2.2V
92+
LVD_THRESHOLD_NOT_AVAILABLE = 0xFFUL, ///< Not Used
93+
} lvd_threshold_t;
94+
#endif
95+
96+
/** Response types for handling threshold crossing event. */
97+
typedef enum e_lvd_response
98+
{
99+
LVD_RESPONSE_NMI, ///< Non-maskable interrupt
100+
LVD_RESPONSE_INTERRUPT, ///< Maskable interrupt
101+
LVD_RESPONSE_RESET, ///< Reset on VCC-fall
102+
LVD_RESPONSE_RESET_ON_RISING, ///< Reset on VCC-rise
103+
LVD_RESPONSE_NONE, ///< No response, status must be requested via statusGet function
104+
} lvd_response_t;
105+
106+
/** The direction from which VCC must cross the threshold to trigger a detection (rising, falling, or both). */
107+
typedef enum e_lvd_voltage_slope
108+
{
109+
LVD_VOLTAGE_SLOPE_RISING = 0, ///< When VCC >= Vdet2 (rise) is detected
110+
LVD_VOLTAGE_SLOPE_FALLING = 1, ///< When VCC < Vdet2 (drop) is detected
111+
LVD_VOLTAGE_SLOPE_BOTH = 2, ///< When drop and rise are detected
112+
} lvd_voltage_slope_t;
113+
114+
/** Sample clock divider, use LVD_SAMPLE_CLOCK_DISABLED to disable digital filtering */
115+
typedef enum e_lvd_sample_clock
116+
{
117+
LVD_SAMPLE_CLOCK_LOCO_DIV_2 = 0, ///< Digital filter sample clock is LOCO divided by 2
118+
LVD_SAMPLE_CLOCK_LOCO_DIV_4 = 1, ///< Digital filter sample clock is LOCO divided by 4
119+
LVD_SAMPLE_CLOCK_LOCO_DIV_8 = 2, ///< Digital filter sample clock is LOCO divided by 8
120+
LVD_SAMPLE_CLOCK_LOCO_DIV_16 = 3, ///< Digital filter sample clock is LOCO divided by 16
121+
LVD_SAMPLE_CLOCK_DISABLED = -1, ///< Digital filter is disabled
122+
} lvd_sample_clock_t;
123+
124+
/** Negation delay of LVD reset signal follows reset or voltage in range */
125+
typedef enum e_lvd_negation_delay
126+
{
127+
/**
128+
* Negation follows a stabilization time (tLVDn)
129+
* after VCC > Vdet1 is detected.
130+
* If a transition to software standby or deep software
131+
* standby is to be made, the only possible value for
132+
* the RN bit is LVD_NEGATION_DELAY_FROM_VOLTAGE
133+
*/
134+
LVD_NEGATION_DELAY_FROM_VOLTAGE = 0,
135+
136+
/**
137+
* Negation follows a stabilization time (tLVDn) after
138+
* assertion of the LVDn reset.
139+
* If a transition to software standby or deep software
140+
* standby is to be made, the only possible value for
141+
* the RN bit is LVD_NEGATION_DELAY_FROM_VOLTAGE
142+
*/
143+
LVD_NEGATION_DELAY_FROM_RESET = 1,
144+
} lvd_negation_delay_t;
145+
146+
/** Threshold crossing detection (latched) */
147+
typedef enum e_lvd_threshold_crossing
148+
{
149+
LVD_THRESHOLD_CROSSING_NOT_DETECTED = 0, ///< Threshold crossing has not been detected.
150+
LVD_THRESHOLD_CROSSING_DETECTED = 1, ///< Threshold crossing has been detected.
151+
} lvd_threshold_crossing_t;
152+
153+
/** Instantaneous status of VCC (above or below threshold) */
154+
typedef enum e_lvd_current_state
155+
{
156+
LVD_CURRENT_STATE_BELOW_THRESHOLD = 0, ///< VCC < threshold
157+
LVD_CURRENT_STATE_ABOVE_THRESHOLD = 1, ///< VCC >= threshold or monitor is disabled
158+
} lvd_current_state_t;
159+
160+
/**
161+
* Current state of a voltage monitor.
162+
*/
163+
typedef struct st_lvd_status
164+
{
165+
/** Threshold crossing detection (latched) */
166+
lvd_threshold_crossing_t crossing_detected;
167+
168+
/** Instantaneous status of monitored voltage (above or below threshold) */
169+
lvd_current_state_t current_state;
170+
} lvd_status_t;
171+
172+
/** LVD callback parameter definition */
173+
typedef struct st_lvd_callback_args
174+
{
175+
uint32_t monitor_number; ///< Monitor number
176+
lvd_current_state_t current_state; ///< Current state of the voltage monitor
177+
void * p_context; ///< Placeholder for user data
178+
} lvd_callback_args_t;
179+
180+
/** LVD configuration structure */
181+
typedef struct st_lvd_cfg
182+
{
183+
/** Monitor number, 1, 2, ... */
184+
uint32_t monitor_number;
185+
186+
/** Threshold for out of range voltage detection */
187+
lvd_threshold_t voltage_threshold;
188+
189+
/** Response on detecting a threshold crossing */
190+
lvd_response_t detection_response;
191+
192+
/** Direction of voltage crossing that will trigger a detection (Rising Edge, Falling Edge, Both). */
193+
lvd_voltage_slope_t voltage_slope;
194+
195+
/** Negation of LVD signal follows reset or voltage in range */
196+
lvd_negation_delay_t negation_delay;
197+
198+
/** Sample clock divider, use LVD_SAMPLE_CLOCK_DISABLED to disable digital filtering */
199+
lvd_sample_clock_t sample_clock_divisor;
200+
201+
/** Interrupt number. */
202+
IRQn_Type irq;
203+
204+
/** Interrupt priority level. */
205+
uint8_t monitor_ipl;
206+
207+
/** User function to be called from interrupt */
208+
void (* p_callback)(lvd_callback_args_t * p_args);
209+
210+
/** Placeholder for user data. Passed to the user callback in */
211+
void * p_context;
212+
213+
/** Extension parameter for hardware specific settings */
214+
void const * p_extend;
215+
} lvd_cfg_t;
216+
217+
/** LVD control block. Allocate an instance specific control block to pass into the LVD API calls.
218+
*/
219+
typedef void lvd_ctrl_t;
220+
221+
/** LVD driver API structure.
222+
* LVD driver functions implemented at the HAL layer will adhere to this API.
223+
*/
224+
typedef struct st_lvd_api
225+
{
226+
/** Initializes a low voltage detection driver according to the passed-in configuration structure.
227+
* @param[in] p_ctrl Pointer to control structure for the driver instance
228+
* @param[in] p_cfg Pointer to the configuration structure for the driver instance
229+
**/
230+
fsp_err_t (* open)(lvd_ctrl_t * const p_ctrl, lvd_cfg_t const * const p_cfg);
231+
232+
/** Get the current state of the monitor, (threshold crossing detected, voltage currently above or below threshold).
233+
* Must be used if the peripheral was initialized with lvd_response_t set to LVD_RESPONSE_NONE.
234+
* @param[in] p_ctrl Pointer to the control structure for the driver instance
235+
* @param[in,out] p_lvd_status Pointer to a lvd_status_t structure
236+
**/
237+
fsp_err_t (* statusGet)(lvd_ctrl_t * const p_ctrl, lvd_status_t * p_lvd_status);
238+
239+
/** Clears the latched status of the monitor.
240+
* Must be used if the peripheral was initialized with lvd_response_t set to LVD_RESPONSE_NONE.
241+
* @param[in] p_ctrl Pointer to the control structure for the driver instance
242+
**/
243+
fsp_err_t (* statusClear)(lvd_ctrl_t * const p_ctrl);
244+
245+
/**
246+
* Specify callback function and optional context pointer and working memory pointer.
247+
*
248+
* @param[in] p_ctrl Pointer to the LVD control block.
249+
* @param[in] p_callback Callback function
250+
* @param[in] p_context Pointer to send to callback function
251+
* @param[in] p_working_memory Pointer to volatile memory where callback structure can be allocated.
252+
* Callback arguments allocated here are only valid during the callback.
253+
*/
254+
fsp_err_t (* callbackSet)(lvd_ctrl_t * const p_ctrl, void (* p_callback)(lvd_callback_args_t *),
255+
void * const p_context, lvd_callback_args_t * const p_callback_memory);
256+
257+
/** Disables the LVD peripheral.
258+
* Closes the driver instance.
259+
* @param[in] p_ctrl Pointer to the control structure for the driver instance
260+
**/
261+
fsp_err_t (* close)(lvd_ctrl_t * const p_ctrl);
262+
} lvd_api_t;
263+
264+
/** This structure encompasses everything that is needed to use an instance of this interface. */
265+
typedef struct st_lvd_instance
266+
{
267+
lvd_ctrl_t * p_ctrl; ///< Pointer to the control structure for this instance
268+
lvd_cfg_t const * p_cfg; ///< Pointer to the configuration structure for this interface instance
269+
lvd_api_t const * p_api; ///< Pointer to the API structure for this interface instance
270+
} lvd_instance_t;
271+
272+
/* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */
273+
FSP_FOOTER
274+
275+
#endif
276+
277+
/*******************************************************************************************************************//**
278+
* @} (end defgroup LVD_API)
279+
**********************************************************************************************************************/
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2020 - 2025 Renesas Electronics Corporation and/or its affiliates
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
/*******************************************************************************************************************//**
8+
* @addtogroup LVD-PVD
9+
* @{
10+
**********************************************************************************************************************/
11+
12+
#ifndef R_LVD_H
13+
#define R_LVD_H
14+
15+
/***********************************************************************************************************************
16+
* Includes
17+
**********************************************************************************************************************/
18+
#include "bsp_api.h"
19+
#include "r_lvd_cfg.h"
20+
#include "r_lvd_api.h"
21+
22+
/* Common macro for FSP header files. There is also a corresponding FSP_FOOTER macro at the end of this file. */
23+
FSP_HEADER
24+
25+
/***********************************************************************************************************************
26+
* Macro definitions
27+
**********************************************************************************************************************/
28+
29+
/***********************************************************************************************************************
30+
* Typedef definitions
31+
**********************************************************************************************************************/
32+
33+
/** LVD instance control structure */
34+
typedef struct st_lvd_instance_ctrl
35+
{
36+
uint32_t open;
37+
lvd_cfg_t const * p_cfg;
38+
39+
void (* p_callback)(lvd_callback_args_t *); // Pointer to callback that is called when lvd_current_state_t changes.
40+
lvd_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.
41+
42+
/* Pointer to context to be passed into callback function */
43+
void * p_context;
44+
} lvd_instance_ctrl_t;
45+
46+
/**********************************************************************************************************************
47+
* Exported global variables
48+
**********************************************************************************************************************/
49+
50+
/** @cond INC_HEADER_DEFS_SEC */
51+
/** Filled in Interface API structure for this Instance. */
52+
extern const lvd_api_t g_lvd_on_lvd;
53+
54+
/** @endcond */
55+
56+
/***********************************************************************************************************************
57+
* Public APIs
58+
**********************************************************************************************************************/
59+
fsp_err_t R_LVD_Open(lvd_ctrl_t * const p_api_ctrl, lvd_cfg_t const * const p_cfg);
60+
fsp_err_t R_LVD_Close(lvd_ctrl_t * const p_api_ctrl);
61+
fsp_err_t R_LVD_StatusGet(lvd_ctrl_t * const p_api_ctrl, lvd_status_t * p_lvd_status);
62+
fsp_err_t R_LVD_StatusClear(lvd_ctrl_t * const p_api_ctrl);
63+
fsp_err_t R_LVD_CallbackSet(lvd_ctrl_t * const p_api_ctrl,
64+
void ( * p_callback)(lvd_callback_args_t *),
65+
void * const p_context,
66+
lvd_callback_args_t * const p_callback_memory);
67+
68+
/* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */
69+
FSP_FOOTER
70+
71+
#endif
72+
73+
/*******************************************************************************************************************//**
74+
* @} (end defgroup LVD-PVD)
75+
**********************************************************************************************************************/

0 commit comments

Comments
 (0)