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