Erste version der Software für der Prototyp

Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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