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