source: rtems/c/src/lib/libbsp/powerpc/gen5200/mscan/mscan-base.c @ 3246789a

4.115
Last change on this file since 3246789a was 3246789a, checked in by Sebastian Huber <sebastian.huber@…>, on 11/18/10 at 11:08:41

2010-11-18 Sebastian Huber <sebastian.huber@…>

  • mscan/mscan-base.c: Do not use sleep mode to enter init mode. Do not wait for sleep mode acknowledge.
  • Property mode set to 100644
File size: 13.1 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup m
5 *
6 * @brief MSCAN support functions code.
7 */
8
9/*
10 * Copyright (c) 2008
11 * Embedded Brains GmbH
12 * Obere Lagerstr. 30
13 * D-82178 Puchheim
14 * Germany
15 * rtems@embedded-brains.de
16 *
17 * The license and distribution terms for this file may be found in the file
18 * LICENSE in this distribution or at http://www.rtems.com/license/LICENSE.
19 */
20
21#include <bsp.h>
22#include <bsp/mscan-base.h>
23
24#define MIN_NO_OF_TQ         7
25#define TSEG_1               1
26#define TSEG_2               2
27#define NO_OF_TABLE_ENTRIES  4
28#define SJW                  3
29
30#define CAN_MAX_NO_OF_TQ         25
31#define CAN_MAX_NO_OF_TQ_TSEG1   15
32#define CAN_MAX_NO_OF_TQ_TSEG2   7
33#define CAN_MAX_NO_OF_TQ_SJW     2
34
35/**
36 * Time segmant table.
37 *
38 * <table>
39 *   <tr>
40 *     <td>Number of time quantas</th>
41 *     <td>Time Segment 1</th>
42 *     <td>Time Segment 2</th>
43 *     <td>Sync: Jump width</th>
44 *   </tr>
45 * </table>
46 */
47static uint8_t can_time_segment_table
48  [CAN_MAX_NO_OF_TQ - MIN_NO_OF_TQ + 1] [NO_OF_TABLE_ENTRIES] = {
49  {7, 4, 2, 1},
50  {8, 5, 2, 1},
51  {9, 6, 2, 2},
52  {10, 6, 3, 2},
53  {11, 7, 3, 2},
54  {12, 8, 3, 2},
55  {13, 8, 4, 2},
56  {14, 9, 4, 2},
57  {15, 10, 4, 2},
58  {16, 10, 5, 2},
59  {17, 11, 5, 2},
60  {18, 12, 5, 2},
61  {19, 12, 6, 2},
62  {20, 13, 6, 2},
63  {21, 14, 6, 2},
64  {22, 14, 7, 2},
65  {23, 15, 7, 2},
66  {24, 15, 8, 2},
67  {25, 16, 8, 2}
68};
69
70/**
71 * @brief Calculates the MSCAN clock prescaler value.
72 */
73static uint8_t prescaler_calculation(
74  unsigned can_bit_rate,
75  unsigned can_clock_frq,
76  uint8_t *tq_no
77)
78{
79
80/* local variables */
81  uint8_t presc_val,
82    tq_no_dev_min = 0;
83  uint32_t bit_rate,
84    bit_rate_dev,
85    frq_tq,
86    bit_rate_dev_min = 0xFFFFFFFF;
87
88/* loop through all values of time quantas */
89  for (*tq_no = CAN_MAX_NO_OF_TQ; *tq_no >= MIN_NO_OF_TQ; (*tq_no)--) {
90
91    /* calculate time quanta freq. */
92    frq_tq = *tq_no * can_bit_rate;
93
94    /* calculate the optimized prescal. val. */
95    presc_val = (can_clock_frq + frq_tq / 2) / frq_tq;
96
97    /* calculate the bitrate */
98    bit_rate = can_clock_frq / (*tq_no * presc_val);
99
100    /* calculate the bitrate deviation */
101    if (can_bit_rate >= bit_rate) {
102      /* calculate the bitrate deviation */
103      bit_rate_dev = can_bit_rate - bit_rate;
104    } else {
105      /* calculate the bitrate deviation */
106      bit_rate_dev = bit_rate - can_bit_rate;
107    }
108
109    /* check the deviation freq. */
110    if (bit_rate_dev == 0) {
111
112      /* return if best match (zero deviation) */
113      return (uint8_t) (presc_val);
114    } else {
115
116      /* check for minimum of bit rate deviation */
117      if (bit_rate_dev < bit_rate_dev_min) {
118
119        /* recognize the minimum freq. deviation */
120        bit_rate_dev_min = bit_rate_dev;
121
122        /* recognize the no. of time quantas */
123        tq_no_dev_min = *tq_no;
124      }
125    }
126  }
127
128  /* get the no of tq's */
129  *tq_no = tq_no_dev_min;
130
131  /* calculate time quanta freq. */
132  frq_tq = *tq_no * can_bit_rate;
133
134  /* return the optimized prescaler value */
135  return (uint8_t) ((can_clock_frq + frq_tq / 2) / frq_tq);
136}
137
138/**
139 * @brief Sets the bit rate for the MSCAN module @a m to @a can_bit_rate
140 * in [bits/s].
141 */
142bool mscan_set_bit_rate( volatile mscan *m, unsigned can_bit_rate)
143{
144  mscan_context context;
145  unsigned prescale_val = 0;
146  uint8_t tq_no,
147    tseg_1,
148    tseg_2,
149    sseg;
150
151  if (can_bit_rate < MSCAN_BIT_RATE_MIN || can_bit_rate > MSCAN_BIT_RATE_MAX) {
152    return false;
153  }
154
155  /* Enter initialization mode */
156  mscan_initialization_mode_enter( m, &context);
157
158  /* get optimized prescaler value */
159  prescale_val = prescaler_calculation(can_bit_rate, IPB_CLOCK, &tq_no);
160
161  /* Check prescaler value */
162  if (prescale_val > 64) {
163    /* Leave initialization mode */
164    mscan_initialization_mode_leave( m, &context);
165
166    return false;
167  }
168
169  /* get time segment length from time segment table */
170  tseg_1 = can_time_segment_table[tq_no - MIN_NO_OF_TQ][TSEG_1];
171  tseg_2 = can_time_segment_table[tq_no - MIN_NO_OF_TQ][TSEG_2];
172  sseg = can_time_segment_table[tq_no - MIN_NO_OF_TQ][SJW];
173
174  /* Bus Timing Register 0 MSCAN_A/_B ------------------------------ */
175  /*    [07]:SJW1        1 : Synchronization jump width, Bit1       */
176  /*    [06]:SJW0        0 : Synchronization jump width, Bit0       */
177  /*                         SJW = 2 -> 3 Tq clock cycles           */
178  /*    [05]:BRP5        0 : Baud Rate Prescaler, Bit 5             */
179  /*    [04]:BRP4        0 : Baud Rate Prescaler, Bit 4             */
180  /*    [03]:BRP3        0 : Baud Rate Prescaler, Bit 3             */
181  /*    [02]:BRP2        1 : Baud Rate Prescaler, Bit 2             */
182  /*    [01]:BRP1        0 : Baud Rate Prescaler, Bit 1             */
183  /*    [00]:BRP0        1 : Baud Rate Prescaler, Bit 0             */
184  m->btr0 = (BTR0_SJW(sseg - 1) | BTR0_BRP(prescale_val - 1));
185
186  /* Bus Timing Register 1 MSCAN_A/_B ------------------------------ */
187  /*    [07]:SAMP        0 : One Sample per bit                     */
188  /*    [06]:TSEG22      0 : Time Segment 2, Bit 2                  */
189  /*    [05]:TSEG21      1 : Time Segment 2, Bit 1                  */
190  /*    [04]:TSEG20      0 : Time Segment 2, Bit 0                  */
191  /*                         -> PHASE_SEG2 = 3 Tq                   */
192  /*    [03]:TSEG13      0 : Time Segment 1, Bit 3                  */
193  /*    [02]:TSEG12      1 : Time Segment 1, Bit 2                  */
194  /*    [01]:TSEG11      1 : Time Segment 1, Bit 1                  */
195  /*    [00]:TSEG10      0 : Time Segment 1, Bit 0                  */
196  m->btr1 = (BTR1_TSEG2(tseg_2 - 1) | BTR1_TSEG1(tseg_1 - 1));
197
198  /* Leave initialization mode */
199  mscan_initialization_mode_leave( m, &context);
200
201  return true;
202}
203
204/**
205 * @brief Disables all interrupts for the MSCAN module @a m.
206 */
207void mscan_interrupts_disable( volatile mscan *m)
208{
209  m->rier = 0;
210  m->tier = 0;
211}
212
213/**
214 * @brief Enter initialization mode for the MSCAN module @a m.
215 *
216 * Saves the current MSCAN context in @a context.
217 */
218void mscan_initialization_mode_enter( volatile mscan *m, mscan_context *context)
219{
220  /* Save context */
221  context->ctl0 = m->ctl0 & CTL0_TIME;
222  context->rier = m->rier;
223  context->tier = m->tier;
224
225  /* Request initialization mode */
226  m->ctl0 |= CTL0_INITRQ;
227
228  /* Wait for initialization mode acknowledge */
229  while ((m->ctl1 & CTL1_INITAK) == 0) {
230    /* Wait */
231  }
232}
233
234/**
235 * @brief Leave initialization mode for the MSCAN module @a m.
236 *
237 * Saves the previous MSCAN context saved in @a context.
238 */
239void mscan_initialization_mode_leave( volatile mscan *m, const mscan_context *context)
240{
241  /* Clear initialization mode request */
242  m->ctl0 &= ~CTL0_INITRQ;
243
244  /* Wait for clearing of initialization mode acknowledge */
245  while ((m->ctl1 & CTL1_INITAK) != 0) {
246    /* Wait */
247  }
248
249  /* Leave sleep mode */
250  mscan_sleep_mode_leave( m);
251
252  /* Restore context */
253  m->ctl0 |= context->ctl0;
254  m->rier |= context->rier;
255  m->tier |= context->tier;
256}
257
258/**
259 * @brief Enter sleep mode for the MSCAN module @a m.
260 */
261void mscan_sleep_mode_enter( volatile mscan *m)
262{
263  /* Request sleep mode */
264  m->ctl0 |= CTL0_SLPRQ;
265}
266
267/**
268 * @brief Leave sleep mode for the MSCAN module @a m.
269 */
270void mscan_sleep_mode_leave( volatile mscan *m)
271{
272  /* Clear sleep mode request */
273  m->ctl0 &= ~CTL0_SLPRQ;
274
275  /* Wait for clearing of sleep mode acknowledge */
276  while ((m->ctl1 & CTL1_SLPAK) != 0) {
277    /* Wait */
278  }
279}
280
281/**
282 * @brief Enables and initializes the MSCAN module @a m.
283 *
284 * The module is set to listen only mode.
285 */
286bool mscan_enable( volatile mscan *m, unsigned bit_rate)
287{
288  bool s = true;
289
290  /* Disable the module */
291  mscan_disable( m);
292
293  /* Enable module in listen only */
294  m->ctl1 = CTL1_CANE | CTL1_LISTEN;
295
296  /* Close acceptance filters */
297  m->idac = IDAC_IDAM1 | IDAC_IDAM0;
298
299  /* Clear filter */
300  mscan_filter_clear( m);
301
302  /* Set bit rate and leave initialization mode */
303  s = mscan_set_bit_rate( m, bit_rate);
304
305  /* Clear all flags */
306  m->ctl0 = CTL0_RXFRM;
307
308  /* Disable interrupts */
309  mscan_interrupts_disable( m);
310
311  return s;
312}
313
314/**
315 * @brief Disables the MSCAN module @a m.
316 *
317 * The module is set to sleep mode and disabled afterwards.
318 */
319void mscan_disable( volatile mscan *m)
320{
321  mscan_context context;
322
323  /* Disable interrupts */
324  mscan_interrupts_disable( m);
325
326  /* Enter initialization mode */
327  mscan_initialization_mode_enter( m, &context);
328
329  /* Disable module */
330  m->ctl1 &= ~CTL1_CANE;
331}
332
333/**
334 * @brief Sets the filter ID and mask registers of the MSCAN module @a m to
335 * default values.
336 */
337void mscan_filter_clear( volatile mscan *m)
338{
339  mscan_context context;
340
341  mscan_initialization_mode_enter( m, &context);
342
343  /* Setup ID acceptance registers */
344  m->idar0 = MSCAN_FILTER_ID_DEFAULT;
345  m->idar1 = MSCAN_FILTER_ID_DEFAULT;
346  m->idar2 = MSCAN_FILTER_ID_DEFAULT;
347  m->idar3 = MSCAN_FILTER_ID_DEFAULT;
348  m->idar4 = MSCAN_FILTER_ID_DEFAULT;
349  m->idar5 = MSCAN_FILTER_ID_DEFAULT;
350  m->idar6 = MSCAN_FILTER_ID_DEFAULT;
351  m->idar7 = MSCAN_FILTER_ID_DEFAULT;
352
353  /* Setup ID mask registers */
354  m->idmr0 = (uint8_t) MSCAN_FILTER_MASK_DEFAULT;
355  m->idmr1 = (uint8_t) MSCAN_FILTER_MASK_DEFAULT;
356  m->idmr2 = (uint8_t) MSCAN_FILTER_MASK_DEFAULT;
357  m->idmr3 = (uint8_t) MSCAN_FILTER_MASK_DEFAULT;
358  m->idmr4 = (uint8_t) MSCAN_FILTER_MASK_DEFAULT;
359  m->idmr5 = (uint8_t) MSCAN_FILTER_MASK_DEFAULT;
360  m->idmr6 = (uint8_t) MSCAN_FILTER_MASK_DEFAULT;
361  m->idmr7 = (uint8_t) MSCAN_FILTER_MASK_DEFAULT;
362
363  mscan_initialization_mode_leave( m, &context);
364}
365
366/**
367 * @brief Returns the number of active filters of the MSCAN module @a m.
368 *
369 * @see MSCAN_FILTER_NUMBER_MIN, MSCAN_FILTER_NUMBER_2, MSCAN_FILTER_NUMBER_4
370 * and MSCAN_FILTER_NUMBER_MAX.
371 */
372unsigned mscan_filter_number( volatile mscan *m)
373{
374  uint8_t idam = m->idac & IDAC_IDAM;
375
376  switch (idam) {
377    case 0:
378      return MSCAN_FILTER_NUMBER_2;
379    case IDAC_IDAM0:
380      return MSCAN_FILTER_NUMBER_4;
381    case IDAC_IDAM1:
382      return MSCAN_FILTER_NUMBER_MAX;
383    default:
384      return MSCAN_FILTER_NUMBER_MIN;
385  }
386}
387
388/**
389 * @brief Sets the number of active filters of the MSCAN module @a m to @a
390 * number and returns true if @a number is valid.
391 *
392 * @see MSCAN_FILTER_NUMBER_MIN, MSCAN_FILTER_NUMBER_2, MSCAN_FILTER_NUMBER_4
393 * and MSCAN_FILTER_NUMBER_MAX.
394 */
395bool mscan_set_filter_number( volatile mscan *m, unsigned number)
396{
397  mscan_context context;
398  uint8_t idac = IDAC_IDAM1 | IDAC_IDAM0;
399
400  switch (number) {
401    case MSCAN_FILTER_NUMBER_MIN:
402      break;
403    case MSCAN_FILTER_NUMBER_2:
404      idac = 0;
405      break;
406    case MSCAN_FILTER_NUMBER_4:
407      idac = IDAC_IDAM0;
408      break;
409    case MSCAN_FILTER_NUMBER_MAX:
410      idac = IDAC_IDAM1;
411      break;
412    default:
413      return false;
414  }
415
416  mscan_initialization_mode_enter( m, &context);
417
418  m->idac = idac;
419
420  mscan_initialization_mode_leave( m, &context);
421
422  /* Clear filter */
423  mscan_filter_clear( m);
424
425  return true;
426}
427
428/**
429 * @brief Returns the  address of the CANIDAR register with index @a i of the
430 * MSCAN module @a m.
431 *
432 * @warning The index @a i is not checked if it is in range.
433 */
434volatile uint8_t *mscan_id_acceptance_register( volatile mscan *m, unsigned i)
435{
436  volatile uint8_t *const idar [8] = {
437    &m->idar0,
438    &m->idar1,
439    &m->idar2,
440    &m->idar3,
441    &m->idar4,
442    &m->idar5,
443    &m->idar6,
444    &m->idar7
445  };
446
447  return idar [i];
448}
449
450/**
451 * @brief Returns the  address of the CANIDMR register with index @a i of the
452 * MSCAN module @a m.
453 *
454 * @warning The index @a i is not checked if it is in range.
455 */
456volatile uint8_t *mscan_id_mask_register( volatile mscan *m, unsigned i)
457{
458  volatile uint8_t *const idmr [8] = {
459    &m->idmr0,
460    &m->idmr1,
461    &m->idmr2,
462    &m->idmr3,
463    &m->idmr4,
464    &m->idmr5,
465    &m->idmr6,
466    &m->idmr7
467  };
468
469  return idmr [i];
470}
471
472/**
473 * @brief Sets or gets the filter ID and mask in @a id and @a mask depending on
474 * @a set of MSCAN module @a m.  The filter is selected by the value of @a
475 * index.
476 *
477 * Returns true if the operation was successful.
478 */
479bool mscan_filter_operation(
480  volatile mscan *m,
481  bool set,
482  unsigned index,
483  uint32_t *id,
484  uint32_t *mask
485)
486{
487  unsigned number = mscan_filter_number( m);
488  unsigned offset = MSCAN_FILTER_NUMBER_MAX / number;
489  unsigned shift = 24;
490
491  volatile uint8_t *idar = NULL;
492  volatile uint8_t *idmr = NULL;
493
494  if (!set) {
495    *id = MSCAN_FILTER_ID_DEFAULT;
496    *mask = MSCAN_FILTER_MASK_DEFAULT;
497  }
498
499  if (index < number) {
500    mscan_context context;
501
502    mscan_initialization_mode_enter( m, &context);
503
504    index *= offset;
505    offset += index;
506    while (index < offset) {
507      idar = mscan_id_acceptance_register( m, index);
508      idmr = mscan_id_mask_register( m, index);
509
510      if (set) {
511        *idar = (uint8_t) (*id >> shift);
512        *idmr = (uint8_t) (*mask >> shift);
513      } else {
514        *id = (*id & ~(0xffU << shift)) | (*idar << shift);
515        *mask = (*mask & ~(0xffU << shift)) | (*idmr << shift);
516      }
517
518      shift -= 8;
519
520      ++index;
521    }
522
523    mscan_initialization_mode_leave( m, &context);
524  } else {
525    return false;
526  }
527
528  return true;
529}
530
531/**
532 * @brief Returns the receiver and transmitter error counter values in @a rec
533 * and @a tec of MSCAN module @a m.
534 */
535void mscan_get_error_counters( volatile mscan *m, unsigned *rec, unsigned *tec)
536{
537  mscan_context context;
538
539  mscan_initialization_mode_enter( m, &context);
540
541  *rec = m->rxerr;
542  *tec = m->txerr;
543
544  mscan_initialization_mode_leave( m, &context);
545}
Note: See TracBrowser for help on using the repository browser.