Skip to content

Commit

Permalink
Merge pull request #240 from boramonideep/adc_diff_gain
Browse files Browse the repository at this point in the history
Variable gain ADC feature
  • Loading branch information
boramonideep committed May 5, 2023
2 parents a514687 + 448e285 commit f58509d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
64 changes: 64 additions & 0 deletions cores/wiring_analog.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,70 @@ if( channel < NUM_ANALOG_INPUTS )
return value;
}

/* analogRead_variableGain takes parameter of ADC channel number and gain value
return 0xFFFFFFFF for invalid channel
gain value gain factor
0 1
1 3
2 6
3 12
Also, refer to macros in wiring_analog.h
*/

uint32_t analogRead_variableGain( uint8_t channel, uint8_t gain_value )
{
#if(XMC_VADC_SHS_AVAILABLE == 1U)
uint32_t value;

value = 0xFFFFFFFF;
if( channel < NUM_ANALOG_INPUTS )
{
XMC_ADC_t *adc = &mapping_adc[ channel ];

#if(XMC_VADC_GROUP_AVAILABLE == 1U)
// ADC grouping
if( !(adc->enabled) )
{
XMC_VADC_CHANNEL_CONFIG_t vadc_gobal_channel_config;
memset( &vadc_gobal_channel_config, 0, sizeof( XMC_VADC_CHANNEL_CONFIG_t ) );
vadc_gobal_channel_config.input_class = XMC_VADC_CHANNEL_CONV_GROUP_CLASS1;
vadc_gobal_channel_config.result_reg_number = adc->result_reg_num;
vadc_gobal_channel_config.alias_channel = XMC_VADC_CHANNEL_ALIAS_DISABLED;

XMC_VADC_RESULT_CONFIG_t vadc_gobal_result_config = { .g_rcr = 0 };
/* Configure a channel belonging to the aforesaid conversion kernel */
XMC_VADC_GROUP_ChannelInit( adc->group, adc->channel_num, &vadc_gobal_channel_config );
/* Configure a result resource belonging to the aforesaid conversion kernel */
XMC_VADC_GROUP_ResultInit( adc->group, adc->result_reg_num, &vadc_gobal_result_config );
/* Add channel into the Background Request Source Channel Select Register */
XMC_VADC_GLOBAL_BackgroundAddChannelToSequence( VADC, (uint32_t)adc->group_num,
(uint32_t)adc->channel_num );
/* Set the gain factor of the Sample and hold module*/
XMC_VADC_GLOBAL_SHS_SetGainFactor( SHS0, gain_value, (uint32_t)adc->group_num, (uint32_t)adc->channel_num );
}
/* Start conversion manually using load event trigger*/
XMC_VADC_GLOBAL_BackgroundTriggerConversion( VADC );
value = XMC_VADC_GROUP_GetResult( adc->group, adc->result_reg_num );
#else
// XMC1100 no ADC grouping
if( !(adc->enabled) )
/* Add a channel to the background source. */
VADC->BRSSEL[ ADC_CONVERSION_GROUP ] = (uint32_t)( 1U << adc->channel_num );
/* Set the gain factor of the Sample and hold module */
XMC_VADC_GLOBAL_SHS_SetGainFactor( SHS0, gain_value, XMC_VADC_GROUP_INDEX_0, (uint32_t)adc->channel_num );
// Generates conversion request
XMC_VADC_GLOBAL_BackgroundTriggerConversion( VADC );

// Wait until conversion is ready
while( ( ( value = XMC_VADC_GLOBAL_GetDetailedResult( VADC ) ) & VADC_GLOBRES_VF_Msk) == 0u );
#endif
value = ( ( value & VADC_GLOBRES_RESULT_Msk) >> ( ADC_MAX_READ_RESOLUTION - _readResolution ) );
return value;
}
#endif
}


/* Helper function for analogWrite and setAnalogWriteFrequency to scan
mapping tables to determine for a given pin which PWM4, PWM8 or DAC
Expand Down
25 changes: 25 additions & 0 deletions cores/wiring_analog.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@
extern "C" {
#endif

/*********************************************************************************************************************
* MACROS
********************************************************************************************************************/
// ADC gain macros
#define ADC_VAR_GAIN_FACTOR_1 0
#define ADC_VAR_GAIN_FACTOR_3 1
#define ADC_VAR_GAIN_FACTOR_6 2
#define ADC_VAR_GAIN_FACTOR_12 3

//****************************************************************************
// @External Prototypes
//****************************************************************************
Expand Down Expand Up @@ -85,6 +94,22 @@ extern "C" {
*/
extern uint32_t analogRead( uint8_t channel ) ;

/*
* \brief Reads the value from the specified analogue channel and add variable gain at input.
*
* \param channel
* \param gain_factor
*
* gain value gain factor
* 0 1
* 1 3
* 2 6
* 3 12
*
* \return Read value from selected channel, or 0xFFFFFFFF for error.
*/
extern uint32_t analogRead_variableGain( uint8_t channel, uint8_t gain_value ) ;

/*
* \brief Set the resolution of analogRead return values in number of bits.
* \note Default is 10 bits (range from 0 to 1023).
Expand Down
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ getAnalogReadBits KEYWORD2
getAnalogWriteBits KEYWORD2
getAnalogReadMaximum KEYWORD2
getAnalogWriteMaximum KEYWORD2
analogRead_variableGain KEYWORD2

#######################################
# Instances (KEYWORD2)
Expand Down

0 comments on commit f58509d

Please sign in to comment.