mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
updated based on mbed-os5.15.0

Who changed what in which revision?

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