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