|
| 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