iofirmware.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #pragma once
  2. #include <stdint.h>
  3. #include <AP_HAL/AP_HAL.h>
  4. #include <AP_Common/AP_Common.h>
  5. #include <AP_RCProtocol/AP_RCProtocol.h>
  6. #include "ch.h"
  7. #include "ioprotocol.h"
  8. #define PWM_IGNORE_THIS_CHANNEL UINT16_MAX
  9. #define SERVO_COUNT 8
  10. class AP_IOMCU_FW {
  11. public:
  12. void process_io_packet();
  13. struct IOPacket rx_io_packet, tx_io_packet;
  14. void init();
  15. void update();
  16. void calculate_fw_crc(void);
  17. void pwm_out_update();
  18. void heater_update();
  19. void rcin_update();
  20. bool handle_code_write();
  21. bool handle_code_read();
  22. void schedule_reboot(uint32_t time_ms);
  23. void safety_update();
  24. void rcout_mode_update();
  25. void rcin_serial_init();
  26. void rcin_serial_update();
  27. void page_status_update(void);
  28. void fill_failsafe_pwm(void);
  29. void run_mixer(void);
  30. int16_t mix_input_angle(uint8_t channel, uint16_t radio_in) const;
  31. int16_t mix_input_range(uint8_t channel, uint16_t radio_in) const;
  32. uint16_t mix_output_angle(uint8_t channel, int16_t angle) const;
  33. uint16_t mix_output_range(uint8_t channel, int16_t value) const;
  34. int16_t mix_elevon_vtail(int16_t angle1, int16_t angle2, bool first_output) const;
  35. void dsm_bind_step(void);
  36. struct {
  37. /* default to RSSI ADC functionality */
  38. uint16_t features;
  39. uint16_t arming;
  40. uint16_t pwm_rates;
  41. uint16_t pwm_defaultrate = 50;
  42. uint16_t pwm_altrate = 200;
  43. uint16_t relays_pad;
  44. uint16_t vbatt_scale = 10000;
  45. uint16_t reserved1;
  46. uint16_t reserved2;
  47. uint16_t set_debug;
  48. uint16_t reboot_bl;
  49. uint16_t crc[2];
  50. uint16_t rc_thr_failsafe_us;
  51. uint16_t reserved3;
  52. uint16_t pwm_reverse;
  53. uint16_t trim_roll;
  54. uint16_t trim_pitch;
  55. uint16_t trim_yaw;
  56. uint16_t sbus_rate = 72;
  57. uint16_t ignore_safety;
  58. uint16_t heater_duty_cycle = 0xFFFFU;
  59. uint16_t pwm_altclock = 1;
  60. } reg_setup;
  61. // CONFIG values
  62. struct page_config config;
  63. // PAGE_STATUS values
  64. struct page_reg_status reg_status;
  65. // PAGE_RAW_RCIN values
  66. struct page_rc_input rc_input;
  67. uint32_t rc_last_input_ms;
  68. // PAGE_SERVO values
  69. struct {
  70. uint16_t pwm[IOMCU_MAX_CHANNELS];
  71. } reg_servo;
  72. // PAGE_DIRECT_PWM values
  73. struct {
  74. uint16_t pwm[IOMCU_MAX_CHANNELS];
  75. } reg_direct_pwm;
  76. // PAGE_FAILSAFE_PWM
  77. struct {
  78. uint16_t pwm[IOMCU_MAX_CHANNELS];
  79. } reg_failsafe_pwm;
  80. // PAGE_SAFETY_PWM
  81. struct {
  82. uint16_t pwm[IOMCU_MAX_CHANNELS];
  83. } reg_safety_pwm;
  84. // output rates
  85. struct {
  86. uint16_t freq;
  87. uint16_t chmask;
  88. uint16_t default_freq = 50;
  89. uint16_t sbus_rate_hz;
  90. } rate;
  91. // MIXER values
  92. struct page_mixing mixing;
  93. // true when override channel active
  94. bool override_active;
  95. // sbus rate handling
  96. uint32_t sbus_last_ms;
  97. uint32_t sbus_interval_ms;
  98. uint32_t fmu_data_received_time;
  99. uint32_t last_heater_ms;
  100. uint32_t reboot_time;
  101. bool do_reboot;
  102. bool update_default_rate;
  103. bool update_rcout_freq;
  104. bool has_heater;
  105. const bool heater_pwm_polarity = IOMCU_IMU_HEATER_POLARITY;
  106. uint32_t last_blue_led_ms;
  107. uint32_t safety_update_ms;
  108. uint32_t safety_button_counter;
  109. uint8_t led_counter;
  110. uint32_t last_loop_ms;
  111. bool oneshot_enabled;
  112. bool brushed_enabled;
  113. thread_t *thread_ctx;
  114. bool last_safety_off;
  115. uint32_t last_status_ms;
  116. uint32_t last_ms;
  117. uint32_t loop_counter;
  118. uint8_t dsm_bind_state;
  119. uint32_t last_dsm_bind_ms;
  120. uint32_t last_failsafe_ms;
  121. };
  122. // GPIO macros
  123. #define HEATER_SET(on) palWriteLine(HAL_GPIO_PIN_HEATER, (on));
  124. #define BLUE_TOGGLE() palToggleLine(HAL_GPIO_PIN_HEATER);
  125. #define AMBER_SET(on) palWriteLine(HAL_GPIO_PIN_AMBER_LED, !(on));
  126. #define SPEKTRUM_POWER(on) palWriteLine(HAL_GPIO_PIN_SPEKTRUM_PWR_EN, on);
  127. #define SPEKTRUM_SET(on) palWriteLine(HAL_GPIO_PIN_SPEKTRUM_OUT, on);