summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/powerpc/gen5200/mscan/mscan-base.c
blob: 2044bb3917d70d0aa793d99cd2e322bdcfa1e5ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/**
 * @file
 *
 * @ingroup m
 *
 * @brief MSCAN support functions code.
 */

/*
 * Copyright (c) 2008
 * Embedded Brains GmbH
 * Obere Lagerstr. 30
 * D-82178 Puchheim
 * Germany
 * rtems@embedded-brains.de
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#include <bsp.h>
#include <bsp/mscan-base.h>

#define MIN_NO_OF_TQ         7
#define TSEG_1               1
#define TSEG_2               2
#define NO_OF_TABLE_ENTRIES  4
#define SJW                  3

#define CAN_MAX_NO_OF_TQ         25
#define CAN_MAX_NO_OF_TQ_TSEG1   15
#define CAN_MAX_NO_OF_TQ_TSEG2   7
#define CAN_MAX_NO_OF_TQ_SJW     2

/**
 * Time segmant table.
 *
 * <table>
 *   <tr>
 *     <td>Number of time quantas</th>
 *     <td>Time Segment 1</th>
 *     <td>Time Segment 2</th>
 *     <td>Sync: Jump width</th>
 *   </tr>
 * </table>
 */
static uint8_t can_time_segment_table
  [CAN_MAX_NO_OF_TQ - MIN_NO_OF_TQ + 1] [NO_OF_TABLE_ENTRIES] = {
  {7, 4, 2, 1},
  {8, 5, 2, 1},
  {9, 6, 2, 2},
  {10, 6, 3, 2},
  {11, 7, 3, 2},
  {12, 8, 3, 2},
  {13, 8, 4, 2},
  {14, 9, 4, 2},
  {15, 10, 4, 2},
  {16, 10, 5, 2},
  {17, 11, 5, 2},
  {18, 12, 5, 2},
  {19, 12, 6, 2},
  {20, 13, 6, 2},
  {21, 14, 6, 2},
  {22, 14, 7, 2},
  {23, 15, 7, 2},
  {24, 15, 8, 2},
  {25, 16, 8, 2}
};

/**
 * @brief Calculates the MSCAN clock prescaler value.
 */
static uint8_t prescaler_calculation(
  unsigned can_bit_rate,
  unsigned can_clock_frq,
  uint8_t *tq_no
)
{

/* local variables */
  uint8_t presc_val,
    tq_no_dev_min = 0;
  uint32_t bit_rate,
    bit_rate_dev,
    frq_tq,
    bit_rate_dev_min = 0xFFFFFFFF;

/* loop through all values of time quantas */
  for (*tq_no = CAN_MAX_NO_OF_TQ; *tq_no >= MIN_NO_OF_TQ; (*tq_no)--) {

    /* calculate time quanta freq. */
    frq_tq = *tq_no * can_bit_rate;

    /* calculate the optimized prescal. val. */
    presc_val = (can_clock_frq + frq_tq / 2) / frq_tq;

    /* calculate the bitrate */
    bit_rate = can_clock_frq / (*tq_no * presc_val);

    /* calculate the bitrate deviation */
    if (can_bit_rate >= bit_rate) {
      /* calculate the bitrate deviation */
      bit_rate_dev = can_bit_rate - bit_rate;
    } else {
      /* calculate the bitrate deviation */
      bit_rate_dev = bit_rate - can_bit_rate;
    }

    /* check the deviation freq. */
    if (bit_rate_dev == 0) {

      /* return if best match (zero deviation) */
      return (uint8_t) (presc_val);
    } else {

      /* check for minimum of bit rate deviation */
      if (bit_rate_dev < bit_rate_dev_min) {

        /* recognize the minimum freq. deviation */
        bit_rate_dev_min = bit_rate_dev;

        /* recognize the no. of time quantas */
        tq_no_dev_min = *tq_no;
      }
    }
  }

  /* get the no of tq's */
  *tq_no = tq_no_dev_min;

  /* calculate time quanta freq. */
  frq_tq = *tq_no * can_bit_rate;

  /* return the optimized prescaler value */
  return (uint8_t) ((can_clock_frq + frq_tq / 2) / frq_tq);
}

/**
 * @brief Sets the bit rate for the MSCAN module @a m to @a can_bit_rate
 * in [bits/s].
 */
