Working fork to test F0 application

Dependents:   ppCANOpen_Example

Fork of CANnucleo by Zoltan Hudak

CAN.cpp

Committer:
hudakz
Date:
2015-07-19
Revision:
0:e29bc8e0dddd
Child:
5:b53e5ee15315

File content as of revision 0:e29bc8e0dddd:

/* mbed Microcontroller Library
 * Copyright (c) 2006-2013 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Modified by Zoltan Hudak    <hudakz@inbox.com>
 *
 */
#include "CAN.h"
//#if DEVICE_CAN
#include "cmsis.h"

namespace   mbed
{
    
/**
 * @brief
 * @note
 * @param
 * @retval
 */
CAN::CAN(PinName rxPin, PinName txPin) :
    _can(),
    _irq() {
    can_init(&_can, rxPin, txPin);
    can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this);
}

/**
 * @brief
 * @note
 * @param
 * @retval
 */
CAN::~CAN(void) {
    can_irq_free(&_can);
    can_free(&_can);
}

/**
 * @brief
 * @note
 * @param
 * @retval
 */
int CAN::frequency(int f) {
    return can_frequency(&_can, f);
}

/**
 * @brief
 * @note
 * @param
 * @retval
 */
int CAN::write(CANMessage msg) {
    return can_write(&_can, msg, 0);
}

/**
 * @brief
 * @note
 * @param
 * @retval
 */
int CAN::read(CANMessage& msg, int handle) {
    return can_read(&_can, &msg, handle);
}

/**
 * @brief
 * @note
 * @param
 * @retval
 */
void CAN::reset(void) {
    can_reset(&_can);
}

/**
 * @brief
 * @note
 * @param
 * @retval
 */
unsigned char CAN::rderror(void) {
    return can_rderror(&_can);
}

/**
 * @brief
 * @note
 * @param
 * @retval
 */
unsigned char CAN::tderror(void) {
    return can_tderror(&_can);
}

/**
 * @brief
 * @note
 * @param
 * @retval
 */
void CAN::monitor(bool silent) {
    can_monitor(&_can, (silent) ? 1 : 0);
}

/**
 * @brief
 * @note
 * @param
 * @retval
 */
int CAN::mode(Mode mode) {
    return can_mode(&_can, (CanMode) mode);
}

/**
 * @brief
 * @note
 * @param
 * @retval
 */
int CAN::filter(unsigned int id, unsigned int mask, CANFormat format /* CANAny */, int handle /* 0 */) {
    return can_filter(&_can, id, mask, format, handle);
}

/**
 * @brief   Attaches handler funcion to CAN1 RX0 Interrupt
 * @note    Only CAN1 RX0 Interrupt supported
 * @param   fptr: pointer to a void (*)(void) function
 * @param   type: not used (only CAN1 RX0 Interrupt supported) 
 * @retval
 */
void CAN::attach(void (*fptr) (void), IrqType type) {
    HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
    if(fptr) {
        can_irq_set(fptr);
    }
    can_irq_init(&_can, &CAN::_irq_handler, (uint32_t) this);
    HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
}

/**
 * @brief
 * @note
 * @param
 * @retval
 */
void CAN::_irq_handler(uint32_t id, CanIrqType type) {
    CAN*    handler = (CAN*)id;
    handler->_irq[type].call();
}

}   // namespace mbed