summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/lpc176x/can/can.c
blob: 781300c23dcec7a8d36e39bfe9476efafdf11c20 (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
/**
 * @file can.c
 *
 * @ingroup lpc176x
 *
 * @brief CAN controller for the mbed lpc1768 board.
 */

/*
 * Copyright (c) 2014 Taller Technologies.
 *
 * @author  Diaz Marcos (marcos.diaz@tallertechnologies.com)
 * @author  Daniel Chicco  (daniel.chicco@tallertechnologies.com)
 *
 * 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 <rtems/status-checks.h>
#include <bsp/irq.h>
#include <bsp/can.h>
#include <bsp/can-defs.h>
#include <bsp/mbed-pinmap.h>

/**
 * @brief The standard isr to be installed for all the can devices.
 *
 * @param arg unused.
 */
static void can_isr( void *arg );

/**
 * @brief Vector of isr for the can_driver .
 */
lpc176x_can_isr_vector isr_vector;

/**
 * @brief Represents all the can devices, and useful things for initialization.
 */
static const can_driver_entry can_driver_table[ CAN_DEVICES_NUMBER ] =
{
  {
    .device = (can_device *) CAN1_BASE_ADDR,
    .module = LPC176X_MODULE_CAN_0,
    .pconp_pin = LPC176X_SCB_PCONP_CAN_1,
    .pins = { DIP9, DIP10 },
    .pinfunction = LPC176X_PIN_FUNCTION_01
  },
  {
    .device = (can_device *) CAN2_BASE_ADDR,
    .module = LPC176X_MODULE_CAN_1,
    .pconp_pin = LPC176X_SCB_PCONP_CAN_2,
    .pins = { DIP30, DIP29 },
    .pinfunction = LPC176X_PIN_FUNCTION_10
  }
};

/**
 * @brief The CAN acceptance filter.
 */
can_acceptance_filter *const acceptance_filter_device =
  (can_acceptance_filter *) CAN_ACCEPT_BASE_ADDR;

/**
 * @brief Sets RX and TX pins for the passed can device number.
 *
 * @param cannumber CAN controller to be used.
 */
static inline void setpins( const lpc176x_can_number cannumber )
{
  const can_driver_entry *const can_driver = &can_driver_table[ cannumber ];

  lpc176x_pin_select( can_driver->pins[ CAN_TX_PIN ],
    can_driver->pinfunction );
  lpc176x_pin_select( can_driver->pins[ CAN_RX_PIN ],
    can_driver->pinfunction );
}

rtems_status_code can_close( const lpc176x_can_number minor )
{
  rtems_status_code sc = RTEMS_INVALID_NUMBER;

  if ( CAN_DRIVER_IS_MINOR_VALID( minor ) ) {
    sc = RTEMS_SUCCESSFUL;
    const can_driver_entry *const can_driver = &can_driver_table[ minor ];
    lpc176x_module_disable( can_driver->module );
  }

  /*else wrong parameters. return RTEMS_INVALID_NUMBER*/

  return sc;
}

/**
 * @brief Enables CAN device.
 *
 * @param obj The device to be enabled.
 */
static inline void can_enable( const can_driver_entry *const obj )
{
  if ( obj->device->MOD & CAN_MOD_RM ) {
    obj->device->MOD &= ~( CAN_MOD_RM );
  }
}

/**
 * @brief Disables CAN device to set parameters, and returns the previous value
 * of the MOD register.
 *
 * @param obj The device to disable.
 * @return The previous status of MOD register.
 */
static inline uint32_t can_disable( const can_driver_entry *const obj )
{
  const uint32_t sm = obj->device->MOD;

  obj->device->MOD |= CAN_MOD_RM;

  return sm;
}

/**
 * @brief Resets the error count.
 *
 * @param obj which device reset.
 */
static inline void can_reset( const can_driver_entry *const obj )
{
  can_disable( obj );
  obj->device->GSR = 0;   /* Reset error counter when CANxMOD is in reset*/
}

/**
 * @brief This table has the sampling points as close to 75% as possible.
 * The first value is TSEG1, the second is TSEG2.
 */
static const unsigned int timing_pts[ MAX_TSEG1_TSEG2_BITS +
                                      1 ][ CAN_NUMBER_OF_TSEG ] = {
  { 0x0, 0x0 },      /* 2,  50%*/
  { 0x1, 0x0 },      /* 3,  67%*/
  { 0x2, 0x0 },      /* 4,  75%*/
  { 0x3, 0x0 },      /* 5,  80%*/
  { 0x3, 0x1 },      /* 6,  67%*/
  { 0x4, 0x1 },      /* 7,  71%*/
  { 0x5, 0x1 },      /* 8,  75%*/
  { 0x6, 0x1 },      /* 9,  78%*/
  { 0x6, 0x2 },      /* 10, 70%*/
  { 0x7, 0x2 },      /* 11, 73%*/
  { 0x8, 0x2 },      /* 12, 75%*/
  { 0x9, 0x2 },      /* 13, 77%*/
  { 0x9, 0x3 },      /* 14, 71%*/
  { 0xA, 0x3 },      /* 15, 73%*/
  { 0xB, 0x3 },      /* 16, 75%*/
  { 0xC, 0x3 },      /* 17, 76%*/
  { 0xD, 0x3 },      /* 18, 78%*/
  { 0xD, 0x4 },      /* 19, 74%*/
  { 0xE, 0x4 },      /* 20, 75%*/
  { 0xF, 0x4 },      /* 21, 76%*/
  { 0xF, 0x5 },      /* 22, 73%*/
  { 0xF, 0x6 },      /* 23, 70%*/
  { 0xF, 0x7 },      /* 24, 67%*/
};

/**
 * @brief Checks if divisor is a divisor of value.
 *
 * @param value The number to be divided.
 * @param divisor The divisor to check.
 *
 * @return true if "number" is divided by "divisor"; false otherwise.
 */
static inline bool is_divisor(
  const uint32_t value,
  const uint16_t divisor
)
{
  return ( ( value % divisor ) == 0 );
}

/**
 * @brief Gets the size of the two tseg values added according to the given
 * bitwidth and brp (The CAN prescaler).
 *
 * @param bitwidth The total bitwidth of a CAN bit (in pclk clocks).
 * @param brp The CAN clock prescaler.
 *
 * @return The value of tseg1 + tseg2  of the CAN bit. It is useful
 *  to serve for index for timing_pts array).
 */
static inline uint32_t get_tseg_bit_size(
  const uint32_t bitwidth,
  const uint16_t brp
)
{
  return ( ( bitwidth / ( brp + CAN_BRP_EXTRA_BIT ) ) - CAN_TSEG_EXTRA_BITS );
}

/**
 * @brief Gets the brp and tsegbitsize in order to achieve the desired bitwidth.
 * @details The following must be fullfilled:
 *(brp + CAN_BRP_EXTRA_BIT) * (tsegbitsize + CAN_TSEG_EXTRA_BITS) == bitwidth
 *
 * @param bitwidth The bitwidth that we need to achieve.
 * @param brp Here it returns the calculated brp value.
 * @param tsegbitsize Here it returns the calculated tseg bit size value.
 * @return true if brp and tsegbitsize have been calculated.
 */
static inline bool get_brp_and_bitsize(
  const uint32_t  bitwidth,
  uint16_t *const brp,
  uint32_t *const tsegbitsize
)
{
  bool hit = false;

  while ( ( !hit ) && ( *brp < bitwidth / MIN_NUMBER_OF_CAN_BITS ) ) {
    if ( ( is_divisor( bitwidth, *brp + CAN_BRP_EXTRA_BIT ) )
         && ( get_tseg_bit_size( bitwidth, *brp ) < MAX_TSEG1_TSEG2_BITS ) ) {
      hit = true;
      *tsegbitsize = get_tseg_bit_size( bitwidth, *brp );
    } else { /*Correct values not found, keep looking*/
      ( *brp )++;
    }
  }

  return hit;
}

/**
 * @brief Constructs the btr register with the passed arguments.
 *
 * @param tsegbitsize The size tseg bits to set.
 * @param psjw The sjw to set.
 * @param brp The prescaler value to set.
 * @return The constructed btr register.
 */
static inline uint32_t get_btr(
  const uint32_t      tsegbitsize,
  const unsigned char psjw,
  const uint32_t      brp
)
{
  const uint32_t tseg2_value_masked =
    ( ( timing_pts[ tsegbitsize ][ CAN_TSEG2 ] << CAN_BTR_TSEG2_SHIFT ) &
      CAN_BTR_TSEG2_MASK );
  const uint32_t tseg1_value_masked =
    ( ( timing_pts[ tsegbitsize ][ CAN_TSEG1 ] <<
        CAN_BTR_TSEG1_SHIFT ) & CAN_BTR_TSEG1_MASK );
  const uint32_t psjw_value_masked =
    ( ( psjw << CAN_BTR_SJW_SHIFT ) & CAN_BTR_SJW_MASK );
  const uint32_t brp_value_masked =
    ( ( brp << CAN_BTR_BRP_SHIFT ) & CAN_BTR_BRP_MASK );

  return tseg1_value_masked | tseg2_value_masked |
         psjw_value_masked | brp_value_masked;
}

/**
 * @brief Calculates and returns a bit timing register (btr) for the desired
 * canclk frequency using the passed psjw, system clock and peripheral clock.
 *
 * @param systemclk The clock of the system (in Hz).
 * @param pclkdiv The peripheral clock divisor for the can device.
 * @param canclk The desired frequency for CAN (in Hz).
 * @param psjw The desired psjw.
 * @return The btr register value if found, WRONG_BTR_VALUE otherwise.
 */
static inline unsigned int can_speed(
  const unsigned int  systemclk,
  const unsigned int  pclkdiv,
  const unsigned int  canclk,
  const unsigned char psjw
)
{
  uint32_t       btr = WRONG_BTR_VALUE;
  const uint32_t bitwidth = systemclk / ( pclkdiv * canclk );

  /* This is for the brp (prescaler) to start searching a reachable multiple.*/
  uint16_t brp = bitwidth / MAX_NUMBER_OF_CAN_BITS;
  uint32_t tsegbitsize;

  if ( get_brp_and_bitsize( bitwidth, &brp, &tsegbitsize ) ) {
    btr = get_btr( tsegbitsize, psjw, brp );
  }

  return btr;
}

/**
 * @brief Configures the desired CAN device with the desired frequency.
 *
 * @param obj The can device to configure.
 * @param f The desired frequency.
 *
 * @return RTEMS_SUCCESSFUL if could be set, RTEMS_INVALID_NUMBER otherwise.
 */
static rtems_status_code can_frequency(
  const can_driver_entry *const obj,
  const can_freq            freq
)
{
  rtems_status_code sc = RTEMS_INVALID_NUMBER;
  const uint32_t    btr = can_speed( LPC176X_CCLK, LPC176X_PCLKDIV, freq, 1 );

  if ( btr != WRONG_BTR_VALUE ) {
    sc = RTEMS_SUCCESSFUL;
    uint32_t modmask = can_disable( obj );
    obj->device->BTR = btr;
    obj->device->MOD = modmask;
  } /*else couldnt found a good timing for the desired frequency,
      return RTEMS_INVALID_NUMBER.*/

  return sc;
}

/**
 * @brief Installs the interrupt handler in rtems.
 */
static inline rtems_status_code can_initialize( void )
{
  return rtems_interrupt_handler_install(
    LPC176X_IRQ_CAN,
    "can_interrupt",
    RTEMS_INTERRUPT_UNIQUE,
    can_isr,
    NULL
  );
}

rtems_status_code can_open( const lpc176x_can_number minor, can_freq freq )
{
  const can_driver_entry *const can_driver = &can_driver_table[ minor ];
  rtems_status_code             sc = RTEMS_INVALID_NUMBER;

  if ( CAN_DRIVER_IS_MINOR_VALID( minor ) ) {
    /*Enable CAN and acceptance filter modules.*/
    sc =
      lpc176x_module_enable( can_driver->module, LPC176X_MODULE_PCLK_DEFAULT );
    RTEMS_CHECK_SC( sc, "enable can module" );
    sc = lpc176x_module_enable( LPC176X_MODULE_ACCF,
      LPC176X_MODULE_PCLK_DEFAULT );
    RTEMS_CHECK_SC( sc, "enable acceptance filter" );
    /*Set pin functions.*/
    setpins( minor );

    can_reset( can_driver );
    can_driver->device->IER = CAN_DEFAULT_INTERRUPT_CONFIGURATION;
    sc = can_frequency( can_driver, freq );
    RTEMS_CHECK_SC( sc, "Configure CAN frequency" );
    can_initialize();

    acceptance_filter_device->AFMR = CAN_ACCF_AFMR_ACCBP;     /*Bypass Filter.*/
  }

  return sc;
}

/**
 * @brief Calls the installed isrs, according to the active interrupts.
 *
 * @param vector The read vector of active interrupts.
 * @param number The CAN device to look for interruptions.
 */
static inline void call_isrs(
  const uint32_t           vector,
  const lpc176x_can_number number
)
{
  can_irq_type i;

  for ( i = IRQ_RX; i < CAN_IRQ_NUMBER; ++i ) {
    if ( ( isr_vector[ i ] != NULL ) && ( vector & ( 1 << i ) ) )
      isr_vector[ i ]( number );

    /* else this interrupt has not been raised or it hasn't got a handler,
       so do nothing.*/
  }
}

/**
 * @brief Checks if the passed CAN device is enabled and if it is checks for
 * active interrupts and calls its installed isr.
 *
 * @param number The CAN device to check for interrupts rised.
 */
static inline void search_and_call_int( const lpc176x_can_number number )
{
  const can_driver_entry *const driver = &can_driver_table[ number ];

  if ( LPC176X_SCB.pconp & driver->pconp_pin ) {
    /*We must read the whole register at once because it resets when read.*/
    const uint32_t int_vector = driver->device->ICR & CAN_INTERRUPT_TYPE_MASK;
    call_isrs( int_vector, number );
  }

  /*else the device is shut down so we must do nothing.*/
}

/**
 * @brief The standard isr to be installed for all the CAN devices.
 *
 * @param arg unused.
 */
static void can_isr( void *arg )
{
  lpc176x_can_number i;

  for ( i = CAN_0; i < CAN_DEVICES_NUMBER; ++i ) {
    search_and_call_int( i );
  }
}

rtems_status_code can_read(
  const lpc176x_can_number minor,
  can_message             *message
)
{
  rtems_status_code             sc = RTEMS_IO_ERROR;
  const can_driver_entry *const can_driver = &can_driver_table[ minor ];
  can_device *const             dev = can_driver->device;
  registers_can_message *const  msg = &( message->registers );

  can_enable( can_driver );

  if ( dev->GSR & CAN_GSR_RBS_MASK ) {
    sc = RTEMS_SUCCESSFUL;
    *msg = dev->receive;
    dev->CMR = CAN_CMR_RRB_MASK;      /* release receive buffer. */
  } /* else Message not received.*/

  return sc;
}

/**
 * @brief Array of masks and control bits for the transmit buffers.
 * It's used for each transmit buffer in order to see if it's available and to
 * send data to them.
 */
static const can_transmit_info transmit_info[ CAN_NUMBER_OF_TRANSMIT_BUFFERS ]
  =
  {
  {
    .can_status_mask = 0x00000004U,
    .not_cc_cmr_value = 0x21U
  },
  {
    .can_status_mask = 0x00000400U,
    .not_cc_cmr_value = 0x41U
  },
  {
    .can_status_mask = 0x00040000U,
    .not_cc_cmr_value = 0x81U
  }
  };

rtems_status_code can_write(
  const lpc176x_can_number minor,
  const can_message *const message
)
{
  const can_driver_entry *const can_driver = &can_driver_table[ minor ];
  can_device *const             obj = can_driver->device;
  const uint32_t                CANStatus = obj->SR;

  const registers_can_message *const msg = &( message->registers );
  rtems_status_code                  sc = RTEMS_IO_ERROR;
  can_transmit_number                transmit_buffer;

  can_enable( can_driver );

  for ( transmit_buffer = CAN_TRANSMIT1;
        ( sc != RTEMS_SUCCESSFUL ) && ( transmit_buffer <
                                        CAN_NUMBER_OF_TRANSMIT_BUFFERS );
        ++transmit_buffer ) {
    if ( CANStatus & transmit_info[ transmit_buffer ].can_status_mask ) {
      sc = RTEMS_SUCCESSFUL;
      obj->transmit[ transmit_buffer ] = *msg;
      obj->CMR = transmit_info[ transmit_buffer ].not_cc_cmr_value;
    }    /*else can buffer busy, try with the next.*/
  }

  return sc;
}

/**
 * @brief Enables the interrupt type passed to the desired CAN device.
 *
 * @param number The CAN device to enable the interrupts.
 * @param type The type of interrupt to enable.
 */
static inline void can_enable_interrupt(
  const lpc176x_can_number number,
  const can_irq_type       type
)
{
  const can_driver_entry *const driver = &can_driver_table[ number ];
  const uint32_t                ier = 1 << type;

  can_disable( driver );
  driver->device->IER |= ier;
  can_enable( driver );
}

rtems_status_code can_register_isr(
  const lpc176x_can_number number,
  const can_irq_type       type,
  const lpc176x_can_isr    isr
)
{
  rtems_status_code sc = RTEMS_INVALID_NUMBER;

  if ( ( 0 <= type ) && ( type < CAN_IRQ_NUMBER ) ) {
    sc = RTEMS_SUCCESSFUL;
    isr_vector[ type ] = isr;
    can_enable_interrupt( number, type );
  }

  return sc;
}

rtems_status_code create_can_message(
  can_message *const msg,
  const int          _id,
  const char *const  _data,
  const char         _len
)
{
  rtems_status_code sc = RTEMS_INVALID_NUMBER;

  if ( ( _len <= CAN_MAXIMUM_DATA_SIZE ) && ( _id <= CAN10_MAXIMUM_ID ) ) {
    sc = RTEMS_SUCCESSFUL;
    msg->low_level.dlc = _len;
    msg->low_level.type = CANStandard;
    msg->low_level.rtr = CANData;
    msg->low_level.id = _id;
    memcpy( msg->low_level.data, _data, _len );
  }

  return sc;
}