Subversion Repositories svn.mios32

Rev

Rev 2646 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2646 Antichambr 1
// $Id: mios32_can.c 2312 2016-02-27 23:04:51Z tk $
2
//! \defgroup MIOS32_CAN
3
//!
4
//! U(S)ART functions for MIOS32
5
//!
6
//! Applications shouldn't call these functions directly, instead please use \ref MIOS32_COM or \ref MIOS32_MIDI layer functions
7
//!
8
//! \{
9
/* ==========================================================================
10
 *
11
 *  Copyright (C) 2008 Thorsten Klose (tk@midibox.org)
12
 *  Licensed for personal non-commercial use only.
13
 *  All other rights reserved.
14
 *
15
 * ==========================================================================
16
 */
17
 
18
/////////////////////////////////////////////////////////////////////////////
19
// Include files
20
/////////////////////////////////////////////////////////////////////////////
21
 
22
#include <mios32.h>
23
 
24
// this module can be optionally enabled in a local mios32_config.h file (included from mios32.h)
25
#if defined(MIOS32_USE_CAN)
26
 
27
 
28
/////////////////////////////////////////////////////////////////////////////
29
// Pin definitions and USART mappings
30
/////////////////////////////////////////////////////////////////////////////
31
 
32
// how many CANs are supported?
33
#define NUM_SUPPORTED_CANS 0
34
 
35
/////////////////////////////////////////////////////////////////////////////
36
// Local variables
37
/////////////////////////////////////////////////////////////////////////////
38
 
39
 
40
/////////////////////////////////////////////////////////////////////////////
41
//! Initializes CAN MIDI layer
42
//! \param[in] mode currently only mode 0 supported
43
//! \return < 0 if initialisation failed
44
//! \note Applications shouldn't call this function directly, instead please use \ref MIOS32_MIDI layer functions
45
/////////////////////////////////////////////////////////////////////////////
46
s32 MIOS32_CAN_VerboseSet(u8 level)
47
{
48
#if MIOS32_CAN_NUM == 0
49
  return -1; // no CAN enabled
50
#else
51
  return 0; // no error
52
#endif
53
}
54
 
55
 
56
/////////////////////////////////////////////////////////////////////////////
57
//! Initializes CAN MIDI layer
58
//! \param[in] mode currently only mode 0 supported
59
//! \return < 0 if initialisation failed
60
//! \note Applications shouldn't call this function directly, instead please use \ref MIOS32_MIDI layer functions
61
/////////////////////////////////////////////////////////////////////////////
62
s32 MIOS32_CAN_VerboseGet(void)
63
{
64
#if MIOS32_CAN_NUM == 0
65
  return -1; // no CAN enabled
66
#else
67
  return 0; // no error
68
#endif
69
}
70
 
71
 
72
/////////////////////////////////////////////////////////////////////////////
73
//! Initializes CAN interfaces
74
//! \param[in] mode currently only mode 0 supported
75
//! \return < 0 if initialisation failed
76
//! \note Applications shouldn't call this function directly, instead please use \ref MIOS32_COM or \ref MIOS32_MIDI layer functions
77
/////////////////////////////////////////////////////////////////////////////
78
s32 MIOS32_CAN_Init(u32 mode)
79
{
80
  // currently only mode 0 supported
81
  if( mode != 0 )
82
    return -1; // unsupported mode
83
 
84
#if NUM_SUPPORTED_CANS == 0
85
  return -1; // no CANs
86
#else
87
  return 0; // no error
88
#endif
89
}
90
 
91
 
92
/////////////////////////////////////////////////////////////////////////////
93
//! \return 0 if CAN is not assigned to a MIDI function
94
//! \return 1 if CAN is assigned to a MIDI function
95
/////////////////////////////////////////////////////////////////////////////
96
s32 MIOS32_CAN_IsAssignedToMIDI(u8 can)
97
{
98
#if NUM_SUPPORTED_CANS == 0
99
  return 0; // no CAN available
100
#else
101
  return 0;
102
#endif
103
}
104
 
105
 
106
/////////////////////////////////////////////////////////////////////////////
107
//! Initializes a given CAN interface based on given baudrate and TX output mode
108
//! \param[in] CAN number (0..1)
109
//! \param[in] is_midi MIDI or common CAN interface?
110
//! \return < 0 if initialisation failed
111
/////////////////////////////////////////////////////////////////////////////
112
s32 MIOS32_CAN_InitPort(u8 can, u8 is_midi)
113
{
114
#if NUM_SUPPORTED_CANS == 0
115
  return -1; // no CAN available
116
#else
117
  return 0; // no error
118
#endif
119
}
120
 
