mbed library sources, include can_api for nucleo-f091rc

Dependents:   CanNucleoF0_example

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