Skip to content

Commit 2a4f856

Browse files
Minh TangKhiemNguyenT
authored andcommitted
hal: renesas: ra: Add Portable Functions for LVD RA
Add files support for additional function on LVD RA Signed-off-by: Minh Tang <[email protected]>
1 parent 0ba53e5 commit 2a4f856

File tree

3 files changed

+233
-0
lines changed

3 files changed

+233
-0
lines changed

zephyr/ra/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ zephyr_library_sources_ifdef(CONFIG_USE_RA_FSP_SCI_B_SPI
1919
portable/src/rp_sci_b_spi/rp_sci_b_spi.c)
2020
zephyr_library_sources_ifdef(CONFIG_USE_RA_FSP_ADC
2121
portable/src/rp_adc/rp_adc.c)
22+
zephyr_library_sources_ifdef(CONFIG_USE_RA_FSP_LVD
23+
portable/src/rp_lvd/rp_lvd.c)

zephyr/ra/portable/inc/rp_lvd.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 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 RP_LVD_H
13+
#define RP_LVD_H
14+
15+
/***********************************************************************************************************************
16+
* Includes
17+
**********************************************************************************************************************/
18+
#include "r_lvd.h"
19+
20+
/* Common macro for FSP header files. There is also a corresponding FSP_FOOTER macro at the end of this file. */
21+
FSP_HEADER
22+
23+
/***********************************************************************************************************************
24+
* Public APIs
25+
**********************************************************************************************************************/
26+
fsp_err_t RP_LVD_Enable(lvd_ctrl_t * const p_api_ctrl, bool enable);
27+
fsp_err_t RP_LVD_IsEnable(lvd_ctrl_t * const p_api_ctrl, bool * is_enabled);
28+
fsp_err_t RP_LVD_TriggerSet(lvd_ctrl_t * const p_api_ctrl, bool reset_action, lvd_voltage_slope_t voltage_slope);
29+
30+
/* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */
31+
FSP_FOOTER
32+
33+
#endif
34+
35+
/*******************************************************************************************************************//**
36+
* @} (end defgroup LVD-PVD)
37+
**********************************************************************************************************************/
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
/*
2+
* Copyright (c) 2025 Renesas Electronics Corporation and/or its affiliates
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
/***********************************************************************************************************************
8+
* Includes
9+
**********************************************************************************************************************/
10+
#include "rp_lvd.h"
11+
12+
/***********************************************************************************************************************
13+
* Macro definitions
14+
**********************************************************************************************************************/
15+
16+
#define LVD_STABILIZATION_TIME_US 20U
17+
18+
/***********************************************************************************************************************
19+
* Private function prototypes
20+
**********************************************************************************************************************/
21+
22+
/***********************************************************************************************************************
23+
* Private global variables
24+
**********************************************************************************************************************/
25+
26+
#if (1U == BSP_FEATURE_LVD_VERSION)
27+
28+
/* Look-up tables for writing to monitor 1 and monitor 2 registers. */
29+
static uint8_t volatile * const g_lvdncr0_lut[] = {&(R_SYSTEM->LVD1CR0), &(R_SYSTEM->LVD2CR0)};
30+
static uint8_t volatile * const g_lvdncr1_lut[] = {&(R_SYSTEM->LVD1CR1), &(R_SYSTEM->LVD2CR1)};
31+
static uint8_t volatile * const g_lvdnsr_lut[] = {&(R_SYSTEM->LVD1SR), &(R_SYSTEM->LVD2SR)};
32+
#elif (2U == BSP_FEATURE_LVD_VERSION)
33+
static uint8_t volatile * const g_lvdnsr_lut[] = {&(R_SYSTEM->LVD1SR)};
34+
#elif (3U == BSP_FEATURE_LVD_VERSION)
35+
36+
/* Look-up tables for writing to monitor 1, monitor 2, monitor 4, monitor 5, registers. monitor 3
37+
* registers are dummy variables. */
38+
static uint8_t volatile * const g_lvdncr0_lut[] =
39+
{
40+
&(R_SYSTEM->LVD1CR0), &(R_SYSTEM->LVD2CR0),
41+
&(R_SYSTEM->LVD3CR0), &(R_SYSTEM->LVD4CR0),
42+
&(R_SYSTEM->LVD5CR0)
43+
};
44+
static uint8_t volatile * const g_lvdncr1_lut[] =
45+
{
46+
&(R_SYSTEM->LVD1CR1), &(R_SYSTEM->LVD2CR1),
47+
&(R_SYSTEM->LVD3CR1), &(R_SYSTEM->LVD4CR1),
48+
&(R_SYSTEM->LVD5CR1)
49+
};
50+
static uint8_t volatile * const g_lvdnsr_lut[] = {&(R_SYSTEM->LVD1SR), &(R_SYSTEM->LVD2SR)};
51+
#endif
52+
53+
#if BSP_FEATURE_LVD_SUPPORT_RESET_ON_RISING_EDGE
54+
#if (3U == BSP_FEATURE_LVD_VERSION)
55+
static uint8_t volatile * const g_lvdnfcr_lut[] =
56+
{
57+
&(R_SYSTEM->LVD1FCR), &(R_SYSTEM->LVD2FCR),
58+
&(R_SYSTEM->LVD3FCR), &(R_SYSTEM->LVD4FCR),
59+
&(R_SYSTEM->LVD5FCR)
60+
};
61+
#else
62+
static uint8_t volatile * const g_lvdnfcr_lut[] = {&(R_SYSTEM->LVD1FCR), &(R_SYSTEM->LVD2FCR)};
63+
#endif
64+
#endif
65+
66+
#if (BSP_FEATURE_LVD_HAS_LVDLVLR == 1)
67+
static uint32_t const g_lvdlvlr_offset_lut[] =
68+
{
69+
R_SYSTEM_LVDLVLR_LVD1LVL_Pos,
70+
R_SYSTEM_LVDLVLR_LVD2LVL_Pos
71+
};
72+
static uint32_t const g_lvdlvlr_mask_lut[] =
73+
{
74+
R_SYSTEM_LVDLVLR_LVD1LVL_Msk,
75+
R_SYSTEM_LVDLVLR_LVD2LVL_Msk
76+
};
77+
#elif (1U == BSP_FEATURE_LVD_VERSION)
78+
static uint8_t volatile * const g_lvdncmpcr_lut[] = {&(R_SYSTEM->LVD1CMPCR), &(R_SYSTEM->LVD2CMPCR)};
79+
#elif (3U == BSP_FEATURE_LVD_VERSION)
80+
static uint8_t volatile * const g_lvdncmpcr_lut[] =
81+
{
82+
&(R_SYSTEM->LVD1CMPCR), &(R_SYSTEM->LVD2CMPCR),
83+
&(R_SYSTEM->LVD3CMPCR), &(R_SYSTEM->LVD4CMPCR),
84+
&(R_SYSTEM->LVD5CMPCR)
85+
};
86+
#endif
87+
88+
/***********************************************************************************************************************
89+
* Global Variables
90+
**********************************************************************************************************************/
91+
92+
/***********************************************************************************************************************
93+
* Functions
94+
**********************************************************************************************************************/
95+
96+
/*******************************************************************************************************************//**
97+
* Enable or disable LVD interrupt/reset response.
98+
*
99+
* @retval FSP_SUCCESS Enable/Disable was successful.
100+
* @retval FSP_ERR_ASSERTION p_ctrl is NULL.
101+
* @retval FSP_ERR_NOT_OPEN Module not opened.
102+
**********************************************************************************************************************/
103+
fsp_err_t RP_LVD_Enable (lvd_ctrl_t * const p_api_ctrl, bool enable)
104+
{
105+
lvd_instance_ctrl_t * p_ctrl = (lvd_instance_ctrl_t *) p_api_ctrl;
106+
int channel_index = p_ctrl->p_cfg->monitor_number - 1;
107+
108+
#if LVD_CFG_PARAM_CHECKING_ENABLE
109+
FSP_ASSERT(p_ctrl);
110+
FSP_ERROR_RETURN(0 != p_ctrl->open, FSP_ERR_NOT_OPEN);
111+
#endif
112+
113+
R_BSP_RegisterProtectDisable(BSP_REG_PROTECT_LVD);
114+
115+
if (enable)
116+
{
117+
*g_lvdncr0_lut[channel_index] |= (R_SYSTEM_LVD1CR0_RIE_Msk);
118+
}
119+
else
120+
{
121+
*g_lvdncr0_lut[channel_index] &= ~(R_SYSTEM_LVD1CR0_RIE_Msk);
122+
*g_lvdnsr_lut[channel_index] &= ~(R_SYSTEM_LVD1SR_DET_Msk);
123+
}
124+
125+
R_BSP_RegisterProtectEnable(BSP_REG_PROTECT_LVD);
126+
127+
return FSP_SUCCESS;
128+
}
129+
130+
/*******************************************************************************************************************//**
131+
* Get the current status of the LVD response (reset/interrupt enabled).
132+
*
133+
* @retval FSP_SUCCESS Get status was successful.
134+
* @retval FSP_ERR_ASSERTION p_ctrl is NULL.
135+
* @retval FSP_ERR_NOT_OPEN Module not opened.
136+
**********************************************************************************************************************/
137+
fsp_err_t RP_LVD_IsEnable (lvd_ctrl_t * const p_api_ctrl, bool * is_enabled)
138+
{
139+
lvd_instance_ctrl_t * p_ctrl = (lvd_instance_ctrl_t *) p_api_ctrl;
140+
int channel_index = p_ctrl->p_cfg->monitor_number - 1;
141+
142+
#if LVD_CFG_PARAM_CHECKING_ENABLE
143+
FSP_ASSERT(p_ctrl);
144+
FSP_ASSERT(is_enabled);
145+
FSP_ERROR_RETURN(0 != p_ctrl->open, FSP_ERR_NOT_OPEN);
146+
#endif
147+
148+
*(is_enabled) = (bool) (*g_lvdncr0_lut[channel_index] & R_SYSTEM_LVD1CR0_RIE_Msk);
149+
150+
return FSP_SUCCESS;
151+
}
152+
153+
/*******************************************************************************************************************//**
154+
* Set LVD trigger edge.
155+
*
156+
* @retval FSP_SUCCESS Setting was successful.
157+
* @retval FSP_ERR_ASSERTION p_ctrl is NULL.
158+
* @retval FSP_ERR_NOT_OPEN Module not opened.
159+
* @retval FSP_ERR_INVALID_ARGUMENT Trigger both edge when in reset action.
160+
**********************************************************************************************************************/
161+
fsp_err_t RP_LVD_TriggerSet (lvd_ctrl_t * const p_api_ctrl, bool reset_action, lvd_voltage_slope_t voltage_slope)
162+
{
163+
lvd_instance_ctrl_t * p_ctrl = (lvd_instance_ctrl_t *) p_api_ctrl;
164+
int channel_index = p_ctrl->p_cfg->monitor_number - 1;
165+
166+
#if LVD_CFG_PARAM_CHECKING_ENABLE
167+
FSP_ASSERT(p_ctrl);
168+
FSP_ERROR_RETURN(0 != p_ctrl->open, FSP_ERR_NOT_OPEN);
169+
170+
/* Reset response is not accepted in case voltage slope is both edges */
171+
FSP_ERROR_RETURN((reset_action == false) || (voltage_slope != LVD_VOLTAGE_SLOPE_BOTH),
172+
FSP_ERR_INVALID_ARGUMENT);
173+
#endif
174+
175+
R_BSP_RegisterProtectDisable(BSP_REG_PROTECT_LVD);
176+
177+
if (reset_action)
178+
{
179+
*g_lvdncmpcr_lut[channel_index] &= ~(R_SYSTEM_LVD1CMPCR_LVDE_Msk);
180+
*g_lvdnfcr_lut[channel_index] = (voltage_slope == LVD_VOLTAGE_SLOPE_RISING);
181+
*g_lvdncmpcr_lut[channel_index] |= R_SYSTEM_LVD1CMPCR_LVDE_Msk;
182+
R_BSP_SoftwareDelay(LVD_STABILIZATION_TIME_US, BSP_DELAY_UNITS_MICROSECONDS);
183+
}
184+
else
185+
{
186+
*g_lvdncr1_lut[channel_index] &= ~(R_SYSTEM_LVD1CR1_IDTSEL_Msk);
187+
*g_lvdncr1_lut[channel_index] |=
188+
((voltage_slope << R_SYSTEM_LVD1CR1_IDTSEL_Pos) & R_SYSTEM_LVD1CR1_IDTSEL_Msk);
189+
}
190+
191+
R_BSP_RegisterProtectEnable(BSP_REG_PROTECT_LVD);
192+
193+
return FSP_SUCCESS;
194+
}

0 commit comments

Comments
 (0)