]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/api/CAN.h
Merge commit '28203e909e83b1ac6becb45a3eadae23b190df32' into master-core-pull
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / api / CAN.h
1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #ifndef MBED_CAN_H
17 #define MBED_CAN_H
18
19 #include "platform.h"
20
21 #if DEVICE_CAN
22
23 #include "can_api.h"
24 #include "can_helper.h"
25 #include "FunctionPointer.h"
26
27 namespace mbed {
28
29 /** CANMessage class
30 */
31 class CANMessage : public CAN_Message {
32
33 public:
34 /** Creates empty CAN message.
35 */
36 CANMessage() : CAN_Message() {
37 len = 8;
38 type = CANData;
39 format = CANStandard;
40 id = 0;
41 memset(data, 0, 8);
42 }
43
44 /** Creates CAN message with specific content.
45 */
46 CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) {
47 len = _len & 0xF;
48 type = _type;
49 format = _format;
50 id = _id;
51 memcpy(data, _data, _len);
52 }
53
54 /** Creates CAN remote message.
55 */
56 CANMessage(int _id, CANFormat _format = CANStandard) {
57 len = 0;
58 type = CANRemote;
59 format = _format;
60 id = _id;
61 memset(data, 0, 8);
62 }
63 };
64
65 /** A can bus client, used for communicating with can devices
66 */
67 class CAN {
68
69 public:
70 /** Creates an CAN interface connected to specific pins.
71 *
72 * @param rd read from transmitter
73 * @param td transmit to transmitter
74 *
75 * Example:
76 * @code
77 * #include "mbed.h"
78 *
79 * Ticker ticker;
80 * DigitalOut led1(LED1);
81 * DigitalOut led2(LED2);
82 * CAN can1(p9, p10);
83 * CAN can2(p30, p29);
84 *
85 * char counter = 0;
86 *
87 * void send() {
88 * if(can1.write(CANMessage(1337, &counter, 1))) {
89 * printf("Message sent: %d\n", counter);
90 * counter++;
91 * }
92 * led1 = !led1;
93 * }
94 *
95 * int main() {
96 * ticker.attach(&send, 1);
97 * CANMessage msg;
98 * while(1) {
99 * if(can2.read(msg)) {
100 * printf("Message received: %d\n\n", msg.data[0]);
101 * led2 = !led2;
102 * }
103 * wait(0.2);
104 * }
105 * }
106 * @endcode
107 */
108 CAN(PinName rd, PinName td);
109 virtual ~CAN();
110
111 /** Set the frequency of the CAN interface
112 *
113 * @param hz The bus frequency in hertz
114 *
115 * @returns
116 * 1 if successful,
117 * 0 otherwise
118 */
119 int frequency(int hz);
120
121 /** Write a CANMessage to the bus.
122 *
123 * @param msg The CANMessage to write.
124 *
125 * @returns
126 * 0 if write failed,
127 * 1 if write was successful
128 */
129 int write(CANMessage msg);
130
131 /** Read a CANMessage from the bus.
132 *
133 * @param msg A CANMessage to read to.
134 * @param handle message filter handle (0 for any message)
135 *
136 * @returns
137 * 0 if no message arrived,
138 * 1 if message arrived
139 */
140 int read(CANMessage &msg, int handle = 0);
141
142 /** Reset CAN interface.
143 *
144 * To use after error overflow.
145 */
146 void reset();
147
148 /** Puts or removes the CAN interface into silent monitoring mode
149 *
150 * @param silent boolean indicating whether to go into silent mode or not
151 */
152 void monitor(bool silent);
153
154 enum Mode {
155 Reset = 0,
156 Normal,
157 Silent,
158 LocalTest,
159 GlobalTest,
160 SilentTest
161 };
162
163 /** Change CAN operation to the specified mode
164 *
165 * @param mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest)
166 *
167 * @returns
168 * 0 if mode change failed or unsupported,
169 * 1 if mode change was successful
170 */
171 int mode(Mode mode);
172
173 /** Filter out incomming messages
174 *
175 * @param id the id to filter on
176 * @param mask the mask applied to the id
177 * @param format format to filter on (Default CANAny)
178 * @param handle message filter handle (Optional)
179 *
180 * @returns
181 * 0 if filter change failed or unsupported,
182 * new filter handle if successful
183 */
184 int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0);
185
186 /** Returns number of read errors to detect read overflow errors.
187 */
188 unsigned char rderror();
189
190 /** Returns number of write errors to detect write overflow errors.
191 */
192 unsigned char tderror();
193
194 enum IrqType {
195 RxIrq = 0,
196 TxIrq,
197 EwIrq,
198 DoIrq,
199 WuIrq,
200 EpIrq,
201 AlIrq,
202 BeIrq,
203 IdIrq
204 };
205
206 /** Attach a function to call whenever a CAN frame received interrupt is
207 * generated.
208 *
209 * @param fptr A pointer to a void function, or 0 to set as none
210 * @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, CAN::TxIrq for transmitted or aborted, CAN::EwIrq for error warning, CAN::DoIrq for data overrun, CAN::WuIrq for wake-up, CAN::EpIrq for error passive, CAN::AlIrq for arbitration lost, CAN::BeIrq for bus error)
211 */
212 void attach(void (*fptr)(void), IrqType type=RxIrq);
213
214 /** Attach a member function to call whenever a CAN frame received interrupt
215 * is generated.
216 *
217 * @param tptr pointer to the object to call the member function on
218 * @param mptr pointer to the member function to be called
219 * @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error)
220 */
221 template<typename T>
222 void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
223 if((mptr != NULL) && (tptr != NULL)) {
224 _irq[type].attach(tptr, mptr);
225 can_irq_set(&_can, (CanIrqType)type, 1);
226 }
227 else {
228 can_irq_set(&_can, (CanIrqType)type, 0);
229 }
230 }
231
232 static void _irq_handler(uint32_t id, CanIrqType type);
233
234 protected:
235 can_t _can;
236 FunctionPointer _irq[9];
237 };
238
239 } // namespace mbed
240
241 #endif
242
243 #endif // MBED_CAN_H
Imprint / Impressum