MCU driver/HAL for the Picocell Gateway concentrator board. The firmware implements either a USB CDC protocol or a UART protocol to bridge commands coming from host to the SX1308 SPI interface.
src/mbed-dev/common/CAN.cpp@0:c76361bd82e8, 2018-04-11 (annotated)
- Committer:
- dgabino
- Date:
- Wed Apr 11 14:42:47 2018 +0000
- Revision:
- 0:c76361bd82e8
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dgabino | 0:c76361bd82e8 | 1 | /* mbed Microcontroller Library |
dgabino | 0:c76361bd82e8 | 2 | * Copyright (c) 2006-2013 ARM Limited |
dgabino | 0:c76361bd82e8 | 3 | * |
dgabino | 0:c76361bd82e8 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
dgabino | 0:c76361bd82e8 | 5 | * you may not use this file except in compliance with the License. |
dgabino | 0:c76361bd82e8 | 6 | * You may obtain a copy of the License at |
dgabino | 0:c76361bd82e8 | 7 | * |
dgabino | 0:c76361bd82e8 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
dgabino | 0:c76361bd82e8 | 9 | * |
dgabino | 0:c76361bd82e8 | 10 | * Unless required by applicable law or agreed to in writing, software |
dgabino | 0:c76361bd82e8 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
dgabino | 0:c76361bd82e8 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
dgabino | 0:c76361bd82e8 | 13 | * See the License for the specific language governing permissions and |
dgabino | 0:c76361bd82e8 | 14 | * limitations under the License. |
dgabino | 0:c76361bd82e8 | 15 | */ |
dgabino | 0:c76361bd82e8 | 16 | #include "CAN.h" |
dgabino | 0:c76361bd82e8 | 17 | |
dgabino | 0:c76361bd82e8 | 18 | #if DEVICE_CAN |
dgabino | 0:c76361bd82e8 | 19 | |
dgabino | 0:c76361bd82e8 | 20 | #include "cmsis.h" |
dgabino | 0:c76361bd82e8 | 21 | |
dgabino | 0:c76361bd82e8 | 22 | namespace mbed { |
dgabino | 0:c76361bd82e8 | 23 | |
dgabino | 0:c76361bd82e8 | 24 | CAN::CAN(PinName rd, PinName td) : _can(), _irq() { |
dgabino | 0:c76361bd82e8 | 25 | // No lock needed in constructor |
dgabino | 0:c76361bd82e8 | 26 | can_init(&_can, rd, td); |
dgabino | 0:c76361bd82e8 | 27 | can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this); |
dgabino | 0:c76361bd82e8 | 28 | } |
dgabino | 0:c76361bd82e8 | 29 | |
dgabino | 0:c76361bd82e8 | 30 | CAN::~CAN() { |
dgabino | 0:c76361bd82e8 | 31 | // No lock needed in destructor |
dgabino | 0:c76361bd82e8 | 32 | can_irq_free(&_can); |
dgabino | 0:c76361bd82e8 | 33 | can_free(&_can); |
dgabino | 0:c76361bd82e8 | 34 | } |
dgabino | 0:c76361bd82e8 | 35 | |
dgabino | 0:c76361bd82e8 | 36 | int CAN::frequency(int f) { |
dgabino | 0:c76361bd82e8 | 37 | lock(); |
dgabino | 0:c76361bd82e8 | 38 | int ret = can_frequency(&_can, f); |
dgabino | 0:c76361bd82e8 | 39 | unlock(); |
dgabino | 0:c76361bd82e8 | 40 | return ret; |
dgabino | 0:c76361bd82e8 | 41 | } |
dgabino | 0:c76361bd82e8 | 42 | |
dgabino | 0:c76361bd82e8 | 43 | int CAN::write(CANMessage msg) { |
dgabino | 0:c76361bd82e8 | 44 | lock(); |
dgabino | 0:c76361bd82e8 | 45 | int ret = can_write(&_can, msg, 0); |
dgabino | 0:c76361bd82e8 | 46 | unlock(); |
dgabino | 0:c76361bd82e8 | 47 | return ret; |
dgabino | 0:c76361bd82e8 | 48 | } |
dgabino | 0:c76361bd82e8 | 49 | |
dgabino | 0:c76361bd82e8 | 50 | int CAN::read(CANMessage &msg, int handle) { |
dgabino | 0:c76361bd82e8 | 51 | lock(); |
dgabino | 0:c76361bd82e8 | 52 | int ret = can_read(&_can, &msg, handle); |
dgabino | 0:c76361bd82e8 | 53 | unlock(); |
dgabino | 0:c76361bd82e8 | 54 | return ret; |
dgabino | 0:c76361bd82e8 | 55 | } |
dgabino | 0:c76361bd82e8 | 56 | |
dgabino | 0:c76361bd82e8 | 57 | void CAN::reset() { |
dgabino | 0:c76361bd82e8 | 58 | lock(); |
dgabino | 0:c76361bd82e8 | 59 | can_reset(&_can); |
dgabino | 0:c76361bd82e8 | 60 | unlock(); |
dgabino | 0:c76361bd82e8 | 61 | } |
dgabino | 0:c76361bd82e8 | 62 | |
dgabino | 0:c76361bd82e8 | 63 | unsigned char CAN::rderror() { |
dgabino | 0:c76361bd82e8 | 64 | lock(); |
dgabino | 0:c76361bd82e8 | 65 | int ret = can_rderror(&_can); |
dgabino | 0:c76361bd82e8 | 66 | unlock(); |
dgabino | 0:c76361bd82e8 | 67 | return ret; |
dgabino | 0:c76361bd82e8 | 68 | } |
dgabino | 0:c76361bd82e8 | 69 | |
dgabino | 0:c76361bd82e8 | 70 | unsigned char CAN::tderror() { |
dgabino | 0:c76361bd82e8 | 71 | lock(); |
dgabino | 0:c76361bd82e8 | 72 | int ret = can_tderror(&_can); |
dgabino | 0:c76361bd82e8 | 73 | unlock(); |
dgabino | 0:c76361bd82e8 | 74 | return ret; |
dgabino | 0:c76361bd82e8 | 75 | } |
dgabino | 0:c76361bd82e8 | 76 | |
dgabino | 0:c76361bd82e8 | 77 | void CAN::monitor(bool silent) { |
dgabino | 0:c76361bd82e8 | 78 | lock(); |
dgabino | 0:c76361bd82e8 | 79 | can_monitor(&_can, (silent) ? 1 : 0); |
dgabino | 0:c76361bd82e8 | 80 | unlock(); |
dgabino | 0:c76361bd82e8 | 81 | } |
dgabino | 0:c76361bd82e8 | 82 | |
dgabino | 0:c76361bd82e8 | 83 | int CAN::mode(Mode mode) { |
dgabino | 0:c76361bd82e8 | 84 | lock(); |
dgabino | 0:c76361bd82e8 | 85 | int ret = can_mode(&_can, (CanMode)mode); |
dgabino | 0:c76361bd82e8 | 86 | unlock(); |
dgabino | 0:c76361bd82e8 | 87 | return ret; |
dgabino | 0:c76361bd82e8 | 88 | } |
dgabino | 0:c76361bd82e8 | 89 | |
dgabino | 0:c76361bd82e8 | 90 | int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle) { |
dgabino | 0:c76361bd82e8 | 91 | lock(); |
dgabino | 0:c76361bd82e8 | 92 | int ret = can_filter(&_can, id, mask, format, handle); |
dgabino | 0:c76361bd82e8 | 93 | unlock(); |
dgabino | 0:c76361bd82e8 | 94 | return ret; |
dgabino | 0:c76361bd82e8 | 95 | } |
dgabino | 0:c76361bd82e8 | 96 | |
dgabino | 0:c76361bd82e8 | 97 | void CAN::attach(Callback<void()> func, IrqType type) { |
dgabino | 0:c76361bd82e8 | 98 | lock(); |
dgabino | 0:c76361bd82e8 | 99 | if (func) { |
dgabino | 0:c76361bd82e8 | 100 | _irq[(CanIrqType)type].attach(func); |
dgabino | 0:c76361bd82e8 | 101 | can_irq_set(&_can, (CanIrqType)type, 1); |
dgabino | 0:c76361bd82e8 | 102 | } else { |
dgabino | 0:c76361bd82e8 | 103 | can_irq_set(&_can, (CanIrqType)type, 0); |
dgabino | 0:c76361bd82e8 | 104 | } |
dgabino | 0:c76361bd82e8 | 105 | unlock(); |
dgabino | 0:c76361bd82e8 | 106 | } |
dgabino | 0:c76361bd82e8 | 107 | |
dgabino | 0:c76361bd82e8 | 108 | void CAN::_irq_handler(uint32_t id, CanIrqType type) { |
dgabino | 0:c76361bd82e8 | 109 | CAN *handler = (CAN*)id; |
dgabino | 0:c76361bd82e8 | 110 | handler->_irq[type].call(); |
dgabino | 0:c76361bd82e8 | 111 | } |
dgabino | 0:c76361bd82e8 | 112 | |
dgabino | 0:c76361bd82e8 | 113 | void CAN::lock() { |
dgabino | 0:c76361bd82e8 | 114 | _mutex.lock(); |
dgabino | 0:c76361bd82e8 | 115 | } |
dgabino | 0:c76361bd82e8 | 116 | |
dgabino | 0:c76361bd82e8 | 117 | void CAN::unlock() { |
dgabino | 0:c76361bd82e8 | 118 | _mutex.unlock(); |
dgabino | 0:c76361bd82e8 | 119 | } |
dgabino | 0:c76361bd82e8 | 120 | |
dgabino | 0:c76361bd82e8 | 121 | } // namespace mbed |
dgabino | 0:c76361bd82e8 | 122 | |
dgabino | 0:c76361bd82e8 | 123 | #endif |