This fork captures the mbed lib v125 for ease of integration into older projects.

Fork of mbed-dev by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CAN.cpp Source File

CAN.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "CAN.h"
00017 
00018 #if DEVICE_CAN
00019 
00020 #include "cmsis.h"
00021 
00022 namespace mbed {
00023 
00024 CAN::CAN(PinName rd, PinName td) : _can(), _irq() {
00025     // No lock needed in constructor
00026     can_init(&_can, rd, td);
00027     can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this);
00028 }
00029 
00030 CAN::~CAN() {
00031     // No lock needed in destructor
00032     can_irq_free(&_can);
00033     can_free(&_can);
00034 }
00035 
00036 int CAN::frequency(int f) {
00037     lock();
00038     int ret = can_frequency(&_can, f);
00039     unlock();
00040     return ret;
00041 }
00042 
00043 int CAN::write(CANMessage msg) {
00044     lock();
00045     int ret = can_write(&_can, msg, 0);
00046     unlock();
00047     return ret;
00048 }
00049 
00050 int CAN::read(CANMessage &msg, int handle) {
00051     lock();
00052     int ret = can_read(&_can, &msg, handle);
00053     unlock();
00054     return ret;
00055 }
00056 
00057 void CAN::reset() {
00058     lock();
00059     can_reset(&_can);
00060     unlock();
00061 }
00062 
00063 unsigned char CAN::rderror() {
00064     lock();
00065     int ret = can_rderror(&_can);
00066     unlock();
00067     return ret;
00068 }
00069 
00070 unsigned char CAN::tderror() {
00071     lock();
00072     int ret = can_tderror(&_can);
00073     unlock();
00074     return ret;
00075 }
00076 
00077 void CAN::monitor(bool silent) {
00078     lock();
00079     can_monitor(&_can, (silent) ? 1 : 0);
00080     unlock();
00081 }
00082 
00083 int CAN::mode(Mode mode) {
00084     lock();
00085     int ret = can_mode(&_can, (CanMode)mode);
00086     unlock();
00087     return ret;
00088 }
00089 
00090 int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle) {
00091     lock();
00092     int ret = can_filter(&_can, id, mask, format, handle);
00093     unlock();
00094     return ret;
00095 }
00096 
00097 void CAN::attach(Callback<void()> func, IrqType type) {
00098     lock();
00099     if (func) {
00100         _irq[(CanIrqType)type].attach(func);
00101         can_irq_set(&_can, (CanIrqType)type, 1);
00102     } else {
00103         can_irq_set(&_can, (CanIrqType)type, 0);
00104     }
00105     unlock();
00106 }
00107 
00108 void CAN::_irq_handler(uint32_t id, CanIrqType type) {
00109     CAN *handler = (CAN*)id;
00110     handler->_irq[type].call();
00111 }
00112 
00113 void CAN::lock() {
00114     _mutex.lock();
00115 }
00116 
00117 void CAN::unlock() {
00118     _mutex.unlock();
00119 }
00120 
00121 } // namespace mbed
00122 
00123 #endif