Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions nrfx/drivers/include/nrfx_comp.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#include <nrfx.h>
#include <haly/nrfy_comp.h>
#include <helpers/nrfx_analog_common.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -95,15 +96,15 @@ typedef enum
typedef struct
{
nrf_comp_ref_t reference; ///< Reference selection.
nrf_comp_ext_ref_t ext_ref; ///< External analog reference selection.
nrfx_analog_input_t ext_ref; ///< External analog reference selection.
nrf_comp_main_mode_t main_mode; ///< Main operation mode.
nrf_comp_th_t threshold; ///< Structure holding THDOWN and THUP values needed by the COMP_TH register.
nrf_comp_sp_mode_t speed_mode; ///< Speed and power mode.
nrf_comp_hyst_t hyst; ///< Comparator hysteresis.
#if NRF_COMP_HAS_ISOURCE
nrf_isource_t isource; ///< Current source selected on analog input.
#endif
nrf_comp_input_t input; ///< Input to be monitored.
nrfx_analog_input_t input; ///< Input to be monitored.
uint8_t interrupt_priority; ///< Interrupt priority.
} nrfx_comp_config_t;

Expand Down Expand Up @@ -136,7 +137,7 @@ typedef struct
.speed_mode = NRF_COMP_SP_MODE_HIGH, \
.hyst = NRF_COMP_HYST_NO_HYST, \
NRFX_COND_CODE_1(NRF_COMP_HAS_ISOURCE, (.isource = NRF_COMP_ISOURCE_OFF,), ()) \
.input = (nrf_comp_input_t)_input, \
.input = (nrfx_analog_input_t)_input, \
.interrupt_priority = NRFX_COMP_DEFAULT_CONFIG_IRQ_PRIORITY \
}

