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