Path: blob/master/libraries/AP_CANManager/AP_SLCANIface.h
9692 views
/*1* This file is free software: you can redistribute it and/or modify it2* under the terms of the GNU General Public License as published by the3* Free Software Foundation, either version 3 of the License, or4* (at your option) any later version.5*6* This file is distributed in the hope that it will be useful, but7* WITHOUT ANY WARRANTY; without even the implied warranty of8* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.9* See the GNU General Public License for more details.10*11* You should have received a copy of the GNU General Public License along12* with this program. If not, see <http://www.gnu.org/licenses/>.13*14* Code by Siddharth Bharat Purohit15*/1617#pragma once1819#include "AP_CANManager_config.h"2021#if AP_CAN_SLCAN_ENABLED2223#include <AP_HAL/AP_HAL.h>24#include "AP_HAL/utility/RingBuffer.h"25#include <AP_Param/AP_Param.h>2627#define SLCAN_BUFFER_SIZE 20028#define SLCAN_RX_QUEUE_SIZE 642930#ifndef HAL_CAN_RX_QUEUE_SIZE31#define HAL_CAN_RX_QUEUE_SIZE 12832#endif3334static_assert(HAL_CAN_RX_QUEUE_SIZE <= 254, "Invalid CAN Rx queue size");3536namespace SLCAN37{3839class CANIface: public AP_HAL::CANIface40{4142int16_t reportFrame(const AP_HAL::CANFrame& frame, uint64_t timestamp_usec);4344const char* processCommand(char* cmd);4546// pushes received frame into queue, false if failed47bool push_Frame(AP_HAL::CANFrame &frame);4849// Methods to handle different types of frames,50// return true if successfully received frame type51bool handle_FrameRTRStd(const char* cmd);52bool handle_FrameRTRExt(const char* cmd);53bool handle_FrameDataStd(const char* cmd);54bool handle_FrameDataExt(const char* cmd, bool canfd);5556bool handle_FDFrameDataExt(const char* cmd);5758// Parsing bytes received on the serial port59inline void addByte(const uint8_t byte);6061// track changes to slcan serial port62void update_slcan_port();6364bool initialized_;6566char buf_[SLCAN_BUFFER_SIZE + 1]; // buffer to record raw frame nibbles before parsing67int16_t pos_ = 0; // position in the buffer recording nibble frames before parsing68AP_HAL::UARTDriver* _port; // UART interface port reference to be used for SLCAN iface69bool _enabled; // Flag to check whether we are allowed to use _port7071ObjectBuffer<AP_HAL::CANIface::CanRxItem> rx_queue_; // Parsed Rx Frame queue7273const uint32_t _serial_lock_key = 0x53494442; // Key used to lock UART port for use by slcan7475AP_Int8 _slcan_can_port;76AP_Int8 _slcan_ser_port;77AP_Int8 _slcan_timeout;78AP_Int8 _slcan_start_delay;7980bool _slcan_start_req;81uint32_t _slcan_start_req_time;82int8_t _prev_ser_port;83int8_t _iface_num = -1;84uint32_t _last_had_activity;85uint8_t num_tries;86AP_HAL::CANIface* _can_iface; // Can interface to be used for interaction by SLCAN interface87HAL_Semaphore port_sem;88bool _set_by_sermgr;89public:90CANIface():91rx_queue_(HAL_CAN_RX_QUEUE_SIZE)92{93AP_Param::setup_object_defaults(this, var_info);94}9596static const struct AP_Param::GroupInfo var_info[];9798bool init(const uint32_t bitrate) override99{100return false;101}102103// Initialisation of SLCAN Passthrough method of operation104bool init_passthrough(uint8_t i);105106void set_can_iface(AP_HAL::CANIface* can_iface)107{108_can_iface = can_iface;109}110111void reset_params();112113// Overriden methods114bool set_event_handle(AP_HAL::BinarySemaphore *sem_handle) override;115uint32_t getErrorCount() const override;116void get_stats(ExpandingString &) override;117bool is_busoff() const override;118void flush_tx() override;119void clear_rx() override;120bool is_initialized() const override;121bool select(bool &read, bool &write,122const AP_HAL::CANFrame* const pending_tx,123uint64_t blocking_deadline) override;124int16_t send(const AP_HAL::CANFrame& frame, uint64_t tx_deadline,125AP_HAL::CANIface::CanIOFlags flags) override;126127int16_t receive(AP_HAL::CANFrame& out_frame, uint64_t& rx_time,128AP_HAL::CANIface::CanIOFlags& out_flags) override;129130protected:131int8_t get_iface_num() const override {132return _iface_num;133}134135bool add_to_rx_queue(const AP_HAL::CANIface::CanRxItem &frm) override {136return rx_queue_.push(frm);137}138bool is_enabled() const {139return (_port != nullptr) && _enabled;140}141};142143}144145#endif // AP_CAN_SLCAN_ENABLED146147148