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