test
Fork of mbed-dev by
targets/TARGET_STM/can_api.c@168:e84263d55307, 2017-06-21 (annotated)
- Committer:
- AnnaBridge
- Date:
- Wed Jun 21 17:46:44 2017 +0100
- Revision:
- 168:e84263d55307
- Parent:
- 160:d5399cc887bb
- Child:
- 171:19eb464bc2be
This updates the lib to the mbed lib v 145
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 160:d5399cc887bb | 1 | /* mbed Microcontroller Library |
<> | 160:d5399cc887bb | 2 | * Copyright (c) 2006-2017 ARM Limited |
<> | 160:d5399cc887bb | 3 | * |
<> | 160:d5399cc887bb | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 160:d5399cc887bb | 5 | * you may not use this file except in compliance with the License. |
<> | 160:d5399cc887bb | 6 | * You may obtain a copy of the License at |
<> | 160:d5399cc887bb | 7 | * |
<> | 160:d5399cc887bb | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 160:d5399cc887bb | 9 | * |
<> | 160:d5399cc887bb | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 160:d5399cc887bb | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 160:d5399cc887bb | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 160:d5399cc887bb | 13 | * See the License for the specific language governing permissions and |
<> | 160:d5399cc887bb | 14 | * limitations under the License. |
<> | 160:d5399cc887bb | 15 | */ |
<> | 160:d5399cc887bb | 16 | #include "can_api.h" |
<> | 160:d5399cc887bb | 17 | |
<> | 160:d5399cc887bb | 18 | #if DEVICE_CAN |
<> | 160:d5399cc887bb | 19 | |
<> | 160:d5399cc887bb | 20 | #include "cmsis.h" |
<> | 160:d5399cc887bb | 21 | #include "pinmap.h" |
<> | 160:d5399cc887bb | 22 | #include "PeripheralPins.h" |
<> | 160:d5399cc887bb | 23 | #include "mbed_error.h" |
<> | 160:d5399cc887bb | 24 | #include "can_device.h" // Specific to STM32 serie |
<> | 160:d5399cc887bb | 25 | #include <math.h> |
<> | 160:d5399cc887bb | 26 | #include <string.h> |
<> | 160:d5399cc887bb | 27 | |
<> | 160:d5399cc887bb | 28 | static CAN_HandleTypeDef CanHandle; |
<> | 160:d5399cc887bb | 29 | static uint32_t can_irq_ids[CAN_NUM] = {0}; |
<> | 160:d5399cc887bb | 30 | static can_irq_handler irq_handler; |
<> | 160:d5399cc887bb | 31 | |
<> | 160:d5399cc887bb | 32 | void can_init(can_t *obj, PinName rd, PinName td) |
<> | 160:d5399cc887bb | 33 | { |
AnnaBridge | 168:e84263d55307 | 34 | can_init_freq(obj, rd, td, 100000); |
AnnaBridge | 168:e84263d55307 | 35 | } |
AnnaBridge | 168:e84263d55307 | 36 | |
AnnaBridge | 168:e84263d55307 | 37 | void can_init_freq (can_t *obj, PinName rd, PinName td, int hz) |
AnnaBridge | 168:e84263d55307 | 38 | { |
<> | 160:d5399cc887bb | 39 | CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD); |
<> | 160:d5399cc887bb | 40 | CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); |
<> | 160:d5399cc887bb | 41 | |
<> | 160:d5399cc887bb | 42 | obj->can = (CANName)pinmap_merge(can_rd, can_td); |
<> | 160:d5399cc887bb | 43 | MBED_ASSERT((int)obj->can != NC); |
<> | 160:d5399cc887bb | 44 | |
<> | 160:d5399cc887bb | 45 | if (obj->can == CAN_1) { |
<> | 160:d5399cc887bb | 46 | __HAL_RCC_CAN1_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 47 | obj->index = 0; |
<> | 160:d5399cc887bb | 48 | } |
<> | 160:d5399cc887bb | 49 | #if defined(CAN2_BASE) && (CAN_NUM == 2) |
<> | 160:d5399cc887bb | 50 | else if (obj->can == CAN_2) { |
<> | 160:d5399cc887bb | 51 | __HAL_RCC_CAN1_CLK_ENABLE(); // needed to set filters |
<> | 160:d5399cc887bb | 52 | __HAL_RCC_CAN2_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 53 | obj->index = 1; |
<> | 160:d5399cc887bb | 54 | } |
<> | 160:d5399cc887bb | 55 | #endif |
<> | 160:d5399cc887bb | 56 | else { |
<> | 160:d5399cc887bb | 57 | return; |
<> | 160:d5399cc887bb | 58 | } |
<> | 160:d5399cc887bb | 59 | |
<> | 160:d5399cc887bb | 60 | // Configure the CAN pins |
<> | 160:d5399cc887bb | 61 | pinmap_pinout(rd, PinMap_CAN_RD); |
<> | 160:d5399cc887bb | 62 | pinmap_pinout(td, PinMap_CAN_TD); |
<> | 160:d5399cc887bb | 63 | if (rd != NC) { |
<> | 160:d5399cc887bb | 64 | pin_mode(rd, PullUp); |
<> | 160:d5399cc887bb | 65 | } |
<> | 160:d5399cc887bb | 66 | if (td != NC) { |
<> | 160:d5399cc887bb | 67 | pin_mode(td, PullUp); |
<> | 160:d5399cc887bb | 68 | } |
<> | 160:d5399cc887bb | 69 | |
<> | 160:d5399cc887bb | 70 | CanHandle.Instance = (CAN_TypeDef *)(obj->can); |
<> | 160:d5399cc887bb | 71 | |
<> | 160:d5399cc887bb | 72 | CanHandle.Init.TTCM = DISABLE; |
<> | 160:d5399cc887bb | 73 | CanHandle.Init.ABOM = DISABLE; |
<> | 160:d5399cc887bb | 74 | CanHandle.Init.AWUM = DISABLE; |
<> | 160:d5399cc887bb | 75 | CanHandle.Init.NART = DISABLE; |
<> | 160:d5399cc887bb | 76 | CanHandle.Init.RFLM = DISABLE; |
<> | 160:d5399cc887bb | 77 | CanHandle.Init.TXFP = DISABLE; |
<> | 160:d5399cc887bb | 78 | CanHandle.Init.Mode = CAN_MODE_NORMAL; |
<> | 160:d5399cc887bb | 79 | CanHandle.Init.SJW = CAN_SJW_1TQ; |
<> | 160:d5399cc887bb | 80 | CanHandle.Init.BS1 = CAN_BS1_6TQ; |
<> | 160:d5399cc887bb | 81 | CanHandle.Init.BS2 = CAN_BS2_8TQ; |
<> | 160:d5399cc887bb | 82 | CanHandle.Init.Prescaler = 2; |
<> | 160:d5399cc887bb | 83 | |
<> | 160:d5399cc887bb | 84 | if (HAL_CAN_Init(&CanHandle) != HAL_OK) { |
<> | 160:d5399cc887bb | 85 | error("Cannot initialize CAN"); |
<> | 160:d5399cc887bb | 86 | } |
<> | 160:d5399cc887bb | 87 | |
<> | 160:d5399cc887bb | 88 | // Set initial CAN frequency to 100 kb/s |
AnnaBridge | 168:e84263d55307 | 89 | if (can_frequency(obj, 100000) != 1) { |
AnnaBridge | 168:e84263d55307 | 90 | error("Can frequency could not be set\n"); |
AnnaBridge | 168:e84263d55307 | 91 | } |
<> | 160:d5399cc887bb | 92 | |
<> | 160:d5399cc887bb | 93 | uint32_t filter_number = (obj->can == CAN_1) ? 0 : 14; |
<> | 160:d5399cc887bb | 94 | can_filter(obj, 0, 0, CANStandard, filter_number); |
<> | 160:d5399cc887bb | 95 | } |
<> | 160:d5399cc887bb | 96 | |
<> | 160:d5399cc887bb | 97 | void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id) |
<> | 160:d5399cc887bb | 98 | { |
<> | 160:d5399cc887bb | 99 | irq_handler = handler; |
<> | 160:d5399cc887bb | 100 | can_irq_ids[obj->index] = id; |
<> | 160:d5399cc887bb | 101 | } |
<> | 160:d5399cc887bb | 102 | |
<> | 160:d5399cc887bb | 103 | void can_irq_free(can_t *obj) |
<> | 160:d5399cc887bb | 104 | { |
<> | 160:d5399cc887bb | 105 | CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); |
<> | 160:d5399cc887bb | 106 | |
<> | 160:d5399cc887bb | 107 | can->IER &= ~(CAN_IT_FMP0 | CAN_IT_FMP1 | CAN_IT_TME | \ |
<> | 160:d5399cc887bb | 108 | CAN_IT_ERR | CAN_IT_EPV | CAN_IT_BOF); |
AnnaBridge | 168:e84263d55307 | 109 | can_irq_ids[obj->index] = 0; |
<> | 160:d5399cc887bb | 110 | } |
<> | 160:d5399cc887bb | 111 | |
<> | 160:d5399cc887bb | 112 | void can_free(can_t *obj) |
<> | 160:d5399cc887bb | 113 | { |
<> | 160:d5399cc887bb | 114 | // Reset CAN and disable clock |
<> | 160:d5399cc887bb | 115 | if (obj->can == CAN_1) { |
<> | 160:d5399cc887bb | 116 | __HAL_RCC_CAN1_FORCE_RESET(); |
<> | 160:d5399cc887bb | 117 | __HAL_RCC_CAN1_RELEASE_RESET(); |
<> | 160:d5399cc887bb | 118 | __HAL_RCC_CAN1_CLK_DISABLE(); |
<> | 160:d5399cc887bb | 119 | } |
<> | 160:d5399cc887bb | 120 | #if defined(CAN2_BASE) && (CAN_NUM == 2) |
<> | 160:d5399cc887bb | 121 | if (obj->can == CAN_2) { |
<> | 160:d5399cc887bb | 122 | __HAL_RCC_CAN2_FORCE_RESET(); |
<> | 160:d5399cc887bb | 123 | __HAL_RCC_CAN2_RELEASE_RESET(); |
<> | 160:d5399cc887bb | 124 | __HAL_RCC_CAN2_CLK_DISABLE(); |
<> | 160:d5399cc887bb | 125 | } |
<> | 160:d5399cc887bb | 126 | #endif |
<> | 160:d5399cc887bb | 127 | } |
<> | 160:d5399cc887bb | 128 | |
<> | 160:d5399cc887bb | 129 | // The following table is used to program bit_timing. It is an adjustment of the sample |
<> | 160:d5399cc887bb | 130 | // point by synchronizing on the start-bit edge and resynchronizing on the following edges. |
<> | 160:d5399cc887bb | 131 | // This table has the sampling points as close to 75% as possible (most commonly used). |
<> | 160:d5399cc887bb | 132 | // The first value is TSEG1, the second TSEG2. |
<> | 160:d5399cc887bb | 133 | static const int timing_pts[23][2] = { |
<> | 160:d5399cc887bb | 134 | {0x0, 0x0}, // 2, 50% |
<> | 160:d5399cc887bb | 135 | {0x1, 0x0}, // 3, 67% |
<> | 160:d5399cc887bb | 136 | {0x2, 0x0}, // 4, 75% |
<> | 160:d5399cc887bb | 137 | {0x3, 0x0}, // 5, 80% |
<> | 160:d5399cc887bb | 138 | {0x3, 0x1}, // 6, 67% |
<> | 160:d5399cc887bb | 139 | {0x4, 0x1}, // 7, 71% |
<> | 160:d5399cc887bb | 140 | {0x5, 0x1}, // 8, 75% |
<> | 160:d5399cc887bb | 141 | {0x6, 0x1}, // 9, 78% |
<> | 160:d5399cc887bb | 142 | {0x6, 0x2}, // 10, 70% |
<> | 160:d5399cc887bb | 143 | {0x7, 0x2}, // 11, 73% |
<> | 160:d5399cc887bb | 144 | {0x8, 0x2}, // 12, 75% |
<> | 160:d5399cc887bb | 145 | {0x9, 0x2}, // 13, 77% |
<> | 160:d5399cc887bb | 146 | {0x9, 0x3}, // 14, 71% |
<> | 160:d5399cc887bb | 147 | {0xA, 0x3}, // 15, 73% |
<> | 160:d5399cc887bb | 148 | {0xB, 0x3}, // 16, 75% |
<> | 160:d5399cc887bb | 149 | {0xC, 0x3}, // 17, 76% |
<> | 160:d5399cc887bb | 150 | {0xD, 0x3}, // 18, 78% |
<> | 160:d5399cc887bb | 151 | {0xD, 0x4}, // 19, 74% |
<> | 160:d5399cc887bb | 152 | {0xE, 0x4}, // 20, 75% |
<> | 160:d5399cc887bb | 153 | {0xF, 0x4}, // 21, 76% |
<> | 160:d5399cc887bb | 154 | {0xF, 0x5}, // 22, 73% |
<> | 160:d5399cc887bb | 155 | {0xF, 0x6}, // 23, 70% |
<> | 160:d5399cc887bb | 156 | {0xF, 0x7}, // 24, 67% |
<> | 160:d5399cc887bb | 157 | }; |
<> | 160:d5399cc887bb | 158 | |
<> | 160:d5399cc887bb | 159 | static unsigned int can_speed(unsigned int pclk, unsigned int cclk, unsigned char psjw) |
<> | 160:d5399cc887bb | 160 | { |
<> | 160:d5399cc887bb | 161 | uint32_t btr; |
<> | 160:d5399cc887bb | 162 | uint16_t brp = 0; |
<> | 160:d5399cc887bb | 163 | uint32_t calcbit; |
<> | 160:d5399cc887bb | 164 | uint32_t bitwidth; |
<> | 160:d5399cc887bb | 165 | int hit = 0; |
<> | 160:d5399cc887bb | 166 | int bits; |
<> | 160:d5399cc887bb | 167 | |
<> | 160:d5399cc887bb | 168 | bitwidth = (pclk / cclk); |
<> | 160:d5399cc887bb | 169 | |
<> | 160:d5399cc887bb | 170 | brp = bitwidth / 0x18; |
<> | 160:d5399cc887bb | 171 | while ((!hit) && (brp < bitwidth / 4)) { |
<> | 160:d5399cc887bb | 172 | brp++; |
<> | 160:d5399cc887bb | 173 | for (bits = 22; bits > 0; bits--) { |
<> | 160:d5399cc887bb | 174 | calcbit = (bits + 3) * (brp + 1); |
<> | 160:d5399cc887bb | 175 | if (calcbit == bitwidth) { |
<> | 160:d5399cc887bb | 176 | hit = 1; |
<> | 160:d5399cc887bb | 177 | break; |
<> | 160:d5399cc887bb | 178 | } |
<> | 160:d5399cc887bb | 179 | } |
<> | 160:d5399cc887bb | 180 | } |
<> | 160:d5399cc887bb | 181 | |
<> | 160:d5399cc887bb | 182 | if (hit) { |
<> | 160:d5399cc887bb | 183 | btr = ((timing_pts[bits][1] << 20) & 0x00700000) |
<> | 160:d5399cc887bb | 184 | | ((timing_pts[bits][0] << 16) & 0x000F0000) |
<> | 160:d5399cc887bb | 185 | | ((psjw << 24) & 0x0000C000) |
<> | 160:d5399cc887bb | 186 | | ((brp << 0) & 0x000003FF); |
<> | 160:d5399cc887bb | 187 | } else { |
<> | 160:d5399cc887bb | 188 | btr = 0xFFFFFFFF; |
<> | 160:d5399cc887bb | 189 | } |
<> | 160:d5399cc887bb | 190 | |
<> | 160:d5399cc887bb | 191 | return btr; |
<> | 160:d5399cc887bb | 192 | |
<> | 160:d5399cc887bb | 193 | } |
<> | 160:d5399cc887bb | 194 | |
<> | 160:d5399cc887bb | 195 | int can_frequency(can_t *obj, int f) |
<> | 160:d5399cc887bb | 196 | { |
<> | 160:d5399cc887bb | 197 | int pclk = HAL_RCC_GetPCLK1Freq(); |
<> | 160:d5399cc887bb | 198 | int btr = can_speed(pclk, (unsigned int)f, 1); |
<> | 160:d5399cc887bb | 199 | CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); |
AnnaBridge | 168:e84263d55307 | 200 | uint32_t tickstart = 0; |
AnnaBridge | 168:e84263d55307 | 201 | int status = 1; |
<> | 160:d5399cc887bb | 202 | |
<> | 160:d5399cc887bb | 203 | if (btr > 0) { |
<> | 160:d5399cc887bb | 204 | can->MCR |= CAN_MCR_INRQ ; |
AnnaBridge | 168:e84263d55307 | 205 | /* Get tick */ |
AnnaBridge | 168:e84263d55307 | 206 | tickstart = HAL_GetTick(); |
<> | 160:d5399cc887bb | 207 | while ((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) { |
AnnaBridge | 168:e84263d55307 | 208 | if ((HAL_GetTick() - tickstart) > 2) { |
AnnaBridge | 168:e84263d55307 | 209 | status = 0; |
AnnaBridge | 168:e84263d55307 | 210 | break; |
AnnaBridge | 168:e84263d55307 | 211 | } |
<> | 160:d5399cc887bb | 212 | } |
AnnaBridge | 168:e84263d55307 | 213 | if (status != 0) { |
AnnaBridge | 168:e84263d55307 | 214 | can->BTR = btr; |
AnnaBridge | 168:e84263d55307 | 215 | can->MCR &= ~(uint32_t)CAN_MCR_INRQ; |
AnnaBridge | 168:e84263d55307 | 216 | /* Get tick */ |
AnnaBridge | 168:e84263d55307 | 217 | tickstart = HAL_GetTick(); |
AnnaBridge | 168:e84263d55307 | 218 | while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) { |
AnnaBridge | 168:e84263d55307 | 219 | if ((HAL_GetTick() - tickstart) > 2) { |
AnnaBridge | 168:e84263d55307 | 220 | status = 0; |
AnnaBridge | 168:e84263d55307 | 221 | break; |
AnnaBridge | 168:e84263d55307 | 222 | } |
AnnaBridge | 168:e84263d55307 | 223 | } |
AnnaBridge | 168:e84263d55307 | 224 | if (status == 0) { |
AnnaBridge | 168:e84263d55307 | 225 | error("can ESR 0x%04x.%04x + timeout status %d", (can->ESR & 0xFFFF0000) >> 16, (can->ESR & 0xFFFF), status); |
AnnaBridge | 168:e84263d55307 | 226 | } |
AnnaBridge | 168:e84263d55307 | 227 | } else { |
AnnaBridge | 168:e84263d55307 | 228 | error("can init request timeout\n"); |
<> | 160:d5399cc887bb | 229 | } |
<> | 160:d5399cc887bb | 230 | } else { |
AnnaBridge | 168:e84263d55307 | 231 | status = 0; |
<> | 160:d5399cc887bb | 232 | } |
AnnaBridge | 168:e84263d55307 | 233 | return status; |
<> | 160:d5399cc887bb | 234 | } |
<> | 160:d5399cc887bb | 235 | |
<> | 160:d5399cc887bb | 236 | int can_write(can_t *obj, CAN_Message msg, int cc) |
<> | 160:d5399cc887bb | 237 | { |
<> | 160:d5399cc887bb | 238 | uint32_t transmitmailbox = CAN_TXSTATUS_NOMAILBOX; |
<> | 160:d5399cc887bb | 239 | CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); |
<> | 160:d5399cc887bb | 240 | |
<> | 160:d5399cc887bb | 241 | /* Select one empty transmit mailbox */ |
<> | 160:d5399cc887bb | 242 | if ((can->TSR & CAN_TSR_TME0) == CAN_TSR_TME0) { |
<> | 160:d5399cc887bb | 243 | transmitmailbox = 0; |
<> | 160:d5399cc887bb | 244 | } else if ((can->TSR & CAN_TSR_TME1) == CAN_TSR_TME1) { |
<> | 160:d5399cc887bb | 245 | transmitmailbox = 1; |
<> | 160:d5399cc887bb | 246 | } else if ((can->TSR & CAN_TSR_TME2) == CAN_TSR_TME2) { |
<> | 160:d5399cc887bb | 247 | transmitmailbox = 2; |
<> | 160:d5399cc887bb | 248 | } else { |
<> | 160:d5399cc887bb | 249 | return 0; |
<> | 160:d5399cc887bb | 250 | } |
<> | 160:d5399cc887bb | 251 | |
<> | 160:d5399cc887bb | 252 | can->sTxMailBox[transmitmailbox].TIR &= CAN_TI0R_TXRQ; |
<> | 160:d5399cc887bb | 253 | if (!(msg.format)) { |
<> | 160:d5399cc887bb | 254 | can->sTxMailBox[transmitmailbox].TIR |= ((msg.id << 21) | msg.type); |
<> | 160:d5399cc887bb | 255 | } else { |
<> | 160:d5399cc887bb | 256 | can->sTxMailBox[transmitmailbox].TIR |= ((msg.id << 3) | CAN_ID_EXT | msg.type); |
<> | 160:d5399cc887bb | 257 | } |
AnnaBridge | 168:e84263d55307 | 258 | |
<> | 160:d5399cc887bb | 259 | /* Set up the DLC */ |
<> | 160:d5399cc887bb | 260 | can->sTxMailBox[transmitmailbox].TDTR &= (uint32_t)0xFFFFFFF0; |
<> | 160:d5399cc887bb | 261 | can->sTxMailBox[transmitmailbox].TDTR |= (msg.len & (uint8_t)0x0000000F); |
AnnaBridge | 168:e84263d55307 | 262 | |
<> | 160:d5399cc887bb | 263 | /* Set up the data field */ |
<> | 160:d5399cc887bb | 264 | can->sTxMailBox[transmitmailbox].TDLR = (((uint32_t)msg.data[3] << 24) | |
AnnaBridge | 168:e84263d55307 | 265 | ((uint32_t)msg.data[2] << 16) | |
AnnaBridge | 168:e84263d55307 | 266 | ((uint32_t)msg.data[1] << 8) | |
AnnaBridge | 168:e84263d55307 | 267 | ((uint32_t)msg.data[0])); |
<> | 160:d5399cc887bb | 268 | can->sTxMailBox[transmitmailbox].TDHR = (((uint32_t)msg.data[7] << 24) | |
AnnaBridge | 168:e84263d55307 | 269 | ((uint32_t)msg.data[6] << 16) | |
AnnaBridge | 168:e84263d55307 | 270 | ((uint32_t)msg.data[5] << 8) | |
<> | 160:d5399cc887bb | 271 | ((uint32_t)msg.data[4])); |
<> | 160:d5399cc887bb | 272 | /* Request transmission */ |
<> | 160:d5399cc887bb | 273 | can->sTxMailBox[transmitmailbox].TIR |= CAN_TI0R_TXRQ; |
<> | 160:d5399cc887bb | 274 | |
<> | 160:d5399cc887bb | 275 | return 1; |
<> | 160:d5399cc887bb | 276 | } |
<> | 160:d5399cc887bb | 277 | |
<> | 160:d5399cc887bb | 278 | int can_read(can_t *obj, CAN_Message *msg, int handle) |
<> | 160:d5399cc887bb | 279 | { |
<> | 160:d5399cc887bb | 280 | //handle is the FIFO number |
<> | 160:d5399cc887bb | 281 | |
<> | 160:d5399cc887bb | 282 | CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); |
<> | 160:d5399cc887bb | 283 | |
<> | 160:d5399cc887bb | 284 | // check FPM0 which holds the pending message count in FIFO 0 |
<> | 160:d5399cc887bb | 285 | // if no message is pending, return 0 |
<> | 160:d5399cc887bb | 286 | if ((can->RF0R & CAN_RF0R_FMP0) == 0) { |
<> | 160:d5399cc887bb | 287 | return 0; |
<> | 160:d5399cc887bb | 288 | } |
<> | 160:d5399cc887bb | 289 | |
<> | 160:d5399cc887bb | 290 | /* Get the Id */ |
<> | 160:d5399cc887bb | 291 | msg->format = (CANFormat)(((uint8_t)0x04 & can->sFIFOMailBox[handle].RIR) >> 2); |
<> | 160:d5399cc887bb | 292 | if (!msg->format) { |
<> | 160:d5399cc887bb | 293 | msg->id = (uint32_t)0x000007FF & (can->sFIFOMailBox[handle].RIR >> 21); |
<> | 160:d5399cc887bb | 294 | } else { |
<> | 160:d5399cc887bb | 295 | msg->id = (uint32_t)0x1FFFFFFF & (can->sFIFOMailBox[handle].RIR >> 3); |
<> | 160:d5399cc887bb | 296 | } |
<> | 160:d5399cc887bb | 297 | |
<> | 160:d5399cc887bb | 298 | msg->type = (CANType)(((uint8_t)0x02 & can->sFIFOMailBox[handle].RIR) >> 1); |
<> | 160:d5399cc887bb | 299 | /* Get the DLC */ |
<> | 160:d5399cc887bb | 300 | msg->len = (uint8_t)0x0F & can->sFIFOMailBox[handle].RDTR; |
<> | 160:d5399cc887bb | 301 | /* Get the FMI */ |
<> | 160:d5399cc887bb | 302 | // msg->FMI = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDTR >> 8); |
<> | 160:d5399cc887bb | 303 | /* Get the data field */ |
<> | 160:d5399cc887bb | 304 | msg->data[0] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDLR; |
<> | 160:d5399cc887bb | 305 | msg->data[1] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 8); |
<> | 160:d5399cc887bb | 306 | msg->data[2] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 16); |
<> | 160:d5399cc887bb | 307 | msg->data[3] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 24); |
<> | 160:d5399cc887bb | 308 | msg->data[4] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDHR; |
<> | 160:d5399cc887bb | 309 | msg->data[5] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 8); |
<> | 160:d5399cc887bb | 310 | msg->data[6] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 16); |
<> | 160:d5399cc887bb | 311 | msg->data[7] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 24); |
<> | 160:d5399cc887bb | 312 | |
<> | 160:d5399cc887bb | 313 | /* Release the FIFO */ |
<> | 160:d5399cc887bb | 314 | if (handle == CAN_FIFO0) { |
<> | 160:d5399cc887bb | 315 | /* Release FIFO0 */ |
<> | 160:d5399cc887bb | 316 | can->RF0R |= CAN_RF0R_RFOM0; |
<> | 160:d5399cc887bb | 317 | } else { /* FIFONumber == CAN_FIFO1 */ |
<> | 160:d5399cc887bb | 318 | /* Release FIFO1 */ |
<> | 160:d5399cc887bb | 319 | can->RF1R |= CAN_RF1R_RFOM1; |
<> | 160:d5399cc887bb | 320 | } |
<> | 160:d5399cc887bb | 321 | |
<> | 160:d5399cc887bb | 322 | return 1; |
<> | 160:d5399cc887bb | 323 | } |
<> | 160:d5399cc887bb | 324 | |
<> | 160:d5399cc887bb | 325 | void can_reset(can_t *obj) |
<> | 160:d5399cc887bb | 326 | { |
<> | 160:d5399cc887bb | 327 | CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); |
<> | 160:d5399cc887bb | 328 | can->MCR |= CAN_MCR_RESET; |
<> | 160:d5399cc887bb | 329 | can->ESR = 0x0; |
<> | 160:d5399cc887bb | 330 | } |
<> | 160:d5399cc887bb | 331 | |
<> | 160:d5399cc887bb | 332 | unsigned char can_rderror(can_t *obj) |
<> | 160:d5399cc887bb | 333 | { |
<> | 160:d5399cc887bb | 334 | CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); |
<> | 160:d5399cc887bb | 335 | return (can->ESR >> 24) & 0xFF; |
<> | 160:d5399cc887bb | 336 | } |
<> | 160:d5399cc887bb | 337 | |
<> | 160:d5399cc887bb | 338 | unsigned char can_tderror(can_t *obj) |
<> | 160:d5399cc887bb | 339 | { |
<> | 160:d5399cc887bb | 340 | CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); |
<> | 160:d5399cc887bb | 341 | return (can->ESR >> 16) & 0xFF; |
<> | 160:d5399cc887bb | 342 | } |
<> | 160:d5399cc887bb | 343 | |
<> | 160:d5399cc887bb | 344 | void can_monitor(can_t *obj, int silent) |
<> | 160:d5399cc887bb | 345 | { |
<> | 160:d5399cc887bb | 346 | CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); |
<> | 160:d5399cc887bb | 347 | |
<> | 160:d5399cc887bb | 348 | can->MCR |= CAN_MCR_INRQ ; |
<> | 160:d5399cc887bb | 349 | while ((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) { |
<> | 160:d5399cc887bb | 350 | } |
<> | 160:d5399cc887bb | 351 | |
<> | 160:d5399cc887bb | 352 | if (silent) { |
<> | 160:d5399cc887bb | 353 | can->BTR |= ((uint32_t)1 << 31); |
<> | 160:d5399cc887bb | 354 | } else { |
<> | 160:d5399cc887bb | 355 | can->BTR &= ~((uint32_t)1 << 31); |
<> | 160:d5399cc887bb | 356 | } |
<> | 160:d5399cc887bb | 357 | |
<> | 160:d5399cc887bb | 358 | can->MCR &= ~(uint32_t)CAN_MCR_INRQ; |
<> | 160:d5399cc887bb | 359 | while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) { |
<> | 160:d5399cc887bb | 360 | } |
<> | 160:d5399cc887bb | 361 | } |
<> | 160:d5399cc887bb | 362 | |
<> | 160:d5399cc887bb | 363 | int can_mode(can_t *obj, CanMode mode) |
<> | 160:d5399cc887bb | 364 | { |
<> | 160:d5399cc887bb | 365 | int success = 0; |
<> | 160:d5399cc887bb | 366 | CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); |
<> | 160:d5399cc887bb | 367 | |
<> | 160:d5399cc887bb | 368 | can->MCR |= CAN_MCR_INRQ ; |
<> | 160:d5399cc887bb | 369 | while ((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) { |
<> | 160:d5399cc887bb | 370 | } |
<> | 160:d5399cc887bb | 371 | |
<> | 160:d5399cc887bb | 372 | switch (mode) { |
<> | 160:d5399cc887bb | 373 | case MODE_NORMAL: |
<> | 160:d5399cc887bb | 374 | can->BTR &= ~(CAN_BTR_SILM | CAN_BTR_LBKM); |
<> | 160:d5399cc887bb | 375 | success = 1; |
<> | 160:d5399cc887bb | 376 | break; |
<> | 160:d5399cc887bb | 377 | case MODE_SILENT: |
<> | 160:d5399cc887bb | 378 | can->BTR |= CAN_BTR_SILM; |
<> | 160:d5399cc887bb | 379 | can->BTR &= ~CAN_BTR_LBKM; |
<> | 160:d5399cc887bb | 380 | success = 1; |
<> | 160:d5399cc887bb | 381 | break; |
<> | 160:d5399cc887bb | 382 | case MODE_TEST_GLOBAL: |
<> | 160:d5399cc887bb | 383 | case MODE_TEST_LOCAL: |
<> | 160:d5399cc887bb | 384 | can->BTR |= CAN_BTR_LBKM; |
<> | 160:d5399cc887bb | 385 | can->BTR &= ~CAN_BTR_SILM; |
<> | 160:d5399cc887bb | 386 | success = 1; |
<> | 160:d5399cc887bb | 387 | break; |
<> | 160:d5399cc887bb | 388 | case MODE_TEST_SILENT: |
<> | 160:d5399cc887bb | 389 | can->BTR |= (CAN_BTR_SILM | CAN_BTR_LBKM); |
<> | 160:d5399cc887bb | 390 | success = 1; |
<> | 160:d5399cc887bb | 391 | break; |
<> | 160:d5399cc887bb | 392 | default: |
<> | 160:d5399cc887bb | 393 | success = 0; |
<> | 160:d5399cc887bb | 394 | break; |
<> | 160:d5399cc887bb | 395 | } |
<> | 160:d5399cc887bb | 396 | |
<> | 160:d5399cc887bb | 397 | can->MCR &= ~(uint32_t)CAN_MCR_INRQ; |
<> | 160:d5399cc887bb | 398 | while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) { |
<> | 160:d5399cc887bb | 399 | } |
<> | 160:d5399cc887bb | 400 | |
<> | 160:d5399cc887bb | 401 | return success; |
<> | 160:d5399cc887bb | 402 | } |
<> | 160:d5399cc887bb | 403 | |
<> | 160:d5399cc887bb | 404 | int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) |
<> | 160:d5399cc887bb | 405 | { |
<> | 160:d5399cc887bb | 406 | int retval = 0; |
<> | 160:d5399cc887bb | 407 | |
<> | 160:d5399cc887bb | 408 | // filter for CANAny format cannot be configured for STM32 |
<> | 160:d5399cc887bb | 409 | if ((format == CANStandard) || (format == CANExtended)) { |
<> | 160:d5399cc887bb | 410 | CanHandle.Instance = (CAN_TypeDef *)(obj->can); |
<> | 160:d5399cc887bb | 411 | CAN_FilterConfTypeDef sFilterConfig; |
<> | 160:d5399cc887bb | 412 | sFilterConfig.FilterNumber = handle; |
<> | 160:d5399cc887bb | 413 | sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK; |
<> | 160:d5399cc887bb | 414 | sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT; |
<> | 160:d5399cc887bb | 415 | |
<> | 160:d5399cc887bb | 416 | if (format == CANStandard) { |
<> | 160:d5399cc887bb | 417 | sFilterConfig.FilterIdHigh = id << 5; |
<> | 160:d5399cc887bb | 418 | sFilterConfig.FilterIdLow = 0x0; |
<> | 160:d5399cc887bb | 419 | sFilterConfig.FilterMaskIdHigh = mask << 5; |
<> | 160:d5399cc887bb | 420 | sFilterConfig.FilterMaskIdLow = 0x0; // allows both remote and data frames |
<> | 160:d5399cc887bb | 421 | } else if (format == CANExtended) { |
<> | 160:d5399cc887bb | 422 | sFilterConfig.FilterIdHigh = id >> 13; // EXTID[28:13] |
<> | 160:d5399cc887bb | 423 | sFilterConfig.FilterIdLow = (0x00FF & (id << 3)) | (1 << 2); // EXTID[12:0] |
<> | 160:d5399cc887bb | 424 | sFilterConfig.FilterMaskIdHigh = mask >> 13; |
<> | 160:d5399cc887bb | 425 | sFilterConfig.FilterMaskIdLow = (0x00FF & (mask << 3)) | (1 << 2); |
<> | 160:d5399cc887bb | 426 | } |
<> | 160:d5399cc887bb | 427 | |
<> | 160:d5399cc887bb | 428 | sFilterConfig.FilterFIFOAssignment = 0; |
<> | 160:d5399cc887bb | 429 | sFilterConfig.FilterActivation = ENABLE; |
<> | 160:d5399cc887bb | 430 | sFilterConfig.BankNumber = 14 + handle; |
<> | 160:d5399cc887bb | 431 | |
<> | 160:d5399cc887bb | 432 | HAL_CAN_ConfigFilter(&CanHandle, &sFilterConfig); |
<> | 160:d5399cc887bb | 433 | retval = handle; |
<> | 160:d5399cc887bb | 434 | } |
<> | 160:d5399cc887bb | 435 | return retval; |
<> | 160:d5399cc887bb | 436 | } |
<> | 160:d5399cc887bb | 437 | |
<> | 160:d5399cc887bb | 438 | static void can_irq(CANName name, int id) |
<> | 160:d5399cc887bb | 439 | { |
<> | 160:d5399cc887bb | 440 | uint32_t tmp1 = 0, tmp2 = 0, tmp3 = 0; |
<> | 160:d5399cc887bb | 441 | CanHandle.Instance = (CAN_TypeDef *)name; |
<> | 160:d5399cc887bb | 442 | |
<> | 160:d5399cc887bb | 443 | if (__HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_TME)) { |
<> | 160:d5399cc887bb | 444 | tmp1 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_0); |
<> | 160:d5399cc887bb | 445 | tmp2 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_1); |
<> | 160:d5399cc887bb | 446 | tmp3 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_2); |
<> | 160:d5399cc887bb | 447 | if (tmp1) { |
<> | 160:d5399cc887bb | 448 | __HAL_CAN_CLEAR_FLAG(&CanHandle, CAN_FLAG_RQCP0); |
<> | 160:d5399cc887bb | 449 | } |
<> | 160:d5399cc887bb | 450 | if (tmp2) { |
<> | 160:d5399cc887bb | 451 | __HAL_CAN_CLEAR_FLAG(&CanHandle, CAN_FLAG_RQCP1); |
<> | 160:d5399cc887bb | 452 | } |
<> | 160:d5399cc887bb | 453 | if (tmp3) { |
<> | 160:d5399cc887bb | 454 | __HAL_CAN_CLEAR_FLAG(&CanHandle, CAN_FLAG_RQCP2); |
<> | 160:d5399cc887bb | 455 | } |
<> | 160:d5399cc887bb | 456 | if (tmp1 || tmp2 || tmp3) { |
<> | 160:d5399cc887bb | 457 | irq_handler(can_irq_ids[id], IRQ_TX); |
<> | 160:d5399cc887bb | 458 | } |
<> | 160:d5399cc887bb | 459 | } |
<> | 160:d5399cc887bb | 460 | |
<> | 160:d5399cc887bb | 461 | tmp1 = __HAL_CAN_MSG_PENDING(&CanHandle, CAN_FIFO0); |
<> | 160:d5399cc887bb | 462 | tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_FMP0); |
<> | 160:d5399cc887bb | 463 | |
<> | 160:d5399cc887bb | 464 | if ((tmp1 != 0) && tmp2) { |
<> | 160:d5399cc887bb | 465 | irq_handler(can_irq_ids[id], IRQ_RX); |
<> | 160:d5399cc887bb | 466 | } |
<> | 160:d5399cc887bb | 467 | |
<> | 160:d5399cc887bb | 468 | tmp1 = __HAL_CAN_GET_FLAG(&CanHandle, CAN_FLAG_EPV); |
<> | 160:d5399cc887bb | 469 | tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_EPV); |
<> | 160:d5399cc887bb | 470 | tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR); |
<> | 160:d5399cc887bb | 471 | |
<> | 160:d5399cc887bb | 472 | if (tmp1 && tmp2 && tmp3) { |
<> | 160:d5399cc887bb | 473 | irq_handler(can_irq_ids[id], IRQ_PASSIVE); |
<> | 160:d5399cc887bb | 474 | } |
<> | 160:d5399cc887bb | 475 | |
<> | 160:d5399cc887bb | 476 | tmp1 = __HAL_CAN_GET_FLAG(&CanHandle, CAN_FLAG_BOF); |
<> | 160:d5399cc887bb | 477 | tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_BOF); |
<> | 160:d5399cc887bb | 478 | tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR); |
<> | 160:d5399cc887bb | 479 | if (tmp1 && tmp2 && tmp3) { |
<> | 160:d5399cc887bb | 480 | irq_handler(can_irq_ids[id], IRQ_BUS); |
<> | 160:d5399cc887bb | 481 | } |
<> | 160:d5399cc887bb | 482 | |
<> | 160:d5399cc887bb | 483 | tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR); |
<> | 160:d5399cc887bb | 484 | if (tmp1 && tmp2 && tmp3) { |
<> | 160:d5399cc887bb | 485 | irq_handler(can_irq_ids[id], IRQ_ERROR); |
<> | 160:d5399cc887bb | 486 | } |
<> | 160:d5399cc887bb | 487 | } |
<> | 160:d5399cc887bb | 488 | |
<> | 160:d5399cc887bb | 489 | #if defined(TARGET_STM32F0) |
<> | 160:d5399cc887bb | 490 | void CAN_IRQHandler(void) |
<> | 160:d5399cc887bb | 491 | { |
<> | 160:d5399cc887bb | 492 | can_irq(CAN_1, 0); |
<> | 160:d5399cc887bb | 493 | } |
<> | 160:d5399cc887bb | 494 | #elif defined(TARGET_STM32F3) |
<> | 160:d5399cc887bb | 495 | void CAN_RX0_IRQHandler(void) |
<> | 160:d5399cc887bb | 496 | { |
<> | 160:d5399cc887bb | 497 | can_irq(CAN_1, 0); |
<> | 160:d5399cc887bb | 498 | } |
<> | 160:d5399cc887bb | 499 | void CAN_TX_IRQHandler(void) |
<> | 160:d5399cc887bb | 500 | { |
<> | 160:d5399cc887bb | 501 | can_irq(CAN_1, 0); |
<> | 160:d5399cc887bb | 502 | } |
<> | 160:d5399cc887bb | 503 | void CAN_SCE_IRQHandler(void) |
<> | 160:d5399cc887bb | 504 | { |
<> | 160:d5399cc887bb | 505 | can_irq(CAN_1, 0); |
<> | 160:d5399cc887bb | 506 | } |
<> | 160:d5399cc887bb | 507 | #else |
<> | 160:d5399cc887bb | 508 | void CAN1_RX0_IRQHandler(void) |
<> | 160:d5399cc887bb | 509 | { |
<> | 160:d5399cc887bb | 510 | can_irq(CAN_1, 0); |
<> | 160:d5399cc887bb | 511 | } |
<> | 160:d5399cc887bb | 512 | void CAN1_TX_IRQHandler(void) |
<> | 160:d5399cc887bb | 513 | { |
<> | 160:d5399cc887bb | 514 | can_irq(CAN_1, 0); |
<> | 160:d5399cc887bb | 515 | } |
<> | 160:d5399cc887bb | 516 | void CAN1_SCE_IRQHandler(void) |
<> | 160:d5399cc887bb | 517 | { |
<> | 160:d5399cc887bb | 518 | can_irq(CAN_1, 0); |
<> | 160:d5399cc887bb | 519 | } |
<> | 160:d5399cc887bb | 520 | #if defined(CAN2_BASE) && (CAN_NUM == 2) |
<> | 160:d5399cc887bb | 521 | void CAN2_RX0_IRQHandler(void) |
<> | 160:d5399cc887bb | 522 | { |
<> | 160:d5399cc887bb | 523 | can_irq(CAN_2, 1); |
<> | 160:d5399cc887bb | 524 | } |
<> | 160:d5399cc887bb | 525 | void CAN2_TX_IRQHandler(void) |
<> | 160:d5399cc887bb | 526 | { |
<> | 160:d5399cc887bb | 527 | can_irq(CAN_2, 1); |
<> | 160:d5399cc887bb | 528 | } |
<> | 160:d5399cc887bb | 529 | void CAN2_SCE_IRQHandler(void) |
<> | 160:d5399cc887bb | 530 | { |
<> | 160:d5399cc887bb | 531 | can_irq(CAN_2, 1); |
<> | 160:d5399cc887bb | 532 | } |
<> | 160:d5399cc887bb | 533 | #endif // defined(CAN2_BASE) && (CAN_NUM == 2) |
<> | 160:d5399cc887bb | 534 | #endif // else |
<> | 160:d5399cc887bb | 535 | |
<> | 160:d5399cc887bb | 536 | void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable) |
<> | 160:d5399cc887bb | 537 | { |
<> | 160:d5399cc887bb | 538 | |
<> | 160:d5399cc887bb | 539 | CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); |
<> | 160:d5399cc887bb | 540 | IRQn_Type irq_n = (IRQn_Type)0; |
<> | 160:d5399cc887bb | 541 | uint32_t vector = 0; |
<> | 160:d5399cc887bb | 542 | uint32_t ier; |
<> | 160:d5399cc887bb | 543 | |
<> | 160:d5399cc887bb | 544 | if (obj->can == CAN_1) { |
<> | 160:d5399cc887bb | 545 | switch (type) { |
<> | 160:d5399cc887bb | 546 | case IRQ_RX: |
<> | 160:d5399cc887bb | 547 | ier = CAN_IT_FMP0; |
<> | 160:d5399cc887bb | 548 | irq_n = CAN1_IRQ_RX_IRQN; |
<> | 160:d5399cc887bb | 549 | vector = (uint32_t)&CAN1_IRQ_RX_VECT; |
<> | 160:d5399cc887bb | 550 | break; |
<> | 160:d5399cc887bb | 551 | case IRQ_TX: |
<> | 160:d5399cc887bb | 552 | ier = CAN_IT_TME; |
<> | 160:d5399cc887bb | 553 | irq_n = CAN1_IRQ_TX_IRQN; |
<> | 160:d5399cc887bb | 554 | vector = (uint32_t)&CAN1_IRQ_TX_VECT; |
<> | 160:d5399cc887bb | 555 | break; |
<> | 160:d5399cc887bb | 556 | case IRQ_ERROR: |
<> | 160:d5399cc887bb | 557 | ier = CAN_IT_ERR; |
<> | 160:d5399cc887bb | 558 | irq_n = CAN1_IRQ_ERROR_IRQN; |
<> | 160:d5399cc887bb | 559 | vector = (uint32_t)&CAN1_IRQ_ERROR_VECT; |
<> | 160:d5399cc887bb | 560 | break; |
<> | 160:d5399cc887bb | 561 | case IRQ_PASSIVE: |
<> | 160:d5399cc887bb | 562 | ier = CAN_IT_EPV; |
<> | 160:d5399cc887bb | 563 | irq_n = CAN1_IRQ_PASSIVE_IRQN; |
<> | 160:d5399cc887bb | 564 | vector = (uint32_t)&CAN1_IRQ_PASSIVE_VECT; |
<> | 160:d5399cc887bb | 565 | break; |
<> | 160:d5399cc887bb | 566 | case IRQ_BUS: |
<> | 160:d5399cc887bb | 567 | ier = CAN_IT_BOF; |
<> | 160:d5399cc887bb | 568 | irq_n = CAN1_IRQ_BUS_IRQN; |
<> | 160:d5399cc887bb | 569 | vector = (uint32_t)&CAN1_IRQ_BUS_VECT; |
<> | 160:d5399cc887bb | 570 | break; |
<> | 160:d5399cc887bb | 571 | default: |
<> | 160:d5399cc887bb | 572 | return; |
<> | 160:d5399cc887bb | 573 | } |
<> | 160:d5399cc887bb | 574 | } |
<> | 160:d5399cc887bb | 575 | #if defined(CAN2_BASE) && (CAN_NUM == 2) |
<> | 160:d5399cc887bb | 576 | else if (obj->can == CAN_2) { |
<> | 160:d5399cc887bb | 577 | switch (type) { |
<> | 160:d5399cc887bb | 578 | case IRQ_RX: |
<> | 160:d5399cc887bb | 579 | ier = CAN_IT_FMP0; |
<> | 160:d5399cc887bb | 580 | irq_n = CAN2_IRQ_RX_IRQN; |
<> | 160:d5399cc887bb | 581 | vector = (uint32_t)&CAN2_IRQ_RX_VECT; |
<> | 160:d5399cc887bb | 582 | break; |
<> | 160:d5399cc887bb | 583 | case IRQ_TX: |
<> | 160:d5399cc887bb | 584 | ier = CAN_IT_TME; |
<> | 160:d5399cc887bb | 585 | irq_n = CAN2_IRQ_TX_IRQN; |
<> | 160:d5399cc887bb | 586 | vector = (uint32_t)&CAN2_IRQ_TX_VECT; |
<> | 160:d5399cc887bb | 587 | break; |
<> | 160:d5399cc887bb | 588 | case IRQ_ERROR: |
<> | 160:d5399cc887bb | 589 | ier = CAN_IT_ERR; |
<> | 160:d5399cc887bb | 590 | irq_n = CAN2_IRQ_ERROR_IRQN; |
<> | 160:d5399cc887bb | 591 | vector = (uint32_t)&CAN2_IRQ_ERROR_VECT; |
<> | 160:d5399cc887bb | 592 | break; |
<> | 160:d5399cc887bb | 593 | case IRQ_PASSIVE: |
<> | 160:d5399cc887bb | 594 | ier = CAN_IT_EPV; |
<> | 160:d5399cc887bb | 595 | irq_n = CAN2_IRQ_PASSIVE_IRQN; |
<> | 160:d5399cc887bb | 596 | vector = (uint32_t)&CAN2_IRQ_PASSIVE_VECT; |
<> | 160:d5399cc887bb | 597 | break; |
<> | 160:d5399cc887bb | 598 | case IRQ_BUS: |
<> | 160:d5399cc887bb | 599 | ier = CAN_IT_BOF; |
<> | 160:d5399cc887bb | 600 | irq_n = CAN2_IRQ_BUS_IRQN; |
<> | 160:d5399cc887bb | 601 | vector = (uint32_t)&CAN2_IRQ_BUS_VECT; |
<> | 160:d5399cc887bb | 602 | break; |
<> | 160:d5399cc887bb | 603 | default: |
<> | 160:d5399cc887bb | 604 | return; |
<> | 160:d5399cc887bb | 605 | } |
<> | 160:d5399cc887bb | 606 | } |
<> | 160:d5399cc887bb | 607 | #endif |
<> | 160:d5399cc887bb | 608 | else { |
<> | 160:d5399cc887bb | 609 | return; |
<> | 160:d5399cc887bb | 610 | } |
<> | 160:d5399cc887bb | 611 | |
<> | 160:d5399cc887bb | 612 | if (enable) { |
<> | 160:d5399cc887bb | 613 | can->IER |= ier; |
<> | 160:d5399cc887bb | 614 | } else { |
<> | 160:d5399cc887bb | 615 | can->IER &= ~ier; |
<> | 160:d5399cc887bb | 616 | } |
<> | 160:d5399cc887bb | 617 | |
<> | 160:d5399cc887bb | 618 | NVIC_SetVector(irq_n, vector); |
<> | 160:d5399cc887bb | 619 | NVIC_EnableIRQ(irq_n); |
<> | 160:d5399cc887bb | 620 | } |
<> | 160:d5399cc887bb | 621 | |
<> | 160:d5399cc887bb | 622 | #endif // DEVICE_CAN |
<> | 160:d5399cc887bb | 623 |