121
 
122
/////////////////////////////////////////////////////////////////////////////
123
//! Initializes a given CAN interface based on default settings
124
//! \param[in] CAN number (0..1)
125
//! \return < 0 if initialisation failed
126
/////////////////////////////////////////////////////////////////////////////
127
s32 MIOS32_CAN_InitPortDefault(u8 can)
128
{
129
#if NUM_SUPPORTED_CANS == 0
130
  return -1; // no CAN available
131
#else
132
  return 0; // no error
133
#endif
134
}
135
 
136
 
137
/////////////////////////////////////////////////////////////////////////////
138
//! sets the baudrate of a CAN port
139
//! \param[in] CAN number (0..1)
140
//! \return -1: can not available
141
//! \return -2: function not prepared for this CAN
142
//! \return -3: CAN Initialisation failed
143
/////////////////////////////////////////////////////////////////////////////
144
s32 MIOS32_CAN_InitPeriph(u8 can)
145
{
146
#if NUM_SUPPORTED_CANS == 0
147
  return -1; // no CAN available
148
#else
149
  return 0;
150
#endif
151
}
152
 
153
/////////////////////////////////////////////////////////////////////////////
154
//! Initializes a 32 bits filter
155
//! \param[in] can filter bank number
156
//! \param[in] extended id for filter
157
//! \param[in] extended id for mask
158
//! \return < 0 if initialisation failed
159
/////////////////////////////////////////////////////////////////////////////
2657 Antichambr 160
s32 MIOS32_CAN_Init32bitFilter(u8 bank, u8 fifo, mios32_can_ext_filter_t filter, u8 enabled)
2646 Antichambr 161
{
162
#if MIOS32_CAN_NUM == 0
163
  return -1; // no CAN enabled
164
#else
165
  return 0; // no error
166
#endif
167
}
168
 
169
/////////////////////////////////////////////////////////////////////////////
170
//! Initializes a 16 bits filter
171
//! \param[in] can filter bank number
172
//! \param[in] standard id for filter
173
//! \param[in] standard id for mask
174
//! \return < 0 if initialisation failed
175
/////////////////////////////////////////////////////////////////////////////
2657 Antichambr 176
s32 MIOS32_CAN_Init16bitFilter(u8 bank, u8 fifo, mios32_can_std_filter_t filter1, mios32_can_std_filter_t filter2, u8 enabled)
2646 Antichambr 177
{
178
#if MIOS32_CAN_NUM == 0
179
  return -1; // no CAN enabled
180
#else
181
  return 0; // no error
182
#endif
183
}
184
 
185
/////////////////////////////////////////////////////////////////////////////
186
//! sets the baudrate of a CAN port
187
//! \param[in] CAN number (0..1)
188
//! \return -1: can not available
189
//! \return -2: function not prepared for this CAN
190
//! \return -3: CAN Initialisation failed
191
/////////////////////////////////////////////////////////////////////////////
2657 Antichambr 192
s32 MIOS32_CAN_InitPacket(mios32_can_packet_t *packet)
2646 Antichambr 193
{
194
#if NUM_SUPPORTED_CANS == 0
195
  return -1; // no CAN available
196
#else
197
  return 0;
198
#endif
199
}
200
 
201
/////////////////////////////////////////////////////////////////////////////
202
//! returns number of free bytes in receive buffer
203
//! \param[in] CAN number (0..1)
204
//! \return can number of free bytes
205
//! \return 1: can available
206
//! \return 0: can not available
207
/////////////////////////////////////////////////////////////////////////////
208
s32 MIOS32_CAN_RxBufferFree(u8 can)
209
{
210
#if NUM_SUPPORTED_CANS == 0
211
  return 0; // no CAN available
212
#else
213
  return 0;
214
#endif
215
}
216
 
217
 
218
/////////////////////////////////////////////////////////////////////////////
219
//! returns number of used bytes in receive buffer
220
//! \param[in] CAN number (0..1)
221
//! \return > 0: number of used bytes
222
//! \return 0 if can not available
223
//! \note Applications shouldn't call these functions directly, instead please use \ref MIOS32_COM or \ref MIOS32_MIDI layer functions
224
/////////////////////////////////////////////////////////////////////////////
225
s32 MIOS32_CAN_RxBufferUsed(u8 can)
226
{
227
#if NUM_SUPPORTED_CANS == 0
228
  return 0; // no CAN available
229
#else
230
  return 0;
231
#endif
232
}
233
 
