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