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/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
CANSensor class, for easy creation of CAN sensors using custom CAN protocols
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-04-18 04:24:17 -03:00
|
|
|
#include "AP_CAN.h"
|
|
|
|
#include "AP_CANDriver.h"
|
|
|
|
#ifndef HAL_BUILD_AP_PERIPH
|
2020-12-31 19:45:53 -04:00
|
|
|
#include "AP_CANManager.h"
|
2023-04-18 04:24:17 -03:00
|
|
|
#endif
|
2020-12-31 19:45:53 -04:00
|
|
|
|
|
|
|
#if HAL_MAX_CAN_PROTOCOL_DRIVERS
|
|
|
|
|
|
|
|
class CANSensor : public AP_CANDriver {
|
|
|
|
public:
|
2021-07-22 13:51:08 -03:00
|
|
|
CANSensor(const char *driver_name, uint16_t stack_size=2048);
|
|
|
|
|
2020-12-31 19:45:53 -04:00
|
|
|
/* Do not allow copies */
|
2022-09-30 06:50:43 -03:00
|
|
|
CLASS_NO_COPY(CANSensor);
|
2020-12-31 19:45:53 -04:00
|
|
|
|
|
|
|
void init(uint8_t driver_index, bool enable_filters) override;
|
|
|
|
bool add_interface(AP_HAL::CANIface* can_iface) override;
|
|
|
|
|
2024-04-30 23:26:16 -03:00
|
|
|
// Return true if this sensor has been successfully registered to a driver and initialized.
|
|
|
|
bool initialized() const { return _initialized; }
|
|
|
|
|
2020-12-31 19:45:53 -04:00
|
|
|
// handler for incoming frames
|
|
|
|
virtual void handle_frame(AP_HAL::CANFrame &frame) = 0;
|
|
|
|
|
2021-04-22 13:04:01 -03:00
|
|
|
// handler for outgoing frames
|
2024-10-13 23:56:01 -03:00
|
|
|
bool write_frame(AP_HAL::CANFrame &out_frame, const uint32_t timeout_us);
|
2021-04-22 13:04:01 -03:00
|
|
|
|
2021-06-08 21:58:53 -03:00
|
|
|
#ifdef HAL_BUILD_AP_PERIPH
|
2023-04-18 04:24:17 -03:00
|
|
|
static void set_periph(const uint8_t i, const AP_CAN::Protocol protocol, AP_HAL::CANIface* iface) {
|
2023-04-15 18:56:06 -03:00
|
|
|
if (i < ARRAY_SIZE(_periph)) {
|
2021-06-08 21:58:53 -03:00
|
|
|
_periph[i].protocol = protocol;
|
|
|
|
_periph[i].iface = iface;
|
|
|
|
}
|
|
|
|
}
|
2023-04-15 18:56:06 -03:00
|
|
|
|
|
|
|
// return driver type index i
|
2023-04-18 04:24:17 -03:00
|
|
|
static AP_CAN::Protocol get_driver_type(const uint8_t i)
|
2023-04-15 18:56:06 -03:00
|
|
|
{
|
|
|
|
if (i < ARRAY_SIZE(_periph)) {
|
|
|
|
return _periph[i].protocol;
|
|
|
|
}
|
2023-04-18 04:24:17 -03:00
|
|
|
return AP_CAN::Protocol::None;
|
2023-04-15 18:56:06 -03:00
|
|
|
}
|
|
|
|
#else
|
2023-04-18 04:24:17 -03:00
|
|
|
static AP_CAN::Protocol get_driver_type(const uint8_t i) { return AP::can().get_driver_type(i); }
|
2021-06-08 21:58:53 -03:00
|
|
|
#endif
|
|
|
|
|
2021-07-22 13:51:08 -03:00
|
|
|
protected:
|
2023-04-18 04:24:17 -03:00
|
|
|
void register_driver(AP_CAN::Protocol dtype);
|
2021-07-22 13:51:08 -03:00
|
|
|
|
2020-12-31 19:45:53 -04:00
|
|
|
private:
|
|
|
|
void loop();
|
|
|
|
|
|
|
|
const char *const _driver_name;
|
|
|
|
const uint16_t _stack_size;
|
|
|
|
bool _initialized;
|
|
|
|
uint8_t _driver_index;
|
2023-11-22 23:15:34 -04:00
|
|
|
|
|
|
|
// this is true when we are setup as an auxillary driver using CAN_Dn_PROTOCOL2
|
|
|
|
bool is_aux_11bit_driver;
|
|
|
|
|
2020-12-31 19:45:53 -04:00
|
|
|
AP_CANDriver *_can_driver;
|
2023-12-29 17:32:20 -04:00
|
|
|
HAL_BinarySemaphore sem_handle;
|
2020-12-31 19:45:53 -04:00
|
|
|
AP_HAL::CANIface* _can_iface;
|
2021-06-08 21:58:53 -03:00
|
|
|
|
|
|
|
#ifdef HAL_BUILD_AP_PERIPH
|
2023-04-18 04:24:17 -03:00
|
|
|
void register_driver_periph(const AP_CAN::Protocol dtype);
|
2021-06-08 21:58:53 -03:00
|
|
|
|
|
|
|
struct CANSensor_Periph {
|
|
|
|
AP_HAL::CANIface* iface;
|
2023-04-18 04:24:17 -03:00
|
|
|
AP_CAN::Protocol protocol;
|
2021-06-08 21:58:53 -03:00
|
|
|
} static _periph[HAL_NUM_CAN_IFACES];
|
|
|
|
#endif
|
2020-12-31 19:45:53 -04:00
|
|
|
};
|
|
|
|
|
2024-02-22 21:59:50 -04:00
|
|
|
// a class to allow for multiple CAN backends with one
|
|
|
|
// CANSensor driver. This can be shared among different libraries like rangefinder and proximity
|
|
|
|
class MultiCAN : public CANSensor {
|
|
|
|
public:
|
|
|
|
// callback functor def for forwarding frames
|
|
|
|
FUNCTOR_TYPEDEF(ForwardCanFrame, bool, AP_HAL::CANFrame &);
|
|
|
|
|
|
|
|
MultiCAN(ForwardCanFrame cf, AP_CAN::Protocol can_type, const char *driver_name);
|
|
|
|
|
|
|
|
// handle a received frame from the CAN bus
|
|
|
|
void handle_frame(AP_HAL::CANFrame &frame) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// class to allow for multiple callbacks implemented as a linked list
|
|
|
|
class MultiCANLinkedList {
|
|
|
|
public:
|
|
|
|
struct CANSensor_Multi {
|
|
|
|
ForwardCanFrame _callback;
|
|
|
|
CANSensor_Multi* next = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
// register a callback for a CAN frame by adding it to the linked list
|
|
|
|
void register_callback(ForwardCanFrame callback);
|
|
|
|
|
|
|
|
// distribute the CAN frame to the registered callbacks
|
|
|
|
void handle_frame(AP_HAL::CANFrame &frame);
|
|
|
|
HAL_Semaphore sem;
|
|
|
|
|
|
|
|
private:
|
|
|
|
CANSensor_Multi* head = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Pointer to static instance of the linked list for persistence across instances
|
|
|
|
static MultiCANLinkedList* callbacks;
|
|
|
|
};
|
|
|
|
|
2020-12-31 19:45:53 -04:00
|
|
|
#endif // HAL_MAX_CAN_PROTOCOL_DRIVERS
|
|
|
|
|