234
 
235
/////////////////////////////////////////////////////////////////////////////
236
//! gets a byte from the receive buffer
237
//! \param[in] CAN number (0..1)
238
//! \return -1 if CAN not available
239
//! \return -2 if no new byte available
240
//! \return >= 0: number of received bytes
241
//! \note Applications shouldn't call these functions directly, instead please use \ref MIOS32_COM or \ref MIOS32_MIDI layer functions
242
/////////////////////////////////////////////////////////////////////////////
2657 Antichambr 243
s32 MIOS32_CAN_RxBufferGet(u8 can, mios32_can_packet_t *p)
2646 Antichambr 244
{
245
#if NUM_SUPPORTED_CANS == 0
246
  return -1; // no CAN available
247
#else
248
  return 0; // return received byte
249
#endif
250
}
251
 
252
 
253
/////////////////////////////////////////////////////////////////////////////
254
//! returns the next byte of the receive buffer without taking it
255
//! \param[in] CAN number (0..1)
256
//! \return -1 if CAN not available
257
//! \return -2 if no new byte available
258
//! \return >= 0: number of received bytes
259
//! \note Applications shouldn't call these functions directly, instead please use \ref MIOS32_COM or \ref MIOS32_MIDI layer functions
260
/////////////////////////////////////////////////////////////////////////////
2657 Antichambr 261
s32 MIOS32_CAN_RxBufferPeek(u8 can, mios32_can_packet_t *p)
2646 Antichambr 262
{
263
#if NUM_SUPPORTED_CANS == 0
264
  return -1; // no CAN available
265
#else
266
  return 0; // return received byte
267
#endif
268
}
269
 
270
/////////////////////////////////////////////////////////////////////////////
271
//! remove the next byte of the receive buffer without taking it
272
//! \param[in] CAN number (0..1)
273
//! \return -1 if CAN not available
274
//! \return -2 if no new byte available
275
//! \return >= 0: number of received bytes
276
//! \note Applications shouldn't call these functions directly, instead please use \ref MIOS32_COM or \ref MIOS32_MIDI layer functions
277
/////////////////////////////////////////////////////////////////////////////
278
s32 MIOS32_CAN_RxBufferRemove(u8 can)
279
{
280
#if NUM_SUPPORTED_CANS == 0
281
  return -1; // no CAN available
282
#else
283
  return 0; // return received byte
284
#endif
285
}
286
 
287
/////////////////////////////////////////////////////////////////////////////
288
//! puts a byte onto the receive buffer
289
//! \param[in] CAN number (0..1)
290
//! \param[in] b byte which should be put into Rx buffer
291
//! \return 0 if no error
292
//! \return -1 if CAN not available
293
//! \return -2 if buffer full (retry)
294
//! \note Applications shouldn't call these functions directly, instead please use \ref MIOS32_COM or \ref MIOS32_MIDI layer functions
295
/////////////////////////////////////////////////////////////////////////////
2657 Antichambr 296
s32 MIOS32_CAN_RxBufferPut(u8 can, mios32_can_packet_t p)
2646 Antichambr 297
{
298
#if NUM_SUPPORTED_CANS == 0
299
  return -1; // no CAN available
300
#else
301
  return 0; // no error
302
#endif
303
}
304
 
305
/////////////////////////////////////////////////////////////////////////////
306
//! returns number of free bytes in transmit buffer
307
//! \param[in] CAN number (0..1)
308
//! \return number of free bytes
309
//! \return 0 if can not available
310
//! \note Applications shouldn't call these functions directly, instead please use \ref MIOS32_COM or \ref MIOS32_MIDI layer functions
311
/////////////////////////////////////////////////////////////////////////////
312
s32 MIOS32_CAN_TxBufferFree(u8 can)
313
{
314
#if NUM_SUPPORTED_CANS == 0
315
  return -1; // no CAN available
316
#else
317
  if( can >= NUM_SUPPORTED_CANS )
318
    return -1;
319
  else
320
    return MIOS32_CAN_TX_BUFFER_SIZE - tx_buffer_size[can];
321
#endif
322
}
323
 
324
 
