Line data Source code
1 : #pragma once
2 :
3 : class AP_Param;
4 :
5 : #include "AP_HAL_Namespace.h"
6 :
7 : #include "AnalogIn.h"
8 : #include "GPIO.h"
9 : #include "RCInput.h"
10 : #include "RCOutput.h"
11 : #include "SPIDevice.h"
12 : #include "WSPIDevice.h"
13 : #include "Storage.h"
14 : #include "UARTDriver.h"
15 : #include "system.h"
16 : #include "OpticalFlow.h"
17 : #include "DSP.h"
18 : #include "CANIface.h"
19 :
20 :
21 : class AP_HAL::HAL {
22 : public:
23 2193 : HAL(AP_HAL::UARTDriver* _serial0, // console
24 : AP_HAL::UARTDriver* _serial1, // telem1
25 : AP_HAL::UARTDriver* _serial2, // telem2
26 : AP_HAL::UARTDriver* _serial3, // 1st GPS
27 : AP_HAL::UARTDriver* _serial4, // 2nd GPS
28 : AP_HAL::UARTDriver* _serial5, // extra1
29 : AP_HAL::UARTDriver* _serial6, // extra2
30 : AP_HAL::UARTDriver* _serial7, // extra3
31 : AP_HAL::UARTDriver* _serial8, // extra4
32 : AP_HAL::UARTDriver* _serial9, // extra5
33 : AP_HAL::I2CDeviceManager* _i2c_mgr,
34 : AP_HAL::SPIDeviceManager* _spi,
35 : AP_HAL::WSPIDeviceManager* _wspi,
36 : AP_HAL::AnalogIn* _analogin,
37 : AP_HAL::Storage* _storage,
38 : AP_HAL::UARTDriver* _console,
39 : AP_HAL::GPIO* _gpio,
40 : AP_HAL::RCInput* _rcin,
41 : AP_HAL::RCOutput* _rcout,
42 : AP_HAL::Scheduler* _scheduler,
43 : AP_HAL::Util* _util,
44 : AP_HAL::OpticalFlow*_opticalflow,
45 : AP_HAL::Flash* _flash,
46 : #if AP_SIM_ENABLED
47 : // note that not much of _simstate is used on AP_HAL_SITL, but
48 : // more is being moved in!
49 : class AP_HAL::SIMState* _simstate,
50 : #endif
51 : #if HAL_WITH_DSP
52 : AP_HAL::DSP* _dsp,
53 : #endif
54 : #if HAL_NUM_CAN_IFACES > 0
55 : AP_HAL::CANIface* _can_ifaces[HAL_NUM_CAN_IFACES])
56 : #else
57 : AP_HAL::CANIface** _can_ifaces)
58 : #endif
59 2193 : :
60 2193 : i2c_mgr(_i2c_mgr),
61 2193 : spi(_spi),
62 2193 : wspi(_wspi),
63 2193 : analogin(_analogin),
64 2193 : storage(_storage),
65 2193 : console(_console),
66 2193 : gpio(_gpio),
67 2193 : rcin(_rcin),
68 2193 : rcout(_rcout),
69 2193 : scheduler(_scheduler),
70 2193 : util(_util),
71 2193 : opticalflow(_opticalflow),
72 2193 : flash(_flash),
73 : #if HAL_WITH_DSP
74 2145 : dsp(_dsp),
75 : #endif
76 2193 : serial_array{
77 : _serial0,
78 : _serial1,
79 : _serial2,
80 : _serial3,
81 : _serial4,
82 : _serial5,
83 : _serial6,
84 : _serial7,
85 : _serial8,
86 48 : _serial9}
87 : #if AP_SIM_ENABLED
88 2145 : ,simstate(_simstate)
89 : #endif
90 : {
91 : #if HAL_NUM_CAN_IFACES > 0
92 2193 : if (_can_ifaces == nullptr) {
93 0 : for (uint8_t i = 0; i < HAL_NUM_CAN_IFACES; i++)
94 0 : can[i] = nullptr;
95 : } else {
96 6579 : for (uint8_t i = 0; i < HAL_NUM_CAN_IFACES; i++)
97 4386 : can[i] = _can_ifaces[i];
98 : }
99 : #endif
100 :
101 2193 : AP_HAL::init();
102 2193 : }
103 :
104 : struct Callbacks {
105 : virtual void setup() = 0;
106 : virtual void loop() = 0;
107 : };
108 :
109 : struct FunCallbacks : public Callbacks {
110 : FunCallbacks(void (*setup_fun)(void), void (*loop_fun)(void));
111 :
112 : void setup() override { _setup(); }
113 : void loop() override { _loop(); }
114 :
115 : private:
116 : void (*_setup)(void);
117 : void (*_loop)(void);
118 : };
119 :
120 : virtual void run(int argc, char * const argv[], Callbacks* callbacks) const = 0;
121 :
122 : public:
123 : AP_HAL::I2CDeviceManager* i2c_mgr;
124 : AP_HAL::SPIDeviceManager* spi;
125 : AP_HAL::WSPIDeviceManager* wspi;
126 : AP_HAL::AnalogIn* analogin;
127 : AP_HAL::Storage* storage;
128 : AP_HAL::UARTDriver* console;
129 : AP_HAL::GPIO* gpio;
130 : AP_HAL::RCInput* rcin;
131 : AP_HAL::RCOutput* rcout;
132 : AP_HAL::Scheduler* scheduler;
133 : AP_HAL::Util *util;
134 : AP_HAL::OpticalFlow *opticalflow;
135 : AP_HAL::Flash *flash;
136 : AP_HAL::DSP *dsp;
137 : #if HAL_NUM_CAN_IFACES > 0
138 : AP_HAL::CANIface* can[HAL_NUM_CAN_IFACES];
139 : #else
140 : AP_HAL::CANIface** can;
141 : #endif
142 :
143 : // access to serial ports using SERIALn_ numbering
144 : UARTDriver* serial(uint8_t sernum) const;
145 :
146 : static constexpr uint8_t num_serial = 10;
147 :
148 : private:
149 : // UART drivers in SERIALn_ order
150 : AP_HAL::UARTDriver* serial_array[num_serial];
151 :
152 : public:
153 : #if AP_SIM_ENABLED
154 : AP_HAL::SIMState *simstate;
155 : #endif
156 :
157 : #ifndef HAL_CONSOLE_DISABLED
158 : # define DEV_PRINTF(fmt, args ...) do { hal.console->printf(fmt, ## args); } while(0)
159 : #else
160 : # define DEV_PRINTF(fmt, args ...)
161 : #endif
162 :
163 : };
164 :
165 : // access serial ports using SERIALn numbering
166 2522803726 : inline AP_HAL::UARTDriver* AP_HAL::HAL::serial(uint8_t sernum) const
167 : {
168 2522803726 : if (sernum >= ARRAY_SIZE(serial_array)) {
169 14 : return nullptr;
170 : }
171 2522803712 : return serial_array[sernum];
172 : }
|