OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [io/] [adc/] [current/] [doc/] [adc.sgml] - Blame information for rev 825

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
2
 
3
4
 
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 
33
34
 
35
ADC Support
36
 
37
38
  
39
    Overview
40
  
41
  
42
    Overview
43
    eCos Support for Analog/Digital Converters
44
  
45
 
46
  Introduction
47
 
48
49
ADC support in eCos is based around the standard character device
50
interface. Hence all device IO function, or file IO functions may be
51
used to access ADC devices.
52
53
 
54
55
ADC devices are presented as read-only serial channels that generate
56
samples at a given rate. The size of each sample is hardware specific
57
and is defined by the cyg_adc_sample_t type. The sample
58
rate may be set at runtime by the application. Most ADC devices
59
support several channels which are all sampled at the same
60
rate. Therefore setting the rate for one channel will usually change
61
the rate for all channels on that device.
62
63
 
64
 
65
66
 
67
Examples
68
 
69
70
The use of the ADC devices is best shown by example.  The following is
71
a simple example of using the eCos device interface to access the ADC:
72
73
 
74
75
 
76
        int res;
77
        cyg_io_handle_t handle;
78
 
79
        // Get a handle for ADC device 0 channel 0
80
        res = cyg_io_lookup( "/dev/adc00", &handle );
81
 
82
        if( res != ENOERR )
83
            handle_error(err);
84
 
85
        for(;;)
86
        {
87
            cyg_adc_sample_t sample;
88
            cyg_uint32 len = sizeof(sample);
89
 
90
            // read a sample from the channel
91
            res = cyg_io_read( handle, &sample, &len );
92
 
93
            if( res != ENOERR )
94
                handle_error(err);
95
 
96
            use_sample( sample );
97
        }
98
 
99
100
 
101
102
In this example, the required channel is looked up and a handle on it
103
acquired. Conventionally ADC devices are named "/dev/adcXY" where X is
104
the device number and Y the channel within that device. Following
105
this, samples are read from the device sequentially.
106
107
 
108
109
ADC devices may also be accessed using FILEIO operations. These allow
110
more sophisticated usage. The following example shows
111
select() being used to gather samples from several devices.
112
113
 
114
 
115
116
 
117
        int fd1, fd2;
118
 
119
        // open channels, non-blocking
120
        fd1 = open( "/dev/adc01", O_RDONLY|O_NONBLOCK );
121
        fd2 = open( "/dev/adc02", O_RDONLY|O_NONBLOCK );
122
 
123
        if( fd1 < 0 || fd2 < 0 )
124
            handle_error( errno );
125
 
126
        for(;;)
127
        {
128
            fd_set rd;
129
            int maxfd = 0;
130
            int err;
131
            cyg_adc_sample_t samples[128];
132
            int len;
133
 
134
            FD_ZERO( &rd );
135
 
136
            FD_SET( fd1, &rd );
137
            FD_SET( fd2, &rd );
138
            maxfd = max(fd1,fd2);
139
 
140
            // select on available data on each channel.
141
            err = select( maxfd+1, &rd, NULL, NULL, NULL );
142
 
143
            if( err < 0 )
144
                handle_error(errno);
145
 
146
            // If channel 1 has data, handle it
147
            if( FD_ISSET( fd1, &rd ) )
148
            {
149
                len = read( fd1, &samples, sizeof(samples) );
150
 
151
                if( len > 0 )
152
                    handle_samples_chan1( &samples, len/sizeof(sample[0]) );
153
            }
154
 
155
            // If channel 2 has data, handle it
156
            if( FD_ISSET( fd2, &rd ) )
157
            {
158
                len = read( fd2, &samples, sizeof(samples) );
159
 
160
                if( len > 0 )
161
                    handle_samples_chan2( &samples, len/sizeof(sample[0]) );
162
            }
163
 
164
        }
165
 
166
167
 
