libeasymcp2221 2.0.3
Functions
mcp2221_analog.h File Reference

Analog, clock-output, and interrupt-on-change helpers. More...

#include <stdint.h>
#include "mcp2221.h"
Include dependency graph for mcp2221_analog.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

mcp2221_error_code_t mcp2221_analog_set_vdd (mcp2221_t *dev, double volts)
 Store the externally supplied MCP2221 supply voltage. More...
 
mcp2221_error_code_t mcp2221_analog_get_vdd (const mcp2221_t *dev, double *volts)
 Return the configured MCP2221 supply voltage. More...
 
mcp2221_error_code_t mcp2221_adc_config (mcp2221_t *dev, const char *ref_str)
 Configure the ADC voltage reference. More...
 
mcp2221_error_code_t mcp2221_adc_read_raw (mcp2221_t *dev, uint16_t out[3])
 Read the three MCP2221 ADC channels as raw 10-bit values. More...
 
mcp2221_error_code_t mcp2221_adc_read_normalized (mcp2221_t *dev, double out[3])
 Read the three ADC channels as normalized values. More...
 
mcp2221_error_code_t mcp2221_adc_read_volts (mcp2221_t *dev, double out[3])
 Read the three ADC channels as voltages. More...
 
mcp2221_error_code_t mcp2221_dac_config (mcp2221_t *dev, const char *ref_str)
 Configure the DAC voltage reference while preserving its output code. More...
 
mcp2221_error_code_t mcp2221_dac_config_out (mcp2221_t *dev, const char *ref_str, int out_code)
 Configure the DAC voltage reference and optionally its raw output code. More...
 
mcp2221_error_code_t mcp2221_dac_write_raw (mcp2221_t *dev, uint8_t code)
 Write a raw 5-bit DAC output code. More...
 
mcp2221_error_code_t mcp2221_dac_write_normalized (mcp2221_t *dev, double normalized)
 Write a normalized DAC output value. More...
 
mcp2221_error_code_t mcp2221_dac_write_volts (mcp2221_t *dev, double volts)
 Write a DAC output voltage. More...
 
mcp2221_error_code_t mcp2221_clock_config (mcp2221_t *dev, int duty_percent, const char *freq_str)
 Configure the MCP2221 clock output. More...
 
mcp2221_error_code_t mcp2221_ioc_read (mcp2221_t *dev, uint8_t *flag)
 Read the interrupt-on-change flag. More...
 
mcp2221_error_code_t mcp2221_ioc_clear (mcp2221_t *dev)
 Clear the interrupt-on-change flag. More...
 
mcp2221_error_code_t mcp2221_ioc_config (mcp2221_t *dev, const char *edge)
 Configure interrupt-on-change edge detection. More...
 

Detailed Description

Analog, clock-output, and interrupt-on-change helpers.

Definition in file mcp2221_analog.h.

Function Documentation

◆ mcp2221_adc_config()

mcp2221_error_code_t mcp2221_adc_config ( mcp2221_t dev,
const char *  ref_str 
)

Configure the ADC voltage reference.

Accepted reference strings are "OFF", "VDD", "1.024V", "2.048V", and "4.096V". Matching is case-insensitive.

Parameters
[in]devOpen MCP2221 device handle.
[in]ref_strADC reference selection string.
Returns
MCP2221_ERR_OK on success, MCP2221_ERR_INVALID for an unsupported reference or invalid argument, or another mcp2221_error_code_t value on failure.

◆ mcp2221_adc_read_normalized()

mcp2221_error_code_t mcp2221_adc_read_normalized ( mcp2221_t dev,
double  out[3] 
)

Read the three ADC channels as normalized values.

Each raw 10-bit result is divided by 1024.0 to match EasyMCP2221 behavior. The returned range is therefore 0.0 through 1023.0/1024.0 rather than reaching exactly 1.0.

Array index 0 through 2 corresponds to GP1 through GP3.

Parameters
[in]devOpen MCP2221 device handle.
[out]outThree-element array receiving normalized ADC values.
Returns
MCP2221_ERR_OK on success, MCP2221_ERR_INVALID for invalid arguments or ADC data, or another mcp2221_error_code_t value on failure.

◆ mcp2221_adc_read_raw()

mcp2221_error_code_t mcp2221_adc_read_raw ( mcp2221_t dev,
uint16_t  out[3] 
)

