helpfor studient
Dependents: STM32_F103-C8T6basecanblink_led
Fork of mbed-dev by
targets/TARGET_NXP/TARGET_LPC176X/can_api.c@186:9c2029bfadbe, 2018-04-20 (annotated)
- Committer:
- Anna Bridge
- Date:
- Fri Apr 20 11:31:35 2018 +0100
- Revision:
- 186:9c2029bfadbe
- Parent:
- 167:e84263d55307
Update to latest version of mbed lib
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 144:ef7eb2e8f9f7 | 1 | /* mbed Microcontroller Library |
<> | 144:ef7eb2e8f9f7 | 2 | * Copyright (c) 2006-2013 ARM Limited |
<> | 144:ef7eb2e8f9f7 | 3 | * |
<> | 144:ef7eb2e8f9f7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 144:ef7eb2e8f9f7 | 5 | * you may not use this file except in compliance with the License. |
<> | 144:ef7eb2e8f9f7 | 6 | * You may obtain a copy of the License at |
<> | 144:ef7eb2e8f9f7 | 7 | * |
<> | 144:ef7eb2e8f9f7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 144:ef7eb2e8f9f7 | 9 | * |
<> | 144:ef7eb2e8f9f7 | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 144:ef7eb2e8f9f7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 144:ef7eb2e8f9f7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 144:ef7eb2e8f9f7 | 13 | * See the License for the specific language governing permissions and |
<> | 144:ef7eb2e8f9f7 | 14 | * limitations under the License. |
<> | 144:ef7eb2e8f9f7 | 15 | */ |
<> | 144:ef7eb2e8f9f7 | 16 | #include "mbed_assert.h" |
<> | 144:ef7eb2e8f9f7 | 17 | #include "can_api.h" |
<> | 144:ef7eb2e8f9f7 | 18 | |
<> | 144:ef7eb2e8f9f7 | 19 | #include "cmsis.h" |
<> | 144:ef7eb2e8f9f7 | 20 | #include "pinmap.h" |
<> | 144:ef7eb2e8f9f7 | 21 | |
<> | 144:ef7eb2e8f9f7 | 22 | #include <math.h> |
<> | 144:ef7eb2e8f9f7 | 23 | #include <string.h> |
<> | 144:ef7eb2e8f9f7 | 24 | |
<> | 144:ef7eb2e8f9f7 | 25 | #define CAN_NUM 2 |
<> | 144:ef7eb2e8f9f7 | 26 | |
<> | 144:ef7eb2e8f9f7 | 27 | /* Acceptance filter mode in AFMR register */ |
<> | 144:ef7eb2e8f9f7 | 28 | #define ACCF_OFF 0x01 |
<> | 144:ef7eb2e8f9f7 | 29 | #define ACCF_BYPASS 0x02 |
<> | 144:ef7eb2e8f9f7 | 30 | #define ACCF_ON 0x00 |
<> | 144:ef7eb2e8f9f7 | 31 | #define ACCF_FULLCAN 0x04 |
<> | 144:ef7eb2e8f9f7 | 32 | |
<> | 144:ef7eb2e8f9f7 | 33 | /* There are several bit timing calculators on the internet. |
<> | 144:ef7eb2e8f9f7 | 34 | http://www.port.de/engl/canprod/sv_req_form.html |
<> | 144:ef7eb2e8f9f7 | 35 | http://www.kvaser.com/can/index.htm |
<> | 144:ef7eb2e8f9f7 | 36 | */ |
<> | 144:ef7eb2e8f9f7 | 37 | |
<> | 144:ef7eb2e8f9f7 | 38 | static const PinMap PinMap_CAN_RD[] = { |
<> | 144:ef7eb2e8f9f7 | 39 | {P0_0 , CAN_1, 1}, |
<> | 144:ef7eb2e8f9f7 | 40 | {P0_4 , CAN_2, 2}, |
<> | 144:ef7eb2e8f9f7 | 41 | {P0_21, CAN_1, 3}, |
<> | 144:ef7eb2e8f9f7 | 42 | {P2_7 , CAN_2, 1}, |
<> | 144:ef7eb2e8f9f7 | 43 | {NC , NC , 0} |
<> | 144:ef7eb2e8f9f7 | 44 | }; |
<> | 144:ef7eb2e8f9f7 | 45 | |
<> | 144:ef7eb2e8f9f7 | 46 | static const PinMap PinMap_CAN_TD[] = { |
<> | 144:ef7eb2e8f9f7 | 47 | {P0_1 , CAN_1, 1}, |
<> | 144:ef7eb2e8f9f7 | 48 | {P0_5 , CAN_2, 2}, |
<> | 144:ef7eb2e8f9f7 | 49 | {P0_22, CAN_1, 3}, |
<> | 144:ef7eb2e8f9f7 | 50 | {P2_8 , CAN_2, 1}, |
<> | 144:ef7eb2e8f9f7 | 51 | {NC , NC , 0} |
<> | 144:ef7eb2e8f9f7 | 52 | }; |
<> | 144:ef7eb2e8f9f7 | 53 | |
<> | 144:ef7eb2e8f9f7 | 54 | // Type definition to hold a CAN message |
<> | 144:ef7eb2e8f9f7 | 55 | struct CANMsg { |
<> | 144:ef7eb2e8f9f7 | 56 | unsigned int reserved1 : 16; |
<> | 144:ef7eb2e8f9f7 | 57 | unsigned int dlc : 4; // Bits 16..19: DLC - Data Length Counter |
<> | 144:ef7eb2e8f9f7 | 58 | unsigned int reserved0 : 10; |
<> | 144:ef7eb2e8f9f7 | 59 | unsigned int rtr : 1; // Bit 30: Set if this is a RTR message |
<> | 144:ef7eb2e8f9f7 | 60 | unsigned int type : 1; // Bit 31: Set if this is a 29-bit ID message |
<> | 144:ef7eb2e8f9f7 | 61 | unsigned int id; // CAN Message ID (11-bit or 29-bit) |
<> | 144:ef7eb2e8f9f7 | 62 | unsigned char data[8]; // CAN Message Data Bytes 0-7 |
<> | 144:ef7eb2e8f9f7 | 63 | }; |
<> | 144:ef7eb2e8f9f7 | 64 | typedef struct CANMsg CANMsg; |
<> | 144:ef7eb2e8f9f7 | 65 | |
<> | 144:ef7eb2e8f9f7 | 66 | static uint32_t can_irq_ids[CAN_NUM] = {0}; |
<> | 144:ef7eb2e8f9f7 | 67 | static can_irq_handler irq_handler; |
<> | 144:ef7eb2e8f9f7 | 68 | |
<> | 144:ef7eb2e8f9f7 | 69 | static uint32_t can_disable(can_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 70 | uint32_t sm = obj->dev->MOD; |
<> | 144:ef7eb2e8f9f7 | 71 | obj->dev->MOD |= 1; |
<> | 144:ef7eb2e8f9f7 | 72 | return sm; |
<> | 144:ef7eb2e8f9f7 | 73 | } |
<> | 144:ef7eb2e8f9f7 | 74 | |
<> | 144:ef7eb2e8f9f7 | 75 | static inline void can_enable(can_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 76 | if (obj->dev->MOD & 1) { |
<> | 144:ef7eb2e8f9f7 | 77 | obj->dev->MOD &= ~(1); |
<> | 144:ef7eb2e8f9f7 | 78 | } |
<> | 144:ef7eb2e8f9f7 | 79 | } |
<> | 144:ef7eb2e8f9f7 | 80 | |
<> | 144:ef7eb2e8f9f7 | 81 | int can_mode(can_t *obj, CanMode mode) { |
<> | 144:ef7eb2e8f9f7 | 82 | int success = 0; |
<> | 144:ef7eb2e8f9f7 | 83 | switch (mode) { |
<> | 144:ef7eb2e8f9f7 | 84 | case MODE_RESET: |
<> | 144:ef7eb2e8f9f7 | 85 | // Clear all special modes |
<> | 144:ef7eb2e8f9f7 | 86 | can_reset(obj); |
<> | 144:ef7eb2e8f9f7 | 87 | obj->dev->MOD &=~ 0x06; |
<> | 144:ef7eb2e8f9f7 | 88 | success = 1; |
<> | 144:ef7eb2e8f9f7 | 89 | break; |
<> | 144:ef7eb2e8f9f7 | 90 | case MODE_NORMAL: |
<> | 144:ef7eb2e8f9f7 | 91 | // Clear all special modes |
<> | 144:ef7eb2e8f9f7 | 92 | can_disable(obj); |
<> | 144:ef7eb2e8f9f7 | 93 | obj->dev->MOD &=~ 0x06; |
<> | 144:ef7eb2e8f9f7 | 94 | can_enable(obj); |
<> | 144:ef7eb2e8f9f7 | 95 | success = 1; |
<> | 144:ef7eb2e8f9f7 | 96 | break; |
<> | 144:ef7eb2e8f9f7 | 97 | case MODE_SILENT: |
<> | 144:ef7eb2e8f9f7 | 98 | // Set listen-only mode and clear self-test mode |
<> | 144:ef7eb2e8f9f7 | 99 | can_disable(obj); |
<> | 144:ef7eb2e8f9f7 | 100 | obj->dev->MOD |= 0x02; |
<> | 144:ef7eb2e8f9f7 | 101 | obj->dev->MOD &=~ 0x04; |
<> | 144:ef7eb2e8f9f7 | 102 | can_enable(obj); |
<> | 144:ef7eb2e8f9f7 | 103 | success = 1; |
<> | 144:ef7eb2e8f9f7 | 104 | break; |
<> | 144:ef7eb2e8f9f7 | 105 | case MODE_TEST_LOCAL: |
<> | 144:ef7eb2e8f9f7 | 106 | // Set self-test mode and clear listen-only mode |
<> | 144:ef7eb2e8f9f7 | 107 | can_disable(obj); |
<> | 144:ef7eb2e8f9f7 | 108 | obj->dev->MOD |= 0x04; |
<> | 144:ef7eb2e8f9f7 | 109 | obj->dev->MOD &=~ 0x02; |
<> | 144:ef7eb2e8f9f7 | 110 | can_enable(obj); |
<> | 144:ef7eb2e8f9f7 | 111 | success = 1; |
<> | 144:ef7eb2e8f9f7 | 112 | break; |
<> | 144:ef7eb2e8f9f7 | 113 | case MODE_TEST_SILENT: |
<> | 144:ef7eb2e8f9f7 | 114 | case MODE_TEST_GLOBAL: |
<> | 144:ef7eb2e8f9f7 | 115 | default: |
<> | 144:ef7eb2e8f9f7 | 116 | success = 0; |
<> | 144:ef7eb2e8f9f7 | 117 | break; |
<> | 144:ef7eb2e8f9f7 | 118 | } |
<> | 144:ef7eb2e8f9f7 | 119 | |
<> | 144:ef7eb2e8f9f7 | 120 | return success; |
<> | 144:ef7eb2e8f9f7 | 121 | } |
<> | 144:ef7eb2e8f9f7 | 122 | |
<> | 144:ef7eb2e8f9f7 | 123 | int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) { |
<> | 144:ef7eb2e8f9f7 | 124 | return 0; // not implemented |
<> | 144:ef7eb2e8f9f7 | 125 | } |
<> | 144:ef7eb2e8f9f7 | 126 | |
<> | 144:ef7eb2e8f9f7 | 127 | static inline void can_irq(uint32_t icr, uint32_t index) { |
<> | 144:ef7eb2e8f9f7 | 128 | uint32_t i; |
<> | 144:ef7eb2e8f9f7 | 129 | |
<> | 144:ef7eb2e8f9f7 | 130 | for(i = 0; i < 8; i++) |
<> | 144:ef7eb2e8f9f7 | 131 | { |
<> | 144:ef7eb2e8f9f7 | 132 | if((can_irq_ids[index] != 0) && (icr & (1 << i))) |
<> | 144:ef7eb2e8f9f7 | 133 | { |
<> | 144:ef7eb2e8f9f7 | 134 | switch (i) { |
<> | 144:ef7eb2e8f9f7 | 135 | case 0: irq_handler(can_irq_ids[index], IRQ_RX); break; |
<> | 144:ef7eb2e8f9f7 | 136 | case 1: irq_handler(can_irq_ids[index], IRQ_TX); break; |
<> | 144:ef7eb2e8f9f7 | 137 | case 2: irq_handler(can_irq_ids[index], IRQ_ERROR); break; |
<> | 144:ef7eb2e8f9f7 | 138 | case 3: irq_handler(can_irq_ids[index], IRQ_OVERRUN); break; |
<> | 144:ef7eb2e8f9f7 | 139 | case 4: irq_handler(can_irq_ids[index], IRQ_WAKEUP); break; |
<> | 144:ef7eb2e8f9f7 | 140 | case 5: irq_handler(can_irq_ids[index], IRQ_PASSIVE); break; |
<> | 144:ef7eb2e8f9f7 | 141 | case 6: irq_handler(can_irq_ids[index], IRQ_ARB); break; |
<> | 144:ef7eb2e8f9f7 | 142 | case 7: irq_handler(can_irq_ids[index], IRQ_BUS); break; |
<> | 144:ef7eb2e8f9f7 | 143 | case 8: irq_handler(can_irq_ids[index], IRQ_READY); break; |
<> | 144:ef7eb2e8f9f7 | 144 | } |
<> | 144:ef7eb2e8f9f7 | 145 | } |
<> | 144:ef7eb2e8f9f7 | 146 | } |
<> | 144:ef7eb2e8f9f7 | 147 | } |
<> | 144:ef7eb2e8f9f7 | 148 | |
<> | 144:ef7eb2e8f9f7 | 149 | // Have to check that the CAN block is active before reading the Interrupt |
<> | 144:ef7eb2e8f9f7 | 150 | // Control Register, or the mbed hangs |
<> | 144:ef7eb2e8f9f7 | 151 | void can_irq_n() { |
<> | 144:ef7eb2e8f9f7 | 152 | uint32_t icr; |
<> | 144:ef7eb2e8f9f7 | 153 | |
<> | 144:ef7eb2e8f9f7 | 154 | if(LPC_SC->PCONP & (1 << 13)) { |
<> | 144:ef7eb2e8f9f7 | 155 | icr = LPC_CAN1->ICR & 0x1FF; |
<> | 144:ef7eb2e8f9f7 | 156 | can_irq(icr, 0); |
<> | 144:ef7eb2e8f9f7 | 157 | } |
<> | 144:ef7eb2e8f9f7 | 158 | |
<> | 144:ef7eb2e8f9f7 | 159 | if(LPC_SC->PCONP & (1 << 14)) { |
<> | 144:ef7eb2e8f9f7 | 160 | icr = LPC_CAN2->ICR & 0x1FF; |
<> | 144:ef7eb2e8f9f7 | 161 | can_irq(icr, 1); |
<> | 144:ef7eb2e8f9f7 | 162 | } |
<> | 144:ef7eb2e8f9f7 | 163 | } |
<> | 144:ef7eb2e8f9f7 | 164 | |
<> | 144:ef7eb2e8f9f7 | 165 | // Register CAN object's irq handler |
<> | 144:ef7eb2e8f9f7 | 166 | void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id) { |
<> | 144:ef7eb2e8f9f7 | 167 | irq_handler = handler; |
<> | 144:ef7eb2e8f9f7 | 168 | can_irq_ids[obj->index] = id; |
<> | 144:ef7eb2e8f9f7 | 169 | } |
<> | 144:ef7eb2e8f9f7 | 170 | |
<> | 144:ef7eb2e8f9f7 | 171 | // Unregister CAN object's irq handler |
<> | 144:ef7eb2e8f9f7 | 172 | void can_irq_free(can_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 173 | obj->dev->IER &= ~(1); |
<> | 144:ef7eb2e8f9f7 | 174 | can_irq_ids[obj->index] = 0; |
<> | 144:ef7eb2e8f9f7 | 175 | |
<> | 144:ef7eb2e8f9f7 | 176 | if ((can_irq_ids[0] == 0) && (can_irq_ids[1] == 0)) { |
<> | 144:ef7eb2e8f9f7 | 177 | NVIC_DisableIRQ(CAN_IRQn); |
<> | 144:ef7eb2e8f9f7 | 178 | } |
<> | 144:ef7eb2e8f9f7 | 179 | } |
<> | 144:ef7eb2e8f9f7 | 180 | |
<> | 144:ef7eb2e8f9f7 | 181 | // Clear or set a irq |
<> | 144:ef7eb2e8f9f7 | 182 | void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable) { |
<> | 144:ef7eb2e8f9f7 | 183 | uint32_t ier; |
<> | 144:ef7eb2e8f9f7 | 184 | |
<> | 144:ef7eb2e8f9f7 | 185 | switch (type) { |
<> | 144:ef7eb2e8f9f7 | 186 | case IRQ_RX: ier = (1 << 0); break; |
<> | 144:ef7eb2e8f9f7 | 187 | case IRQ_TX: ier = (1 << 1); break; |
<> | 144:ef7eb2e8f9f7 | 188 | case IRQ_ERROR: ier = (1 << 2); break; |
<> | 144:ef7eb2e8f9f7 | 189 | case IRQ_OVERRUN: ier = (1 << 3); break; |
<> | 144:ef7eb2e8f9f7 | 190 | case IRQ_WAKEUP: ier = (1 << 4); break; |
<> | 144:ef7eb2e8f9f7 | 191 | case IRQ_PASSIVE: ier = (1 << 5); break; |
<> | 144:ef7eb2e8f9f7 | 192 | case IRQ_ARB: ier = (1 << 6); break; |
<> | 144:ef7eb2e8f9f7 | 193 | case IRQ_BUS: ier = (1 << 7); break; |
<> | 144:ef7eb2e8f9f7 | 194 | case IRQ_READY: ier = (1 << 8); break; |
<> | 144:ef7eb2e8f9f7 | 195 | default: return; |
<> | 144:ef7eb2e8f9f7 | 196 | } |
<> | 144:ef7eb2e8f9f7 | 197 | |
<> | 144:ef7eb2e8f9f7 | 198 | obj->dev->MOD |= 1; |
<> | 144:ef7eb2e8f9f7 | 199 | if(enable == 0) { |
<> | 144:ef7eb2e8f9f7 | 200 | obj->dev->IER &= ~ier; |
<> | 144:ef7eb2e8f9f7 | 201 | } |
<> | 144:ef7eb2e8f9f7 | 202 | else { |
<> | 144:ef7eb2e8f9f7 | 203 | obj->dev->IER |= ier; |
<> | 144:ef7eb2e8f9f7 | 204 | } |
<> | 144:ef7eb2e8f9f7 | 205 | obj->dev->MOD &= ~(1); |
<> | 144:ef7eb2e8f9f7 | 206 | |
<> | 144:ef7eb2e8f9f7 | 207 | // Enable NVIC if at least 1 interrupt is active |
<> | 144:ef7eb2e8f9f7 | 208 | if(((LPC_SC->PCONP & (1 << 13)) && LPC_CAN1->IER) || ((LPC_SC->PCONP & (1 << 14)) && LPC_CAN2->IER)) { |
<> | 144:ef7eb2e8f9f7 | 209 | NVIC_SetVector(CAN_IRQn, (uint32_t) &can_irq_n); |
<> | 144:ef7eb2e8f9f7 | 210 | NVIC_EnableIRQ(CAN_IRQn); |
<> | 144:ef7eb2e8f9f7 | 211 | } |
<> | 144:ef7eb2e8f9f7 | 212 | else { |
<> | 144:ef7eb2e8f9f7 | 213 | NVIC_DisableIRQ(CAN_IRQn); |
<> | 144:ef7eb2e8f9f7 | 214 | } |
<> | 144:ef7eb2e8f9f7 | 215 | } |
<> | 144:ef7eb2e8f9f7 | 216 | |
<> | 144:ef7eb2e8f9f7 | 217 | static int can_pclk(can_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 218 | int value = 0; |
<> | 144:ef7eb2e8f9f7 | 219 | switch ((int)obj->dev) { |
<> | 144:ef7eb2e8f9f7 | 220 | case CAN_1: value = (LPC_SC->PCLKSEL0 & (0x3 << 26)) >> 26; break; |
<> | 144:ef7eb2e8f9f7 | 221 | case CAN_2: value = (LPC_SC->PCLKSEL0 & (0x3 << 28)) >> 28; break; |
<> | 144:ef7eb2e8f9f7 | 222 | } |
<> | 144:ef7eb2e8f9f7 | 223 | |
<> | 144:ef7eb2e8f9f7 | 224 | switch (value) { |
<> | 144:ef7eb2e8f9f7 | 225 | case 1: return 1; |
<> | 144:ef7eb2e8f9f7 | 226 | case 2: return 2; |
<> | 144:ef7eb2e8f9f7 | 227 | case 3: return 6; |
<> | 144:ef7eb2e8f9f7 | 228 | default: return 4; |
<> | 144:ef7eb2e8f9f7 | 229 | } |
<> | 144:ef7eb2e8f9f7 | 230 | } |
<> | 144:ef7eb2e8f9f7 | 231 | |
<> | 144:ef7eb2e8f9f7 | 232 | // This table has the sampling points as close to 75% as possible. The first |
<> | 144:ef7eb2e8f9f7 | 233 | // value is TSEG1, the second TSEG2. |
<> | 144:ef7eb2e8f9f7 | 234 | static const int timing_pts[23][2] = { |
<> | 144:ef7eb2e8f9f7 | 235 | {0x0, 0x0}, // 2, 50% |
<> | 144:ef7eb2e8f9f7 | 236 | {0x1, 0x0}, // 3, 67% |
<> | 144:ef7eb2e8f9f7 | 237 | {0x2, 0x0}, // 4, 75% |
<> | 144:ef7eb2e8f9f7 | 238 | {0x3, 0x0}, // 5, 80% |
<> | 144:ef7eb2e8f9f7 | 239 | {0x3, 0x1}, // 6, 67% |
<> | 144:ef7eb2e8f9f7 | 240 | {0x4, 0x1}, // 7, 71% |
<> | 144:ef7eb2e8f9f7 | 241 | {0x5, 0x1}, // 8, 75% |
<> | 144:ef7eb2e8f9f7 | 242 | {0x6, 0x1}, // 9, 78% |
<> | 144:ef7eb2e8f9f7 | 243 | {0x6, 0x2}, // 10, 70% |
<> | 144:ef7eb2e8f9f7 | 244 | {0x7, 0x2}, // 11, 73% |
<> | 144:ef7eb2e8f9f7 | 245 | {0x8, 0x2}, // 12, 75% |
<> | 144:ef7eb2e8f9f7 | 246 | {0x9, 0x2}, // 13, 77% |
<> | 144:ef7eb2e8f9f7 | 247 | {0x9, 0x3}, // 14, 71% |
<> | 144:ef7eb2e8f9f7 | 248 | {0xA, 0x3}, // 15, 73% |
<> | 144:ef7eb2e8f9f7 | 249 | {0xB, 0x3}, // 16, 75% |
<> | 144:ef7eb2e8f9f7 | 250 | {0xC, 0x3}, // 17, 76% |
<> | 144:ef7eb2e8f9f7 | 251 | {0xD, 0x3}, // 18, 78% |
<> | 144:ef7eb2e8f9f7 | 252 | {0xD, 0x4}, // 19, 74% |
<> | 144:ef7eb2e8f9f7 | 253 | {0xE, 0x4}, // 20, 75% |
<> | 144:ef7eb2e8f9f7 | 254 | {0xF, 0x4}, // 21, 76% |
<> | 144:ef7eb2e8f9f7 | 255 | {0xF, 0x5}, // 22, 73% |
<> | 144:ef7eb2e8f9f7 | 256 | {0xF, 0x6}, // 23, 70% |
<> | 144:ef7eb2e8f9f7 | 257 | {0xF, 0x7}, // 24, 67% |
<> | 144:ef7eb2e8f9f7 | 258 | }; |
<> | 144:ef7eb2e8f9f7 | 259 | |
<> | 144:ef7eb2e8f9f7 | 260 | static unsigned int can_speed(unsigned int sclk, unsigned int pclk, unsigned int cclk, unsigned char psjw) { |
<> | 144:ef7eb2e8f9f7 | 261 | uint32_t btr; |
<> | 144:ef7eb2e8f9f7 | 262 | uint16_t brp = 0; |
<> | 144:ef7eb2e8f9f7 | 263 | uint32_t calcbit; |
<> | 144:ef7eb2e8f9f7 | 264 | uint32_t bitwidth; |
<> | 144:ef7eb2e8f9f7 | 265 | int hit = 0; |
<> | 144:ef7eb2e8f9f7 | 266 | int bits; |
<> | 144:ef7eb2e8f9f7 | 267 | |
<> | 144:ef7eb2e8f9f7 | 268 | bitwidth = sclk / (pclk * cclk); |
<> | 144:ef7eb2e8f9f7 | 269 | |
<> | 144:ef7eb2e8f9f7 | 270 | brp = bitwidth / 0x18; |
<> | 144:ef7eb2e8f9f7 | 271 | while ((!hit) && (brp < bitwidth / 4)) { |
<> | 144:ef7eb2e8f9f7 | 272 | brp++; |
<> | 144:ef7eb2e8f9f7 | 273 | for (bits = 22; bits > 0; bits--) { |
<> | 144:ef7eb2e8f9f7 | 274 | calcbit = (bits + 3) * (brp + 1); |
<> | 144:ef7eb2e8f9f7 | 275 | if (calcbit == bitwidth) { |
<> | 144:ef7eb2e8f9f7 | 276 | hit = 1; |
<> | 144:ef7eb2e8f9f7 | 277 | break; |
<> | 144:ef7eb2e8f9f7 | 278 | } |
<> | 144:ef7eb2e8f9f7 | 279 | } |
<> | 144:ef7eb2e8f9f7 | 280 | } |
<> | 144:ef7eb2e8f9f7 | 281 | |
<> | 144:ef7eb2e8f9f7 | 282 | if (hit) { |
<> | 144:ef7eb2e8f9f7 | 283 | btr = ((timing_pts[bits][1] << 20) & 0x00700000) |
<> | 144:ef7eb2e8f9f7 | 284 | | ((timing_pts[bits][0] << 16) & 0x000F0000) |
<> | 144:ef7eb2e8f9f7 | 285 | | ((psjw << 14) & 0x0000C000) |
<> | 144:ef7eb2e8f9f7 | 286 | | ((brp << 0) & 0x000003FF); |
<> | 144:ef7eb2e8f9f7 | 287 | } else { |
<> | 144:ef7eb2e8f9f7 | 288 | btr = 0xFFFFFFFF; |
<> | 144:ef7eb2e8f9f7 | 289 | } |
<> | 144:ef7eb2e8f9f7 | 290 | |
<> | 144:ef7eb2e8f9f7 | 291 | return btr; |
<> | 144:ef7eb2e8f9f7 | 292 | |
<> | 144:ef7eb2e8f9f7 | 293 | } |
<> | 144:ef7eb2e8f9f7 | 294 | |
AnnaBridge | 167:e84263d55307 | 295 | void can_init_freq(can_t *obj, PinName rd, PinName td, int hz) { |
<> | 144:ef7eb2e8f9f7 | 296 | CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD); |
<> | 144:ef7eb2e8f9f7 | 297 | CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); |
<> | 144:ef7eb2e8f9f7 | 298 | obj->dev = (LPC_CAN_TypeDef *)pinmap_merge(can_rd, can_td); |
<> | 144:ef7eb2e8f9f7 | 299 | MBED_ASSERT((int)obj->dev != NC); |
<> | 144:ef7eb2e8f9f7 | 300 | |
<> | 144:ef7eb2e8f9f7 | 301 | switch ((int)obj->dev) { |
<> | 144:ef7eb2e8f9f7 | 302 | case CAN_1: LPC_SC->PCONP |= 1 << 13; break; |
<> | 144:ef7eb2e8f9f7 | 303 | case CAN_2: LPC_SC->PCONP |= 1 << 14; break; |
<> | 144:ef7eb2e8f9f7 | 304 | } |
<> | 144:ef7eb2e8f9f7 | 305 | |
<> | 144:ef7eb2e8f9f7 | 306 | pinmap_pinout(rd, PinMap_CAN_RD); |
<> | 144:ef7eb2e8f9f7 | 307 | pinmap_pinout(td, PinMap_CAN_TD); |
<> | 144:ef7eb2e8f9f7 | 308 | |
<> | 144:ef7eb2e8f9f7 | 309 | switch ((int)obj->dev) { |
<> | 144:ef7eb2e8f9f7 | 310 | case CAN_1: obj->index = 0; break; |
<> | 144:ef7eb2e8f9f7 | 311 | case CAN_2: obj->index = 1; break; |
<> | 144:ef7eb2e8f9f7 | 312 | } |
<> | 144:ef7eb2e8f9f7 | 313 | |
<> | 144:ef7eb2e8f9f7 | 314 | can_reset(obj); |
<> | 144:ef7eb2e8f9f7 | 315 | obj->dev->IER = 0; // Disable Interrupts |
AnnaBridge | 167:e84263d55307 | 316 | can_frequency(obj, hz); |
<> | 144:ef7eb2e8f9f7 | 317 | |
<> | 144:ef7eb2e8f9f7 | 318 | LPC_CANAF->AFMR = ACCF_BYPASS; // Bypass Filter |
<> | 144:ef7eb2e8f9f7 | 319 | } |
<> | 144:ef7eb2e8f9f7 | 320 | |
AnnaBridge | 167:e84263d55307 | 321 | void can_init(can_t *obj, PinName rd, PinName td) { |
AnnaBridge | 167:e84263d55307 | 322 | can_init_freq(obj, rd, td, 100000); |
AnnaBridge | 167:e84263d55307 | 323 | } |
AnnaBridge | 167:e84263d55307 | 324 | |
<> | 144:ef7eb2e8f9f7 | 325 | void can_free(can_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 326 | switch ((int)obj->dev) { |
<> | 144:ef7eb2e8f9f7 | 327 | case CAN_1: LPC_SC->PCONP &= ~(1 << 13); break; |
<> | 144:ef7eb2e8f9f7 | 328 | case CAN_2: LPC_SC->PCONP &= ~(1 << 14); break; |
<> | 144:ef7eb2e8f9f7 | 329 | } |
<> | 144:ef7eb2e8f9f7 | 330 | } |
<> | 144:ef7eb2e8f9f7 | 331 | |
<> | 144:ef7eb2e8f9f7 | 332 | int can_frequency(can_t *obj, int f) { |
<> | 144:ef7eb2e8f9f7 | 333 | int pclk = can_pclk(obj); |
<> | 144:ef7eb2e8f9f7 | 334 | |
<> | 144:ef7eb2e8f9f7 | 335 | int btr = can_speed(SystemCoreClock, pclk, (unsigned int)f, 1); |
<> | 144:ef7eb2e8f9f7 | 336 | |
<> | 144:ef7eb2e8f9f7 | 337 | if (btr > 0) { |
<> | 144:ef7eb2e8f9f7 | 338 | uint32_t modmask = can_disable(obj); |
<> | 144:ef7eb2e8f9f7 | 339 | obj->dev->BTR = btr; |
<> | 144:ef7eb2e8f9f7 | 340 | obj->dev->MOD = modmask; |
<> | 144:ef7eb2e8f9f7 | 341 | return 1; |
<> | 144:ef7eb2e8f9f7 | 342 | } else { |
<> | 144:ef7eb2e8f9f7 | 343 | return 0; |
<> | 144:ef7eb2e8f9f7 | 344 | } |
<> | 144:ef7eb2e8f9f7 | 345 | } |
<> | 144:ef7eb2e8f9f7 | 346 | |
<> | 144:ef7eb2e8f9f7 | 347 | int can_write(can_t *obj, CAN_Message msg, int cc) { |
<> | 144:ef7eb2e8f9f7 | 348 | unsigned int CANStatus; |
<> | 144:ef7eb2e8f9f7 | 349 | CANMsg m; |
<> | 144:ef7eb2e8f9f7 | 350 | |
<> | 144:ef7eb2e8f9f7 | 351 | can_enable(obj); |
<> | 144:ef7eb2e8f9f7 | 352 | |
<> | 144:ef7eb2e8f9f7 | 353 | m.id = msg.id ; |
<> | 144:ef7eb2e8f9f7 | 354 | m.dlc = msg.len & 0xF; |
<> | 144:ef7eb2e8f9f7 | 355 | m.rtr = msg.type; |
<> | 144:ef7eb2e8f9f7 | 356 | m.type = msg.format; |
<> | 144:ef7eb2e8f9f7 | 357 | memcpy(m.data, msg.data, msg.len); |
<> | 144:ef7eb2e8f9f7 | 358 | const unsigned int *buf = (const unsigned int *)&m; |
<> | 144:ef7eb2e8f9f7 | 359 | |
<> | 144:ef7eb2e8f9f7 | 360 | CANStatus = obj->dev->SR; |
<> | 144:ef7eb2e8f9f7 | 361 | |
<> | 144:ef7eb2e8f9f7 | 362 | // Send the message to ourself if in a test mode |
<> | 144:ef7eb2e8f9f7 | 363 | if (obj->dev->MOD & 0x04) { |
<> | 144:ef7eb2e8f9f7 | 364 | cc = 1; |
<> | 144:ef7eb2e8f9f7 | 365 | } |
<> | 144:ef7eb2e8f9f7 | 366 | |
<> | 144:ef7eb2e8f9f7 | 367 | if (CANStatus & 0x00000004) { |
<> | 144:ef7eb2e8f9f7 | 368 | obj->dev->TFI1 = buf[0] & 0xC00F0000; |
<> | 144:ef7eb2e8f9f7 | 369 | obj->dev->TID1 = buf[1]; |
<> | 144:ef7eb2e8f9f7 | 370 | obj->dev->TDA1 = buf[2]; |
<> | 144:ef7eb2e8f9f7 | 371 | obj->dev->TDB1 = buf[3]; |
<> | 144:ef7eb2e8f9f7 | 372 | if(cc) { |
<> | 144:ef7eb2e8f9f7 | 373 | obj->dev->CMR = 0x30; |
<> | 144:ef7eb2e8f9f7 | 374 | } else { |
<> | 144:ef7eb2e8f9f7 | 375 | obj->dev->CMR = 0x21; |
<> | 144:ef7eb2e8f9f7 | 376 | } |
<> | 144:ef7eb2e8f9f7 | 377 | return 1; |
<> | 144:ef7eb2e8f9f7 | 378 | |
<> | 144:ef7eb2e8f9f7 | 379 | } else if (CANStatus & 0x00000400) { |
<> | 144:ef7eb2e8f9f7 | 380 | obj->dev->TFI2 = buf[0] & 0xC00F0000; |
<> | 144:ef7eb2e8f9f7 | 381 | obj->dev->TID2 = buf[1]; |
<> | 144:ef7eb2e8f9f7 | 382 | obj->dev->TDA2 = buf[2]; |
<> | 144:ef7eb2e8f9f7 | 383 | obj->dev->TDB2 = buf[3]; |
<> | 144:ef7eb2e8f9f7 | 384 | if (cc) { |
<> | 144:ef7eb2e8f9f7 | 385 | obj->dev->CMR = 0x50; |
<> | 144:ef7eb2e8f9f7 | 386 | } else { |
<> | 144:ef7eb2e8f9f7 | 387 | obj->dev->CMR = 0x41; |
<> | 144:ef7eb2e8f9f7 | 388 | } |
<> | 144:ef7eb2e8f9f7 | 389 | return 1; |
<> | 144:ef7eb2e8f9f7 | 390 | |
<> | 144:ef7eb2e8f9f7 | 391 | } else if (CANStatus & 0x00040000) { |
<> | 144:ef7eb2e8f9f7 | 392 | obj->dev->TFI3 = buf[0] & 0xC00F0000; |
<> | 144:ef7eb2e8f9f7 | 393 | obj->dev->TID3 = buf[1]; |
<> | 144:ef7eb2e8f9f7 | 394 | obj->dev->TDA3 = buf[2]; |
<> | 144:ef7eb2e8f9f7 | 395 | obj->dev->TDB3 = buf[3]; |
<> | 144:ef7eb2e8f9f7 | 396 | if (cc) { |
<> | 144:ef7eb2e8f9f7 | 397 | obj->dev->CMR = 0x90; |
<> | 144:ef7eb2e8f9f7 | 398 | } else { |
<> | 144:ef7eb2e8f9f7 | 399 | obj->dev->CMR = 0x81; |
<> | 144:ef7eb2e8f9f7 | 400 | } |
<> | 144:ef7eb2e8f9f7 | 401 | return 1; |
<> | 144:ef7eb2e8f9f7 | 402 | } |
<> | 144:ef7eb2e8f9f7 | 403 | |
<> | 144:ef7eb2e8f9f7 | 404 | return 0; |
<> | 144:ef7eb2e8f9f7 | 405 | } |
<> | 144:ef7eb2e8f9f7 | 406 | |
<> | 144:ef7eb2e8f9f7 | 407 | int can_read(can_t *obj, CAN_Message *msg, int handle) { |
<> | 144:ef7eb2e8f9f7 | 408 | CANMsg x; |
<> | 144:ef7eb2e8f9f7 | 409 | unsigned int *i = (unsigned int *)&x; |
<> | 144:ef7eb2e8f9f7 | 410 | |
<> | 144:ef7eb2e8f9f7 | 411 | can_enable(obj); |
<> | 144:ef7eb2e8f9f7 | 412 | |
<> | 144:ef7eb2e8f9f7 | 413 | if (obj->dev->GSR & 0x1) { |
<> | 144:ef7eb2e8f9f7 | 414 | *i++ = obj->dev->RFS; // Frame |
<> | 144:ef7eb2e8f9f7 | 415 | *i++ = obj->dev->RID; // ID |
<> | 144:ef7eb2e8f9f7 | 416 | *i++ = obj->dev->RDA; // Data A |
<> | 144:ef7eb2e8f9f7 | 417 | *i++ = obj->dev->RDB; // Data B |
<> | 144:ef7eb2e8f9f7 | 418 | obj->dev->CMR = 0x04; // release receive buffer |
<> | 144:ef7eb2e8f9f7 | 419 | |
<> | 144:ef7eb2e8f9f7 | 420 | msg->id = x.id; |
<> | 144:ef7eb2e8f9f7 | 421 | msg->len = x.dlc; |
<> | 144:ef7eb2e8f9f7 | 422 | msg->format = (x.type)? CANExtended : CANStandard; |
<> | 144:ef7eb2e8f9f7 | 423 | msg->type = (x.rtr)? CANRemote: CANData; |
<> | 144:ef7eb2e8f9f7 | 424 | memcpy(msg->data,x.data,x.dlc); |
<> | 144:ef7eb2e8f9f7 | 425 | return 1; |
<> | 144:ef7eb2e8f9f7 | 426 | } |
<> | 144:ef7eb2e8f9f7 | 427 | |
<> | 144:ef7eb2e8f9f7 | 428 | return 0; |
<> | 144:ef7eb2e8f9f7 | 429 | } |
<> | 144:ef7eb2e8f9f7 | 430 | |
<> | 144:ef7eb2e8f9f7 | 431 | void can_reset(can_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 432 | can_disable(obj); |
<> | 144:ef7eb2e8f9f7 | 433 | obj->dev->GSR = 0; // Reset error counter when CAN1MOD is in reset |
<> | 144:ef7eb2e8f9f7 | 434 | } |
<> | 144:ef7eb2e8f9f7 | 435 | |
<> | 144:ef7eb2e8f9f7 | 436 | unsigned char can_rderror(can_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 437 | return (obj->dev->GSR >> 16) & 0xFF; |
<> | 144:ef7eb2e8f9f7 | 438 | } |
<> | 144:ef7eb2e8f9f7 | 439 | |
<> | 144:ef7eb2e8f9f7 | 440 | unsigned char can_tderror(can_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 441 | return (obj->dev->GSR >> 24) & 0xFF; |
<> | 144:ef7eb2e8f9f7 | 442 | } |
<> | 144:ef7eb2e8f9f7 | 443 | |
<> | 144:ef7eb2e8f9f7 | 444 | void can_monitor(can_t *obj, int silent) { |
<> | 144:ef7eb2e8f9f7 | 445 | uint32_t mod_mask = can_disable(obj); |
<> | 144:ef7eb2e8f9f7 | 446 | if (silent) { |
<> | 144:ef7eb2e8f9f7 | 447 | obj->dev->MOD |= (1 << 1); |
<> | 144:ef7eb2e8f9f7 | 448 | } else { |
<> | 144:ef7eb2e8f9f7 | 449 | obj->dev->MOD &= ~(1 << 1); |
<> | 144:ef7eb2e8f9f7 | 450 | } |
<> | 144:ef7eb2e8f9f7 | 451 | if (!(mod_mask & 1)) { |
<> | 144:ef7eb2e8f9f7 | 452 | can_enable(obj); |
<> | 144:ef7eb2e8f9f7 | 453 | } |
<> | 144:ef7eb2e8f9f7 | 454 | } |