mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
targets/TARGET_STM/can_api.c@178:79309dc6340a, 2017-11-23 (annotated)
- Committer:
- AnnaBridge
- Date:
- Thu Nov 23 11:57:25 2017 +0000
- Revision:
- 178:79309dc6340a
- Parent:
- 174:b96e65c34a4d
- Child:
- 180:96ed750bd169
mbed-dev library. Release version 156
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 uint32_t can_irq_ids[CAN_NUM] = {0}; |
<> | 160:d5399cc887bb | 29 | static can_irq_handler irq_handler; |
<> | 160:d5399cc887bb | 30 | |
AnnaBridge | 174:b96e65c34a4d | 31 | static void can_registers_init(can_t *obj) |
AnnaBridge | 174:b96e65c34a4d | 32 | { |
AnnaBridge | 174:b96e65c34a4d | 33 | if (HAL_CAN_Init(&obj->CanHandle) != HAL_OK) { |
AnnaBridge | 174:b96e65c34a4d | 34 | error("Cannot initialize CAN"); |
AnnaBridge | 174:b96e65c34a4d | 35 | } |
AnnaBridge | 174:b96e65c34a4d | 36 | |
AnnaBridge | 174:b96e65c34a4d | 37 | // Set initial CAN frequency to specified frequency |
AnnaBridge | 174:b96e65c34a4d | 38 | if (can_frequency(obj, obj->hz) != 1) { |
AnnaBridge | 174:b96e65c34a4d | 39 | error("Can frequency could not be set\n"); |
AnnaBridge | 174:b96e65c34a4d | 40 | } |
AnnaBridge | 174:b96e65c34a4d | 41 | } |
AnnaBridge | 174:b96e65c34a4d | 42 | |
<> | 160:d5399cc887bb | 43 | void can_init(can_t *obj, PinName rd, PinName td) |
<> | 160:d5399cc887bb | 44 | { |
AnnaBridge | 167:e84263d55307 | 45 | can_init_freq(obj, rd, td, 100000); |
AnnaBridge | 167:e84263d55307 | 46 | } |
AnnaBridge | 167:e84263d55307 | 47 | |
AnnaBridge | 167:e84263d55307 | 48 | void can_init_freq (can_t *obj, PinName rd, PinName td, int hz) |
AnnaBridge | 167:e84263d55307 | 49 | { |
<> | 160:d5399cc887bb | 50 | CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD); |
<> | 160:d5399cc887bb | 51 | CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); |
AnnaBridge | 174:b96e65c34a4d | 52 | CANName can = (CANName)pinmap_merge(can_rd, can_td); |
<> | 160:d5399cc887bb | 53 | |
AnnaBridge | 174:b96e65c34a4d | 54 | MBED_ASSERT((int)can != NC); |
<> | 160:d5399cc887bb | 55 | |
AnnaBridge | 174:b96e65c34a4d | 56 | if (can == CAN_1) { |
<> | 160:d5399cc887bb | 57 | __HAL_RCC_CAN1_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 58 | obj->index = 0; |
<> | 160:d5399cc887bb | 59 | } |
AnnaBridge | 178:79309dc6340a | 60 | #if defined(CAN2_BASE) && defined(CAN_2) |
AnnaBridge | 174:b96e65c34a4d | 61 | else if (can == CAN_2) { |
<> | 160:d5399cc887bb | 62 | __HAL_RCC_CAN1_CLK_ENABLE(); // needed to set filters |
<> | 160:d5399cc887bb | 63 | __HAL_RCC_CAN2_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 64 | obj->index = 1; |
<> | 160:d5399cc887bb | 65 | } |
<> | 160:d5399cc887bb | 66 | #endif |
AnnaBridge | 178:79309dc6340a | 67 | #if defined(CAN3_BASE) && defined(CAN_3) |
AnnaBridge | 178:79309dc6340a | 68 | else if (can == CAN_3) { |
AnnaBridge | 178:79309dc6340a | 69 | __HAL_RCC_CAN3_CLK_ENABLE(); |
AnnaBridge | 178:79309dc6340a | 70 | obj->index = 2; |
AnnaBridge | 178:79309dc6340a | 71 | } |
AnnaBridge | 178:79309dc6340a | 72 | #endif |
<> | 160:d5399cc887bb | 73 | else { |
<> | 160:d5399cc887bb | 74 | return; |
<> | 160:d5399cc887bb | 75 | } |
<> | 160:d5399cc887bb | 76 | |
<> | 160:d5399cc887bb | 77 | // Configure the CAN pins |
<> | 160:d5399cc887bb | 78 | pinmap_pinout(rd, PinMap_CAN_RD); |
<> | 160:d5399cc887bb | 79 | pinmap_pinout(td, PinMap_CAN_TD); |
<> | 160:d5399cc887bb | 80 | if (rd != NC) { |
<> | 160:d5399cc887bb | 81 | pin_mode(rd, PullUp); |
<> | 160:d5399cc887bb | 82 | } |
<> | 160:d5399cc887bb | 83 | if (td != NC) { |
<> | 160:d5399cc887bb | 84 | pin_mode(td, PullUp); |
<> | 160:d5399cc887bb | 85 | } |
<> | 160:d5399cc887bb | 86 | |
AnnaBridge | 174:b96e65c34a4d | 87 | /* Use default values for rist init */ |
AnnaBridge | 174:b96e65c34a4d | 88 | obj->CanHandle.Instance = (CAN_TypeDef *)can; |
AnnaBridge | 174:b96e65c34a4d | 89 | obj->CanHandle.Init.TTCM = DISABLE; |
AnnaBridge | 174:b96e65c34a4d | 90 | obj->CanHandle.Init.ABOM = DISABLE; |
AnnaBridge | 174:b96e65c34a4d | 91 | obj->CanHandle.Init.AWUM = DISABLE; |
AnnaBridge | 174:b96e65c34a4d | 92 | obj->CanHandle.Init.NART = DISABLE; |
AnnaBridge | 174:b96e65c34a4d | 93 | obj->CanHandle.Init.RFLM = DISABLE; |
AnnaBridge | 174:b96e65c34a4d | 94 | obj->CanHandle.Init.TXFP = DISABLE; |
AnnaBridge | 174:b96e65c34a4d | 95 | obj->CanHandle.Init.Mode = CAN_MODE_NORMAL; |
AnnaBridge | 174:b96e65c34a4d | 96 | obj->CanHandle.Init.SJW = CAN_SJW_1TQ; |
AnnaBridge | 174:b96e65c34a4d | 97 | obj->CanHandle.Init.BS1 = CAN_BS1_6TQ; |
AnnaBridge | 174:b96e65c34a4d | 98 | obj->CanHandle.Init.BS2 = CAN_BS2_8TQ; |
AnnaBridge | 174:b96e65c34a4d | 99 | obj->CanHandle.Init.Prescaler = 2; |
<> | 160:d5399cc887bb | 100 | |
AnnaBridge | 174:b96e65c34a4d | 101 | /* Store frequency to be restored in case of reset */ |
AnnaBridge | 174:b96e65c34a4d | 102 | obj->hz = hz; |
<> | 160:d5399cc887bb | 103 | |
AnnaBridge | 174:b96e65c34a4d | 104 | can_registers_init(obj); |
<> | 160:d5399cc887bb | 105 | |
AnnaBridge | 174:b96e65c34a4d | 106 | uint32_t filter_number = (can == CAN_1) ? 0 : 14; |
<> | 160:d5399cc887bb | 107 | can_filter(obj, 0, 0, CANStandard, filter_number); |
<> | 160:d5399cc887bb | 108 | } |
<> | 160:d5399cc887bb | 109 | |
AnnaBridge | 174:b96e65c34a4d | 110 | |
<> | 160:d5399cc887bb | 111 | void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id) |
<> | 160:d5399cc887bb | 112 | { |
<> | 160:d5399cc887bb | 113 | irq_handler = handler; |
<> | 160:d5399cc887bb | 114 | can_irq_ids[obj->index] = id; |
<> | 160:d5399cc887bb | 115 | } |
<> | 160:d5399cc887bb | 116 | |
<> | 160:d5399cc887bb | 117 | void can_irq_free(can_t *obj) |
<> | 160:d5399cc887bb | 118 | { |
AnnaBridge | 174:b96e65c34a4d | 119 | CAN_TypeDef *can = obj->CanHandle.Instance; |
<> | 160:d5399cc887bb | 120 | |
<> | 160:d5399cc887bb | 121 | can->IER &= ~(CAN_IT_FMP0 | CAN_IT_FMP1 | CAN_IT_TME | \ |
<> | 160:d5399cc887bb | 122 | CAN_IT_ERR | CAN_IT_EPV | CAN_IT_BOF); |
AnnaBridge | 167:e84263d55307 | 123 | can_irq_ids[obj->index] = 0; |
<> | 160:d5399cc887bb | 124 | } |
<> | 160:d5399cc887bb | 125 | |
<> | 160:d5399cc887bb | 126 | void can_free(can_t *obj) |
<> | 160:d5399cc887bb | 127 | { |
AnnaBridge | 174:b96e65c34a4d | 128 | CANName can = (CANName) obj->CanHandle.Instance; |
<> | 160:d5399cc887bb | 129 | // Reset CAN and disable clock |
AnnaBridge | 174:b96e65c34a4d | 130 | if (can == CAN_1) { |
<> | 160:d5399cc887bb | 131 | __HAL_RCC_CAN1_FORCE_RESET(); |
<> | 160:d5399cc887bb | 132 | __HAL_RCC_CAN1_RELEASE_RESET(); |
<> | 160:d5399cc887bb | 133 | __HAL_RCC_CAN1_CLK_DISABLE(); |
<> | 160:d5399cc887bb | 134 | } |
AnnaBridge | 178:79309dc6340a | 135 | #if defined(CAN2_BASE) && defined(CAN_2) |
AnnaBridge | 174:b96e65c34a4d | 136 | if (can == CAN_2) { |
<> | 160:d5399cc887bb | 137 | __HAL_RCC_CAN2_FORCE_RESET(); |
<> | 160:d5399cc887bb | 138 | __HAL_RCC_CAN2_RELEASE_RESET(); |
<> | 160:d5399cc887bb | 139 | __HAL_RCC_CAN2_CLK_DISABLE(); |
<> | 160:d5399cc887bb | 140 | } |
<> | 160:d5399cc887bb | 141 | #endif |
AnnaBridge | 178:79309dc6340a | 142 | #if defined(CAN3_BASE) && defined(CAN_3) |
AnnaBridge | 178:79309dc6340a | 143 | if (can == CAN_3) { |
AnnaBridge | 178:79309dc6340a | 144 | __HAL_RCC_CAN3_FORCE_RESET(); |
AnnaBridge | 178:79309dc6340a | 145 | __HAL_RCC_CAN3_RELEASE_RESET(); |
AnnaBridge | 178:79309dc6340a | 146 | __HAL_RCC_CAN3_CLK_DISABLE(); |
AnnaBridge | 178:79309dc6340a | 147 | } |
AnnaBridge | 178:79309dc6340a | 148 | #endif |
<> | 160:d5399cc887bb | 149 | } |
<> | 160:d5399cc887bb | 150 | |
<> | 160:d5399cc887bb | 151 | // The following table is used to program bit_timing. It is an adjustment of the sample |
<> | 160:d5399cc887bb | 152 | // point by synchronizing on the start-bit edge and resynchronizing on the following edges. |
<> | 160:d5399cc887bb | 153 | // This table has the sampling points as close to 75% as possible (most commonly used). |
<> | 160:d5399cc887bb | 154 | // The first value is TSEG1, the second TSEG2. |
<> | 160:d5399cc887bb | 155 | static const int timing_pts[23][2] = { |
<> | 160:d5399cc887bb | 156 | {0x0, 0x0}, // 2, 50% |
<> | 160:d5399cc887bb | 157 | {0x1, 0x0}, // 3, 67% |
<> | 160:d5399cc887bb | 158 | {0x2, 0x0}, // 4, 75% |
<> | 160:d5399cc887bb | 159 | {0x3, 0x0}, // 5, 80% |
<> | 160:d5399cc887bb | 160 | {0x3, 0x1}, // 6, 67% |
<> | 160:d5399cc887bb | 161 | {0x4, 0x1}, // 7, 71% |
<> | 160:d5399cc887bb | 162 | {0x5, 0x1}, // 8, 75% |
<> | 160:d5399cc887bb | 163 | {0x6, 0x1}, // 9, 78% |
<> | 160:d5399cc887bb | 164 | {0x6, 0x2}, // 10, 70% |
<> | 160:d5399cc887bb | 165 | {0x7, 0x2}, // 11, 73% |
<> | 160:d5399cc887bb | 166 | {0x8, 0x2}, // 12, 75% |
<> | 160:d5399cc887bb | 167 | {0x9, 0x2}, // 13, 77% |
<> | 160:d5399cc887bb | 168 | {0x9, 0x3}, // 14, 71% |
<> | 160:d5399cc887bb | 169 | {0xA, 0x3}, // 15, 73% |
<> | 160:d5399cc887bb | 170 | {0xB, 0x3}, // 16, 75% |
<> | 160:d5399cc887bb | 171 | {0xC, 0x3}, // 17, 76% |
<> | 160:d5399cc887bb | 172 | {0xD, 0x3}, // 18, 78% |
<> | 160:d5399cc887bb | 173 | {0xD, 0x4}, // 19, 74% |
<> | 160:d5399cc887bb | 174 | {0xE, 0x4}, // 20, 75% |
<> | 160:d5399cc887bb | 175 | {0xF, 0x4}, // 21, 76% |
<> | 160:d5399cc887bb | 176 | {0xF, 0x5}, // 22, 73% |
<> | 160:d5399cc887bb | 177 | {0xF, 0x6}, // 23, 70% |
<> | 160:d5399cc887bb | 178 | {0xF, 0x7}, // 24, 67% |
<> | 160:d5399cc887bb | 179 | }; |
<> | 160:d5399cc887bb | 180 | |
<> | 160:d5399cc887bb | 181 | static unsigned int can_speed(unsigned int pclk, unsigned int cclk, unsigned char psjw) |
<> | 160:d5399cc887bb | 182 | { |
<> | 160:d5399cc887bb | 183 | uint32_t btr; |
<> | 160:d5399cc887bb | 184 | uint16_t brp = 0; |
<> | 160:d5399cc887bb | 185 | uint32_t calcbit; |
<> | 160:d5399cc887bb | 186 | uint32_t bitwidth; |
<> | 160:d5399cc887bb | 187 | int hit = 0; |
<> | 160:d5399cc887bb | 188 | int bits; |
<> | 160:d5399cc887bb | 189 | |
<> | 160:d5399cc887bb | 190 | bitwidth = (pclk / cclk); |
<> | 160:d5399cc887bb | 191 | |
<> | 160:d5399cc887bb | 192 | brp = bitwidth / 0x18; |
<> | 160:d5399cc887bb | 193 | while ((!hit) && (brp < bitwidth / 4)) { |
<> | 160:d5399cc887bb | 194 | brp++; |
<> | 160:d5399cc887bb | 195 | for (bits = 22; bits > 0; bits--) { |
<> | 160:d5399cc887bb | 196 | calcbit = (bits + 3) * (brp + 1); |
<> | 160:d5399cc887bb | 197 | if (calcbit == bitwidth) { |
<> | 160:d5399cc887bb | 198 | hit = 1; |
<> | 160:d5399cc887bb | 199 | break; |
<> | 160:d5399cc887bb | 200 | } |
<> | 160:d5399cc887bb | 201 | } |
<> | 160:d5399cc887bb | 202 | } |
<> | 160:d5399cc887bb | 203 | |
<> | 160:d5399cc887bb | 204 | if (hit) { |
AnnaBridge | 172:7d866c31b3c5 | 205 | btr = ((timing_pts[bits][1] << CAN_BTR_TS2_Pos) & CAN_BTR_TS2) | |
AnnaBridge | 172:7d866c31b3c5 | 206 | ((timing_pts[bits][0] << CAN_BTR_TS1_Pos) & CAN_BTR_TS1) | |
AnnaBridge | 172:7d866c31b3c5 | 207 | ((psjw << CAN_BTR_SJW_Pos) & CAN_BTR_SJW) | |
AnnaBridge | 172:7d866c31b3c5 | 208 | ((brp << CAN_BTR_BRP_Pos) & CAN_BTR_BRP); |
<> | 160:d5399cc887bb | 209 | } else { |
<> | 160:d5399cc887bb | 210 | btr = 0xFFFFFFFF; |
<> | 160:d5399cc887bb | 211 | } |
<> | 160:d5399cc887bb | 212 | |
<> | 160:d5399cc887bb | 213 | return btr; |
<> | 160:d5399cc887bb | 214 | |
<> | 160:d5399cc887bb | 215 | } |
<> | 160:d5399cc887bb | 216 | |
<> | 160:d5399cc887bb | 217 | int can_frequency(can_t *obj, int f) |
<> | 160:d5399cc887bb | 218 | { |
<> | 160:d5399cc887bb | 219 | int pclk = HAL_RCC_GetPCLK1Freq(); |
<> | 160:d5399cc887bb | 220 | int btr = can_speed(pclk, (unsigned int)f, 1); |
AnnaBridge | 174:b96e65c34a4d | 221 | CAN_TypeDef *can = obj->CanHandle.Instance; |
AnnaBridge | 167:e84263d55307 | 222 | uint32_t tickstart = 0; |
AnnaBridge | 167:e84263d55307 | 223 | int status = 1; |
<> | 160:d5399cc887bb | 224 | |
<> | 160:d5399cc887bb | 225 | if (btr > 0) { |
<> | 160:d5399cc887bb | 226 | can->MCR |= CAN_MCR_INRQ ; |
AnnaBridge | 167:e84263d55307 | 227 | /* Get tick */ |
AnnaBridge | 167:e84263d55307 | 228 | tickstart = HAL_GetTick(); |
<> | 160:d5399cc887bb | 229 | while ((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) { |
AnnaBridge | 167:e84263d55307 | 230 | if ((HAL_GetTick() - tickstart) > 2) { |
AnnaBridge | 167:e84263d55307 | 231 | status = 0; |
AnnaBridge | 167:e84263d55307 | 232 | break; |
AnnaBridge | 167:e84263d55307 | 233 | } |
<> | 160:d5399cc887bb | 234 | } |
AnnaBridge | 167:e84263d55307 | 235 | if (status != 0) { |
AnnaBridge | 174:b96e65c34a4d | 236 | /* Do not erase all BTR registers (e.g. silent mode), only the |
AnnaBridge | 174:b96e65c34a4d | 237 | * ones calculated in can_speed */ |
AnnaBridge | 174:b96e65c34a4d | 238 | can->BTR &= ~(CAN_BTR_TS2 | CAN_BTR_TS1 | CAN_BTR_SJW | CAN_BTR_BRP); |
AnnaBridge | 174:b96e65c34a4d | 239 | can->BTR |= btr; |
AnnaBridge | 174:b96e65c34a4d | 240 | |
AnnaBridge | 167:e84263d55307 | 241 | can->MCR &= ~(uint32_t)CAN_MCR_INRQ; |
AnnaBridge | 167:e84263d55307 | 242 | /* Get tick */ |
AnnaBridge | 167:e84263d55307 | 243 | tickstart = HAL_GetTick(); |
AnnaBridge | 167:e84263d55307 | 244 | while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) { |
AnnaBridge | 167:e84263d55307 | 245 | if ((HAL_GetTick() - tickstart) > 2) { |
AnnaBridge | 167:e84263d55307 | 246 | status = 0; |
AnnaBridge | 167:e84263d55307 | 247 | break; |
AnnaBridge | 167:e84263d55307 | 248 | } |
AnnaBridge | 167:e84263d55307 | 249 | } |
AnnaBridge | 167:e84263d55307 | 250 | if (status == 0) { |
AnnaBridge | 167:e84263d55307 | 251 | error("can ESR 0x%04x.%04x + timeout status %d", (can->ESR & 0xFFFF0000) >> 16, (can->ESR & 0xFFFF), status); |
AnnaBridge | 167:e84263d55307 | 252 | } |
AnnaBridge | 167:e84263d55307 | 253 | } else { |
AnnaBridge | 167:e84263d55307 | 254 | error("can init request timeout\n"); |
<> | 160:d5399cc887bb | 255 | } |
<> | 160:d5399cc887bb | 256 | } else { |
AnnaBridge | 167:e84263d55307 | 257 | status = 0; |
<> | 160:d5399cc887bb | 258 | } |
AnnaBridge | 167:e84263d55307 | 259 | return status; |
<> | 160:d5399cc887bb | 260 | } |
<> | 160:d5399cc887bb | 261 | |
<> | 160:d5399cc887bb | 262 | int can_write(can_t *obj, CAN_Message msg, int cc) |
<> | 160:d5399cc887bb | 263 | { |
<> | 160:d5399cc887bb | 264 | uint32_t transmitmailbox = CAN_TXSTATUS_NOMAILBOX; |
AnnaBridge | 174:b96e65c34a4d | 265 | CAN_TypeDef *can = obj->CanHandle.Instance; |
<> | 160:d5399cc887bb | 266 | |
<> | 160:d5399cc887bb | 267 | /* Select one empty transmit mailbox */ |
<> | 160:d5399cc887bb | 268 | if ((can->TSR & CAN_TSR_TME0) == CAN_TSR_TME0) { |
<> | 160:d5399cc887bb | 269 | transmitmailbox = 0; |
<> | 160:d5399cc887bb | 270 | } else if ((can->TSR & CAN_TSR_TME1) == CAN_TSR_TME1) { |
<> | 160:d5399cc887bb | 271 | transmitmailbox = 1; |
<> | 160:d5399cc887bb | 272 | } else if ((can->TSR & CAN_TSR_TME2) == CAN_TSR_TME2) { |
<> | 160:d5399cc887bb | 273 | transmitmailbox = 2; |
<> | 160:d5399cc887bb | 274 | } else { |
<> | 160:d5399cc887bb | 275 | return 0; |
<> | 160:d5399cc887bb | 276 | } |
<> | 160:d5399cc887bb | 277 | |
<> | 160:d5399cc887bb | 278 | can->sTxMailBox[transmitmailbox].TIR &= CAN_TI0R_TXRQ; |
<> | 160:d5399cc887bb | 279 | if (!(msg.format)) { |
<> | 160:d5399cc887bb | 280 | can->sTxMailBox[transmitmailbox].TIR |= ((msg.id << 21) | msg.type); |
<> | 160:d5399cc887bb | 281 | } else { |
<> | 160:d5399cc887bb | 282 | can->sTxMailBox[transmitmailbox].TIR |= ((msg.id << 3) | CAN_ID_EXT | msg.type); |
<> | 160:d5399cc887bb | 283 | } |
AnnaBridge | 167:e84263d55307 | 284 | |
<> | 160:d5399cc887bb | 285 | /* Set up the DLC */ |
<> | 160:d5399cc887bb | 286 | can->sTxMailBox[transmitmailbox].TDTR &= (uint32_t)0xFFFFFFF0; |
<> | 160:d5399cc887bb | 287 | can->sTxMailBox[transmitmailbox].TDTR |= (msg.len & (uint8_t)0x0000000F); |
AnnaBridge | 167:e84263d55307 | 288 | |
<> | 160:d5399cc887bb | 289 | /* Set up the data field */ |
<> | 160:d5399cc887bb | 290 | can->sTxMailBox[transmitmailbox].TDLR = (((uint32_t)msg.data[3] << 24) | |
AnnaBridge | 167:e84263d55307 | 291 | ((uint32_t)msg.data[2] << 16) | |
AnnaBridge | 167:e84263d55307 | 292 | ((uint32_t)msg.data[1] << 8) | |
AnnaBridge | 167:e84263d55307 | 293 | ((uint32_t)msg.data[0])); |
<> | 160:d5399cc887bb | 294 | can->sTxMailBox[transmitmailbox].TDHR = (((uint32_t)msg.data[7] << 24) | |
AnnaBridge | 167:e84263d55307 | 295 | ((uint32_t)msg.data[6] << 16) | |
AnnaBridge | 167:e84263d55307 | 296 | ((uint32_t)msg.data[5] << 8) | |
<> | 160:d5399cc887bb | 297 | ((uint32_t)msg.data[4])); |
<> | 160:d5399cc887bb | 298 | /* Request transmission */ |
<> | 160:d5399cc887bb | 299 | can->sTxMailBox[transmitmailbox].TIR |= CAN_TI0R_TXRQ; |
<> | 160:d5399cc887bb | 300 | |
<> | 160:d5399cc887bb | 301 | return 1; |
<> | 160:d5399cc887bb | 302 | } |
<> | 160:d5399cc887bb | 303 | |
<> | 160:d5399cc887bb | 304 | int can_read(can_t *obj, CAN_Message *msg, int handle) |
<> | 160:d5399cc887bb | 305 | { |
<> | 160:d5399cc887bb | 306 | //handle is the FIFO number |
<> | 160:d5399cc887bb | 307 | |
AnnaBridge | 174:b96e65c34a4d | 308 | CAN_TypeDef *can = obj->CanHandle.Instance; |
<> | 160:d5399cc887bb | 309 | |
<> | 160:d5399cc887bb | 310 | // check FPM0 which holds the pending message count in FIFO 0 |
<> | 160:d5399cc887bb | 311 | // if no message is pending, return 0 |
<> | 160:d5399cc887bb | 312 | if ((can->RF0R & CAN_RF0R_FMP0) == 0) { |
<> | 160:d5399cc887bb | 313 | return 0; |
<> | 160:d5399cc887bb | 314 | } |
<> | 160:d5399cc887bb | 315 | |
<> | 160:d5399cc887bb | 316 | /* Get the Id */ |
<> | 160:d5399cc887bb | 317 | msg->format = (CANFormat)(((uint8_t)0x04 & can->sFIFOMailBox[handle].RIR) >> 2); |
<> | 160:d5399cc887bb | 318 | if (!msg->format) { |
<> | 160:d5399cc887bb | 319 | msg->id = (uint32_t)0x000007FF & (can->sFIFOMailBox[handle].RIR >> 21); |
<> | 160:d5399cc887bb | 320 | } else { |
<> | 160:d5399cc887bb | 321 | msg->id = (uint32_t)0x1FFFFFFF & (can->sFIFOMailBox[handle].RIR >> 3); |
<> | 160:d5399cc887bb | 322 | } |
<> | 160:d5399cc887bb | 323 | |
<> | 160:d5399cc887bb | 324 | msg->type = (CANType)(((uint8_t)0x02 & can->sFIFOMailBox[handle].RIR) >> 1); |
<> | 160:d5399cc887bb | 325 | /* Get the DLC */ |
<> | 160:d5399cc887bb | 326 | msg->len = (uint8_t)0x0F & can->sFIFOMailBox[handle].RDTR; |
<> | 160:d5399cc887bb | 327 | /* Get the FMI */ |
<> | 160:d5399cc887bb | 328 | // msg->FMI = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDTR >> 8); |
<> | 160:d5399cc887bb | 329 | /* Get the data field */ |
<> | 160:d5399cc887bb | 330 | msg->data[0] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDLR; |
<> | 160:d5399cc887bb | 331 | msg->data[1] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 8); |
<> | 160:d5399cc887bb | 332 | msg->data[2] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 16); |
<> | 160:d5399cc887bb | 333 | msg->data[3] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 24); |
<> | 160:d5399cc887bb | 334 | msg->data[4] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDHR; |
<> | 160:d5399cc887bb | 335 | msg->data[5] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 8); |
<> | 160:d5399cc887bb | 336 | msg->data[6] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 16); |
<> | 160:d5399cc887bb | 337 | msg->data[7] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 24); |
<> | 160:d5399cc887bb | 338 | |
<> | 160:d5399cc887bb | 339 | /* Release the FIFO */ |
<> | 160:d5399cc887bb | 340 | if (handle == CAN_FIFO0) { |
<> | 160:d5399cc887bb | 341 | /* Release FIFO0 */ |
<> | 160:d5399cc887bb | 342 | can->RF0R |= CAN_RF0R_RFOM0; |
<> | 160:d5399cc887bb | 343 | } else { /* FIFONumber == CAN_FIFO1 */ |
<> | 160:d5399cc887bb | 344 | /* Release FIFO1 */ |
<> | 160:d5399cc887bb | 345 | can->RF1R |= CAN_RF1R_RFOM1; |
<> | 160:d5399cc887bb | 346 | } |
<> | 160:d5399cc887bb | 347 | |
<> | 160:d5399cc887bb | 348 | return 1; |
<> | 160:d5399cc887bb | 349 | } |
<> | 160:d5399cc887bb | 350 | |
<> | 160:d5399cc887bb | 351 | void can_reset(can_t *obj) |
<> | 160:d5399cc887bb | 352 | { |
AnnaBridge | 174:b96e65c34a4d | 353 | CAN_TypeDef *can = obj->CanHandle.Instance; |
AnnaBridge | 174:b96e65c34a4d | 354 | |
AnnaBridge | 174:b96e65c34a4d | 355 | /* Reset IP and delete errors */ |
<> | 160:d5399cc887bb | 356 | can->MCR |= CAN_MCR_RESET; |
<> | 160:d5399cc887bb | 357 | can->ESR = 0x0; |
AnnaBridge | 174:b96e65c34a4d | 358 | |
AnnaBridge | 174:b96e65c34a4d | 359 | /* restore registers state as saved in obj context */ |
AnnaBridge | 174:b96e65c34a4d | 360 | can_registers_init(obj); |
<> | 160:d5399cc887bb | 361 | } |
<> | 160:d5399cc887bb | 362 | |
<> | 160:d5399cc887bb | 363 | unsigned char can_rderror(can_t *obj) |
<> | 160:d5399cc887bb | 364 | { |
AnnaBridge | 174:b96e65c34a4d | 365 | CAN_TypeDef *can = obj->CanHandle.Instance; |
<> | 160:d5399cc887bb | 366 | return (can->ESR >> 24) & 0xFF; |
<> | 160:d5399cc887bb | 367 | } |
<> | 160:d5399cc887bb | 368 | |
<> | 160:d5399cc887bb | 369 | unsigned char can_tderror(can_t *obj) |
<> | 160:d5399cc887bb | 370 | { |
AnnaBridge | 174:b96e65c34a4d | 371 | CAN_TypeDef *can = obj->CanHandle.Instance; |
<> | 160:d5399cc887bb | 372 | return (can->ESR >> 16) & 0xFF; |
<> | 160:d5399cc887bb | 373 | } |
<> | 160:d5399cc887bb | 374 | |
<> | 160:d5399cc887bb | 375 | void can_monitor(can_t *obj, int silent) |
<> | 160:d5399cc887bb | 376 | { |
AnnaBridge | 174:b96e65c34a4d | 377 | CanMode mode = MODE_NORMAL; |
AnnaBridge | 174:b96e65c34a4d | 378 | /* Update current state w/ or w/o silent */ |
AnnaBridge | 174:b96e65c34a4d | 379 | if(silent) { |
AnnaBridge | 174:b96e65c34a4d | 380 | switch (obj->CanHandle.Init.Mode) { |
AnnaBridge | 174:b96e65c34a4d | 381 | case CAN_MODE_LOOPBACK: |
AnnaBridge | 174:b96e65c34a4d | 382 | case CAN_MODE_SILENT_LOOPBACK: |
AnnaBridge | 174:b96e65c34a4d | 383 | mode = MODE_TEST_SILENT; |
AnnaBridge | 174:b96e65c34a4d | 384 | break; |
AnnaBridge | 174:b96e65c34a4d | 385 | default: |
AnnaBridge | 174:b96e65c34a4d | 386 | mode = MODE_SILENT; |
AnnaBridge | 174:b96e65c34a4d | 387 | break; |
AnnaBridge | 174:b96e65c34a4d | 388 | } |
AnnaBridge | 174:b96e65c34a4d | 389 | } else { |
AnnaBridge | 174:b96e65c34a4d | 390 | switch (obj->CanHandle.Init.Mode) { |
AnnaBridge | 174:b96e65c34a4d | 391 | case CAN_MODE_LOOPBACK: |
AnnaBridge | 174:b96e65c34a4d | 392 | case CAN_MODE_SILENT_LOOPBACK: |
AnnaBridge | 174:b96e65c34a4d | 393 | mode = MODE_TEST_LOCAL; |
AnnaBridge | 174:b96e65c34a4d | 394 | break; |
AnnaBridge | 174:b96e65c34a4d | 395 | default: |
AnnaBridge | 174:b96e65c34a4d | 396 | mode = MODE_NORMAL; |
AnnaBridge | 174:b96e65c34a4d | 397 | break; |
AnnaBridge | 174:b96e65c34a4d | 398 | } |
<> | 160:d5399cc887bb | 399 | } |
<> | 160:d5399cc887bb | 400 | |
AnnaBridge | 174:b96e65c34a4d | 401 | can_mode(obj, mode); |
<> | 160:d5399cc887bb | 402 | } |
<> | 160:d5399cc887bb | 403 | |
<> | 160:d5399cc887bb | 404 | int can_mode(can_t *obj, CanMode mode) |
<> | 160:d5399cc887bb | 405 | { |
<> | 160:d5399cc887bb | 406 | int success = 0; |
AnnaBridge | 174:b96e65c34a4d | 407 | CAN_TypeDef *can = obj->CanHandle.Instance; |
<> | 160:d5399cc887bb | 408 | |
<> | 160:d5399cc887bb | 409 | can->MCR |= CAN_MCR_INRQ ; |
<> | 160:d5399cc887bb | 410 | while ((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) { |
<> | 160:d5399cc887bb | 411 | } |
<> | 160:d5399cc887bb | 412 | |
<> | 160:d5399cc887bb | 413 | switch (mode) { |
<> | 160:d5399cc887bb | 414 | case MODE_NORMAL: |
AnnaBridge | 174:b96e65c34a4d | 415 | obj->CanHandle.Init.Mode = CAN_MODE_NORMAL; |
<> | 160:d5399cc887bb | 416 | can->BTR &= ~(CAN_BTR_SILM | CAN_BTR_LBKM); |
<> | 160:d5399cc887bb | 417 | success = 1; |
<> | 160:d5399cc887bb | 418 | break; |
<> | 160:d5399cc887bb | 419 | case MODE_SILENT: |
AnnaBridge | 174:b96e65c34a4d | 420 | obj->CanHandle.Init.Mode = CAN_MODE_SILENT; |
<> | 160:d5399cc887bb | 421 | can->BTR |= CAN_BTR_SILM; |
<> | 160:d5399cc887bb | 422 | can->BTR &= ~CAN_BTR_LBKM; |
<> | 160:d5399cc887bb | 423 | success = 1; |
<> | 160:d5399cc887bb | 424 | break; |
<> | 160:d5399cc887bb | 425 | case MODE_TEST_GLOBAL: |
<> | 160:d5399cc887bb | 426 | case MODE_TEST_LOCAL: |
AnnaBridge | 174:b96e65c34a4d | 427 | obj->CanHandle.Init.Mode = CAN_MODE_LOOPBACK; |
<> | 160:d5399cc887bb | 428 | can->BTR |= CAN_BTR_LBKM; |
<> | 160:d5399cc887bb | 429 | can->BTR &= ~CAN_BTR_SILM; |
<> | 160:d5399cc887bb | 430 | success = 1; |
<> | 160:d5399cc887bb | 431 | break; |
<> | 160:d5399cc887bb | 432 | case MODE_TEST_SILENT: |
AnnaBridge | 174:b96e65c34a4d | 433 | obj->CanHandle.Init.Mode = CAN_MODE_SILENT_LOOPBACK; |
<> | 160:d5399cc887bb | 434 | can->BTR |= (CAN_BTR_SILM | CAN_BTR_LBKM); |
<> | 160:d5399cc887bb | 435 | success = 1; |
<> | 160:d5399cc887bb | 436 | break; |
<> | 160:d5399cc887bb | 437 | default: |
<> | 160:d5399cc887bb | 438 | success = 0; |
<> | 160:d5399cc887bb | 439 | break; |
<> | 160:d5399cc887bb | 440 | } |
<> | 160:d5399cc887bb | 441 | |
<> | 160:d5399cc887bb | 442 | can->MCR &= ~(uint32_t)CAN_MCR_INRQ; |
<> | 160:d5399cc887bb | 443 | while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) { |
<> | 160:d5399cc887bb | 444 | } |
<> | 160:d5399cc887bb | 445 | |
<> | 160:d5399cc887bb | 446 | return success; |
<> | 160:d5399cc887bb | 447 | } |
<> | 160:d5399cc887bb | 448 | |
<> | 160:d5399cc887bb | 449 | int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) |
<> | 160:d5399cc887bb | 450 | { |
<> | 160:d5399cc887bb | 451 | int retval = 0; |
<> | 160:d5399cc887bb | 452 | |
<> | 160:d5399cc887bb | 453 | // filter for CANAny format cannot be configured for STM32 |
<> | 160:d5399cc887bb | 454 | if ((format == CANStandard) || (format == CANExtended)) { |
<> | 160:d5399cc887bb | 455 | CAN_FilterConfTypeDef sFilterConfig; |
<> | 160:d5399cc887bb | 456 | sFilterConfig.FilterNumber = handle; |
<> | 160:d5399cc887bb | 457 | sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK; |
<> | 160:d5399cc887bb | 458 | sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT; |
<> | 160:d5399cc887bb | 459 | |
<> | 160:d5399cc887bb | 460 | if (format == CANStandard) { |
<> | 160:d5399cc887bb | 461 | sFilterConfig.FilterIdHigh = id << 5; |
<> | 160:d5399cc887bb | 462 | sFilterConfig.FilterIdLow = 0x0; |
<> | 160:d5399cc887bb | 463 | sFilterConfig.FilterMaskIdHigh = mask << 5; |
<> | 160:d5399cc887bb | 464 | sFilterConfig.FilterMaskIdLow = 0x0; // allows both remote and data frames |
<> | 160:d5399cc887bb | 465 | } else if (format == CANExtended) { |
<> | 160:d5399cc887bb | 466 | sFilterConfig.FilterIdHigh = id >> 13; // EXTID[28:13] |
<> | 160:d5399cc887bb | 467 | sFilterConfig.FilterIdLow = (0x00FF & (id << 3)) | (1 << 2); // EXTID[12:0] |
<> | 160:d5399cc887bb | 468 | sFilterConfig.FilterMaskIdHigh = mask >> 13; |
<> | 160:d5399cc887bb | 469 | sFilterConfig.FilterMaskIdLow = (0x00FF & (mask << 3)) | (1 << 2); |
<> | 160:d5399cc887bb | 470 | } |
<> | 160:d5399cc887bb | 471 | |
<> | 160:d5399cc887bb | 472 | sFilterConfig.FilterFIFOAssignment = 0; |
<> | 160:d5399cc887bb | 473 | sFilterConfig.FilterActivation = ENABLE; |
<> | 160:d5399cc887bb | 474 | sFilterConfig.BankNumber = 14 + handle; |
<> | 160:d5399cc887bb | 475 | |
AnnaBridge | 174:b96e65c34a4d | 476 | HAL_CAN_ConfigFilter(&obj->CanHandle, &sFilterConfig); |
<> | 160:d5399cc887bb | 477 | retval = handle; |
<> | 160:d5399cc887bb | 478 | } |
<> | 160:d5399cc887bb | 479 | return retval; |
<> | 160:d5399cc887bb | 480 | } |
<> | 160:d5399cc887bb | 481 | |
<> | 160:d5399cc887bb | 482 | static void can_irq(CANName name, int id) |
<> | 160:d5399cc887bb | 483 | { |
<> | 160:d5399cc887bb | 484 | uint32_t tmp1 = 0, tmp2 = 0, tmp3 = 0; |
AnnaBridge | 174:b96e65c34a4d | 485 | CAN_HandleTypeDef CanHandle; |
<> | 160:d5399cc887bb | 486 | CanHandle.Instance = (CAN_TypeDef *)name; |
<> | 160:d5399cc887bb | 487 | |
<> | 160:d5399cc887bb | 488 | if (__HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_TME)) { |
<> | 160:d5399cc887bb | 489 | tmp1 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_0); |
<> | 160:d5399cc887bb | 490 | tmp2 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_1); |
<> | 160:d5399cc887bb | 491 | tmp3 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_2); |
<> | 160:d5399cc887bb | 492 | if (tmp1) { |
<> | 160:d5399cc887bb | 493 | __HAL_CAN_CLEAR_FLAG(&CanHandle, CAN_FLAG_RQCP0); |
<> | 160:d5399cc887bb | 494 | } |
<> | 160:d5399cc887bb | 495 | if (tmp2) { |
<> | 160:d5399cc887bb | 496 | __HAL_CAN_CLEAR_FLAG(&CanHandle, CAN_FLAG_RQCP1); |
<> | 160:d5399cc887bb | 497 | } |
<> | 160:d5399cc887bb | 498 | if (tmp3) { |
<> | 160:d5399cc887bb | 499 | __HAL_CAN_CLEAR_FLAG(&CanHandle, CAN_FLAG_RQCP2); |
<> | 160:d5399cc887bb | 500 | } |
<> | 160:d5399cc887bb | 501 | if (tmp1 || tmp2 || tmp3) { |
<> | 160:d5399cc887bb | 502 | irq_handler(can_irq_ids[id], IRQ_TX); |
<> | 160:d5399cc887bb | 503 | } |
<> | 160:d5399cc887bb | 504 | } |
<> | 160:d5399cc887bb | 505 | |
<> | 160:d5399cc887bb | 506 | tmp1 = __HAL_CAN_MSG_PENDING(&CanHandle, CAN_FIFO0); |
<> | 160:d5399cc887bb | 507 | tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_FMP0); |
<> | 160:d5399cc887bb | 508 | |
<> | 160:d5399cc887bb | 509 | if ((tmp1 != 0) && tmp2) { |
<> | 160:d5399cc887bb | 510 | irq_handler(can_irq_ids[id], IRQ_RX); |
<> | 160:d5399cc887bb | 511 | } |
<> | 160:d5399cc887bb | 512 | |
<> | 160:d5399cc887bb | 513 | tmp1 = __HAL_CAN_GET_FLAG(&CanHandle, CAN_FLAG_EPV); |
<> | 160:d5399cc887bb | 514 | tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_EPV); |
<> | 160:d5399cc887bb | 515 | tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR); |
<> | 160:d5399cc887bb | 516 | |
<> | 160:d5399cc887bb | 517 | if (tmp1 && tmp2 && tmp3) { |
<> | 160:d5399cc887bb | 518 | irq_handler(can_irq_ids[id], IRQ_PASSIVE); |
<> | 160:d5399cc887bb | 519 | } |
<> | 160:d5399cc887bb | 520 | |
<> | 160:d5399cc887bb | 521 | tmp1 = __HAL_CAN_GET_FLAG(&CanHandle, CAN_FLAG_BOF); |
<> | 160:d5399cc887bb | 522 | tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_BOF); |
<> | 160:d5399cc887bb | 523 | tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR); |
<> | 160:d5399cc887bb | 524 | if (tmp1 && tmp2 && tmp3) { |
<> | 160:d5399cc887bb | 525 | irq_handler(can_irq_ids[id], IRQ_BUS); |
<> | 160:d5399cc887bb | 526 | } |
<> | 160:d5399cc887bb | 527 | |
<> | 160:d5399cc887bb | 528 | tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR); |
<> | 160:d5399cc887bb | 529 | if (tmp1 && tmp2 && tmp3) { |
<> | 160:d5399cc887bb | 530 | irq_handler(can_irq_ids[id], IRQ_ERROR); |
<> | 160:d5399cc887bb | 531 | } |
<> | 160:d5399cc887bb | 532 | } |
<> | 160:d5399cc887bb | 533 | |
<> | 160:d5399cc887bb | 534 | #if defined(TARGET_STM32F0) |
<> | 160:d5399cc887bb | 535 | void CAN_IRQHandler(void) |
<> | 160:d5399cc887bb | 536 | { |
<> | 160:d5399cc887bb | 537 | can_irq(CAN_1, 0); |
<> | 160:d5399cc887bb | 538 | } |
<> | 160:d5399cc887bb | 539 | #elif defined(TARGET_STM32F3) |
<> | 160:d5399cc887bb | 540 | void CAN_RX0_IRQHandler(void) |
<> | 160:d5399cc887bb | 541 | { |
<> | 160:d5399cc887bb | 542 | can_irq(CAN_1, 0); |
<> | 160:d5399cc887bb | 543 | } |
<> | 160:d5399cc887bb | 544 | void CAN_TX_IRQHandler(void) |
<> | 160:d5399cc887bb | 545 | { |
<> | 160:d5399cc887bb | 546 | can_irq(CAN_1, 0); |
<> | 160:d5399cc887bb | 547 | } |
<> | 160:d5399cc887bb | 548 | void CAN_SCE_IRQHandler(void) |
<> | 160:d5399cc887bb | 549 | { |
<> | 160:d5399cc887bb | 550 | can_irq(CAN_1, 0); |
<> | 160:d5399cc887bb | 551 | } |
<> | 160:d5399cc887bb | 552 | #else |
<> | 160:d5399cc887bb | 553 | void CAN1_RX0_IRQHandler(void) |
<> | 160:d5399cc887bb | 554 | { |
<> | 160:d5399cc887bb | 555 | can_irq(CAN_1, 0); |
<> | 160:d5399cc887bb | 556 | } |
<> | 160:d5399cc887bb | 557 | void CAN1_TX_IRQHandler(void) |
<> | 160:d5399cc887bb | 558 | { |
<> | 160:d5399cc887bb | 559 | can_irq(CAN_1, 0); |
<> | 160:d5399cc887bb | 560 | } |
<> | 160:d5399cc887bb | 561 | void CAN1_SCE_IRQHandler(void) |
<> | 160:d5399cc887bb | 562 | { |
<> | 160:d5399cc887bb | 563 | can_irq(CAN_1, 0); |
<> | 160:d5399cc887bb | 564 | } |
AnnaBridge | 178:79309dc6340a | 565 | #if defined(CAN2_BASE) && defined(CAN_2) |
<> | 160:d5399cc887bb | 566 | void CAN2_RX0_IRQHandler(void) |
<> | 160:d5399cc887bb | 567 | { |
<> | 160:d5399cc887bb | 568 | can_irq(CAN_2, 1); |
<> | 160:d5399cc887bb | 569 | } |
<> | 160:d5399cc887bb | 570 | void CAN2_TX_IRQHandler(void) |
<> | 160:d5399cc887bb | 571 | { |
<> | 160:d5399cc887bb | 572 | can_irq(CAN_2, 1); |
<> | 160:d5399cc887bb | 573 | } |
<> | 160:d5399cc887bb | 574 | void CAN2_SCE_IRQHandler(void) |
<> | 160:d5399cc887bb | 575 | { |
<> | 160:d5399cc887bb | 576 | can_irq(CAN_2, 1); |
<> | 160:d5399cc887bb | 577 | } |
AnnaBridge | 178:79309dc6340a | 578 | #endif |
AnnaBridge | 178:79309dc6340a | 579 | #if defined(CAN3_BASE) && defined(CAN_3) |
AnnaBridge | 178:79309dc6340a | 580 | void CAN3_RX0_IRQHandler(void) |
AnnaBridge | 178:79309dc6340a | 581 | { |
AnnaBridge | 178:79309dc6340a | 582 | can_irq(CAN_3, 1); |
AnnaBridge | 178:79309dc6340a | 583 | } |
AnnaBridge | 178:79309dc6340a | 584 | void CAN3_TX_IRQHandler(void) |
AnnaBridge | 178:79309dc6340a | 585 | { |
AnnaBridge | 178:79309dc6340a | 586 | can_irq(CAN_3, 1); |
AnnaBridge | 178:79309dc6340a | 587 | } |
AnnaBridge | 178:79309dc6340a | 588 | void CAN3_SCE_IRQHandler(void) |
AnnaBridge | 178:79309dc6340a | 589 | { |
AnnaBridge | 178:79309dc6340a | 590 | can_irq(CAN_3, 1); |
AnnaBridge | 178:79309dc6340a | 591 | } |
AnnaBridge | 178:79309dc6340a | 592 | #endif |
<> | 160:d5399cc887bb | 593 | #endif // else |
<> | 160:d5399cc887bb | 594 | |
<> | 160:d5399cc887bb | 595 | void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable) |
<> | 160:d5399cc887bb | 596 | { |
AnnaBridge | 174:b96e65c34a4d | 597 | CAN_TypeDef *can = obj->CanHandle.Instance; |
<> | 160:d5399cc887bb | 598 | IRQn_Type irq_n = (IRQn_Type)0; |
<> | 160:d5399cc887bb | 599 | uint32_t vector = 0; |
<> | 160:d5399cc887bb | 600 | uint32_t ier; |
<> | 160:d5399cc887bb | 601 | |
AnnaBridge | 174:b96e65c34a4d | 602 | if ((CANName) can == CAN_1) { |
<> | 160:d5399cc887bb | 603 | switch (type) { |
<> | 160:d5399cc887bb | 604 | case IRQ_RX: |
<> | 160:d5399cc887bb | 605 | ier = CAN_IT_FMP0; |
<> | 160:d5399cc887bb | 606 | irq_n = CAN1_IRQ_RX_IRQN; |
<> | 160:d5399cc887bb | 607 | vector = (uint32_t)&CAN1_IRQ_RX_VECT; |
<> | 160:d5399cc887bb | 608 | break; |
<> | 160:d5399cc887bb | 609 | case IRQ_TX: |
<> | 160:d5399cc887bb | 610 | ier = CAN_IT_TME; |
<> | 160:d5399cc887bb | 611 | irq_n = CAN1_IRQ_TX_IRQN; |
<> | 160:d5399cc887bb | 612 | vector = (uint32_t)&CAN1_IRQ_TX_VECT; |
<> | 160:d5399cc887bb | 613 | break; |
<> | 160:d5399cc887bb | 614 | case IRQ_ERROR: |
<> | 160:d5399cc887bb | 615 | ier = CAN_IT_ERR; |
<> | 160:d5399cc887bb | 616 | irq_n = CAN1_IRQ_ERROR_IRQN; |
<> | 160:d5399cc887bb | 617 | vector = (uint32_t)&CAN1_IRQ_ERROR_VECT; |
<> | 160:d5399cc887bb | 618 | break; |
<> | 160:d5399cc887bb | 619 | case IRQ_PASSIVE: |
<> | 160:d5399cc887bb | 620 | ier = CAN_IT_EPV; |
<> | 160:d5399cc887bb | 621 | irq_n = CAN1_IRQ_PASSIVE_IRQN; |
<> | 160:d5399cc887bb | 622 | vector = (uint32_t)&CAN1_IRQ_PASSIVE_VECT; |
<> | 160:d5399cc887bb | 623 | break; |
<> | 160:d5399cc887bb | 624 | case IRQ_BUS: |
<> | 160:d5399cc887bb | 625 | ier = CAN_IT_BOF; |
<> | 160:d5399cc887bb | 626 | irq_n = CAN1_IRQ_BUS_IRQN; |
<> | 160:d5399cc887bb | 627 | vector = (uint32_t)&CAN1_IRQ_BUS_VECT; |
<> | 160:d5399cc887bb | 628 | break; |
<> | 160:d5399cc887bb | 629 | default: |
<> | 160:d5399cc887bb | 630 | return; |
<> | 160:d5399cc887bb | 631 | } |
<> | 160:d5399cc887bb | 632 | } |
AnnaBridge | 178:79309dc6340a | 633 | #if defined(CAN2_BASE) && defined(CAN_2) |
AnnaBridge | 174:b96e65c34a4d | 634 | else if ((CANName) can == CAN_2) { |
<> | 160:d5399cc887bb | 635 | switch (type) { |
<> | 160:d5399cc887bb | 636 | case IRQ_RX: |
<> | 160:d5399cc887bb | 637 | ier = CAN_IT_FMP0; |
<> | 160:d5399cc887bb | 638 | irq_n = CAN2_IRQ_RX_IRQN; |
<> | 160:d5399cc887bb | 639 | vector = (uint32_t)&CAN2_IRQ_RX_VECT; |
<> | 160:d5399cc887bb | 640 | break; |
<> | 160:d5399cc887bb | 641 | case IRQ_TX: |
<> | 160:d5399cc887bb | 642 | ier = CAN_IT_TME; |
<> | 160:d5399cc887bb | 643 | irq_n = CAN2_IRQ_TX_IRQN; |
<> | 160:d5399cc887bb | 644 | vector = (uint32_t)&CAN2_IRQ_TX_VECT; |
<> | 160:d5399cc887bb | 645 | break; |
<> | 160:d5399cc887bb | 646 | case IRQ_ERROR: |
<> | 160:d5399cc887bb | 647 | ier = CAN_IT_ERR; |
<> | 160:d5399cc887bb | 648 | irq_n = CAN2_IRQ_ERROR_IRQN; |
<> | 160:d5399cc887bb | 649 | vector = (uint32_t)&CAN2_IRQ_ERROR_VECT; |
<> | 160:d5399cc887bb | 650 | break; |
<> | 160:d5399cc887bb | 651 | case IRQ_PASSIVE: |
<> | 160:d5399cc887bb | 652 | ier = CAN_IT_EPV; |
<> | 160:d5399cc887bb | 653 | irq_n = CAN2_IRQ_PASSIVE_IRQN; |
<> | 160:d5399cc887bb | 654 | vector = (uint32_t)&CAN2_IRQ_PASSIVE_VECT; |
<> | 160:d5399cc887bb | 655 | break; |
<> | 160:d5399cc887bb | 656 | case IRQ_BUS: |
<> | 160:d5399cc887bb | 657 | ier = CAN_IT_BOF; |
<> | 160:d5399cc887bb | 658 | irq_n = CAN2_IRQ_BUS_IRQN; |
<> | 160:d5399cc887bb | 659 | vector = (uint32_t)&CAN2_IRQ_BUS_VECT; |
<> | 160:d5399cc887bb | 660 | break; |
<> | 160:d5399cc887bb | 661 | default: |
<> | 160:d5399cc887bb | 662 | return; |
<> | 160:d5399cc887bb | 663 | } |
<> | 160:d5399cc887bb | 664 | } |
<> | 160:d5399cc887bb | 665 | #endif |
AnnaBridge | 178:79309dc6340a | 666 | #if defined(CAN3_BASE) && defined(CAN_3) |
AnnaBridge | 178:79309dc6340a | 667 | else if ((CANName) can == CAN_3) { |
AnnaBridge | 178:79309dc6340a | 668 | switch (type) { |
AnnaBridge | 178:79309dc6340a | 669 | case IRQ_RX: |
AnnaBridge | 178:79309dc6340a | 670 | ier = CAN_IT_FMP0; |
AnnaBridge | 178:79309dc6340a | 671 | irq_n = CAN3_IRQ_RX_IRQN; |
AnnaBridge | 178:79309dc6340a | 672 | vector = (uint32_t)&CAN3_IRQ_RX_VECT; |
AnnaBridge | 178:79309dc6340a | 673 | break; |
AnnaBridge | 178:79309dc6340a | 674 | case IRQ_TX: |
AnnaBridge | 178:79309dc6340a | 675 | ier = CAN_IT_TME; |
AnnaBridge | 178:79309dc6340a | 676 | irq_n = CAN3_IRQ_TX_IRQN; |
AnnaBridge | 178:79309dc6340a | 677 | vector = (uint32_t)&CAN3_IRQ_TX_VECT; |
AnnaBridge | 178:79309dc6340a | 678 | break; |
AnnaBridge | 178:79309dc6340a | 679 | case IRQ_ERROR: |
AnnaBridge | 178:79309dc6340a | 680 | ier = CAN_IT_ERR; |
AnnaBridge | 178:79309dc6340a | 681 | irq_n = CAN3_IRQ_ERROR_IRQN; |
AnnaBridge | 178:79309dc6340a | 682 | vector = (uint32_t)&CAN3_IRQ_ERROR_VECT; |
AnnaBridge | 178:79309dc6340a | 683 | break; |
AnnaBridge | 178:79309dc6340a | 684 | case IRQ_PASSIVE: |
AnnaBridge | 178:79309dc6340a | 685 | ier = CAN_IT_EPV; |
AnnaBridge | 178:79309dc6340a | 686 | irq_n = CAN3_IRQ_PASSIVE_IRQN; |
AnnaBridge | 178:79309dc6340a | 687 | vector = (uint32_t)&CAN3_IRQ_PASSIVE_VECT; |
AnnaBridge | 178:79309dc6340a | 688 | break; |
AnnaBridge | 178:79309dc6340a | 689 | case IRQ_BUS: |
AnnaBridge | 178:79309dc6340a | 690 | ier = CAN_IT_BOF; |
AnnaBridge | 178:79309dc6340a | 691 | irq_n = CAN3_IRQ_BUS_IRQN; |
AnnaBridge | 178:79309dc6340a | 692 | vector = (uint32_t)&CAN3_IRQ_BUS_VECT; |
AnnaBridge | 178:79309dc6340a | 693 | break; |
AnnaBridge | 178:79309dc6340a | 694 | default: |
AnnaBridge | 178:79309dc6340a | 695 | return; |
AnnaBridge | 178:79309dc6340a | 696 | } |
AnnaBridge | 178:79309dc6340a | 697 | } |
AnnaBridge | 178:79309dc6340a | 698 | #endif |
<> | 160:d5399cc887bb | 699 | else { |
<> | 160:d5399cc887bb | 700 | return; |
<> | 160:d5399cc887bb | 701 | } |
<> | 160:d5399cc887bb | 702 | |
<> | 160:d5399cc887bb | 703 | if (enable) { |
<> | 160:d5399cc887bb | 704 | can->IER |= ier; |
<> | 160:d5399cc887bb | 705 | } else { |
<> | 160:d5399cc887bb | 706 | can->IER &= ~ier; |
<> | 160:d5399cc887bb | 707 | } |
<> | 160:d5399cc887bb | 708 | |
<> | 160:d5399cc887bb | 709 | NVIC_SetVector(irq_n, vector); |
<> | 160:d5399cc887bb | 710 | NVIC_EnableIRQ(irq_n); |
<> | 160:d5399cc887bb | 711 | } |
<> | 160:d5399cc887bb | 712 | |
<> | 160:d5399cc887bb | 713 | #endif // DEVICE_CAN |
<> | 160:d5399cc887bb | 714 |