]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC23XX/can_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC23XX / can_api.c
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 #include "mbed_assert.h"
17 #include "can_api.h"
18 #include "cmsis.h"
19 #include "pinmap.h"
20
21 #include <math.h>
22 #include <string.h>
23
24 /* Acceptance filter mode in AFMR register */
25 #define ACCF_OFF                0x01
26 #define ACCF_BYPASS             0x02
27 #define ACCF_ON                 0x00
28 #define ACCF_FULLCAN            0x04
29
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
33 */
34
35 static const PinMap PinMap_CAN_RD[] = {
36     {P0_0 , CAN_1, 1},
37     {P0_4 , CAN_2, 2},
38     {P0_21, CAN_1, 3},
39     {P2_7 , CAN_2, 1},
40     {NC   , NC   , 0}
41 };
42
43 static const PinMap PinMap_CAN_TD[] = {
44     {P0_1 , CAN_1, 1},
45     {P0_5 , CAN_2, 2},
46     {P0_22, CAN_1, 3},
47     {P2_8 , CAN_2, 1},
48     {NC   , NC   , 0}
49 };
50
51 // Type definition to hold a CAN message
52 struct CANMsg {
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
60 };
61 typedef struct CANMsg CANMsg;
62
63 static uint32_t can_disable(can_t *obj) {
64     uint32_t sm = obj->dev->MOD;
65     obj->dev->MOD |= 1;
66     return sm;
67 }
68
69 static inline void can_enable(can_t *obj) {
70     if (obj->dev->MOD & 1) {
71         obj->dev->MOD &= ~(1);
72     }
73 }
74
75 int can_mode(can_t *obj, CanMode mode) {
76     return 0; // not implemented
77 }
78
79 int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) {
80     return 0; // not implemented
81 }
82
83 static int can_pclk(can_t *obj) {
84     int value = 0;
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;
88     }
89     
90     switch (value) {
91         case 1: return 1;
92         case 2: return 2;
93         case 3: return 6;
94         default: return 4;
95     }
96 }
97
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%
124 };
125
126 static unsigned int can_speed(unsigned int sclk, unsigned int pclk, unsigned int cclk, unsigned char psjw) {
127     uint32_t    btr;
128     uint16_t    brp = 0;
129     uint32_t    calcbit;
130     uint32_t    bitwidth;
131     int         hit = 0;
132     int         bits;
133     
134     bitwidth = sclk / (pclk * cclk);
135     
136     brp = bitwidth / 0x18;
137     while ((!hit) && (brp < bitwidth / 4)) {
138         brp++;
139         for (bits = 22; bits > 0; bits--) {
140             calcbit = (bits + 3) * (brp + 1);
141             if (calcbit == bitwidth) {
142                 hit = 1;
143                 break;
144             }
145         }
146     }
147     
148     if (hit) {
149         btr = ((timing_pts[bits][1] << 20) & 0x00700000)
150             | ((timing_pts[bits][0] << 16) & 0x000F0000)
151             | ((psjw                << 14) & 0x0000C000)
152             | ((brp                 <<  0) & 0x000003FF);
153     } else {
154         btr = 0xFFFFFFFF;
155     }
156     
157     return btr;
158 }
159
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);
165     
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;
169     }
170     
171     pinmap_pinout(rd, PinMap_CAN_RD);
172     pinmap_pinout(td, PinMap_CAN_TD);
173     
174     can_reset(obj);
175     obj->dev->IER = 0;             // Disable Interrupts
176     can_frequency(obj, 100000);
177     
178     LPC_CANAF->AFMR = ACCF_BYPASS; // Bypass Filter
179 }
180
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;
185     }
186 }
187
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);
191     
192     if (btr > 0) {
193         uint32_t modmask = can_disable(obj);
194         obj->dev->BTR = btr;
195         obj->dev->MOD = modmask;
196         return 1;
197     } else {
198         return 0;
199     }
200 }
201
202 int can_write(can_t *obj, CAN_Message msg, int cc) {
203     unsigned int CANStatus;
204     CANMsg m;
205     
206     can_enable(obj);
207     
208     m.id   = msg.id ;
209     m.dlc  = msg.len & 0xF;
210     m.rtr  = msg.type;
211     m.type = msg.format;
212     memcpy(m.data, msg.data, msg.len);
213     const unsigned int *buf = (const unsigned int *)&m;
214     
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];
221         if (cc) {
222             obj->dev->CMR = 0x30;
223         } else {
224             obj->dev->CMR = 0x21;
225         }
226         return 1;
227     
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];
233         if (cc) {
234             obj->dev->CMR = 0x50;
235         } else {
236             obj->dev->CMR = 0x41;
237         }
238         return 1;
239     
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];
245         if (cc) {
246             obj->dev->CMR = 0x90;
247         } else {
248             obj->dev->CMR = 0x81;
249         }
250         return 1;
251     }
252     
253     return 0;
254 }
255
256 int can_read(can_t *obj, CAN_Message *msg, int handle) {
257     CANMsg x;
258     unsigned int *i = (unsigned int *)&x;
259     
260     can_enable(obj);
261     
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
268         
269         msg->id     = x.id;
270         msg->len    = x.dlc;
271         msg->format = (x.type)? CANExtended : CANStandard;
272         msg->type   = (x.rtr)?  CANRemote:    CANData;
273         memcpy(msg->data,x.data,x.dlc);
274         return 1;
275     }
276     
277     return 0;
278 }
279
280 void can_reset(can_t *obj) {
281     can_disable(obj);
282     obj->dev->GSR = 0; // Reset error counter when CAN1MOD is in reset
283 }
284
285 unsigned char can_rderror(can_t *obj) {
286     return (obj->dev->GSR >> 16) & 0xFF;
287 }
288
289 unsigned char can_tderror(can_t *obj) {
290     return (obj->dev->GSR >> 24) & 0xFF;
291 }
292
293 void can_monitor(can_t *obj, int silent) {
294     uint32_t mod_mask = can_disable(obj);
295     if (silent) {
296         obj->dev->MOD |= (1 << 1);
297     } else {
298         obj->dev->MOD &= ~(1 << 1);
299     }
300     if (!(mod_mask & 1)) {
301         can_enable(obj);
302     }
303 }