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