Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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