This fork captures the mbed lib v125 for ease of integration into older projects.
Fork of mbed-dev by
common/CAN.cpp@187:92cbb9eec47b, 2018-07-20 (annotated)
- Committer:
- apluscw
- Date:
- Fri Jul 20 21:24:42 2018 +0000
- Revision:
- 187:92cbb9eec47b
Mbed library with source code from mbed lib v125. Posted to ease integration with some older projects.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
apluscw | 187:92cbb9eec47b | 1 | /* mbed Microcontroller Library |
apluscw | 187:92cbb9eec47b | 2 | * Copyright (c) 2006-2013 ARM Limited |
apluscw | 187:92cbb9eec47b | 3 | * |
apluscw | 187:92cbb9eec47b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
apluscw | 187:92cbb9eec47b | 5 | * you may not use this file except in compliance with the License. |
apluscw | 187:92cbb9eec47b | 6 | * You may obtain a copy of the License at |
apluscw | 187:92cbb9eec47b | 7 | * |
apluscw | 187:92cbb9eec47b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
apluscw | 187:92cbb9eec47b | 9 | * |
apluscw | 187:92cbb9eec47b | 10 | * Unless required by applicable law or agreed to in writing, software |
apluscw | 187:92cbb9eec47b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
apluscw | 187:92cbb9eec47b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
apluscw | 187:92cbb9eec47b | 13 | * See the License for the specific language governing permissions and |
apluscw | 187:92cbb9eec47b | 14 | * limitations under the License. |
apluscw | 187:92cbb9eec47b | 15 | */ |
apluscw | 187:92cbb9eec47b | 16 | #include "CAN.h" |
apluscw | 187:92cbb9eec47b | 17 | |
apluscw | 187:92cbb9eec47b | 18 | #if DEVICE_CAN |
apluscw | 187:92cbb9eec47b | 19 | |
apluscw | 187:92cbb9eec47b | 20 | #include "cmsis.h" |
apluscw | 187:92cbb9eec47b | 21 | |
apluscw | 187:92cbb9eec47b | 22 | namespace mbed { |
apluscw | 187:92cbb9eec47b | 23 | |
apluscw | 187:92cbb9eec47b | 24 | CAN::CAN(PinName rd, PinName td) : _can(), _irq() { |
apluscw | 187:92cbb9eec47b | 25 | // No lock needed in constructor |
apluscw | 187:92cbb9eec47b | 26 | can_init(&_can, rd, td); |
apluscw | 187:92cbb9eec47b | 27 | can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this); |
apluscw | 187:92cbb9eec47b | 28 | } |
apluscw | 187:92cbb9eec47b | 29 | |
apluscw | 187:92cbb9eec47b | 30 | CAN::~CAN() { |
apluscw | 187:92cbb9eec47b | 31 | // No lock needed in destructor |
apluscw | 187:92cbb9eec47b | 32 | can_irq_free(&_can); |
apluscw | 187:92cbb9eec47b | 33 | can_free(&_can); |
apluscw | 187:92cbb9eec47b | 34 | } |
apluscw | 187:92cbb9eec47b | 35 | |
apluscw | 187:92cbb9eec47b | 36 | int CAN::frequency(int f) { |
apluscw | 187:92cbb9eec47b | 37 | lock(); |
apluscw | 187:92cbb9eec47b | 38 | int ret = can_frequency(&_can, f); |
apluscw | 187:92cbb9eec47b | 39 | unlock(); |
apluscw | 187:92cbb9eec47b | 40 | return ret; |
apluscw | 187:92cbb9eec47b | 41 | } |
apluscw | 187:92cbb9eec47b | 42 | |
apluscw | 187:92cbb9eec47b | 43 | int CAN::write(CANMessage msg) { |
apluscw | 187:92cbb9eec47b | 44 | lock(); |
apluscw | 187:92cbb9eec47b | 45 | int ret = can_write(&_can, msg, 0); |
apluscw | 187:92cbb9eec47b | 46 | unlock(); |
apluscw | 187:92cbb9eec47b | 47 | return ret; |
apluscw | 187:92cbb9eec47b | 48 | } |
apluscw | 187:92cbb9eec47b | 49 | |
apluscw | 187:92cbb9eec47b | 50 | int CAN::read(CANMessage &msg, int handle) { |
apluscw | 187:92cbb9eec47b | 51 | lock(); |
apluscw | 187:92cbb9eec47b | 52 | int ret = can_read(&_can, &msg, handle); |
apluscw | 187:92cbb9eec47b | 53 | unlock(); |
apluscw | 187:92cbb9eec47b | 54 | return ret; |
apluscw | 187:92cbb9eec47b | 55 | } |
apluscw | 187:92cbb9eec47b | 56 | |
apluscw | 187:92cbb9eec47b | 57 | void CAN::reset() { |
apluscw | 187:92cbb9eec47b | 58 | lock(); |
apluscw | 187:92cbb9eec47b | 59 | can_reset(&_can); |
apluscw | 187:92cbb9eec47b | 60 | unlock(); |
apluscw | 187:92cbb9eec47b | 61 | } |
apluscw | 187:92cbb9eec47b | 62 | |
apluscw | 187:92cbb9eec47b | 63 | unsigned char CAN::rderror() { |
apluscw | 187:92cbb9eec47b | 64 | lock(); |
apluscw | 187:92cbb9eec47b | 65 | int ret = can_rderror(&_can); |
apluscw | 187:92cbb9eec47b | 66 | unlock(); |
apluscw | 187:92cbb9eec47b | 67 | return ret; |
apluscw | 187:92cbb9eec47b | 68 | } |
apluscw | 187:92cbb9eec47b | 69 | |
apluscw | 187:92cbb9eec47b | 70 | unsigned char CAN::tderror() { |
apluscw | 187:92cbb9eec47b | 71 | lock(); |
apluscw | 187:92cbb9eec47b | 72 | int ret = can_tderror(&_can); |
apluscw | 187:92cbb9eec47b | 73 | unlock(); |
apluscw | 187:92cbb9eec47b | 74 | return ret; |
apluscw | 187:92cbb9eec47b | 75 | } |
apluscw | 187:92cbb9eec47b | 76 | |
apluscw | 187:92cbb9eec47b | 77 | void CAN::monitor(bool silent) { |
apluscw | 187:92cbb9eec47b | 78 | lock(); |
apluscw | 187:92cbb9eec47b | 79 | can_monitor(&_can, (silent) ? 1 : 0); |
apluscw | 187:92cbb9eec47b | 80 | unlock(); |
apluscw | 187:92cbb9eec47b | 81 | } |
apluscw | 187:92cbb9eec47b | 82 | |
apluscw | 187:92cbb9eec47b | 83 | int CAN::mode(Mode mode) { |
apluscw | 187:92cbb9eec47b | 84 | lock(); |
apluscw | 187:92cbb9eec47b | 85 | int ret = can_mode(&_can, (CanMode)mode); |
apluscw | 187:92cbb9eec47b | 86 | unlock(); |
apluscw | 187:92cbb9eec47b | 87 | return ret; |
apluscw | 187:92cbb9eec47b | 88 | } |
apluscw | 187:92cbb9eec47b | 89 | |
apluscw | 187:92cbb9eec47b | 90 | int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle) { |
apluscw | 187:92cbb9eec47b | 91 | lock(); |
apluscw | 187:92cbb9eec47b | 92 | int ret = can_filter(&_can, id, mask, format, handle); |
apluscw | 187:92cbb9eec47b | 93 | unlock(); |
apluscw | 187:92cbb9eec47b | 94 | return ret; |
apluscw | 187:92cbb9eec47b | 95 | } |
apluscw | 187:92cbb9eec47b | 96 | |
apluscw | 187:92cbb9eec47b | 97 | void CAN::attach(Callback<void()> func, IrqType type) { |
apluscw | 187:92cbb9eec47b | 98 | lock(); |
apluscw | 187:92cbb9eec47b | 99 | if (func) { |
apluscw | 187:92cbb9eec47b | 100 | _irq[(CanIrqType)type].attach(func); |
apluscw | 187:92cbb9eec47b | 101 | can_irq_set(&_can, (CanIrqType)type, 1); |
apluscw | 187:92cbb9eec47b | 102 | } else { |
apluscw | 187:92cbb9eec47b | 103 | can_irq_set(&_can, (CanIrqType)type, 0); |
apluscw | 187:92cbb9eec47b | 104 | } |
apluscw | 187:92cbb9eec47b | 105 | unlock(); |
apluscw | 187:92cbb9eec47b | 106 | } |
apluscw | 187:92cbb9eec47b | 107 | |
apluscw | 187:92cbb9eec47b | 108 | void CAN::_irq_handler(uint32_t id, CanIrqType type) { |
apluscw | 187:92cbb9eec47b | 109 | CAN *handler = (CAN*)id; |
apluscw | 187:92cbb9eec47b | 110 | handler->_irq[type].call(); |
apluscw | 187:92cbb9eec47b | 111 | } |
apluscw | 187:92cbb9eec47b | 112 | |
apluscw | 187:92cbb9eec47b | 113 | void CAN::lock() { |
apluscw | 187:92cbb9eec47b | 114 | _mutex.lock(); |
apluscw | 187:92cbb9eec47b | 115 | } |
apluscw | 187:92cbb9eec47b | 116 | |
apluscw | 187:92cbb9eec47b | 117 | void CAN::unlock() { |
apluscw | 187:92cbb9eec47b | 118 | _mutex.unlock(); |
apluscw | 187:92cbb9eec47b | 119 | } |
apluscw | 187:92cbb9eec47b | 120 | |
apluscw | 187:92cbb9eec47b | 121 | } // namespace mbed |
apluscw | 187:92cbb9eec47b | 122 | |
apluscw | 187:92cbb9eec47b | 123 | #endif |