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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [io/] [can/] [current/] [doc/] [can.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
CAN Support
34
 
35
36
Overview
37
 
38
39
Description
40
 
41
42
The Controller Area Network, CAN, is a multicast shared, differential
43
serial bus standard especially suited for networking "intelligent"
44
devices as well as sensors and actuators within a system or sub-system.
45
The protocol was originally developed in the 1980s by Robert Bosch GmbH
46
aiming at automotive applications. Nowadays CAN has gained widespread use
47
and is used in industrial automation as well as in automotive, mobile
48
machines and in many embedded control applications.
49
50
 
51
52
The CAN protocol is defined by the ISO 11898-1 standard. The physical layer
53
uses differential transmission on a twisted pair wire.  CAN uses a
54
non-destructive bit-wise arbitration to control access to the bus.
55
56
 
57
58
There is no explicit address in the messages because in CAN networks there
59
is no addressing of subscribers or stations, but instead, each message carries
60
a prioritized identifier. A transmitter sends a message to all CAN nodes
61
(broadcasting). The identifier may serve as an identification of the contents
62
of the message and also determines the priority that the message enjoys in
63
competition for bus access. A node decides on the basis of this identifier
64
received whether it should process the message or not.
65
66
 
67
68
The CAN messages are small (at most eight data bytes) and are protected
69
by a checksum. Each CAN message consists of  an 11 bit message ID, up to 8
70
bytes of data and, a CRC checksum and a number of  control bits. These
71
short messages ensure a robust transfer of data in electromagnetically
72
noisy environments. An extended version of the CAN frame supports 29 bit
73
message identifiers.
74
75
 
76
77
Basically there are two different operational modes for CAN receivers -
78
FullCAN and BasicCAN.  The difference between these two modes is the
79
Object Storage function. The BasicCAN architecture is quite similar to a
80
simple UART. A BasicCAN device has typically one transmit buffer and two
81
receive buffers. The CAN chip handles only the transmitting and receiving
82
of the data (and the error handling) and so most of the manipulation of the
83
data has to be done by the CPU. The CPU has to request the transmitting or
84
acknowledge the receiving of the data through the interrupt flags. This will
85
burden the CPU and take up much of the CPU time.
86
87
 
88
89
The FullCAN architecture is more suitable for high-speed performance. It
90
has its own storage area on chip and works with a number of message buffers
91
or message boxes. The CAN controller has its own Acceptance Filtering Mask
92
on chip. It can thus determine which frames are to be received by examining
93
the identifiers. The CPU in this case will only receive the valid (wanted)
94
frames and hence improve the performance of the CPU.
95
96
 
97
98
You can find more information at the
99
CAN in Automation website.
100
101
 
102
 
103
 
104
105
eCos Support for CAN
106
 
107
108
The eCos CAN subsystem supports the BasicCAN and FullCAN mode. The architecture
109
and the interface of the eCos CAN driver is quite similar to the eCos serial
110
driver and supports the same interface.
111
112
 
113
114
The eCos CAN support for any given platform is spread over a number of different
115
packages:
116
117
 
118
119
  
120
    
121
 
122
This package, CYGPKG_IO_CAN, exports a generic
123
device independent CAN I/O API for accessing devices attached to a CAN
124
network. This API handles issues such as locking between threads. The
125
package does not contain any hardware-specific code. Instead it will
126
call into a CAN device driver to handle the hardware device
127
access. This package also defines the interface that such hardware
128
drivers should provide.
129
    
130
  
131
  
132
    
133
Each CAN device will have its own device driver, which is implemented
134
as a separate package, for example
135
CYGPKG_DEVS_CAN_MCF52xx_FLEXCAN. For devices that may
136
be attached to a variety of different boards the device driver will be
137
generic and a second platform specific package will be used to customize
138
it to each platform. For devices that are associated with a specific
139
chipset, only a single package may be present.
140
    
141
  
142
143
 
144
145
Typically all appropriate packages will be loaded automatically when
146
you configure eCos for a given platform. If the application does not use
147
any of the CAN I/O facilities, directly or indirectly, then linker garbage
148
collection should eliminate all unnecessary code and data. All necessary
149
initialization should happen automatically. However the exact details may
150
depend on the platform, so the platform HAL documentation should be
151
checked for further details.
152
153
 
154
155
There is an important exception to this: if the CAN devices are attached
156
to an expansion connector, such as PCI, then the platform HAL will not
157
know about these devices. Instead the necessary packages will need to be
158
added explicitly during configuration.
159
160
161
162
 
163
164
User API
165
 
166
167
The CAN driver uses the standard eCos I/O API functions. All functions
168
except cyg_io_lookup() require an I/O "handle".
169
170
 
171
172
All functions return a value of the type Cyg_ErrNo.
173
If an error condition is detected, this value will be negative and the
174
absolute value indicates the actual error, as specified in
175
cyg/error/codes.h. The only other legal return values
176
will be ENOERR, -EINTR,
177
-EINVAL and -EAGAIN.  All other
178
function arguments are pointers (references). This allows the drivers
179
to pass information efficiently, both into and out of the driver. The
180
most striking example of this is the len value
181
passed to the read and write functions.  This parameter contains the
182
desired length of data on input to the function and the actual
183
transferred length on return.
184
185
 
186
187
// Lookup a CAN device and return its handle
188
Cyg_ErrNo cyg_io_lookup(
189
    const char *name,
190
    cyg_io_handle_t *handle )
191
192
 
193
194
This function maps a CAN device name onto an appropriate handle. If the
195
named device is not in the system, then the error
196
-ENOENT is returned. If the device is found, then
197
the handle for the device is returned by way of the handle pointer
198
*handle.
199
200
 
201
202
// Send a CAN message
203
Cyg_ErrNo cyg_io_write(
204
    cyg_io_handle_t handle,
205
    const void *buf,
206
    cyg_uint32 *len )
207
208
 
209
210
This function sends one single CAN message (not a buffer of CAN messages)
211
to a device. The size of data to send is contained in
212
*len and the actual size sent will be returned in
213
the same place.
214
215
 
216
217
// Read one CAN event from device
218
Cyg_ErrNo cyg_io_read(
219
    cyg_io_handle_t handle,
220
    void *buf,
221
    cyg_uint32 *len )
222
223
 
224
225
This function receives one single CAN event from a device. The desired size
226
of data to receive is contained in *len and the
227
actual size obtained will be returned in the same place.
228
229
 
230
231
You may notice that the data sent (messages) is different from the
232
data received (events). An event includes flags that describe the
233
event, and eventually a received message and a timestamp.
234
235
 
236
237
// Read configuration of a CAN device
238
Cyg_ErrNo cyg_io_get_config(
239
    cyg_io_handle_t handle,
240
    cyg_uint32 key,
241
    void *buf,
242
    cyg_uint32 *len )
243
244
 
245
246
This function is used to obtain run-time configuration about a
247
device. The type of information retrieved is specified by the
248
key. The data will be returned in the given
249
buffer. The value of *len should contain the
250
amount of data requested, which must be at least as large as the size
251
appropriate to the selected key. The actual size of data retrieved is
252
placed in *len. The appropriate key values
253
are all listed in the file <cyg/io/config_keys.h>.
254
255
 
256
257
// Change configuration of a CAN device
258
Cyg_ErrNo cyg_io_set_config(
259
    cyg_io_handle_t handle,
260
    cyg_uint32 key,
261
    const void *buf,
262
    cyg_uint32 *len )
263
264
 
265
266
This function is used to manipulate or change the run-time
267
configuration of a device. The type of information is specified by the
268
key. The data will be obtained from the given
269
buffer. The value of *len should contain the
270
amount of data provided, which must match the size appropriate to the
271
selected key.  The appropriate key values are all listed in the file
272
<cyg/io/config_keys.h>.
273
274
275
 
276
277
CAN driver details
278
 
279
280
Allow applications and other packages to access CAN devices.
281
282
 
283
284
Description
285
 
286
287
A raw CAN driver is is provided as a standard part of the eCos system.
288
289
 
290
291
Use the include file <cyg/io/canio.h> for this driver.
292
293
 
294
295
The CAN driver is capable of sending single CAN messages to a device and
296
receiving single CAN events from a CAN device. Controls are provided to
297
configure the actual hardware, but there is no manipulation of the data by
298
this driver.
299
300
 
301
302
There may be many instances of this driver in a given system, one for each
303
CAN channel. Each channel corresponds to a physical device and there will
304
typically be a device module created for this purpose. The device modules
305
themselves are configurable, allowing specification of the actual hardware
306
details.
307
308
309
 
310
311
API Details
312
 
313
314
cyg_io_write
315
 
316
317
cyg_io_write(handle, buf, len)
318
319
 
320
321
To transmit a message an application must fill a cyg_can_message
322
buffer and call cyg_io_write().
323
This function sends one single CAN message (not a buffer of CAN messages)
324
to a device. The size of data to send is contained in *len
325
and the actual size sent will be returned in the same place. A pointer to a
326
cyg_can_message is contained in *buf.
327
The driver maintains a buffer to hold the data. The size of the intermediate
328
buffer is configurable within the interface module. The data is not modified
329
at all while it is being buffered. On return, *len
330
contains the amount of characters actually consumed - that means
331
*len always contains
332
sizeof(cyg_can_message).
333
334
 
335
336
It is possible to configure the write call to be blocking (default) or
337
non-blocking. Non-blocking mode requires both the configuration option
338
CYGOPT_IO_CAN_SUPPORT_NONBLOCKING to be enabled, and
339
the specific device to be set to non-blocking mode for writes
340
(see cyg_io_set_config()). In blocking mode, the
341
call will not return until there is space in the buffer and the content
342
of the CAN message has been consumed. In non-blocking mode, if there is
343
no space in buffer for the CAN message, -EAGAIN is
344
returned and the caller must try again.
345
346
 
347
348
It is possible to configure the write call to be non-blocking with timeout.
349
None-blocking mode with timeout requires the configuration option
350
CYGOPT_IO_CAN_SUPPORT_NONBLOCKING and
351
CYGOPT_IO_CAN_SUPPORT_TIMEOUTS to be enabled, requires
352
the eCos kernel package to be included and the specific device to be set
353
to non-blocking mode for writes (see cyg_io_set_config()
354
).
355
In non-blocking mode with timeouts, if there is no space in buffer for the
356
CAN message, the driver waits a certain amount of time (the timeout time)
357
for space in the buffer. If there is still no space in buffer after
358
expiration of the timeout time, -EINTR is returned and
359
the caller must try again.
360
361
 
362
363
If a message was successfully sent, the function returns ENOERR.
364
365
366
 
367
368
CAN Messages
369
 
370
371
The CAN driver uses cyg_can_message structures to
372
pass messages between the application and the CAN driver. The type
373
cyg_can_message provides a device independent type of CAN message.
374
Before calling the write function this message should be setup properly.
375
376
 
377
378
typedef struct can_message
379
{
380
    cyg_uint32         id;
381
    cyg_uint8          data[8];
382
    cyg_can_id_type    ext;
383
    cyg_can_frame_type rtr;
384
    cyg_uint8          dlc;
385
} cyg_can_message;
386
387
 
388
389
The structure contains the following fields:
390
391
 
392
393
   
394
       cyg_uint32 id
395
           
396
Message ID. This is the ID to be transmitted with the message, or the
397
ID received. If the ext field is set, then
398
this will contain a 29 bit ID, otherwise it will contain an 11 bit ID.
399
          
400
   
401
   
402
       cyg_uint32 data
403
           
404
Message data. Only the first dlc bytes of
405
data are valid. If the rtr field is set,
406
then the contents of this field are ignored.
407
          
408
   
409
   
410
       cyg_can_id_type ext
411
           
412
Extended ID. If this field is CYGNUM_CAN_ID_EXT then the
413
id field contains a 29 bit extended ID. If it
414
contains CYGNUM_CAN_ID_STD then the ID is 11 bits.
415
          
416
   
417
   
418
       cyg_can_frame_type rtr
419
           
420
Remote Transmission Request. If this field contains
421
CYGNUM_CAN_FRAME_RTR then the RTR bit on the message
422
will be set and the data field will be ignored.
423
If the field contains CYGNUM_CAN_FRAME_DATA then a
424
normal data frame will be send.
425
          
426
   
427
   
428
       cyg_uint8 dlc
429
           
430
The length of the data carried in the message. This can range from
431
zero to 8. In a message with the rtr field set,
432
this indicates the size of data being requested.
433
          
434
   
435
436
 
437
438
Example code for sending one single CAN message:
439
440
 
441
442
cyg_can_message tx_msg;
443
cyg_uint32      len;
444
Cyg_ErrNo       ret;
445
 
446
tx_msg.id  = 0x100;
447
tx_msg.ext = CYGNUM_CAN_ID_EXT;
448
tx_msg.rtr = CYGNUM_CAN_FRAME_DATA;
449
tx_msg.dlc = 1;
450
tx_msg.data[0] = 0xF1;
451
 
452
len = sizeof(tx_msg);
453
ret = cyg_io_write(hDrvCAN, &tx_msg, &len);
454
455
456
 
457
458
cyg_io_read
459
 
460
461
cyg_io_read(handle, buf, len)
462
463
 
464
465
To receive a message the application calls cyg_io_read().
466
This function receives one single event from a device. The desired size
467
of data to receive is contained in *len and the
468
actual size obtained will be returned in the same place. A pointer to
469
a cyg_can_event is contained in *buf.
470
No manipulation of the data is performed before being transferred.
471
Again, this buffering is completely configurable. On return,
472
*len contains sizeof(cyg_can_event).
473
474
 
475
476
It is possible to configure the read call to be blocking (default) or
477
non-blocking. Non-blocking mode requires both the configuration option
478
CYGOPT_IO_CAN_SUPPORT_NONBLOCKING to be enabled,
479
and the specific device to be set to non-blocking mode for reads
480
(see cyg_io_set_config()). In blocking mode,
481
the call will not return until one single CAN event has been read.
482
In non-blocking mode, if there is no CAN event in buffer, the call
483
returns immediately with -EAGAIN and the caller must
484
try again.
485
486
 
487
488
It is possible to configure the read call to be non-blocking with timeout.
489
None-blocking mode with timeout requires the configuration option
490
CYGOPT_IO_CAN_SUPPORT_NONBLOCKING and
491
CYGOPT_IO_CAN_SUPPORT_TIMEOUTS to be enabled,
492
requires the eCos kernel package to be included and the specific device
493
to be set to non-blocking mode for reads (see
494
cyg_io_set_config()). In non-blocking mode with timeouts,
495
if there is no CAN event in receive buffer, the driver waits a certain amount
496
of time (the timeout time) for a CAN event to arrive. If there is still no
497
CAN event in buffer after expiration of the timeout time, -EINTR
498
is returned and the caller must try again.
499
500
 
501
502
If an event was successfully received, the function returns ENOERR.
503
504
505
 
506
 
507
508
CAN Events
509
 
510
511
The CAN driver uses cyg_can_event structures to
512
pass events from hardware device driver to the generic CAN driver.
513
A cyg_can_event provides a generic device
514
independent type for handling CAN events that may occur.
515
516
 
517
518
typedef struct cyg_can_event_st
519
{
520
    cyg_uint32      timestamp;
521
    cyg_can_event_flags_t flags;
522
    cyg_can_message msg;
523
} cyg_can_event;
524
525
 
526
527
The structure contains the following fields:
528
529
 
530
531
   
532
       cyg_uint32 timestamp
533
           
534
If the hardware CAN device driver supports timestamps then this field may
535
contain a timestamp value for an event that occured.
536
          
537
   
538
   
539
       cyg_can_event_flags_t flags
540
           
541
Event flags. The flags field contains bits that
542
indicate which kind of events occured. More than one flag can be raised
543
in a single event.
544
          
545
   
546
   
547
       cyg_can_message msg
548
           
549
CAN message. The msg field contains a CAN message if an RX or TX event
550
occured.  If another type of event occured,
551
the data field of
552
the msg may contain additional event
553
specific data.
554
          
555
   
556
557
 
558
559
The following events are supported and after receiving an event the
560
application should check the flags field against these values:
561
562
 
563
564
CYGNUM_CAN_EVENT_RX               0x00000001 // message received
565
CYGNUM_CAN_EVENT_TX               0x00000002 // mesage transmitted
566
CYGNUM_CAN_EVENT_WARNING_RX       0x00000004 // tx error counter (TEC) reached warning level (>96)
567
CYGNUM_CAN_EVENT_WARNING_TX       0x00000008 // rx error counter (REC) reached warning level (>96)
568
CYGNUM_CAN_EVENT_ERR_PASSIVE      0x00000010 // CAN "error passive" occured
569
CYGNUM_CAN_EVENT_BUS_OFF          0x00000020 // CAN "bus off" error occured
570
CYGNUM_CAN_EVENT_OVERRUN_RX       0x00000040 // overrun in RX queue occured
571
CYGNUM_CAN_EVENT_OVERRUN_TX       0x00000080 // overrun in TX queue occured
572
CYGNUM_CAN_EVENT_CAN_ERR          0x00000100 // a CAN bit or frame error occured
573
CYGNUM_CAN_EVENT_LEAVING_STANDBY  0x00000200 // CAN hardware leaves standby / power down mode or is waked up
574
CYGNUM_CAN_EVENT_ENTERING_STANDBY 0x00000400 // CAN hardware enters standby / power down mode
575
CYGNUM_CAN_EVENT_ARBITRATION_LOST 0x00000800 // arbitration lost
576
CYGNUM_CAN_EVENT_FILTER_ERR       0x00001000 // CAN message filter / acceptance filter error
577
CYGNUM_CAN_EVENT_PHY_FAULT        0x00002000 // General failure of physical layer detected
578
CYGNUM_CAN_EVENT_PHY_H            0x00004000 // Fault on CAN-H detected (Low Speed CAN)
579
CYGNUM_CAN_EVENT_PHY_L            0x00008000 // Fault on CAN-L detected (Low Speed CAN)
580
CYGNUM_CAN_EVENT_ERR_ACTIVE       0x00010000 // CAN controller now "error active"
581
CYGNUM_CAN_EVENT_OVERRUN_RX_HW    0x00020000 // CAN controller reports a RX overrun
582
583
 
584
585
Often the flags field will contain only one single set flag. But it is
586
possible that a number of flags is set and so the flag field should
587
always be checked by a receiver.  Most of the flags are independent
588
from each other and the receiver has to handle each of them separately.
589
590
 
591
592
The internal receive buffers of the CAN device driver are circular
593
buffers.  That means that even if the buffers are completely filled
594
new messages will be received. In this case the newest message will
595
always overwrite the oldest message in the receive buffer. If this
596
happens the
597
CYGNUM_CAN_EVENT_OVERRUN_RX flag will be set for
598
this new message that caused overwriting of the old one. However if an
599
overrun occurs in the hardware message buffers of the CAN controller,
600
the flag CYGNUM_CAN_EVENT_OVERRUN_RX_HW is raised
601
instead.
602
603
 
604
605
Example code for receiving one single CAN event:
606
607
 
608
609
cyg_can_event rx_event;
610
cyg_uint32    len;
611
Cyg_ErrNo     ret;
612
 
613
len = sizeof(rx_event);
614
ret = cyg_io_read(hDrvCAN, &rx_event, &len);
615
 
616
if (ENOERR == ret)
617
{
618
    if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
619
    {
620
        // handle RX event
621
    }
622
 
623
    if (rx_event.flags & ~CYGNUM_CAN_EVENT_RX)
624
    {
625
        // handle other events
626
    }
627
}
628
else if (-EINTR == ret)
629
{
630
    // handle timeout
631
}
632
633
634
 
635
 
636
637
cyg_io_get_config
638
 
639
640
cyg_io_get_config(handle, key, buf, len)
641
642
 
643
644
This function is used to obtain run-time configuration about a device.
645
The type of information retrieved is specified by the key.
646
The data will be returned in the given buffer. The value of
647
*len should contain the amount of data requested,
648
which must be at least as large as the size appropriate to the selected
649
key. The actual size of data retrieved is placed
650
in *len. The appropriate key values are all listed
651
in the file <cyg/io/config_keys.h>.
652
653
 
654
655
The following config keys are currently supported:
656
657
 
658
659
CYG_IO_GET_CONFIG_READ_BLOCKING
660
CYG_IO_GET_CONFIG_WRITE_BLOCKING
661
CYG_IO_GET_CONFIG_CAN_INFO
662
CYG_IO_GET_CONFIG_CAN_BUFFER_INFO
663
CYG_IO_GET_CONFIG_CAN_MSGBUF_INFO
664
CYG_IO_GET_CONFIG_CAN_TIMEOUT
665
CYG_IO_GET_CONFIG_CAN_HDI
666
CYG_IO_GET_CONFIG_CAN_STATE
667
668
669
 
670
671
cyg_io_set_config
672
 
673
674
cyg_io_set_config(handle, key, buf, len)
675
676
 
677
678
This function is used to manipulate or change the run-time configuration
679
of a device. The type of information is specified by the key.
680
The data will be obtained from the given buffer. The value of
681
*len should contain the amount of data provided,
682
which must match the size appropriate to the selected key.
683
The appropriate key values are all listed in the file
684
<cyg/io/config_keys.h>.
685
686
 
687
688
The following config keys are currently supported:
689
690
 
691
692
CYG_IO_SET_CONFIG_READ_BLOCKING
693
CYG_IO_SET_CONFIG_WRITE_BLOCKING
694
CYG_IO_SET_CONFIG_CAN_INFO
695
CYG_IO_SET_CONFIG_CAN_OUTPUT_DRAIN
696
CYG_IO_SET_CONFIG_CAN_OUTPUT_FLUSH
697
CYG_IO_SET_CONFIG_CAN_INPUT_FLUSH
698
CYG_IO_SET_CONFIG_CAN_TIMEOUT
699
CYG_IO_SET_CONFIG_CAN_MSGBUF
700
CYG_IO_SET_CONFIG_CAN_MODE
701
CYG_IO_SET_CONFIG_CAN_ABORT
702
CYG_IO_SET_CONFIG_CAN_CALLBACK
703
CYG_IO_SET_CONFIG_CAN_RANGE_FILTER
704
CYG_IO_SET_CONFIG_CAN_MASK_FILTER
705
706
707
708
 
709
710
Runtime Configuration
711
 
712
713
Runtime configuration is achieved by exchanging data structures with the
714
driver via the cyg_io_set_config() and
715
cyg_io_get_config() functions.
716
717
 
718
719
Device configuration
720
 
721
722
typedef struct cyg_can_info_st {
723
    cyg_can_baud_rate_t baud;
724
} cyg_can_info_t;
725
726
 
727
728
Device configuration is achieved by by exchanging
729
cyg_can_info_t data structures with the driver
730
via the cyg_io_set_config() and
731
cyg_io_get_config() functions using the config keys
732
CYG_IO_GET_CONFIG_CAN_INFO and
733
CYG_IO_SET_CONFIG_CAN_INFO.
734
The field baud contains a baud rate selection.
735
This must be one of the following values:
736
737
 
738
739
CYGNUM_CAN_KBAUD_10
740
CYGNUM_CAN_KBAUD_20
741
CYGNUM_CAN_KBAUD_50
742
CYGNUM_CAN_KBAUD_100
743
CYGNUM_CAN_KBAUD_125
744
CYGNUM_CAN_KBAUD_250
745
CYGNUM_CAN_KBAUD_500
746
CYGNUM_CAN_KBAUD_800
747
CYGNUM_CAN_KBAUD_1000
748
749
750
 
751
752
Timeout configuration
753
 
754
755
typedef struct cyg_can_timeout_info_st
756
{
757
    cyg_uint32 rx_timeout;
758
    cyg_uint32 tx_timeout;
759
} cyg_can_timeout_info_t;
760
761
 
762
763
Timeout configuration is achieved by by exchanging
764
cyg_can_timeout_info_t data structures with the
765
driver via the cyg_io_set_config() and
766
cyg_io_get_config() functions using the config keys
767
CYG_IO_SET_CONFIG_CAN_TIMEOUT and
768
CYG_IO_SET_CONFIG_CAN_TIMEOUT.
769
770
 
771
772
   
773
       cyg_uint32 rx_timeout
774
           
775
Timeout for cyg_io_read calls.
776
          
777
   
778
   
779
       cyg_uint32 tx_timeout
780
           
781
Timeout for cyg_io_write calls.
782
          
783
   
784
785
 
786
787
Timeout runtime configuration is supported if the configuration options
788
CYGOPT_IO_CAN_SUPPORT_NONBLOCKING
789
and CYGOPT_IO_CAN_SUPPORT_TIMEOUTS are enabled.
790
791
792
 
793
794
Reading buffer configuration
795
 
796
797
typedef struct cyg_can_buf_info_st
798
{
799
    cyg_int32 rx_bufsize;
800
    cyg_int32 rx_count;
801
    cyg_int32 tx_bufsize;
802
    cyg_int32 tx_count;
803
} cyg_can_buf_info_t;
804
805
 
806
807
CYG_IO_GET_CONFIG_CAN_BUFFER_INFO - This function
808
retrieves the current state of the software buffers in the CAN drivers.
809
For the transmit buffer it returns the the total number of
810
cyg_can_message objects in buffer and the current number of
811
cyg_can_message objects occupied in the buffer.
812
For the receive buffer it returns the total number of
813
cyg_can_event objects in receive buffer and the current
814
number of cyg_can_event objects occupied in the buffer.
815
It does not take into account any buffering such as FIFOs or holding
816
registers that the CAN hardware device itself may have.
817
818
 
819
820
   
821
       cyg_uint32 rx_bufsize
822
           
823
Total number of cyg_can_event buffers in receive queue.
824
          
825
   
826
   
827
       cyg_uint32 rx_count
828
           
829
Current number of cyg_can_event buffers occupied in receive queue.
830
          
831
   
832
      
833
       cyg_uint32 tx_bufsize
834
           
835
Total number of cyg_can_message buffers in transmit queue.
836
          
837
   
838
   
839
       cyg_uint32 rtx_count
840
           
841
Current number of cyg_can_message buffers occupied in transmit queue.
842
          
843
   
844
845
  
846
 
847
 
848
849
Reading hardware description information
850
 
851
852
typedef struct cyg_can_hdi_st
853
{
854
    cyg_uint8 support_flags;
855
    cyg_uint8 controller_type;
856
} cyg_can_hdi;
857
858
 
859
860
CYG_IO_GET_CONFIG_CAN_HDI - This function retrieves
861
information about the used hardware. The Hardware Description Interface
862
provides a method to gather information about the CAN hardware and the
863
functionality of the driver. For this purpose the structure
864
cyg_can_hdi is defined.
865
866
 
867
868
   
869
       cyg_uint8 support_flags
870
           
871
Contains information about the capabilities of the used CAN hardware.
872
          
873
   
874
   
875
       cyg_uint8 controller_type
876
           
877
A number that identifies the CAN controller type.
878
          
879
   
880
881
 
882
883
The following flags are available in the field support_flags:
884
885
 
886
887
|   7   |   6   |   5   |   4   |   3   |   2   |   1   |   0   |
888
+-------+-------+-------+-------+--------+-------+-------+-------+
889
|ListenO|Mask F |Range F|timest.|autobaud|FullCAN|   Frametype   |
890
891
 
892
893
  
894
    Frametype
895
    
896
Bit 0 and Bit 1 of the structure describe the possibilities of the CAN
897
controller. The following values are defined:
898
    
899
CYGNUM_CAN_HDI_FRAMETYPE_STD          // receives only standard frame
900
CYGNUM_CAN_HDI_FRAMETYPE_EXT_PASSIVE  // can receive but not send extended frames
901
CYGNUM_CAN_HDI_FRAMETYPE_EXT_ACTIVE   // can send and receive extended frames
902
    
903
    
904
  
905
    
906
    FullCAN
907
    
908
If the Bit 2 - CYGNUM_CAN_HDI_FULLCAN  - is set to one,
909
the CAN controller supports more than one message buffer.
910
    
911
  
912
  
913
    autobaud
914
    
915
If Bit 3 - CYGNUM_CAN_HDI_AUTBAUD - is set to one then
916
the CAN driver supports an autobaud feature.
917
    
918
  
919
  
920
    Timestamp
921
    
922
If Bit 4 - CYGNUM_CAN_HDI_TIMESTAMP - is set to one then
923
the CAN hardware supports timestamps for CAN messages.
924
    
925
  
926
  
927
    Identifier Range filtering
928
    
929
If Bit 5 - CYGNUM_CAN_HDI_RANGE_FILTERING - is set to one
930
then the CAN hardware supports message filtering based on identifier ranges.
931
    
932
  
933
  
934
    Identifier Mask filtering
935
    
936
If Bit 6 - CYGNUM_CAN_HDI_MASK_FILTERING - is set to one
937
then the CAN hardware supports message filtering based on identifier masks.
938
    
939
  
940
  
941
    Listen Only mode
942
    
943
If Bit 7 - CYGNUM_CAN_HDI_LISTEN_ONLY - is set to one
944
then the CAN hardware supports a 'listen-only' mode.
945
    
946
  
947
948
  
949
 
950
951
Reading hardware message buffer configuration
952
953
typedef struct cyg_can_msgbox_info_st
954
{
955
    cyg_uint16 count; // number of message buffers available for this device
956
    cyg_uint16 free;  // number of free message buffers
957
} cyg_can_msgbuf_info;
958
959
 
960
961
CYG_IO_GET_CONFIG_CAN_MSGBUF_INFO - If the CAN hardware supports
962
more than one message buffer for reception of CAN messages (flag
963
CYGNUM_CAN_HDI_FULLCAN is set while reading hardware description
964
interface with CYG_IO_GET_CONFIG_CAN_HDI) then this function
965
reads the number of message buffers the CAN hardware supports and the number of
966
free message buffers.
967
968
 
969
970
   
971
       cyg_uint16 count
972
           
973
Counts the number of message buffers supported by the device.
974
          
975
   
976
   
977
       cyg_uint16 free
978
           
979
Contains the number of free message buffers. The free message buffers are
980
available for setting up remote buffers (CYG_IO_SET_CONFIG_CAN_REMOTE_BUF)
981
and message filters (CYG_IO_SET_CONFIG_CAN_FILTER_MSG).
982
          
983
   
984
985
 
986
 
987
988
Reading state of CAN hardware
989
 
990
991
typedef enum
992
{
993
  CYGNUM_CAN_STATE_ACTIVE,      // CAN controller active, no errors
994
  CYGNUM_CAN_STATE_STOPPED,     // CAN controller in stopped mode
995
  CYGNUM_CAN_STATE_STANDBY,     // CAN controller in Sleep mode
996
  CYGNUM_CAN_STATE_BUS_WARN,    // CAN controller active, warning level is reached
997
  CYGNUM_CAN_STATE_ERR_PASSIVE, // CAN controller went into error passive mode
998
  CYGNUM_CAN_STATE_BUS_OFF,     // CAN controller went into bus off mode
999
  CYGNUM_CAN_STATE_PHY_FAULT,   // General failure of physical layer
1000
  CYGNUM_CAN_STATE_PHY_H,       // Fault on CAN-H detected (Low Speed CAN)
1001
  CYGNUM_CAN_STATE_PHY_L,       // Fault on CAN-L detected (Low Speed CAN)
1002
} cyg_can_state;
1003
1004
 
1005
1006
CYG_IO_GET_CONFIG_CAN_STATE - This function retrieves the
1007
present state of the CAN controller. Possible values are defined in the
1008
cyg_can_state enumeration.
1009
1010
 
1011
 
1012
 
1013
1014
Changing mode of CAN hardware
1015
 
1016
1017
CYG_IO_SET_CONFIG_CAN_MODE - This function changes
1018
the operating mode of the CAN controller. The identifiers for the different
1019
operating modes are defined in the cyg_can_mode enumeration.
1020
1021
 
1022
1023
typedef enum
1024
{
1025
  CYGNUM_CAN_MODE_STOP,   // set controller into stop mode
1026
  CYGNUM_CAN_MODE_START,  // set controller into operational mode
1027
  CYGNUM_CAN_MODE_STANDBY,// set controller into standby / sleep mode
1028
  CYGNUM_CAN_MODE_CONFIG, // safe mode to add/delete message buffers
1029
  CYGNUM_CAN_MODE_LISTEN_ONLY_ENTER, // set controller into listen only mode.
1030
  CYGNUM_CAN_MODE_LISTEN_ONLY_EXIT   // set controller out of listen only mode.
1031
} cyg_can_mode;
1032
1033
 
1034
1035
   
1036
       CYGNUM_CAN_MODE_STOP
1037
           
1038
Set controller into stop mode
1039
          
1040
   
1041
   
1042
       CYGNUM_CAN_MODE_START
1043
           
1044
Set controller into operational mode
1045
          
1046
   
1047
   
1048
       CYGNUM_CAN_MODE_STANDBY
1049
           
1050
Set controller into standby / sleep mode.
1051
          
1052
   
1053
   
1054
       CYGNUM_CAN_MODE_CONFIG
1055
           
1056
Set controller into a mode allowing modifying message buffers.
1057
          
1058
   
1059
   
1060
       CYGNUM_CAN_MODE_LISTEN_ONLY_ENTER
1061
            Make controller enter listen-only mode (if
1062
supported by hardware). In such a mode the CAN controller won't
1063
acknowledge the messages it sees on the bus. This mode can help to
1064
perform autobaud at application level if the underlying hardware does
1065
not support it directly. Depending on your CAN transceiver, such a
1066
mode may also be implemented by the transceiver.
1067
          
1068
   
1069
   
1070
       CYGNUM_CAN_MODE_LISTEN_ONLY_EXIT
1071
            Make controller exit of listen-only
1072
mode. The controller will acknowledge all messages it sees on the bus.
1073
          
1074
   
1075
1076
 
1077
1078
Before the hardware configuration of the device is changed, that means
1079
if baud rate is changed or the message buffer and filter configuration
1080
is changed, the CAN hardware should be set into stop or config mode
1081
and if configuration is finished, then device should be set back into
1082
operational mode.  Before the device is set into standby mode, the
1083
output buffers should be flushed or drained because transmission of a
1084
CAN message may wake up the CAN hardware. If a received message wakes
1085
up the CAN hardware from standby mode then
1086
a CYGNUM_CAN_EVENT_LEAVING_STANDBY event will be
1087
inserted into receive message buffer or
1088
the CYGNUM_CAN_EVENT_LEAVING_STANDBY flag will be
1089
set for the message that caused wake up of CAN hardware.
1090
1091
 
1092
1093
You must also check with the CAN controller data sheet if an incoming
1094
message waking up the controller is fully received and processed by
1095
the controller, or if such a wake up message is lost.
1096
1097
 
1098
 
1099
1100
Flush or drain buffers
1101
 
1102
1103
CYG_IO_SET_CONFIG_CAN_OUTPUT_DRAIN - This function
1104
waits for any buffered output to complete. This function only
1105
completes when there is no more data remaining to be sent to the
1106
device.
1107
1108
 
1109
1110
CYG_IO_SET_CONFIG_CAN_OUTPUT_FLUSH - This function
1111
discards any buffered output for the device.
1112
1113
 
1114
1115
CYG_IO_SET_CONFIG_CAN_INPUT_FLUSH - This function
1116
discards any buffered input for the device.
1117
1118
1119
 
1120
1121
Configuring blocking/non-blocking calls
1122
1123
By default all calls to cyg_io_read()
1124
and cyg_io_write() are blocking calls. The config
1125
keys
1126
1127
 
1128
1129
CYG_IO_SET_CONFIG_READ_BLOCKING
1130
CYG_IO_SET_CONFIG_WRITE_BLOCKING
1131
1132
 
1133
1134
enable switching between blocking and nonblocking calls separatly for
1135
read and write calls.  If blocking calls are configured then the
1136
read/write functions return only if a message was stored into TX
1137
buffer or an event was received from RX buffer. If non-blocking calls
1138
are enabled and there is no space in TX buffer or RX buffer is empty
1139
then the function returns immediately
1140
with -EAGAIN.
1141
1142
 
1143
1144
If non-blocking calls are enabled and additionally timeouts are
1145
supported by driver, then the read/write functions wait until timeout
1146
value is expired and then return with -EINTR.  If
1147
the read/write operation succeeds during the timed wait then the
1148
functions return succesfully with
1149
ENOERR.
1150
1151
 
1152
1153
To query if cyg_io_read()
1154
and cyg_io_write() are blocking or non-blocking
1155
you can use the config keys
1156
1157
 
1158
1159
CYG_IO_GET_CONFIG_READ_BLOCKING
1160
CYG_IO_GET_CONFIG_WRITE_BLOCKING
1161
1162
 
1163
 
1164
1165
Message buffer management
1166
 
1167
1168
Full CAN controllers often support more than one message buffer. These
1169
message buffers are often configurable for transmission or reception
1170
of certain CAN messages or as a remote buffers.  If a CAN hardware
1171
supports more than one message buffer then it is possible to configure
1172
the CAN hardware to receive only CAN messages with certain identifiers
1173
or to configure hardware support for remote buffers. If message
1174
filtering is done by hardware, the number of received CAN messages
1175
decreases and so also the time for processing received CAN messages
1176
and the memory required for buffering received messages
1177
decreases. This saves valuable memory and processing time.
1178
1179
 
1180
1181
The eCos CAN driver supports a generic way of adding message filters
1182
or remote buffers. By default the CAN driver is configured for
1183
reception of any kind of CAN standard and extended
1184
frames. Configuration of message buffers is done by
1185
calling cyg_io_set_config() with the config key
1186
1187
 
1188
1189
CYG_IO_SET_CONFIG_CAN_MSGBUF
1190
1191
 
1192
1193
and by exchanging cyg_can_msgbuf_cfg data structures.
1194
1195
 
1196
1197
typedef struct cyg_can_msgbox_cfg_st
1198
{
1199
    cyg_can_msgbuf_cfg_id cfg_id; // configuration id
1200
    cyg_can_msgbuf_handle handle; // handle to message buffer
1201
    cyg_can_message msg;          // CAN message - for configuration of buffer
1202
} cyg_can_msgbuf_cfg;
1203
1204
1205
   
1206
       cyg_can_msgbuf_cfg_id cfg_id
1207
            The cfg_id field
1208
contains the configuration ID that tells the driver what to do with a
1209
message buffer.
1210
          
1211
   
1212
    
1213
       cyg_can_msgbuf_handle handle
1214
           
1215
Contains a reference to a certain message buffer.
1216
          
1217
   
1218
    
1219
       cyg_can_message msg
1220
           
1221
Required for configuration of message buffer parameters.
1222
          
1223
   
1224
1225
 
1226
1227
The following configuration identifiers are supported:
1228
1229
 
1230
1231
CYGNUM_CAN_MSGBUF_RESET_ALL        // clears alle message buffers
1232
CYGNUM_CAN_MSGBUF_RX_FILTER_ALL    // cfg driver for reception of all can messages
1233
CYGNUM_CAN_MSGBUF_RX_FILTER_ADD    // add single message filter
1234
CYGNUM_CAN_MSGBUF_REMOTE_BUF_ADD   // add new remote response buffer
1235
CYGNUM_CAN_MSGBUF_REMOTE_BUF_WRITE // stores data into existing remote buffer
1236
1237
 
1238
1239
   
1240
       CYGNUM_CAN_MSGBUF_RESET_ALL
1241
           
1242
Clears all message buffers - no message will be received and all remote buffers are deleted.
1243
          
1244
   
1245
   
1246
       CYGNUM_CAN_MSGBUF_RX_FILTER_ALL
1247
           
1248
Configure driver for reception of all can messages
1249
          
1250
   
1251
   
1252
       CYGNUM_CAN_MSGBUF_RX_FILTER_ADD
1253
           
1254
Add single message filter.
1255
          
1256
   
1257
   
1258
       CYGNUM_CAN_MSGBUF_REMOTE_BUF_ADD
1259
           
1260
Add new remote response buffer.
1261
          
1262
   
1263
   
1264
       CYGNUM_CAN_MSGBUF_REMOTE_BUF_WRITE
1265
           
1266
Stores data into existing remote buffer (remote buffer handle required).
1267
          
1268
   
1269
1270
 
1271
1272
Example code for resetting all message buffers:
1273
1274
 
1275
1276
cyg_can_msgbuf_cfg msgbox_cfg;
1277
 
1278
msgbox_cfg.cfg_id = CYGNUM_CAN_MSGBUF_RESET_ALL;
1279
len = sizeof(msgbox_cfg);
1280
if (ENOERR != cyg_io_set_config(hDrvFlexCAN,
1281
                                CYG_IO_SET_CONFIG_CAN_MSGBUF,
1282
                                &msgbox_cfg, &len))
1283
{
1284
    // handle configuration error
1285
}
1286
1287
 
1288
 
1289
 
1290
1291
Remote frame response buffer configuration
1292
 
1293
1294
The remote frame is a message frame which is transmitted to request a
1295
data frame. Some CAN hardware generates receive interrupts when a
1296
remote transmission request arrives. Other CAN hardware, i.e.  the
1297
Motorola FlexCAN module, does not generate any receive
1298
interrupt. These CAN hardware chips like the FlexCAN module can be
1299
configured to transmit a data frame automatically in response to a
1300
remote frame. In order to support any kind of CAN hardware the eCos CAN
1301
driver provides a generic handling of remote transmission requests.
1302
1303
 
1304
1305
The transmission of the data frame in response to a remote frame is
1306
completely handled by the CAN driver.  If the hardware driver, like
1307
the driver for the FlexCAN module, supports hardware message buffers,
1308
then the response frame is automatically transmitted if a remote
1309
transmission request with a matching ID arrives.  If a CAN hardware
1310
does not provide hardware support for sending data frames in response
1311
to a remote frame, then this need to be implemented in software by the
1312
hardware device driver.
1313
1314
 
1315
1316
It is always possible to add remote response buffers. It does not
1317
matter if the driver is configured for reception of all CAN messages
1318
or if message filtering is used. As long as there are free message
1319
buffers available, it is possible to add remote response buffers.
1320
1321
 
1322
1323
In order to respond to a remote frame, a remote frame response buffer
1324
need to be initialized before a data frame can be sent in response to
1325
a remote frame. This is achieved by by
1326
exchanging cyg_can_remote_buf data structures with the
1327
driver via the cyg_io_set_config() function using
1328
the config key CYG_IO_SET_CONFIG_CAN_MSGBUF. Once
1329
the buffer is initialized, the CAN data can be changed at any time by
1330
the application.
1331
1332
 
1333
1334
typedef struct cyg_can_msgbuf_cfg_st
1335
{
1336
    cyg_can_msgbuf_cfg_id cfg_id; // configuration id
1337
    cyg_can_msgbuf_handle handle; // handle to message buffer
1338
    cyg_can_message msg;          // CAN message - for configuration of buffer
1339
} cyg_can_remote_buf;
1340
1341
1342
   
1343
       cyg_can_msgbuf_cfg_id cfg_id
1344
           
1345
The cfg_id field contains the configuration ID that tells the driver what to do with
1346
a message buffer (CYGNUM_CAN_MSGBUF_REMOTE_BUF_ADD or
1347
CYGNUM_CAN_MSGBUF_REMOTE_BUF_WRITE).
1348
          
1349
   
1350
    
1351
       cyg_can_msgbuf_handle handle
1352
           
1353
If there is no buffer initialized for this data, the value of the handle field need to be set to
1354
CYGNUM_CAN_MSGBUF_INIT. After the call to cyg_io_set_config()
1355
the handle field contains a valid remote buffer handle ( >= 0) or the value
1356
CYGNUM_CAN_MSGBUF_NA ( < 0) if no free buffer is available.
1357
          
1358
   
1359
    
1360
       cyg_can_message msg
1361
           
1362
The CAN frame that should be transmitted in response to a remote frame.
1363
          
1364
   
1365
1366
 
1367
1368
Example code for setting up a remote response buffer:
1369
1370
 
1371
1372
cyg_can_remote_buf rtr_buf;
1373
 
1374
// prepare the remote response buffer
1375
rtr_buf.cfg_id  = CYGNUM_CAN_MSGBUF_REMOTE_BUF_ADD;
1376
rtr_buf.handle  = CYGNUM_CAN_MSGBUF_INIT;
1377
rtr_buf.msg.id  = 0x7FF;
1378
rtr_buf.msg.ext = CYGNUM_CAN_ID_STD;
1379
rtr_buf.msg.rtr = CYGNUM_CAN_FRAME_DATA;
1380
rtr_buf.msg.dlc = 1;
1381
rtr_buf.msg.data[0] = 0xAB;
1382
 
1383
len = sizeof(rtr_buf);
1384
if (ENOERR != cyg_io_set_config(hDrvFlexCAN,
1385
                                CYG_IO_SET_CONFIG_CAN_MSGBUF,
1386
                                &rtr_buf, &len))
1387
{
1388
    // handle configuration error
1389
}
1390
 
1391
if (rtr_buf.handle == CYGNUM_CAN_MSGBUF_NA)
1392
{
1393
    // no free message buffer available - handle this problem here
1394
}
1395
 
1396
 
1397
// change CAN data for a buffer that is already initialized
1398
rtr_buf.cfg_id = CYGNUM_CAN_MSGBUF_REMOTE_BUF_WRITE;
1399
rtr_buf.msg.data[0] = 0x11;
1400
 
1401
len = sizeof(rtr_buf);
1402
if (ENOERR != cyg_io_set_config(hDrvFlexCAN,
1403
                                CYG_IO_SET_CONFIG_CAN_MSGBUF,
1404
                                &rtr_buf, &len))
1405
{
1406
    // handle configuration error
1407
}
1408
1409
 
1410
 
1411
 
1412
1413
Message filter configuration
1414
 
1415
1416
If message filtering is done by hardware the number of received CAN
1417
messages decreases and so also the time for processing received CAN
1418
messages and the memory required for buffering received messages
1419
decreases.  This saves valuable memory and processing time. The eCos
1420
CAN driver supports a generic way of adding message filters. By
1421
default the CAN driver is configured for reception of any kind of CAN
1422
standard and extended frames. As soon as a message filter is added,
1423
the CAN driver will only receive the CAN frames with the identifier of
1424
the CAN filter. By adding a number of message filters it is possible
1425
for the CAN hardware to receive an number of different CAN messages.
1426
1427
 
1428
1429
Adding message filters is only possible if driver is not configured
1430
for reception of all available CAN messages. If the driver is
1431
configured for reception of all CAN messages then message buffers need
1432
to be reset before adding single message filters.
1433
1434
 
1435
1436
In order to add a message filter, a message buffer need to be
1437
initialized. This is achieved by
1438
exchanging cyg_can_filter data structures with the driver
1439
via the cyg_io_set_config() function using the
1440
config key CYG_IO_SET_CONFIG_CAN_MSGBUF. Once the
1441
buffer is initialized, the CAN hardware can receive messages with the
1442
identifier of the filter.
1443
1444
 
1445
1446
typedef struct cyg_can_msgbox_cfg_st
1447
{
1448
    cyg_can_msgbuf_cfg_id cfg_id;
1449
    cyg_can_msgbuf_handle handle;
1450
    cyg_can_message       msg;
1451
} cyg_can_filter;
1452
1453
 
1454
1455
   
1456
       cyg_can_msgbuf_cfg_id cfg_id
1457
           
1458
The cfg_id field contains the configuration ID that tells the driver what to do with
1459
a message buffer.
1460
          
1461
   
1462
    
1463
       cyg_can_msgbuf_handle handle
1464
           
1465
After the call to cyg_io_set_config() the handle field contains a valid value
1466
( >= 0) or the value CYGNUM_CAN_MSGBUF_NA ( < 0) if no free buffer is available.
1467
          
1468
   
1469
    
1470
       cyg_can_message msg
1471
           
1472
The fields id and ext of the msg
1473
configure the type of message to receive by a certain message filter.
1474
          
1475
   
1476
1477
 
1478
1479
Before adding message filters the device should be stopped and after
1480
configuration it should be set into operational mode again.
1481
1482
 
1483
1484
Example code for setting up a message filter:
1485
1486
 
1487
1488
cyg_can_msgbuf_cfg msgbox_cfg;
1489
cyg_can_filter rx_filter;
1490
 
1491
// reset all message buffers
1492
msgbox_cfg.cfg_id = CYGNUM_CAN_MSGBUF_RESET_ALL;
1493
len = sizeof(msgbox_cfg);
1494
if (ENOERR != cyg_io_set_config(hDrvFlexCAN,
1495
                                CYG_IO_SET_CONFIG_CAN_MSGBUF,
1496
                                &msgbox_cfg, &len))
1497
{
1498
    // handle configuration error
1499
}
1500
 
1501
// prepare the message filter
1502
rx_filter.cfg_id = CYGNUM_CAN_MSGBUF_RX_FILTER_ADD;
1503
rx_filter.msg.id  = 0x800;
1504
rx_filter.msg.ext = CYGNUM_CAN_ID_EXT;
1505
 
1506
len = sizeof(rx_filter);
1507
if (ENOERR != cyg_io_set_config(hDrvFlexCAN,
1508
                                CYG_IO_SET_CONFIG_CAN_MSGBUF,
1509
                                &rx_filter, &len))
1510
{
1511
    // handle configuration error;
1512
}
1513
else if (CYGNUM_CAN_MSGBUF_NA == rx_filter.handle)
1514
{
1515
    // no free message buffer available - handle this problem here
1516
}
1517
1518
 
1519
 
1520
 
1521
1522
Message filter deactivation
1523
 
1524
1525
After startup of your device the CAN driver is configured for
1526
reception of all available CAN messages.  If you change this
1527
configuration by adding single message filters then you can reset this
1528
default state with the configuration ID:
1529
1530
 
1531
1532
CYGNUM_CAN_MSGBUF_RX_FILTER_ALL
1533
1534
 
1535
1536
This message buffer configuration id will clear all message filters
1537
and remote buffers and prepares the CAN hardware for reception of any
1538
kind of CAN standard and extended frames. It is not necessary to reset
1539
the message buffer configuration before this configuration step is
1540
executed because this should be done by device driver.
1541
1542
 
1543
1544
Example code for deactivation of message filtering:
1545
1546
 
1547
1548
cyg_can_filter rx_filter;
1549
 
1550
// now setup a RX all configuration
1551
rx_filter.cfg_id = CYGNUM_CAN_MSGBUF_RX_FILTER_ALL;
1552
len = sizeof(rx_filter);
1553
if (ENOERR != cyg_io_set_config(hDrvFlexCAN,
1554
                                CYG_IO_SET_CONFIG_CAN_MSGBUF,
1555
                                &rx_filter, &len))
1556
{
1557
    CYG_TEST_FAIL_FINISH("Error writing config of /dev/can0");
1558
}
1559
1560
 
1561
 
1562
1563
Message filtering using identifier ranges
1564
 
1565
1566
If the low level driver supports it, you can filter messages using
1567
identifier ranges. Such filtering is interesting if it is directly
1568
supported by the CAN controller hardware. Instead of waiting for a
1569
particular message identifier, the application can define one or more
1570
ranges of identifiers. If a received message has an identifier value
1571
(and type) matching a defined identifier range, then the message
1572
passes the filter and is made available to the application.
1573
1574
 
1575
1576
To add such a filter, use this configuration ID:
1577
1578
 
1579
1580
CYG_IO_SET_CONFIG_CAN_RANGE_FILTER
1581
1582
 
1583
1584
The buffer argument given to cyg_io_set_config()
1585
must point to an area holding the following structure:
1586
1587
 
1588
1589
typedef struct cyg_can_filter_range_cfg_st
1590
{
1591
    cyg_can_id_type        ext;            // type of identifier concerned
1592
    cyg_uint32             lower_id_bound; // lower bound identifier (included)
1593
    cyg_uint32             upper_id_bound; // upper bound identifier (included)
1594
} cyg_can_filter_range_cfg;
1595
1596
 
1597
1598
   
1599
       cyg_can_id_type ext
1600
            Extended ID. If this field
1601
is CYGNUM_CAN_ID_EXT then the next two fields contains
1602
29 bit extended ID. If ext
1603
contains CYGNUM_CAN_ID_STD then the next two fields
1604
represent 11 bits identifiers.
1605
          
1606
   
1607
   
1608
       cyg_uint32 lower_id_bound
1609
           
1610
Combined with upper_id_bound an identifier range is
1611
defined. All messages having an identifier of the type defined by
1612
ext and included in the range lower_id_bound
1613
to upper_id_bound will pass the filter. The identifiers
1614
lower_id_bound and upper_id_bound are
1615
included in the defined range.
1616
          
1617
   
1618
   
1619
       cyg_uint32 upper_id_bound
1620
           
1621
Upper identifier value of the defined range.
1622
          
1623
   
1624
1625
 
1626
 
1627
1628
Message filtering using identifier masks
1629
 
1630
1631
If the low level driver supports it, you can filter messages using
1632
identifier masks. Such filtering is interesting if it is directly
1633
supported by the CAN controller hardware. Instead of waiting for a
1634
particular message identifier, the application can define one or more
1635
pair of identifier and mask. If a received message has an identifier
1636
value (and type) that, for each bit set of the mask, matches the
1637
corresponding bit in the provided identifier, then the message passes
1638
the filter and is made available to the application.
1639
1640
 
1641
1642
To add such a filter, use this configuration ID:
1643
1644
 
1645
1646
CYG_IO_SET_CONFIG_CAN_MASK_FILTER
1647
1648
 
1649
1650
The buffer argument given to cyg_io_set_config()
1651
must point to an area holding the following structure:
1652
1653
 
1654
1655
typedef struct cyg_can_filter_mask_cfg_st
1656
{
1657
    cyg_can_id_type        ext;            // type of identifier concerned
1658
    cyg_uint32             id;             // identifier to use for filtering
1659
    cyg_uint32             mask;           // mask to apply for filtering
1660
} cyg_can_filter_mask_cfg;
1661
1662
 
1663
1664
   
1665
       cyg_can_id_type ext
1666
           
1667
Extended ID. If this field is CYGNUM_CAN_ID_EXT then the
1668
id field contains a 29 bit extended ID. If it
1669
contains CYGNUM_CAN_ID_STD then the ID is 11 bits.
1670
          
1671
   
1672
   
1673
       cyg_uint32 id
1674
           
1675
Message ID. This is the ID to be matched with an incoming message, after
1676
having considered the mask field.
1677
          
1678
   
1679
   
1680
       cyg_uint32 mask
1681
           
1682
Mask value. A message will pass the filter if, for each bit set in
1683
mask, the received message ID has its corresponding
1684
bit equals to the corresponding bit in the id field.
1685
          
1686
   
1687
1688
 
1689
1690
For instance let's suppose that the id field is
1691
0x05 (bits 0 and 2 are set) and the mask field is
1692
0x07 (bits 0, 1 and 2 are set). If an incoming message has an ID of
1693
0x01: bit 0 matches since bit 0 is set in the mask and both the
1694
incoming message and the id field have a similar
1695
value for bit 0. The message ID has its bit 1 set to 0, as
1696
the id field and the mask tells the controller to
1697
check this bit: it passes too. However the message ID has its bit 2
1698
unset, while bit 2 of the mask tells the controller to check bit
1699
2. Bit 2 of the identifier isn't set in the id
1700
field, hence this example message does not pass the filter.
1701
1702
 
1703
 
1704
 
1705
1706
Configuring a callback on events
1707
 
1708
1709
By default application cannot get information about an event arriving
1710
in the RX buffer until it calls
1711
the cyg_io_read().  Usually this leads applications
1712
to use accessory threads to wait for new CAN events.
1713
1714
 
1715
1716
The CDL option CYGOPT_IO_CAN_SUPPORT_CALLBACK
1717
allows application to use a callback on event arrival. It is
1718
configured by passing a cyg_can_callback_cfg
1719
data structure to the driver via
1720
the cyg_io_set_config() function using the config
1721
key
1722
CYG_IO_SET_CONFIG_CAN_CALLBACK.
1723
1724
 
1725
1726
CYG_IO_SET_CONFIG_CAN_CALLBACK
1727
1728
 
1729
1730
typedef void (*cyg_can_event_cb_t)(cyg_uint16, CYG_ADDRWORD);
1731
 
1732
typedef struct cyg_can_callback_cfg_st
1733
{
1734
    cyg_can_event_cb_t callback_func;   // callback function
1735
    cyg_can_event_flags_t flag_mask;  // flags mask
1736
    CYG_ADDRWORD data;                  // data passed to callback
1737
} cyg_can_callback_cfg;
1738
1739
 
1740
1741
   
1742
       cyg_can_event_cb_t callback_func
1743
           
1744
Pointer to the callback function. The function will be called from DSR context so
1745
you should be careful to only call API functions that are safe in DSR
1746
context.  The first parameter is a combination of event flags for events that have
1747
occurred. Second parameter is a user defined data pointer or value.
1748
          
1749
   
1750
   
1751
       CYG_ADDRWORD data
1752
           
1753
Additional user data that will be passed to callback function as a second parameter.
1754
          
1755
   
1756
   
1757
       cyg_can_event_flags_t flag_mask
1758
           
1759
Should be set with a combination
1760
of CYGNUM_CAN_EVENT_* flags.  If one of these
1761
events happens, the callback function will be called, with the
1762
actually event flags passed as a parameter.  To disable the callback
1763
function from being called set flag_mask to 0. To
1764
set all possible flags use the CYGNUM_CAN_EVENT_ALL macro.
1765
          
1766
   
1767
1768
 
1769
1770
Instead of using a thread dedicated to reading CAN events, it is
1771
possible, if the CAN controller I/O handle was set in non-blocking
1772
mode for read operations, to have the callback function to read each
1773
event it is waiting for. However the callback function runs in DSR
1774
mode, so it must be carefully written to avoid any blocking call.
1775
1776
 
1777
1778
If you plan to have the callback function to perform read operations,
1779
be aware that cyg_io_read() retrieves events of
1780
all kinds while the callback function is triggered only on events it
1781
is expecting, as defined by the flag_mask
1782
field. The side effect is that the callback function, if it is not
1783
waiting for all events, may see its first parameter (the flag(s)
1784
describing why the callback function is called) different from an
1785
event it get from cyg_io_read().
1786
1787
 
1788
1789
For instance, let's suppose the callback function is expecting
1790
only CYGNUM_CAN_EVENT_RX events while the bus
1791
activity triggers other kind of events, like
1792
the CYGNUM_CAN_EVENT_WARNING_RX event. It is
1793
possible to have in the receive queue a first event of
1794
type CYGNUM_CAN_EVENT_WARNING_RX followed by a
1795
second event of type CYGNUM_CAN_EVENT_RX. In that
1796
case, the callback function is triggered and have its first parameter
1797
set to CYGNUM_CAN_EVENT_RX exactly
1798
when CYGNUM_CAN_EVENT_RX occurs, but if the
1799
callback function reads the event queue, it will first get
1800
the CYGNUM_CAN_EVENT_WARNING_RX event.
1801
1802
 
1803
1804
If the callback function is set to process all kind of events and
1805
always call cyg_io_read() to get each event, it
1806
is possible to have a receive queue size of one event.
1807
1808
 
1809
1810
If the CAN controller does not provide message timestamps of its own,
1811
or if the provided timestamps do not match your needs, the callback
1812
function mechanism can be helpful since it is called from the DSR
1813
processing the hardware related events. The callback function can
1814
manage timestamps in the way that suits you the best however there is
1815
a slight delay between an event related to the CAN bus activity occurs
1816
and the time the callback function runs. If you implement your own
1817
timestamps this way, pay also attention to what gives you the current
1818
time. For instance the granularity
1819
of cyg_current_time() may not be accurate enough,
1820
according to the clock resolution and your needs of accuracy.
1821
1822
 
1823
 
1824
 
1825
1826
1827
 
1828
1829
Configuration
1830
 
1831
1832
The CAN subsystem has a number of configuration options.
1833
1834
 
1835
1836
   
1837
       cdl_interface CYGINT_IO_CAN_TIMESTAMP
1838
           
1839
A hardware device driver that supports timestamps should implement this interface.
1840
          
1841
   
1842
   
1843
       cdl_option CYGOPT_IO_CAN_SUPPORT_TIMESTAMP
1844
           
1845
If the CAN hardware driver supports some kind of timestamps then this option enables
1846
propagation of timestamps to higher layers. This may add some extra code to hardware
1847
drivers.
1848
          
1849
   
1850
   
1851
       cdl_option CYGOPT_IO_CAN_TX_EVENT_SUPPORT
1852
           
1853
 This option enables support for TX
1854
events. If a CAN message is transmitted successfully a TX event will
1855
be inserted into the receive event queue and propagated to higher
1856
layers.  If this option is enabled the RX event queue will be filled
1857
faster.
1858
1859
   
1860
   
1861
       cdl_option CYGOPT_IO_CAN_SUPPORT_NONBLOCKING
1862
           
1863
1864
This option enables extra code in the generic CAN driver which allows
1865
clients to switch read() and write() call semantics from blocking to
1866
non-blocking.
1867
1868
   
1869
   
1870
       cdl_option CYGOPT_IO_CAN_SUPPORT_CALLBACK
1871
           
1872
1873
This option enables extra code in the generic CAN driver which allows
1874
an application to register a callback for events. The callback function
1875
is called from DSR context so you should be careful to only call API
1876
functions that are safe in DSR context.
1877
1878
   
1879
   
1880
       cdl_option CYGNUM_IO_CAN_DEFAULT_TIMEOUT_READ
1881
           
1882
The initial timeout value in clock ticks for cyg_io_read() calls.
1883
          
1884
   
1885
   
1886
       cdl_option CYGNUM_IO_CAN_DEFAULT_TIMEOUT_WRITE
1887
            The initial timeout value in clock ticks
1888
for cyg_io_write() calls.
1889
          
1890
   
1891
1892
1893
 
1894
 
1895
1896
Writing a CAN hardware device driver
1897
 
1898
1899
A CAN driver is nothing more than a named entity that supports the
1900
basic I/O functions - read, write, get config, and set config. The
1901
device driver uses and manages interrupts from the device. While the
1902
interface is generic and device driver independent, the actual driver
1903
implementation is completely up to the device driver designer.
1904
1905
 
1906
1907
That said, the reason for using a device driver is to provide access
1908
to a CAN device from application code in as general purpose a fashion
1909
as reasonable. Most driver writers are also concerned with making this
1910
access as simple as possible while being as efficient as possible.
1911
1912
 
1913
1914
Like other device drivers the CAN device driver is concerned with the
1915
movement of information - the CAN messages. In order to make the most
1916
efficient use of system resources, interrupts are used.  This will
1917
allow other application processing to take place while the data
1918
transfers are under way, with interrupts used to indicate when various
1919
events have occurred. For example, a CAN device typically generates an
1920
interrupt after a CAN message has been sent or a CAN message has been
1921
received by a CAN hardware message buffer. It makes sense to allow
1922
further application processing while the data is being sent since this
1923
can take quite a long time. The interrupt can be used to allow the
1924
driver to send a CAN message as soon as the current one is complete,
1925
without any active participation by the application code.
1926
1927
 
1928
1929
The main building blocks for CAN device drivers are found in the
1930
include files
1931
<cyg/io/devtab.h> and <cyg/io/can.h>
1932
1933
 
1934
1935
Like many other device drivers in eCos, CAN device drivers are described by a device
1936
table entry, using the cyg_devtab_entry_t type. The entry should be created using
1937
the DEVTAB_ENTRY() macro.
1938
1939
 
1940
 
1941
1942
How to Write a CAN Hardware Interface Driver
1943
 
1944
1945
The standard CAN driver supplied with eCos is structured as a hardware
1946
independent portion and a hardware dependent interface module. To add
1947
support for a new CAN device, the user should be able to use the
1948
existing hardware independent portion and just add their own interface
1949
driver which handles the details of the actual device. The user should
1950
have no need to change the hardware independent portion.
1951
1952
 
1953
1954
The interfaces used by the CAN driver and CAN implementation modules
1955
are contained in the file <cyg/io/can.h>.
1956
1957
 
1958
1959
DevTab Entry
1960
 
1961
1962
The interface module contains the devtab entry (or entries if a single
1963
module supports more than one interface). This entry should have the
1964
form:
1965
1966
 
1967
1968
DEVTAB_ENTRY(<<module_name>>,
1969
             <<device_name>>,
1970
             0,
1971
             &can_devio,
1972
             <<module_init>>,
1973
             <<module_lookup>>,
1974
             &<<can_channel>>
1975
            );
1976
1977
 
1978
1979
Arguments
1980
  
1981
    module_name
1982
    The "C" label for this devtab entry
1983
  
1984
  
1985
    device_name
1986
    The "C" string for the
1987
    device. E.g. /dev/can0.
1988
  
1989
  
1990
    can_devio
1991
    The table of I/O functions. This set is defined in
1992
    the hardware independent CAN driver and should be used.
1993
    
1994
  
1995
  
1996
    module_init
1997
    The hardware module initialization function.
1998
  
1999
  
2000
    module_lookup
2001
    The device lookup function. This function
2002
    typically sets up the CAN device for actual use, turning on
2003
    interrupts, configuring the message buffers, etc.
2004
  
2005
  
2006
    can_channel
2007
    This table (defined below) contains the interface
2008
    between the interface module and the CAN driver proper.
2009
  
2010
2011
 
2012
2013
Example devtab entry for Motorola FlexCAN device driver:
2014
2015
 
2016
2017
DEVTAB_ENTRY(flexcan_devtab,
2018
             CYGDAT_DEVS_CAN_MCF52xx_FLEXCAN0_NAME,
2019
             0,                     // Does not depend on a lower level interface
2020
             &cyg_io_can_devio,
2021
             flexcan_init,
2022
             flexcan_lookup,        // CAN driver may need initializing
2023
             &flexcan_can0_chan
2024
    );
2025
2026
2027
 
2028
 
2029
2030
CAN Channel Structure
2031
 
2032
2033
Each CAN device must have a “CAN channel”.
2034
This is a set of data which describes all operations on the device.
2035
It also contains buffers, etc. The CAN channel is created by the macro:
2036
2037
 
2038
2039
CAN_CHANNEL_USING_INTERRUPTS(l, funs, dev_priv, baud,
2040
                             out_buf, out_buflen,
2041
                             in_buf,  in_buflen)
2042
2043
 
2044
2045
  Arguments
2046
  
2047
    l
2048
    The "C" label for this structure.
2049
  
2050
  
2051
    funs
2052
    The set of interface functions (see below).
2053
  
2054
  
2055
    dev_priv
2056
    A placeholder for any device specific data for
2057
    this channel.
2058
  
2059
  
2060
    baud
2061
    The initial baud rate value
2062
    (cyg_can_baud_rate_t).
2063
  
2064
  
2065
    out_buf
2066
    Pointer to the output buffer
2067
  
2068
  
2069
    out_buflen
2070
    The length of the output buffer.
2071
  
2072
  
2073
    in_buf
2074
    pointer to the input buffer.
2075
  
2076
  
2077
    in_buflen
2078
    The length of the input buffer.
2079
  
2080
2081
 
2082
2083
Example CAN channel implementation for Motorola FlexCAN device driver:
2084
2085
 
2086
2087
CAN_CHANNEL_USING_INTERRUPTS(
2088
    flexcan_can0_chan,
2089
    flexcan_lowlevel_funs,
2090
    flexcan_can0_info,
2091
    CYG_CAN_BAUD_RATE(CYGNUM_DEVS_CAN_MCF52xx_FLEXCAN0_KBAUD),
2092
    flexcan_can0_txbuf, CYGNUM_DEVS_CAN_MCF52xx_FLEXCAN0_QUEUESIZE_TX,
2093
    flexcan_can0_rxbuf, CYGNUM_DEVS_CAN_MCF52xx_FLEXCAN0_QUEUESIZE_RX
2094
);
2095
2096
 
2097
2098
The interface from the hardware independent driver into the hardware
2099
interface module is contained in the funs
2100
table.  This is defined by the macro:
2101
2102
2103
 
2104
2105
CAN Lowlevel Functions Structure
2106
 
2107
2108
CAN_LOWLEVEL_FUNS(l, putmsg, getevent, get_config, set_config, start_xmit, stop_xmit)
2109
2110
 
2111
2112
  Arguments
2113
  
2114
    l
2115
    The "C" label for this structure.
2116
  
2117
  
2118
    putmsg
2119
    
2120
      
2121
      bool (*putmsg)(can_channel *priv, cyg_can_message *pmsg, void *pdata)
2122
      
2123
      
2124
      This function sends one CAN message to the interface. It should
2125
      return true if the message is actually
2126
      consumed. It should return false if there is
2127
      no space in the interface
2128
      
2129
    
2130
  
2131
  
2132
    getevent
2133
    
2134
      
2135
      bool (*getevent)(can_channel *priv, cyg_can_event *pevent, void *pdata)
2136
      
2137
      
2138
      This function fetches one event from the interface.
2139
      
2140
    
2141
  
2142
  
2143
    get_config
2144
    
2145
      
2146
      Cyg_ErrNo (*get_config)(can_channel *priv, cyg_uint32 key, const void *xbuf, cyg_uint32 *len)
2147
      
2148
      
2149
        This function is used to query the configuration of a CAN channel.
2150
      
2151
    
2152
  
2153
  
2154
    set_config
2155
    
2156
      
2157
      Cyg_ErrNo (*set_config)(can_channel *priv, cyg_uint32 key, const void *xbuf, cyg_uint32 *len)
2158
      
2159
      
2160
        This function is used to change configuration of a CAN channel.
2161
      
2162
    
2163
  
2164
  
2165
    start_xmit
2166
    void (*start_xmit)(can_channel *priv)
2167
      
2168
        Enable the transmit channel and turn on transmit interrupts.
2169
      
2170
    
2171
  
2172
  
2173
    stop_xmit
2174
    
2175
      void (*stop_xmit)(can_channel *priv)
2176
      Disable the transmit channel and turn transmit interrupts off.
2177
    
2178
  
2179
2180
 
2181
2182
Example implementation of low level function structure for Motorola FlexCAN
2183
device driver:
2184
2185
 
2186
2187
CAN_LOWLEVEL_FUNS(flexcan_lowlevel_funs,
2188
                  flexcan_putmsg,
2189
                  flexcan_getevent,
2190
                  flexcan_get_config,
2191
                  flexcan_set_config,
2192
                  flexcan_start_xmit,
2193
                  flexcan_stop_xmit
2194
     );
2195
2196
2197
 
2198
2199
Callbacks
2200
 
2201
2202
The device interface module can execute functions in the
2203
hardware independent driver via chan->callbacks.
2204
These functions are available:
2205
2206
 
2207
2208
void (*can_init)(can_channel *chan)
2209
2210
 
2211
This function is used to initialize the CAN channel.
2212
 
2213
2214
cyg_bool (*xmt_msg)(can_channel *chan, void *pdata)
2215
2216
 
2217
2218
This function would be called from an interrupt handler after a
2219
transmit interrupt indicating that additional messages may be
2220
sent. The upper driver will call the putmsg
2221
function as appropriate to send more data to the
2222
device. xmt_msg() returns true
2223
if a message has been provided to the low level
2224
driver, false otherwise.
2225
2226
 
2227
2228
cyg_bool (*rcv_event)(can_channel *chan, void *pdata)
2229
2230
 
2231
2232
This function is used to tell the driver that a message has arrived at
2233
the interface or that an event has occurred. This function is
2234
typically called from the interrupt
2235
handler. rcv_event()
2236
returns true if an event has been provided by the
2237
low level driver, false otherwise.
2238
2239
2240
 
2241
2242
2243
 
2244

powered by: WebSVN 2.1.0

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