1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 /* Acceptance filter mode in AFMR register */
28 #define ACCF_BYPASS 0x02
30 #define ACCF_FULLCAN 0x04
32 /* There are several bit timing calculators on the internet.
33 http://www.port.de/engl/canprod/sv_req_form.html
34 http://www.kvaser.com/can/index.htm
37 static const PinMap PinMap_CAN_RD[] = {
44 static const PinMap PinMap_CAN_TD[] = {
50 // Type definition to hold a CAN message
52 unsigned int reserved1 : 16;
53 unsigned int dlc : 4; // Bits 16..19: DLC - Data Length Counter
54 unsigned int reserved0 : 10;
55 unsigned int rtr : 1; // Bit 30: Set if this is a RTR message
56 unsigned int type : 1; // Bit 31: Set if this is a 29-bit ID message
57 unsigned int id; // CAN Message ID (11-bit or 29-bit)
58 unsigned char data[8]; // CAN Message Data Bytes 0-7
60 typedef struct CANMsg CANMsg;
62 static uint32_t can_irq_ids[CAN_NUM] = {0};
63 static can_irq_handler irq_handler;
65 static uint32_t can_disable(can_t *obj) {
66 uint32_t sm = obj->dev->MOD;
71 static inline void can_enable(can_t *obj) {
72 if (obj->dev->MOD & 1) {
73 obj->dev->MOD &= ~(1);
77 int can_mode(can_t *obj, CanMode mode)
79 return 0; // not implemented
82 int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) {
83 return 0; // not implemented
86 static inline void can_irq(uint32_t icr, uint32_t index) {
89 for(i = 0; i < 8; i++)
91 if((can_irq_ids[index] != 0) && (icr & (1 << i)))
94 case 0: irq_handler(can_irq_ids[index], IRQ_RX); break;
95 case 1: irq_handler(can_irq_ids[index], IRQ_TX); break;
96 case 2: irq_handler(can_irq_ids[index], IRQ_ERROR); break;
97 case 3: irq_handler(can_irq_ids[index], IRQ_OVERRUN); break;
98 case 4: irq_handler(can_irq_ids[index], IRQ_WAKEUP); break;
99 case 5: irq_handler(can_irq_ids[index], IRQ_PASSIVE); break;
100 case 6: irq_handler(can_irq_ids[index], IRQ_ARB); break;
101 case 7: irq_handler(can_irq_ids[index], IRQ_BUS); break;
102 case 8: irq_handler(can_irq_ids[index], IRQ_READY); break;
108 // Have to check that the CAN block is active before reading the Interrupt
109 // Control Register, or the mbed hangs
113 if(LPC_SC->PCONP & (1 << 13)) {
114 icr = LPC_CAN1->ICR & 0x1FF;
118 if(LPC_SC->PCONP & (1 << 14)) {
119 icr = LPC_CAN2->ICR & 0x1FF;
124 // Register CAN object's irq handler
125 void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id) {
126 irq_handler = handler;
127 can_irq_ids[obj->index] = id;
130 // Unregister CAN object's irq handler
131 void can_irq_free(can_t *obj) {
132 obj->dev->IER &= ~(1);
133 can_irq_ids[obj->index] = 0;
135 if ((can_irq_ids[0] == 0) && (can_irq_ids[1] == 0)) {
136 NVIC_DisableIRQ(CAN_IRQn);
140 // Clear or set a irq
141 void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable) {
145 case IRQ_RX: ier = (1 << 0); break;
146 case IRQ_TX: ier = (1 << 1); break;
147 case IRQ_ERROR: ier = (1 << 2); break;
148 case IRQ_OVERRUN: ier = (1 << 3); break;
149 case IRQ_WAKEUP: ier = (1 << 4); break;
150 case IRQ_PASSIVE: ier = (1 << 5); break;
151 case IRQ_ARB: ier = (1 << 6); break;
152 case IRQ_BUS: ier = (1 << 7); break;
153 case IRQ_READY: ier = (1 << 8); break;
159 obj->dev->IER &= ~ier;
162 obj->dev->IER |= ier;
164 obj->dev->MOD &= ~(1);
166 // Enable NVIC if at least 1 interrupt is active
167 if(((LPC_SC->PCONP & (1 << 13)) && LPC_CAN1->IER) || ((LPC_SC->PCONP & (1 << 14)) && LPC_CAN2->IER)) {
168 NVIC_SetVector(CAN_IRQn, (uint32_t) &can_irq_n);
169 NVIC_EnableIRQ(CAN_IRQn);
172 NVIC_DisableIRQ(CAN_IRQn);
176 // This table has the sampling points as close to 75% as possible. The first
177 // value is TSEG1, the second TSEG2.
178 static const int timing_pts[23][2] = {
179 {0x0, 0x0}, // 2, 50%
180 {0x1, 0x0}, // 3, 67%
181 {0x2, 0x0}, // 4, 75%
182 {0x3, 0x0}, // 5, 80%
183 {0x3, 0x1}, // 6, 67%
184 {0x4, 0x1}, // 7, 71%
185 {0x5, 0x1}, // 8, 75%
186 {0x6, 0x1}, // 9, 78%
187 {0x6, 0x2}, // 10, 70%
188 {0x7, 0x2}, // 11, 73%
189 {0x8, 0x2}, // 12, 75%
190 {0x9, 0x2}, // 13, 77%
191 {0x9, 0x3}, // 14, 71%
192 {0xA, 0x3}, // 15, 73%
193 {0xB, 0x3}, // 16, 75%
194 {0xC, 0x3}, // 17, 76%
195 {0xD, 0x3}, // 18, 78%
196 {0xD, 0x4}, // 19, 74%
197 {0xE, 0x4}, // 20, 75%
198 {0xF, 0x4}, // 21, 76%
199 {0xF, 0x5}, // 22, 73%
200 {0xF, 0x6}, // 23, 70%
201 {0xF, 0x7}, // 24, 67%
204 static unsigned int can_speed(unsigned int pclk, unsigned int cclk, unsigned char psjw) {
212 bitwidth = (pclk / cclk);
214 brp = bitwidth / 0x18;
215 while ((!hit) && (brp < bitwidth / 4)) {
217 for (bits = 22; bits > 0; bits--) {
218 calcbit = (bits + 3) * (brp + 1);
219 if (calcbit == bitwidth) {
227 btr = ((timing_pts[bits][1] << 20) & 0x00700000)
228 | ((timing_pts[bits][0] << 16) & 0x000F0000)
229 | ((psjw << 14) & 0x0000C000)
230 | ((brp << 0) & 0x000003FF);
239 void can_init(can_t *obj, PinName rd, PinName td) {
240 CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD);
241 CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD);
242 obj->dev = (LPC_CAN_TypeDef *)pinmap_merge(can_rd, can_td);
243 MBED_ASSERT((int)obj->dev != NC);
245 switch ((int)obj->dev) {
246 case CAN_1: LPC_SC->PCONP |= 1 << 13; break;
247 case CAN_2: LPC_SC->PCONP |= 1 << 14; break;
250 pinmap_pinout(rd, PinMap_CAN_RD);
251 pinmap_pinout(td, PinMap_CAN_TD);
253 switch ((int)obj->dev) {
254 case CAN_1: obj->index = 0; break;
255 case CAN_2: obj->index = 1; break;
259 obj->dev->IER = 0; // Disable Interrupts
260 can_frequency(obj, 100000);
262 LPC_CANAF->AFMR = ACCF_BYPASS; // Bypass Filter
265 void can_free(can_t *obj) {
266 switch ((int)obj->dev) {
267 case CAN_1: LPC_SC->PCONP &= ~(1 << 13); break;
268 case CAN_2: LPC_SC->PCONP &= ~(1 << 14); break;
272 int can_frequency(can_t *obj, int f) {
273 int pclk = PeripheralClock;
275 int btr = can_speed(pclk, (unsigned int)f, 1);
278 uint32_t modmask = can_disable(obj);
280 obj->dev->MOD = modmask;
287 int can_write(can_t *obj, CAN_Message msg, int cc) {
288 unsigned int CANStatus;
294 m.dlc = msg.len & 0xF;
297 memcpy(m.data, msg.data, msg.len);
298 const unsigned int *buf = (const unsigned int *)&m;
300 CANStatus = obj->dev->SR;
301 if (CANStatus & 0x00000004) {
302 obj->dev->TFI1 = buf[0] & 0xC00F0000;
303 obj->dev->TID1 = buf[1];
304 obj->dev->TDA1 = buf[2];
305 obj->dev->TDB1 = buf[3];
307 obj->dev->CMR = 0x30;
309 obj->dev->CMR = 0x21;
313 } else if (CANStatus & 0x00000400) {
314 obj->dev->TFI2 = buf[0] & 0xC00F0000;
315 obj->dev->TID2 = buf[1];
316 obj->dev->TDA2 = buf[2];
317 obj->dev->TDB2 = buf[3];
319 obj->dev->CMR = 0x50;
321 obj->dev->CMR = 0x41;
325 } else if (CANStatus & 0x00040000) {
326 obj->dev->TFI3 = buf[0] & 0xC00F0000;
327 obj->dev->TID3 = buf[1];
328 obj->dev->TDA3 = buf[2];
329 obj->dev->TDB3 = buf[3];
331 obj->dev->CMR = 0x90;
333 obj->dev->CMR = 0x81;
341 int can_read(can_t *obj, CAN_Message *msg, int handle) {
343 unsigned int *i = (unsigned int *)&x;
347 if (obj->dev->GSR & 0x1) {
348 *i++ = obj->dev->RFS; // Frame
349 *i++ = obj->dev->RID; // ID
350 *i++ = obj->dev->RDA; // Data A
351 *i++ = obj->dev->RDB; // Data B
352 obj->dev->CMR = 0x04; // release receive buffer
356 msg->format = (x.type)? CANExtended : CANStandard;
357 msg->type = (x.rtr)? CANRemote: CANData;
358 memcpy(msg->data,x.data,x.dlc);
365 void can_reset(can_t *obj) {
367 obj->dev->GSR = 0; // Reset error counter when CAN1MOD is in reset
370 unsigned char can_rderror(can_t *obj) {
371 return (obj->dev->GSR >> 16) & 0xFF;
374 unsigned char can_tderror(can_t *obj) {
375 return (obj->dev->GSR >> 24) & 0xFF;
378 void can_monitor(can_t *obj, int silent) {
379 uint32_t mod_mask = can_disable(obj);
381 obj->dev->MOD |= (1 << 1);
383 obj->dev->MOD &= ~(1 << 1);
385 if (!(mod_mask & 1)) {