bool mscan_set_bit_rate( volatile mscan *m, unsigned can_bit_rate)
{
  mscan_context context;
  unsigned prescale_val = 0;
  uint8_t tq_no,
    tseg_1,
    tseg_2,
    sseg;

  if (can_bit_rate < MSCAN_BIT_RATE_MIN || can_bit_rate > MSCAN_BIT_RATE_MAX) {
    return false;
  }

  /* Enter initialization mode */
  mscan_initialization_mode_enter( m, &context);

  /* get optimized prescaler value */
  prescale_val = prescaler_calculation(can_bit_rate, IPB_CLOCK, &tq_no);

  /* Check prescaler value */
  if (prescale_val > 64) {
    /* Leave initialization mode */
    mscan_initialization_mode_leave( m, &context);

    return false;
  }

  /* get time segment length from time segment table */
  tseg_1 = can_time_segment_table[tq_no - MIN_NO_OF_TQ][TSEG_1];
  tseg_2 = can_time_segment_table[tq_no - MIN_NO_OF_TQ][TSEG_2];
  sseg = can_time_segment_table[tq_no - MIN_NO_OF_TQ][SJW];

  /* Bus Timing Register 0 MSCAN_A/_B ------------------------------ */
  /*    [07]:SJW1        1 : Synchronization jump width, Bit1       */
  /*    [06]:SJW0        0 : Synchronization jump width, Bit0       */
  /*                         SJW = 2 -> 3 Tq clock cycles           */
  /*    [05]:BRP5        0 : Baud Rate Prescaler, Bit 5             */
  /*    [04]:BRP4        0 : Baud Rate Prescaler, Bit 4             */
  /*    [03]:BRP3        0 : Baud Rate Prescaler, Bit 3             */
  /*    [02]:BRP2        1 : Baud Rate Prescaler, Bit 2             */
  /*    [01]:BRP1        0 : Baud Rate Prescaler, Bit 1             */
  /*    [00]:BRP0        1 : Baud Rate Prescaler, Bit 0             */
  m->btr0 = (BTR0_SJW(sseg - 1) | BTR0_BRP(prescale_val - 1));

  /* Bus Timing Register 1 MSCAN_A/_B ------------------------------ */
  /*    [07]:SAMP        0 : One Sample per bit                     */
  /*    [06]:TSEG22      0 : Time Segment 2, Bit 2                  */
  /*    [05]:TSEG21      1 : Time Segment 2, Bit 1                  */
  /*    [04]:TSEG20      0 : Time Segment 2, Bit 0                  */
  /*                         -> PHASE_SEG2 = 3 Tq                   */
  /*    [03]:TSEG13      0 : Time Segment 1, Bit 3                  */
  /*    [02]:TSEG12      1 : Time Segment 1, Bit 2                  */
  /*    [01]:TSEG11      1 : Time Segment 1, Bit 1                  */
  /*    [00]:TSEG10      0 : Time Segment 1, Bit 0                  */
  m->btr1 = (BTR1_TSEG2(tseg_2 - 1) | BTR1_TSEG1(tseg_1 - 1));

  /* Leave initialization mode */
  mscan_initialization_mode_leave( m, &context);

  return true;
}

/**
 * @brief Disables all interrupts for the MSCAN module @a m.
 */
void mscan_interrupts_disable( volatile mscan *m)
{
  m->rier = 0;
  m->tier = 0;
}

/**
 * @brief Enter initialization mode for the MSCAN module @a m.
 *
 * Saves the current MSCAN context in @a context.
 */
void mscan_initialization_mode_enter( volatile mscan *m, mscan_context *context)
{
  /* Save context */
  context->ctl0 = m->ctl0 & CTL0_TIME;
  context->rier = m->rier;
  context->tier = m->tier;

  /* Request initialization mode */
  m->ctl0 |= CTL0_INITRQ;

  /* Wait for initialization mode acknowledge */
  while ((m->ctl1 & CTL1_INITAK) == 0) {
    /* Wait */
  }
}

/**
 * @brief Leave initialization mode for the MSCAN module @a m.
 *
 * Saves the previous MSCAN context saved in @a context.
 */
void mscan_initialization_mode_leave( volatile mscan *m, const mscan_context *context)
{
  /* Clear initialization mode request */
  m->ctl0 &= ~CTL0_INITRQ;

  /* Wait for clearing of initialization mode acknowledge */
  while ((m->ctl1 & CTL1_INITAK) != 0) {
    /* Wait */
  }

  /* Leave sleep mode */
  mscan_sleep_mode_leave( m);

  /* Restore context */
  m->ctl0 |= context->ctl0;
  m->rier |= context->rier;
  m->tier |= context->tier;
}

