mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
drivers/CAN.cpp@174:b96e65c34a4d, 2017-10-02 (annotated)
- Committer:
- AnnaBridge
- Date:
- Mon Oct 02 15:33:19 2017 +0100
- Revision:
- 174:b96e65c34a4d
- Parent:
- 169:e3b6fe271b81
- Child:
- 175:af195413fb11
This updates the lib to the mbed lib v 152
Who changed what in which revision?
User | Revision | Line number | New 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 | 174: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 | 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 | 167:e84263d55307 | 36 | CAN::CAN(PinName rd, PinName td, int hz) : _can(), _irq() { |
AnnaBridge | 167:e84263d55307 | 37 | // No lock needed in constructor |
AnnaBridge | 167:e84263d55307 | 38 | |
Kojto | 169:e3b6fe271b81 | 39 | for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) { |
AnnaBridge | 174:b96e65c34a4d | 40 | _irq[i] = NULL; |
AnnaBridge | 167:e84263d55307 | 41 | } |
AnnaBridge | 167:e84263d55307 | 42 | |
AnnaBridge | 167:e84263d55307 | 43 | can_init_freq(&_can, rd, td, hz); |
AnnaBridge | 167:e84263d55307 | 44 | can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this); |
AnnaBridge | 167:e84263d55307 | 45 | } |
AnnaBridge | 167:e84263d55307 | 46 | |
<> | 149:156823d33999 | 47 | CAN::~CAN() { |
<> | 149:156823d33999 | 48 | // No lock needed in destructor |
<> | 149:156823d33999 | 49 | can_irq_free(&_can); |
<> | 149:156823d33999 | 50 | can_free(&_can); |
<> | 149:156823d33999 | 51 | } |
<> | 149:156823d33999 | 52 | |
<> | 149:156823d33999 | 53 | int CAN::frequency(int f) { |
<> | 149:156823d33999 | 54 | lock(); |
<> | 149:156823d33999 | 55 | int ret = can_frequency(&_can, f); |
<> | 149:156823d33999 | 56 | unlock(); |
<> | 149:156823d33999 | 57 | return ret; |
<> | 149:156823d33999 | 58 | } |
<> | 149:156823d33999 | 59 | |
<> | 149:156823d33999 | 60 | int CAN::write(CANMessage msg) { |
<> | 149:156823d33999 | 61 | lock(); |
<> | 149:156823d33999 | 62 | int ret = can_write(&_can, msg, 0); |
<> | 149:156823d33999 | 63 | unlock(); |
<> | 149:156823d33999 | 64 | return ret; |
<> | 149:156823d33999 | 65 | } |
<> | 149:156823d33999 | 66 | |
<> | 149:156823d33999 | 67 | int CAN::read(CANMessage &msg, int handle) { |
<> | 149:156823d33999 | 68 | lock(); |
<> | 149:156823d33999 | 69 | int ret = can_read(&_can, &msg, handle); |
<> | 149:156823d33999 | 70 | unlock(); |
<> | 149:156823d33999 | 71 | return ret; |
<> | 149:156823d33999 | 72 | } |
<> | 149:156823d33999 | 73 | |
<> | 149:156823d33999 | 74 | void CAN::reset() { |
<> | 149:156823d33999 | 75 | lock(); |
<> | 149:156823d33999 | 76 | can_reset(&_can); |
<> | 149:156823d33999 | 77 | unlock(); |
<> | 149:156823d33999 | 78 | } |
<> | 149:156823d33999 | 79 | |
<> | 149:156823d33999 | 80 | unsigned char CAN::rderror() { |
<> | 149:156823d33999 | 81 | lock(); |
<> | 149:156823d33999 | 82 | int ret = can_rderror(&_can); |
<> | 149:156823d33999 | 83 | unlock(); |
<> | 149:156823d33999 | 84 | return ret; |
<> | 149:156823d33999 | 85 | } |
<> | 149:156823d33999 | 86 | |
<> | 149:156823d33999 | 87 | unsigned char CAN::tderror() { |
<> | 149:156823d33999 | 88 | lock(); |
<> | 149:156823d33999 | 89 | int ret = can_tderror(&_can); |
<> | 149:156823d33999 | 90 | unlock(); |
<> | 149:156823d33999 | 91 | return ret; |
<> | 149:156823d33999 | 92 | } |
<> | 149:156823d33999 | 93 | |
<> | 149:156823d33999 | 94 | void CAN::monitor(bool silent) { |
<> | 149:156823d33999 | 95 | lock(); |
<> | 149:156823d33999 | 96 | can_monitor(&_can, (silent) ? 1 : 0); |
<> | 149:156823d33999 | 97 | unlock(); |
<> | 149:156823d33999 | 98 | } |
<> | 149:156823d33999 | 99 | |
<> | 149:156823d33999 | 100 | int CAN::mode(Mode mode) { |
<> | 149:156823d33999 | 101 | lock(); |
<> | 149:156823d33999 | 102 | int ret = can_mode(&_can, (CanMode)mode); |
<> | 149:156823d33999 | 103 | unlock(); |
<> | 149:156823d33999 | 104 | return ret; |
<> | 149:156823d33999 | 105 | } |
<> | 149:156823d33999 | 106 | |
<> | 149:156823d33999 | 107 | int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle) { |
<> | 149:156823d33999 | 108 | lock(); |
<> | 149:156823d33999 | 109 | int ret = can_filter(&_can, id, mask, format, handle); |
<> | 149:156823d33999 | 110 | unlock(); |
<> | 149:156823d33999 | 111 | return ret; |
<> | 149:156823d33999 | 112 | } |
<> | 149:156823d33999 | 113 | |
<> | 149:156823d33999 | 114 | void CAN::attach(Callback<void()> func, IrqType type) { |
<> | 149:156823d33999 | 115 | lock(); |
<> | 149:156823d33999 | 116 | if (func) { |
AnnaBridge | 174:b96e65c34a4d | 117 | // lock deep sleep only the first time |
AnnaBridge | 174:b96e65c34a4d | 118 | if (!_irq[(CanIrqType)type]) { |
AnnaBridge | 174:b96e65c34a4d | 119 | sleep_manager_lock_deep_sleep(); |
AnnaBridge | 174:b96e65c34a4d | 120 | } |
Anna Bridge |
163:74e0ce7f98e8 | 121 | _irq[(CanIrqType)type] = func; |
<> | 149:156823d33999 | 122 | can_irq_set(&_can, (CanIrqType)type, 1); |
<> | 149:156823d33999 | 123 | } else { |
AnnaBridge | 174:b96e65c34a4d | 124 | // unlock deep sleep only the first time |
AnnaBridge | 174:b96e65c34a4d | 125 | if (_irq[(CanIrqType)type]) { |
AnnaBridge | 174:b96e65c34a4d | 126 | sleep_manager_unlock_deep_sleep(); |
AnnaBridge | 174:b96e65c34a4d | 127 | } |
AnnaBridge | 174:b96e65c34a4d | 128 | _irq[(CanIrqType)type] = NULL; |
<> | 149:156823d33999 | 129 | can_irq_set(&_can, (CanIrqType)type, 0); |
<> | 149:156823d33999 | 130 | } |
<> | 149:156823d33999 | 131 | unlock(); |
<> | 149:156823d33999 | 132 | } |
<> | 149:156823d33999 | 133 | |
<> | 149:156823d33999 | 134 | void CAN::_irq_handler(uint32_t id, CanIrqType type) { |
<> | 149:156823d33999 | 135 | CAN *handler = (CAN*)id; |
AnnaBridge | 174:b96e65c34a4d | 136 | if (handler->_irq[type]) { |
AnnaBridge | 174:b96e65c34a4d | 137 | handler->_irq[type].call(); |
AnnaBridge | 174:b96e65c34a4d | 138 | } |
<> | 149:156823d33999 | 139 | } |
<> | 149:156823d33999 | 140 | |
<> | 149:156823d33999 | 141 | void CAN::lock() { |
<> | 149:156823d33999 | 142 | _mutex.lock(); |
<> | 149:156823d33999 | 143 | } |
<> | 149:156823d33999 | 144 | |
<> | 149:156823d33999 | 145 | void CAN::unlock() { |
<> | 149:156823d33999 | 146 | _mutex.unlock(); |
<> | 149:156823d33999 | 147 | } |
<> | 149:156823d33999 | 148 | |
<> | 149:156823d33999 | 149 | } // namespace mbed |
<> | 149:156823d33999 | 150 | |
<> | 149:156823d33999 | 151 | #endif |