,,

Fork of Application by Daniel Sygut

Committer:
Zaitsev
Date:
Tue Jan 10 20:42:26 2017 +0000
Revision:
10:41552d038a69
USB Serial bi-directional bridge

Who changed what in which revision?

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