Skip to content

Commit

Permalink
final changes for ADC gain stage enable
Browse files Browse the repository at this point in the history
  • Loading branch information
boramonideep committed May 2, 2023
1 parent efaeb6d commit e73be6f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
23 changes: 18 additions & 5 deletions cores/wiring_analog.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,19 @@ if( channel < NUM_ANALOG_INPUTS )
return value;
}

uint32_t analogRead_special( uint8_t channel )
/* 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
*/

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

value = 0xFFFFFFFF;
Expand All @@ -243,8 +254,8 @@ if( channel < NUM_ANALOG_INPUTS )
/* 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 );

XMC_VADC_GLOBAL_SHS_SetGainFactor(SHS0,3,XMC_VADC_GROUP_INDEX_0,(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 );
Expand All @@ -254,16 +265,18 @@ if( channel < NUM_ANALOG_INPUTS )
if( !(adc->enabled) )
/* Add a channel to the background source. */
VADC->BRSSEL[ ADC_CONVERSION_GROUP ] = (uint32_t)( 1U << adc->channel_num );
XMC_VADC_GLOBAL_SHS_SetGainFactor(SHS0,1,XMC_VADC_GROUP_INDEX_0,(uint32_t)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
}


Expand Down
16 changes: 15 additions & 1 deletion cores/wiring_analog.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,21 @@ extern "C" {
*/
extern uint32_t analogRead( uint8_t channel ) ;

extern uint32_t analogRead_special( 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.
Expand Down
2 changes: 1 addition & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ getAnalogReadBits KEYWORD2
getAnalogWriteBits KEYWORD2
getAnalogReadMaximum KEYWORD2
getAnalogWriteMaximum KEYWORD2
analogRead_special KEYWORD2
analogRead_variableGain KEYWORD2

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

0 comments on commit e73be6f

Please sign in to comment.