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