mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
188:bcfe06ba3d64
mbed library release version 165

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