mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

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     can_init(&_can, rd, td);
00026     can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this);
00027 }
00028 
00029 CAN::~CAN() {
00030     can_irq_free(&_can);
00031     can_free(&_can);
00032 }
00033 
00034 int CAN::frequency(int f) {
00035     return can_frequency(&_can, f);
00036 }
00037 
00038 int CAN::write(CANMessage msg) {
00039     return can_write(&_can, msg, 0);
00040 }
00041 
00042 int CAN::read(CANMessage &msg, int handle) {
00043     return can_read(&_can, &msg, handle);
00044 }
00045 
00046 void CAN::reset() {
00047     can_reset(&_can);
00048 }
00049 
00050 unsigned char CAN::rderror() {
00051     return can_rderror(&_can);
00052 }
00053 
00054 unsigned char CAN::tderror() {
00055     return can_tderror(&_can);
00056 }
00057 
00058 void CAN::monitor(bool silent) {
00059     can_monitor(&_can, (silent) ? 1 : 0);
00060 }
00061 
00062 int CAN::mode(Mode mode) {
00063     return can_mode(&_can, (CanMode)mode);
00064 }
00065 
00066 int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle) {
00067     return can_filter(&_can, id, mask, format, handle);
00068 }
00069 
00070 void CAN::attach(void (*fptr)(void), IrqType type) {
00071     if (fptr) {
00072         _irq[(CanIrqType)type].attach(fptr);
00073         can_irq_set(&_can, (CanIrqType)type, 1);
00074     } else {
00075         can_irq_set(&_can, (CanIrqType)type, 0);
00076     }
00077 }
00078 
00079 void CAN::_irq_handler(uint32_t id, CanIrqType type) {
00080     CAN *handler = (CAN*)id;
00081     handler->_irq[type].call();
00082 }
00083 
00084 } // namespace mbed
00085 
00086 #endif