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