Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-dev by
CAN.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #include "drivers/CAN.h" 00017 00018 #if DEVICE_CAN 00019 00020 #include "cmsis.h" 00021 00022 namespace mbed { 00023 00024 static void donothing() {} 00025 00026 CAN::CAN(PinName rd, PinName td) : _can(), _irq() { 00027 // No lock needed in constructor 00028 00029 for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) { 00030 _irq[i] = callback(donothing); 00031 } 00032 00033 can_init(&_can, rd, td); 00034 can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this); 00035 } 00036 00037 CAN::CAN(PinName rd, PinName td, int hz) : _can(), _irq() { 00038 // No lock needed in constructor 00039 00040 for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) { 00041 _irq[i].attach(donothing); 00042 } 00043 00044 can_init_freq(&_can, rd, td, hz); 00045 can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this); 00046 } 00047 00048 CAN::~CAN() { 00049 // No lock needed in destructor 00050 can_irq_free(&_can); 00051 can_free(&_can); 00052 } 00053 00054 int CAN::frequency(int f) { 00055 lock(); 00056 int ret = can_frequency(&_can, f); 00057 unlock(); 00058 return ret; 00059 } 00060 00061 int CAN::write(CANMessage msg) { 00062 lock(); 00063 int ret = can_write(&_can, msg, 0); 00064 unlock(); 00065 return ret; 00066 } 00067 00068 int CAN::read(CANMessage &msg, int handle) { 00069 lock(); 00070 int ret = can_read(&_can, &msg, handle); 00071 unlock(); 00072 return ret; 00073 } 00074 00075 void CAN::reset() { 00076 lock(); 00077 can_reset(&_can); 00078 unlock(); 00079 } 00080 00081 unsigned char CAN::rderror() { 00082 lock(); 00083 int ret = can_rderror(&_can); 00084 unlock(); 00085 return ret; 00086 } 00087 00088 unsigned char CAN::tderror() { 00089 lock(); 00090 int ret = can_tderror(&_can); 00091 unlock(); 00092 return ret; 00093 } 00094 00095 void CAN::monitor(bool silent) { 00096 lock(); 00097 can_monitor(&_can, (silent) ? 1 : 0); 00098 unlock(); 00099 } 00100 00101 int CAN::mode(Mode mode) { 00102 lock(); 00103 int ret = can_mode(&_can, (CanMode)mode); 00104 unlock(); 00105 return ret; 00106 } 00107 00108 int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle) { 00109 lock(); 00110 int ret = can_filter(&_can, id, mask, format, handle); 00111 unlock(); 00112 return ret; 00113 } 00114 00115 void CAN::attach(Callback<void()> func, IrqType type) { 00116 lock(); 00117 if (func) { 00118 _irq[(CanIrqType)type] = func; 00119 can_irq_set(&_can, (CanIrqType)type, 1); 00120 } else { 00121 _irq[(CanIrqType)type] = callback(donothing); 00122 can_irq_set(&_can, (CanIrqType)type, 0); 00123 } 00124 unlock(); 00125 } 00126 00127 void CAN::_irq_handler(uint32_t id, CanIrqType type) { 00128 CAN *handler = (CAN*)id; 00129 handler->_irq[type].call(); 00130 } 00131 00132 void CAN::lock() { 00133 _mutex.lock(); 00134 } 00135 00136 void CAN::unlock() { 00137 _mutex.unlock(); 00138 } 00139 00140 } // namespace mbed 00141 00142 #endif
Generated on Tue Jul 12 2022 12:11:33 by
1.7.2
