AFE_NXP_Arduino 2.0.3
Analog Front-End (AFE) device operation sample code for Arduino
Loading...
Searching...
No Matches
NAFE33352.cpp
Go to the documentation of this file.
1
9#include "NAFE33352.h"
10#include <Arduino.h>
11#include <math.h>
12
16
21
23{
24 const uint16_t tmp_ch_config[ 4 ] = { cc[ 0 ], cc[ 1 ], cc[ 2 ], 0 };
25 afe_ptr->open_logical_channel( ch_number, tmp_ch_config );
26}
27
29{
30 const uint16_t tmp_ch_config[ 4 ] = { cc0, cc1, cc2, 0 };
31 afe_ptr->open_logical_channel( ch_number, tmp_ch_config );
32}
33
37
41
43{
44 const uint16_t tmp_ch_config[ 6 ] = { cc0, cc1, cc2, cc3, cc4, cc5 };
45 afe_ptr->open_dac_output( tmp_ch_config );
46}
47
49{
50 afe_ptr->open_dac_output( cc );
51}
52
54{
55 uint16_t default_dac_setting[ 6 ] = { 0x0000, 0x1000, 0x87FF, 0x8200, 0xE7FF, 0x0C00 };
56
57 output_mode = mode;
58
59 switch ( output_mode )
60 {
62 full_scale = 12.50;
63 default_dac_setting[ 0 ] |= 0x0002;
64 break;
66 full_scale = 12.50;
67 default_dac_setting[ 0 ] |= 0x6040;
68 break;
70 full_scale = 0.025 / 0.95;
71 default_dac_setting[ 0 ] |= 0x6061;
72 default_dac_setting[ 1 ] |= 0x1000;
73 break;
75 full_scale = 0.025;
76 default_dac_setting[ 0 ] |= 0x6061;
77 default_dac_setting[ 1 ] |= 0x1000;
78 break;
80 full_scale = 0.025;
81 default_dac_setting[ 0 ] |= 0x6000;
82 break;
84 full_scale = 0.025;
85 default_dac_setting[ 0 ] |= 0x7000;
86 break;
87 }
88
89 if ( 0.0 < full_scale_range ) // overwrite fullscale range if it is specified
90 full_scale = full_scale_range;
91
92 configure( default_dac_setting );
93}
94
99
101{
102 afe_ptr->dac_out( value, full_scale, 18 );
103}
104
106{
107 output( value );
108 return *this;
109}
110
111
112
113/* NAFE33352_Base class ******************************************/
114
117{
118 for ( auto i = 0; i < 16; i++ )
119 {
120 logical_channel[ i ].afe_ptr = this;
122 }
123
124 dac.afe_ptr = this;
125}
126
130
146
147void NAFE33352_Base::reset( bool hardware_reset )
148{
149 if ( hardware_reset )
150 Serial.println( "warning: UIOM doesn't have hardware RESET pin on the board. This reset will be ignored\r\n" );
151
153
154 constexpr uint16_t CHIP_READY = 1 << 13;
155 constexpr auto RETRY = 10;
156
157 for ( auto i = 0; i < RETRY; i++ )
158 {
159 delay( 1 );
161 return;
162 }
163
164 Serial.println( "NAFE33352 couldn't get ready. Check power supply or pin conections\r\n" );
165
166 while ( true )
167 ;
168
169}
170
171void NAFE33352_Base::open_dac_output( const uint16_t (&cc)[ 6 ] )
172{
173 for ( auto i = 0; i < 6; i++ )
175}
176
177
178void NAFE33352_Base::open_logical_channel( int ch, const uint16_t (&cc)[ 4 ] )
179{
180 static bool pga_enabled = false;
181 constexpr double pow2_24 = (double)(1L << 24);
182 double coeff = 0.00;
183 bool pga_on = false;
184
185 mux_setting[ ch ] = (cc[ 0 ] >> 3) & 0x1F;
186
188
189 switch ( mux_setting[ ch ] )
190 {
191 case VCM_VCM:
192 case GPIO0_GPIO1:
193 coeff = (20.00 * 2.50) / (12.5 * pow2_24);
194 break;
195 case AIP_AIN:
196 coeff = (20.00 * 2.50) / ((cc[ 0 ] & 0x0100 ? 16.00 : 1.00) * pow2_24);
197 pga_on = true;
198 break;
199 case AIP_VSNS:
200 case VSNS:
201 coeff = (20.00 * 2.50) / pow2_24;
202 pga_on = true;
203 break;
204 case AIP:
205 case AIN:
206 coeff = (20.00 * 2.50) / ((cc[ 0 ] & 0x0100 ? 16.00 : 1.00) * pow2_24);
207 pga_on = true;
208 break;
209 case ISNS:
210 coeff = (20.00 * 2.50) / (3.7989 * pow2_24);
211 break;
212 case TIA:
213 coeff = (20.00 * 2.50) / (2.50 * pow2_24);
214 break;
215 case GPIO0_VCM:
216 case VCM_GPIO1:
217 case REF_BYP__VCM:
218 case LDO:
219 case DAC_REF:
220 coeff = (20.00 * 2.50) / (12.5 * pow2_24) + 1.50;
221 break;
222 case VCM__REF_BYP:
223 coeff = (20.00 * 2.50) / (12.5 * pow2_24) - 1.50;
224 break;
225 case BG:
226 //coeff = (20.00 * 2.50) / (12.5 * pow2_24) - 1.50;
227 break;
228 case VADD:
229 coeff = (2.00 * 20.00 * 2.50) / (12.5 * pow2_24) - 1.50;
230 break;
231 case VHDD:
232 case VHSS:
233 coeff = (40.00 * 20.00 * 2.50) / (12.5 * pow2_24);
234 break;
235 }
236
237 coeff_V[ ch ] = coeff;
238
239 if ( pga_on && !pga_enabled )
240 {
242 pga_enabled = true;
243 }
244
245 for ( auto i = 0; i < 3; i++ )
247
249
250 ch_delay[ ch ] = calc_delay( ch );
251
252#if 0
253 Serial.print("lc[ ");
254 Serial.print(ch);
255 Serial.print("] : ");
256 Serial.println( ch_delay[ ch ], 10 );
257#endif
258}
259
261{
262 constexpr auto bit_length = 16;
264 total_delay = 0.00;
265
266 memset( sequence_order, 0, 16 );
267
268 for ( auto i = 0; i < bit_length; i++ )
269 {
270 if ( value & (0x1 << i) )
271 {
274 total_delay += ch_delay[ i ];
275 }
276 }
277
278#if 0
279 for ( auto i = 0; i < bit_length; i++ )
280 printf( " %x", sequence_order[ i ] );
281 printf( "\r\n" );
282#endif
283}
284
286{
287 constexpr double system_clock = 4608000.00;
288
289 constexpr static double data_rates[] = { 288000, 192000, 144000, 96000, 72000, 48000, 36000, 24000,
290 18000, 12000, 9000, 6000, 4500, 3000, 2250, 1125,
291 562.5, 400, 300, 200, 100, 60, 50, 30,
292 25, 20, 15, 10, 7.5, };
293 constexpr static uint16_t delays[] = { 0, 2, 4, 6, 8, 10, 12, 14,
294 16, 18, 20, 28, 38, 40, 42, 56,
295 64, 76, 90, 128, 154, 178, 204, 224,
296 256, 358, 512, 716,
297 1024, 1664, 3276, 7680, 19200, 23040, };
298
300
303
304 uint8_t adc_data_rate = (ch_config1 >> 3) & 0x001F;
305 uint8_t adc_sinc = (ch_config1 >> 0) & 0x0007;
306 uint8_t ch_delay = (ch_config2 >> 10) & 0x003F;
307 bool adc_normal_setting = (ch_config2 >> 9) & 0x0001;
308 bool ch_chop = (ch_config2 >> 7) & 0x0001;
311
312 if ( highspeed_variant )
313 {
314 base_freq *= 2.00;
315 delay_setting /= 2.00;
316 }
317
318 if ( (28 < adc_data_rate) || (4 < adc_sinc) || ((adc_data_rate < 12) && (adc_sinc)) )
319 return 0.00;
320
321 if ( !adc_normal_setting )
322 base_freq /= (adc_sinc + 1);
323
324 if ( ch_chop )
325 base_freq /= 2;
326
327#if 0
328 Serial.print( "adc_data_rate =" );
329 Serial.println( adc_data_rate );
330 Serial.print( "base_freq = " );
331 Serial.println( base_freq );
332 Serial.print( "delay_setting = " );
333 Serial.println( delay_setting, 10 );
334 Serial.print( "channel delay = " );
335 Serial.println( (1 / base_freq) + delay_setting, 10 );
336#endif
337
338 return (1 / base_freq) + delay_setting;
339}
340
341#pragma GCC diagnostic ignored "-Wunused-parameter"
342void NAFE33352_Base::open_logical_channel( int ch, uint16_t cc0, uint16_t cc1, uint16_t cc2, uint16_t dummy )
343{
344 const ch_setting_t tmp_ch_config = { cc0, cc1, cc2 };
346}
347#pragma GCC diagnostic pop
348
356
364
370
376
381
386
388{
389 bit_op( NAFE33352_Base::Register16::AI_SYSCFG, ~0x0100, flag ? 0x0100 : 0x0000 );
390}
391
392int32_t NAFE33352_Base::read( int ch )
393{
395}
396
398{
399 burst( (uint32_t *)data, enabled_channels );
400}
401
403{
404 raw_t raw_data[ 16 ];
405
406 read( raw_data );
407
408 for ( auto i = 0; i < enabled_channels; i++ )
409 data[ i ] = raw2uv( sequence_order[ i ], raw_data[ i ] );
410}
411
412void NAFE33352_Base::dac_out( double vi, double full_scale, uint8_t bit_length )
413{
415}
416
417int32_t NAFE33352_Base::dac_code( double a, double full_scale, uint8_t bit_length )
418{
419 int32_t fsv = (1L << (bit_length - 1));
420
421 int32_t v = (int32_t)((double)fsv * -a / full_scale);
422
423 v = v < -fsv ? -fsv : v;
424 v = v > (fsv - 1) ? (fsv - 1) : v;
425
426 return v << (24 - bit_length);
427}
428
429void NAFE33352_Base::command( uint16_t com )
430{
431 write_r16( com );
432}
433
434void NAFE33352_Base::reg( Register16 r, uint16_t value )
435{
436 write_r16( static_cast<uint16_t>( r ), value );
437}
438
439void NAFE33352_Base::reg( Register24 r, uint32_t value )
440{
441 write_r24( static_cast<uint16_t>( r ), value );
442}
443
445{
446 return read_r16( static_cast<uint16_t>( r ) );
447}
448
450{
451 return read_r24( static_cast<uint16_t>( r ) );
452}
453
455{
456 return (static_cast<uint64_t>( reg( NAFE33352_Base::Register16::PN2 ) ) << (16 + 8)) | static_cast<uint64_t>( reg( NAFE33352_Base::Register16::PN1 ) ) << 8 | reg( NAFE33352_Base::Register16::PN0_REV ) >> 8;
457}
458
460{
462}
463
472
474{
476}
477
478/* NAFE33352 class ******************************************/
479
480NAFE33352::NAFE33352( bool spi_addr, bool hsv, int nINT, int DRDY, int SYN, int nRESET, int DRDY_input, int SYNCDAC )
481 : NAFE33352_Base( spi_addr, hsv, nINT, DRDY, SYN, nRESET, DRDY_input, SYNCDAC )
482{
483}
484
488
489/* NAFE33352 class ******************************************/
490
491NAFE33352_UIOM::NAFE33352_UIOM( bool spi_addr, bool hsv, int nINT, int DRDY, int SYN, int nRESET, int DRDY_input, int SYNCDAC )
492 : NAFE33352_Base( spi_addr, hsv, nINT, DRDY, SYN, nRESET, DRDY_input, SYNCDAC )
493{
494}
495
int32_t raw_t
Definition AFE_NXP.h:22
double volt_t
Definition AFE_NXP.h:23
bool highspeed_variant
Definition AFE_NXP.h:216
double raw2uv(int ch, raw_t value)
Definition AFE_NXP.h:164
double coeff_V[16]
Definition AFE_NXP.h:234
int enabled_channels
Definition AFE_NXP.h:228
uint8_t sequence_order[16]
Definition AFE_NXP.h:231
double ch_delay[16]
Definition AFE_NXP.h:240
int mux_setting[16]
Definition AFE_NXP.h:237
double total_delay
Definition AFE_NXP.h:241
AFE_base * afe_ptr
Definition AFE_NXP.h:285
NAFE33352_Base * afe_ptr
Definition NAFE33352.h:137
DAC & operator=(double value)
void output(double value)
void configure(const uint16_t(&cc)[6])
Definition NAFE33352.cpp:48
void configure(const uint16_t(&cc)[3])
Definition NAFE33352.cpp:22
uint32_t bit_op(T rg, uint32_t mask, uint32_t value)
Definition NAFE33352.h:435
uint64_t part_number(void)
uint8_t revision_number(void)
virtual raw_t read(int ch)
void enable_logical_channel(int ch)
virtual void boot(void)
virtual void open_logical_channel(int ch, uint16_t cc0, uint16_t cc1, uint16_t cc2, uint16_t dummy)
virtual void close_logical_channel(void)
void open_dac_output(const uint16_t(&cc)[6])
virtual void reset(bool hardware_reset=false)
virtual void command(uint16_t com)
virtual void start(void)
virtual ~NAFE33352_Base()
void channel_info_update(uint16_t value)
uint64_t serial_number(void)
int32_t dac_code(double a, double full_scale, uint8_t bit_length)
NAFE33352_Base(bool spi_addr, bool highspeed_variant, int nINT, int DRDY, int SYN, int nRESET, int DRDY_input, int SYNCDAC)
double calc_delay(int ch)
virtual void start_continuous_conversion()
uint16_t[4] ch_setting_t
Definition NAFE33352.h:19
virtual void dac_out(double vi, double full_scale, uint8_t bit_length)
virtual void DRDY_by_sequencer_done(bool flag=true)
float temperature(void)
LogicalChannel logical_channel[16]
Definition NAFE33352.h:74
virtual void reg(Register16 r, uint16_t value)
virtual ~NAFE33352_UIOM()
NAFE33352_UIOM(bool spi_addr=0, bool highspeed_variant=false, int nINT=7, int DRDY=4, int SYN=14, int nRESET=14, int DRDY_input=2, int SYNCDAC=14)
NAFE33352(bool spi_addr=0, bool highspeed_variant=false, int nINT=7, int DRDY=4, int SYN=14, int nRESET=14, int DRDY_input=2, int SYNCDAC=14)
virtual ~NAFE33352()
int32_t read_r24(uint16_t reg)
void write_r24(uint16_t reg, uint32_t val)
uint16_t read_r16(uint16_t reg)
void burst(uint32_t *data, int length, int width=3)
void write_r16(uint16_t reg)