test

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Wed Nov 08 13:50:44 2017 +0000
Revision:
178:d650f5d4c87a
Parent:
176:af195413fb11
This updates the lib to the mbed lib v 155

Who changed what in which revision?

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