mbed library sources. Supersedes mbed-src.

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

Committer:
AnnaBridge
Date:
Thu Nov 08 11:46:34 2018 +0000
Revision:
188:bcfe06ba3d64
Parent:
187:0387e8f68319
Child:
189:f392fc9709a3
mbed-dev library. Release version 164

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
AnnaBridge 184:08ed48f1de7f 20 #include "platform/mbed_power_mgmt.h"
<> 149:156823d33999 21
<> 149:156823d33999 22 namespace mbed {
<> 149:156823d33999 23
AnnaBridge 187:0387e8f68319 24 CAN::CAN(PinName rd, PinName td) : _can(), _irq()
AnnaBridge 187:0387e8f68319 25 {
<> 149:156823d33999 26 // No lock needed in constructor
<> 149:156823d33999 27
Kojto 169:e3b6fe271b81 28 for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) {
AnnaBridge 174: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 187:0387e8f68319 36 CAN::CAN(PinName rd, PinName td, int hz) : _can(), _irq()
AnnaBridge 187:0387e8f68319 37 {
AnnaBridge 167:e84263d55307 38 // No lock needed in constructor
AnnaBridge 167:e84263d55307 39
Kojto 169:e3b6fe271b81 40 for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) {
AnnaBridge 174:b96e65c34a4d 41 _irq[i] = NULL;
AnnaBridge 167:e84263d55307 42 }
AnnaBridge 167:e84263d55307 43
AnnaBridge 167:e84263d55307 44 can_init_freq(&_can, rd, td, hz);
AnnaBridge 167:e84263d55307 45 can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this);
AnnaBridge 167:e84263d55307 46 }
AnnaBridge 167:e84263d55307 47
AnnaBridge 187:0387e8f68319 48 CAN::~CAN()
AnnaBridge 187:0387e8f68319 49 {
<> 149:156823d33999 50 // No lock needed in destructor
AnnaBridge 175:af195413fb11 51
AnnaBridge 175:af195413fb11 52 // Detaching interrupts releases the sleep lock if it was locked
AnnaBridge 175:af195413fb11 53 for (int irq = 0; irq < IrqCnt; irq++) {
AnnaBridge 175:af195413fb11 54 attach(NULL, (IrqType)irq);
AnnaBridge 175:af195413fb11 55 }
<> 149:156823d33999 56 can_irq_free(&_can);
<> 149:156823d33999 57 can_free(&_can);
<> 149:156823d33999 58 }
<> 149:156823d33999 59
AnnaBridge 187:0387e8f68319 60 int CAN::frequency(int f)
AnnaBridge 187:0387e8f68319 61 {
<> 149:156823d33999 62 lock();
<> 149:156823d33999 63 int ret = can_frequency(&_can, f);
<> 149:156823d33999 64 unlock();
<> 149:156823d33999 65 return ret;
<> 149:156823d33999 66 }
<> 149:156823d33999 67
AnnaBridge 187:0387e8f68319 68 int CAN::write(CANMessage msg)
AnnaBridge 187:0387e8f68319 69 {
<> 149:156823d33999 70 lock();
<> 149:156823d33999 71 int ret = can_write(&_can, msg, 0);
<> 149:156823d33999 72 unlock();
<> 149:156823d33999 73 return ret;
<> 149:156823d33999 74 }
<> 149:156823d33999 75
AnnaBridge 187:0387e8f68319 76 int CAN::read(CANMessage &msg, int handle)
AnnaBridge 187:0387e8f68319 77 {
<> 149:156823d33999 78 lock();
<> 149:156823d33999 79 int ret = can_read(&_can, &msg, handle);
<> 149:156823d33999 80 unlock();
<> 149:156823d33999 81 return ret;
<> 149:156823d33999 82 }
<> 149:156823d33999 83
AnnaBridge 187:0387e8f68319 84 void CAN::reset()
AnnaBridge 187:0387e8f68319 85 {
<> 149:156823d33999 86 lock();
<> 149:156823d33999 87 can_reset(&_can);
<> 149:156823d33999 88 unlock();
<> 149:156823d33999 89 }
<> 149:156823d33999 90
AnnaBridge 187:0387e8f68319 91 unsigned char CAN::rderror()
AnnaBridge 187:0387e8f68319 92 {
<> 149:156823d33999 93 lock();
<> 149:156823d33999 94 int ret = can_rderror(&_can);
<> 149:156823d33999 95 unlock();
<> 149:156823d33999 96 return ret;
<> 149:156823d33999 97 }
<> 149:156823d33999 98
AnnaBridge 187:0387e8f68319 99 unsigned char CAN::tderror()
AnnaBridge 187:0387e8f68319 100 {
<> 149:156823d33999 101 lock();
<> 149:156823d33999 102 int ret = can_tderror(&_can);
<> 149:156823d33999 103 unlock();
<> 149:156823d33999 104 return ret;
<> 149:156823d33999 105 }
<> 149:156823d33999 106
AnnaBridge 187:0387e8f68319 107 void CAN::monitor(bool silent)
AnnaBridge 187:0387e8f68319 108 {
<> 149:156823d33999 109 lock();
<> 149:156823d33999 110 can_monitor(&_can, (silent) ? 1 : 0);
<> 149:156823d33999 111 unlock();
<> 149:156823d33999 112 }
<> 149:156823d33999 113
AnnaBridge 187:0387e8f68319 114 int CAN::mode(Mode mode)
AnnaBridge 187:0387e8f68319 115 {
<> 149:156823d33999 116 lock();
<> 149:156823d33999 117 int ret = can_mode(&_can, (CanMode)mode);
<> 149:156823d33999 118 unlock();
<> 149:156823d33999 119 return ret;
<> 149:156823d33999 120 }
<> 149:156823d33999 121
AnnaBridge 187:0387e8f68319 122 int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle)
AnnaBridge 187:0387e8f68319 123 {
<> 149:156823d33999 124 lock();
<> 149:156823d33999 125 int ret = can_filter(&_can, id, mask, format, handle);
<> 149:156823d33999 126 unlock();
<> 149:156823d33999 127 return ret;
<> 149:156823d33999 128 }
<> 149:156823d33999 129
AnnaBridge 187:0387e8f68319 130 void CAN::attach(Callback<void()> func, IrqType type)
AnnaBridge 187:0387e8f68319 131 {
<> 149:156823d33999 132 lock();
<> 149:156823d33999 133 if (func) {
AnnaBridge 174:b96e65c34a4d 134 // lock deep sleep only the first time
AnnaBridge 174:b96e65c34a4d 135 if (!_irq[(CanIrqType)type]) {
AnnaBridge 174:b96e65c34a4d 136 sleep_manager_lock_deep_sleep();
AnnaBridge 174:b96e65c34a4d 137 }
Anna Bridge 163:74e0ce7f98e8 138 _irq[(CanIrqType)type] = func;
<> 149:156823d33999 139 can_irq_set(&_can, (CanIrqType)type, 1);
<> 149:156823d33999 140 } else {
AnnaBridge 174:b96e65c34a4d 141 // unlock deep sleep only the first time
AnnaBridge 174:b96e65c34a4d 142 if (_irq[(CanIrqType)type]) {
AnnaBridge 174:b96e65c34a4d 143 sleep_manager_unlock_deep_sleep();
AnnaBridge 174:b96e65c34a4d 144 }
AnnaBridge 174:b96e65c34a4d 145 _irq[(CanIrqType)type] = NULL;
<> 149:156823d33999 146 can_irq_set(&_can, (CanIrqType)type, 0);
<> 149:156823d33999 147 }
<> 149:156823d33999 148 unlock();
<> 149:156823d33999 149 }
<> 149:156823d33999 150
AnnaBridge 187:0387e8f68319 151 void CAN::_irq_handler(uint32_t id, CanIrqType type)
AnnaBridge 187:0387e8f68319 152 {
AnnaBridge 187:0387e8f68319 153 CAN *handler = (CAN *)id;
AnnaBridge 174:b96e65c34a4d 154 if (handler->_irq[type]) {
AnnaBridge 174:b96e65c34a4d 155 handler->_irq[type].call();
AnnaBridge 174:b96e65c34a4d 156 }
<> 149:156823d33999 157 }
<> 149:156823d33999 158
AnnaBridge 187:0387e8f68319 159 void CAN::lock()
AnnaBridge 187:0387e8f68319 160 {
<> 149:156823d33999 161 _mutex.lock();
<> 149:156823d33999 162 }
<> 149:156823d33999 163
AnnaBridge 187:0387e8f68319 164 void CAN::unlock()
AnnaBridge 187:0387e8f68319 165 {
<> 149:156823d33999 166 _mutex.unlock();
<> 149:156823d33999 167 }
<> 149:156823d33999 168
<> 149:156823d33999 169 } // namespace mbed
<> 149:156823d33999 170
<> 149:156823d33999 171 #endif