BA
/
BaBoRo_test2
Backup 1
mbed-os/drivers/CAN.cpp@0:02dd72d1d465, 2018-04-24 (annotated)
- Committer:
- borlanic
- Date:
- Tue Apr 24 11:45:18 2018 +0000
- Revision:
- 0:02dd72d1d465
BaBoRo_test2 - backup 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
borlanic | 0:02dd72d1d465 | 1 | /* mbed Microcontroller Library |
borlanic | 0:02dd72d1d465 | 2 | * Copyright (c) 2006-2013 ARM Limited |
borlanic | 0:02dd72d1d465 | 3 | * |
borlanic | 0:02dd72d1d465 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
borlanic | 0:02dd72d1d465 | 5 | * you may not use this file except in compliance with the License. |
borlanic | 0:02dd72d1d465 | 6 | * You may obtain a copy of the License at |
borlanic | 0:02dd72d1d465 | 7 | * |
borlanic | 0:02dd72d1d465 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
borlanic | 0:02dd72d1d465 | 9 | * |
borlanic | 0:02dd72d1d465 | 10 | * Unless required by applicable law or agreed to in writing, software |
borlanic | 0:02dd72d1d465 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
borlanic | 0:02dd72d1d465 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
borlanic | 0:02dd72d1d465 | 13 | * See the License for the specific language governing permissions and |
borlanic | 0:02dd72d1d465 | 14 | * limitations under the License. |
borlanic | 0:02dd72d1d465 | 15 | */ |
borlanic | 0:02dd72d1d465 | 16 | #include "drivers/CAN.h" |
borlanic | 0:02dd72d1d465 | 17 | |
borlanic | 0:02dd72d1d465 | 18 | #if DEVICE_CAN |
borlanic | 0:02dd72d1d465 | 19 | |
borlanic | 0:02dd72d1d465 | 20 | #include "cmsis.h" |
borlanic | 0:02dd72d1d465 | 21 | #include "platform/mbed_power_mgmt.h" |
borlanic | 0:02dd72d1d465 | 22 | |
borlanic | 0:02dd72d1d465 | 23 | namespace mbed { |
borlanic | 0:02dd72d1d465 | 24 | |
borlanic | 0:02dd72d1d465 | 25 | CAN::CAN(PinName rd, PinName td) : _can(), _irq() { |
borlanic | 0:02dd72d1d465 | 26 | // No lock needed in constructor |
borlanic | 0:02dd72d1d465 | 27 | |
borlanic | 0:02dd72d1d465 | 28 | for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) { |
borlanic | 0:02dd72d1d465 | 29 | _irq[i] = NULL; |
borlanic | 0:02dd72d1d465 | 30 | } |
borlanic | 0:02dd72d1d465 | 31 | |
borlanic | 0:02dd72d1d465 | 32 | can_init(&_can, rd, td); |
borlanic | 0:02dd72d1d465 | 33 | can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this); |
borlanic | 0:02dd72d1d465 | 34 | } |
borlanic | 0:02dd72d1d465 | 35 | |
borlanic | 0:02dd72d1d465 | 36 | CAN::CAN(PinName rd, PinName td, int hz) : _can(), _irq() { |
borlanic | 0:02dd72d1d465 | 37 | // No lock needed in constructor |
borlanic | 0:02dd72d1d465 | 38 | |
borlanic | 0:02dd72d1d465 | 39 | for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) { |
borlanic | 0:02dd72d1d465 | 40 | _irq[i] = NULL; |
borlanic | 0:02dd72d1d465 | 41 | } |
borlanic | 0:02dd72d1d465 | 42 | |
borlanic | 0:02dd72d1d465 | 43 | can_init_freq(&_can, rd, td, hz); |
borlanic | 0:02dd72d1d465 | 44 | can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this); |
borlanic | 0:02dd72d1d465 | 45 | } |
borlanic | 0:02dd72d1d465 | 46 | |
borlanic | 0:02dd72d1d465 | 47 | CAN::~CAN() { |
borlanic | 0:02dd72d1d465 | 48 | // No lock needed in destructor |
borlanic | 0:02dd72d1d465 | 49 | |
borlanic | 0:02dd72d1d465 | 50 | // Detaching interrupts releases the sleep lock if it was locked |
borlanic | 0:02dd72d1d465 | 51 | for (int irq = 0; irq < IrqCnt; irq++) { |
borlanic | 0:02dd72d1d465 | 52 | attach(NULL, (IrqType)irq); |
borlanic | 0:02dd72d1d465 | 53 | } |
borlanic | 0:02dd72d1d465 | 54 | can_irq_free(&_can); |
borlanic | 0:02dd72d1d465 | 55 | can_free(&_can); |
borlanic | 0:02dd72d1d465 | 56 | } |
borlanic | 0:02dd72d1d465 | 57 | |
borlanic | 0:02dd72d1d465 | 58 | int CAN::frequency(int f) { |
borlanic | 0:02dd72d1d465 | 59 | lock(); |
borlanic | 0:02dd72d1d465 | 60 | int ret = can_frequency(&_can, f); |
borlanic | 0:02dd72d1d465 | 61 | unlock(); |
borlanic | 0:02dd72d1d465 | 62 | return ret; |
borlanic | 0:02dd72d1d465 | 63 | } |
borlanic | 0:02dd72d1d465 | 64 | |
borlanic | 0:02dd72d1d465 | 65 | int CAN::write(CANMessage msg) { |
borlanic | 0:02dd72d1d465 | 66 | lock(); |
borlanic | 0:02dd72d1d465 | 67 | int ret = can_write(&_can, msg, 0); |
borlanic | 0:02dd72d1d465 | 68 | unlock(); |
borlanic | 0:02dd72d1d465 | 69 | return ret; |
borlanic | 0:02dd72d1d465 | 70 | } |
borlanic | 0:02dd72d1d465 | 71 | |
borlanic | 0:02dd72d1d465 | 72 | int CAN::read(CANMessage &msg, int handle) { |
borlanic | 0:02dd72d1d465 | 73 | lock(); |
borlanic | 0:02dd72d1d465 | 74 | int ret = can_read(&_can, &msg, handle); |
borlanic | 0:02dd72d1d465 | 75 | unlock(); |
borlanic | 0:02dd72d1d465 | 76 | return ret; |
borlanic | 0:02dd72d1d465 | 77 | } |
borlanic | 0:02dd72d1d465 | 78 | |
borlanic | 0:02dd72d1d465 | 79 | void CAN::reset() { |
borlanic | 0:02dd72d1d465 | 80 | lock(); |
borlanic | 0:02dd72d1d465 | 81 | can_reset(&_can); |
borlanic | 0:02dd72d1d465 | 82 | unlock(); |
borlanic | 0:02dd72d1d465 | 83 | } |
borlanic | 0:02dd72d1d465 | 84 | |
borlanic | 0:02dd72d1d465 | 85 | unsigned char CAN::rderror() { |
borlanic | 0:02dd72d1d465 | 86 | lock(); |
borlanic | 0:02dd72d1d465 | 87 | int ret = can_rderror(&_can); |
borlanic | 0:02dd72d1d465 | 88 | unlock(); |
borlanic | 0:02dd72d1d465 | 89 | return ret; |
borlanic | 0:02dd72d1d465 | 90 | } |
borlanic | 0:02dd72d1d465 | 91 | |
borlanic | 0:02dd72d1d465 | 92 | unsigned char CAN::tderror() { |
borlanic | 0:02dd72d1d465 | 93 | lock(); |
borlanic | 0:02dd72d1d465 | 94 | int ret = can_tderror(&_can); |
borlanic | 0:02dd72d1d465 | 95 | unlock(); |
borlanic | 0:02dd72d1d465 | 96 | return ret; |
borlanic | 0:02dd72d1d465 | 97 | } |
borlanic | 0:02dd72d1d465 | 98 | |
borlanic | 0:02dd72d1d465 | 99 | void CAN::monitor(bool silent) { |
borlanic | 0:02dd72d1d465 | 100 | lock(); |
borlanic | 0:02dd72d1d465 | 101 | can_monitor(&_can, (silent) ? 1 : 0); |
borlanic | 0:02dd72d1d465 | 102 | unlock(); |
borlanic | 0:02dd72d1d465 | 103 | } |
borlanic | 0:02dd72d1d465 | 104 | |
borlanic | 0:02dd72d1d465 | 105 | int CAN::mode(Mode mode) { |
borlanic | 0:02dd72d1d465 | 106 | lock(); |
borlanic | 0:02dd72d1d465 | 107 | int ret = can_mode(&_can, (CanMode)mode); |
borlanic | 0:02dd72d1d465 | 108 | unlock(); |
borlanic | 0:02dd72d1d465 | 109 | return ret; |
borlanic | 0:02dd72d1d465 | 110 | } |
borlanic | 0:02dd72d1d465 | 111 | |
borlanic | 0:02dd72d1d465 | 112 | int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle) { |
borlanic | 0:02dd72d1d465 | 113 | lock(); |
borlanic | 0:02dd72d1d465 | 114 | int ret = can_filter(&_can, id, mask, format, handle); |
borlanic | 0:02dd72d1d465 | 115 | unlock(); |
borlanic | 0:02dd72d1d465 | 116 | return ret; |
borlanic | 0:02dd72d1d465 | 117 | } |
borlanic | 0:02dd72d1d465 | 118 | |
borlanic | 0:02dd72d1d465 | 119 | void CAN::attach(Callback<void()> func, IrqType type) { |
borlanic | 0:02dd72d1d465 | 120 | lock(); |
borlanic | 0:02dd72d1d465 | 121 | if (func) { |
borlanic | 0:02dd72d1d465 | 122 | // lock deep sleep only the first time |
borlanic | 0:02dd72d1d465 | 123 | if (!_irq[(CanIrqType)type]) { |
borlanic | 0:02dd72d1d465 | 124 | sleep_manager_lock_deep_sleep(); |
borlanic | 0:02dd72d1d465 | 125 | } |
borlanic | 0:02dd72d1d465 | 126 | _irq[(CanIrqType)type] = func; |
borlanic | 0:02dd72d1d465 | 127 | can_irq_set(&_can, (CanIrqType)type, 1); |
borlanic | 0:02dd72d1d465 | 128 | } else { |
borlanic | 0:02dd72d1d465 | 129 | // unlock deep sleep only the first time |
borlanic | 0:02dd72d1d465 | 130 | if (_irq[(CanIrqType)type]) { |
borlanic | 0:02dd72d1d465 | 131 | sleep_manager_unlock_deep_sleep(); |
borlanic | 0:02dd72d1d465 | 132 | } |
borlanic | 0:02dd72d1d465 | 133 | _irq[(CanIrqType)type] = NULL; |
borlanic | 0:02dd72d1d465 | 134 | can_irq_set(&_can, (CanIrqType)type, 0); |
borlanic | 0:02dd72d1d465 | 135 | } |
borlanic | 0:02dd72d1d465 | 136 | unlock(); |
borlanic | 0:02dd72d1d465 | 137 | } |
borlanic | 0:02dd72d1d465 | 138 | |
borlanic | 0:02dd72d1d465 | 139 | void CAN::_irq_handler(uint32_t id, CanIrqType type) { |
borlanic | 0:02dd72d1d465 | 140 | CAN *handler = (CAN*)id; |
borlanic | 0:02dd72d1d465 | 141 | if (handler->_irq[type]) { |
borlanic | 0:02dd72d1d465 | 142 | handler->_irq[type].call(); |
borlanic | 0:02dd72d1d465 | 143 | } |
borlanic | 0:02dd72d1d465 | 144 | } |
borlanic | 0:02dd72d1d465 | 145 | |
borlanic | 0:02dd72d1d465 | 146 | void CAN::lock() { |
borlanic | 0:02dd72d1d465 | 147 | _mutex.lock(); |
borlanic | 0:02dd72d1d465 | 148 | } |
borlanic | 0:02dd72d1d465 | 149 | |
borlanic | 0:02dd72d1d465 | 150 | void CAN::unlock() { |
borlanic | 0:02dd72d1d465 | 151 | _mutex.unlock(); |
borlanic | 0:02dd72d1d465 | 152 | } |
borlanic | 0:02dd72d1d465 | 153 | |
borlanic | 0:02dd72d1d465 | 154 | } // namespace mbed |
borlanic | 0:02dd72d1d465 | 155 | |
borlanic | 0:02dd72d1d465 | 156 | #endif |