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