mbed library sources. With a patch for the can_api

Fork of mbed-dev by mbed official

Committer:
DangerousElectrician
Date:
Mon Nov 14 04:39:23 2016 +0000
Revision:
151:91825d030f9b
Parent:
150:02e0a0aed4ec
stuff changed?

Who changed what in which revision?

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