4545

Dependents:   LSS_Rev_1

Fork of mbed-dev by Umar Naeem

Committer:
<>
Date:
Tue Mar 14 16:40:56 2017 +0000
Revision:
160:d5399cc887bb
This updates the lib to the mbed lib v138

Who changed what in which revision?

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