/**
 * @brief Enter sleep mode for the MSCAN module @a m.
 */
void mscan_sleep_mode_enter( volatile mscan *m)
{
  /* Request sleep mode */
  m->ctl0 |= CTL0_SLPRQ;
}

/**
 * @brief Leave sleep mode for the MSCAN module @a m.
 */
void mscan_sleep_mode_leave( volatile mscan *m)
{
  /* Clear sleep mode request */
  m->ctl0 &= ~CTL0_SLPRQ;

  /* Wait for clearing of sleep mode acknowledge */
  while ((m->ctl1 & CTL1_SLPAK) != 0) {
    /* Wait */
  }
}

/**
 * @brief Enables and initializes the MSCAN module @a m.
 *
 * The module is set to listen only mode.
 */
bool mscan_enable( volatile mscan *m, unsigned bit_rate)
{
  bool s = true;

  /* Disable the module */
  mscan_disable( m);

  /* Enable module in listen only */
  m->ctl1 = CTL1_CANE | CTL1_LISTEN;

  /* Close acceptance filters */
  m->idac = IDAC_IDAM1 | IDAC_IDAM0;

  /* Clear filter */
  mscan_filter_clear( m);

  /* Set bit rate and leave initialization mode */
  s = mscan_set_bit_rate( m, bit_rate);

  /* Clear all flags */
  m->ctl0 = CTL0_RXFRM;

  /* Disable interrupts */
  mscan_interrupts_disable( m);

  return s;
}

/**
 * @brief Disables the MSCAN module @a m.
 *
 * The module is set to sleep mode and disabled afterwards.
 */
void mscan_disable( volatile mscan *m)
{
  mscan_context context;

  /* Disable interrupts */
  mscan_interrupts_disable( m);

  /* Enter initialization mode */
  mscan_initialization_mode_enter( m, &context);

  /* Disable module */
  m->ctl1 &= ~CTL1_CANE;
}

/**
 * @brief Sets the filter ID and mask registers of the MSCAN module @a m to
 * default values.
 */
void mscan_filter_clear( volatile mscan *m)
{
  mscan_context context;

  mscan_initialization_mode_enter( m, &context);

  /* Setup ID acceptance registers */
  m->idar0 = MSCAN_FILTER_ID_DEFAULT;
  m->idar1 = MSCAN_FILTER_ID_DEFAULT;
  m->idar2 = MSCAN_FILTER_ID_DEFAULT;
  m->idar3 = MSCAN_FILTER_ID_DEFAULT;
  m->idar4 = MSCAN_FILTER_ID_DEFAULT;
  m->idar5 = MSCAN_FILTER_ID_DEFAULT;
  m->idar6 = MSCAN_FILTER_ID_DEFAULT;
  m->idar7 = MSCAN_FILTER_ID_DEFAULT;

  /* Setup ID mask registers */
  m->idmr0 = (uint8_t) MSCAN_FILTER_MASK_DEFAULT;
  m->idmr1 = (uint8_t) MSCAN_FILTER_MASK_DEFAULT;
  m->idmr2 = (uint8_t) MSCAN_FILTER_MASK_DEFAULT;
  m->idmr3 = (uint8_t) MSCAN_FILTER_MASK_DEFAULT;
  m->idmr4 = (uint8_t) MSCAN_FILTER_MASK_DEFAULT;
  m->idmr5 = (uint8_t) MSCAN_FILTER_MASK_DEFAULT;
  m->idmr6 = (uint8_t) MSCAN_FILTER_MASK_DEFAULT;
  m->idmr7 = (uint8_t) MSCAN_FILTER_MASK_DEFAULT;

  mscan_initialization_mode_leave( m, &context);
}

/**
 * @brief Returns the number of active filters of the MSCAN module @a m.
 *
 * @see MSCAN_FILTER_NUMBER_MIN, MSCAN_FILTER_NUMBER_2, MSCAN_FILTER_NUMBER_4
 * and MSCAN_FILTER_NUMBER_MAX.
 */
unsigned mscan_filter_number( volatile mscan *m)
{
  uint8_t idam = m->idac & IDAC_IDAM;

  switch (idam) {
    case 0:
      return MSCAN_FILTER_NUMBER_2;
    case IDAC_IDAM0:
      return MSCAN_FILTER_NUMBER_4;
    case IDAC_IDAM1:
      return MSCAN_FILTER_NUMBER_MAX;
    default:
      return MSCAN_FILTER_NUMBER_MIN;
  }
}

