RTEMS  5.1
grcan.h
Go to the documentation of this file.
1 
6 /*
7  * COPYRIGHT (c) 2007.
8  * Cobham Gaisler AB.
9  *
10  * The license and distribution terms for this file may be
11  * found in the file LICENSE in this distribution or at
12  * http://www.rtems.org/license/LICENSE.
13  */
14 
15 #ifndef __GRCAN_H__
16 #define __GRCAN_H__
17 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 struct grcan_regs {
33  volatile unsigned int conf; /* 0x00 */
34  volatile unsigned int stat; /* 0x04 */
35  volatile unsigned int ctrl; /* 0x08 */
36  volatile unsigned int dummy0[3]; /* 0x0C-0x014 */
37  volatile unsigned int smask; /* 0x18 */
38  volatile unsigned int scode; /* 0x1C */
39 
40  volatile unsigned int dummy1[56]; /* 0x20-0xFC */
41 
42  volatile unsigned int pimsr; /* 0x100 */
43  volatile unsigned int pimr; /* 0x104 */
44  volatile unsigned int pisr; /* 0x108 */
45  volatile unsigned int pir; /* 0x10C */
46  volatile unsigned int imr; /* 0x110 */
47  volatile unsigned int picr; /* 0x114 */
48 
49  volatile unsigned int dummy2[58]; /* 0x118-0x1FC */
50 
51  volatile unsigned int tx0ctrl; /* 0x200 */
52  volatile unsigned int tx0addr; /* 0x204 */
53  volatile unsigned int tx0size; /* 0x208 */
54  volatile unsigned int tx0wr; /* 0x20C */
55  volatile unsigned int tx0rd; /* 0x210 */
56  volatile unsigned int tx0irq; /* 0x214 */
57 
58  volatile unsigned int dummy3[58]; /* 0x218-0x2FC */
59 
60  volatile unsigned int rx0ctrl; /* 0x300 */
61  volatile unsigned int rx0addr; /* 0x304 */
62  volatile unsigned int rx0size; /* 0x308 */
63  volatile unsigned int rx0wr; /* 0x30C */
64  volatile unsigned int rx0rd; /* 0x310 */
65  volatile unsigned int rx0irq; /* 0x314 */
66  volatile unsigned int rx0mask; /* 0x318 */
67  volatile unsigned int rx0code; /* 0x31C */
68 };
69 
70 struct grcan_stats {
71  unsigned int passive_cnt;
72  unsigned int overrun_cnt;
73  unsigned int rxsync_cnt;
74  unsigned int txsync_cnt;
75  unsigned int txloss_cnt;
76  unsigned int ahberr_cnt;
77  unsigned int ints;
78  unsigned int busoff_cnt;
79 };
80 
81 struct grcan_timing {
82  unsigned char scaler;
83  unsigned char ps1;
84  unsigned char ps2;
85  unsigned int rsj;
86  unsigned char bpr;
87 };
88 
90  int selection;
91  int enable0;
92  int enable1;
93 };
94 
95 struct grcan_filter {
96  unsigned long long mask;
97  unsigned long long code;
98 };
99 
100 /* CAN MESSAGE */
101 typedef struct {
102  char extended; /* 1= Extended Frame (29-bit id), 0= STD Frame (11-bit id) */
103  char rtr; /* RTR - Remote Transmission Request */
104  char unused; /* unused */
105  unsigned char len;
106  unsigned char data[8];
107  unsigned int id;
108 } CANMsg;
109 
110 enum {
111  GRCAN_RET_OK = 0,
112  GRCAN_RET_INVARG = -1,
113  GRCAN_RET_NOTSTARTED = -2,
114  GRCAN_RET_TIMEOUT = -3,
115  /* Bus-off condition detected (request aborted by driver) */
116  GRCAN_RET_BUSOFF = -4,
117  /* AHB error condition detected (request aborted by driver) */
118  GRCAN_RET_AHBERR = -5,
119 };
120 
121 /*
122  * User functions can cause these transitions:
123  * STATE_STOPPED -> STATE_STARTED
124  * STATE_STARTED -> STATE_STOPPED
125  * STATE_BUSOFF -> STATE_STOPPED
126  * STATE_AHBERR -> STATE_STOPPED
127  *
128  * ISR can cause these transition
129  * STATE_STARTED -> STATE_BUSOFF
130  * STATE_STARTED -> STATE_AHBERR
131  *
132  * STATE_BUSOFF is entered from ISR on bus-off condition. STATE_AHBERR is
133  * entered from ISR on AHB DMA errors on RX/TX operations. At transition the ISR
134  * disables DMA, masks all interrupts and releases semaphores.
135  */
136 enum grcan_state {
137  STATE_STOPPED = 0,
138  STATE_STARTED = 1,
139  STATE_BUSOFF = 2,
140  STATE_AHBERR = 3,
141 };
142 
143 #define GRCAN_CFG_ABORT 0x00000001
144 #define GRCAN_CFG_ENABLE0 0x00000002
145 #define GRCAN_CFG_ENABLE1 0x00000004
146 #define GRCAN_CFG_SELECTION 0x00000008
147 #define GRCAN_CFG_SILENT 0x00000010
148 #define GRCAN_CFG_BPR 0x00000300
149 #define GRCAN_CFG_RSJ 0x00007000
150 #define GRCAN_CFG_PS1 0x00f00000
151 #define GRCAN_CFG_PS2 0x000f0000
152 #define GRCAN_CFG_SCALER 0xff000000
153 
154 #define GRCAN_CFG_BPR_BIT 8
155 #define GRCAN_CFG_RSJ_BIT 12
156 #define GRCAN_CFG_PS1_BIT 20
157 #define GRCAN_CFG_PS2_BIT 16
158 #define GRCAN_CFG_SCALER_BIT 24
159 
160 #define GRCAN_CTRL_RESET 0x2
161 #define GRCAN_CTRL_ENABLE 0x1
162 
163 #define GRCAN_TXCTRL_ENABLE 1
164 #define GRCAN_TXCTRL_ONGOING 1
165 
166 #define GRCAN_RXCTRL_ENABLE 1
167 #define GRCAN_RXCTRL_ONGOING 1
168 
169 /* Relative offset of IRQ sources to AMBA Plug&Play */
170 #define GRCAN_IRQ_IRQ 0
171 #define GRCAN_IRQ_TXSYNC 1
172 #define GRCAN_IRQ_RXSYNC 2
173 
174 #define GRCAN_ERR_IRQ 0x1
175 #define GRCAN_OFF_IRQ 0x2
176 #define GRCAN_OR_IRQ 0x4
177 #define GRCAN_RXAHBERR_IRQ 0x8
178 #define GRCAN_TXAHBERR_IRQ 0x10
179 #define GRCAN_RXIRQ_IRQ 0x20
180 #define GRCAN_TXIRQ_IRQ 0x40
181 #define GRCAN_RXFULL_IRQ 0x80
182 #define GRCAN_TXEMPTY_IRQ 0x100
183 #define GRCAN_RX_IRQ 0x200
184 #define GRCAN_TX_IRQ 0x400
185 #define GRCAN_RXSYNC_IRQ 0x800
186 #define GRCAN_TXSYNC_IRQ 0x1000
187 #define GRCAN_RXERR_IRQ 0x2000
188 #define GRCAN_TXERR_IRQ 0x4000
189 #define GRCAN_RXMISS_IRQ 0x8000
190 #define GRCAN_TXLOSS_IRQ 0x10000
191 
192 #define GRCAN_STAT_PASS 0x1
193 #define GRCAN_STAT_OFF 0x2
194 #define GRCAN_STAT_OR 0x4
195 #define GRCAN_STAT_AHBERR 0x8
196 #define GRCAN_STAT_ACTIVE 0x10
197 #define GRCAN_STAT_RXERRCNT 0xff00
198 #define GRCAN_STAT_TXERRCNT 0xff0000
199 
200 /*
201  * Return number of GRCAN devices available to driver
202  */
203 extern int grcan_dev_count(void);
204 
205 /*
206  * Open a GRCAN device
207  *
208  * dev_no: Device number to open
209  * return: Device handle to use with all other grcan_ API functions. The
210  * function returns NULL if device can not be opened.
211  */
212 extern void *grcan_open(int dev_no);
213 
214 /*
215  * Open a GRCAN device by name. Finds device index then calls
216  * grcan_open(index).
217  *
218  * name: Device name to open
219  * dev_no: Device number matching name. Will be set if device found.
220  * return: Device handle to use with all other grcan_ API functions. The
221  * function returns NULL if device can not be opened or not found.
222  */
223 extern void *grcan_open_by_name(char *name, int *dev_no);
224 
225 /*
226  * Close a GRCAN device
227  *
228  * return: This function always returns 0 (success)
229  */
230 extern int grcan_close(void *d);
231 
232 /*
233  * Receive CAN messages
234  *
235  * Multiple CAN messages can be received in one call.
236  *
237  * d: Device handle
238  * msg: Pointer to receive messages
239  * count: Number of CAN messages to receive
240  *
241  * return:
242  * >=0: Number of CAN messages received. This can be
243  * less than the count parameter.
244  * GRCAN_RET_INVARG: count parameter less than one or NULL msg.
245  * GRCAN_RET_NOTSTARTED: Device not in started mode
246  * GRCAN_RET_TIMEOUT: Timeout in non-blocking mode
247  * GRCAN_RET_BUSOFF: A read was interrupted by a bus-off error.
248  * Device has left started mode.
249  * GRCAN_RET_AHBERR: Similar to BUSOFF, but was caused by AHB Error.
250  */
251 extern int grcan_read(
252  void *d,
253  CANMsg *msg,
254  size_t count
255 );
256 
257 /*
258  * Transmit CAN messages
259  *
260  * Multiple CAN messages can be transmit in one call.
261  *
262  * d: Device handle
263  * msg: Pointer to messages to transmit
264  * count: Number of CAN messages to transmit
265  *
266  * return:
267  * >=0: Number of CAN messages transmitted. This can be
268  * less than the count parameter.
269  * GRCAN_RET_INVARG: count parameter less than one.
270  * GRCAN_RET_NOTSTARTED: Device not in started mode
271  * GRCAN_RET_TIMEOUT: Timeout in non-blocking mode
272  * GRCAN_RET_BUSOFF: A write was interrupted by a Bus-off error.
273  * Device has left started mode
274  * GRCAN_RET_AHBERR: Similar to BUSOFF, but was caused by AHB Error.
275  */
276 extern int grcan_write(
277  void *d,
278  CANMsg *msg,
279  size_t count
280 );
281 
282 /*
283  * Returns current GRCAN software state
284  *
285  * If STATE_BUSOFF or STATE_AHBERR is returned then the function grcan_stop()
286  * shall be called before continue using the driver.
287  *
288  * d: Device handle
289  * return:
290  * STATE_STOPPED Stopped
291  * STATE_STARTED Started
292  * STATE_BUSOFF Bus-off has been detected
293  * STATE_AHBERR AHB error has been detected
294  */
295 extern int grcan_get_state(void *d);
296 
297 /* The remaining functions return 0 on success and non-zero on failure. */
298 
299 /* Functions controlling operational
300  * mode
301  */
302 /* Bring the link up after open or bus-off */
303 extern int grcan_start(void *d);
304 /* stop to change baud rate/config or closing down */
305 extern int grcan_stop(void *d);
306 /* Wait until all TX messages have been sent */
307 extern int grcan_flush(void *d);
308 
309 /* Functions that require connection
310  * to be stopped
311  */
312 /* enable silent mode read only state */
313 extern int grcan_set_silent(void *d, int silent);
314 /* enable/disable stopping link on AHB Error */
315 extern int grcan_set_abort(void *d, int abort);
316 /* Set Enable0,Enable1,Selection */
317 extern int grcan_set_selection(void *d, const struct grcan_selection *selection);
318 /* Set baudrate by using driver's baud rate timing calculation routines */
319 extern int grcan_set_speed(void *d, unsigned int hz);
320 /* Set baudrate by specifying the timing registers manually */
321 extern int grcan_set_btrs(void *d, const struct grcan_timing *timing);
322 
323 /* Functions can be called whenever */
324 /* Enable/disable Blocking on reception (until at least one message has been received) */
325 int grcan_set_rxblock(void* d, int block);
326 /* Enable/disable Blocking on transmission (until at least one message has been transmitted) */
327 int grcan_set_txblock(void* d, int block);
328 /* Enable/disable Blocking until all requested messages has been sent */
329 int grcan_set_txcomplete(void* d, int complete);
330 /* Enable/disable Blocking until all requested has been received */
331 int grcan_set_rxcomplete(void* d, int complete);
332 /* Get statistics */
333 extern int grcan_get_stats(void *d, struct grcan_stats *stats);
334 /* Clear statistics */
335 extern int grcan_clr_stats(void *d);
336 /* Set Acceptance filters, provide pointer to "struct grcan_filter" or NULL to disable filtering (let all messages pass) */
337 extern int grcan_set_afilter(void *d, const struct grcan_filter *filter);
338 /* Set Sync Messages RX/TX filters, NULL disables the IRQ completely */
339 extern int grcan_set_sfilter(void *d, const struct grcan_filter *filter);
340 /* Get status register of GRCAN core */
341 extern int grcan_get_status(void *d, unsigned int *status);
342 
343 void grcan_register_drv(void);
344 
345 #ifdef __cplusplus
346 }
347 #endif
348 
351 #endif
Definition: grcan.h:95
Definition: grcan.h:89
Definition: grcan.h:70
Definition: grcan.h:101
Definition: grcan.h:32
Definition: inftrees.h:24
Definition: b1553brm.c:75
Definition: grcan.h:81