source: trunk/MagicSoft/Cosy/incl/bcan.h@ 750

Last change on this file since 750 was 731, checked in by tbretz, 24 years ago
*** empty log message ***
File size: 16.0 KB
Line 
1/*-----------------------------------------------------------------------------
2bcan.h -- Interface to Basic CAN Controller Chip PCA82C200
3
4Copyright (c) 1994 JANZ Computer AG
5All Rights Reserved
6
7Created 94/10/11 by Soenke Hansen
8Version 1.18 of 00/03/30
9
10Prototypes for functions in bcan.c.
11Macros for descriptor word, CAN identifiers, RTR bits, and data length codes.
12Defintions of register addresses and values for the Basic CAN controller.
13
14-----------------------------------------------------------------------------*/
15
16#ifndef bcan_DEFINED
17#define bcan_DEFINED
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23#include "defs.h"
24#include "msg.h"
25
26/*-------- COBs CAN Communication Objects ----------------------------------*/
27
28/* COBs (CAN Communication Objects) consists of the following data:
29 WORD_t desc;
30 BYTE_t data[8];
31 The format of the descriptor word follows that of the PCA82C200 controller:
32 desc >> 5 is the 11-bit identifier
33 desc & 0x0010 is set iff the RTR bit is set
34 desc & 0x000f is the data length code DLC
35 There are at most 8 data bytes in data[] (DLC <= 8).
36*/
37
38/* RTR bit in COB descriptor */
39#define COBD_RTR 0x0010
40
41/* Get RTR bit from COB descriptor */
42#define cobd_get_rtr(desc) ((desc) & COBD_RTR)
43
44/* Get data length code from COB descriptor */
45#define cobd_get_dlc(desc) ((desc) & 0x000f)
46
47/* Get real data length */
48#define cobd_get_len(desc) (((desc) & COBD_RTR) ? 0 : \
49 (((desc) & 0x08) ? 8 : (desc) & 0x07))
50
51/* Get id and RTR bit from COB descriptor */
52#define cobd_get_id_rtr(desc) ((desc) & 0xfff0)
53
54/* Get RTR bit and data length code from COB descriptor */
55#define cobd_get_rtr_dlc(desc) ((desc) & 0x001f)
56
57/* Get identifier from COB descriptor */
58#define cobd_get_id(desc) ((desc) >> 5) /* numerical value of id */
59
60/* Shift identifier for oring into COB descriptor */
61#define cobd_id_to_desc(id) ((id) << 5)
62
63/* Set COB descriptor from Id, and data length code, with or without RTR */
64#define cobd_set(id, dlc) (((id) << 5) | (0x000f & (WORD_t)(dlc)))
65#define cobd_setrtr(id, dlc) (((id) << 5) | COBD_RTR | (0x000f & (WORD_t)(dlc)))
66
67
68/*-------- Definitions of registers ----------------------------------------*/
69
70/*
71 * 82C200 and SJA1000 in BasicCAN Mode
72 */
73
74/* PCA82C200 Address Allocation */
75#define BCAN_CR 0 /* control register */
76#define BCAN_CMR 1 /* command register */
77#define BCAN_SR 2 /* status register */
78#define BCAN_IR 3 /* interrupt register */
79#define BCAN_AC 4 /* acceptance code register */
80#define BCAN_AM 5 /* acceptance mask register */
81#define BCAN_BT0 6 /* bus timing register 0 */
82#define BCAN_BT1 7 /* bus timing register 1 */
83#define BCAN_OCR 8 /* output control register */
84#define BCAN_TDESC1 10 /* first descriptor transmit buffer */
85#define BCAN_TDESC2 11 /* second descriptor transmit buffer */
86#define BCAN_TDATA 12 /* start data transmit buffer */
87#define BCAN_RDESC1 20 /* first descriptor receive buffer */
88#define BCAN_RDESC2 21 /* second descriptor receive buffer */
89#define BCAN_RDATA 22 /* start data receive buffer */
90#define BCAN_CDR 31 /* clock divider */
91
92
93/* Control Register Bits */
94#define BCAN_CR_OIE 0x10 /* Overrun Interrupt Enable */
95#define BCAN_CR_EIE 0x08 /* Error Interrupt Enable */
96#define BCAN_CR_TIE 0x04 /* Transmit Interrupt Enable */
97#define BCAN_CR_RIE 0x02 /* Receive Interrupt Enable */
98#define BCAN_CR_RR 0x01 /* Reset Request */
99
100/* Command Register Bits */
101#define BCAN_CMR_GTS 0x10 /* Goto Sleep */
102#define BCAN_CMR_COS 0x08 /* Clear Overrun Status */
103#define BCAN_CMR_RRB 0x04 /* Release Receive Buffer */
104#define BCAN_CMR_AT 0x02 /* Abort Transmission */
105#define BCAN_CMR_TR 0x01 /* Transmission Request */
106
107/* Status Register Bits */
108#define BCAN_SR_BS 0x80 /* Bus Status */
109#define BCAN_SR_ES 0x40 /* Error Status */
110#define BCAN_SR_TS 0x20 /* Transmit Status */
111#define BCAN_SR_RS 0x10 /* Receive Status */
112#define BCAN_SR_TCS 0x08 /* Transmission Complete Status */
113#define BCAN_SR_TBS 0x04 /* Transmit Buffer Status */
114#define BCAN_SR_DO 0x02 /* Data Overrun */
115#define BCAN_SR_RBS 0x01 /* Receive Buffer Status */
116
117/* Interrupt Register Bits */
118#define BCAN_IR_WUI 0x10 /* Wake-Up Interrupt */
119#define BCAN_IR_OI 0x08 /* Overrun Interrupt */
120#define BCAN_IR_EI 0x04 /* Error Interrupt */
121#define BCAN_IR_TI 0x02 /* Transmit Interrupt */
122#define BCAN_IR_RI 0x01 /* Receive Interrupt */
123
124
125/*
126 * JSA1000 in PeliCAN mode
127 */
128
129/* PeliCAN mode address allocation */
130#define PCAN_MODR 0 /* Mode register (rw) */
131#define PCAN_CMR 1 /* Command register (wo) */
132#define PCAN_SR 2 /* Status register (ro) */
133#define PCAN_IR 3 /* Interrupt register (ro) */
134#define PCAN_IER 4 /* Interrupt enable register (rw) */
135#define PCAN_BTR0 6 /* Bus timing register 0 (ro, rw) */
136#define PCAN_BTR1 7 /* Bus timing register 1 (ro, rw) */
137#define PCAN_OCR 8 /* Output control register 1 (ro, rw) */
138#define PCAN_TESTR 9 /* Test register */
139#define PCAN_ALCR 11 /* Arbitration lost capture reg (ro) */
140#define PCAN_ECCR 12 /* Error code capture register (ro) */
141#define PCAN_EWLR 13 /* Error warning limit register (ro, rw) */
142#define PCAN_RXERR 14 /* Rx error counter register (ro, rw) */
143#define PCAN_TXERR 15 /* Tx error counter register (ro, rw) */
144#define PCAN_ACR0 16 /* acceptance code register 0 (-, rw) */
145#define PCAN_ACR1 17 /* acceptance code register 1 (-, rw) */
146#define PCAN_ACR2 18 /* acceptance code register 2 (-, rw) */
147#define PCAN_ACR3 19 /* acceptance code register 3 (-, rw) */
148#define PCAN_AMR0 20 /* acceptance mask register 0 (-, rw) */
149#define PCAN_AMR1 21 /* acceptance mask register 1 (-, rw) */
150#define PCAN_AMR2 22 /* acceptance mask register 2 (-, rw) */
151#define PCAN_AMR3 23 /* acceptance mask register 3 (-, rw) */
152#define PCAN_RXFI 16 /* Rx Frame info SFF, EFF (ro, -) */
153#define PCAN_RXID1 17 /* Rx Identifier 1 SFF, EFF (ro, -) */
154#define PCAN_RXID2 18 /* Rx Identifier 2 SFF, EFF (ro, -) */
155#define PCAN_RXID3 19 /* Rx Identifier 3 EFF (ro, -) */
156#define PCAN_RXID4 20 /* Rx Identifier 4 EFF (ro, -) */
157#define PCAN_RXSFFD 19 /* Rx standard frame data (ro, -) */
158#define PCAN_RXEFFD 21 /* Rx extended frame data (ro, -) */
159#define PCAN_TXFI 16 /* Tx Frame info SFF, EFF (wo, -) */
160#define PCAN_TXID1 17 /* Tx Identifier 1 SFF, EFF (wo, -) */
161#define PCAN_TXID2 18 /* Tx Identifier 2 SFF, EFF (wo, -) */
162#define PCAN_TXID3 19 /* Tx Identifier 3 EFF (wo, -) */
163#define PCAN_TXID4 20 /* Tx Identifier 4 EFF (wo, -) */
164#define PCAN_TXSFFD 19 /* Tx standard frame data (wo, -) */
165#define PCAN_TXEFFD 21 /* Tx extended frame data (wo, -) */
166#define PCAN_RXMCR 29 /* Rx message counter register (ro) */
167#define PCAN_RXBSAR 30 /* Rx buffer start address register (ro, rw) */
168#define PCAN_CDR 31 /* Clock divider register ('rw', rw)*/
169
170#define PCAN_RXFI_RAM 96 /* RAM mirror of RXFI */
171#define PCAN_TXFI_RAM 96 /* RAM mirror of TXFI */
172#define PCAN_TXID1_RAM 97 /* RAM mirror Tx Identifier 1 SFF, EFF */
173#define PCAN_TXID2_RAM 98 /* RAM mirror Tx Identifier 2 SFF, EFF */
174#define PCAN_TXID3_RAM 99 /* RAM mirror Tx Identifier 3 EFF */
175#define PCAN_TXID4_RAM 100 /* RAM mirror Tx Identifier 4 EFF */
176#define PCAN_TXSFFD_RAM 99 /* RAM mirror Tx standard frame data */
177#define PCAN_TXEFFD_RAM 101 /* RAM mirror Tx extended frame data */
178
179
180/* Mode Register Bits */
181#define PCAN_MODR_SM (1<<4) /* Sleep mode */
182#define PCAN_MODR_AFM (1<<3) /* Acceptance filter mode */
183#define PCAN_MODR_STM (1<<2) /* Self test mode */
184#define PCAN_MODR_LOM (1<<1) /* Listen only mode */
185#define PCAN_MODR_RM (1<<0) /* Reset mode */
186
187/* Command Register Bits */
188#define PCAN_CMR_SRR (1<<4) /* Self reception request */
189#define PCAN_CMR_CDO (1<<3) /* Clear data overrun */
190#define PCAN_CMR_RRB (1<<2) /* Release receive buffer */
191#define PCAN_CMR_AT (1<<1) /* Abort transmission */
192#define PCAN_CMR_TR (1<<0) /* Transmission request */
193
194/* Status Register Bits */
195#define PCAN_SR_BS (1<<7) /* Bus status */
196#define PCAN_SR_ES (1<<6) /* Error status */
197#define PCAN_SR_TS (1<<5) /* Transmit status */
198#define PCAN_SR_RS (1<<4) /* Receive status */
199#define PCAN_SR_TCS (1<<3) /* Transmission complete status */
200#define PCAN_SR_TBS (1<<2) /* Transmit buffer status */
201#define PCAN_SR_DOS (1<<1) /* Data overrun status */
202#define PCAN_SR_RBS (1<<0) /* Receive buffer status */
203
204/* Interrupt Register Bits */
205#define PCAN_IR_BEI (1<<7) /* Bus-eror interrupt */
206#define PCAN_IR_ALI (1<<6) /* Arbitration lost interrupt */
207#define PCAN_IR_EPI (1<<5) /* Error-passive interrupt */
208#define PCAN_IR_WUI (1<<4) /* Wake-up interrupt */
209#define PCAN_IR_DOI (1<<3) /* Data-overrun interrupt */
210#define PCAN_IR_EI (1<<2) /* Error interrupt */
211#define PCAN_IR_TI (1<<1) /* Transmit interrupt */
212#define PCAN_IR_RI (1<<0) /* Receive interrupt */
213
214/* Interrupt enable register bits */
215#define PCAN_IER_BEIE (1<<7) /* Bus-eror interrupt enable */
216#define PCAN_IER_ALIE (1<<6) /* Arbitration lost interrupt enable */
217#define PCAN_IER_EPIE (1<<5) /* Error-passive interrupt enable */
218#define PCAN_IER_WUIE (1<<4) /* Wake-up interrupt enable */
219#define PCAN_IER_DOIE (1<<3) /* Data-overrun interrupt enable */
220#define PCAN_IER_EIE (1<<2) /* Error warning interrupt enable */
221#define PCAN_IER_TIE (1<<1) /* Transmit interrupt enable */
222#define PCAN_IER_RIE (1<<0) /* Receive interrupt enable */
223
224/* Clock divider register bits */
225#define PCAN_CDR_PCAN (1<<7) /* Enable PCAN mode */
226#define PCAN_CDR_CBP (1<<6) /* Enable Rx input comparator bypass */
227#define PCAN_CDR_RXINTEN (1<<5) /* Enable RXINT output at TX1 */
228#define PCAN_CDR_CLKOFF (1<<3) /* Disable clock output */
229
230/* Bit definitions for Rx/Tx Frame info */
231#define PCAN_FINFO_FF (1<<7) /* Frame format bit */
232#define PCAN_FINFO_EFF (1<<7) /* Extended frame indication */
233#define PCAN_FINFO_RTR (1<<6) /* RTR frame bit */
234#define PCAN_FINFO_DLC_MASK (0x0f) /* Data length code mask */
235
236
237
238/*
239 * BasicCAN, PeliCAN common definitions.
240 */
241
242/* Values of acceptance code/mask registers */
243#define ACM_ALL word_from_bytes(0x00,0xff) /* accept all ids */
244
245/* Values of output control register */
246#define OCR_PUSHPULL 0xfa /* push/pull */
247
248/* Admissable bus timing values (BTR1=msb, BTR0=lsb) */
249#ifdef OSC_16MHZ /* if using a 16 MHz oscillator */
250#define BTR_1MB 0x2300 /* 1 MBaud */
251#if 0
252#define BTR_1MB 0x1400 /* 1 MBaud */ /* The better value */
253#endif
254#define BTR_800KB 0x2500 /* 800 KBaud */
255#define BTR_500KB 0x1c00 /* 500 KBaud */
256#define BTR_250KB 0x1c01 /* 250 KBaud */
257#define BTR_125KB 0x1c03 /* 125 KBaud */
258#define BTR_100KB 0x34c7 /* 100 KBaud */
259#define BTR_62_5KB 0xbac7 /* 62.5 KBaud */
260#define BTR_50KB 0x34cf /* 50 KBaud */
261#define BTR_20KB 0x7fcf /* 20 KBaud */
262#define BTR_10KB 0x7fdf /* 10 KBaud */
263#endif
264
265/* Admissable values in clock divider register */
266#ifdef OSC_16MHZ /* if using a 16 MHz oscillator */
267#define CDR_16MHZ 0x07 /* 16 MHz output */
268#define CDR_8MHZ 0x00 /* 8 MHz output (division by 2) */
269#define CDR_4MHZ 0x01 /* 4 MHz output (division by 4) */
270#define CDR_2MHZ 0x03 /* 2 MHz output (division by 8) */
271#endif
272
273/*-------- Gloal variables in bcan.c ---------------------------------------*/
274
275/* Indicates that we are using SJA1000 in pelican mode */
276extern short pcan_mode;
277
278/* ----------------------- BulkBuffer-Stuff --------------------------*/
279/* must be greater than length of biggest message type!!! */
280#define BULK_LEFT_FREE 14
281
282#define DEFAULT_MAX_BULK_BUF_BYTE_COUNT MSGLEN
283/* we need a minimum size of bulk buffer to generate messages */
284/* of a certain format (not bounde, but allocated buffer!) */
285/* to satisfy the message-handling inside the asm isr */
286#define DEFAULT_MIN_BULK_BUF_BYTE_COUNT 20
287#define DEFAULT_BULK_PREALLOC_COUNT 20
288
289/* Timer value for elapsed (e.g.) ms to determine */
290/* if it is time to send current BulkBuffer. Will */
291/* be maintained in timer.c */
292extern LWORD_t bulkBufTim;
293
294void configBulkBuffer(unsigned short, unsigned char, unsigned char);
295
296/* ----------------------- SniffBuffer-Stuff --------------------------*/
297/* must be greater than length of biggest message type!!! */
298#define SNIFF_LEFT_FREE 26
299
300#define DEFAULT_MAX_SNIFF_BUF_BYTE_COUNT MSGLEN
301/* we need a minimum size of sniff buffer to generate messages */
302/* of a certain format (not bounde, but allocated buffer!) */
303/* to satisfy the message-handling inside the asm isr */
304#define DEFAULT_MIN_SNIFF_BUF_BYTE_COUNT 20
305#define DEFAULT_SNIFF_PREALLOC_COUNT 20
306
307/* Timer value for elapsed (e.g.) ms to determine */
308/* if it is time to send current SniffBuffer. Will */
309/* be maintained in timer.c */
310extern LWORD_t sniffBufTim;
311
312void configSniffBuffer(unsigned short, unsigned char, unsigned char);
313void configSniffBufferEcho(unsigned short);
314/* valid values for echo2sniff, configured by configSniffBufferEcho: */
315#define SNIFF_ECHO_FROM_CANLOOK (1 << 0)
316#define SNIFF_ECHO_FROM_PLAINQ (1 << 1)
317#define SNIFF_ECHO_FROM_FASTQ (1 << 2)
318#define SNIFF_RX_FROM_CAN (1 << 3)
319
320
321/* message-commands inside sniff-message */
322#define SNIFF_MS_ECHO_FROM_PLAIN_STD 3
323#define SNIFF_MS_ECHO_FROM_PLAIN_XTD 4 /* not yet used */
324#define SNIFF_MS_ECHO_FROM_FAST_STD 5
325#define SNIFF_MS_ECHO_FROM_FAST_XTD 6
326#define SNIFF_MS_ECHO_FROM_CANLOOK_STD 7
327#define SNIFF_MS_ECHO_FROM_CANLOOK_XTD 8
328#define SNIFF_MS_CAN_RX_STD 9
329#define SNIFF_MS_CAN_RX_XTD 10
330#define SNIFF_MS_MSG_LOST 0x80
331#define SNIFF_MS_UNKNOWN 0xff
332
333
334/* -----------------------Busload-Statistic-Stuff --------------------------*/
335#define BUS_LOAD_STAT_SEND_TO_HOST (1 << 0)
336#define BUS_LOAD_STAT_INTEGR_TIME_ELAPSED (1 << 1)
337#define BUS_LOAD_STAT_ENABLED (1 << 7)
338
339#define BUS_LOAD_REQUEST_ALL 0
340#define BUS_LOAD_REQUEST_STD 1
341#define BUS_LOAD_REQUEST_XTD 2
342#define BUS_LOAD_REQUEST_RX 3
343#define BUS_LOAD_REQUEST_TX 4
344
345void resetBusLoad(void);
346void configBusLoad(WORD_t, BYTE_t);
347void sendBusLoadToHost(void);
348void busLoadUpdate(void);
349void checkBusLoadStatistic(void);
350
351void resetBusLoadSniff(void);
352void configBusLoadSniff(WORD_t, BYTE_t);
353void sendBusLoadToHostSniff(void);
354void busLoadUpdateSniff(void);
355void checkBusLoadStatisticSniff(void);
356
357/*-------- Functions in bcan.c ---------------------------------------------*/
358
359/* Switch to bus-on state */
360extern void buson_bcan(void);
361
362/* Reset: Switch to bus-off state */
363extern void busoff_bcan(void);
364
365/* Set bus timing registers (assumed state: bus-off) */
366extern void write_btr_bcan(WORD_t);
367
368/* Set error waring limit */
369void write_ewl_bcan( BYTE_t ewl);
370
371/* Control bus-error reporting */
372void switch_berr_bcan( BYTE_t state);
373
374/* provide access to berrs_to_go outside bcan.c */
375BYTE_t getBerrsToGo(void);
376
377/* Control listen only mode */
378void switch_lom_bcan( BYTE_t state);
379
380/* Control self test mode */
381void switch_stm_bcan( BYTE_t state);
382
383/* Set hardware acceptance filter */
384extern void write_acm_bcan(WORD_t);
385
386/* Set extended hardware acceptance filter */
387void write_ext_acm_bcan(int mode, LWORD_t ac, LWORD_t am);
388
389/* Abort current transmission */
390extern void sendabort_bcan(void);
391
392/* Flush the transmit queue. */
393extern void flush_tx_bcan(void);
394
395/* Initialize the BCAN controller. The controller is left in the bus-off
396 state. To start communication buson_bcan() must be called.
397 All interrupt sources are accepted. All messages pass the acceptance
398 filter. The bus timing and the clock divider registers are initialized
399 with the parameters to the function. */
400extern void init_bcan(WORD_t, WORD_t, WORD_t, WORD_t);
401
402/* Service of BCAN message pool -- to be called cyclically */
403extern void serv_bcan(void);
404
405/* Interrupt service for PCA82C200. */
406extern void isr_bcan(void);
407#ifdef ASSHACK
408extern void a_isr_bcan(void);
409extern void a_isr_bcan_end(void);
410#endif
411
412
413/* Request to send a CAN message: Fill the transmit buffer of the
414 CAN chip and issue a transmit request.
415 Return values: 1 okay, transmit request accepted by controller
416 0 remote request successful
417 -1 transmit request failed (no buffer, no mem)
418*/
419extern int sendreq_bcan(
420 int, /* transmit request specifier */
421 WORD_t, /* request id */
422 WORD_t, /* descriptor (id, rtr, dlc) */
423 BYTE_t * /* data bytes */
424);
425
426#ifdef __cplusplus
427}
428#endif
429
430#endif /* !bcan_DEFINED */
Note: See TracBrowser for help on using the repository browser.