This is the final version of Mini Gateway for Automation and Security desgined for Renesas GR Peach Design Contest

Dependencies:   GR-PEACH_video GraphicsFramework HTTPServer R_BSP mbed-rpc mbed-rtos Socket lwip-eth lwip-sys lwip FATFileSystem

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CAN.cpp Source File

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 (int i = 0; i < sizeof _irq / sizeof _irq[0]; i++) {
00030         _irq[i].attach(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() {
00038     // No lock needed in destructor
00039     can_irq_free(&_can);
00040     can_free(&_can);
00041 }
00042 
00043 int CAN::frequency(int f) {
00044     lock();
00045     int ret = can_frequency(&_can, f);
00046     unlock();
00047     return ret;
00048 }
00049 
00050 int CAN::write(CANMessage msg) {
00051     lock();
00052     int ret = can_write(&_can, msg, 0);
00053     unlock();
00054     return ret;
00055 }
00056 
00057 int CAN::read(CANMessage &msg, int handle) {
00058     lock();
00059     int ret = can_read(&_can, &msg, handle);
00060     unlock();
00061     return ret;
00062 }
00063 
00064 void CAN::reset() {
00065     lock();
00066     can_reset(&_can);
00067     unlock();
00068 }
00069 
00070 unsigned char CAN::rderror() {
00071     lock();
00072     int ret = can_rderror(&_can);
00073     unlock();
00074     return ret;
00075 }
00076 
00077 unsigned char CAN::tderror() {
00078     lock();
00079     int ret = can_tderror(&_can);
00080     unlock();
00081     return ret;
00082 }
00083 
00084 void CAN::monitor(bool silent) {
00085     lock();
00086     can_monitor(&_can, (silent) ? 1 : 0);
00087     unlock();
00088 }
00089 
00090 int CAN::mode(Mode mode) {
00091     lock();
00092     int ret = can_mode(&_can, (CanMode)mode);
00093     unlock();
00094     return ret;
00095 }
00096 
00097 int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle) {
00098     lock();
00099     int ret = can_filter(&_can, id, mask, format, handle);
00100     unlock();
00101     return ret;
00102 }
00103 
00104 void CAN::attach(Callback<void()> func, IrqType type) {
00105     lock();
00106     if (func) {
00107         _irq[(CanIrqType)type].attach(func);
00108         can_irq_set(&_can, (CanIrqType)type, 1);
00109     } else {
00110         _irq[(CanIrqType)type].attach(donothing);
00111         can_irq_set(&_can, (CanIrqType)type, 0);
00112     }
00113     unlock();
00114 }
00115 
00116 void CAN::_irq_handler(uint32_t id, CanIrqType type) {
00117     CAN *handler = (CAN*)id;
00118     handler->_irq[type].call();
00119 }
00120 
00121 void CAN::lock() {
00122     _mutex.lock();
00123 }
00124 
00125 void CAN::unlock() {
00126     _mutex.unlock();
00127 }
00128 
00129 } // namespace mbed
00130 
00131 #endif