Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 04:46:28 2018 +0000
Revision:
1:fcdb45ee95b9
Parent:
0:6ad07c9019fd
Entrega Final

Who changed what in which revision?

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