AP_HAL_ChibiOS: add CAN driver

This commit is contained in:
Siddharth Purohit 2018-02-02 00:50:41 +05:30 committed by Andrew Tridgell
parent 5bcbc4c142
commit 3c009cf02c
2 changed files with 167 additions and 0 deletions

View File

@ -0,0 +1,84 @@
/*
* This file is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Code by Andrew Tridgell and Siddharth Bharat Purohit
* Based on stm32 can driver by Pavel Kirienko
*/
#include "CAN.h"
#if HAL_WITH_UAVCAN
#include <uavcan_stm32/../../src/internal.hpp>
using namespace ChibiOS;
using namespace uavcan_stm32;
extern const AP_HAL::HAL& hal;
namespace uavcan_stm32 {
uint64_t clock::getUtcUSecFromCanInterrupt()
{
return AP_HAL::micros64();
}
uavcan::MonotonicTime clock::getMonotonic()
{
return uavcan::MonotonicTime::fromUSec(AP_HAL::micros64());
}
}
bool CANManager::begin(uint32_t bitrate, uint8_t can_number)
{
if (can_helper.init(bitrate, CanIface::OperatingMode::NormalMode, can_number) == 0) {
bitrate_ = bitrate;
initialized_ = true;
}
return initialized_;
}
bool CANManager::is_initialized()
{
return initialized_;
}
void CANManager::initialized(bool val)
{
initialized_ = val;
}
AP_UAVCAN *CANManager::get_UAVCAN(void)
{
return p_uavcan;
}
void CANManager::set_UAVCAN(AP_UAVCAN *uavcan)
{
p_uavcan = uavcan;
}
void CANManager::_timer_tick()
{
if (!initialized_) return;
if (p_uavcan != nullptr) {
p_uavcan->do_cyclic();
} else {
hal.console->printf("p_uavcan is nullptr");
}
}
#endif //HAL_WITH_UAVCAN

View File

@ -0,0 +1,83 @@
/*
* This file is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Code by Andrew Tridgell and Siddharth Bharat Purohit
*/
#pragma once
#include "AP_HAL_ChibiOS.h"
#if HAL_WITH_UAVCAN
#define UAVCAN_STM32_LOG(fmt, ...) hal.console->printf("CANManager: " fmt "\n", ##__VA_ARGS__)
#include <uavcan/uavcan.hpp>
#include <uavcan/time.hpp>
#include <uavcan_stm32/thread.hpp>
#include <uavcan_stm32/clock.hpp>
#include <uavcan_stm32/can.hpp>
#include <AP_UAVCAN/AP_UAVCAN.h>
#define MAX_NUMBER_OF_CAN_INTERFACES 2
#define MAX_NUMBER_OF_CAN_DRIVERS 2
#define CAN_STM32_RX_QUEUE_SIZE 64
class AP_UAVCAN;
using namespace uavcan_stm32;
namespace ChibiOS {
/**
* Generic CAN driver.
*/
class CANManager: public AP_HAL::CANManager {
public:
CANManager()
: can_helper(), AP_HAL::CANManager(&can_helper.driver) { }
/**
* Whether at least one iface had at least one successful IO since previous call of this method.
* This is designed for use with iface activity LEDs.
*/
//bool hadActivity();
static CANManager *from(AP_HAL::CANManager *can)
{
return static_cast<CANManager*>(can);
}
bool begin(uint32_t bitrate, uint8_t can_number) override;
/*
Test if CAN manager is ready and initialized
return false - CAN manager not initialized
true - CAN manager is initialized
*/
bool is_initialized() override;
void initialized(bool val) override;
AP_UAVCAN *get_UAVCAN(void) override;
void set_UAVCAN(AP_UAVCAN *uavcan) override;
void _timer_tick();
private:
AP_UAVCAN *p_uavcan;
bool initialized_;
uint32_t bitrate_;
CanInitHelper<CAN_STM32_RX_QUEUE_SIZE> can_helper;
};
}
#endif