mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

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