168
169
This test uses FILEIO operations to access ADC channels. It starts by
170
opening two channels for reading only and with blocking disabled. It
171
then falls into a loop using select to wake up whenever either channel
172
has samples available.
173
174
 
175
176
 
177
 
178
Details
179
 
180
181
As indicated, the main interface to ADC devices is via the standard
182
character device interface. However, there are a number of aspects
183
that are ADC specific.
184
185
 
186
Sample Type
187
 
188
189
Samples can vary in size depending on the underlying hardware and is
190
often a non-standard number of bits. The actual number of bits is
191
defined by the hardware driver package, and the generic ADC package
192
uses this to define a type cyg_adc_sample_t which can
193
contain at least the required number of bits. All reads from an ADC
194
channel should be expressed in multiples of this type, and actual
195
bytes read will also always be a multiple.
196
197
 
198
199
 
200
Sample Rate
201
 
202
203
The sample rate of an ADC device can be varied by calling a
204
set_config function, either at the device IO API
205
level or at the FILEIO level. The following two functions show how
206
this is done at each:
207
208
209
 
210
int set_rate_io( cyg_io_handle_t handle, int rate )
211
{
212
    cyg_adc_info_t info;
213
    cyg_uint32 len = sizeof(info);
214
 
215
    info.rate = rate;
216
 
217
    return cyg_io_set_config( handle,
218
                              CYG_IO_SET_CONFIG_ADC_RATE,
219
                              &info,
220
                              &len);
221
}
222
 
223
int set_rate_fileio( int fd, int rate )
224
{
225
    cyg_adc_info_t info;
226
 
227
    info.rate = rate;
228
 
229
    return cyg_fs_fsetinfo( fd,
230
                            CYG_IO_SET_CONFIG_ADC_RATE,
231
                            &info,
232
                            sizeof(info) );
233
}
234
235
 
236
237
 
238
Enabling a Channel
239
 
240
241
Channels are initialized in a disabled state and generate no
242
samples. When a channel is first looked up or opened, then it is
243
automatically enabled and samples start to accumulate. A channel may
244
then be disable or re-enabled via a set_config
245
function:
246
247
 
248
249
int disable_io( cyg_io_handle_t handle )
250
{
251
    return cyg_io_set_config( handle,
252
                              CYG_IO_SET_CONFIG_ADC_DISABLE,
253
                              NULL,
254
                              NULL);
255
}
256
 
257
int enable_io( cyg_io_handle_t handle )
258
{
259
    return cyg_io_set_config( handle,
260
                              CYG_IO_SET_CONFIG_ADC_ENABLE,
261
                              NULL,
262
                              NULL);
263
}
264
265
 
266
267
 
268
Flushing a Channel
269
 
270
271
In some particular cases, user might require to flush the ADC data buffer. Flushing
272
may be perfomed via a set_config
273
function:
274
275
 
276
277
int flush_io( cyg_io_handle_t handle )
278
{
279
    return cyg_io_set_config( handle,
280
                              CYG_IO_SET_CONFIG_ADC_DATA_FLUSH,
281
                              NULL,
282
                              NULL);
283
}
284
285
 
286
287
 
288
289
 
290
Configuration
291
 
292
293
The ADC package defines a number of generic configuration options that
294
apply to all ADC implementations:
295
296
 
297
298
 
299
cdl_component CYGPKG_IO_ADC_DEVICES
300
301
This option enables the hardware device drivers for the current
302
platform. ADC devices will only be enabled if this option is itself
303
enabled.
304
305
306
 
307
cdl_option CYGNUM_IO_ADC_SAMPLE_SIZE
308
309
This option defines the sample size for the ADC devices.  Given in
310
bits, it will be rounded up to 8, 16 or 32 to define the
311
cyg_adc_sample_t type. This option is usually set by the
312
hardware device driver.
313
314
315
 
316
cdl_option CYGPKG_IO_ADC_SELECT_SUPPORT
317
318
 
319
This option enables support for the select() API
320
function on all ADC devices. This option can be disabled if the
321
select() is not used, saving some code and data
322
space.
323
324
325
 
