APM:Libraries
CAN.h
Go to the documentation of this file.
1 /*
2  * This file is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by the
4  * Free Software Foundation, either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * This file is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License along
13  * with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  * Code by Andrew Tridgell and Siddharth Bharat Purohit
16  */
17 
18 #pragma once
19 
20 #include "AP_HAL_ChibiOS.h"
21 
22 #if HAL_WITH_UAVCAN
23 
24 #define UAVCAN_STM32_LOG(fmt, ...) hal.console->printf("CANManager: " fmt "\n", ##__VA_ARGS__)
25 
26 #include <uavcan/uavcan.hpp>
27 #include <uavcan/time.hpp>
28 
29 #include <uavcan_stm32/thread.hpp>
30 #include <uavcan_stm32/clock.hpp>
31 #include <uavcan_stm32/can.hpp>
32 
33 #include <AP_UAVCAN/AP_UAVCAN.h>
34 
35 #define MAX_NUMBER_OF_CAN_INTERFACES 2
36 #define MAX_NUMBER_OF_CAN_DRIVERS 2
37 #define CAN_STM32_RX_QUEUE_SIZE 64
38 class AP_UAVCAN;
39 
40 namespace ChibiOS {
44 class CANManager: public AP_HAL::CANManager {
45 public:
46  CANManager()
47  : can_helper(), AP_HAL::CANManager(&can_helper.driver) { }
48 
53  //bool hadActivity();
54 
55  static CANManager *from(AP_HAL::CANManager *can)
56  {
57  return static_cast<CANManager*>(can);
58  }
59 
60  bool begin(uint32_t bitrate, uint8_t can_number) override;
61 
62  /*
63  Test if CAN manager is ready and initialized
64  return false - CAN manager not initialized
65  true - CAN manager is initialized
66  */
67  bool is_initialized() override;
68  void initialized(bool val) override;
69 
70  AP_UAVCAN *get_UAVCAN(void) override;
71  void set_UAVCAN(AP_UAVCAN *uavcan) override;
72  void _timer_tick();
73 
74 private:
75  AP_UAVCAN *p_uavcan;
76  bool initialized_;
77  uint32_t bitrate_;
78  uavcan_stm32::CanInitHelper<CAN_STM32_RX_QUEUE_SIZE> can_helper;
79 };
80 
81 }
82 #endif