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