326
327
 
328
329
In addition to the generic options, each hardware device driver
330
defines some parameters for each device and channel. The exact names
331
of the following option depends on the hardware device driver, but
332
options of this form should be available in all drivers.
333
334
 
335
 
336
337
 
338
cdl_option CYGDAT_IO_ADC_EXAMPLE_CHANNELN_NAME
339
340
This option specifies the name of the device for an ADC
341
channel. Channel names should be of the form "/dev/adcXY" where X is
342
the device number and Y the channel within that device.
343
344
345
 
346
cdl_option CYGNUM_IO_ADC_EXAMPLE_CHANNELN_BUFSIZE
347
348
This option specifies the buffer size for an ADC channel. The value is
349
expressed in multiples of cyg_adc_sample_t rather than
350
bytes. The default value is 128.
351
352
353
 
354
cdl_option CYGNUM_IO_ADC_EXAMPLE_DEFAULT_RATE
355
356
This option defines the initial default sample rate for all
357
channels. The hardware driver may place constraints on the range of
358
values this option may take.
359
360
361
 
362
363
 
364
365
 
366
367
 
368
369
  
370
    ADC Device Drivers
371
  
372
  
373
    Overview
374
    ADC Device Drivers
375
  
376
 
377
Introduction
378
 
379
380
This section describes how to write an ADC hardware device. While
381
users of ADC devices do not need to read it, it may provide added
382
insight into how the devices work.
383
384
 
385
386
 
387
 
388
Data Structures
389
 
390
391
An ADC hardware driver is represented by a number of data
392
structures. These are generic device and
393
channel data structures, a driver private device
394
data structure, a generic character device table entry and a driver
395
function table. Most of these structures are instantiated using
396
macros, which will be described here.
397
398
 
399
400
The data structure instantiation for a typical single device, four
401
channel ADC would look like this:
402
403
 
404
405
//==========================================================================
406
// Instantiate data structures
407
 
408
// -------------------------------------------------------------------------
409
// Driver functions:
410
 
411
CYG_ADC_FUNCTIONS( example_adc_funs,
412
                   example_adc_enable,
413
                   example_adc_disable,
414
                   example_adc_set_rate );
415
 
416
// -------------------------------------------------------------------------
417
// Device instance:
418
 
419
static example_adc_info example_adc_info0 =
420
{
421
    .base               = CYGARC_HAL_EXAMPLE_ADC_BASE,
422
    .vector             = CYGNUM_HAL_INTERRUPT_ADC
423
};
424
 
425
CYG_ADC_DEVICE( example_adc_device,
426
                &example_adc_funs,
427
                &example_adc_info0,
428
                CYGNUM_IO_ADC_EXAMPLE_DEFAULT_RATE );
429
 
430
// -------------------------------------------------------------------------
431
// Channel instances:
432
 
433
#define EXAMPLE_ADC_CHANNEL( __chan )                                    \
434
CYG_ADC_CHANNEL( example_adc_channel##__chan,                            \
435
                 __chan,                                                 \
436
                 CYGNUM_IO_ADC_EXAMPLE_CHANNEL##__chan##_BUFSIZE,        \
437
                 &example_adc_device );                                  \
438
                                                                         \
