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