RCOutput.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. #pragma once
  18. #include "AP_HAL_ChibiOS.h"
  19. #include "shared_dma.h"
  20. #include "ch.h"
  21. #include "hal.h"
  22. #if HAL_USE_PWM == TRUE
  23. #if !STM32_DMA_ADVANCED
  24. #define DISABLE_DSHOT
  25. #endif
  26. class ChibiOS::RCOutput : public AP_HAL::RCOutput {
  27. public:
  28. void init() override;
  29. void set_freq(uint32_t chmask, uint16_t freq_hz) override;
  30. uint16_t get_freq(uint8_t ch) override;
  31. void enable_ch(uint8_t ch) override;
  32. void disable_ch(uint8_t ch) override;
  33. void write(uint8_t ch, uint16_t period_us) override;
  34. uint16_t read(uint8_t ch) override;
  35. void read(uint16_t* period_us, uint8_t len) override;
  36. uint16_t read_last_sent(uint8_t ch) override;
  37. void read_last_sent(uint16_t* period_us, uint8_t len) override;
  38. void set_esc_scaling(uint16_t min_pwm, uint16_t max_pwm) override {
  39. _esc_pwm_min = min_pwm;
  40. _esc_pwm_max = max_pwm;
  41. }
  42. bool get_esc_scaling(uint16_t &min_pwm, uint16_t &max_pwm) override {
  43. min_pwm = _esc_pwm_min;
  44. max_pwm = _esc_pwm_max;
  45. return true;
  46. }
  47. void set_output_mode(uint16_t mask, enum output_mode mode) override;
  48. float scale_esc_to_unity(uint16_t pwm) override {
  49. return 2.0 * ((float) pwm - _esc_pwm_min) / (_esc_pwm_max - _esc_pwm_min) - 1.0;
  50. }
  51. void cork(void) override;
  52. void push(void) override;
  53. /*
  54. force the safety switch on, disabling PWM output from the IO board
  55. */
  56. bool force_safety_on(void) override;
  57. /*
  58. force the safety switch off, enabling PWM output from the IO board
  59. */
  60. void force_safety_off(void) override;
  61. /*
  62. set PWM to send to a set of channels when the safety switch is
  63. in the safe state
  64. */
  65. void set_safety_pwm(uint32_t chmask, uint16_t period_us) override;
  66. bool enable_px4io_sbus_out(uint16_t rate_hz) override;
  67. /*
  68. set default update rate
  69. */
  70. void set_default_rate(uint16_t rate_hz) override;
  71. /*
  72. timer push (for oneshot min rate)
  73. */
  74. void timer_tick(void) override;
  75. /*
  76. setup for serial output to a set of ESCs, using the given
  77. baudrate. Assumes 1 start bit, 1 stop bit, LSB first and 8
  78. databits. This is used for ESC configuration and firmware
  79. flashing
  80. */
  81. bool setup_serial_output(uint16_t chan_mask, ByteBuffer *buffer, uint32_t baudrate);
  82. /*
  83. setup for serial output to an ESC using the given
  84. baudrate. Assumes 1 start bit, 1 stop bit, LSB first and 8
  85. databits. This is used for passthrough ESC configuration and
  86. firmware flashing
  87. While serial output is active normal output to this channel is
  88. suspended. Output to some other channels (such as those in the
  89. same channel timer group) may also be stopped, depending on the
  90. implementation
  91. */
  92. bool serial_setup_output(uint8_t chan, uint32_t baudrate, uint16_t motor_mask) override;
  93. /*
  94. write a set of bytes to an ESC, using settings from
  95. serial_setup_output. This is a blocking call
  96. */
  97. bool serial_write_bytes(const uint8_t *bytes, uint16_t len) override;
  98. /*
  99. read a byte from a port, using serial parameters from serial_setup_output()
  100. return the number of bytes read
  101. */
  102. uint16_t serial_read_bytes(uint8_t *buf, uint16_t len) override;
  103. /*
  104. stop serial output. This restores the previous output mode for
  105. the channel and any other channels that were stopped by
  106. serial_setup_output()
  107. */
  108. void serial_end(void) override;
  109. /*
  110. enable telemetry request for a mask of channels. This is used
  111. with DShot to get telemetry feedback
  112. */
  113. void set_telem_request_mask(uint16_t mask) override { telem_request_mask = (mask >> chan_offset); }
  114. /*
  115. get safety switch state, used by Util.cpp
  116. */
  117. AP_HAL::Util::safety_state _safety_switch_state(void);
  118. /*
  119. set PWM to send to a set of channels if the FMU firmware dies
  120. */
  121. void set_failsafe_pwm(uint32_t chmask, uint16_t period_us) override;
  122. /*
  123. set safety mask for IOMCU
  124. */
  125. void set_safety_mask(uint16_t mask) { safety_mask = mask; }
  126. /*
  127. * mark the channels in chanmask as reversible. This is needed for some ESC types (such as DShot)
  128. * so that output scaling can be performed correctly. The chanmask passed is added (ORed) into
  129. * any existing mask.
  130. */
  131. void set_reversible_mask(uint16_t chanmask) override {
  132. reversible_mask |= chanmask;
  133. }
  134. /*
  135. setup neopixel (WS2812B) output for a given channel number, with
  136. the given max number of LEDs in the chain.
  137. */
  138. bool set_neopixel_num_LEDs(const uint16_t chan, uint8_t num_leds) override;
  139. /*
  140. setup neopixel (WS2812B) output data for a given output channel
  141. and mask of LEDs on the channel
  142. */
  143. void set_neopixel_rgb_data(const uint16_t chan, uint32_t ledmask, uint8_t red, uint8_t green, uint8_t blue) override;
  144. /*
  145. trigger send of neopixel data
  146. */
  147. void neopixel_send(void) override;
  148. private:
  149. struct pwm_group {
  150. // only advanced timers can do high clocks needed for more than 400Hz
  151. bool advanced_timer;
  152. uint8_t chan[4]; // chan number, zero based, 255 for disabled
  153. PWMConfig pwm_cfg;
  154. PWMDriver* pwm_drv;
  155. bool have_up_dma; // can we do DMAR outputs for DShot?
  156. uint8_t dma_up_stream_id;
  157. uint8_t dma_up_channel;
  158. uint8_t alt_functions[4];
  159. ioline_t pal_lines[4];
  160. // below this line is not initialised by hwdef.h
  161. enum output_mode current_mode;
  162. uint16_t frequency_hz;
  163. uint16_t ch_mask;
  164. const stm32_dma_stream_t *dma;
  165. Shared_DMA *dma_handle;
  166. uint32_t *dma_buffer;
  167. uint16_t dma_buffer_len;
  168. bool have_lock;
  169. bool pwm_started;
  170. uint32_t bit_width_mul;
  171. uint32_t rc_frequency;
  172. bool in_serial_dma;
  173. uint64_t last_dmar_send_us;
  174. virtual_timer_t dma_timeout;
  175. uint8_t neopixel_nleds;
  176. // serial output
  177. struct {
  178. // expected time per bit
  179. uint32_t bit_time_us;
  180. // channel to output to within group (0 to 3)
  181. uint8_t chan;
  182. // thread waiting for byte to be written
  183. thread_t *waiter;
  184. } serial;
  185. };
  186. /*
  187. structure for IRQ handler for soft-serial input
  188. */
  189. static struct irq_state {
  190. // ioline for port being read
  191. ioline_t line;
  192. // time the current byte started
  193. systime_t byte_start_tick;
  194. // number of bits we have read in this byte
  195. uint8_t nbits;
  196. // bitmask of bits so far (includes start and stop bits)
  197. uint16_t bitmask;
  198. // value of completed byte (includes start and stop bits)
  199. uint16_t byteval;
  200. // expected time per bit in system ticks
  201. systime_t bit_time_tick;
  202. // the bit value of the last bit received
  203. uint8_t last_bit;
  204. // thread waiting for byte to be read
  205. thread_t *waiter;
  206. // timeout for byte read
  207. virtual_timer_t serial_timeout;
  208. bool timed_out;
  209. } irq;
  210. // the group being used for serial output
  211. struct pwm_group *serial_group;
  212. thread_t *serial_thread;
  213. tprio_t serial_priority;
  214. static pwm_group pwm_group_list[];
  215. uint16_t _esc_pwm_min;
  216. uint16_t _esc_pwm_max;
  217. // offset of first local channel
  218. uint8_t chan_offset;
  219. // total number of channels on FMU
  220. uint8_t num_fmu_channels;
  221. // number of active fmu channels
  222. uint8_t active_fmu_channels;
  223. static const uint8_t max_channels = 16;
  224. // last sent values are for all channels
  225. uint16_t last_sent[max_channels];
  226. // these values are for the local channels. Non-local channels are handled by IOMCU
  227. uint32_t en_mask;
  228. uint16_t period[max_channels];
  229. uint16_t safe_pwm[max_channels]; // pwm to use when safety is on
  230. bool corked;
  231. // mask of channels that are running in high speed
  232. uint16_t fast_channel_mask;
  233. uint16_t io_fast_channel_mask;
  234. uint16_t reversible_mask;
  235. // min time to trigger next pulse to prevent overlap
  236. uint64_t min_pulse_trigger_us;
  237. // mutex for oneshot triggering
  238. mutex_t trigger_mutex;
  239. // which output groups need triggering
  240. uint8_t trigger_groupmask;
  241. // widest pulse for oneshot triggering
  242. uint16_t trigger_widest_pulse;
  243. // are we using oneshot125 for the iomcu?
  244. bool iomcu_oneshot125;
  245. // find a channel group given a channel number
  246. struct pwm_group *find_chan(uint8_t chan, uint8_t &group_idx);
  247. // push out values to local PWM
  248. void push_local(void);
  249. // trigger group pulses
  250. void trigger_groups(void);
  251. // setup output frequency for a group
  252. void set_freq_group(pwm_group &group);
  253. // safety switch state
  254. AP_HAL::Util::safety_state safety_state;
  255. uint32_t safety_update_ms;
  256. uint8_t led_counter;
  257. int8_t safety_button_counter;
  258. uint8_t safety_press_count; // 0.1s units
  259. // mask of channels to allow when safety on
  260. uint16_t safety_mask;
  261. // update safety switch and LED
  262. void safety_update(void);
  263. /*
  264. DShot handling
  265. */
  266. // the pre-bit is needed with TIM5, or we can get some corrupt frames
  267. const uint8_t dshot_pre = 1;
  268. const uint8_t dshot_post = 2;
  269. const uint16_t dshot_bit_length = 16 + dshot_pre + dshot_post;
  270. const uint16_t dshot_buffer_length = dshot_bit_length*4*sizeof(uint32_t);
  271. static const uint16_t dshot_min_gap_us = 100;
  272. uint32_t dshot_pulse_time_us;
  273. uint16_t telem_request_mask;
  274. /*
  275. NeoPixel handling. Max of 32 LEDs uses max 12k of memory per group
  276. */
  277. void neopixel_send(pwm_group &group);
  278. bool neopixel_pending;
  279. void dma_allocate(Shared_DMA *ctx);
  280. void dma_deallocate(Shared_DMA *ctx);
  281. uint16_t create_dshot_packet(const uint16_t value, bool telem_request);
  282. void fill_DMA_buffer_dshot(uint32_t *buffer, uint8_t stride, uint16_t packet, uint16_t clockmul);
  283. void dshot_send(pwm_group &group, bool blocking);
  284. static void dma_irq_callback(void *p, uint32_t flags);
  285. static void dma_unlock(void *p);
  286. bool mode_requires_dma(enum output_mode mode) const;
  287. bool setup_group_DMA(pwm_group &group, uint32_t bitrate, uint32_t bit_width, bool active_high, const uint16_t buffer_length);
  288. void send_pulses_DMAR(pwm_group &group, uint32_t buffer_length);
  289. void set_group_mode(pwm_group &group);
  290. bool is_dshot_protocol(const enum output_mode mode) const;
  291. uint32_t protocol_bitrate(const enum output_mode mode) const;
  292. // serial output support
  293. static const eventmask_t serial_event_mask = EVENT_MASK(1);
  294. bool serial_write_byte(uint8_t b);
  295. bool serial_read_byte(uint8_t &b);
  296. void fill_DMA_buffer_byte(uint32_t *buffer, uint8_t stride, uint8_t b , uint32_t bitval);
  297. static void serial_bit_irq(void);
  298. static void serial_byte_timeout(void *ctx);
  299. };
  300. #endif // HAL_USE_PWM