Read the three MCP2221 ADC channels as raw 10-bit values.

The returned array maps channels to pins as follows:

  • out[0]: ADC channel 0 on GP1
  • out[1]: ADC channel 1 on GP2
  • out[2]: ADC channel 2 on GP3
Parameters
[in]devOpen MCP2221 device handle.
[out]outThree-element array receiving raw values from 0 through 1023.
Returns
MCP2221_ERR_OK on success, MCP2221_ERR_INVALID for invalid arguments, or another mcp2221_error_code_t value on failure.

◆ mcp2221_adc_read_volts()

mcp2221_error_code_t mcp2221_adc_read_volts ( mcp2221_t dev,
double  out[3] 
)

Read the three ADC channels as voltages.

The current ADC reference is read from device SRAM. Fixed internal references are resolved automatically. If VDD is selected, a supply voltage must first have been provided with mcp2221_analog_set_vdd(). An OFF reference cannot be converted to volts.

Each raw result is converted as raw / 1024.0 * reference_voltage. Array index 0 through 2 corresponds to GP1 through GP3.

Parameters
[in]devOpen MCP2221 device handle.
[out]outThree-element array receiving ADC voltages.
Returns
MCP2221_ERR_OK on success, MCP2221_ERR_INVALID when the reference cannot be resolved or an argument/data value is invalid, or another mcp2221_error_code_t value on failure.

◆ mcp2221_analog_get_vdd()

mcp2221_error_code_t mcp2221_analog_get_vdd ( const mcp2221_t dev,
double *  volts 
)

Return the configured MCP2221 supply voltage.

Parameters
[in]devOpen MCP2221 device handle.
[out]voltsReceives the previously configured supply voltage.
Returns
MCP2221_ERR_OK on success, or MCP2221_ERR_INVALID if an argument is invalid or no VDD value has been configured.
See also
mcp2221_analog_set_vdd()

◆ mcp2221_analog_set_vdd()

mcp2221_error_code_t mcp2221_analog_set_vdd ( mcp2221_t dev,
double  volts 
)

Store the externally supplied MCP2221 supply voltage.

The MCP2221 cannot provide the application with a sufficiently accurate VDD value for ADC/DAC conversion, so the caller supplies it explicitly. The value is stored in the device context and is used whenever a voltage conversion operates with VDD as its reference.

Parameters
[in]devOpen MCP2221 device handle.
[in]voltsSupply voltage in volts. Must be within the supported MCP2221 supply-voltage range.
Returns
MCP2221_ERR_OK on success, MCP2221_ERR_INVALID for an invalid handle or voltage, or another mcp2221_error_code_t value on failure.

◆ mcp2221_clock_config()

mcp2221_error_code_t mcp2221_clock_config ( mcp2221_t dev,
int  duty_percent,
const char *  freq_str 
)

Configure the MCP2221 clock output.

Supported duty-cycle values are 0, 25, 50, and 75 percent. Supported frequency strings are "375kHz", "750kHz", "1.5MHz", "3MHz", "6MHz", "12MHz", and "24MHz". Frequency matching is case-insensitive.

Parameters
[in]devOpen MCP2221 device handle.
[in]duty_percentClock duty cycle in percent.
[in]freq_strClock frequency selection string.
Returns
MCP2221_ERR_OK on success, MCP2221_ERR_INVALID for an unsupported duty cycle, frequency, or invalid argument, or another mcp2221_error_code_t value on failure.

◆ mcp2221_dac_config()

mcp2221_error_code_t mcp2221_dac_config ( mcp2221_t dev,
const char *  ref_str 
)

Configure the DAC voltage reference while preserving its output code.

Accepted reference strings are "OFF", "VDD", "1.024V", "2.048V", and "4.096V". Matching is case-insensitive.

Parameters
[in]devOpen MCP2221 device handle.
[in]ref_strDAC reference selection string.
Returns
MCP2221_ERR_OK on success, MCP2221_ERR_INVALID for an unsupported reference or invalid argument, or another mcp2221_error_code_t value on failure.
See also
mcp2221_dac_config_out()

◆ mcp2221_dac_config_out()

mcp2221_error_code_t mcp2221_dac_config_out ( mcp2221_t dev,
const char *  ref_str,
int  out_code 
)