/**
 * @brief Sets the number of active filters of the MSCAN module @a m to @a
 * number and returns true if @a number is valid.
 *
 * @see MSCAN_FILTER_NUMBER_MIN, MSCAN_FILTER_NUMBER_2, MSCAN_FILTER_NUMBER_4
 * and MSCAN_FILTER_NUMBER_MAX.
 */
bool mscan_set_filter_number( volatile mscan *m, unsigned number)
{
  mscan_context context;
  uint8_t idac = IDAC_IDAM1 | IDAC_IDAM0;

  switch (number) {
    case MSCAN_FILTER_NUMBER_MIN:
      break;
    case MSCAN_FILTER_NUMBER_2:
      idac = 0;
      break;
    case MSCAN_FILTER_NUMBER_4:
      idac = IDAC_IDAM0;
      break;
    case MSCAN_FILTER_NUMBER_MAX:
      idac = IDAC_IDAM1;
      break;
    default:
      return false;
  }

  mscan_initialization_mode_enter( m, &context);

  m->idac = idac;

  mscan_initialization_mode_leave( m, &context);

  /* Clear filter */
  mscan_filter_clear( m);

  return true;
}

/**
 * @brief Returns the  address of the CANIDAR register with index @a i of the
 * MSCAN module @a m.
 *
 * @warning The index @a i is not checked if it is in range.
 */
volatile uint8_t *mscan_id_acceptance_register( volatile mscan *m, unsigned i)
{
  volatile uint8_t *const idar [8] = {
    &m->idar0,
    &m->idar1,
    &m->idar2,
    &m->idar3,
    &m->idar4,
    &m->idar5,
    &m->idar6,
    &m->idar7
  };

  return idar [i];
}

/**
 * @brief Returns the  address of the CANIDMR register with index @a i of the
 * MSCAN module @a m.
 *
 * @warning The index @a i is not checked if it is in range.
 */
volatile uint8_t *mscan_id_mask_register( volatile mscan *m, unsigned i)
{
  volatile uint8_t *const idmr [8] = {
    &m->idmr0,
    &m->idmr1,
    &m->idmr2,
    &m->idmr3,
    &m->idmr4,
    &m->idmr5,
    &m->idmr6,
    &m->idmr7
  };

  return idmr [i];
}

/**
 * @brief Sets or gets the filter ID and mask in @a id and @a mask depending on
 * @a set of MSCAN module @a m.  The filter is selected by the value of @a
 * index.
 *
 * Returns true if the operation was successful.
 */
bool mscan_filter_operation(
  volatile mscan *m,
  bool set,
  unsigned index,
  uint32_t *id,
  uint32_t *mask
)
{
  unsigned number = mscan_filter_number( m);
  unsigned offset = MSCAN_FILTER_NUMBER_MAX / number;
  unsigned shift = 24;

  volatile uint8_t *idar = NULL;
  volatile uint8_t *idmr = NULL;

  if (!set) {
    *id = MSCAN_FILTER_ID_DEFAULT;
    *mask = MSCAN_FILTER_MASK_DEFAULT;
  }

  if (index < number) {
    mscan_context context;

    mscan_initialization_mode_enter( m, &context);

    index *= offset;
    offset += index;
    while (index < offset) {
      idar = mscan_id_acceptance_register( m, index);
      idmr = mscan_id_mask_register( m, index);

      if (set) {
        *idar = (uint8_t) (*id >> shift);
        *idmr = (uint8_t) (*mask >> shift);
      } else {
        *id = (*id & ~(0xffU << shift)) | (*idar << shift);
        *mask = (*mask & ~(0xffU << shift)) | (*idmr << shift);
      }

      shift -= 8;

      ++index;
    }

    mscan_initialization_mode_leave( m, &context);
  } else {
    return false;
  }

  return true;
}

/**
 * @brief Returns the receiver and transmitter error counter values in @a rec
 * and @a tec of MSCAN module @a m.
 */
void mscan_get_error_counters( volatile mscan *m, unsigned *rec, unsigned *tec)
{
  mscan_context context;

  mscan_initialization_mode_enter( m, &context);

  *rec = m->rxerr;
  *tec = m->txerr;

  mscan_initialization_mode_leave( m, &context);
}