Skip to content

Commit 17f343b

Browse files
Thinh Le CongKhiemNguyenT
authored andcommitted
hal: renesas: ra: portable: rp_adc: Add calculating sample state support
Add function to calculate ADC sample state count based on desired sampling time Signed-off-by: Thinh Le Cong <[email protected]>
1 parent 362e776 commit 17f343b

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

zephyr/ra/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ zephyr_library_sources_ifdef(CONFIG_USE_RA_FSP_I3C
1717
portable/src/rp_i3c/rp_i3c.c)
1818
zephyr_library_sources_ifdef(CONFIG_USE_RA_FSP_SCI_B_SPI
1919
portable/src/rp_sci_b_spi/rp_sci_b_spi.c)
20+
zephyr_library_sources_ifdef(CONFIG_USE_RA_FSP_ADC
21+
portable/src/rp_adc/rp_adc.c)

zephyr/ra/portable/inc/rp_adc.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2020 - 2025 Renesas Electronics Corporation and/or its affiliates
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#ifndef RP_ADC_H
8+
#define RP_ADC_H
9+
10+
/***********************************************************************************************************************
11+
* Includes
12+
**********************************************************************************************************************/
13+
#include "r_adc_api.h"
14+
#include "r_adc.h"
15+
16+
/* Common macro for FSP header files. There is also a corresponding FSP_FOOTER macro at the end of this file. */
17+
FSP_HEADER
18+
19+
/*****************************************************************************************************************//**
20+
* @ingroup ADC
21+
* @{
22+
********************************************************************************************************************/
23+
24+
/***********************************************************************************************************************
25+
* Macro definitions
26+
**********************************************************************************************************************/
27+
28+
/* Definitions for unspecificed value of sampling time. */
29+
#define UNSPECIFIED 0xFFFFFFFFU
30+
31+
/* Number of microseconds in one second (1 s = 1,000,000 µs) */
32+
#define ADC_PRV_USEC_PER_SEC (1000000U)
33+
34+
/* Number of Hertz in one kilohertz (1 kHz = 1,000 Hz) */
35+
#define ADC_PRV_HZ_PER_KHZ (1000U)
36+
37+
/***********************************************************************************************************************
38+
* Typedef definitions
39+
**********************************************************************************************************************/
40+
41+
/**********************************************************************************************************************
42+
* Exported global variables
43+
**********************************************************************************************************************/
44+
45+
/**********************************************************************************************************************
46+
* Public Function Prototypes
47+
**********************************************************************************************************************/
48+
49+
fsp_err_t RP_ADC_SampleStateCalculation(uint32_t sampling_time_ns, uint32_t *p_sample_count);
50+
51+
/* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */
52+
FSP_FOOTER
53+
54+
#endif
55+
56+
/*******************************************************************************************************************//**
57+
* @} (end ingroup ADC)
58+
**********************************************************************************************************************/
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 "r_adc.h"
11+
#include "r_adc_cfg.h"
12+
#include "rp_adc.h"
13+
14+
/***********************************************************************************************************************
15+
* Macro definitions
16+
**********************************************************************************************************************/
17+
18+
/*******************************************************************************************************************//**
19+
* Calculate ADC sample state count based on desired sampling time (in ns).
20+
*
21+
* @param[in] sampling_time_ns Desired sampling time in nanoseconds, or UNSPECIFIED to use BSP default.
22+
* @param[out] p_sample_count Pointer to store calculated sample state count.
23+
*
24+
* @retval FSP_SUCCESS Calculation successful.
25+
* @retval FSP_ERR_INVALID_ARGUMENT p_sample_count is NULL.
26+
* @retval FSP_ERR_INVALID_HW_CONDITION ADC clock frequency not available.
27+
**********************************************************************************************************************/
28+
fsp_err_t RP_ADC_SampleStateCalculation(uint32_t sampling_time_ns, uint32_t *p_sample_count)
29+
{
30+
if (NULL == p_sample_count) {
31+
return FSP_ERR_INVALID_ARGUMENT;
32+
}
33+
34+
/* Get ADC clock frequency */
35+
uint32_t adc_clk_hz = R_FSP_SystemClockHzGet(BSP_FEATURE_ADC_CLOCK_SOURCE);
36+
if (0u == adc_clk_hz) {
37+
return FSP_ERR_INVALID_HW_CONDITION;
38+
}
39+
40+
/* If user did not specify a sampling time, use the BSP-feature minimum value for ADC */
41+
if (UNSPECIFIED == sampling_time_ns) {
42+
sampling_time_ns = BSP_FEATURE_ADC_SENSOR_MIN_SAMPLING_TIME;
43+
}
44+
45+
/* Calculate number of sample states with round-up */
46+
uint32_t sample_states =
47+
((sampling_time_ns * (adc_clk_hz / ADC_PRV_HZ_PER_KHZ)) / ADC_PRV_USEC_PER_SEC) +
48+
1U;
49+
50+
/* Ensure the state count is not smaller than the minimum allowed */
51+
if (sample_states < ADC_SAMPLE_STATE_COUNT_MIN) {
52+
sample_states = ADC_SAMPLE_STATE_COUNT_MIN;
53+
}
54+
55+
/* Clamp to hardware maximum (8-bit register, so max 255) */
56+
if (sample_states > ADC_SAMPLE_STATE_COUNT_MAX) {
57+
sample_states = ADC_SAMPLE_STATE_COUNT_MAX;
58+
}
59+
60+
*p_sample_count = sample_states;
61+
62+
return FSP_SUCCESS;
63+
}

0 commit comments

Comments
 (0)