mbed library sources

Committer:
ebrus
Date:
Wed Jul 27 18:35:32 2016 +0000
Revision:
0:0a673c671a56
4

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ebrus 0:0a673c671a56 1 /* mbed Microcontroller Library
ebrus 0:0a673c671a56 2 * Copyright (c) 2006-2013 ARM Limited
ebrus 0:0a673c671a56 3 *
ebrus 0:0a673c671a56 4 * Licensed under the Apache License, Version 2.0 (the "License");
ebrus 0:0a673c671a56 5 * you may not use this file except in compliance with the License.
ebrus 0:0a673c671a56 6 * You may obtain a copy of the License at
ebrus 0:0a673c671a56 7 *
ebrus 0:0a673c671a56 8 * http://www.apache.org/licenses/LICENSE-2.0
ebrus 0:0a673c671a56 9 *
ebrus 0:0a673c671a56 10 * Unless required by applicable law or agreed to in writing, software
ebrus 0:0a673c671a56 11 * distributed under the License is distributed on an "AS IS" BASIS,
ebrus 0:0a673c671a56 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ebrus 0:0a673c671a56 13 * See the License for the specific language governing permissions and
ebrus 0:0a673c671a56 14 * limitations under the License.
ebrus 0:0a673c671a56 15 */
ebrus 0:0a673c671a56 16 #include "CAN.h"
ebrus 0:0a673c671a56 17
ebrus 0:0a673c671a56 18 #if DEVICE_CAN
ebrus 0:0a673c671a56 19
ebrus 0:0a673c671a56 20 #include "cmsis.h"
ebrus 0:0a673c671a56 21
ebrus 0:0a673c671a56 22 namespace mbed {
ebrus 0:0a673c671a56 23
ebrus 0:0a673c671a56 24 CAN::CAN(PinName rd, PinName td) : _can(), _irq() {
ebrus 0:0a673c671a56 25 can_init(&_can, rd, td);
ebrus 0:0a673c671a56 26 can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this);
ebrus 0:0a673c671a56 27 }
ebrus 0:0a673c671a56 28
ebrus 0:0a673c671a56 29 CAN::~CAN() {
ebrus 0:0a673c671a56 30 can_irq_free(&_can);
ebrus 0:0a673c671a56 31 can_free(&_can);
ebrus 0:0a673c671a56 32 }
ebrus 0:0a673c671a56 33
ebrus 0:0a673c671a56 34 int CAN::frequency(int f) {
ebrus 0:0a673c671a56 35 return can_frequency(&_can, f);
ebrus 0:0a673c671a56 36 }
ebrus 0:0a673c671a56 37
ebrus 0:0a673c671a56 38 int CAN::write(CANMessage msg) {
ebrus 0:0a673c671a56 39 return can_write(&_can, msg, 0);
ebrus 0:0a673c671a56 40 }
ebrus 0:0a673c671a56 41
ebrus 0:0a673c671a56 42 int CAN::read(CANMessage &msg, int handle) {
ebrus 0:0a673c671a56 43 return can_read(&_can, &msg, handle);
ebrus 0:0a673c671a56 44 }
ebrus 0:0a673c671a56 45
ebrus 0:0a673c671a56 46 void CAN::reset() {
ebrus 0:0a673c671a56 47 can_reset(&_can);
ebrus 0:0a673c671a56 48 }
ebrus 0:0a673c671a56 49
ebrus 0:0a673c671a56 50 unsigned char CAN::rderror() {
ebrus 0:0a673c671a56 51 return can_rderror(&_can);
ebrus 0:0a673c671a56 52 }
ebrus 0:0a673c671a56 53
ebrus 0:0a673c671a56 54 unsigned char CAN::tderror() {
ebrus 0:0a673c671a56 55 return can_tderror(&_can);
ebrus 0:0a673c671a56 56 }
ebrus 0:0a673c671a56 57
ebrus 0:0a673c671a56 58 void CAN::monitor(bool silent) {
ebrus 0:0a673c671a56 59 can_monitor(&_can, (silent) ? 1 : 0);
ebrus 0:0a673c671a56 60 }
ebrus 0:0a673c671a56 61
ebrus 0:0a673c671a56 62 int CAN::mode(Mode mode) {
ebrus 0:0a673c671a56 63 return can_mode(&_can, (CanMode)mode);
ebrus 0:0a673c671a56 64 }
ebrus 0:0a673c671a56 65
ebrus 0:0a673c671a56 66 int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle) {
ebrus 0:0a673c671a56 67 return can_filter(&_can, id, mask, format, handle);
ebrus 0:0a673c671a56 68 }
ebrus 0:0a673c671a56 69
ebrus 0:0a673c671a56 70 void CAN::attach(void (*fptr)(void), IrqType type) {
ebrus 0:0a673c671a56 71 if (fptr) {
ebrus 0:0a673c671a56 72 _irq[(CanIrqType)type].attach(fptr);
ebrus 0:0a673c671a56 73 can_irq_set(&_can, (CanIrqType)type, 1);
ebrus 0:0a673c671a56 74 } else {
ebrus 0:0a673c671a56 75 can_irq_set(&_can, (CanIrqType)type, 0);
ebrus 0:0a673c671a56 76 }
ebrus 0:0a673c671a56 77 }
ebrus 0:0a673c671a56 78
ebrus 0:0a673c671a56 79 void CAN::_irq_handler(uint32_t id, CanIrqType type) {
ebrus 0:0a673c671a56 80 CAN *handler = (CAN*)id;
ebrus 0:0a673c671a56 81 handler->_irq[type].call();
ebrus 0:0a673c671a56 82 }
ebrus 0:0a673c671a56 83
ebrus 0:0a673c671a56 84 } // namespace mbed
ebrus 0:0a673c671a56 85
ebrus 0:0a673c671a56 86 #endif