325
/////////////////////////////////////////////////////////////////////////////
326
//! returns number of used bytes in transmit buffer
327
//! \param[in] CAN number (0..1)
328
//! \return number of used bytes
329
//! \return 0 if can not available
330
//! \note Applications shouldn't call these functions directly, instead please use \ref MIOS32_COM or \ref MIOS32_MIDI layer functions
331
/////////////////////////////////////////////////////////////////////////////
332
s32 MIOS32_CAN_TxBufferUsed(u8 can)
333
{
334
#if NUM_SUPPORTED_CANS == 0
335
  return -1; // no CAN available
336
#else
337
  if( can >= NUM_SUPPORTED_CANS )
338
    return -1;
339
  else
340
    return tx_buffer_size[can];
341
#endif
342
}
343
 
344
 
345
/////////////////////////////////////////////////////////////////////////////
346
//! gets a byte from the transmit buffer
347
//! \param[in] CAN number (0..1)
348
//! \return -1 if CAN not available
349
//! \return -2 if no new byte available
350
//! \return >= 0: transmitted byte
351
//! \note Applications shouldn't call these functions directly, instead please use \ref MIOS32_COM or \ref MIOS32_MIDI layer functions
352
/////////////////////////////////////////////////////////////////////////////
2657 Antichambr 353
s32 MIOS32_CAN_TxBufferGet(u8 can, mios32_can_packet_t *p)
2646 Antichambr 354
{
355
#if NUM_SUPPORTED_CANS == 0
356
  return -1; // no CAN available
357
#else
358
  return 0; // no error
359
#endif
360
}
361
 
362
 
363
/////////////////////////////////////////////////////////////////////////////
364
//! puts more than one byte onto the transmit buffer (used for atomic sends)
365
//! \param[in] CAN number (0..1)
366
//! \param[in] *buffer pointer to buffer to be sent
367
//! \param[in] len number of bytes to be sent
368
//! \return 0 if no error
369
//! \return -1 if CAN not available
370
//! \return -2 if buffer full or cannot get all requested bytes (retry)
371
//! \return -3 if CAN not supported by MIOS32_CAN_TxBufferPut Routine
372
//! \note Applications shouldn't call these functions directly, instead please use \ref MIOS32_COM or \ref MIOS32_MIDI layer functions
373
/////////////////////////////////////////////////////////////////////////////
2657 Antichambr 374
s32 MIOS32_CAN_TxBufferPutMore_NonBlocking(u8 can, mios32_can_packet_t* p,u16 len)
2646 Antichambr 375
{
376
#if NUM_SUPPORTED_CANS == 0
377
  return -1; // no CAN available
378
#else
379
  return 0; // no error
380
#endif
381
}
382
 
383
/////////////////////////////////////////////////////////////////////////////
384
//! puts more than one byte onto the transmit buffer (used for atomic sends)<BR>
385
//! (blocking function)
386
//! \param[in] CAN number (0..1)
387
//! \param[in] *buffer pointer to buffer to be sent
388
//! \param[in] len number of bytes to be sent
389
//! \return 0 if no error
390
//! \return -1 if CAN not available
391
//! \return -3 if CAN not supported by MIOS32_CAN_TxBufferPut Routine
392
//! \note Applications shouldn't call these functions directly, instead please use \ref MIOS32_COM or \ref MIOS32_MIDI layer functions
393
/////////////////////////////////////////////////////////////////////////////
2657 Antichambr 394
s32 MIOS32_CAN_TxBufferPutMore(u8 can, mios32_can_packet_t *packets, u16 len)
2646 Antichambr 395
{
396
  s32 error;
397
 
398
  while( (error=MIOS32_CAN_TxBufferPutMore_NonBlocking(can, packets, len)) == -2 );
399
 
400
  return error;
401
}
402
 
403
 
404
/////////////////////////////////////////////////////////////////////////////
405
//! puts a byte onto the transmit buffer
406
//! \param[in] CAN number (0..1)
407
//! \param[in] b byte which should be put into Tx buffer
408
//! \return 0 if no error
409
//! \return -1 if CAN not available
410
//! \return -2 if buffer full (retry)
411
//! \return -3 if CAN not supported by MIOS32_CAN_TxBufferPut Routine
412
//! \note Applications shouldn't call these functions directly, instead please use \ref MIOS32_COM or \ref MIOS32_MIDI layer functions
413
/////////////////////////////////////////////////////////////////////////////
2657 Antichambr 414
s32 MIOS32_CAN_TxBufferPut_NonBlocking(u8 can, mios32_can_packet_t p)
2646 Antichambr 415
{
416
  // for more comfortable usage...
417
  // -> just forward to MIOS32_CAN_TxBufferPutMore
418
  return MIOS32_CAN_TxBufferPutMore(can, &p, 1);
419
}
420
 