Configure the DAC voltage reference and optionally its raw output code.

A negative out_code preserves the current 5-bit DAC value. Values from 0 through 31 set the new output code.

When the reference changes, the helper first turns the DAC reference module off and sets the output value to zero before applying the requested reference and value. This preserves the EasyMCP2221 workaround for the MCP2221 ADC/DAC reference-transition hardware quirk.

Parameters
[in]devOpen MCP2221 device handle.
[in]ref_strDAC reference selection string. Accepted values are "OFF", "VDD", "1.024V", "2.048V", and "4.096V" (case-insensitive).
[in]out_codeRaw DAC code from 0 through 31, or any negative value to preserve the current code.
Returns
MCP2221_ERR_OK on success, MCP2221_ERR_INVALID for an invalid reference, code, or argument, or another mcp2221_error_code_t value on failure.

◆ mcp2221_dac_write_normalized()

mcp2221_error_code_t mcp2221_dac_write_normalized ( mcp2221_t dev,
double  normalized 
)

Write a normalized DAC output value.

The value is multiplied by 32 and converted to the 5-bit DAC code. Accepted inputs range from 0.0 through 31.0/32.0 inclusive. Values outside that range, including NaN, are rejected.

Parameters
[in]devOpen MCP2221 device handle.
[in]normalizedNormalized DAC output value.
Returns
MCP2221_ERR_OK on success, MCP2221_ERR_INVALID for an invalid argument or normalized value, or another mcp2221_error_code_t value on failure.

◆ mcp2221_dac_write_raw()

mcp2221_error_code_t mcp2221_dac_write_raw ( mcp2221_t dev,
uint8_t  code 
)

Write a raw 5-bit DAC output code.

The currently configured DAC voltage reference is preserved.

Parameters
[in]devOpen MCP2221 device handle.
[in]codeRaw DAC code from 0 through 31.
Returns
MCP2221_ERR_OK on success, MCP2221_ERR_INVALID for an invalid argument or code, or another mcp2221_error_code_t value on failure.

◆ mcp2221_dac_write_volts()

mcp2221_error_code_t mcp2221_dac_write_volts ( mcp2221_t dev,
double  volts 
)

Write a DAC output voltage.

The current DAC reference is read from device SRAM. Fixed internal references are resolved automatically. If VDD is selected, a supply voltage must first have been provided with mcp2221_analog_set_vdd(). An OFF reference cannot be converted to a voltage.

The largest accepted voltage is 31.0/32.0 of the selected reference voltage. Values between two DAC steps are truncated to the lower raw output code.

Parameters
[in]devOpen MCP2221 device handle.
[in]voltsRequested DAC output voltage.
Returns
MCP2221_ERR_OK on success, MCP2221_ERR_INVALID for an invalid voltage, unresolved reference, or invalid argument, or another mcp2221_error_code_t value on failure.

◆ mcp2221_ioc_clear()

mcp2221_error_code_t mcp2221_ioc_clear ( mcp2221_t dev)

Clear the interrupt-on-change flag.

Parameters
[in]devOpen MCP2221 device handle.
Returns
MCP2221_ERR_OK on success, MCP2221_ERR_INVALID for an invalid device handle, or another mcp2221_error_code_t value on failure.

◆ mcp2221_ioc_config()

mcp2221_error_code_t mcp2221_ioc_config ( mcp2221_t dev,
const char *  edge 
)

Configure interrupt-on-change edge detection.

Accepted edge strings are "none", "rising", "falling", and "both". Matching is case-insensitive.

Parameters
[in]devOpen MCP2221 device handle.
[in]edgeRequested edge-detection mode.
Returns
MCP2221_ERR_OK on success, MCP2221_ERR_INVALID for an unsupported mode or invalid argument, or another mcp2221_error_code_t value on failure.

◆ mcp2221_ioc_read()

mcp2221_error_code_t mcp2221_ioc_read ( mcp2221_t dev,
uint8_t *  flag 
)

Read the interrupt-on-change flag.

Parameters
[in]devOpen MCP2221 device handle.
[out]flagReceives the interrupt-on-change flag reported by the MCP2221.
Returns
MCP2221_ERR_OK on success, MCP2221_ERR_INVALID for invalid arguments, or another mcp2221_error_code_t value on failure.