mirror of
https://github.com/elua/elua.git
synced 2025-01-08 20:56:17 +08:00
System buffer-free version of ADC should now work. Doing so, saves about 1kb
of flash space. NOTE: getsamples function is disabled since this is dependant on having a buffer to get more than one sample. Side note: single sampling without the buffer is faster by about 5-10 us/channel. It should be possible to sidestep the buffer in certain situations for buffer enabled mode and get a similar benefit at the cost of some additional complexity.
This commit is contained in:
parent
551644e844
commit
709399a324
@ -11,7 +11,8 @@ typedef struct
|
||||
volatile u8 op_pending: 1, // Is there a pending conversion?
|
||||
blocking: 1, // Are we in blocking or non-blocking mode? (0 - blocking, 1 - nonblocking)
|
||||
freerunning: 1, // If true, we don't stop when we've acquired the requested number of samples
|
||||
smooth_ready: 1; // Has smoothing filter warmed up (i.e. smoothlen samples collected)
|
||||
smooth_ready: 1, // Has smoothing filter warmed up (i.e. smoothlen samples collected)
|
||||
value_fresh: 1; // Whether the value pointed to by value_ptr is fresh
|
||||
|
||||
unsigned id;
|
||||
|
||||
|
@ -69,8 +69,9 @@ int adc_setup_channel( unsigned id, u8 logcount )
|
||||
{
|
||||
elua_adc_ch_state *s = adc_get_ch_state( id );
|
||||
elua_adc_dev_state *d = adc_get_dev_state( 0 );
|
||||
#if defined( BUF_ENABLE_ADC )
|
||||
int res;
|
||||
|
||||
|
||||
platform_cpu_disable_interrupts();
|
||||
if( ( (u16) 1 << logcount ) != buf_get_size( BUF_ID_ADC, id ) )
|
||||
{
|
||||
@ -79,7 +80,8 @@ int adc_setup_channel( unsigned id, u8 logcount )
|
||||
return res;
|
||||
buf_flush( BUF_ID_ADC, id );
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
s->reqsamples = (u16) 1 << logcount;
|
||||
s->op_pending = 1;
|
||||
|
||||
@ -192,9 +194,13 @@ void adc_smooth_data( unsigned id )
|
||||
if ( s->smooth_ready == 1 )
|
||||
buf_read( BUF_ID_ADC, id, ( t_buf_data* )&sample );
|
||||
else
|
||||
{
|
||||
sample = *( s->value_ptr );
|
||||
s->value_fresh = 0;
|
||||
}
|
||||
#else
|
||||
sample = *( s->value_ptr );
|
||||
s->value_fresh = 0;
|
||||
#endif
|
||||
s->smoothbuf[ s->smoothidx ] = sample;
|
||||
|
||||
@ -237,9 +243,16 @@ u16 adc_get_processed_sample( unsigned id )
|
||||
else if ( s->logsmoothlen == 0 )
|
||||
{
|
||||
#if defined( BUF_ENABLE_ADC )
|
||||
buf_read( BUF_ID_ADC, id, ( t_buf_data* )&sample );
|
||||
if( adc_samples_available( id ) == 1 )
|
||||
{
|
||||
sample = *( s->value_ptr ); // Don't hit buffer if only one sample is available
|
||||
s->value_fresh = 0;
|
||||
}
|
||||
else
|
||||
buf_read( BUF_ID_ADC, id, ( t_buf_data* )&sample );
|
||||
#else
|
||||
sample = *( s->value_ptr );
|
||||
s->value_fresh = 0;
|
||||
#endif
|
||||
if ( s->reqsamples > 0)
|
||||
s->reqsamples -- ;
|
||||
@ -272,10 +285,13 @@ u16 adc_samples_requested( unsigned id )
|
||||
// Return count of available samples in the buffer
|
||||
u16 adc_samples_available( unsigned id )
|
||||
{
|
||||
elua_adc_ch_state *s = adc_get_ch_state( id );
|
||||
|
||||
#if defined( BUF_ENABLE_ADC )
|
||||
return ( u16 ) buf_get_count( BUF_ID_ADC, id );
|
||||
u16 buffer_count = ( u16 )buf_get_count( BUF_ID_ADC, id );
|
||||
return ( ( buffer_count == 0 ) ? s->value_fresh : buffer_count );
|
||||
#else
|
||||
return 1; // FIXME: should keep track of whether single sample is fresh
|
||||
return s->value_fresh;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -159,7 +159,7 @@ static int adc_getsample( lua_State* L )
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#if defined( BUF_ENABLE_ADC )
|
||||
// Lua: table_of_vals = getsamples( id, [count] )
|
||||
static int adc_getsamples( lua_State* L )
|
||||
{
|
||||
@ -193,7 +193,7 @@ static int adc_getsamples( lua_State* L )
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// Module function map
|
||||
#define MIN_OPT_LEVEL 2
|
||||
@ -207,8 +207,10 @@ const LUA_REG_TYPE adc_map[] =
|
||||
{ LSTRKEY( "setblocking" ), LFUNCVAL( adc_setblocking ) },
|
||||
{ LSTRKEY( "setsmoothing" ), LFUNCVAL( adc_setsmoothing ) },
|
||||
{ LSTRKEY( "getsample" ), LFUNCVAL( adc_getsample ) },
|
||||
#if defined( BUF_ENABLE_ADC )
|
||||
{ LSTRKEY( "getsamples" ), LFUNCVAL( adc_getsamples ) },
|
||||
// { LSTRKEY( "putsamples" ), LFUNCVAL( adc_putsamples ) },
|
||||
#endif
|
||||
{ LNILKEY, LNILVAL }
|
||||
};
|
||||
|
||||
|
@ -554,13 +554,17 @@ void ADCIntHandler( void )
|
||||
{
|
||||
s = d->ch_state[ d->seq_ctr ];
|
||||
d->sample_buf[ d->seq_ctr ] = ( u16 )tmpbuff[ d->seq_ctr ];
|
||||
s->value_fresh = 1; // Mark sample as fresh
|
||||
|
||||
// Fill in smoothing buffer until warmed up
|
||||
if ( s->logsmoothlen > 0 && s->smooth_ready == 0)
|
||||
adc_smooth_data( s->id );
|
||||
#if defined( BUF_ENABLE_ADC )
|
||||
else
|
||||
{
|
||||
buf_write( BUF_ID_ADC, s->id, ( t_buf_data* )s->value_ptr );
|
||||
s->value_fresh = 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
// If we have the number of requested samples, stop sampling
|
||||
|
@ -648,13 +648,17 @@ void DMA1_Channel1_IRQHandler(void)
|
||||
while( d->seq_ctr < d->seq_len )
|
||||
{
|
||||
s = d->ch_state[ d->seq_ctr ];
|
||||
s->value_fresh = 1;
|
||||
|
||||
// Fill in smoothing buffer until warmed up
|
||||
if ( s->logsmoothlen > 0 && s->smooth_ready == 0)
|
||||
adc_smooth_data( s->id );
|
||||
#if defined( BUF_ENABLE_ADC )
|
||||
else
|
||||
{
|
||||
buf_write( BUF_ID_ADC, s->id, ( t_buf_data* )s->value_ptr );
|
||||
s->value_fresh = 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
// If we have the number of requested samples, stop sampling
|
||||
|
Loading…
x
Reference in New Issue
Block a user