CAT2 Peripheral Driver Library
Initialization and Basic Functions

This set of functions is for initialization and basic usage. More...

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...
 
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...
 
bool Cy_SAR_GetBusyStatus (const SAR_Type *base)
 Get SAR conversion status. 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...
 
__STATIC_INLINE void Cy_SAR_EnableInjection (SAR_Type *base, bool tailgating)
 Triggers the injection channel sample. More...
 

Detailed Description

This set of functions is for initialization and basic usage.

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.

Note
If interleaved averaging mode is used, the Differential Result Format should be the same as the Single-Ended Result Format. Otherwise, this function will return CY_SAR_BAD_PARAM.
Parameters
basePointer to structure describing SAR instance registers
configPointer to structure containing configuration data. See cy_stc_sar_config_t and guidance in the Initialization and Enable section.
Returns
  • CY_SAR_SUCCESS : initialization complete successfully
  • CY_SAR_BAD_PARAM : input pointers are null or some configuration setting is invalid, initialization incomplete.
Function Usage
cy_en_sar_status_t sarStatus = Cy_SAR_Init(SAR0, &config);
if (CY_SAR_SUCCESS != sarStatus)
{
/* insert error handling here */
}

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

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. */
(void) Cy_SAR_DeInit(SAR0, true);

◆ 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()

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 continuous 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 48 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.
Note
CY_SAR_WAIT_FOR_RESULT and CY_SAR_WAIT_FOR_RESULT_INJ return modes are not recommended, especially in RTOS applications, as the timeout is generated by the CPU waiting for a few seconds. It is recommended to handle the timeout in the user firmware.
Function Usage
/* Scenario: The polling method can be used instead of interrupts */
adcResult[0] = Cy_SAR_GetResult16(SAR0, 0);
adcResult[1] = Cy_SAR_GetResult16(SAR0, 1);
adcResult[2] = Cy_SAR_GetResult16(SAR0, 2);

◆ Cy_SAR_GetBusyStatus()

bool Cy_SAR_GetBusyStatus ( const SAR_Type base)

Get SAR conversion status.

Parameters
basePointer to structure describing registers
Returns
TRUE : SAR conversion is busy. FALSE: SAR conversion is not busy.

◆ 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_INJ_CHANNEL
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(SAR0, chan);
resultmV = Cy_SAR_CountsTo_mVolts(SAR0, 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_INJ_CHANNEL
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(SAR0, 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.

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(SAR0);
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. */

◆ Cy_SAR_EnableInjection()

__STATIC_INLINE void Cy_SAR_EnableInjection ( SAR_Type base,
bool  tailgating 
)

Triggers the injection channel sample.

Parameters
basePointer to structure describing registers
tailgatingInjection channel tailgating enable:
  • true: The addressed pin is sampled after the next trigger and after all enabled channels have been scanned.
  • false: SAR is immediately triggered when the SAR is not busy. If the SAR is busy, the INJ channel addressed pin is sampled at the end of the current scan.
Function Usage
/* Scenario: execute the injection channel at the tail of the nearest ADC scanning */