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