PSoC 6 Peripheral Driver Library
Initialization and Basic Functions

General Description

This set of functions is for initialization and basic usage.

Functions

cy_en_sar_status_t Cy_SAR_Init (SAR_Type *base, const cy_stc_sar_config_t *config)
 Initialize all SAR configuration registers. More...
 
cy_en_sar_status_t Cy_SAR_DeInit (SAR_Type *base, bool deInitRouting)
 Reset SAR registers back to power on reset defaults. More...
 
void Cy_SAR_Enable (SAR_Type *base)
 Power up the SAR ADC subsystem block. More...
 
__STATIC_INLINE void Cy_SAR_Disable (SAR_Type *base)
 Turn off the hardware block. More...
 
void Cy_SAR_StartConvert (SAR_Type *base, cy_en_sar_start_convert_sel_t startSelect)
 Start a single scan (one shot) of all enabled channels or start scanning continuously. More...
 
void Cy_SAR_StopConvert (SAR_Type *base)
 Stop continuous scanning of enabled channels. More...
 
cy_en_sar_status_t Cy_SAR_IsEndConversion (SAR_Type *base, cy_en_sar_return_mode_t retMode)
 Immediately return the status of the conversion or does not return (blocking) until the conversion completes, depending on the retMode parameter. More...
 
int16_t Cy_SAR_GetResult16 (const SAR_Type *base, uint32_t chan)
 Return the data available in the channel result data register as a signed 16-bit integer. More...
 
int32_t Cy_SAR_GetResult32 (const SAR_Type *base, uint32_t chan)
 Return the data available in the channel result data register as a signed 32-bit integer. More...
 
__STATIC_INLINE uint32_t Cy_SAR_GetChanResultUpdated (const SAR_Type *base)
 Return whether the RESULT register has been updated or not. More...
 

Function Documentation

◆ Cy_SAR_Init()

cy_en_sar_status_t Cy_SAR_Init ( SAR_Type base,
const cy_stc_sar_config_t config 
)

Initialize all SAR configuration registers.

If routing is to be configured, all switches will be cleared before being initialized.

