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