439
DEVTAB_ENTRY( example_adc_channel##__chan##_device,                      \
440
              CYGDAT_IO_ADC_EXAMPLE_CHANNEL##__chan##_NAME,              \
441
              0,                                                         \
442
              &cyg_io_adc_devio,                                         \
443
              example_adc_init,                                          \
444
              example_adc_lookup,                                        \
445
              &example_adc_channel##__chan );
446
 
447
EXAMPLE_ADC_CHANNEL( 0 );
448
EXAMPLE_ADC_CHANNEL( 1 );
449
EXAMPLE_ADC_CHANNEL( 2 );
450
EXAMPLE_ADC_CHANNEL( 3 );
451
452
 
453
454
The macro CYG_ADC_FUNCTIONS() instantiates a
455
function table called example_adc_funs and
456
populates it with the ADC driver functions (see later for details).
457
458
 
459
460
Then an instance of the driver private device data structure is
461
instantiated. In addition to the device base address and interrupt
462
vector shown here, this stucture should contain the interrupt object
463
and handle for attaching to the vector. It may also contain any other
464
variables needed to manage the device.
465
466
 
467
468
The macro CYG_ADC_DEVICE() instantiates a
469
cyg_adc_device structure, named
470
example_adc_device which will contain pointers to
471
the function table and private data structure. The initial sample rate
472
is also supplied here.
473
474
 
475
476
For each channel, an ADC channel structure and a device table entry
477
must be created. The macro EXAMPLE_ADC_CHANNEL() is
478
defined to simplify this process. The macro
479
CYG_ADC_CHANNEL defines a
480
cyg_adc_channel structure, which contains the
481
channel number, the buffer size, and a pointer to the device object
482
defined earlier. The call to DEVTAB_ENTRY()
483
generates a device table entry containing the configured channel name,
484
a pointer to a device function table defined in the generic ADC
485
driver, pointers to init and lookup functions implemented here, and a
486
pointer to the channel data structure just defined.
487
488
 
489
490
Finally, four channels, numbered 0 to 3 are created.
491
492
 
493
 
494
495
 
496
Functions
497
 
498
499
There are several classes of function that need to be defined in an
500
ADC driver. These are those function that go into the channel's device
501
table, those that go into the ADC device's function table, calls that
502
the driver makes into the generic ADC package, and interrupt handling
503
functions.
504
505
 
506
Device Table Functions
507
 
508
509
These functions are placed in the standard device table entry for each
510
channel and handle initialization and location of the device within
511
the generic driver infrastructure.
512
513
 
514
515
static bool example_adc_init(struct cyg_devtab_entry *tab)
516
 
517
This function is called from the device IO infrastructure to
518
initialize the device. It should perform any work needed to start up
519
the device, short of actually starting the generation of samples. This
520
function will be called for each channel, so if there is
521
initialization that only needs to be done once, such as creating an
522
interrupt object, then care should be taken to do this. This function
523
should also call cyg_adc_device_init() to
524
initialize the generic parts of the driver.
525
526
 
527
528
static Cyg_ErrNo example_adc_lookup(struct cyg_devtab_entry **tab, struct cyg_devtab_entry *sub_tab, const char *name)
529
 
530
This function is called when a client looks up or opens a channel. It
531
should call cyg_adc_channel_init() to initialize
532
the generic part of the channel. It should also perform any operations
533
needed to start the channel generating samples.
534
535
 
536
537
 
538
Driver Functions
539
 
540
541
These are the functions installed into the driver function table by
542
the CYG_ADC_FUNCTIONS() macro.
543
544
 
545
546
static void example_adc_enable( cyg_adc_channel *chan )
547
 
548
This function is called from the generic ADC package to enable the
549
channel in response to a
550
CYG_IO_SET_CONFIG_ADC_ENABLE config operation. It
551
should take any steps needed to start the channel generating samples.
552
553
 
554
555
static void example_adc_disable( cyg_adc_channel *chan )
556
 
557
This function is called from the generic ADC package to enable the
558
channel in response to a
559
CYG_IO_SET_CONFIG_ADC_DISABLE config operation. It
560
should take any steps needed to stop the channel generating samples.
561
562
 
563
564
static void example_adc_set_rate( cyg_adc_channel *chan, cyg_uint32 rate )
565
 
566
This function is called from the generic ADC package to enable the
567
channel in response to a CYG_IO_SET_CONFIG_ADC_RATE
568
config operation. It should take any steps needed to change the sample
569
rate of the channel, or of the entire device.
570
571
 
572
 
573
574
 
575
Generic Package Functions
576
 
577
578
These functions are called by a hardware ADC device driver to perform
579
operations in the generic ADC package.
580
581
 
582
583
__externC void cyg_adc_device_init( cyg_adc_device *device )
584
 
585
This function is called from the driver's init function and is used to
586
initialize the cyg_adc_device object.
587
588
 
589
590
__externC void cyg_adc_channel_init(cyg_adc_channel *chan)
591
 
592
This function is called from the driver's lookup function and is used
593
to initialize the cyg_adc_channel object.
594
595
 
596
597
__externC cyg_uint32 cyg_adc_receive_sample(cyg_adc_channel *chan, cyg_adc_sample_t sample)
598
 
599
This function is called from the driver's ISR to add a new sample to
600
the buffer. The return value will be either zero, or
601
CYG_ISR_CALL_DSR and should be ORed with the return
602
value of the ISR.
603
604
 
605
606
__externC void cyg_adc_wakeup(cyg_adc_channel *chan )
607
 
608
This function is called from the driver's DSR to cause any threads
609
waiting for data to wake up when a new sample is available. It should
610
only be called if the wakeup field of the
611
channel object is true.
612
613
 
614
 
615
 
616
617
 
618
Interrupt Functions
619
 
620
621
These functions are internal to the driver, but make calls on generic
622
package functions. Typically an ADC device will have a single
623
interrupt vector with which it signals available samples on the
624
channels and any error conditions such as overruns.
625
626
 
627
628
static cyg_uint32 example_adc_isr(cyg_vector_t vector, cyg_addrword_t data)
629
 
630
This function is the ISR attached to the ADC device's interrupt
631
vector. It is responsible for reading samples from the channels and
632
passing them on to the generic layer. It needs to check each channel
633
for data, and call cyg_adc_receive_sample() for
634
each new sample available, and then ready the device for the next
635
interrupt.  It's activities are best explained by example:
636
637
 
638
639
static cyg_uint32 example_adc_isr(cyg_vector_t vector, cyg_addrword_t data)
640
{
641
    cyg_adc_device *example_device = (cyg_adc_device *) data;
642
    example_adc_info *example_info = example_device->dev_priv;
643
    cyg_uint32 res = 0;
644
    int i;
645
 
646
    // Deal with errors if necessary
647
    DEVICE_CHECK_ERRORS( example_info );
648
 
649
    // Look for all channels with data available
650
    for( i = 0; i < CHANNEL_COUNT; i++ )
651
    {
652
        if( CHANNEL_SAMPLE_AVALIABLE(i) )
653
        {
654
            // Fetch data from this channel and pass up to higher
655
            // level.
656
 
657
            cyg_adc_sample_t data = CHANNEL_GET_SAMPLE(i);
658
 
659
            res |= CYG_ISR_HANDLED | cyg_adc_receive_sample( example_info->channel[i], data );
660
        }
661
    }
662
 
663
   // Clear any interrupt conditions
664
    DEVICE_CLEAR_INTERRUPTS( example_info );
665
 
666
    cyg_drv_interrupt_acknowledge(example_info->vector);
667
 
668
    return res;
669
}
670
671
 
672
673
static void example_adc_dsr(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
674
 
675
This function is the DSR attached to the ADC device's interrupt
676
vector. It is called by the kernel if the ISR return value contains
677
the CYG_ISR_HANDLED bit. It needs to call
678
cyg_adc_wakeup() for each channel that has its
679
wakeup field set. Again, and example should
680
make it all clear:
681
682
 
683
684
static void example_adc_dsr(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
685
{
686
    cyg_adc_device *example_device = (cyg_adc_device *) data;
687
    example_adc_info *example_info = example_device->dev_priv;
688
    int i;
689
 
690
    // Look for all channels with pending wakeups
691
    for( i = 0; i < CHANNEL_COUNT; i++ )
692
    {
693
        if( example_info->channel[i]->wakeup )
694
            cyg_adc_wakeup( example_info->channel[i] );
695
    }
696
}
697
 
698
699
 
700
 
701
702
 
703
 
704
705
 
706
 
707
708
 
709

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.