Parameters
basePointer to structure describing registers
configPointer to structure containing configuration data. See cy_stc_sar_config_t and guidance in the Initialization and Enable section.
Returns
Function Usage
/* Scenario: Configure the SAR to sample two channels with a scan rate of 50 ksps.
* - Channel 0 is a single ended channel on P10.2
* - Channel 1 is a differential channel between P10.0 and P10.1.
*/
#define CHANNEL_EN 3u /* Enable channels 0 and 1. */
#define CHANNEL_0_CONFIG CY_SAR_CHAN_SINGLE_ENDED /* Channel 0 is a single ended channel. */ \
| CY_SAR_CHAN_SAMPLE_TIME_0 /* Aperature time is set by Sample Time 0. */\
| CY_SAR_POS_PORT_ADDR_SARMUX /* Use the SARMUX dedicated port (see the device datasheet for the SARMUX port). */\
| CY_SAR_CHAN_POS_PIN_ADDR_2 /* Use Pin 2 of the SARMUX. */\
| CY_SAR_CHAN_AVG_DISABLE /* Disable averaging. */
#define CHANNEL_1_CONFIG CY_SAR_CHAN_DIFFERENTIAL_PAIRED /* Channel 1 is a differential paired channel. */ \
| CY_SAR_CHAN_SAMPLE_TIME_1 /* Aperature time is set by Sample Time 1. */\
| CY_SAR_POS_PORT_ADDR_SARMUX /* Use the SARMUX dedicated port (see the device datasheet for the SARMUX port). */\
| CY_SAR_CHAN_POS_PIN_ADDR_0 /* Positive terminal is connected to Pin 0 and negative terminal is connected to Pin 1 of the SARMUX. */\
| CY_SAR_CHAN_AVG_ENABLE /* Enable averaging. */
#define CHANNEL_CONFIG {(uint32_t)CHANNEL_0_CONFIG, \
(uint32_t)CHANNEL_1_CONFIG, \
0U,0U,0U,0U,0U,0U,0U,0U,0U,0U,0U,0U,0U,0U} /* All other channels are unconfigured. */
#define CTRL (uint32_t)CY_SAR_VREF_PWR_100 /* Set reference voltage buffer to full power (100%) */\
| (uint32_t)CY_SAR_VREF_SEL_VDDA_DIV_2 /* Select internal VDDA/2 as the Vref. */\
| (uint32_t)CY_SAR_BYPASS_CAP_ENABLE /* Enable Vref bypass capacitory. */\
| (uint32_t)CY_SAR_NEG_SEL_VREF /* Use Vref as the negative terminal for all single-ended channels. */\
| (uint32_t)CY_SAR_CTRL_NEGVREF_HW /* Enable hardware control of switch between Vref and negative terminal. */\
| (uint32_t)CY_SAR_CTRL_COMP_DLY_2P5 /* Set comparator latch delay to 2.5 ns delay for maximum conversion rates. */\
| (uint32_t)CY_SAR_COMP_PWR_100 /* Set comparator power to full power for maximum conversion rates. */\
| (uint32_t)CY_SAR_DEEPSLEEP_SARMUX_OFF /* Disable SARMUX in Deep Sleep mode. */\
| (uint32_t)CY_SAR_SARSEQ_SWITCH_ENABLE /* Enable the SARSEQ. */
#define SAMPLE_CTRL (uint32_t)CY_SAR_RIGHT_ALIGN /* Right align result data to bits[11:0]. */\
| (uint32_t)CY_SAR_SINGLE_ENDED_UNSIGNED /* Single ended channels are unsigned. */\
| (uint32_t)CY_SAR_DIFFERENTIAL_SIGNED /* Differential channels are signed. */\
| (uint32_t)CY_SAR_AVG_CNT_2 /* Set number of samples averaged to 2. */\
| (uint32_t)CY_SAR_AVG_MODE_SEQUENTIAL_FIXED /* Averaging mode is sequential 12-bit fixed.*/\
| (uint32_t)CY_SAR_TRIGGER_MODE_FW_ONLY /* Hardware trigger is disabled. */
#define SAMPLE_TIME01 (135 << CY_SAR_SAMPLE_TIME0_SHIFT) /* Sample Time 0 set to 134 ADC clock cycles. */ \
| (4 << CY_SAR_SAMPLE_TIME1_SHIFT) /* Sample Time 1 set to 3 ADC clock cycles. */
/* Note, sample times 2 and 3 are not used by any channels in this example. */
#define SAMPLE_TIME23 (4 << CY_SAR_SAMPLE_TIME2_SHIFT) /* Sample Time 2 set to 3 ADC clock cycles. */ \
| (4 << CY_SAR_SAMPLE_TIME3_SHIFT) /* Sample Time 3 set to 3 ADC clock cycles. */
#define RANGE_THRES (0x300uL << CY_SAR_RANGE_LOW_SHIFT) /* Lower limit of range detection is 0x300. */\
| (0xC00uL << CY_SAR_RANGE_HIGH_SHIFT) /* Upper limit of range detection is 0xC00. */
#define INTR_MASK CY_SAR_INTR_EOS_MASK | CY_SAR_INTR_OVERFLOW_MASK /* Enable both EOS and overflow interrupt evnts. */
#define MUX_SWITCH CY_SAR_MUX_FW_P2_VPLUS /* Connect Pin 2 of the SARMUX to the positive terminal of the SARADC. */\
| CY_SAR_MUX_FW_P0_VPLUS /* Connect Pin 0 of the SARMUX to the positive terminal of the SARADC. */\
| CY_SAR_MUX_FW_P1_VMINUS /* Connect Pin 1 of the SARMUX to the negative terminal of the SARADC. */
#define MUX_SWITCH_SQ_CTRL CY_SAR_MUX_SQ_CTRL_P0 /* Enable sequencer control of Pins 0 of the SARMUX. */\
| CY_SAR_MUX_SQ_CTRL_P1 /* Enable sequencer control of Pins 1 of the SARMUX. */\
| CY_SAR_MUX_SQ_CTRL_P2 /* Enable sequencer control of Pins 2 of the SARMUX. */
/* Define the config structure. */
const cy_stc_sar_config_t config =
{
/* .ctrl */ (uint32_t)CTRL,
/* .sampleCtrl */ (uint32_t)SAMPLE_CTRL,
/* .sampleTime01 */ SAMPLE_TIME01,
/* .sampleTime23 */ SAMPLE_TIME23,
/* .rangeThres */ RANGE_THRES,
/* .rangeCond */ CY_SAR_RANGE_COND_OUTSIDE, /* Set limit condition to outside of lower and upper limits. */
/* .chanEn */ CHANNEL_EN,
/* .chanConfig */ CHANNEL_CONFIG,
/* .intrMask */ INTR_MASK,
/* .satIntrMask */ 0u, /* Disable saturation interrupts. */
/* .rangeIntrMask */ 0u, /* Disable limit interrupts. */
/* .muxSwitch */ MUX_SWITCH,
/* .muxSwitchSqCtrl */ MUX_SWITCH_SQ_CTRL,
/* .configRouting */ true, /* Configure the routing. */
/* .vrefMvValue */ 1650u, /* Vdda = 3.3 V so Vref = Vdda/2 = 1.65 V. */
};
status = Cy_SAR_Init(SAR, &config);
if (CY_SAR_SUCCESS == status)
{
/* Turn on the SAR hardware. */
/* Begin continuous conversions. */
}

