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