RTEMS 6.1-rc5
Loading...
Searching...
No Matches
grcan.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-2-Clause */
2
9/*
10 * COPYRIGHT (c) 2007.
11 * Cobham Gaisler AB.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#ifndef __GRCAN_H__
36#define __GRCAN_H__
37
48#include <stdint.h>
49
50#ifdef __cplusplus
51extern "C" {
52#endif
53
54struct grcan_regs {
55 volatile unsigned int conf; /* 0x00 */
56 volatile unsigned int stat; /* 0x04 */
57 volatile unsigned int ctrl; /* 0x08 */
58 volatile unsigned int dummy0[3]; /* 0x0C-0x014 */
59 volatile unsigned int smask; /* 0x18 */
60 volatile unsigned int scode; /* 0x1C */
61
62 volatile unsigned int dummy1[8]; /* 0x20-0x3C */
63
64 volatile unsigned int nbtr; /* 0x40 */
65 volatile unsigned int fdbtr; /* 0x44 */
66 volatile unsigned int tdelay; /* 0x48 */
67
68 volatile unsigned int dummy1b[45]; /* 0x4C-0xFC */
69
70 volatile unsigned int pimsr; /* 0x100 */
71 volatile unsigned int pimr; /* 0x104 */
72 volatile unsigned int pisr; /* 0x108 */
73 volatile unsigned int pir; /* 0x10C */
74 volatile unsigned int imr; /* 0x110 */
75 volatile unsigned int picr; /* 0x114 */
76
77 volatile unsigned int dummy2[58]; /* 0x118-0x1FC */
78
79 volatile unsigned int tx0ctrl; /* 0x200 */
80 volatile unsigned int tx0addr; /* 0x204 */
81 volatile unsigned int tx0size; /* 0x208 */
82 volatile unsigned int tx0wr; /* 0x20C */
83 volatile unsigned int tx0rd; /* 0x210 */
84 volatile unsigned int tx0irq; /* 0x214 */
85
86 volatile unsigned int dummy3[58]; /* 0x218-0x2FC */
87
88 volatile unsigned int rx0ctrl; /* 0x300 */
89 volatile unsigned int rx0addr; /* 0x304 */
90 volatile unsigned int rx0size; /* 0x308 */
91 volatile unsigned int rx0wr; /* 0x30C */
92 volatile unsigned int rx0rd; /* 0x310 */
93 volatile unsigned int rx0irq; /* 0x314 */
94 volatile unsigned int rx0mask; /* 0x318 */
95 volatile unsigned int rx0code; /* 0x31C */
96};
97
99 unsigned int passive_cnt;
100 unsigned int overrun_cnt;
101 unsigned int rxsync_cnt;
102 unsigned int txsync_cnt;
103 unsigned int txloss_cnt;
104 unsigned int ahberr_cnt;
105 unsigned int ints;
106 unsigned int busoff_cnt;
107};
108
110 unsigned char scaler;
111 unsigned char ps1;
112 unsigned char ps2;
113 unsigned char rsj;
114 unsigned char bpr;
115};
116
118 unsigned char scaler;
119 unsigned char ps1;
120 unsigned char ps2;
121 unsigned char sjw;
122 unsigned char resv_zero;
123};
124
126 int selection;
127 int enable0;
128 int enable1;
129};
130
132 unsigned long long mask;
133 unsigned long long code;
134};
135
136#define GRCAN_FDOPT_NOM 0
137#define GRCAN_FDOPT_FDBTR 0x01
138#define GRCAN_FDOPT_FDFRM 0x02
139#define GRCAN_FDMASK (GRCAN_FDOPT_FDBTR | GRCAN_FDOPT_FDFRM)
140
141/* CAN MESSAGE */
142typedef struct {
143 char extended; /* 1= Extended Frame (29-bit id), 0= STD Frame (11-bit id) */
144 char rtr; /* RTR - Remote Transmission Request */
145 char unused; /* Must be 0 to select classic CAN frame */
146 unsigned char len;
147 unsigned char data[8];
148 unsigned int id;
149} CANMsg;
150
151/* CAN-FD MESSAGE */
152typedef struct {
153 uint8_t extended; /* 1= Extended Frame (29-bit id), 0= STD Frame (11-bit id) */
154 uint8_t rtr; /* RTR - Remote Transmission Request */
155 uint8_t fdopts; /* Bit1: 1=Switch bit rate. bit2: 1=FD frame. */
156 uint8_t len; /* 0-8, 12, 16, 20, 24, 32, 48 or 64 bytes */
157 uint32_t id;
158 union {
159 uint64_t dwords[8]; /* up to 64 bytes if FD=1 and len>8 */
160 uint8_t bytes[64]; /* up to 64 bytes if FD=1 and len>8 */
161 } data;
162} CANFDMsg;
163
164enum {
165 GRCAN_RET_OK = 0,
166 GRCAN_RET_INVARG = -1,
167 GRCAN_RET_NOTSTARTED = -2,
168 GRCAN_RET_TIMEOUT = -3,
169 /* Bus-off condition detected (request aborted by driver) */
170 GRCAN_RET_BUSOFF = -4,
171 /* AHB error condition detected (request aborted by driver) */
172 GRCAN_RET_AHBERR = -5,
173};
174
175/*
176 * User functions can cause these transitions:
177 * STATE_STOPPED -> STATE_STARTED
178 * STATE_STARTED -> STATE_STOPPED
179 * STATE_BUSOFF -> STATE_STOPPED
180 * STATE_AHBERR -> STATE_STOPPED
181 *
182 * ISR can cause these transition
183 * STATE_STARTED -> STATE_BUSOFF
184 * STATE_STARTED -> STATE_AHBERR
185 *
186 * STATE_BUSOFF is entered from ISR on bus-off condition. STATE_AHBERR is
187 * entered from ISR on AHB DMA errors on RX/TX operations. At transition the ISR
188 * disables DMA, masks all interrupts and releases semaphores.
189 */
190enum grcan_state {
191 STATE_STOPPED = 0,
192 STATE_STARTED = 1,
193 STATE_BUSOFF = 2,
194 STATE_AHBERR = 3,
195};
196
197#define GRCAN_CFG_ABORT 0x00000001
198#define GRCAN_CFG_ENABLE0 0x00000002
199#define GRCAN_CFG_ENABLE1 0x00000004
200#define GRCAN_CFG_SELECTION 0x00000008
201#define GRCAN_CFG_SILENT 0x00000010
202#define GRCAN_CFG_BPR 0x00000300
203#define GRCAN_CFG_RSJ 0x00007000
204#define GRCAN_CFG_PS1 0x00f00000
205#define GRCAN_CFG_PS2 0x000f0000
206#define GRCAN_CFG_SCALER 0xff000000
207
208#define GRCAN_CFG_BPR_BIT 8
209#define GRCAN_CFG_RSJ_BIT 12
210#define GRCAN_CFG_PS1_BIT 20
211#define GRCAN_CFG_PS2_BIT 16
212#define GRCAN_CFG_SCALER_BIT 24
213
214#define GRCAN_CTRL_RESET 0x2
215#define GRCAN_CTRL_ENABLE 0x1
216
217#define GRCAN_TXCTRL_ENABLE 1
218#define GRCAN_TXCTRL_ONGOING 1
219
220#define GRCAN_RXCTRL_ENABLE 1
221#define GRCAN_RXCTRL_ONGOING 1
222
223#define GRCANFD_NBTR_SCALER 0x00ff0000
224#define GRCANFD_NBTR_PS1 0x0000fc00
225#define GRCANFD_NBTR_PS2 0x000003e0
226#define GRCANFD_NBTR_SJW 0x0000001f
227
228#define GRCANFD_NBTR_SCALER_BIT 16
229#define GRCANFD_NBTR_PS1_BIT 10
230#define GRCANFD_NBTR_PS2_BIT 5
231#define GRCANFD_NBTR_SJW_BIT 0
232
233#define GRCANFD_FDBTR_SCALER 0x00ff0000
234#define GRCANFD_FDBTR_PS1 0x00003c00
235#define GRCANFD_FDBTR_PS2 0x000001e0
236#define GRCANFD_FDBTR_SJW 0x0000000f
237
238#define GRCANFD_FDBTR_SCALER_BIT 16
239#define GRCANFD_FDBTR_PS1_BIT 10
240#define GRCANFD_FDBTR_PS2_BIT 5
241#define GRCANFD_FDBTR_SJW_BIT 0
242
243/* Relative offset of IRQ sources to AMBA Plug&Play */
244#define GRCAN_IRQ_IRQ 0
245#define GRCAN_IRQ_TXSYNC 1
246#define GRCAN_IRQ_RXSYNC 2
247
248#define GRCAN_ERR_IRQ 0x1
249#define GRCAN_OFF_IRQ 0x2
250#define GRCAN_OR_IRQ 0x4
251#define GRCAN_RXAHBERR_IRQ 0x8
252#define GRCAN_TXAHBERR_IRQ 0x10
253#define GRCAN_RXIRQ_IRQ 0x20
254#define GRCAN_TXIRQ_IRQ 0x40
255#define GRCAN_RXFULL_IRQ 0x80
256#define GRCAN_TXEMPTY_IRQ 0x100
257#define GRCAN_RX_IRQ 0x200
258#define GRCAN_TX_IRQ 0x400
259#define GRCAN_RXSYNC_IRQ 0x800
260#define GRCAN_TXSYNC_IRQ 0x1000
261#define GRCAN_RXERR_IRQ 0x2000
262#define GRCAN_TXERR_IRQ 0x4000
263#define GRCAN_RXMISS_IRQ 0x8000
264#define GRCAN_TXLOSS_IRQ 0x10000
265
266#define GRCAN_STAT_PASS 0x1
267#define GRCAN_STAT_OFF 0x2
268#define GRCAN_STAT_OR 0x4
269#define GRCAN_STAT_AHBERR 0x8
270#define GRCAN_STAT_ACTIVE 0x10
271#define GRCAN_STAT_RXERRCNT 0xff00
272#define GRCAN_STAT_TXERRCNT 0xff0000
273
274/*
275 * Return number of GRCAN devices available to driver
276 */
277extern int grcan_dev_count(void);
278
279/*
280 * Open a GRCAN device
281 *
282 * dev_no: Device number to open
283 * return: Device handle to use with all other grcan_ API functions. The
284 * function returns NULL if device can not be opened.
285 */
286extern void *grcan_open(int dev_no);
287
288/*
289 * Open a GRCAN device by name. Finds device index then calls
290 * grcan_open(index).
291 *
292 * name: Device name to open
293 * dev_no: Device number matching name. Will be set if device found.
294 * return: Device handle to use with all other grcan_ API functions. The
295 * function returns NULL if device can not be opened or not found.
296 */
297extern void *grcan_open_by_name(char *name, int *dev_no);
298
299/*
300 * Close a GRCAN device
301 *
302 * return: This function always returns 0 (success)
303 */
304extern int grcan_close(void *d);
305
306/*
307 * Returns if CAN hardware device is CANFD capable.
308 *
309 * dev_no: Device handle
310 * return: 0=Not FD capable, 1=FD capable.
311 * function returns NULL if device can not be opened.
312 */
313extern int grcan_canfd_capable(void *d);
314
315/*
316 * Receive CAN messages
317 *
318 * Multiple CAN messages can be received in one call.
319 *
320 * d: Device handle
321 * msg: Pointer to receive messages
322 * count: Number of CAN messages to receive
323 *
324 * return:
325 * >=0: Number of CAN messages received. This can be
326 * less than the count parameter.
327 * GRCAN_RET_INVARG: count parameter less than one or NULL msg.
328 * GRCAN_RET_NOTSTARTED: Device not in started mode
329 * GRCAN_RET_TIMEOUT: Timeout in non-blocking mode
330 * GRCAN_RET_BUSOFF: A read was interrupted by a bus-off error.
331 * Device has left started mode.
332 * GRCAN_RET_AHBERR: Similar to BUSOFF, but was caused by AHB Error.
333 */
334extern int grcan_read(
335 void *d,
336 CANMsg *msg,
337 size_t count
338);
339
340/*
341 * Receive CAN messages (only GRCANFD)
342 *
343 * Multiple CAN messages can be received in one call.
344 *
345 * d: Device handle
346 * msg: Pointer to receive messages
347 * count: Number of CAN messages to receive
348 *
349 * return:
350 * >=0: Number of CAN messages received. This can be
351 * less than the count parameter.
352 * GRCAN_RET_INVARG: count parameter less than one or NULL msg.
353 * GRCAN_RET_NOTSTARTED: Device not in started mode
354 * GRCAN_RET_TIMEOUT: Timeout in non-blocking mode
355 * GRCAN_RET_BUSOFF: A read was interrupted by a bus-off error.
356 * Device has left started mode.
357 * GRCAN_RET_AHBERR: Similar to BUSOFF, but was caused by AHB Error.
358 */
359extern int grcanfd_read(
360 void *d,
361 CANFDMsg *msg,
362 size_t count
363);
364
365/*
366 * Transmit CAN messages
367 *
368 * Multiple CAN messages can be transmit in one call.
369 *
370 * d: Device handle
371 * msg: Pointer to messages to transmit
372 * count: Number of CAN messages to transmit
373 *
374 * return:
375 * >=0: Number of CAN messages transmitted. This can be
376 * less than the count parameter.
377 * GRCAN_RET_INVARG: count parameter less than one.
378 * GRCAN_RET_NOTSTARTED: Device not in started mode
379 * GRCAN_RET_TIMEOUT: Timeout in non-blocking mode
380 * GRCAN_RET_BUSOFF: A write was interrupted by a Bus-off error.
381 * Device has left started mode
382 * GRCAN_RET_AHBERR: Similar to BUSOFF, but was caused by AHB Error.
383 */
384extern int grcan_write(
385 void *d,
386 CANMsg *msg,
387 size_t count
388);
389
390/*
391 * Transmit CAN-FD complient messages (only GRCANFD)
392 *
393 * Multiple CAN messages can be transmit in one call.
394 *
395 * d: Device handle
396 * msg: Pointer to messages to transmit
397 * count: Number of CAN messages to transmit
398 *
399 * return:
400 * >=0: Number of CAN messages transmitted. This can be
401 * less than the count parameter.
402 * GRCAN_RET_INVARG: count parameter less than one.
403 * GRCAN_RET_NOTSTARTED: Device not in started mode
404 * GRCAN_RET_TIMEOUT: Timeout in non-blocking mode
405 * GRCAN_RET_BUSOFF: A write was interrupted by a Bus-off error.
406 * Device has left started mode
407 * GRCAN_RET_AHBERR: Similar to BUSOFF, but was caused by AHB Error.
408 */
409extern int grcanfd_write(
410 void *d,
411 CANFDMsg *msg,
412 size_t ucount
413);
414
415/*
416 * Returns current GRCAN software state
417 *
418 * If STATE_BUSOFF or STATE_AHBERR is returned then the function grcan_stop()
419 * shall be called before continue using the driver.
420 *
421 * d: Device handle
422 * return:
423 * STATE_STOPPED Stopped
424 * STATE_STARTED Started
425 * STATE_BUSOFF Bus-off has been detected
426 * STATE_AHBERR AHB error has been detected
427 */
428extern int grcan_get_state(void *d);
429
430/* The remaining functions return 0 on success and non-zero on failure. */
431
432/* Functions controlling operational
433 * mode
434 */
435/* Bring the link up after open or bus-off */
436extern int grcan_start(void *d);
437/* stop to change baud rate/config or closing down */
438extern int grcan_stop(void *d);
439/* Wait until all TX messages have been sent */
440extern int grcan_flush(void *d);
441
442/* Functions that require connection
443 * to be stopped
444 */
445/* enable silent mode read only state */
446extern int grcan_set_silent(void *d, int silent);
447/* enable/disable stopping link on AHB Error */
448extern int grcan_set_abort(void *d, int abort);
449/* Set Enable0,Enable1,Selection */
450extern int grcan_set_selection(void *d, const struct grcan_selection *selection);
451/* Set baudrate by using driver's baud rate timing calculation routines */
452extern int grcan_set_speed(void *d, unsigned int hz);
453/* Set baudrate by specifying the timing registers manually */
454extern int grcan_set_btrs(void *d, const struct grcan_timing *timing);
455
456/* Set the Nominal and FD baudrate by using driver's baud rate timing
457 * calculation routines
458 */
459extern int grcanfd_set_speed(void *d, unsigned int nomhz, unsigned int fdhz);
460/* Set Nominal and FD baudrate by specifying the timing registers manually*/
461extern int grcanfd_set_btrs(
462 void *d,
463 const struct grcanfd_timing *nominal,
464 const struct grcanfd_timing *fd);
465
466/* Functions can be called whenever */
467/* Enable/disable Blocking on reception (until at least one message has been received) */
468int grcan_set_rxblock(void* d, int block);
469/* Enable/disable Blocking on transmission (until at least one message has been transmitted) */
470int grcan_set_txblock(void* d, int block);
471/* Enable/disable Blocking until all requested messages has been sent */
472int grcan_set_txcomplete(void* d, int complete);
473/* Enable/disable Blocking until all requested has been received */
474int grcan_set_rxcomplete(void* d, int complete);
475/* Get statistics */
476extern int grcan_get_stats(void *d, struct grcan_stats *stats);
477/* Clear statistics */
478extern int grcan_clr_stats(void *d);
479/* Set Acceptance filters, provide pointer to "struct grcan_filter" or NULL to disable filtering (let all messages pass) */
480extern int grcan_set_afilter(void *d, const struct grcan_filter *filter);
481/* Set Sync Messages RX/TX filters, NULL disables the IRQ completely */
482extern int grcan_set_sfilter(void *d, const struct grcan_filter *filter);
483/* Get status register of GRCAN core */
484extern int grcan_get_status(void *d, unsigned int *status);
485
486void grcan_register_drv(void);
487
488#ifdef __cplusplus
489}
490#endif
491
494#endif
Definition: grcan.h:152
Definition: grcan.h:142
Definition: inftrees.h:24
Definition: grcan.h:131
Definition: grcan.h:54
Definition: grcan.h:125
Definition: grcan.h:98
Definition: grcan.h:109
Definition: grcan.h:117
Definition: b1553brm.c:94