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.
16 #include "mbed_assert.h"
24 /* Acceptance filter mode in AFMR register */
26 #define ACCF_BYPASS 0x02
28 #define ACCF_FULLCAN 0x04
30 /* There are several bit timing calculators on the internet.
31 http://www.port.de/engl/canprod/sv_req_form.html
32 http://www.kvaser.com/can/index.htm
35 static const PinMap PinMap_CAN_RD[] = {
43 static const PinMap PinMap_CAN_TD[] = {
51 // Type definition to hold a CAN message
53 unsigned int reserved1 : 16;
54 unsigned int dlc : 4; // Bits 16..19: DLC - Data Length Counter
55 unsigned int reserved0 : 10;
56 unsigned int rtr : 1; // Bit 30: Set if this is a RTR message
57 unsigned int type : 1; // Bit 31: Set if this is a 29-bit ID message
58 unsigned int id; // CAN Message ID (11-bit or 29-bit)
59 unsigned char data[8]; // CAN Message Data Bytes 0-7
61 typedef struct CANMsg CANMsg;
63 static uint32_t can_disable(can_t *obj) {
64 uint32_t sm = obj->dev->MOD;
69 static inline void can_enable(can_t *obj) {
70 if (obj->dev->MOD & 1) {
71 obj->dev->MOD &= ~(1);
75 int can_mode(can_t *obj, CanMode mode) {
76 return 0; // not implemented
79 int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) {
80 return 0; // not implemented
83 static int can_pclk(can_t *obj) {
85 switch ((int)obj->dev) {
86 case CAN_1: value = (LPC_SC->PCLKSEL0 & (0x3 << 26)) >> 26; break;
87 case CAN_2: value = (LPC_SC->PCLKSEL0 & (0x3 << 28)) >> 28; break;
98 // This table has the sampling points as close to 75% as possible. The first
99 // value is TSEG1, the second TSEG2.
100 static const int timing_pts[23][2] = {
101 {0x0, 0x0}, // 2, 50%
102 {0x1, 0x0}, // 3, 67%
103 {0x2, 0x0}, // 4, 75%
104 {0x3, 0x0}, // 5, 80%
105 {0x3, 0x1}, // 6, 67%
106 {0x4, 0x1}, // 7, 71%
107 {0x5, 0x1}, // 8, 75%
108 {0x6, 0x1}, // 9, 78%
109 {0x6, 0x2}, // 10, 70%
110 {0x7, 0x2}, // 11, 73%
111 {0x8, 0x2}, // 12, 75%
112 {0x9, 0x2}, // 13, 77%
113 {0x9, 0x3}, // 14, 71%
114 {0xA, 0x3}, // 15, 73%
115 {0xB, 0x3}, // 16, 75%
116 {0xC, 0x3}, // 17, 76%
117 {0xD, 0x3}, // 18, 78%
118 {0xD, 0x4}, // 19, 74%
119 {0xE, 0x4}, // 20, 75%
120 {0xF, 0x4}, // 21, 76%
121 {0xF, 0x5}, // 22, 73%
122 {0xF, 0x6}, // 23, 70%
123 {0xF, 0x7}, // 24, 67%
126 static unsigned int can_speed(unsigned int sclk, unsigned int pclk, unsigned int cclk, unsigned char psjw) {
134 bitwidth = sclk / (pclk * cclk);
136 brp = bitwidth / 0x18;
137 while ((!hit) && (brp < bitwidth / 4)) {
139 for (bits = 22; bits > 0; bits--) {
140 calcbit = (bits + 3) * (brp + 1);
141 if (calcbit == bitwidth) {
149 btr = ((timing_pts[bits][1] << 20) & 0x00700000)
150 | ((timing_pts[bits][0] << 16) & 0x000F0000)
151 | ((psjw << 14) & 0x0000C000)
152 | ((brp << 0) & 0x000003FF);
160 void can_init(can_t *obj, PinName rd, PinName td) {
161 CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD);
162 CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD);
163 obj->dev = (LPC_CAN_TypeDef *)pinmap_merge(can_rd, can_td);
164 MBED_ASSERT((int)obj->dev != NC);
166 switch ((int)obj->dev) {
167 case CAN_1: LPC_SC->PCONP |= 1 << 13; break;
168 case CAN_2: LPC_SC->PCONP |= 1 << 14; break;
171 pinmap_pinout(rd, PinMap_CAN_RD);
172 pinmap_pinout(td, PinMap_CAN_TD);
175 obj->dev->IER = 0; // Disable Interrupts
176 can_frequency(obj, 100000);
178 LPC_CANAF->AFMR = ACCF_BYPASS; // Bypass Filter
181 void can_free(can_t *obj) {
182 switch ((int)obj->dev) {
183 case CAN_1: LPC_SC->PCONP &= ~(1 << 13); break;
184 case CAN_2: LPC_SC->PCONP &= ~(1 << 14); break;
188 int can_frequency(can_t *obj, int f) {
189 int pclk = can_pclk(obj);
190 int btr = can_speed(SystemCoreClock, pclk, (unsigned int)f, 1);
193 uint32_t modmask = can_disable(obj);
195 obj->dev->MOD = modmask;
202 int can_write(can_t *obj, CAN_Message msg, int cc) {
203 unsigned int CANStatus;
209 m.dlc = msg.len & 0xF;
212 memcpy(m.data, msg.data, msg.len);
213 const unsigned int *buf = (const unsigned int *)&m;
215 CANStatus = obj->dev->SR;
216 if (CANStatus & 0x00000004) {
217 obj->dev->TFI1 = buf[0] & 0xC00F0000;
218 obj->dev->TID1 = buf[1];
219 obj->dev->TDA1 = buf[2];
220 obj->dev->TDB1 = buf[3];
222 obj->dev->CMR = 0x30;
224 obj->dev->CMR = 0x21;
228 } else if (CANStatus & 0x00000400) {
229 obj->dev->TFI2 = buf[0] & 0xC00F0000;
230 obj->dev->TID2 = buf[1];
231 obj->dev->TDA2 = buf[2];
232 obj->dev->TDB2 = buf[3];
234 obj->dev->CMR = 0x50;
236 obj->dev->CMR = 0x41;
240 } else if (CANStatus & 0x00040000) {
241 obj->dev->TFI3 = buf[0] & 0xC00F0000;
242 obj->dev->TID3 = buf[1];
243 obj->dev->TDA3 = buf[2];
244 obj->dev->TDB3 = buf[3];
246 obj->dev->CMR = 0x90;
248 obj->dev->CMR = 0x81;
256 int can_read(can_t *obj, CAN_Message *msg, int handle) {
258 unsigned int *i = (unsigned int *)&x;
262 if (obj->dev->GSR & 0x1) {
263 *i++ = obj->dev->RFS; // Frame
264 *i++ = obj->dev->RID; // ID
265 *i++ = obj->dev->RDA; // Data A
266 *i++ = obj->dev->RDB; // Data B
267 obj->dev->CMR = 0x04; // release receive buffer
271 msg->format = (x.type)? CANExtended : CANStandard;
272 msg->type = (x.rtr)? CANRemote: CANData;
273 memcpy(msg->data,x.data,x.dlc);
280 void can_reset(can_t *obj) {
282 obj->dev->GSR = 0; // Reset error counter when CAN1MOD is in reset
285 unsigned char can_rderror(can_t *obj) {
286 return (obj->dev->GSR >> 16) & 0xFF;
289 unsigned char can_tderror(can_t *obj) {
290 return (obj->dev->GSR >> 24) & 0xFF;
293 void can_monitor(can_t *obj, int silent) {
294 uint32_t mod_mask = can_disable(obj);
296 obj->dev->MOD |= (1 << 1);
298 obj->dev->MOD &= ~(1 << 1);
300 if (!(mod_mask & 1)) {