◆ Cy_SAR_DeInit()

cy_en_sar_status_t Cy_SAR_DeInit ( SAR_Type base,
bool  deInitRouting 
)

Reset SAR registers back to power on reset defaults.

The Cy_SAR_offset and Cy_SAR_countsPer10Volt arrays are NOT reset.

Parameters
basePointer to structure describing registers
deInitRoutingIf true, all SARMUX switches are opened and switch control registers are reset to zero. If false, switch registers are untouched.
Returns
Function Usage
/* Scenario: Switch to a different SAR configuration, for example, to scan different channels.
* Disable and reset all SAR settings and analog routing before re-initialization. */
bool deInitRouting = true;
(void) Cy_SAR_DeInit(SAR, deInitRouting);

◆ Cy_SAR_Enable()

void Cy_SAR_Enable ( SAR_Type base)

Power up the SAR ADC subsystem block.

The hardware is ready to use after 2 us, which is included in this function.

Parameters
basePointer to structure describing registers
Returns
None

◆ Cy_SAR_Disable()

__STATIC_INLINE void Cy_SAR_Disable ( SAR_Type base)

Turn off the hardware block.

Parameters
basePointer to structure describing registers
Returns
None

◆ Cy_SAR_StartConvert()

void Cy_SAR_StartConvert ( SAR_Type base,
cy_en_sar_start_convert_sel_t  startSelect 
)

Start a single scan (one shot) of all enabled channels or start scanning continuously.

When in continuous mode, all firmware and hardware triggers are ignored. To stop continuous scanning, call Cy_SAR_StopConvert.

Parameters
basePointer to structure describing registers
startSelectA value of the enum cy_en_sar_start_convert_sel_t
Returns
None
Function Usage
/* Scenario: Initiate scanning in continuous mode. */

◆ Cy_SAR_StopConvert()

void Cy_SAR_StopConvert ( SAR_Type base)

Stop continuous scanning of enabled channels.

If a conversion is currently executing, that conversion will complete, but no further conversions will occur until the next call to Cy_SAR_StartConvert or the next hardware trigger, if enabled.

Parameters
basePointer to structure describing registers
Returns
None
Function Usage
/* Scenario: Stop continous scanning so that scans can be firmware triggered (one-shot mode). */

◆ Cy_SAR_IsEndConversion()

