mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

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-2013 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
elessair 0:f269e3021894 17 #include "can_api.h"
elessair 0:f269e3021894 18
elessair 0:f269e3021894 19 #include "cmsis.h"
elessair 0:f269e3021894 20 #include "mbed_error.h"
elessair 0:f269e3021894 21
elessair 0:f269e3021894 22 #include <math.h>
elessair 0:f269e3021894 23 #include <string.h>
elessair 0:f269e3021894 24
elessair 0:f269e3021894 25 /* Handy defines */
elessair 0:f269e3021894 26 #define MSG_OBJ_MAX 32
elessair 0:f269e3021894 27 #define DLC_MAX 8
elessair 0:f269e3021894 28
elessair 0:f269e3021894 29 #define ID_STD_MASK 0x07FF
elessair 0:f269e3021894 30 #define ID_EXT_MASK 0x1FFFFFFF
elessair 0:f269e3021894 31 #define DLC_MASK 0x0F
elessair 0:f269e3021894 32
elessair 0:f269e3021894 33 #define CANIFn_ARB2_DIR (1UL << 13)
elessair 0:f269e3021894 34 #define CANIFn_ARB2_XTD (1UL << 14)
elessair 0:f269e3021894 35 #define CANIFn_ARB2_MSGVAL (1UL << 15)
elessair 0:f269e3021894 36 #define CANIFn_MSK2_MXTD (1UL << 15)
elessair 0:f269e3021894 37 #define CANIFn_MSK2_MDIR (1UL << 14)
elessair 0:f269e3021894 38 #define CANIFn_MCTRL_EOB (1UL << 7)
elessair 0:f269e3021894 39 #define CANIFn_MCTRL_TXRQST (1UL << 8)
elessair 0:f269e3021894 40 #define CANIFn_MCTRL_RMTEN (1UL << 9)
elessair 0:f269e3021894 41 #define CANIFn_MCTRL_RXIE (1UL << 10)
elessair 0:f269e3021894 42 #define CANIFn_MCTRL_TXIE (1UL << 11)
elessair 0:f269e3021894 43 #define CANIFn_MCTRL_UMASK (1UL << 12)
elessair 0:f269e3021894 44 #define CANIFn_MCTRL_INTPND (1UL << 13)
elessair 0:f269e3021894 45 #define CANIFn_MCTRL_MSGLST (1UL << 14)
elessair 0:f269e3021894 46 #define CANIFn_MCTRL_NEWDAT (1UL << 15)
elessair 0:f269e3021894 47 #define CANIFn_CMDMSK_DATA_B (1UL << 0)
elessair 0:f269e3021894 48 #define CANIFn_CMDMSK_DATA_A (1UL << 1)
elessair 0:f269e3021894 49 #define CANIFn_CMDMSK_TXRQST (1UL << 2)
elessair 0:f269e3021894 50 #define CANIFn_CMDMSK_NEWDAT (1UL << 2)
elessair 0:f269e3021894 51 #define CANIFn_CMDMSK_CLRINTPND (1UL << 3)
elessair 0:f269e3021894 52 #define CANIFn_CMDMSK_CTRL (1UL << 4)
elessair 0:f269e3021894 53 #define CANIFn_CMDMSK_ARB (1UL << 5)
elessair 0:f269e3021894 54 #define CANIFn_CMDMSK_MASK (1UL << 6)
elessair 0:f269e3021894 55 #define CANIFn_CMDMSK_WR (1UL << 7)
elessair 0:f269e3021894 56 #define CANIFn_CMDMSK_RD (0UL << 7)
elessair 0:f269e3021894 57 #define CANIFn_CMDREQ_BUSY (1UL << 15)
elessair 0:f269e3021894 58
elessair 0:f269e3021894 59 #define CANCNTL_INIT (1 << 0) // Initialization
elessair 0:f269e3021894 60 #define CANCNTL_IE (1 << 1) // Module interrupt enable
elessair 0:f269e3021894 61 #define CANCNTL_SIE (1 << 2) // Status change interrupt enable
elessair 0:f269e3021894 62 #define CANCNTL_EIE (1 << 3) // Error interrupt enable
elessair 0:f269e3021894 63 #define CANCNTL_DAR (1 << 5) // Disable automatic retransmission
elessair 0:f269e3021894 64 #define CANCNTL_CCE (1 << 6) // Configuration change enable
elessair 0:f269e3021894 65 #define CANCNTL_TEST (1 << 7) // Test mode enable
elessair 0:f269e3021894 66
elessair 0:f269e3021894 67 #define CANTEST_BASIC (1 << 2) // Basic mode
elessair 0:f269e3021894 68 #define CANTEST_SILENT (1 << 3) // Silent mode
elessair 0:f269e3021894 69 #define CANTEST_LBACK (1 << 4) // Loop back mode
elessair 0:f269e3021894 70 #define CANTEST_TX_MASK 0x0060 // Control of CAN_TXD pins
elessair 0:f269e3021894 71 #define CANTEST_TX_SHIFT 5
elessair 0:f269e3021894 72 #define CANTEST_RX (1 << 7) // Monitors the actual value of the CAN_RXD pin.
elessair 0:f269e3021894 73
elessair 0:f269e3021894 74 static uint32_t can_irq_id = 0;
elessair 0:f269e3021894 75 static can_irq_handler irq_handler;
elessair 0:f269e3021894 76
elessair 0:f269e3021894 77 static inline void can_disable(can_t *obj) {
elessair 0:f269e3021894 78 LPC_C_CAN0->CANCNTL |= 0x1;
elessair 0:f269e3021894 79 }
elessair 0:f269e3021894 80
elessair 0:f269e3021894 81 static inline void can_enable(can_t *obj) {
elessair 0:f269e3021894 82 if (LPC_C_CAN0->CANCNTL & 0x1) {
elessair 0:f269e3021894 83 LPC_C_CAN0->CANCNTL &= ~(0x1);
elessair 0:f269e3021894 84 }
elessair 0:f269e3021894 85 }
elessair 0:f269e3021894 86
elessair 0:f269e3021894 87 int can_mode(can_t *obj, CanMode mode) {
elessair 0:f269e3021894 88 int success = 0;
elessair 0:f269e3021894 89 switch (mode) {
elessair 0:f269e3021894 90 case MODE_RESET:
elessair 0:f269e3021894 91 LPC_C_CAN0->CANCNTL &=~CANCNTL_TEST;
elessair 0:f269e3021894 92 can_disable(obj);
elessair 0:f269e3021894 93 success = 1;
elessair 0:f269e3021894 94 break;
elessair 0:f269e3021894 95 case MODE_NORMAL:
elessair 0:f269e3021894 96 LPC_C_CAN0->CANCNTL &=~CANCNTL_TEST;
elessair 0:f269e3021894 97 can_enable(obj);
elessair 0:f269e3021894 98 success = 1;
elessair 0:f269e3021894 99 break;
elessair 0:f269e3021894 100 case MODE_SILENT:
elessair 0:f269e3021894 101 LPC_C_CAN0->CANCNTL |= CANCNTL_TEST;
elessair 0:f269e3021894 102 LPC_C_CAN0->CANTEST |= CANTEST_SILENT;
elessair 0:f269e3021894 103 LPC_C_CAN0->CANTEST &=~ CANTEST_LBACK;
elessair 0:f269e3021894 104 success = 1;
elessair 0:f269e3021894 105 break;
elessair 0:f269e3021894 106 case MODE_TEST_LOCAL:
elessair 0:f269e3021894 107 LPC_C_CAN0->CANCNTL |= CANCNTL_TEST;
elessair 0:f269e3021894 108 LPC_C_CAN0->CANTEST &=~CANTEST_SILENT;
elessair 0:f269e3021894 109 LPC_C_CAN0->CANTEST |= CANTEST_LBACK;
elessair 0:f269e3021894 110 success = 1;
elessair 0:f269e3021894 111 break;
elessair 0:f269e3021894 112 case MODE_TEST_SILENT:
elessair 0:f269e3021894 113 LPC_C_CAN0->CANCNTL |= CANCNTL_TEST;
elessair 0:f269e3021894 114 LPC_C_CAN0->CANTEST |= (CANTEST_LBACK | CANTEST_SILENT);
elessair 0:f269e3021894 115 success = 1;
elessair 0:f269e3021894 116 break;
elessair 0:f269e3021894 117 case MODE_TEST_GLOBAL:
elessair 0:f269e3021894 118 default:
elessair 0:f269e3021894 119 success = 0;
elessair 0:f269e3021894 120 break;
elessair 0:f269e3021894 121 }
elessair 0:f269e3021894 122
elessair 0:f269e3021894 123 return success;
elessair 0:f269e3021894 124 }
elessair 0:f269e3021894 125
elessair 0:f269e3021894 126 int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) {
elessair 0:f269e3021894 127 uint16_t i;
elessair 0:f269e3021894 128
elessair 0:f269e3021894 129 // Find first free message object
elessair 0:f269e3021894 130 if (handle == 0) {
elessair 0:f269e3021894 131 uint32_t msgval = LPC_C_CAN0->CANMSGV1 | (LPC_C_CAN0->CANMSGV2 << 16);
elessair 0:f269e3021894 132
elessair 0:f269e3021894 133 // Find first free messagebox
elessair 0:f269e3021894 134 for (i = 0; i < 32; i++) {
elessair 0:f269e3021894 135 if ((msgval & (1 << i)) == 0) {
elessair 0:f269e3021894 136 handle = i+1;
elessair 0:f269e3021894 137 break;
elessair 0:f269e3021894 138 }
elessair 0:f269e3021894 139 }
elessair 0:f269e3021894 140 }
elessair 0:f269e3021894 141
elessair 0:f269e3021894 142 if (handle > 0 && handle < 32) {
elessair 0:f269e3021894 143 if (format == CANExtended) {
elessair 0:f269e3021894 144 // Mark message valid, Direction = TX, Extended Frame, Set Identifier and mask everything
elessair 0:f269e3021894 145 LPC_C_CAN0->CANIF1_ARB1 = (id & 0xFFFF);
elessair 0:f269e3021894 146 LPC_C_CAN0->CANIF1_ARB2 = CANIFn_ARB2_MSGVAL | CANIFn_ARB2_XTD | ((id >> 16) & 0x1FFF);
elessair 0:f269e3021894 147 LPC_C_CAN0->CANIF1_MSK1 = (mask & 0xFFFF);
elessair 0:f269e3021894 148 LPC_C_CAN0->CANIF1_MSK2 = CANIFn_MSK2_MXTD /*| CANIFn_MSK2_MDIR*/ | ((mask >> 16) & 0x1FFF);
elessair 0:f269e3021894 149 } else {
elessair 0:f269e3021894 150 // Mark message valid, Direction = TX, Set Identifier and mask everything
elessair 0:f269e3021894 151 LPC_C_CAN0->CANIF1_ARB2 = CANIFn_ARB2_MSGVAL | ((id << 2) & 0x1FFF);
elessair 0:f269e3021894 152 LPC_C_CAN0->CANIF1_MSK2 = /*CANIFn_MSK2_MDIR |*/ ((mask << 2) & 0x1FFF);
elessair 0:f269e3021894 153 }
elessair 0:f269e3021894 154
elessair 0:f269e3021894 155 // Use mask, single message object and set DLC
elessair 0:f269e3021894 156 LPC_C_CAN0->CANIF1_MCTRL = CANIFn_MCTRL_UMASK | CANIFn_MCTRL_EOB | CANIFn_MCTRL_RXIE | (DLC_MAX & 0xF);
elessair 0:f269e3021894 157
elessair 0:f269e3021894 158 // Transfer all fields to message object
elessair 0:f269e3021894 159 LPC_C_CAN0->CANIF1_CMDMSK_W = CANIFn_CMDMSK_WR | CANIFn_CMDMSK_MASK | CANIFn_CMDMSK_ARB | CANIFn_CMDMSK_CTRL;
elessair 0:f269e3021894 160
elessair 0:f269e3021894 161 // Start Transfer to given message number
elessair 0:f269e3021894 162 LPC_C_CAN0->CANIF1_CMDREQ = (handle & 0x3F);
elessair 0:f269e3021894 163
elessair 0:f269e3021894 164 // Wait until transfer to message ram complete - TODO: maybe not block??
elessair 0:f269e3021894 165 while ( LPC_C_CAN0->CANIF1_CMDREQ & CANIFn_CMDREQ_BUSY );
elessair 0:f269e3021894 166 }
elessair 0:f269e3021894 167
elessair 0:f269e3021894 168 return handle;
elessair 0:f269e3021894 169 }
elessair 0:f269e3021894 170
elessair 0:f269e3021894 171 static inline void can_irq() {
elessair 0:f269e3021894 172 irq_handler(can_irq_id, IRQ_RX);
elessair 0:f269e3021894 173 }
elessair 0:f269e3021894 174
elessair 0:f269e3021894 175 // Register CAN object's irq handler
elessair 0:f269e3021894 176 void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id) {
elessair 0:f269e3021894 177 irq_handler = handler;
elessair 0:f269e3021894 178 can_irq_id = id;
elessair 0:f269e3021894 179 }
elessair 0:f269e3021894 180
elessair 0:f269e3021894 181 // Unregister CAN object's irq handler
elessair 0:f269e3021894 182 void can_irq_free(can_t *obj) {
elessair 0:f269e3021894 183 LPC_C_CAN0->CANCNTL &= ~(1UL << 1); // Disable Interrupts :)
elessair 0:f269e3021894 184 can_irq_id = 0;
elessair 0:f269e3021894 185 NVIC_DisableIRQ(C_CAN0_IRQn);
elessair 0:f269e3021894 186 }
elessair 0:f269e3021894 187
elessair 0:f269e3021894 188 // Clear or set a irq
elessair 0:f269e3021894 189 void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable) {
elessair 0:f269e3021894 190 // Put CAN in Reset Mode and enable interrupt
elessair 0:f269e3021894 191 can_disable(obj);
elessair 0:f269e3021894 192 if (enable == 0) {
elessair 0:f269e3021894 193 LPC_C_CAN0->CANCNTL &= ~(1UL << 1 | 1UL << 2);
elessair 0:f269e3021894 194 } else {
elessair 0:f269e3021894 195 LPC_C_CAN0->CANCNTL |= 1UL << 1 | 1UL << 2;
elessair 0:f269e3021894 196 }
elessair 0:f269e3021894 197 // Take it out of reset...
elessair 0:f269e3021894 198 can_enable(obj);
elessair 0:f269e3021894 199
elessair 0:f269e3021894 200 // Enable NVIC if at least 1 interrupt is active
elessair 0:f269e3021894 201 NVIC_SetVector(C_CAN0_IRQn, (uint32_t) &can_irq);
elessair 0:f269e3021894 202 NVIC_EnableIRQ(C_CAN0_IRQn);
elessair 0:f269e3021894 203 }
elessair 0:f269e3021894 204
elessair 0:f269e3021894 205 // This table has the sampling points as close to 75% as possible. The first
elessair 0:f269e3021894 206 // value is TSEG1, the second TSEG2.
elessair 0:f269e3021894 207 static const int timing_pts[23][2] = {
elessair 0:f269e3021894 208 {0x0, 0x0}, // 2, 50%
elessair 0:f269e3021894 209 {0x1, 0x0}, // 3, 67%
elessair 0:f269e3021894 210 {0x2, 0x0}, // 4, 75%
elessair 0:f269e3021894 211 {0x3, 0x0}, // 5, 80%
elessair 0:f269e3021894 212 {0x3, 0x1}, // 6, 67%
elessair 0:f269e3021894 213 {0x4, 0x1}, // 7, 71%
elessair 0:f269e3021894 214 {0x5, 0x1}, // 8, 75%
elessair 0:f269e3021894 215 {0x6, 0x1}, // 9, 78%
elessair 0:f269e3021894 216 {0x6, 0x2}, // 10, 70%
elessair 0:f269e3021894 217 {0x7, 0x2}, // 11, 73%
elessair 0:f269e3021894 218 {0x8, 0x2}, // 12, 75%
elessair 0:f269e3021894 219 {0x9, 0x2}, // 13, 77%
elessair 0:f269e3021894 220 {0x9, 0x3}, // 14, 71%
elessair 0:f269e3021894 221 {0xA, 0x3}, // 15, 73%
elessair 0:f269e3021894 222 {0xB, 0x3}, // 16, 75%
elessair 0:f269e3021894 223 {0xC, 0x3}, // 17, 76%
elessair 0:f269e3021894 224 {0xD, 0x3}, // 18, 78%
elessair 0:f269e3021894 225 {0xD, 0x4}, // 19, 74%
elessair 0:f269e3021894 226 {0xE, 0x4}, // 20, 75%
elessair 0:f269e3021894 227 {0xF, 0x4}, // 21, 76%
elessair 0:f269e3021894 228 {0xF, 0x5}, // 22, 73%
elessair 0:f269e3021894 229 {0xF, 0x6}, // 23, 70%
elessair 0:f269e3021894 230 {0xF, 0x7}, // 24, 67%
elessair 0:f269e3021894 231 };
elessair 0:f269e3021894 232
elessair 0:f269e3021894 233 static unsigned int can_speed(unsigned int sclk, unsigned int cclk, unsigned char psjw) {
elessair 0:f269e3021894 234 uint32_t btr;
elessair 0:f269e3021894 235 uint32_t clkdiv = 1;
elessair 0:f269e3021894 236 uint16_t brp = 0;
elessair 0:f269e3021894 237 uint32_t calcbit;
elessair 0:f269e3021894 238 uint32_t bitwidth;
elessair 0:f269e3021894 239 int hit = 0;
elessair 0:f269e3021894 240 int bits = 0;
elessair 0:f269e3021894 241
elessair 0:f269e3021894 242 bitwidth = sclk / cclk;
elessair 0:f269e3021894 243
elessair 0:f269e3021894 244 brp = bitwidth / 0x18;
elessair 0:f269e3021894 245 while ((!hit) && (brp < bitwidth / 4)) {
elessair 0:f269e3021894 246 brp++;
elessair 0:f269e3021894 247 for (bits = 22; bits > 0; bits--) {
elessair 0:f269e3021894 248 calcbit = (bits + 3) * (brp + 1);
elessair 0:f269e3021894 249 if (calcbit == bitwidth) {
elessair 0:f269e3021894 250 hit = 1;
elessair 0:f269e3021894 251 break;
elessair 0:f269e3021894 252 }
elessair 0:f269e3021894 253 }
elessair 0:f269e3021894 254 }
elessair 0:f269e3021894 255
elessair 0:f269e3021894 256 clkdiv = clkdiv - 1;
elessair 0:f269e3021894 257
elessair 0:f269e3021894 258 if (hit) {
elessair 0:f269e3021894 259 btr = (timing_pts[bits][1] & 0x7) << 12
elessair 0:f269e3021894 260 | (timing_pts[bits][0] & 0xf) << 8
elessair 0:f269e3021894 261 | (psjw & 0x3) << 6
elessair 0:f269e3021894 262 | (brp & 0x3F);
elessair 0:f269e3021894 263 btr = btr | (clkdiv << 16);
elessair 0:f269e3021894 264 } else {
elessair 0:f269e3021894 265 btr = 0;
elessair 0:f269e3021894 266 }
elessair 0:f269e3021894 267
elessair 0:f269e3021894 268 return btr;
elessair 0:f269e3021894 269 }
elessair 0:f269e3021894 270
elessair 0:f269e3021894 271
elessair 0:f269e3021894 272 int can_config_rxmsgobj(can_t *obj) {
elessair 0:f269e3021894 273 uint16_t i = 0;
elessair 0:f269e3021894 274
elessair 0:f269e3021894 275 // Make sure the interface is available
elessair 0:f269e3021894 276 while ( LPC_C_CAN0->CANIF1_CMDREQ & CANIFn_CMDREQ_BUSY );
elessair 0:f269e3021894 277
elessair 0:f269e3021894 278 // Mark message valid, Direction = RX, Don't care about anything else
elessair 0:f269e3021894 279 LPC_C_CAN0->CANIF1_ARB1 = 0;
elessair 0:f269e3021894 280 LPC_C_CAN0->CANIF1_ARB2 = 0;
elessair 0:f269e3021894 281 LPC_C_CAN0->CANIF1_MCTRL = 0;
elessair 0:f269e3021894 282
elessair 0:f269e3021894 283 for ( i = 0; i < MSG_OBJ_MAX; i++ ) {
elessair 0:f269e3021894 284 // Transfer arb and control fields to message object
elessair 0:f269e3021894 285 LPC_C_CAN0->CANIF1_CMDMSK_W = CANIFn_CMDMSK_WR | CANIFn_CMDMSK_ARB | CANIFn_CMDMSK_CTRL | CANIFn_CMDMSK_TXRQST;
elessair 0:f269e3021894 286
elessair 0:f269e3021894 287 // Start Transfer to given message number
elessair 0:f269e3021894 288 LPC_C_CAN0->CANIF1_CMDREQ = (i & 0x3F);
elessair 0:f269e3021894 289
elessair 0:f269e3021894 290 // Wait until transfer to message ram complete - TODO: maybe not block??
elessair 0:f269e3021894 291 while ( LPC_C_CAN0->CANIF1_CMDREQ & CANIFn_CMDREQ_BUSY );
elessair 0:f269e3021894 292 }
elessair 0:f269e3021894 293
elessair 0:f269e3021894 294 // Accept all messages
elessair 0:f269e3021894 295 can_filter(obj, 0, 0, CANStandard, 1);
elessair 0:f269e3021894 296
elessair 0:f269e3021894 297 return 1;
elessair 0:f269e3021894 298 }
elessair 0:f269e3021894 299
elessair 0:f269e3021894 300
elessair 0:f269e3021894 301 void can_init(can_t *obj, PinName rd, PinName td) {
elessair 0:f269e3021894 302 // Enable power and clock
elessair 0:f269e3021894 303 LPC_SYSCON->SYSAHBCLKCTRL1 |= (1UL << 7);
elessair 0:f269e3021894 304 LPC_SYSCON->PRESETCTRL1 |= (1UL << 7);
elessair 0:f269e3021894 305 LPC_SYSCON->PRESETCTRL1 &= ~(1UL << 7);
elessair 0:f269e3021894 306
elessair 0:f269e3021894 307 // Enable Initialization mode
elessair 0:f269e3021894 308 if (!(LPC_C_CAN0->CANCNTL & (1UL << 0))) {
elessair 0:f269e3021894 309 LPC_C_CAN0->CANCNTL |= (1UL << 0);
elessair 0:f269e3021894 310 }
elessair 0:f269e3021894 311
elessair 0:f269e3021894 312 LPC_SWM->PINASSIGN[6] &= ~(0x00FFFF00L);
elessair 0:f269e3021894 313 LPC_SWM->PINASSIGN[6] |= (rd << 16) | (td << 8);
elessair 0:f269e3021894 314
elessair 0:f269e3021894 315 can_frequency(obj, 100000);
elessair 0:f269e3021894 316
elessair 0:f269e3021894 317 // Resume operation
elessair 0:f269e3021894 318 LPC_C_CAN0->CANCNTL &= ~(1UL << 0);
elessair 0:f269e3021894 319 while ( LPC_C_CAN0->CANCNTL & (1UL << 0) );
elessair 0:f269e3021894 320
elessair 0:f269e3021894 321 // Initialize RX message object
elessair 0:f269e3021894 322 can_config_rxmsgobj(obj);
elessair 0:f269e3021894 323 }
elessair 0:f269e3021894 324
elessair 0:f269e3021894 325 void can_free(can_t *obj) {
elessair 0:f269e3021894 326 LPC_SYSCON->SYSAHBCLKCTRL1 &= ~(1UL << 7);
elessair 0:f269e3021894 327 LPC_SYSCON->PRESETCTRL1 &= ~(1UL << 7);
elessair 0:f269e3021894 328 }
elessair 0:f269e3021894 329
elessair 0:f269e3021894 330 int can_frequency(can_t *obj, int f) {
elessair 0:f269e3021894 331 int btr = can_speed(SystemCoreClock, (unsigned int)f, 1);
elessair 0:f269e3021894 332 int clkdiv = (btr >> 16) & 0x0F;
elessair 0:f269e3021894 333 btr = btr & 0xFFFF;
elessair 0:f269e3021894 334
elessair 0:f269e3021894 335 if (btr > 0) {
elessair 0:f269e3021894 336 // Set the bit clock
elessair 0:f269e3021894 337 LPC_C_CAN0->CANCNTL |= (1UL << 6 | 1UL << 0); // set CCE and INIT
elessair 0:f269e3021894 338 LPC_C_CAN0->CANCLKDIV = clkdiv;
elessair 0:f269e3021894 339 LPC_C_CAN0->CANBT = btr;
elessair 0:f269e3021894 340 LPC_C_CAN0->CANBRPE = 0x0000;
elessair 0:f269e3021894 341 LPC_C_CAN0->CANCNTL &= ~(1UL << 6 | 1UL << 0); // clear CCE and INIT
elessair 0:f269e3021894 342 return 1;
elessair 0:f269e3021894 343 }
elessair 0:f269e3021894 344 return 0;
elessair 0:f269e3021894 345 }
elessair 0:f269e3021894 346
elessair 0:f269e3021894 347 int can_write(can_t *obj, CAN_Message msg, int cc) {
elessair 0:f269e3021894 348 uint16_t msgnum = 0;
elessair 0:f269e3021894 349
elessair 0:f269e3021894 350 // Make sure controller is enabled
elessair 0:f269e3021894 351 can_enable(obj);
elessair 0:f269e3021894 352
elessair 0:f269e3021894 353 // Make sure the interface is available
elessair 0:f269e3021894 354 while ( LPC_C_CAN0->CANIF1_CMDREQ & CANIFn_CMDREQ_BUSY );
elessair 0:f269e3021894 355
elessair 0:f269e3021894 356 // Set the direction bit based on the message type
elessair 0:f269e3021894 357 uint32_t direction = 0;
elessair 0:f269e3021894 358 if (msg.type == CANData) {
elessair 0:f269e3021894 359 direction = CANIFn_ARB2_DIR;
elessair 0:f269e3021894 360 }
elessair 0:f269e3021894 361
elessair 0:f269e3021894 362 if (msg.format == CANExtended) {
elessair 0:f269e3021894 363 // Mark message valid, Extended Frame, Set Identifier and mask everything
elessair 0:f269e3021894 364 LPC_C_CAN0->CANIF1_ARB1 = (msg.id & 0xFFFF);
elessair 0:f269e3021894 365 LPC_C_CAN0->CANIF1_ARB2 = CANIFn_ARB2_MSGVAL | CANIFn_ARB2_XTD | direction | ((msg.id >> 16) & 0x1FFFF);
elessair 0:f269e3021894 366 LPC_C_CAN0->CANIF1_MSK1 = (ID_EXT_MASK & 0xFFFF);
elessair 0:f269e3021894 367 LPC_C_CAN0->CANIF1_MSK2 = CANIFn_MSK2_MXTD | CANIFn_MSK2_MDIR | ((ID_EXT_MASK >> 16) & 0x1FFF);
elessair 0:f269e3021894 368 } else {
elessair 0:f269e3021894 369 // Mark message valid, Set Identifier and mask everything
elessair 0:f269e3021894 370 LPC_C_CAN0->CANIF1_ARB2 = CANIFn_ARB2_MSGVAL | direction | ((msg.id << 2) & 0x1FFF);
elessair 0:f269e3021894 371 LPC_C_CAN0->CANIF1_MSK2 = CANIFn_MSK2_MDIR | ((ID_STD_MASK << 2) & 0x1FFF);
elessair 0:f269e3021894 372 }
elessair 0:f269e3021894 373
elessair 0:f269e3021894 374 // Use mask, request transmission, single message object and set DLC
elessair 0:f269e3021894 375 LPC_C_CAN0->CANIF1_MCTRL = CANIFn_MCTRL_UMASK | CANIFn_MCTRL_TXRQST | CANIFn_MCTRL_EOB | (msg.len & 0xF);
elessair 0:f269e3021894 376
elessair 0:f269e3021894 377 LPC_C_CAN0->CANIF1_DA1 = ((msg.data[1] & 0xFF) << 8) | (msg.data[0] & 0xFF);
elessair 0:f269e3021894 378 LPC_C_CAN0->CANIF1_DA2 = ((msg.data[3] & 0xFF) << 8) | (msg.data[2] & 0xFF);
elessair 0:f269e3021894 379 LPC_C_CAN0->CANIF1_DB1 = ((msg.data[5] & 0xFF) << 8) | (msg.data[4] & 0xFF);
elessair 0:f269e3021894 380 LPC_C_CAN0->CANIF1_DB2 = ((msg.data[7] & 0xFF) << 8) | (msg.data[6] & 0xFF);
elessair 0:f269e3021894 381
elessair 0:f269e3021894 382 // Transfer all fields to message object
elessair 0:f269e3021894 383 LPC_C_CAN0->CANIF1_CMDMSK_W = CANIFn_CMDMSK_WR | CANIFn_CMDMSK_MASK | CANIFn_CMDMSK_ARB | CANIFn_CMDMSK_CTRL | CANIFn_CMDMSK_TXRQST | CANIFn_CMDMSK_DATA_A | CANIFn_CMDMSK_DATA_B;
elessair 0:f269e3021894 384
elessair 0:f269e3021894 385 // Start Transfer to given message number
elessair 0:f269e3021894 386 LPC_C_CAN0->CANIF1_CMDREQ = (msgnum & 0x3F);
elessair 0:f269e3021894 387
elessair 0:f269e3021894 388 // Wait until transfer to message ram complete - TODO: maybe not block??
elessair 0:f269e3021894 389 while ( LPC_C_CAN0->CANIF1_CMDREQ & CANIFn_CMDREQ_BUSY);
elessair 0:f269e3021894 390
elessair 0:f269e3021894 391 // Wait until TXOK is set, then clear it - TODO: maybe not block
elessair 0:f269e3021894 392 //while ( !(LPC_C_CAN0->STAT & CANSTAT_TXOK) );
elessair 0:f269e3021894 393 LPC_C_CAN0->CANSTAT &= ~(1UL << 3);
elessair 0:f269e3021894 394
elessair 0:f269e3021894 395 return 1;
elessair 0:f269e3021894 396 }
elessair 0:f269e3021894 397
elessair 0:f269e3021894 398 int can_read(can_t *obj, CAN_Message *msg, int handle) {
elessair 0:f269e3021894 399 uint16_t i;
elessair 0:f269e3021894 400
elessair 0:f269e3021894 401 // Make sure controller is enabled
elessair 0:f269e3021894 402 can_enable(obj);
elessair 0:f269e3021894 403
elessair 0:f269e3021894 404 // Find first message object with new data
elessair 0:f269e3021894 405 if (handle == 0) {
elessair 0:f269e3021894 406 uint32_t newdata = LPC_C_CAN0->CANND1 | (LPC_C_CAN0->CANND2 << 16);
elessair 0:f269e3021894 407 // Find first free messagebox
elessair 0:f269e3021894 408 for (i = 0; i < 32; i++) {
elessair 0:f269e3021894 409 if (newdata & (1 << i)) {
elessair 0:f269e3021894 410 handle = i+1;
elessair 0:f269e3021894 411 break;
elessair 0:f269e3021894 412 }
elessair 0:f269e3021894 413 }
elessair 0:f269e3021894 414 }
elessair 0:f269e3021894 415
elessair 0:f269e3021894 416 if (handle > 0 && handle < 32) {
elessair 0:f269e3021894 417 // Wait until message interface is free
elessair 0:f269e3021894 418 while ( LPC_C_CAN0->CANIF2_CMDREQ & CANIFn_CMDREQ_BUSY );
elessair 0:f269e3021894 419
elessair 0:f269e3021894 420 // Transfer all fields to message object
elessair 0:f269e3021894 421 LPC_C_CAN0->CANIF2_CMDMSK_W = CANIFn_CMDMSK_RD | CANIFn_CMDMSK_MASK | CANIFn_CMDMSK_ARB | CANIFn_CMDMSK_CTRL | CANIFn_CMDMSK_CLRINTPND | CANIFn_CMDMSK_TXRQST | CANIFn_CMDMSK_DATA_A | CANIFn_CMDMSK_DATA_B;
elessair 0:f269e3021894 422
elessair 0:f269e3021894 423 // Start Transfer from given message number
elessair 0:f269e3021894 424 LPC_C_CAN0->CANIF2_CMDREQ = (handle & 0x3F);
elessair 0:f269e3021894 425
elessair 0:f269e3021894 426 // Wait until transfer to message ram complete
elessair 0:f269e3021894 427 while ( LPC_C_CAN0->CANIF2_CMDREQ & CANIFn_CMDREQ_BUSY );
elessair 0:f269e3021894 428
elessair 0:f269e3021894 429 if (LPC_C_CAN0->CANIF2_ARB2 & CANIFn_ARB2_XTD) {
elessair 0:f269e3021894 430 msg->format = CANExtended;
elessair 0:f269e3021894 431 msg->id = (LPC_C_CAN0->CANIF2_ARB1 & 0x1FFF) << 16;
elessair 0:f269e3021894 432 msg->id |= (LPC_C_CAN0->CANIF2_ARB2 & 0x1FFF);
elessair 0:f269e3021894 433 } else {
elessair 0:f269e3021894 434 msg->format = CANStandard;
elessair 0:f269e3021894 435 msg->id = (LPC_C_CAN0->CANIF2_ARB2 & 0x1FFF) >> 2;
elessair 0:f269e3021894 436 }
elessair 0:f269e3021894 437
elessair 0:f269e3021894 438 if (LPC_C_CAN0->CANIF2_ARB2 & CANIFn_ARB2_DIR) {
elessair 0:f269e3021894 439 msg->type = CANRemote;
elessair 0:f269e3021894 440 }
elessair 0:f269e3021894 441 else {
elessair 0:f269e3021894 442 msg->type = CANData;
elessair 0:f269e3021894 443 }
elessair 0:f269e3021894 444
elessair 0:f269e3021894 445 msg->len = (LPC_C_CAN0->CANIF2_MCTRL & 0xF); // TODO: If > 8, len = 8
elessair 0:f269e3021894 446 msg->data[0] = ((LPC_C_CAN0->CANIF2_DA1 >> 0) & 0xFF);
elessair 0:f269e3021894 447 msg->data[1] = ((LPC_C_CAN0->CANIF2_DA1 >> 8) & 0xFF);
elessair 0:f269e3021894 448 msg->data[2] = ((LPC_C_CAN0->CANIF2_DA2 >> 0) & 0xFF);
elessair 0:f269e3021894 449 msg->data[3] = ((LPC_C_CAN0->CANIF2_DA2 >> 8) & 0xFF);
elessair 0:f269e3021894 450 msg->data[4] = ((LPC_C_CAN0->CANIF2_DB1 >> 0) & 0xFF);
elessair 0:f269e3021894 451 msg->data[5] = ((LPC_C_CAN0->CANIF2_DB1 >> 8) & 0xFF);
elessair 0:f269e3021894 452 msg->data[6] = ((LPC_C_CAN0->CANIF2_DB2 >> 0) & 0xFF);
elessair 0:f269e3021894 453 msg->data[7] = ((LPC_C_CAN0->CANIF2_DB2 >> 8) & 0xFF);
elessair 0:f269e3021894 454
elessair 0:f269e3021894 455 LPC_C_CAN0->CANSTAT &= ~(1UL << 4);
elessair 0:f269e3021894 456 return 1;
elessair 0:f269e3021894 457 }
elessair 0:f269e3021894 458 return 0;
elessair 0:f269e3021894 459 }
elessair 0:f269e3021894 460
elessair 0:f269e3021894 461 void can_reset(can_t *obj) {
elessair 0:f269e3021894 462 LPC_SYSCON->PRESETCTRL1 &= ~(1UL << 7);
elessair 0:f269e3021894 463 LPC_C_CAN0->CANSTAT = 0;
elessair 0:f269e3021894 464 can_config_rxmsgobj(obj);
elessair 0:f269e3021894 465 }
elessair 0:f269e3021894 466
elessair 0:f269e3021894 467 unsigned char can_rderror(can_t *obj) {
elessair 0:f269e3021894 468 return ((LPC_C_CAN0->CANEC >> 8) & 0x7F);
elessair 0:f269e3021894 469 }
elessair 0:f269e3021894 470
elessair 0:f269e3021894 471 unsigned char can_tderror(can_t *obj) {
elessair 0:f269e3021894 472 return (LPC_C_CAN0->CANEC & 0xFF);
elessair 0:f269e3021894 473 }
elessair 0:f269e3021894 474
elessair 0:f269e3021894 475 void can_monitor(can_t *obj, int silent) {
elessair 0:f269e3021894 476 if (silent) {
elessair 0:f269e3021894 477 LPC_C_CAN0->CANCNTL |= (1UL << 7);
elessair 0:f269e3021894 478 LPC_C_CAN0->CANTEST |= (1UL << 3);
elessair 0:f269e3021894 479 } else {
elessair 0:f269e3021894 480 LPC_C_CAN0->CANCNTL &= ~(1UL << 7);
elessair 0:f269e3021894 481 LPC_C_CAN0->CANTEST &= ~(1UL << 3);
elessair 0:f269e3021894 482 }
elessair 0:f269e3021894 483
elessair 0:f269e3021894 484 if (!(LPC_C_CAN0->CANCNTL & (1UL << 0))) {
elessair 0:f269e3021894 485 LPC_C_CAN0->CANCNTL |= (1UL << 0);
elessair 0:f269e3021894 486 }
elessair 0:f269e3021894 487 }