mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /* mbed Microcontroller Library
elessair 0:f269e3021894 2 * Copyright (c) 2006-2015 ARM Limited
elessair 0:f269e3021894 3 *
elessair 0:f269e3021894 4 * Licensed under the Apache License, Version 2.0 (the "License");
elessair 0:f269e3021894 5 * you may not use this file except in compliance with the License.
elessair 0:f269e3021894 6 * You may obtain a copy of the License at
elessair 0:f269e3021894 7 *
elessair 0:f269e3021894 8 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 9 *
elessair 0:f269e3021894 10 * Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 11 * distributed under the License is distributed on an "AS IS" BASIS,
elessair 0:f269e3021894 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 13 * See the License for the specific language governing permissions and
elessair 0:f269e3021894 14 * limitations under the License.
elessair 0:f269e3021894 15 */
elessair 0:f269e3021894 16 #include "mbed_assert.h"
elessair 0:f269e3021894 17 #include "can_api.h"
elessair 0:f269e3021894 18 #include "cmsis.h"
elessair 0:f269e3021894 19 #include "pinmap.h"
elessair 0:f269e3021894 20
elessair 0:f269e3021894 21 #include <math.h>
elessair 0:f269e3021894 22 #include <string.h>
elessair 0:f269e3021894 23
elessair 0:f269e3021894 24 /* Acceptance filter mode in AFMR register */
elessair 0:f269e3021894 25 #define ACCF_OFF 0x01
elessair 0:f269e3021894 26 #define ACCF_BYPASS 0x02
elessair 0:f269e3021894 27 #define ACCF_ON 0x00
elessair 0:f269e3021894 28 #define ACCF_FULLCAN 0x04
elessair 0:f269e3021894 29
elessair 0:f269e3021894 30 /* There are several bit timing calculators on the internet.
elessair 0:f269e3021894 31 http://www.port.de/engl/canprod/sv_req_form.html
elessair 0:f269e3021894 32 http://www.kvaser.com/can/index.htm
elessair 0:f269e3021894 33 */
elessair 0:f269e3021894 34
elessair 0:f269e3021894 35 static const PinMap PinMap_CAN_RD[] = {
elessair 0:f269e3021894 36 {P0_0 , CAN_1, 1},
elessair 0:f269e3021894 37 {P0_4 , CAN_2, 2},
elessair 0:f269e3021894 38 {P0_21, CAN_1, 3},
elessair 0:f269e3021894 39 {P2_7 , CAN_2, 1},
elessair 0:f269e3021894 40 {NC , NC , 0}
elessair 0:f269e3021894 41 };
elessair 0:f269e3021894 42
elessair 0:f269e3021894 43 static const PinMap PinMap_CAN_TD[] = {
elessair 0:f269e3021894 44 {P0_1 , CAN_1, 1},
elessair 0:f269e3021894 45 {P0_5 , CAN_2, 2},
elessair 0:f269e3021894 46 {P0_22, CAN_1, 3},
elessair 0:f269e3021894 47 {P2_8 , CAN_2, 1},
elessair 0:f269e3021894 48 {NC , NC , 0}
elessair 0:f269e3021894 49 };
elessair 0:f269e3021894 50
elessair 0:f269e3021894 51 // Type definition to hold a CAN message
elessair 0:f269e3021894 52 struct CANMsg {
elessair 0:f269e3021894 53 unsigned int reserved1 : 16;
elessair 0:f269e3021894 54 unsigned int dlc : 4; // Bits 16..19: DLC - Data Length Counter
elessair 0:f269e3021894 55 unsigned int reserved0 : 10;
elessair 0:f269e3021894 56 unsigned int rtr : 1; // Bit 30: Set if this is a RTR message
elessair 0:f269e3021894 57 unsigned int type : 1; // Bit 31: Set if this is a 29-bit ID message
elessair 0:f269e3021894 58 unsigned int id; // CAN Message ID (11-bit or 29-bit)
elessair 0:f269e3021894 59 unsigned char data[8]; // CAN Message Data Bytes 0-7
elessair 0:f269e3021894 60 };
elessair 0:f269e3021894 61 typedef struct CANMsg CANMsg;
elessair 0:f269e3021894 62
elessair 0:f269e3021894 63 static uint32_t can_disable(can_t *obj) {
elessair 0:f269e3021894 64 uint32_t sm = obj->dev->MOD;
elessair 0:f269e3021894 65 obj->dev->MOD |= 1;
elessair 0:f269e3021894 66 return sm;
elessair 0:f269e3021894 67 }
elessair 0:f269e3021894 68
elessair 0:f269e3021894 69 static inline void can_enable(can_t *obj) {
elessair 0:f269e3021894 70 if (obj->dev->MOD & 1) {
elessair 0:f269e3021894 71 obj->dev->MOD &= ~(1);
elessair 0:f269e3021894 72 }
elessair 0:f269e3021894 73 }
elessair 0:f269e3021894 74
elessair 0:f269e3021894 75 int can_mode(can_t *obj, CanMode mode) {
elessair 0:f269e3021894 76 return 0; // not implemented
elessair 0:f269e3021894 77 }
elessair 0:f269e3021894 78
elessair 0:f269e3021894 79 int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) {
elessair 0:f269e3021894 80 return 0; // not implemented
elessair 0:f269e3021894 81 }
elessair 0:f269e3021894 82
elessair 0:f269e3021894 83 static int can_pclk(can_t *obj) {
elessair 0:f269e3021894 84 int value = 0;
elessair 0:f269e3021894 85 switch ((int)obj->dev) {
elessair 0:f269e3021894 86 case CAN_1: value = (LPC_SC->PCLKSEL0 & (0x3 << 26)) >> 26; break;
elessair 0:f269e3021894 87 case CAN_2: value = (LPC_SC->PCLKSEL0 & (0x3 << 28)) >> 28; break;
elessair 0:f269e3021894 88 }
elessair 0:f269e3021894 89
elessair 0:f269e3021894 90 switch (value) {
elessair 0:f269e3021894 91 case 1: return 1;
elessair 0:f269e3021894 92 case 2: return 2;
elessair 0:f269e3021894 93 case 3: return 6;
elessair 0:f269e3021894 94 default: return 4;
elessair 0:f269e3021894 95 }
elessair 0:f269e3021894 96 }
elessair 0:f269e3021894 97
elessair 0:f269e3021894 98 // This table has the sampling points as close to 75% as possible. The first
elessair 0:f269e3021894 99 // value is TSEG1, the second TSEG2.
elessair 0:f269e3021894 100 static const int timing_pts[23][2] = {
elessair 0:f269e3021894 101 {0x0, 0x0}, // 2, 50%
elessair 0:f269e3021894 102 {0x1, 0x0}, // 3, 67%
elessair 0:f269e3021894 103 {0x2, 0x0}, // 4, 75%
elessair 0:f269e3021894 104 {0x3, 0x0}, // 5, 80%
elessair 0:f269e3021894 105 {0x3, 0x1}, // 6, 67%
elessair 0:f269e3021894 106 {0x4, 0x1}, // 7, 71%
elessair 0:f269e3021894 107 {0x5, 0x1}, // 8, 75%
elessair 0:f269e3021894 108 {0x6, 0x1}, // 9, 78%
elessair 0:f269e3021894 109 {0x6, 0x2}, // 10, 70%
elessair 0:f269e3021894 110 {0x7, 0x2}, // 11, 73%
elessair 0:f269e3021894 111 {0x8, 0x2}, // 12, 75%
elessair 0:f269e3021894 112 {0x9, 0x2}, // 13, 77%
elessair 0:f269e3021894 113 {0x9, 0x3}, // 14, 71%
elessair 0:f269e3021894 114 {0xA, 0x3}, // 15, 73%
elessair 0:f269e3021894 115 {0xB, 0x3}, // 16, 75%
elessair 0:f269e3021894 116 {0xC, 0x3}, // 17, 76%
elessair 0:f269e3021894 117 {0xD, 0x3}, // 18, 78%
elessair 0:f269e3021894 118 {0xD, 0x4}, // 19, 74%
elessair 0:f269e3021894 119 {0xE, 0x4}, // 20, 75%
elessair 0:f269e3021894 120 {0xF, 0x4}, // 21, 76%
elessair 0:f269e3021894 121 {0xF, 0x5}, // 22, 73%
elessair 0:f269e3021894 122 {0xF, 0x6}, // 23, 70%
elessair 0:f269e3021894 123 {0xF, 0x7}, // 24, 67%
elessair 0:f269e3021894 124 };
elessair 0:f269e3021894 125
elessair 0:f269e3021894 126 static unsigned int can_speed(unsigned int sclk, unsigned int pclk, unsigned int cclk, unsigned char psjw) {
elessair 0:f269e3021894 127 uint32_t btr;
elessair 0:f269e3021894 128 uint16_t brp = 0;
elessair 0:f269e3021894 129 uint32_t calcbit;
elessair 0:f269e3021894 130 uint32_t bitwidth;
elessair 0:f269e3021894 131 int hit = 0;
elessair 0:f269e3021894 132 int bits;
elessair 0:f269e3021894 133
elessair 0:f269e3021894 134 bitwidth = sclk / (pclk * cclk);
elessair 0:f269e3021894 135
elessair 0:f269e3021894 136 brp = bitwidth / 0x18;
elessair 0:f269e3021894 137 while ((!hit) && (brp < bitwidth / 4)) {
elessair 0:f269e3021894 138 brp++;
elessair 0:f269e3021894 139 for (bits = 22; bits > 0; bits--) {
elessair 0:f269e3021894 140 calcbit = (bits + 3) * (brp + 1);
elessair 0:f269e3021894 141 if (calcbit == bitwidth) {
elessair 0:f269e3021894 142 hit = 1;
elessair 0:f269e3021894 143 break;
elessair 0:f269e3021894 144 }
elessair 0:f269e3021894 145 }
elessair 0:f269e3021894 146 }
elessair 0:f269e3021894 147
elessair 0:f269e3021894 148 if (hit) {
elessair 0:f269e3021894 149 btr = ((timing_pts[bits][1] << 20) & 0x00700000)
elessair 0:f269e3021894 150 | ((timing_pts[bits][0] << 16) & 0x000F0000)
elessair 0:f269e3021894 151 | ((psjw << 14) & 0x0000C000)
elessair 0:f269e3021894 152 | ((brp << 0) & 0x000003FF);
elessair 0:f269e3021894 153 } else {
elessair 0:f269e3021894 154 btr = 0xFFFFFFFF;
elessair 0:f269e3021894 155 }
elessair 0:f269e3021894 156
elessair 0:f269e3021894 157 return btr;
elessair 0:f269e3021894 158 }
elessair 0:f269e3021894 159
elessair 0:f269e3021894 160 void can_init(can_t *obj, PinName rd, PinName td) {
elessair 0:f269e3021894 161 CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD);
elessair 0:f269e3021894 162 CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD);
elessair 0:f269e3021894 163 obj->dev = (LPC_CAN_TypeDef *)pinmap_merge(can_rd, can_td);
elessair 0:f269e3021894 164 MBED_ASSERT((int)obj->dev != NC);
elessair 0:f269e3021894 165
elessair 0:f269e3021894 166 switch ((int)obj->dev) {
elessair 0:f269e3021894 167 case CAN_1: LPC_SC->PCONP |= 1 << PCAN1; break;
elessair 0:f269e3021894 168 case CAN_2: LPC_SC->PCONP |= 1 << PCAN2; break;
elessair 0:f269e3021894 169 }
elessair 0:f269e3021894 170
elessair 0:f269e3021894 171 pinmap_pinout(rd, PinMap_CAN_RD);
elessair 0:f269e3021894 172 pinmap_pinout(td, PinMap_CAN_TD);
elessair 0:f269e3021894 173
elessair 0:f269e3021894 174 can_reset(obj);
elessair 0:f269e3021894 175 obj->dev->IER = 0; // Disable Interrupts
elessair 0:f269e3021894 176 can_frequency(obj, 100000);
elessair 0:f269e3021894 177
elessair 0:f269e3021894 178 LPC_CANAF->AFMR = ACCF_BYPASS; // Bypass Filter
elessair 0:f269e3021894 179 }
elessair 0:f269e3021894 180
elessair 0:f269e3021894 181 void can_free(can_t *obj) {
elessair 0:f269e3021894 182 switch ((int)obj->dev) {
elessair 0:f269e3021894 183 case CAN_1: LPC_SC->PCONP &= ~(1 << PCAN1); break;
elessair 0:f269e3021894 184 case CAN_2: LPC_SC->PCONP &= ~(1 << PCAN2); break;
elessair 0:f269e3021894 185 }
elessair 0:f269e3021894 186 }
elessair 0:f269e3021894 187
elessair 0:f269e3021894 188 int can_frequency(can_t *obj, int f) {
elessair 0:f269e3021894 189 int pclk = can_pclk(obj);
elessair 0:f269e3021894 190 int btr = can_speed(SystemCoreClock, pclk, (unsigned int)f, 1);
elessair 0:f269e3021894 191
elessair 0:f269e3021894 192 if (btr > 0) {
elessair 0:f269e3021894 193 uint32_t modmask = can_disable(obj);
elessair 0:f269e3021894 194 obj->dev->BTR = btr;
elessair 0:f269e3021894 195 obj->dev->MOD = modmask;
elessair 0:f269e3021894 196 return 1;
elessair 0:f269e3021894 197 } else {
elessair 0:f269e3021894 198 return 0;
elessair 0:f269e3021894 199 }
elessair 0:f269e3021894 200 }
elessair 0:f269e3021894 201
elessair 0:f269e3021894 202 int can_write(can_t *obj, CAN_Message msg, int cc) {
elessair 0:f269e3021894 203 unsigned int CANStatus;
elessair 0:f269e3021894 204 CANMsg m;
elessair 0:f269e3021894 205
elessair 0:f269e3021894 206 can_enable(obj);
elessair 0:f269e3021894 207
elessair 0:f269e3021894 208 m.id = msg.id ;
elessair 0:f269e3021894 209 m.dlc = msg.len & 0xF;
elessair 0:f269e3021894 210 m.rtr = msg.type;
elessair 0:f269e3021894 211 m.type = msg.format;
elessair 0:f269e3021894 212 memcpy(m.data, msg.data, msg.len);
elessair 0:f269e3021894 213 const unsigned int *buf = (const unsigned int *)&m;
elessair 0:f269e3021894 214
elessair 0:f269e3021894 215 CANStatus = obj->dev->SR;
elessair 0:f269e3021894 216 if (CANStatus & 0x00000004) {
elessair 0:f269e3021894 217 obj->dev->TFI1 = buf[0] & 0xC00F0000;
elessair 0:f269e3021894 218 obj->dev->TID1 = buf[1];
elessair 0:f269e3021894 219 obj->dev->TDA1 = buf[2];
elessair 0:f269e3021894 220 obj->dev->TDB1 = buf[3];
elessair 0:f269e3021894 221 if (cc) {
elessair 0:f269e3021894 222 obj->dev->CMR = 0x30;
elessair 0:f269e3021894 223 } else {
elessair 0:f269e3021894 224 obj->dev->CMR = 0x21;
elessair 0:f269e3021894 225 }
elessair 0:f269e3021894 226 return 1;
elessair 0:f269e3021894 227
elessair 0:f269e3021894 228 } else if (CANStatus & 0x00000400) {
elessair 0:f269e3021894 229 obj->dev->TFI2 = buf[0] & 0xC00F0000;
elessair 0:f269e3021894 230 obj->dev->TID2 = buf[1];
elessair 0:f269e3021894 231 obj->dev->TDA2 = buf[2];
elessair 0:f269e3021894 232 obj->dev->TDB2 = buf[3];
elessair 0:f269e3021894 233 if (cc) {
elessair 0:f269e3021894 234 obj->dev->CMR = 0x50;
elessair 0:f269e3021894 235 } else {
elessair 0:f269e3021894 236 obj->dev->CMR = 0x41;
elessair 0:f269e3021894 237 }
elessair 0:f269e3021894 238 return 1;
elessair 0:f269e3021894 239
elessair 0:f269e3021894 240 } else if (CANStatus & 0x00040000) {
elessair 0:f269e3021894 241 obj->dev->TFI3 = buf[0] & 0xC00F0000;
elessair 0:f269e3021894 242 obj->dev->TID3 = buf[1];
elessair 0:f269e3021894 243 obj->dev->TDA3 = buf[2];
elessair 0:f269e3021894 244 obj->dev->TDB3 = buf[3];
elessair 0:f269e3021894 245 if (cc) {
elessair 0:f269e3021894 246 obj->dev->CMR = 0x90;
elessair 0:f269e3021894 247 } else {
elessair 0:f269e3021894 248 obj->dev->CMR = 0x81;
elessair 0:f269e3021894 249 }
elessair 0:f269e3021894 250 return 1;
elessair 0:f269e3021894 251 }
elessair 0:f269e3021894 252
elessair 0:f269e3021894 253 return 0;
elessair 0:f269e3021894 254 }
elessair 0:f269e3021894 255
elessair 0:f269e3021894 256 int can_read(can_t *obj, CAN_Message *msg, int handle) {
elessair 0:f269e3021894 257 CANMsg x;
elessair 0:f269e3021894 258 unsigned int *i = (unsigned int *)&x;
elessair 0:f269e3021894 259
elessair 0:f269e3021894 260 can_enable(obj);
elessair 0:f269e3021894 261
elessair 0:f269e3021894 262 if (obj->dev->GSR & 0x1) {
elessair 0:f269e3021894 263 *i++ = obj->dev->RFS; // Frame
elessair 0:f269e3021894 264 *i++ = obj->dev->RID; // ID
elessair 0:f269e3021894 265 *i++ = obj->dev->RDA; // Data A
elessair 0:f269e3021894 266 *i++ = obj->dev->RDB; // Data B
elessair 0:f269e3021894 267 obj->dev->CMR = 0x04; // release receive buffer
elessair 0:f269e3021894 268
elessair 0:f269e3021894 269 msg->id = x.id;
elessair 0:f269e3021894 270 msg->len = x.dlc;
elessair 0:f269e3021894 271 msg->format = (x.type)? CANExtended : CANStandard;
elessair 0:f269e3021894 272 msg->type = (x.rtr)? CANRemote: CANData;
elessair 0:f269e3021894 273 memcpy(msg->data,x.data,x.dlc);
elessair 0:f269e3021894 274 return 1;
elessair 0:f269e3021894 275 }
elessair 0:f269e3021894 276
elessair 0:f269e3021894 277 return 0;
elessair 0:f269e3021894 278 }
elessair 0:f269e3021894 279
elessair 0:f269e3021894 280 void can_reset(can_t *obj) {
elessair 0:f269e3021894 281 can_disable(obj);
elessair 0:f269e3021894 282 obj->dev->GSR = 0; // Reset error counter when CAN1MOD is in reset
elessair 0:f269e3021894 283 }
elessair 0:f269e3021894 284
elessair 0:f269e3021894 285 unsigned char can_rderror(can_t *obj) {
elessair 0:f269e3021894 286 return (obj->dev->GSR >> 16) & 0xFF;
elessair 0:f269e3021894 287 }
elessair 0:f269e3021894 288
elessair 0:f269e3021894 289 unsigned char can_tderror(can_t *obj) {
elessair 0:f269e3021894 290 return (obj->dev->GSR >> 24) & 0xFF;
elessair 0:f269e3021894 291 }
elessair 0:f269e3021894 292
elessair 0:f269e3021894 293 void can_monitor(can_t *obj, int silent) {
elessair 0:f269e3021894 294 uint32_t mod_mask = can_disable(obj);
elessair 0:f269e3021894 295 if (silent) {
elessair 0:f269e3021894 296 obj->dev->MOD |= (1 << 1);
elessair 0:f269e3021894 297 } else {
elessair 0:f269e3021894 298 obj->dev->MOD &= ~(1 << 1);
elessair 0:f269e3021894 299 }
elessair 0:f269e3021894 300 if (!(mod_mask & 1)) {
elessair 0:f269e3021894 301 can_enable(obj);
elessair 0:f269e3021894 302 }
elessair 0:f269e3021894 303 }