cy_en_sar_status_t Cy_SAR_IsEndConversion ( SAR_Type base,
cy_en_sar_return_mode_t  retMode 
)

Immediately return the status of the conversion or does not return (blocking) until the conversion completes, depending on the retMode parameter.

In blocking mode, there is a time out of about 10 seconds for a CPU speed of 100 MHz.

Parameters
basePointer to structure describing registers
retModeA value of the enum cy_en_sar_return_mode_t
Returns
Side Effects
This function reads the end of conversion status and clears it after.
Function Usage
/* Scenario: SAR conversions are configured for one-shot firmware triggered mode.
* Before triggering the next scan, make sure the current conversion has completed.
* Implement a state machine to avoid blocking code. */
#define SCAN (0u)
#define PROCESS (1u)
uint8_t state = SCAN;
for(;;)
{
switch(state)
{
case SCAN:
/* Initiate a single (one shot) scan. */
state = PROCESS;
break;
case PROCESS:
break;
default:
/* Query end of conversion status. */
if (CY_SAR_SUCCESS == status)
{
/* Retrieve result and do something with it here. */
/* Start next scan. */
state = SCAN;
} /* else, conversion has not completed. */
break;
}
}

◆ Cy_SAR_GetResult16()

int16_t Cy_SAR_GetResult16 ( const SAR_Type base,
uint32_t  chan 
)

Return the data available in the channel result data register as a signed 16-bit integer.

Parameters
basePointer to structure describing registers
chanThe channel to read the result from, between 0 and CY_SAR_MAX_NUM_CHANNELS - 1
Returns
Data is returned as a signed 16-bit integer. If channel number is invalid, 0 is returned.
Function Usage
/* Scenario: ADC conversion has completed and result is valid.
* Retrieve the result on channel 0 and convert it to millivolts. */
uint32_t chan = 0UL;
int16_t result;
int16_t resultmV;
result = Cy_SAR_GetResult16(SAR, chan);
resultmV = Cy_SAR_CountsTo_mVolts(SAR, chan, result);

◆ Cy_SAR_GetResult32()

int32_t Cy_SAR_GetResult32 ( const SAR_Type base,
uint32_t  chan 
)

Return the data available in the channel result data register as a signed 32-bit integer.

Parameters
basePointer to structure describing registers
chanThe channel to read the result from, between 0 and CY_SAR_MAX_NUM_CHANNELS - 1
Returns
Data is returned as a signed 32-bit integer. If channel number is invalid, 0 is returned.
Function Usage
/* Scenario: ADC conversion has completed and result is valid.
* Retrieve the result on channel 0 as a signed 32-bit integer. */
uint32_t chan = 0UL;
int32_t result;
result = Cy_SAR_GetResult32(SAR, chan);

◆ Cy_SAR_GetChanResultUpdated()

__STATIC_INLINE uint32_t Cy_SAR_GetChanResultUpdated ( const SAR_Type base)

Return whether the RESULT register has been updated or not.

If the bit is high, the corresponding channel RESULT register was updated, i.e. was sampled during the previous scan and, in case of Interleaved averaging, reached the averaging count. If the bit is low, the corresponding channel is not enabled or the averaging count is not yet reached for Interleaved averaging.

Parameters
basePointer to structure describing registers
Returns
Each bit of the result corresponds to the channel. Bit 0 is for channel 0, etc.
Function Usage
/* Scenario: The averaging mode has been set to interleaved.
* In interleaved mode, it is not enough to rely on the EOS interrupt
* to determine if the result is valid. One must call Cy_SAR_GetChanResultUpdated() as well. */
uint32_t chan = 0UL;
uint32_t chanMask = (1u << chan);
uint32_t chanResultUpdated;
chanResultUpdated = Cy_SAR_GetChanResultUpdated(SAR);
if (chanMask == (chanResultUpdated & chanMask))
{
/* Averaging is complete and the result is valid. Retrieve the result and do something here. */
} /* else, result is not yet valid. */