mbed-os
Fork of mbed-os by
targets/TARGET_STM/TARGET_STM32F7/can_api.c@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* mbed Microcontroller Library |
elessair | 0:f269e3021894 | 2 | * Copyright (c) 2006-2016 ARM Limited |
elessair | 0:f269e3021894 | 3 | * |
elessair | 0:f269e3021894 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
elessair | 0:f269e3021894 | 5 | * you may not use this file except in compliance with the License. |
elessair | 0:f269e3021894 | 6 | * You may obtain a copy of the License at |
elessair | 0:f269e3021894 | 7 | * |
elessair | 0:f269e3021894 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
elessair | 0:f269e3021894 | 9 | * |
elessair | 0:f269e3021894 | 10 | * Unless required by applicable law or agreed to in writing, software |
elessair | 0:f269e3021894 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
elessair | 0:f269e3021894 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elessair | 0:f269e3021894 | 13 | * See the License for the specific language governing permissions and |
elessair | 0:f269e3021894 | 14 | * limitations under the License. |
elessair | 0:f269e3021894 | 15 | */ |
elessair | 0:f269e3021894 | 16 | #include "can_api.h" |
elessair | 0:f269e3021894 | 17 | |
elessair | 0:f269e3021894 | 18 | #if DEVICE_CAN |
elessair | 0:f269e3021894 | 19 | |
elessair | 0:f269e3021894 | 20 | #include "cmsis.h" |
elessair | 0:f269e3021894 | 21 | #include "pinmap.h" |
elessair | 0:f269e3021894 | 22 | #include "PeripheralPins.h" |
elessair | 0:f269e3021894 | 23 | #include "mbed_error.h" |
elessair | 0:f269e3021894 | 24 | #include <math.h> |
elessair | 0:f269e3021894 | 25 | #include <string.h> |
elessair | 0:f269e3021894 | 26 | |
elessair | 0:f269e3021894 | 27 | #define CAN_NUM 2 |
elessair | 0:f269e3021894 | 28 | static CAN_HandleTypeDef CanHandle; |
elessair | 0:f269e3021894 | 29 | static uint32_t can_irq_ids[CAN_NUM] = {0}; |
elessair | 0:f269e3021894 | 30 | static can_irq_handler irq_handler; |
elessair | 0:f269e3021894 | 31 | |
elessair | 0:f269e3021894 | 32 | void can_init(can_t *obj, PinName rd, PinName td) |
elessair | 0:f269e3021894 | 33 | { |
elessair | 0:f269e3021894 | 34 | uint32_t filter_number; |
elessair | 0:f269e3021894 | 35 | CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD); |
elessair | 0:f269e3021894 | 36 | CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); |
elessair | 0:f269e3021894 | 37 | obj->can = (CANName)pinmap_merge(can_rd, can_td); |
elessair | 0:f269e3021894 | 38 | MBED_ASSERT((int)obj->can != NC); |
elessair | 0:f269e3021894 | 39 | |
elessair | 0:f269e3021894 | 40 | if(obj->can == CAN_1) { |
elessair | 0:f269e3021894 | 41 | __HAL_RCC_CAN1_CLK_ENABLE(); |
elessair | 0:f269e3021894 | 42 | obj->index = 0; |
elessair | 0:f269e3021894 | 43 | } else { |
elessair | 0:f269e3021894 | 44 | __HAL_RCC_CAN2_CLK_ENABLE(); |
elessair | 0:f269e3021894 | 45 | obj->index = 1; |
elessair | 0:f269e3021894 | 46 | } |
elessair | 0:f269e3021894 | 47 | |
elessair | 0:f269e3021894 | 48 | // Configure the CAN pins |
elessair | 0:f269e3021894 | 49 | pinmap_pinout(rd, PinMap_CAN_RD); |
elessair | 0:f269e3021894 | 50 | pinmap_pinout(td, PinMap_CAN_TD); |
elessair | 0:f269e3021894 | 51 | if (rd != NC) { |
elessair | 0:f269e3021894 | 52 | pin_mode(rd, PullUp); |
elessair | 0:f269e3021894 | 53 | } |
elessair | 0:f269e3021894 | 54 | if (td != NC) { |
elessair | 0:f269e3021894 | 55 | pin_mode(td, PullUp); |
elessair | 0:f269e3021894 | 56 | } |
elessair | 0:f269e3021894 | 57 | |
elessair | 0:f269e3021894 | 58 | CanHandle.Instance = (CAN_TypeDef *)(obj->can); |
elessair | 0:f269e3021894 | 59 | |
elessair | 0:f269e3021894 | 60 | CanHandle.Init.TTCM = DISABLE; |
elessair | 0:f269e3021894 | 61 | CanHandle.Init.ABOM = DISABLE; |
elessair | 0:f269e3021894 | 62 | CanHandle.Init.AWUM = DISABLE; |
elessair | 0:f269e3021894 | 63 | CanHandle.Init.NART = DISABLE; |
elessair | 0:f269e3021894 | 64 | CanHandle.Init.RFLM = DISABLE; |
elessair | 0:f269e3021894 | 65 | CanHandle.Init.TXFP = DISABLE; |
elessair | 0:f269e3021894 | 66 | CanHandle.Init.Mode = CAN_MODE_NORMAL; |
elessair | 0:f269e3021894 | 67 | CanHandle.Init.SJW = CAN_SJW_1TQ; |
elessair | 0:f269e3021894 | 68 | CanHandle.Init.BS1 = CAN_BS1_6TQ; |
elessair | 0:f269e3021894 | 69 | CanHandle.Init.BS2 = CAN_BS2_8TQ; |
elessair | 0:f269e3021894 | 70 | CanHandle.Init.Prescaler = 2; |
elessair | 0:f269e3021894 | 71 | |
elessair | 0:f269e3021894 | 72 | if (HAL_CAN_Init(&CanHandle) != HAL_OK) { |
elessair | 0:f269e3021894 | 73 | error("Cannot initialize CAN"); |
elessair | 0:f269e3021894 | 74 | } |
elessair | 0:f269e3021894 | 75 | |
elessair | 0:f269e3021894 | 76 | filter_number = (obj->can == CAN_1) ? 0 : 14; |
elessair | 0:f269e3021894 | 77 | |
elessair | 0:f269e3021894 | 78 | // Set initial CAN frequency to 100kb/s |
elessair | 0:f269e3021894 | 79 | can_frequency(obj, 100000); |
elessair | 0:f269e3021894 | 80 | |
elessair | 0:f269e3021894 | 81 | can_filter(obj, 0, 0, CANStandard, filter_number); |
elessair | 0:f269e3021894 | 82 | } |
elessair | 0:f269e3021894 | 83 | |
elessair | 0:f269e3021894 | 84 | void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id) |
elessair | 0:f269e3021894 | 85 | { |
elessair | 0:f269e3021894 | 86 | irq_handler = handler; |
elessair | 0:f269e3021894 | 87 | can_irq_ids[obj->index] = id; |
elessair | 0:f269e3021894 | 88 | } |
elessair | 0:f269e3021894 | 89 | |
elessair | 0:f269e3021894 | 90 | void can_irq_free(can_t *obj) |
elessair | 0:f269e3021894 | 91 | { |
elessair | 0:f269e3021894 | 92 | CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); |
elessair | 0:f269e3021894 | 93 | |
elessair | 0:f269e3021894 | 94 | can->IER &= ~(CAN_IT_FMP0 | CAN_IT_FMP1 | CAN_IT_TME | \ |
elessair | 0:f269e3021894 | 95 | CAN_IT_ERR | CAN_IT_EPV | CAN_IT_BOF); |
elessair | 0:f269e3021894 | 96 | can_irq_ids[obj->can] = 0; |
elessair | 0:f269e3021894 | 97 | } |
elessair | 0:f269e3021894 | 98 | |
elessair | 0:f269e3021894 | 99 | void can_free(can_t *obj) |
elessair | 0:f269e3021894 | 100 | { |
elessair | 0:f269e3021894 | 101 | // Reset CAN and disable clock |
elessair | 0:f269e3021894 | 102 | if (obj->can == CAN_1) { |
elessair | 0:f269e3021894 | 103 | __HAL_RCC_CAN1_FORCE_RESET(); |
elessair | 0:f269e3021894 | 104 | __HAL_RCC_CAN1_RELEASE_RESET(); |
elessair | 0:f269e3021894 | 105 | __HAL_RCC_CAN1_CLK_DISABLE(); |
elessair | 0:f269e3021894 | 106 | } |
elessair | 0:f269e3021894 | 107 | |
elessair | 0:f269e3021894 | 108 | if (obj->can == CAN_2) { |
elessair | 0:f269e3021894 | 109 | __HAL_RCC_CAN2_FORCE_RESET(); |
elessair | 0:f269e3021894 | 110 | __HAL_RCC_CAN2_RELEASE_RESET(); |
elessair | 0:f269e3021894 | 111 | __HAL_RCC_CAN2_CLK_DISABLE(); |
elessair | 0:f269e3021894 | 112 | } |
elessair | 0:f269e3021894 | 113 | } |
elessair | 0:f269e3021894 | 114 | |
elessair | 0:f269e3021894 | 115 | // The following table is used to program bit_timing. It is an adjustment of the sample |
elessair | 0:f269e3021894 | 116 | // point by synchronizing on the start-bit edge and resynchronizing on the following edges. |
elessair | 0:f269e3021894 | 117 | // This table has the sampling points as close to 75% as possible (most commonly used). |
elessair | 0:f269e3021894 | 118 | // The first value is TSEG1, the second TSEG2. |
elessair | 0:f269e3021894 | 119 | static const int timing_pts[23][2] = { |
elessair | 0:f269e3021894 | 120 | {0x0, 0x0}, // 2, 50% |
elessair | 0:f269e3021894 | 121 | {0x1, 0x0}, // 3, 67% |
elessair | 0:f269e3021894 | 122 | {0x2, 0x0}, // 4, 75% |
elessair | 0:f269e3021894 | 123 | {0x3, 0x0}, // 5, 80% |
elessair | 0:f269e3021894 | 124 | {0x3, 0x1}, // 6, 67% |
elessair | 0:f269e3021894 | 125 | {0x4, 0x1}, // 7, 71% |
elessair | 0:f269e3021894 | 126 | {0x5, 0x1}, // 8, 75% |
elessair | 0:f269e3021894 | 127 | {0x6, 0x1}, // 9, 78% |
elessair | 0:f269e3021894 | 128 | {0x6, 0x2}, // 10, 70% |
elessair | 0:f269e3021894 | 129 | {0x7, 0x2}, // 11, 73% |
elessair | 0:f269e3021894 | 130 | {0x8, 0x2}, // 12, 75% |
elessair | 0:f269e3021894 | 131 | {0x9, 0x2}, // 13, 77% |
elessair | 0:f269e3021894 | 132 | {0x9, 0x3}, // 14, 71% |
elessair | 0:f269e3021894 | 133 | {0xA, 0x3}, // 15, 73% |
elessair | 0:f269e3021894 | 134 | {0xB, 0x3}, // 16, 75% |
elessair | 0:f269e3021894 | 135 | {0xC, 0x3}, // 17, 76% |
elessair | 0:f269e3021894 | 136 | {0xD, 0x3}, // 18, 78% |
elessair | 0:f269e3021894 | 137 | {0xD, 0x4}, // 19, 74% |
elessair | 0:f269e3021894 | 138 | {0xE, 0x4}, // 20, 75% |
elessair | 0:f269e3021894 | 139 | {0xF, 0x4}, // 21, 76% |
elessair | 0:f269e3021894 | 140 | {0xF, 0x5}, // 22, 73% |
elessair | 0:f269e3021894 | 141 | {0xF, 0x6}, // 23, 70% |
elessair | 0:f269e3021894 | 142 | {0xF, 0x7}, // 24, 67% |
elessair | 0:f269e3021894 | 143 | }; |
elessair | 0:f269e3021894 | 144 | |
elessair | 0:f269e3021894 | 145 | static unsigned int can_speed(unsigned int pclk, unsigned int cclk, unsigned char psjw) |
elessair | 0:f269e3021894 | 146 | { |
elessair | 0:f269e3021894 | 147 | uint32_t btr; |
elessair | 0:f269e3021894 | 148 | uint16_t brp = 0; |
elessair | 0:f269e3021894 | 149 | uint32_t calcbit; |
elessair | 0:f269e3021894 | 150 | uint32_t bitwidth; |
elessair | 0:f269e3021894 | 151 | int hit = 0; |
elessair | 0:f269e3021894 | 152 | int bits; |
elessair | 0:f269e3021894 | 153 | |
elessair | 0:f269e3021894 | 154 | bitwidth = (pclk / cclk); |
elessair | 0:f269e3021894 | 155 | |
elessair | 0:f269e3021894 | 156 | brp = bitwidth / 0x18; |
elessair | 0:f269e3021894 | 157 | while ((!hit) && (brp < bitwidth / 4)) { |
elessair | 0:f269e3021894 | 158 | brp++; |
elessair | 0:f269e3021894 | 159 | for (bits = 22; bits > 0; bits--) { |
elessair | 0:f269e3021894 | 160 | calcbit = (bits + 3) * (brp + 1); |
elessair | 0:f269e3021894 | 161 | if (calcbit == bitwidth) { |
elessair | 0:f269e3021894 | 162 | hit = 1; |
elessair | 0:f269e3021894 | 163 | break; |
elessair | 0:f269e3021894 | 164 | } |
elessair | 0:f269e3021894 | 165 | } |
elessair | 0:f269e3021894 | 166 | } |
elessair | 0:f269e3021894 | 167 | |
elessair | 0:f269e3021894 | 168 | if (hit) { |
elessair | 0:f269e3021894 | 169 | btr = ((timing_pts[bits][1] << 20) & 0x00700000) |
elessair | 0:f269e3021894 | 170 | | ((timing_pts[bits][0] << 16) & 0x000F0000) |
elessair | 0:f269e3021894 | 171 | | ((psjw << 24) & 0x0000C000) |
elessair | 0:f269e3021894 | 172 | | ((brp << 0) & 0x000003FF); |
elessair | 0:f269e3021894 | 173 | } else { |
elessair | 0:f269e3021894 | 174 | btr = 0xFFFFFFFF; |
elessair | 0:f269e3021894 | 175 | } |
elessair | 0:f269e3021894 | 176 | |
elessair | 0:f269e3021894 | 177 | return btr; |
elessair | 0:f269e3021894 | 178 | |
elessair | 0:f269e3021894 | 179 | } |
elessair | 0:f269e3021894 | 180 | |
elessair | 0:f269e3021894 | 181 | int can_frequency(can_t *obj, int f) |
elessair | 0:f269e3021894 | 182 | { |
elessair | 0:f269e3021894 | 183 | int pclk = HAL_RCC_GetPCLK1Freq(); |
elessair | 0:f269e3021894 | 184 | int btr = can_speed(pclk, (unsigned int)f, 1); |
elessair | 0:f269e3021894 | 185 | CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); |
elessair | 0:f269e3021894 | 186 | |
elessair | 0:f269e3021894 | 187 | if (btr > 0) { |
elessair | 0:f269e3021894 | 188 | can->MCR |= CAN_MCR_INRQ ; |
elessair | 0:f269e3021894 | 189 | while((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) { |
elessair | 0:f269e3021894 | 190 | } |
elessair | 0:f269e3021894 | 191 | can->BTR = btr; |
elessair | 0:f269e3021894 | 192 | can->MCR &= ~(uint32_t)CAN_MCR_INRQ; |
elessair | 0:f269e3021894 | 193 | while((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) { |
elessair | 0:f269e3021894 | 194 | } |
elessair | 0:f269e3021894 | 195 | return 1; |
elessair | 0:f269e3021894 | 196 | } else { |
elessair | 0:f269e3021894 | 197 | return 0; |
elessair | 0:f269e3021894 | 198 | } |
elessair | 0:f269e3021894 | 199 | } |
elessair | 0:f269e3021894 | 200 | |
elessair | 0:f269e3021894 | 201 | int can_write(can_t *obj, CAN_Message msg, int cc) |
elessair | 0:f269e3021894 | 202 | { |
elessair | 0:f269e3021894 | 203 | uint32_t transmitmailbox = 5; |
elessair | 0:f269e3021894 | 204 | CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); |
elessair | 0:f269e3021894 | 205 | |
elessair | 0:f269e3021894 | 206 | /* Select one empty transmit mailbox */ |
elessair | 0:f269e3021894 | 207 | if ((can->TSR&CAN_TSR_TME0) == CAN_TSR_TME0) { |
elessair | 0:f269e3021894 | 208 | transmitmailbox = 0; |
elessair | 0:f269e3021894 | 209 | } else if ((can->TSR&CAN_TSR_TME1) == CAN_TSR_TME1) { |
elessair | 0:f269e3021894 | 210 | transmitmailbox = 1; |
elessair | 0:f269e3021894 | 211 | } else if ((can->TSR&CAN_TSR_TME2) == CAN_TSR_TME2) { |
elessair | 0:f269e3021894 | 212 | transmitmailbox = 2; |
elessair | 0:f269e3021894 | 213 | } else { |
elessair | 0:f269e3021894 | 214 | transmitmailbox = CAN_TXSTATUS_NOMAILBOX; |
elessair | 0:f269e3021894 | 215 | } |
elessair | 0:f269e3021894 | 216 | |
elessair | 0:f269e3021894 | 217 | if (transmitmailbox != CAN_TXSTATUS_NOMAILBOX) { |
elessair | 0:f269e3021894 | 218 | can->sTxMailBox[transmitmailbox].TIR &= CAN_TI0R_TXRQ; |
elessair | 0:f269e3021894 | 219 | if (!(msg.format)) |
elessair | 0:f269e3021894 | 220 | { |
elessair | 0:f269e3021894 | 221 | can->sTxMailBox[transmitmailbox].TIR |= ((msg.id << 21) | msg.type); |
elessair | 0:f269e3021894 | 222 | } |
elessair | 0:f269e3021894 | 223 | else |
elessair | 0:f269e3021894 | 224 | { |
elessair | 0:f269e3021894 | 225 | can->sTxMailBox[transmitmailbox].TIR |= ((msg.id << 3) | CAN_ID_EXT | msg.type); |
elessair | 0:f269e3021894 | 226 | } |
elessair | 0:f269e3021894 | 227 | |
elessair | 0:f269e3021894 | 228 | /* Set up the DLC */ |
elessair | 0:f269e3021894 | 229 | can->sTxMailBox[transmitmailbox].TDTR &= (uint32_t)0xFFFFFFF0; |
elessair | 0:f269e3021894 | 230 | can->sTxMailBox[transmitmailbox].TDTR |= (msg.len & (uint8_t)0x0000000F); |
elessair | 0:f269e3021894 | 231 | |
elessair | 0:f269e3021894 | 232 | /* Set up the data field */ |
elessair | 0:f269e3021894 | 233 | can->sTxMailBox[transmitmailbox].TDLR = (((uint32_t)msg.data[3] << 24) | |
elessair | 0:f269e3021894 | 234 | ((uint32_t)msg.data[2] << 16) | |
elessair | 0:f269e3021894 | 235 | ((uint32_t)msg.data[1] << 8) | |
elessair | 0:f269e3021894 | 236 | ((uint32_t)msg.data[0])); |
elessair | 0:f269e3021894 | 237 | can->sTxMailBox[transmitmailbox].TDHR = (((uint32_t)msg.data[7] << 24) | |
elessair | 0:f269e3021894 | 238 | ((uint32_t)msg.data[6] << 16) | |
elessair | 0:f269e3021894 | 239 | ((uint32_t)msg.data[5] << 8) | |
elessair | 0:f269e3021894 | 240 | ((uint32_t)msg.data[4])); |
elessair | 0:f269e3021894 | 241 | /* Request transmission */ |
elessair | 0:f269e3021894 | 242 | can->sTxMailBox[transmitmailbox].TIR |= CAN_TI0R_TXRQ; |
elessair | 0:f269e3021894 | 243 | } |
elessair | 0:f269e3021894 | 244 | |
elessair | 0:f269e3021894 | 245 | return 1; |
elessair | 0:f269e3021894 | 246 | } |
elessair | 0:f269e3021894 | 247 | |
elessair | 0:f269e3021894 | 248 | int can_read(can_t *obj, CAN_Message *msg, int handle) |
elessair | 0:f269e3021894 | 249 | { |
elessair | 0:f269e3021894 | 250 | //handle is the FIFO number |
elessair | 0:f269e3021894 | 251 | |
elessair | 0:f269e3021894 | 252 | CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); |
elessair | 0:f269e3021894 | 253 | |
elessair | 0:f269e3021894 | 254 | /* Get the Id */ |
elessair | 0:f269e3021894 | 255 | msg->format = (CANFormat)((uint8_t)0x04 & can->sFIFOMailBox[handle].RIR); |
elessair | 0:f269e3021894 | 256 | if (!msg->format) { |
elessair | 0:f269e3021894 | 257 | msg->id = (uint32_t)0x000007FF & (can->sFIFOMailBox[handle].RIR >> 21); |
elessair | 0:f269e3021894 | 258 | } else { |
elessair | 0:f269e3021894 | 259 | msg->id = (uint32_t)0x1FFFFFFF & (can->sFIFOMailBox[handle].RIR >> 3); |
elessair | 0:f269e3021894 | 260 | } |
elessair | 0:f269e3021894 | 261 | |
elessair | 0:f269e3021894 | 262 | msg->type = (CANType)((uint8_t)0x02 & can->sFIFOMailBox[handle].RIR); |
elessair | 0:f269e3021894 | 263 | /* Get the DLC */ |
elessair | 0:f269e3021894 | 264 | msg->len = (uint8_t)0x0F & can->sFIFOMailBox[handle].RDTR; |
elessair | 0:f269e3021894 | 265 | // /* Get the FMI */ |
elessair | 0:f269e3021894 | 266 | // msg->FMI = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDTR >> 8); |
elessair | 0:f269e3021894 | 267 | /* Get the data field */ |
elessair | 0:f269e3021894 | 268 | msg->data[0] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDLR; |
elessair | 0:f269e3021894 | 269 | msg->data[1] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 8); |
elessair | 0:f269e3021894 | 270 | msg->data[2] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 16); |
elessair | 0:f269e3021894 | 271 | msg->data[3] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 24); |
elessair | 0:f269e3021894 | 272 | msg->data[4] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDHR; |
elessair | 0:f269e3021894 | 273 | msg->data[5] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 8); |
elessair | 0:f269e3021894 | 274 | msg->data[6] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 16); |
elessair | 0:f269e3021894 | 275 | msg->data[7] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 24); |
elessair | 0:f269e3021894 | 276 | |
elessair | 0:f269e3021894 | 277 | /* Release the FIFO */ |
elessair | 0:f269e3021894 | 278 | if(handle == CAN_FIFO0) { |
elessair | 0:f269e3021894 | 279 | /* Release FIFO0 */ |
elessair | 0:f269e3021894 | 280 | can->RF0R = CAN_RF0R_RFOM0; |
elessair | 0:f269e3021894 | 281 | } else { /* FIFONumber == CAN_FIFO1 */ |
elessair | 0:f269e3021894 | 282 | /* Release FIFO1 */ |
elessair | 0:f269e3021894 | 283 | can->RF1R = CAN_RF1R_RFOM1; |
elessair | 0:f269e3021894 | 284 | } |
elessair | 0:f269e3021894 | 285 | |
elessair | 0:f269e3021894 | 286 | return 1; |
elessair | 0:f269e3021894 | 287 | } |
elessair | 0:f269e3021894 | 288 | |
elessair | 0:f269e3021894 | 289 | void can_reset(can_t *obj) |
elessair | 0:f269e3021894 | 290 | { |
elessair | 0:f269e3021894 | 291 | CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); |
elessair | 0:f269e3021894 | 292 | |
elessair | 0:f269e3021894 | 293 | can->MCR |= CAN_MCR_RESET; |
elessair | 0:f269e3021894 | 294 | can->ESR = 0x0; |
elessair | 0:f269e3021894 | 295 | } |
elessair | 0:f269e3021894 | 296 | |
elessair | 0:f269e3021894 | 297 | unsigned char can_rderror(can_t *obj) |
elessair | 0:f269e3021894 | 298 | { |
elessair | 0:f269e3021894 | 299 | CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); |
elessair | 0:f269e3021894 | 300 | return (can->ESR >> 24) & 0xFF; |
elessair | 0:f269e3021894 | 301 | } |
elessair | 0:f269e3021894 | 302 | |
elessair | 0:f269e3021894 | 303 | unsigned char can_tderror(can_t *obj) |
elessair | 0:f269e3021894 | 304 | { |
elessair | 0:f269e3021894 | 305 | CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); |
elessair | 0:f269e3021894 | 306 | return (can->ESR >> 16) & 0xFF; |
elessair | 0:f269e3021894 | 307 | } |
elessair | 0:f269e3021894 | 308 | |
elessair | 0:f269e3021894 | 309 | void can_monitor(can_t *obj, int silent) |
elessair | 0:f269e3021894 | 310 | { |
elessair | 0:f269e3021894 | 311 | CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); |
elessair | 0:f269e3021894 | 312 | |
elessair | 0:f269e3021894 | 313 | can->MCR |= CAN_MCR_INRQ ; |
elessair | 0:f269e3021894 | 314 | while((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) { |
elessair | 0:f269e3021894 | 315 | } |
elessair | 0:f269e3021894 | 316 | if (silent) { |
elessair | 0:f269e3021894 | 317 | can->BTR |= ((uint32_t)1 << 31); |
elessair | 0:f269e3021894 | 318 | } else { |
elessair | 0:f269e3021894 | 319 | can->BTR &= ~((uint32_t)1 << 31); |
elessair | 0:f269e3021894 | 320 | } |
elessair | 0:f269e3021894 | 321 | can->MCR &= ~(uint32_t)CAN_MCR_INRQ; |
elessair | 0:f269e3021894 | 322 | while((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) { |
elessair | 0:f269e3021894 | 323 | } |
elessair | 0:f269e3021894 | 324 | } |
elessair | 0:f269e3021894 | 325 | |
elessair | 0:f269e3021894 | 326 | int can_mode(can_t *obj, CanMode mode) |
elessair | 0:f269e3021894 | 327 | { |
elessair | 0:f269e3021894 | 328 | int success = 0; |
elessair | 0:f269e3021894 | 329 | CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); |
elessair | 0:f269e3021894 | 330 | can->MCR |= CAN_MCR_INRQ ; |
elessair | 0:f269e3021894 | 331 | while((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) { |
elessair | 0:f269e3021894 | 332 | } |
elessair | 0:f269e3021894 | 333 | switch (mode) { |
elessair | 0:f269e3021894 | 334 | case MODE_NORMAL: |
elessair | 0:f269e3021894 | 335 | can->BTR &= ~(CAN_BTR_SILM | CAN_BTR_LBKM); |
elessair | 0:f269e3021894 | 336 | success = 1; |
elessair | 0:f269e3021894 | 337 | break; |
elessair | 0:f269e3021894 | 338 | case MODE_SILENT: |
elessair | 0:f269e3021894 | 339 | can->BTR |= CAN_BTR_SILM; |
elessair | 0:f269e3021894 | 340 | can->BTR &= ~CAN_BTR_LBKM; |
elessair | 0:f269e3021894 | 341 | success = 1; |
elessair | 0:f269e3021894 | 342 | break; |
elessair | 0:f269e3021894 | 343 | case MODE_TEST_GLOBAL: |
elessair | 0:f269e3021894 | 344 | case MODE_TEST_LOCAL: |
elessair | 0:f269e3021894 | 345 | can->BTR |= CAN_BTR_LBKM; |
elessair | 0:f269e3021894 | 346 | can->BTR &= ~CAN_BTR_SILM; |
elessair | 0:f269e3021894 | 347 | success = 1; |
elessair | 0:f269e3021894 | 348 | break; |
elessair | 0:f269e3021894 | 349 | case MODE_TEST_SILENT: |
elessair | 0:f269e3021894 | 350 | can->BTR |= (CAN_BTR_SILM | CAN_BTR_LBKM); |
elessair | 0:f269e3021894 | 351 | success = 1; |
elessair | 0:f269e3021894 | 352 | break; |
elessair | 0:f269e3021894 | 353 | default: |
elessair | 0:f269e3021894 | 354 | success = 0; |
elessair | 0:f269e3021894 | 355 | break; |
elessair | 0:f269e3021894 | 356 | } |
elessair | 0:f269e3021894 | 357 | can->MCR &= ~(uint32_t)CAN_MCR_INRQ; |
elessair | 0:f269e3021894 | 358 | while((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) { |
elessair | 0:f269e3021894 | 359 | } |
elessair | 0:f269e3021894 | 360 | return success; |
elessair | 0:f269e3021894 | 361 | } |
elessair | 0:f269e3021894 | 362 | |
elessair | 0:f269e3021894 | 363 | int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) |
elessair | 0:f269e3021894 | 364 | { |
elessair | 0:f269e3021894 | 365 | CanHandle.Instance = (CAN_TypeDef *)(obj->can); |
elessair | 0:f269e3021894 | 366 | CAN_FilterConfTypeDef sFilterConfig; |
elessair | 0:f269e3021894 | 367 | |
elessair | 0:f269e3021894 | 368 | sFilterConfig.FilterNumber = handle; |
elessair | 0:f269e3021894 | 369 | sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK; |
elessair | 0:f269e3021894 | 370 | sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT; |
elessair | 0:f269e3021894 | 371 | sFilterConfig.FilterIdHigh = (uint8_t) (id >> 8); |
elessair | 0:f269e3021894 | 372 | sFilterConfig.FilterIdLow = (uint8_t) id; |
elessair | 0:f269e3021894 | 373 | sFilterConfig.FilterMaskIdHigh = (uint8_t) (mask >> 8); |
elessair | 0:f269e3021894 | 374 | sFilterConfig.FilterMaskIdLow = (uint8_t) mask; |
elessair | 0:f269e3021894 | 375 | sFilterConfig.FilterFIFOAssignment = 0; |
elessair | 0:f269e3021894 | 376 | sFilterConfig.FilterActivation = ENABLE; |
elessair | 0:f269e3021894 | 377 | sFilterConfig.BankNumber = 14; |
elessair | 0:f269e3021894 | 378 | |
elessair | 0:f269e3021894 | 379 | HAL_CAN_ConfigFilter(&CanHandle, &sFilterConfig); |
elessair | 0:f269e3021894 | 380 | |
elessair | 0:f269e3021894 | 381 | return 0; |
elessair | 0:f269e3021894 | 382 | } |
elessair | 0:f269e3021894 | 383 | |
elessair | 0:f269e3021894 | 384 | static void can_irq(CANName name, int id) |
elessair | 0:f269e3021894 | 385 | { |
elessair | 0:f269e3021894 | 386 | uint32_t tmp1 = 0, tmp2 = 0, tmp3 = 0; |
elessair | 0:f269e3021894 | 387 | CanHandle.Instance = (CAN_TypeDef *)name; |
elessair | 0:f269e3021894 | 388 | |
elessair | 0:f269e3021894 | 389 | if(__HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_TME)) { |
elessair | 0:f269e3021894 | 390 | tmp1 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_0); |
elessair | 0:f269e3021894 | 391 | tmp2 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_1); |
elessair | 0:f269e3021894 | 392 | tmp3 = __HAL_CAN_TRANSMIT_STATUS(&CanHandle, CAN_TXMAILBOX_2); |
elessair | 0:f269e3021894 | 393 | if(tmp1 || tmp2 || tmp3) |
elessair | 0:f269e3021894 | 394 | { |
elessair | 0:f269e3021894 | 395 | irq_handler(can_irq_ids[id], IRQ_TX); |
elessair | 0:f269e3021894 | 396 | } |
elessair | 0:f269e3021894 | 397 | } |
elessair | 0:f269e3021894 | 398 | |
elessair | 0:f269e3021894 | 399 | tmp1 = __HAL_CAN_MSG_PENDING(&CanHandle, CAN_FIFO0); |
elessair | 0:f269e3021894 | 400 | tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_FMP0); |
elessair | 0:f269e3021894 | 401 | |
elessair | 0:f269e3021894 | 402 | if((tmp1 != 0) && tmp2) { |
elessair | 0:f269e3021894 | 403 | irq_handler(can_irq_ids[id], IRQ_RX); |
elessair | 0:f269e3021894 | 404 | } |
elessair | 0:f269e3021894 | 405 | |
elessair | 0:f269e3021894 | 406 | tmp1 = __HAL_CAN_GET_FLAG(&CanHandle, CAN_FLAG_EPV); |
elessair | 0:f269e3021894 | 407 | tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_EPV); |
elessair | 0:f269e3021894 | 408 | tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR); |
elessair | 0:f269e3021894 | 409 | |
elessair | 0:f269e3021894 | 410 | if(tmp1 && tmp2 && tmp3) { |
elessair | 0:f269e3021894 | 411 | irq_handler(can_irq_ids[id], IRQ_PASSIVE); |
elessair | 0:f269e3021894 | 412 | } |
elessair | 0:f269e3021894 | 413 | |
elessair | 0:f269e3021894 | 414 | tmp1 = __HAL_CAN_GET_FLAG(&CanHandle, CAN_FLAG_BOF); |
elessair | 0:f269e3021894 | 415 | tmp2 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_BOF); |
elessair | 0:f269e3021894 | 416 | tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR); |
elessair | 0:f269e3021894 | 417 | if(tmp1 && tmp2 && tmp3) { |
elessair | 0:f269e3021894 | 418 | irq_handler(can_irq_ids[id], IRQ_BUS); |
elessair | 0:f269e3021894 | 419 | } |
elessair | 0:f269e3021894 | 420 | |
elessair | 0:f269e3021894 | 421 | tmp3 = __HAL_CAN_GET_IT_SOURCE(&CanHandle, CAN_IT_ERR); |
elessair | 0:f269e3021894 | 422 | if(tmp1 && tmp2 && tmp3) { |
elessair | 0:f269e3021894 | 423 | irq_handler(can_irq_ids[id], IRQ_ERROR); |
elessair | 0:f269e3021894 | 424 | } |
elessair | 0:f269e3021894 | 425 | } |
elessair | 0:f269e3021894 | 426 | |
elessair | 0:f269e3021894 | 427 | void CAN1_RX0_IRQHandler(void) |
elessair | 0:f269e3021894 | 428 | { |
elessair | 0:f269e3021894 | 429 | can_irq(CAN_1, 0); |
elessair | 0:f269e3021894 | 430 | } |
elessair | 0:f269e3021894 | 431 | |
elessair | 0:f269e3021894 | 432 | void CAN1_TX_IRQHandler(void) |
elessair | 0:f269e3021894 | 433 | { |
elessair | 0:f269e3021894 | 434 | can_irq(CAN_1, 0); |
elessair | 0:f269e3021894 | 435 | } |
elessair | 0:f269e3021894 | 436 | |
elessair | 0:f269e3021894 | 437 | void CAN1_SCE_IRQHandler(void) |
elessair | 0:f269e3021894 | 438 | { |
elessair | 0:f269e3021894 | 439 | can_irq(CAN_1, 0); |
elessair | 0:f269e3021894 | 440 | } |
elessair | 0:f269e3021894 | 441 | |
elessair | 0:f269e3021894 | 442 | void CAN2_RX0_IRQHandler(void) |
elessair | 0:f269e3021894 | 443 | { |
elessair | 0:f269e3021894 | 444 | can_irq(CAN_2, 1); |
elessair | 0:f269e3021894 | 445 | } |
elessair | 0:f269e3021894 | 446 | |
elessair | 0:f269e3021894 | 447 | void CAN2_TX_IRQHandler(void) |
elessair | 0:f269e3021894 | 448 | { |
elessair | 0:f269e3021894 | 449 | can_irq(CAN_2, 1); |
elessair | 0:f269e3021894 | 450 | } |
elessair | 0:f269e3021894 | 451 | |
elessair | 0:f269e3021894 | 452 | void CAN2_SCE_IRQHandler(void) |
elessair | 0:f269e3021894 | 453 | { |
elessair | 0:f269e3021894 | 454 | can_irq(CAN_2, 1); |
elessair | 0:f269e3021894 | 455 | } |
elessair | 0:f269e3021894 | 456 | |
elessair | 0:f269e3021894 | 457 | void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable) |
elessair | 0:f269e3021894 | 458 | { |
elessair | 0:f269e3021894 | 459 | |
elessair | 0:f269e3021894 | 460 | CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); |
elessair | 0:f269e3021894 | 461 | IRQn_Type irq_n = (IRQn_Type)0; |
elessair | 0:f269e3021894 | 462 | uint32_t vector = 0; |
elessair | 0:f269e3021894 | 463 | uint32_t ier; |
elessair | 0:f269e3021894 | 464 | |
elessair | 0:f269e3021894 | 465 | if(obj->can == CAN_1) { |
elessair | 0:f269e3021894 | 466 | switch (type) { |
elessair | 0:f269e3021894 | 467 | case IRQ_RX: |
elessair | 0:f269e3021894 | 468 | ier = CAN_IT_FMP0; |
elessair | 0:f269e3021894 | 469 | irq_n = CAN1_RX0_IRQn; |
elessair | 0:f269e3021894 | 470 | vector = (uint32_t)&CAN1_RX0_IRQHandler; |
elessair | 0:f269e3021894 | 471 | break; |
elessair | 0:f269e3021894 | 472 | case IRQ_TX: |
elessair | 0:f269e3021894 | 473 | ier = CAN_IT_TME; |
elessair | 0:f269e3021894 | 474 | irq_n = CAN1_TX_IRQn; |
elessair | 0:f269e3021894 | 475 | vector = (uint32_t)&CAN1_TX_IRQHandler; |
elessair | 0:f269e3021894 | 476 | break; |
elessair | 0:f269e3021894 | 477 | case IRQ_ERROR: |
elessair | 0:f269e3021894 | 478 | ier = CAN_IT_ERR; |
elessair | 0:f269e3021894 | 479 | irq_n = CAN1_SCE_IRQn; |
elessair | 0:f269e3021894 | 480 | vector = (uint32_t)&CAN1_SCE_IRQHandler; |
elessair | 0:f269e3021894 | 481 | break; |
elessair | 0:f269e3021894 | 482 | case IRQ_PASSIVE: |
elessair | 0:f269e3021894 | 483 | ier = CAN_IT_EPV; |
elessair | 0:f269e3021894 | 484 | irq_n = CAN1_SCE_IRQn; |
elessair | 0:f269e3021894 | 485 | vector = (uint32_t)&CAN1_SCE_IRQHandler; |
elessair | 0:f269e3021894 | 486 | break; |
elessair | 0:f269e3021894 | 487 | case IRQ_BUS: |
elessair | 0:f269e3021894 | 488 | ier = CAN_IT_BOF; |
elessair | 0:f269e3021894 | 489 | irq_n = CAN1_SCE_IRQn; |
elessair | 0:f269e3021894 | 490 | vector = (uint32_t)&CAN1_SCE_IRQHandler; |
elessair | 0:f269e3021894 | 491 | break; |
elessair | 0:f269e3021894 | 492 | default: return; |
elessair | 0:f269e3021894 | 493 | } |
elessair | 0:f269e3021894 | 494 | } else { |
elessair | 0:f269e3021894 | 495 | switch (type) { |
elessair | 0:f269e3021894 | 496 | case IRQ_RX: |
elessair | 0:f269e3021894 | 497 | ier = CAN_IT_FMP0; |
elessair | 0:f269e3021894 | 498 | irq_n = CAN2_RX0_IRQn; |
elessair | 0:f269e3021894 | 499 | vector = (uint32_t)&CAN2_RX0_IRQHandler; |
elessair | 0:f269e3021894 | 500 | break; |
elessair | 0:f269e3021894 | 501 | case IRQ_TX: |
elessair | 0:f269e3021894 | 502 | ier = CAN_IT_TME; |
elessair | 0:f269e3021894 | 503 | irq_n = CAN2_TX_IRQn; |
elessair | 0:f269e3021894 | 504 | vector = (uint32_t)&CAN2_TX_IRQHandler; |
elessair | 0:f269e3021894 | 505 | break; |
elessair | 0:f269e3021894 | 506 | case IRQ_ERROR: |
elessair | 0:f269e3021894 | 507 | ier = CAN_IT_ERR; |
elessair | 0:f269e3021894 | 508 | irq_n = CAN2_SCE_IRQn; |
elessair | 0:f269e3021894 | 509 | vector = (uint32_t)&CAN2_SCE_IRQHandler; |
elessair | 0:f269e3021894 | 510 | break; |
elessair | 0:f269e3021894 | 511 | case IRQ_PASSIVE: |
elessair | 0:f269e3021894 | 512 | ier = CAN_IT_EPV; |
elessair | 0:f269e3021894 | 513 | irq_n = CAN2_SCE_IRQn; |
elessair | 0:f269e3021894 | 514 | vector = (uint32_t)&CAN2_SCE_IRQHandler; |
elessair | 0:f269e3021894 | 515 | break; |
elessair | 0:f269e3021894 | 516 | case IRQ_BUS: |
elessair | 0:f269e3021894 | 517 | ier = CAN_IT_BOF; |
elessair | 0:f269e3021894 | 518 | irq_n = CAN2_SCE_IRQn; |
elessair | 0:f269e3021894 | 519 | vector = (uint32_t)&CAN2_SCE_IRQHandler; |
elessair | 0:f269e3021894 | 520 | break; |
elessair | 0:f269e3021894 | 521 | default: return; |
elessair | 0:f269e3021894 | 522 | } |
elessair | 0:f269e3021894 | 523 | } |
elessair | 0:f269e3021894 | 524 | |
elessair | 0:f269e3021894 | 525 | if(enable) { |
elessair | 0:f269e3021894 | 526 | can->IER |= ier; |
elessair | 0:f269e3021894 | 527 | } else { |
elessair | 0:f269e3021894 | 528 | can->IER &= ~ier; |
elessair | 0:f269e3021894 | 529 | } |
elessair | 0:f269e3021894 | 530 | |
elessair | 0:f269e3021894 | 531 | NVIC_SetVector(irq_n, vector); |
elessair | 0:f269e3021894 | 532 | NVIC_EnableIRQ(irq_n); |
elessair | 0:f269e3021894 | 533 | } |
elessair | 0:f269e3021894 | 534 | |
elessair | 0:f269e3021894 | 535 | #endif // DEVICE_CAN |
elessair | 0:f269e3021894 | 536 |