421
 
422
/////////////////////////////////////////////////////////////////////////////
423
//! puts a byte onto the transmit buffer<BR>
424
//! (blocking function)
425
//! \param[in] CAN number (0..1)
426
//! \param[in] b byte which should be put into Tx buffer
427
//! \return 0 if no error
428
//! \return -1 if CAN not available
429
//! \return -3 if CAN not supported by MIOS32_CAN_TxBufferPut Routine
430
//! \note Applications shouldn't call these functions directly, instead please use \ref MIOS32_COM or \ref MIOS32_MIDI layer functions
431
/////////////////////////////////////////////////////////////////////////////
2657 Antichambr 432
s32 MIOS32_CAN_TxBufferPut(u8 can, mios32_can_packet_t p)
2646 Antichambr 433
{
434
  s32 error;
435
 
436
  while( (error=MIOS32_CAN_TxBufferPutMore(can, &p, 1)) == -2 );
437
 
438
  return error;
439
}
440
 
441
 
442
/////////////////////////////////////////////////////////////////////////////
443
// Used during transmit or receive polling to determine if a bus error has occured
444
// (e.g. receiver passive or no nodes connected to bus)
445
// In this case, all pending transmissions will be aborted
446
// The midian_state.PANIC flag is set to report to the application, that the
447
// bus is not ready for transactions (flag accessible via MIDIAN_ErrorStateGet).
448
// This flag will be cleared by WaitAck once we got back a message from any slave
449
// OUT: returns -1 if bus permanent off (requires re-initialisation)
450
//      returns -2 if panic state reached
451
/////////////////////////////////////////////////////////////////////////////
452
s32 MIOS32_CAN_BusErrorCheck(u8 can)
453
{
454
#if NUM_SUPPORTED_CANS == 0
455
  return -1; // no CAN available
456
#else
457
  return 0 ;
458
#endif
459
}
460
 
461
/////////////////////////////////////////////////////////////////////////////
462
//! transmit more than one byte
463
//! \param[in] CAN number (0..1)
464
//! \param[in] *buffer pointer to buffer to be sent
465
//! \param[in] len number of bytes to be sent
466
//! \return 0 if no error
467
//! \return -1 if CAN not available
468
//! \return -2 if buffer full or cannot get all requested bytes (retry)
469
//! \return -3 if CAN not supported by MIOS32_CAN_TxBufferPut Routine
470
//! \note Applications shouldn't call these functions directly, instead please use \ref MIOS32_COM or \ref MIOS32_MIDI layer functions
471
/////////////////////////////////////////////////////////////////////////////
2657 Antichambr 472
s32 MIOS32_CAN_Transmit(u8 can, mios32_can_packet_t p, s16 block_time)
2646 Antichambr 473
{
474
#if NUM_SUPPORTED_CANS == 0
475
  return -1; // no CAN available
476
#else
477
  return 0; // no error
478
#endif
479
}
480
 
481
/////////////////////////////////////////////////////////////////////////////
482
//
483
/////////////////////////////////////////////////////////////////////////////
2657 Antichambr 484
s32 MIOS32_CAN_ReportLastErr(u8 can, mios32_can_stat_err_t* err)
2646 Antichambr 485
{
486
#if NUM_SUPPORTED_CANS == 0
487
  return -1; // no CAN available
488
#else
489
  return 0; // no error
490
#endif
491
}
492
 
493
/////////////////////////////////////////////////////////////////////////////
494
//
495
/////////////////////////////////////////////////////////////////////////////
2657 Antichambr 496
s32 MIOS32_CAN_ReportGetCurr(u8 can, mios32_can_stat_report_t* report)
2646 Antichambr 497
{
498
#if NUM_SUPPORTED_CANS == 0
499
  return -1; // no CAN available
500
#else
501
  return 0; // no error
502
#endif
503
}
504
 
505
/////////////////////////////////////////////////////////////////////////////
506
//
507
/////////////////////////////////////////////////////////////////////////////
508
s32 MIOS32_CAN_ReportReset(u8 can)
509
{
510
#if NUM_SUPPORTED_CANS == 0
511
  return -1; // no CAN available
512
#else
513
  return 0; // no error
514
#endif
515
}
516
 
517
 
518
#endif /* MIOS32_USE_CAN */