2020-12-31 19:45:53 -04:00
|
|
|
/*
|
|
|
|
This program 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 program 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/>.
|
|
|
|
*/
|
|
|
|
/*
|
2023-10-11 04:41:51 -03:00
|
|
|
generic CAN sensor class, for easy creation of CAN sensors using proprietary protocols
|
2020-12-31 19:45:53 -04:00
|
|
|
*/
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
|
|
|
#if HAL_MAX_CAN_PROTOCOL_DRIVERS
|
|
|
|
|
|
|
|
#include <AP_Scheduler/AP_Scheduler.h>
|
|
|
|
#include "AP_CANSensor.h"
|
2024-02-22 21:59:50 -04:00
|
|
|
#include <AP_BoardConfig/AP_BoardConfig.h>
|
2020-12-31 19:45:53 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2021-06-20 03:30:07 -03:00
|
|
|
#if HAL_CANMANAGER_ENABLED
|
2020-12-31 19:45:53 -04:00
|
|
|
#define debug_can(level_debug, fmt, args...) do { AP::can().log_text(level_debug, _driver_name, fmt, ##args); } while (0)
|
2021-06-08 21:58:53 -03:00
|
|
|
#else
|
|
|
|
#define debug_can(level_debug, fmt, args...)
|
|
|
|
#endif
|
2020-12-31 19:45:53 -04:00
|
|
|
|
2021-07-22 13:51:08 -03:00
|
|
|
CANSensor::CANSensor(const char *driver_name, uint16_t stack_size) :
|
2020-12-31 19:45:53 -04:00
|
|
|
_driver_name(driver_name),
|
|
|
|
_stack_size(stack_size)
|
2021-07-22 13:51:08 -03:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
2023-04-18 04:24:17 -03:00
|
|
|
void CANSensor::register_driver(AP_CAN::Protocol dtype)
|
2020-12-31 19:45:53 -04:00
|
|
|
{
|
2021-06-20 03:30:07 -03:00
|
|
|
#if HAL_CANMANAGER_ENABLED
|
2020-12-31 19:45:53 -04:00
|
|
|
if (!AP::can().register_driver(dtype, this)) {
|
2023-11-22 23:15:34 -04:00
|
|
|
if (AP::can().register_11bit_driver(dtype, this, _driver_index)) {
|
|
|
|
is_aux_11bit_driver = true;
|
|
|
|
_can_driver = AP::can().get_driver(_driver_index);
|
|
|
|
_initialized = true;
|
|
|
|
} else {
|
|
|
|
debug_can(AP_CANManager::LOG_ERROR, "Failed to register CANSensor %s", _driver_name);
|
|
|
|
}
|
2020-12-31 19:45:53 -04:00
|
|
|
} else {
|
2021-07-22 13:51:08 -03:00
|
|
|
debug_can(AP_CANManager::LOG_INFO, "%s: constructed", _driver_name);
|
2020-12-31 19:45:53 -04:00
|
|
|
}
|
2021-06-08 21:58:53 -03:00
|
|
|
#elif defined(HAL_BUILD_AP_PERIPH)
|
|
|
|
register_driver_periph(dtype);
|
|
|
|
#endif
|
2020-12-31 19:45:53 -04:00
|
|
|
}
|
2021-07-22 13:51:08 -03:00
|
|
|
|
|
|
|
|
2021-06-08 21:58:53 -03:00
|
|
|
#ifdef HAL_BUILD_AP_PERIPH
|
|
|
|
CANSensor::CANSensor_Periph CANSensor::_periph[HAL_NUM_CAN_IFACES];
|
|
|
|
|
2023-04-18 04:24:17 -03:00
|
|
|
void CANSensor::register_driver_periph(const AP_CAN::Protocol dtype)
|
2021-06-08 21:58:53 -03:00
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < HAL_NUM_CAN_IFACES; i++) {
|
|
|
|
if (_periph[i].protocol != dtype) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!add_interface(_periph[i].iface)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
init(0, false); // TODO: allow multiple drivers
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2020-12-31 19:45:53 -04:00
|
|
|
|
|
|
|
void CANSensor::init(uint8_t driver_index, bool enable_filters)
|
|
|
|
{
|
|
|
|
_driver_index = driver_index;
|
|
|
|
|
|
|
|
debug_can(AP_CANManager::LOG_INFO, "starting init");
|
|
|
|
|
|
|
|
if (_initialized) {
|
|
|
|
debug_can(AP_CANManager::LOG_ERROR, "already initialized");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:58:53 -03:00
|
|
|
#ifndef HAL_BUILD_AP_PERIPH
|
2020-12-31 19:45:53 -04:00
|
|
|
// get CAN manager instance
|
|
|
|
_can_driver = AP::can().get_driver(driver_index);
|
|
|
|
|
|
|
|
if (_can_driver == nullptr) {
|
|
|
|
debug_can(AP_CANManager::LOG_ERROR, "no CAN driver");
|
|
|
|
return;
|
|
|
|
}
|
2021-06-08 21:58:53 -03:00
|
|
|
#endif
|
2020-12-31 19:45:53 -04:00
|
|
|
|
|
|
|
// start thread for receiving and sending CAN frames
|
|
|
|
if (!hal.scheduler->thread_create(FUNCTOR_BIND_MEMBER(&CANSensor::loop, void), _driver_name, _stack_size, AP_HAL::Scheduler::PRIORITY_CAN, 0)) {
|
|
|
|
debug_can(AP_CANManager::LOG_ERROR, "couldn't create thread");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_initialized = true;
|
|
|
|
|
|
|
|
debug_can(AP_CANManager::LOG_INFO, "init done");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CANSensor::add_interface(AP_HAL::CANIface* can_iface)
|
|
|
|
{
|
|
|
|
if (_can_iface != nullptr) {
|
|
|
|
debug_can(AP_CANManager::LOG_ERROR, "Multiple Interface not supported");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_can_iface = can_iface;
|
|
|
|
|
|
|
|
if (_can_iface == nullptr) {
|
|
|
|
debug_can(AP_CANManager::LOG_ERROR, "CAN driver not found");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_can_iface->is_initialized()) {
|
|
|
|
debug_can(AP_CANManager::LOG_ERROR, "Driver not initialized");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-12-29 17:32:20 -04:00
|
|
|
if (!_can_iface->set_event_handle(&sem_handle)) {
|
2020-12-31 19:45:53 -04:00
|
|
|
debug_can(AP_CANManager::LOG_ERROR, "Cannot add event handle");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-10-13 23:56:01 -03:00
|
|
|
bool CANSensor::write_frame(AP_HAL::CANFrame &out_frame, const uint32_t timeout_us)
|
2021-04-22 13:04:01 -03:00
|
|
|
{
|
|
|
|
if (!_initialized) {
|
|
|
|
debug_can(AP_CANManager::LOG_ERROR, "Driver not initialized for write_frame");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-11-22 23:15:34 -04:00
|
|
|
if (is_aux_11bit_driver && _can_driver != nullptr) {
|
|
|
|
return _can_driver->write_aux_frame(out_frame, timeout_us);
|
|
|
|
}
|
|
|
|
|
2021-04-22 13:04:01 -03:00
|
|
|
bool read_select = false;
|
|
|
|
bool write_select = true;
|
2023-08-21 22:41:12 -03:00
|
|
|
bool ret = _can_iface->select(read_select, write_select, &out_frame, AP_HAL::micros64() + timeout_us);
|
2021-04-22 13:04:01 -03:00
|
|
|
if (!ret || !write_select) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-21 22:41:12 -03:00
|
|
|
uint64_t deadline = AP_HAL::micros64() + 2000000;
|
2021-04-22 13:04:01 -03:00
|
|
|
return (_can_iface->send(out_frame, deadline, AP_HAL::CANIface::AbortOnError) == 1);
|
|
|
|
}
|
|
|
|
|
2020-12-31 19:45:53 -04:00
|
|
|
void CANSensor::loop()
|
|
|
|
{
|
|
|
|
while (!hal.scheduler->is_system_initialized()) {
|
|
|
|
// don't process packets till startup complete
|
|
|
|
hal.scheduler->delay(1);
|
|
|
|
}
|
2021-06-08 21:58:53 -03:00
|
|
|
|
|
|
|
#ifdef HAL_BUILD_AP_PERIPH
|
|
|
|
const uint32_t LOOP_INTERVAL_US = 1000;
|
|
|
|
#else
|
2020-12-31 19:45:53 -04:00
|
|
|
const uint32_t LOOP_INTERVAL_US = AP::scheduler().get_loop_period_us();
|
2021-06-08 21:58:53 -03:00
|
|
|
#endif
|
|
|
|
|
2020-12-31 19:45:53 -04:00
|
|
|
while (true) {
|
2024-10-13 23:56:01 -03:00
|
|
|
uint64_t deadline_us = AP_HAL::micros64() + LOOP_INTERVAL_US;
|
2020-12-31 19:45:53 -04:00
|
|
|
|
|
|
|
// wait to receive frame
|
|
|
|
bool read_select = true;
|
|
|
|
bool write_select = false;
|
2024-10-13 23:56:01 -03:00
|
|
|
bool ret = _can_iface->select(read_select, write_select, nullptr, deadline_us);
|
2020-12-31 19:45:53 -04:00
|
|
|
if (ret && read_select) {
|
|
|
|
uint64_t time;
|
|
|
|
AP_HAL::CANIface::CanIOFlags flags {};
|
|
|
|
|
|
|
|
AP_HAL::CANFrame frame;
|
|
|
|
int16_t res = _can_iface->receive(frame, time, flags);
|
|
|
|
|
|
|
|
if (res == 1) {
|
|
|
|
handle_frame(frame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-22 21:59:50 -04:00
|
|
|
MultiCAN::MultiCANLinkedList* MultiCAN::callbacks;
|
|
|
|
|
|
|
|
MultiCAN::MultiCAN(ForwardCanFrame cf, AP_CAN::Protocol can_type, const char *driver_name) :
|
|
|
|
CANSensor(driver_name)
|
|
|
|
{
|
|
|
|
if (callbacks == nullptr) {
|
2024-05-26 22:24:09 -03:00
|
|
|
callbacks = NEW_NOTHROW MultiCANLinkedList();
|
2024-02-22 21:59:50 -04:00
|
|
|
}
|
|
|
|
if (callbacks == nullptr) {
|
|
|
|
AP_BoardConfig::allocation_error("Failed to create multican callback");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register new driver
|
|
|
|
register_driver(can_type);
|
|
|
|
callbacks->register_callback(cf);
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle a received frame from the CAN bus
|
|
|
|
void MultiCAN::handle_frame(AP_HAL::CANFrame &frame)
|
|
|
|
{
|
|
|
|
if (callbacks != nullptr) {
|
|
|
|
callbacks->handle_frame(frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// register a callback for a CAN frame by adding it to the linked list
|
|
|
|
void MultiCAN::MultiCANLinkedList::register_callback(ForwardCanFrame callback)
|
|
|
|
{
|
2024-05-26 22:24:09 -03:00
|
|
|
CANSensor_Multi* newNode = NEW_NOTHROW CANSensor_Multi();
|
2024-02-22 21:59:50 -04:00
|
|
|
if (newNode == nullptr) {
|
|
|
|
AP_BoardConfig::allocation_error("Failed to create multican node");
|
|
|
|
}
|
|
|
|
WITH_SEMAPHORE(sem);
|
|
|
|
{
|
|
|
|
newNode->_callback = callback;
|
|
|
|
newNode->next = head;
|
|
|
|
head = newNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// distribute the CAN frame to the registered callbacks
|
|
|
|
void MultiCAN::MultiCANLinkedList::handle_frame(AP_HAL::CANFrame &frame)
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(sem);
|
|
|
|
for (CANSensor_Multi* current = head; current != nullptr; current = current->next) {
|
|
|
|
if (current->_callback(frame)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-31 19:45:53 -04:00
|
|
|
#endif // HAL_MAX_CAN_PROTOCOL_DRIVERS
|
|
|
|
|