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