Expand All @@ -157,6 +158,7 @@ typedef struct
* @retval NRFX_ERROR_BUSY The LPCOMP peripheral is already in use.
* This is possible only if @ref nrfx_prs module
* is enabled.
* @retval NRFX_ERROR_INVALID_PARAM The analog input pin or external reference is invalid.
*/
nrfx_err_t nrfx_comp_init(nrfx_comp_config_t const * p_config,
nrfx_comp_event_handler_t event_handler);
Expand All @@ -169,6 +171,7 @@ nrfx_err_t nrfx_comp_init(nrfx_comp_config_t const * p_config,
* @retval NRFX_SUCCESS Reconfiguration was successful.
* @retval NRFX_ERROR_BUSY The driver is running and cannot be reconfigured.
* @retval NRFX_ERROR_INVALID_STATE The driver is uninitialized.
* @retval NRFX_ERROR_INVALID_PARAM The analog input pin or external reference is invalid.
*/
nrfx_err_t nrfx_comp_reconfigure(nrfx_comp_config_t const * p_config);

Expand All @@ -194,9 +197,11 @@ bool nrfx_comp_init_check(void);
/**
* @brief Function for setting the analog input.
*
* @param[in] psel COMP analog pin selection.
* @param[in] psel Generic analog pin selection.
*
* @retval NRFX_ERROR_INVALID_PARAM The analog input pin is invalid.
*/
void nrfx_comp_pin_select(nrf_comp_input_t psel);
nrfx_err_t nrfx_comp_pin_select(nrfx_analog_input_t psel);

/**
* @brief Function for starting the COMP peripheral and interrupts.
Expand Down
19 changes: 11 additions & 8 deletions nrfx/drivers/include/nrfx_lpcomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#include <nrfx.h>
#include <haly/nrfy_lpcomp.h>
#include <helpers/nrfx_analog_common.h>

#ifdef __cplusplus
extern "C" {
Expand All @@ -58,17 +59,17 @@ typedef void (* nrfx_lpcomp_event_handler_t)(nrf_lpcomp_event_t event);
typedef struct
{
#if NRFX_API_VER_AT_LEAST(3, 2, 0) || defined(__NRFX_DOXYGEN__)
nrf_lpcomp_ref_t reference; ///< Reference selection.
nrf_lpcomp_ext_ref_t ext_ref; ///< External analog reference selection.
nrf_lpcomp_detect_t detection; ///< Detection type.
nrf_lpcomp_ref_t reference; ///< Reference selection.
nrfx_analog_input_t ext_ref; ///< External analog reference selection.
nrf_lpcomp_detect_t detection; ///< Detection type.
#if NRF_LPCOMP_HAS_HYST
nrf_lpcomp_hyst_t hyst; ///< Comparator hysteresis.
nrf_lpcomp_hyst_t hyst; ///< Comparator hysteresis.
#endif
#else
nrf_lpcomp_config_t config; ///< Peripheral configuration.
nrf_lpcomp_config_t config; ///< Peripheral configuration.
#endif
nrf_lpcomp_input_t input; ///< Input to be monitored.
uint8_t interrupt_priority; ///< LPCOMP interrupt priority.
nrfx_analog_input_t input; ///< Input to be monitored.
uint8_t interrupt_priority; ///< LPCOMP interrupt priority.
} nrfx_lpcomp_config_t;

#if NRFX_API_VER_AT_LEAST(3, 2, 0) || defined(__NRFX_DOXYGEN__)
Expand All @@ -87,7 +88,7 @@ typedef struct
.reference = NRF_LPCOMP_REF_SUPPLY_4_8, \
.detection = NRF_LPCOMP_DETECT_CROSS, \
NRFX_COND_CODE_1(LPCOMP_FEATURE_HYST_PRESENT, (.hyst = NRF_LPCOMP_HYST_NOHYST,), ()) \
.input = (nrf_lpcomp_input_t)_input, \
.input = (nrfx_analog_input_t)_input, \
.interrupt_priority = NRFX_LPCOMP_DEFAULT_CONFIG_IRQ_PRIORITY \
}
#else
Expand Down Expand Up @@ -121,6 +122,7 @@ typedef struct
* @retval NRFX_ERROR_BUSY The COMP peripheral is already in use.
* This is possible only if @ref nrfx_prs module
* is enabled.
* @retval NRFX_ERROR_INVALID_PARAM The analog input pin or external reference is invalid.
*/
nrfx_err_t nrfx_lpcomp_init(nrfx_lpcomp_config_t const * p_config,
nrfx_lpcomp_event_handler_t event_handler);
Expand All @@ -133,6 +135,7 @@ nrfx_err_t nrfx_lpcomp_init(nrfx_lpcomp_config_t const * p_config,
* @retval NRFX_SUCCESS Reconfiguration was successful.
* @retval NRFX_ERROR_BUSY The driver is running and cannot be reconfigured.
* @retval NRFX_ERROR_INVALID_STATE The driver is uninitialized.
* @retval NRFX_ERROR_INVALID_PARAM The analog input pin or external reference is invalid.
*/
nrfx_err_t nrfx_lpcomp_reconfigure(nrfx_lpcomp_config_t const * p_config);

Expand Down
44 changes: 35 additions & 9 deletions nrfx/drivers/include/nrfx_saadc.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#include <nrfx.h>
#include <haly/nrfy_saadc.h>
#include <helpers/nrfx_analog_common.h>

#ifdef __cplusplus
extern "C" {
Expand All @@ -60,6 +61,12 @@ extern "C" {
#define NRFX_SAADC_DEFAULT_CONV_TIME 7
#endif

/** @brief Maximum value of the internal timer interval in microseconds. */
#define NRFX_SAADC_INTERNAL_TIMER_INTERVAL_MAX_US 128U

/** @brief Symbol specifying internal reference voltage. */
#define NRFX_SAADC_REF_INTERNAL_VALUE NRFX_ANALOG_REF_INTERNAL_VAL

/**
* @brief SAADC channel default configuration for the single-ended mode.
*
Expand Down Expand Up @@ -102,8 +109,8 @@ extern "C" {
(.conv_time = NRFX_SAADC_DEFAULT_CONV_TIME,), \
()) \
}, \
.pin_p = (nrf_saadc_input_t)_pin_p, \
.pin_n = NRF_SAADC_INPUT_DISABLED, \
.pin_p = (nrfx_analog_input_t)_pin_p, \
.pin_n = NRFX_ANALOG_INPUT_DISABLED, \
.channel_index = _index, \
}

Expand Down Expand Up @@ -150,8 +157,8 @@ extern "C" {
(.conv_time = NRFX_SAADC_DEFAULT_CONV_TIME,), \
()) \
}, \
.pin_p = (nrf_saadc_input_t)_pin_p, \
.pin_n = (nrf_saadc_input_t)_pin_n, \
.pin_p = (nrfx_analog_input_t)_pin_p, \
.pin_n = (nrfx_analog_input_t)_pin_n, \
.channel_index = _index, \
}

Expand Down Expand Up @@ -207,8 +214,8 @@ extern "C" {
typedef struct
{
nrf_saadc_channel_config_t channel_config; ///< Channel hardware configuration.
nrf_saadc_input_t pin_p; ///< Input positive pin selection.
nrf_saadc_input_t pin_n; ///< Input negative pin selection.
nrfx_analog_input_t pin_p; ///< Input positive pin selection.
nrfx_analog_input_t pin_n; ///< Input negative pin selection.
uint8_t channel_index; ///< Channel index.
} nrfx_saadc_channel_t;

Expand Down Expand Up @@ -297,6 +304,23 @@ void nrfx_saadc_uninit(void);
*/
bool nrfx_saadc_init_check(void);

/**
* @brief Function for getting the internal timer CC value from the interval in microseconds.
*
* @note The internal timer runs at 16 MHz, so to convert the interval in microseconds
* to the internal timer CC value, we can use the formula:
* interval_cc = interval_us * 16 MHz
* where 16 MHz is the frequency of the internal timer.
* @note The maximum value for interval_cc is 2047, which corresponds to
* approximately 7816 Hz ~ 128us.
* The minimum value for interval_cc depends on the SoC.
*
* @param[in] interval_us The interval in microseconds to be converted.
*
* @return The internal timer CC value.
*/
uint16_t nrfx_saadc_interval_to_cc(uint16_t interval_us);

/**
* @brief Function for configuring multiple SAADC channels.
*
Expand All @@ -313,7 +337,8 @@ bool nrfx_saadc_init_check(void);
*
* @retval NRFX_SUCCESS Configuration was successful.
* @retval NRFX_ERROR_BUSY There is a conversion or calibration ongoing.
* @retval NRFX_ERROR_INVALID_PARAM Attempt to configure the same channel more than once.
* @retval NRFX_ERROR_INVALID_PARAM Attempt to configure the same channel more than once or
* attempt to configure an invalid analog pin.
*/
nrfx_err_t nrfx_saadc_channels_config(nrfx_saadc_channel_t const * p_channels,
uint32_t channel_count);
Expand All @@ -330,8 +355,9 @@ nrfx_err_t nrfx_saadc_channels_config(nrfx_saadc_channel_t const * p_channels,
*
* @param[in] p_channel Pointer to the channel configuration structure.
*
* @retval NRFX_SUCCESS Configuration was successful.
* @retval NRFX_ERROR_BUSY There is a conversion or calibration ongoing.
* @retval NRFX_SUCCESS Configuration was successful.
* @retval NRFX_ERROR_BUSY There is a conversion or calibration ongoing.
* @retval NRFX_ERROR_INVALID_PARAM Attempt to configure an invalid analog pin.
*/
nrfx_err_t nrfx_saadc_channel_config(nrfx_saadc_channel_t const * p_channel);

Expand Down
90 changes: 82 additions & 8 deletions nrfx/drivers/src/nrfx_comp.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#if NRFX_CHECK(NRFX_COMP_ENABLED)

#include <nrfx_comp.h>
#include "nrfx_comp_common.h"
#include "prs/nrfx_prs.h"

#define NRFX_LOG_MODULE COMP
Expand All @@ -56,20 +57,70 @@
static nrfx_comp_event_handler_t m_comp_event_handler = NULL;
static nrfx_drv_state_t m_state = NRFX_DRV_STATE_UNINITIALIZED;

static void comp_configure(nrfx_comp_config_t const * p_config)
static nrfx_err_t comp_input_convert(nrfx_analog_input_t analog_input,
nrf_comp_input_t * p_comp_input)
{
NRFX_ASSERT(p_comp_input);

nrf_comp_input_t comp_input = nrfx_comp_ain_get(analog_input);

if (comp_input == (nrf_comp_input_t)NRFX_COMP_INPUT_NOT_PRESENT)
{
return NRFX_ERROR_INVALID_PARAM;
}

*p_comp_input = comp_input;
return NRFX_SUCCESS;
}

static nrfx_err_t comp_ext_ref_input_convert(nrfx_analog_input_t analog_input,
nrf_comp_ext_ref_t * p_ext_ref_input)
{
NRFX_ASSERT(p_ext_ref_input);

nrf_comp_ext_ref_t ext_ref_input = nrfx_comp_ext_ref_get(analog_input);

if (ext_ref_input == (nrf_comp_ext_ref_t)NRFX_COMP_INPUT_NOT_PRESENT)
{
return NRFX_ERROR_INVALID_PARAM;
}

*p_ext_ref_input = ext_ref_input;
return NRFX_SUCCESS;
}

static nrfx_err_t comp_configure(nrfx_comp_config_t const * p_config)
{
nrfx_err_t err_code;

nrfy_comp_config_t nrfy_config =
{
.reference = p_config->reference,
.ext_ref = p_config->ext_ref,
.main_mode = p_config->main_mode,
.threshold = p_config->threshold,
.speed_mode = p_config->speed_mode,
.hyst = p_config->hyst,
NRFX_COND_CODE_1(NRF_COMP_HAS_ISOURCE, (.isource = p_config->isource,), ())
.input = p_config->input
};

err_code = comp_input_convert(p_config->input, &nrfy_config.input);
if (err_code != NRFX_SUCCESS)
{
NRFX_LOG_WARNING("Function: %s, error code: %s.",
__func__,
NRFX_LOG_ERROR_STRING_GET(err_code));
return err_code;
}

err_code = comp_ext_ref_input_convert(p_config->ext_ref, &nrfy_config.ext_ref);
if (err_code != NRFX_SUCCESS)
{
NRFX_LOG_WARNING("Function: %s, error code: %s.",
__func__,
NRFX_LOG_ERROR_STRING_GET(err_code));
return err_code;
}

nrfy_comp_periph_configure(NRF_COMP, &nrfy_config);
nrfy_comp_int_init(NRF_COMP,
NRF_COMP_INT_READY_MASK |
Expand All @@ -82,6 +133,8 @@ static void comp_configure(nrfx_comp_config_t const * p_config)
uint32_t trim = nrf_ficr_global_comp_reftrim_get(NRF_FICR);
nrfy_comp_reftrim_set(NRF_COMP, trim);
#endif

return NRFX_SUCCESS;
}

static void comp_execute_handler(nrf_comp_event_t event, uint32_t event_mask)
Expand All @@ -107,7 +160,6 @@ void nrfx_comp_irq_handler(void)
comp_execute_handler(NRF_COMP_EVENT_CROSS, evt_mask);
}


nrfx_err_t nrfx_comp_init(nrfx_comp_config_t const * p_config,
nrfx_comp_event_handler_t event_handler)
{
Expand Down Expand Up @@ -152,7 +204,11 @@ nrfx_err_t nrfx_comp_init(nrfx_comp_config_t const * p_config,

if (p_config)
{
comp_configure(p_config);
err_code = comp_configure(p_config);
if (err_code != NRFX_SUCCESS)
{
return err_code;
}
}

nrfy_comp_enable(NRF_COMP);
Expand Down Expand Up @@ -188,7 +244,14 @@ nrfx_err_t nrfx_comp_reconfigure(nrfx_comp_config_t const * p_config)
return err_code;
}
nrfy_comp_disable(NRF_COMP);
comp_configure(p_config);
err_code = comp_configure(p_config);
if (err_code != NRFX_SUCCESS)
{
NRFX_LOG_WARNING("Function: %s, error code: %s.",
__func__,
NRFX_LOG_ERROR_STRING_GET(err_code));
return err_code;
}
nrfy_comp_enable(NRF_COMP);
return NRFX_SUCCESS;
}
Expand All @@ -215,22 +278,33 @@ bool nrfx_comp_init_check(void)
return (m_state != NRFX_DRV_STATE_UNINITIALIZED);
}

void nrfx_comp_pin_select(nrf_comp_input_t psel)
nrfx_err_t nrfx_comp_pin_select(nrfx_analog_input_t psel)
{
NRFX_ASSERT(m_state != NRFX_DRV_STATE_UNINITIALIZED);

nrfx_err_t err = NRFX_SUCCESS;
bool comp_enable_state = nrfy_comp_enable_check(NRF_COMP);
nrf_comp_input_t comp_input;

nrfy_comp_task_trigger(NRF_COMP, NRF_COMP_TASK_STOP);
if (m_state == NRFX_DRV_STATE_POWERED_ON)
{
m_state = NRFX_DRV_STATE_INITIALIZED;
}
nrfy_comp_disable(NRF_COMP);
nrfy_comp_input_select(NRF_COMP, psel);

err = comp_input_convert(psel, &comp_input);
if (err != NRFX_SUCCESS)
{
return err;
}
nrfy_comp_input_select(NRF_COMP, comp_input);
if (comp_enable_state == true)
{
nrfy_comp_enable(NRF_COMP);
}

return err;
}

void nrfx_comp_start(uint32_t comp_int_mask, uint32_t comp_shorts_mask)
Expand Down
Loading