hsk_libs-dev
163:b63ae088cc97
High Speed Karlsruhe XC878 library collection
Main Page
Related Pages
Modules
Data Structures
Files
File List
Globals
All
Data Structures
Files
Functions
Variables
Typedefs
Macros
Groups
Pages
hsk_can.h
Go to the documentation of this file.
1
/** \file
2
* HSK Controller Area Network headers
3
*
4
* This file contains the function prototypes to initialize and engage in
5
* CAN communication over the builtin CAN nodes 0 and 1.
6
*
7
* @author kami
8
*
9
* \section tuples CAN Message/Signal Tuples
10
*
11
* The recommended way to use messages and signals is not to specify them
12
* inline, but to provide defines with a set of parameters.
13
*
14
* These tupples should follow the following pattern:
15
* \code
16
* #define MSG_<MSGNAME> <id>, <extended>, <dlc>
17
* #define SIG_<SIGNAME> <endian>, <bitPos>, <bitCount>
18
* \endcode
19
*
20
* The symbols have the following meaning:
21
* - MSGNAME: The name of the message in capitals, e.g. AFB_CHANNELS
22
* - id: The CAN id of the message, e.g. 0x403
23
* - extended: Whether the CAN ID is extended or not, e.g. 0 for a
24
* regular ID
25
* - dlc: The data length count of the message, e.g. 3
26
* - SIGNAME: The name of the signal in capitals, e.g.
27
* AFB_CHANNEL0_CURRENT
28
* - endian: Whether the signal is in little or big endian format,
29
* e.g. CAN_ENDIAN_INTEL
30
* - bitPos: The starting bit of the signal, e.g. 0
31
* - bitCount: The length of the signal in bits, e.g. 10
32
*
33
* Tuples using the specified format can directly be used as parameters
34
* for several functions in the library.
35
*/
36
37
#ifndef _HSK_CAN_H_
38
#define _HSK_CAN_H_
39
40
/**
41
* Value returned by functions in case of an error.
42
*/
43
#define CAN_ERROR 0xff
44
45
/**
46
* CAN node 0.
47
*/
48
#define CAN0 0
49
50
/**
51
* CAN node 1.
52
*/
53
#define CAN1 1
54
55
/**
56
* CAN node 0 IO RX on P1.0, TX on P1.1.
57
*/
58
#define CAN0_IO_P10_P11 0
59
60
/**
61
* CAN node 0 IO RX on P1.6, TX on P1.7.
62
*/
63
#define CAN0_IO_P16_P17 1
64
65
/**
66
* CAN node 0 IO RX on P3.4, TX on P3.5.
67
*/
68
#define CAN0_IO_P34_P35 2
69
70
/**
71
* CAN node 0 IO RX on P4.0, TX on P4.1.
72
*/
73
#define CAN0_IO_P40_P41 3
74
75
/**
76
* CAN node 1 IO RX on P0.1, TX on P0.2.
77
*/
78
#define CAN1_IO_P01_P02 4
79
80
/**
81
* CAN node 1 IO RX on P1.4, TX on P1.3.
82
*/
83
#define CAN1_IO_P14_P13 5
84
85
/**
86
* CAN node 1 IO RX on P3.2, TX on P3.3.
87
*/
88
#define CAN1_IO_P32_P33 6
89
90
/**
91
* Little endian signal encoding.
92
*/
93
#define CAN_ENDIAN_INTEL 0
94
95
/**
96
* Big endian signal encoding.
97
*/
98
#define CAN_ENDIAN_MOTOROLA 1
99
100
/**
101
* CAN node identifiers.
102
*/
103
typedef
ubyte
hsk_can_node
;
104
105
/**
106
* CAN message object identifiers.
107
*/
108
typedef
ubyte
hsk_can_msg
;
109
110
/**
111
* CAN message FIFO identifiers.
112
*/
113
typedef
ubyte
hsk_can_fifo
;
114
115
/** \file
116
* \section nodes CAN Node Management
117
*
118
* There are 7 port pairs available for CAN communication, check the CANn_IO_*
119
* defines. Four for the node CAN0 and three for CAN1.
120
*/
121
122
/**
123
* Setup CAN communication with the desired baud rate.
124
*
125
* The CAN node is chosen with the pin configuration.
126
*
127
* The bus still needs to be enabled after being setup.
128
*
129
* @param pins
130
* Choose one of 7 CANn_IO_* configurations
131
* @param baud
132
* The target baud rate to use
133
*/
134
void
hsk_can_init
(
const
ubyte pins,
const
ulong __xdata baud);
135
136
/**
137
* Go live on the CAN bus.
138
*
139
* To be called when everything is set up.
140
*
141
* @param node
142
* The CAN node to enable
143
*/
144
void
hsk_can_enable
(
const
hsk_can_node
node);
145
146
/**
147
* Disable a CAN node.
148
*
149
* This completely shuts down a CAN node, cutting it off from the
150
* internal clock, to reduce energy consumption.
151
*
152
* @param node
153
* The CAN node to disable
154
*/
155
void
hsk_can_disable
(
const
hsk_can_node
node);
156
157
/**
158
* \defgroup CAN_STATUS CAN Node Status Fields
159
*
160
* This group of defines specifies status fields that can be queried from
161
* hsk_can_status().
162
*
163
* @{
164
*/
165
166
/**
167
* The Last Error Code field provides the error triggered by the last message
168
* on the bus.
169
*
170
* For details check table 16-8 from the User Manual 1.1.
171
*
172
* @retval 0
173
* No Error
174
* @retval 1
175
* Stuff Error, 5 consecutive bits of the same value are stuffed, this
176
* error is triggered when the stuff bit is missing
177
* @retval 2
178
* Form Error, the frame format was violated
179
* @retval 3
180
* Ack Error, the message was not acknowledged, maybe nobody else
181
* is on the bus
182
* @retval 4
183
* Bit1 Error, a recessive (1) bit was sent out of sync
184
* @retval 5
185
* Bit0 Error, a recessive (1) bit won against a dominant (0) bit
186
* @retval 6
187
* CRC Error, wrong checksum for a received message
188
*/
189
#define CAN_STATUS_LEC 0
190
191
/**
192
* Message Transmitted Successfully.
193
*
194
* @retval 0
195
* No successful transmission since TXOK was queried last time
196
* @retval 1
197
* A message was transmitted and acknowledged successfully
198
*/
199
#define CAN_STATUS_TXOK 1
200
201
/**
202
* Message Received Successfully.
203
*
204
* @retval 0
205
* No successful receptions since the last time this field was queried
206
* @retval 1
207
* A message was received successfully
208
*/
209
#define CAN_STATUS_RXOK 2
210
211
/**
212
* Alert Warning.
213
*
214
* @retval 0
215
* No warnings
216
* @retval 1
217
* One of the following error conditions applies: \ref CAN_STATUS_EWRN;
218
* \ref CAN_STATUS_BOFF
219
*/
220
#define CAN_STATUS_ALERT 3
221
222
/**
223
* Error Warning Status.
224
*
225
* @retval 0
226
* No error warnings exceeded
227
* @retval 1
228
* An error counter has exceeded the warning level of 96
229
*/
230
#define CAN_STATUS_EWRN 4
231
232
/**
233
* Bus-off Status
234
*
235
* @retval 0
236
* The bus is not off
237
* @retval 1
238
* The bus is turned off due to an error counter exceeding 256
239
*/
240
#define CAN_STATUS_BOFF 5
241
242
/**
243
* @}
244
*/
245
246
/**
247
* Returns a status field of a CAN node.
248
*
249
* @param node
250
* The CAN node to return the status of
251
* @param field
252
* The status field to select
253
* @return
254
* The status field state
255
* @see \ref CAN_STATUS
256
*/
257
ubyte
hsk_can_status
(
const
hsk_can_node
node,
const
ubyte field);
258
259
/** \file
260
* \section CAN Message Object Management
261
*
262
* The MultiCAN module offers up to 32 message objects. New messages are set
263
* up for receiving messages. Message object can be switched from RX to TX
264
* mode and back with the hsk_can_msg_send() and hsk_can_msg_receive()
265
* functions.
266
*/
267
268
/**
269
* Creates a new CAN message.
270
*
271
* Note that only up to 32 messages can exist at any given time.
272
*
273
* Extended messages have 29 bit IDs and non-extended 11 bit IDs.
274
*
275
* @param id
276
* The message ID.
277
* @param extended
278
* Set this to 1 for an extended CAN message.
279
* @param dlc
280
* The data length code, # of bytes in the message, valid values
281
* range from 0 to 8.
282
* @retval CAN_ERROR
283
* Creating the message failed
284
* @retval [0;32[
285
* A message identifier
286
*/
287
hsk_can_msg
hsk_can_msg_create
(
const
ulong
id
,
const
bool
extended,
288
const
ubyte dlc);
289
290
/**
291
* Connect a message object to a CAN node.
292
*
293
* @param msg
294
* The identifier of the message object
295
* @param node
296
* The CAN node to connect to
297
* @retval CAN_ERROR
298
* The given message is not valid
299
* @retval 0
300
* Success
301
*/
302
ubyte
hsk_can_msg_connect
(
const
hsk_can_msg
msg,
const
hsk_can_node
__xdata node);
303
304
/**
305
* Disconnect a CAN message object from its CAN node.
306
*
307
* This takes a CAN message out of active communication, without deleting
308
* it.
309
*
310
* @param msg
311
* The identifier of the message object
312
* @retval CAN_ERROR
313
* The given message is not valid
314
* @retval 0
315
* Success
316
*/
317
ubyte
hsk_can_msg_disconnect
(
const
hsk_can_msg
msg);
318
319
/**
320
* Delete a CAN message object.
321
*
322
* @param msg
323
* The identifier of the message object
324
* @retval CAN_ERROR
325
* The given message is not valid
326
* @retval 0
327
* Success
328
*/
329
ubyte
hsk_can_msg_delete
(
const
hsk_can_msg
msg);
330
331
/**
332
* Gets the current data in the CAN message.
333
*
334
* This writes DLC bytes from the CAN message object into msgdata.
335
*
336
* @param msg
337
* The identifier of the message object
338
* @param msgdata
339
* The character array to store the message data in
340
*/
341
void
hsk_can_msg_getData
(
const
hsk_can_msg
msg,
342
ubyte *
const
msgdata);
343
344
/**
345
* Sets the current data in the CAN message.
346
*
347
* This writes DLC bytes from msgdata to the CAN message object.
348
*
349
* @param msg
350
* The identifier of the message object
351
* @param msgdata
352
* The character array to get the message data from
353
*/
354
void
hsk_can_msg_setData
(
const
hsk_can_msg
msg,
355
const
ubyte *
const
msgdata);
356
357
/**
358
* Request transmission of a message.
359
*
360
* @param msg
361
* The identifier of the message to send
362
*/
363
void
hsk_can_msg_send
(
const
hsk_can_msg
msg);
364
365
366
/**
367
* Return whether the message was successfully sent between this and the
368
* previous call of this method.
369
*
370
* @param msg
371
* The identifier of the message to check
372
* @retval 1
373
* The message was sent since the last call of this function
374
* @retval 0
375
* The message has not been sent since the last call of this function
376
*/
377
bool
hsk_can_msg_sent
(
const
hsk_can_msg
msg);
378
379
/**
380
* Return the message into RX mode after sending a message.
381
*
382
* After sending a message the messages with the same ID from other
383
* bus participants are ignored. This restores the original setting to receive
384
* messages.
385
*
386
* @param msg
387
* The identifier of the message to receive
388
*/
389
void
hsk_can_msg_receive
(
const
hsk_can_msg
msg);
390
391
/**
392
* Return whether the message was updated via CAN bus between this call and
393
* the previous call of this method.
394
*
395
* An update does not entail a change of message data. It just means the
396
* message was received on the CAN bus.
397
*
398
* This is useful for cyclic message occurance checks.
399
*
400
* @param msg
401
* The identifier of the message to check
402
* @retval 1
403
* The message was updated since the last call of this function
404
* @retval 0
405
* The message has not been updated since the last call of this function
406
*/
407
bool
hsk_can_msg_updated
(
const
hsk_can_msg
msg);
408
409
/** \file
410
* \section fifos FIFOs
411
*
412
* FIFOs are the weapon of choice when dealing with large numbers of
413
* individual messages or when receving multiplexed data. In most use cases
414
* only the latest version of a message is relevant and FIFOs are not
415
* required. But messages containing multiplexed signals may contain critical
416
* signals that would be overwritten by a message with the same ID, but a
417
* different multiplexor.
418
*
419
* If more message IDs than available message objects are used to send and/or
420
* receive data, there is no choice but to use a FIFO.
421
*
422
* Currently only RX FIFOs are supported.
423
*
424
* A FIFO can act as a buffer the CAN module can store message data in until
425
* it can be dealt with. The following example illustrates how to read from
426
* a FIFO:
427
* \code
428
* if (hsk_can_fifo_updated(fifo0)) {
429
* hsk_can_fifo_getData(fifo0, data0);
430
* hsk_can_fifo_next(fifo0);
431
* select = hsk_can_data_getSignal(data0, SIG_MULTIPLEXOR);
432
* [...]
433
* }
434
* \endcode
435
*
436
* When using a mask to accept several messages checking the ID becomes
437
* necessary:
438
* \code
439
* if (hsk_can_fifo_updated(fifo0)) {
440
* switch (hsk_can_fifo_getId()) {
441
* case MSG_0_ID:
442
* hsk_can_fifo_getData(fifo0, data0);
443
* [...]
444
* break;
445
* case MSG_1_ID:
446
* hsk_can_fifo_getData(fifo0, data1);
447
* [...]
448
* break;
449
* [...]
450
* }
451
* hsk_can_fifo_next(fifo0);
452
* }
453
* \endcode
454
*
455
*
456
* FIFOs draw from the same message object pool regular message objects do.
457
*/
458
459
/**
460
* Creates a message FIFO.
461
*
462
* FIFOs can be used to ensure that multiplexed signals are not lost.
463
*
464
* For receiving multiplexed signals it is recommended to use a FIFO as large
465
* as the number of multiplexed messages that might occur in a single burst.
466
*
467
* If the multiplexor is large, e.g. 8 bits, it's obviously not possible to
468
* carve a 256 messages FIFO out of 32 message objects. Make an educated
469
* guess and hope that the signal provider is not hostile.
470
*
471
* If the number of available message objects is at least one, but less than
472
* the requested length this function succeeds, but the FIFO is only created
473
* as long as possible.
474
*
475
* @param size
476
* The desired FIFO size
477
* @retval CAN_ERROR
478
* Creating the FIFO failed
479
* @retval [0;32[
480
* The created FIFO id
481
*/
482
hsk_can_fifo
hsk_can_fifo_create
(ubyte
size
);
483
484
/**
485
* Set the FIFO up for receiving messages.
486
*
487
* @param fifo
488
* The FIFO to setup
489
* @param id
490
* The message ID.
491
* @param extended
492
* Set this to 1 for an extended CAN message
493
* @param dlc
494
* The data length code, # of bytes in the message, valid values
495
* range from 0 to 8
496
*/
497
void
hsk_can_fifo_setupRx
(
hsk_can_fifo
fifo,
const
ulong
id
,
498
const
bool
extended,
const
ubyte dlc);
499
500
/**
501
* Changes the ID matching mask of an RX FIFO.
502
*
503
* Every RX FIFO is setup to receive only on complete ID matches. This
504
* function allows updating the mask.
505
*
506
* To generate a mask from a list of IDs use the following formula:
507
* \f[ msk = \sim(id_0 | id_1 | ... | id_n) | (id_0 \& id_1 \& ... \& id_n) \f]
508
*
509
* @pre hsk_can_fifo_setupRx()
510
* @param fifo
511
* The FIFO to change the RX mask for
512
* @param msk
513
* The bit mask to set for the FIFO
514
*/
515
void
hsk_can_fifo_setRxMask
(
const
hsk_can_fifo
fifo, ulong msk);
516
517
/**
518
* Connect a FIFO to a CAN node.
519
*
520
* @param fifo
521
* The identifier of the FIFO
522
* @param node
523
* The CAN node to connect to
524
* @retval CAN_ERROR
525
* The given FIFO is not valid
526
* @retval 0
527
* Success
528
*/
529
ubyte
hsk_can_fifo_connect
(
const
hsk_can_fifo
fifo,
530
const
hsk_can_node
__xdata node);
531
532
/**
533
* Disconnect a FIFO from its CAN node.
534
*
535
* This takes the FIFO out of active communication, without deleting
536
* it.
537
*
538
* @param fifo
539
* The identifier of the FIFO
540
* @retval CAN_ERROR
541
* The given FIFO is not valid
542
* @retval 0
543
* Success
544
*/
545
ubyte
hsk_can_fifo_disconnect
(
const
hsk_can_fifo
fifo);
546
547
/**
548
* Delete a FIFO.
549
*
550
* @param fifo
551
* The identifier of the FIFO
552
* @retval CAN_ERROR
553
* The given FIFO is not valid
554
* @retval 0
555
* Success
556
*/
557
ubyte
hsk_can_fifo_delete
(
const
hsk_can_fifo
fifo);
558
559
/**
560
* Select the next FIFO entry.
561
*
562
* The hsk_can_fifo_updated() and hsk_can_fifo_getData() functions always
563
* refer to a certain message within the FIFO. This function selects the
564
* next entry.
565
*
566
* @param fifo
567
* The ID of the FIFO to select the next entry from
568
*/
569
void
hsk_can_fifo_next
(
const
hsk_can_fifo
fifo);
570
571
/**
572
* Returns the CAN ID of the selected FIFO entry.
573
*
574
* @param fifo
575
* The ID of the FIFO
576
* @return
577
* The ID of the currently selected message object
578
*/
579
ulong
hsk_can_fifo_getId
(
const
hsk_can_fifo
fifo);
580
581
/**
582
* Return whether the currently selected FIFO entry was updated via CAN bus
583
* between this call and the previous call of this method.
584
*
585
* It can be used to decide when to call hsk_can_fifo_getData() and
586
* hsk_can_fifo_next().
587
*
588
* @param fifo
589
* The identifier of the FIFO to check
590
* @retval 1
591
* The FIFO entry was updated since the last call of this function
592
* @retval 0
593
* The FIFO entry has not been updated since the last call of this function
594
*/
595
bool
hsk_can_fifo_updated
(
const
hsk_can_fifo
fifo);
596
597
/**
598
* Gets the data from the currently selected FIFO entry.
599
*
600
* This writes DLC bytes from the FIFO entry into msgdata.
601
*
602
* @param fifo
603
* The identifier of the FIFO
604
* @param msgdata
605
* The character array to store the message data in
606
*/
607
void
hsk_can_fifo_getData
(
const
hsk_can_fifo
fifo,
608
ubyte *
const
msgdata);
609
610
/** \file
611
* \section data Message Data
612
*
613
* The hsk_can_data_setSignal() and hsk_can_data_getSignal() functions allow
614
* writing and reading signals across byte boundaries to and from a buffer.
615
*
616
* For big endian signals the bit position of the most significant bit must
617
* be supplied (highest bit in the first byte). For little endian signals
618
* the least significant bit must be supplied (lowest bit in the first byte).
619
*
620
* This conforms to the way signal positions are stored in Vector CANdb++
621
* DBC files.
622
*/
623
624
/**
625
* Sets a signal value in a data field.
626
*
627
* @param msg
628
* The message data field to write into
629
* @param endian
630
* Little or big endian encoding
631
* @param sign
632
* Indicates whether the value has a signed type
633
* @param bitPos
634
* The bit position of the signal
635
* @param bitCount
636
* The length of the signal
637
* @param value
638
* The signal value to write into the data field
639
*/
640
void
hsk_can_data_setSignal
(ubyte *
const
msg,
const
bool
endian,
641
const
bool
sign,
const
ubyte bitPos,
642
const
char
bitCount,
const
ulong idata value);
643
644
/**
645
* Get a signal value from a data field.
646
*
647
* @param msg
648
* The message data field to read from
649
* @param endian
650
* Little or big endian encoding
651
* @param sign
652
* Indicates whether the value has a signed type
653
* @param bitPos
654
* The bit position of the signal
655
* @param bitCount
656
* The length of the signal
657
* @return
658
* The signal from the data field msg
659
*/
660
ulong
hsk_can_data_getSignal
(
const
ubyte *
const
msg,
const
bool
endian,
661
const
bool
sign,
const
ubyte bitPos,
662
const
char
bitCount);
663
664
#endif
/* _HSK_CAN_H_ */
hsk_can
hsk_can.h
Generated on Fri Oct 11 2013 12:54:53 for hsk_libs-dev by
1.8.3.1