summaryrefslogtreecommitdiffstats
path: root/bsps/arm/lpc176x/include/bsp/can.h
blob: 95480596fe7230715f6f1c32c42cbaec16475ab4 (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
/**
 * @file
 *
 * @ingroup RTEMSBSPsARMLPC176X
 *
 * @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.
 */

#ifndef LPC176X_CAN_H
#define LPC176X_CAN_H

#include <bsp.h>
#include <bsp/io.h>
#include <bsp/lpc176x.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/**
 * @brief The CAN devices of the board.
 */
typedef enum {
  CAN_0,
  CAN_1,
  CAN_DEVICES_NUMBER
} lpc176x_can_number;

/**
 * @brief  A CAN message represented for the registers of the device.
 */
typedef struct {
  uint32_t info;
  uint32_t id;
  uint32_t data_a;
  uint32_t data_b;
} registers_can_message;

/**
 * @brief A CAN message represented with each logical parts
 */
typedef struct {
  unsigned int reserved1 : 16;
  unsigned int dlc       :  4;   /* Bits 16..19: DLC - Data Length Counter*/
  unsigned int reserved0 : 10;
  unsigned int rtr       :  1;   /* Bit 30: Set if this is a RTR message*/
  unsigned int type      :  1;   /* Bit 31: Set if this is a 29-bit ID message*/
  unsigned int id;               /* CAN Message ID (11-bit or 29-bit)*/
  unsigned char data[ 8 ];       /* CAN Message Data Bytes 0-7*/
} low_level_can_message;

/**
 * @brief A CAN message represented of both forms.
 */
typedef union {
  low_level_can_message low_level;
  registers_can_message registers;
} can_message;

/**
 * @brief The possible interrupt sources for CAN.
 */
typedef enum {
  IRQ_RX = 0,
  IRQ_TX,
  IRQ_ERROR,
  IRQ_OVERRUN,
  IRQ_WAKEUP,
  IRQ_PASSIVE,
  IRQ_ARB,
  IRQ_BUS,
  IRQ_READY,
  CAN_IRQ_NUMBER
} can_irq_type;

/**
 * @brief An isr for a CAN interrupt
 *
 * @param number The CAN which rised the interrupt.
 */
typedef void (*lpc176x_can_isr) ( lpc176x_can_number number );

/**
 * @brief A CAN frequency value
 */
typedef unsigned int can_freq;

/**
 * @brief Opens CAN device.
 * @details It enables the module and gives it a clock, sets the pins,
 * disables the interrupts, sets the frequency and bypasses
 * the acceptance filter.
 *
 * @param  minor The device to open.
 * @param freq The desired frequency.
 * @return RTEMS_SUCCESFUL on success.
 */
rtems_status_code can_open( lpc176x_can_number minor, can_freq freq );

/**
 * @brief Closes the passed CAN device and shut it down.
 *
 * @param minor The device to close.
 * @return RTEMS_SUCCESSFUL  if ok, RTEMS_INVALID_NUMBER for a bad parameter.
 */
rtems_status_code can_close( lpc176x_can_number minor );

/**
 * @brief Reads the CAN device.
 *
 * @param minor The CAN device to read.
 * @param message The read message.
 * @return RTEMS_SUCCESSFUL if read ok, RTEMS_IO_ERROR otherwise.
 */
rtems_status_code can_read(
  const lpc176x_can_number minor,
  can_message             *message
);

/**
 * @brief Writes the passed CAN message into the selected CAN device.
 *
 * @param minor The device to write.
 * @param message The message to write.
 * @return RTEMS_SUCCESFUL if write ok. RTEMS_IO_ERROR otherwise.
 */
rtems_status_code can_write(
  const lpc176x_can_number minor,
  const can_message *const message
);

/**
 * @brief Registers an isr in the driver vector, and enables the interrupt
*  in the device.
 *
 * @param number The CAN device to set
 * @param type The interrupt type.
 * @param isr The isr to register.
 * @return RTEMS_SUCCESSFUL if ok RTEMS_INVALID_NUMBER otherwise.
 */
rtems_status_code can_register_isr(
  const lpc176x_can_number number,
  const can_irq_type       type,
  const lpc176x_can_isr    isr
);

/**
 * @brief Creates a CAN message.
 * @details [long description]
 *
 * @param msg The created message.
 * @param _id The can id for the message.
 * @param _data The data of the message.
 * @param _len The length of the message.
 * @return RTEMS_SUCCESFUL if created, RTEMS_INVALID_NUMBER otherwise.
 */
rtems_status_code create_can_message(
  can_message *const msg,
  const int          _id,
  const char *const  _data,
  const char         _len
);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* ifndef LPC176X_CAN_H */