2017-12-26 19:03:21 -04:00
|
|
|
/*
|
|
|
|
implement protocol for controlling an IO microcontroller
|
|
|
|
|
|
|
|
For bootstrapping this will initially implement the px4io protocol,
|
|
|
|
but will later move to an ArduPilot specific protocol
|
|
|
|
*/
|
2023-04-30 20:29:02 -03:00
|
|
|
#pragma once
|
2017-12-26 19:03:21 -04:00
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2018-01-02 01:47:42 -04:00
|
|
|
|
|
|
|
#if HAL_WITH_IO_MCU
|
|
|
|
|
2018-05-10 21:37:01 -03:00
|
|
|
#include "iofirmware/ioprotocol.h"
|
2018-10-30 21:07:47 -03:00
|
|
|
#include <AP_RCMapper/AP_RCMapper.h>
|
2023-06-17 19:26:56 -03:00
|
|
|
#include <AP_HAL/RCOutput.h>
|
2023-06-23 17:15:14 -03:00
|
|
|
#include <AP_ESC_Telem/AP_ESC_Telem_Backend.h>
|
2018-01-16 06:21:59 -04:00
|
|
|
|
2022-02-21 03:38:21 -04:00
|
|
|
typedef uint32_t eventmask_t;
|
|
|
|
typedef struct ch_thread thread_t;
|
|
|
|
|
2023-06-23 17:15:14 -03:00
|
|
|
class AP_IOMCU
|
|
|
|
#ifdef HAL_WITH_ESC_TELEM
|
|
|
|
: public AP_ESC_Telem_Backend
|
|
|
|
#endif
|
|
|
|
{
|
2017-12-26 19:03:21 -04:00
|
|
|
public:
|
|
|
|
AP_IOMCU(AP_HAL::UARTDriver &uart);
|
|
|
|
|
2017-12-27 03:24:15 -04:00
|
|
|
void init(void);
|
|
|
|
|
|
|
|
// write to one channel
|
2017-12-26 19:03:21 -04:00
|
|
|
void write_channel(uint8_t chan, uint16_t pwm);
|
2017-12-27 03:24:15 -04:00
|
|
|
|
|
|
|
// read from one channel
|
|
|
|
uint16_t read_channel(uint8_t chan);
|
|
|
|
|
|
|
|
// cork output
|
|
|
|
void cork(void);
|
|
|
|
|
|
|
|
// push output
|
|
|
|
void push(void);
|
|
|
|
|
|
|
|
// set output frequency
|
|
|
|
void set_freq(uint16_t chmask, uint16_t freq);
|
|
|
|
|
|
|
|
// get output frequency
|
|
|
|
uint16_t get_freq(uint16_t chan);
|
|
|
|
|
|
|
|
// get state of safety switch
|
|
|
|
AP_HAL::Util::safety_state get_safety_switch_state(void) const;
|
|
|
|
|
|
|
|
// force safety on
|
|
|
|
bool force_safety_on(void);
|
|
|
|
|
|
|
|
// force safety off
|
|
|
|
void force_safety_off(void);
|
|
|
|
|
2018-04-14 00:54:04 -03:00
|
|
|
// set mask of channels that ignore safety state
|
|
|
|
void set_safety_mask(uint16_t chmask);
|
2018-09-12 17:22:26 -03:00
|
|
|
|
|
|
|
// set PWM of channels when in FMU failsafe
|
|
|
|
void set_failsafe_pwm(uint16_t chmask, uint16_t period_us);
|
2019-10-20 10:47:14 -03:00
|
|
|
|
2017-12-31 22:28:59 -04:00
|
|
|
/*
|
|
|
|
enable sbus output
|
|
|
|
*/
|
|
|
|
bool enable_sbus_out(uint16_t rate_hz);
|
|
|
|
|
|
|
|
/*
|
|
|
|
check for new RC input
|
|
|
|
*/
|
|
|
|
bool check_rcinput(uint32_t &last_frame_us, uint8_t &num_channels, uint16_t *channels, uint8_t max_channels);
|
2017-12-31 22:51:10 -04:00
|
|
|
|
2018-08-26 12:06:10 -03:00
|
|
|
// Do DSM receiver binding
|
|
|
|
void bind_dsm(uint8_t mode);
|
|
|
|
|
2018-11-05 01:44:20 -04:00
|
|
|
// get the name of the RC protocol
|
|
|
|
const char *get_rc_protocol(void);
|
|
|
|
|
2019-12-02 03:46:36 -04:00
|
|
|
// get receiver RSSI
|
|
|
|
int16_t get_RSSI(void) const {
|
|
|
|
return rc_input.rssi;
|
|
|
|
}
|
|
|
|
|
2017-12-31 22:51:10 -04:00
|
|
|
/*
|
2021-09-28 13:40:08 -03:00
|
|
|
get servo rail voltage adc counts
|
2017-12-31 22:51:10 -04:00
|
|
|
*/
|
2021-09-28 13:40:08 -03:00
|
|
|
uint16_t get_vservo_adc_count(void) const { return reg_status.vservo; }
|
2017-12-31 22:51:10 -04:00
|
|
|
|
|
|
|
/*
|
2021-09-28 13:40:08 -03:00
|
|
|
get rssi voltage adc counts
|
2017-12-31 22:51:10 -04:00
|
|
|
*/
|
2021-09-28 13:40:08 -03:00
|
|
|
uint16_t get_vrssi_adc_count(void) const { return reg_status.vrssi; }
|
2018-01-03 02:25:30 -04:00
|
|
|
|
|
|
|
// set target for IMU heater
|
|
|
|
void set_heater_duty_cycle(uint8_t duty_cycle);
|
|
|
|
|
2018-01-12 23:52:29 -04:00
|
|
|
// set default output rate
|
|
|
|
void set_default_rate(uint16_t rate_hz);
|
2018-01-16 01:37:14 -04:00
|
|
|
|
|
|
|
// set to oneshot mode
|
|
|
|
void set_oneshot_mode(void);
|
|
|
|
|
2018-07-12 23:28:43 -03:00
|
|
|
// set to brushed mode
|
|
|
|
void set_brushed_mode(void);
|
2019-10-20 10:47:14 -03:00
|
|
|
|
2020-07-11 09:22:05 -03:00
|
|
|
// set output mode
|
|
|
|
void set_output_mode(uint16_t mask, uint16_t mode);
|
|
|
|
|
2023-06-23 17:15:14 -03:00
|
|
|
// set bi-directional mask
|
|
|
|
void set_bidir_dshot_mask(uint16_t mask);
|
|
|
|
|
2024-08-13 09:59:08 -03:00
|
|
|
// set reversible mask
|
|
|
|
void set_reversible_mask(uint16_t mask);
|
|
|
|
|
2023-06-17 19:26:56 -03:00
|
|
|
// get output mode
|
|
|
|
AP_HAL::RCOutput::output_mode get_output_mode(uint8_t& mask) const;
|
|
|
|
|
|
|
|
// MCUID
|
|
|
|
uint32_t get_mcu_id() const { return config.mcuid; }
|
|
|
|
|
2023-06-24 15:16:43 -03:00
|
|
|
// CPUID
|
|
|
|
uint32_t get_cpu_id() const { return config.cpuid; }
|
|
|
|
|
2023-05-24 17:43:17 -03:00
|
|
|
#if HAL_DSHOT_ENABLED
|
|
|
|
// set dshot output period
|
|
|
|
void set_dshot_period(uint16_t period_us, uint8_t drate);
|
|
|
|
|
|
|
|
// set telem request mask
|
|
|
|
void set_telem_request_mask(uint32_t mask);
|
|
|
|
|
2023-06-23 17:15:14 -03:00
|
|
|
// set the dshot esc_type
|
|
|
|
void set_dshot_esc_type(AP_HAL::RCOutput::DshotEscType dshot_esc_type);
|
|
|
|
|
2023-05-24 17:43:17 -03:00
|
|
|
// send a dshot command
|
|
|
|
void send_dshot_command(uint8_t command, uint8_t chan, uint32_t command_timeout_ms, uint16_t repeat_count, bool priority);
|
|
|
|
#endif
|
|
|
|
// setup channels
|
|
|
|
void enable_ch(uint8_t ch);
|
|
|
|
void disable_ch(uint8_t ch);
|
|
|
|
|
2018-04-16 19:04:33 -03:00
|
|
|
// check if IO is healthy
|
|
|
|
bool healthy(void);
|
|
|
|
|
2018-10-29 18:50:59 -03:00
|
|
|
// shutdown IO protocol (for reboot)
|
|
|
|
void shutdown();
|
|
|
|
|
2023-05-24 17:43:17 -03:00
|
|
|
void soft_reboot();
|
|
|
|
|
2018-10-30 21:07:47 -03:00
|
|
|
// setup for FMU failsafe mixing
|
2018-10-31 01:09:49 -03:00
|
|
|
bool setup_mixing(RCMapper *rcmap, int8_t override_chan,
|
|
|
|
float mixing_gain, uint16_t manual_rc_mask);
|
2019-10-20 10:47:14 -03:00
|
|
|
|
2022-04-20 22:14:10 -03:00
|
|
|
// Check if pin number is valid and configured for GPIO
|
2021-09-20 10:44:12 -03:00
|
|
|
bool valid_GPIO_pin(uint8_t pin) const;
|
|
|
|
|
|
|
|
// convert external pin numbers 101 to 108 to internal 0 to 7
|
|
|
|
bool convert_pin_number(uint8_t& pin) const;
|
|
|
|
|
|
|
|
// set GPIO mask of channels setup for output
|
|
|
|
void set_GPIO_mask(uint8_t mask);
|
|
|
|
|
2024-07-28 13:32:50 -03:00
|
|
|
// Get GPIO mask of channels setup for output
|
|
|
|
uint8_t get_GPIO_mask() const;
|
|
|
|
|
2021-09-20 10:44:12 -03:00
|
|
|
// write to a output pin
|
|
|
|
void write_GPIO(uint8_t pin, bool value);
|
|
|
|
|
2024-07-28 13:32:50 -03:00
|
|
|
// Read the last output value send to the GPIO pin
|
|
|
|
// This is not a real read of the actual pin
|
|
|
|
// This allows callers to check for state change
|
|
|
|
uint8_t read_virtual_GPIO(uint8_t pin) const;
|
|
|
|
|
2021-09-20 10:44:12 -03:00
|
|
|
// toggle a output pin
|
|
|
|
void toggle_GPIO(uint8_t pin);
|
|
|
|
|
2019-09-22 19:52:31 -03:00
|
|
|
// channel group masks
|
|
|
|
const uint8_t ch_masks[3] = { 0x03,0x0C,0xF0 };
|
|
|
|
|
2019-11-01 23:32:48 -03:00
|
|
|
static AP_IOMCU *get_singleton(void) {
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
2017-12-26 19:03:21 -04:00
|
|
|
private:
|
|
|
|
AP_HAL::UARTDriver &uart;
|
2017-12-27 03:24:15 -04:00
|
|
|
|
|
|
|
void thread_main(void);
|
|
|
|
|
|
|
|
// read count 16 bit registers
|
|
|
|
bool read_registers(uint8_t page, uint8_t offset, uint8_t count, uint16_t *regs);
|
|
|
|
|
|
|
|
// write count 16 bit registers
|
|
|
|
bool write_registers(uint8_t page, uint8_t offset, uint8_t count, const uint16_t *regs);
|
|
|
|
|
|
|
|
// write a single register
|
|
|
|
bool write_register(uint8_t page, uint8_t offset, uint16_t v) {
|
|
|
|
return write_registers(page, offset, 1, &v);
|
|
|
|
}
|
2019-10-20 10:47:14 -03:00
|
|
|
|
2017-12-27 03:24:15 -04:00
|
|
|
// modify a single register
|
|
|
|
bool modify_register(uint8_t page, uint8_t offset, uint16_t clearbits, uint16_t setbits);
|
|
|
|
|
|
|
|
// trigger an ioevent
|
|
|
|
void trigger_event(uint8_t event);
|
|
|
|
|
|
|
|
// IOMCU thread
|
|
|
|
thread_t *thread_ctx;
|
|
|
|
|
2018-11-03 03:27:10 -03:00
|
|
|
eventmask_t initial_event_mask;
|
|
|
|
|
2017-12-27 03:24:15 -04:00
|
|
|
// time when we last read various pages
|
|
|
|
uint32_t last_status_read_ms;
|
|
|
|
uint32_t last_rc_read_ms;
|
|
|
|
uint32_t last_servo_read_ms;
|
2018-04-13 03:17:08 -03:00
|
|
|
uint32_t last_safety_option_check_ms;
|
2023-11-22 05:23:21 -04:00
|
|
|
uint32_t last_reg_access_ms;
|
2023-06-23 17:15:14 -03:00
|
|
|
uint32_t last_erpm_read_ms;
|
|
|
|
uint32_t last_telem_read_ms;
|
2018-04-13 03:17:08 -03:00
|
|
|
|
|
|
|
// last value of safety options
|
|
|
|
uint16_t last_safety_options = 0xFFFF;
|
2017-12-27 03:24:15 -04:00
|
|
|
|
2018-09-03 23:16:19 -03:00
|
|
|
// have we forced the safety off?
|
|
|
|
bool safety_forced_off;
|
|
|
|
|
2021-05-21 18:04:43 -03:00
|
|
|
// was safety off on last status?
|
|
|
|
bool last_safety_off;
|
|
|
|
|
2017-12-27 03:24:15 -04:00
|
|
|
void send_servo_out(void);
|
|
|
|
void read_rc_input(void);
|
2023-06-23 17:15:14 -03:00
|
|
|
void read_erpm(void);
|
|
|
|
void read_telem(void);
|
2017-12-27 03:24:15 -04:00
|
|
|
void read_servo(void);
|
|
|
|
void read_status(void);
|
|
|
|
void discard_input(void);
|
2021-02-23 17:47:24 -04:00
|
|
|
void event_failed(uint32_t event_mask);
|
2018-04-13 03:17:08 -03:00
|
|
|
void update_safety_options(void);
|
2020-08-12 23:28:47 -03:00
|
|
|
void send_rc_protocols(void);
|
2018-10-30 21:07:47 -03:00
|
|
|
|
|
|
|
// CONFIG page
|
|
|
|
struct page_config config;
|
|
|
|
|
2017-12-27 03:24:15 -04:00
|
|
|
// PAGE_STATUS values
|
2018-05-10 21:37:01 -03:00
|
|
|
struct page_reg_status reg_status;
|
2019-08-13 21:07:48 -03:00
|
|
|
uint32_t last_log_ms;
|
2017-12-27 03:24:15 -04:00
|
|
|
|
2017-12-27 18:26:11 -04:00
|
|
|
// PAGE_RAW_RCIN values
|
2018-05-10 21:37:01 -03:00
|
|
|
struct page_rc_input rc_input;
|
2019-08-13 21:07:48 -03:00
|
|
|
uint32_t rc_last_input_ms;
|
2018-10-30 21:07:47 -03:00
|
|
|
|
|
|
|
// MIXER values
|
|
|
|
struct page_mixing mixing;
|
2019-10-20 10:47:14 -03:00
|
|
|
|
2017-12-27 03:24:15 -04:00
|
|
|
// output pwm values
|
|
|
|
struct {
|
|
|
|
uint8_t num_channels;
|
2024-04-27 04:44:31 -03:00
|
|
|
uint16_t pwm[IOMCU_MAX_RC_CHANNELS];
|
2018-04-14 00:54:04 -03:00
|
|
|
uint16_t safety_mask;
|
2024-04-27 04:44:31 -03:00
|
|
|
uint16_t failsafe_pwm[IOMCU_MAX_RC_CHANNELS];
|
2018-09-12 17:22:26 -03:00
|
|
|
uint8_t failsafe_pwm_set;
|
|
|
|
uint8_t failsafe_pwm_sent;
|
2023-05-24 17:43:17 -03:00
|
|
|
uint16_t channel_mask;
|
2017-12-27 03:24:15 -04:00
|
|
|
} pwm_out;
|
|
|
|
|
|
|
|
// read back pwm values
|
|
|
|
struct {
|
2024-04-27 04:44:31 -03:00
|
|
|
uint16_t pwm[IOMCU_MAX_RC_CHANNELS];
|
2017-12-27 03:24:15 -04:00
|
|
|
} pwm_in;
|
2017-12-27 05:15:55 -04:00
|
|
|
|
|
|
|
// output rates
|
|
|
|
struct {
|
|
|
|
uint16_t freq;
|
|
|
|
uint16_t chmask;
|
|
|
|
uint16_t default_freq = 50;
|
2017-12-31 22:28:59 -04:00
|
|
|
uint16_t sbus_rate_hz;
|
2021-02-23 17:47:24 -04:00
|
|
|
bool oneshot_enabled;
|
|
|
|
bool brushed_enabled;
|
2017-12-27 05:15:55 -04:00
|
|
|
} rate;
|
2018-01-03 02:25:30 -04:00
|
|
|
|
2023-05-24 17:43:17 -03:00
|
|
|
struct {
|
|
|
|
uint16_t period_us;
|
|
|
|
uint16_t rate;
|
|
|
|
} dshot_rate;
|
|
|
|
|
2023-11-16 13:44:55 -04:00
|
|
|
#if HAL_WITH_IO_MCU_BIDIR_DSHOT
|
2023-06-23 17:15:14 -03:00
|
|
|
// bi-directional dshot erpm values
|
|
|
|
struct page_dshot_erpm dshot_erpm;
|
2023-11-13 15:58:36 -04:00
|
|
|
struct page_dshot_telem dshot_telem[IOMCU_MAX_TELEM_CHANNELS/4];
|
2023-06-23 17:15:14 -03:00
|
|
|
uint8_t esc_group;
|
2023-11-16 13:44:55 -04:00
|
|
|
#endif
|
2023-07-10 06:32:29 -03:00
|
|
|
// queue of dshot commands that need sending
|
|
|
|
ObjectBuffer<page_dshot> dshot_command_queue{8};
|
2023-05-24 17:43:17 -03:00
|
|
|
|
2021-09-20 10:44:12 -03:00
|
|
|
struct page_GPIO GPIO;
|
2020-07-11 09:22:05 -03:00
|
|
|
// output mode values
|
2023-06-23 17:15:14 -03:00
|
|
|
struct page_mode_out mode_out;
|
2021-09-20 10:44:12 -03:00
|
|
|
|
2018-01-03 02:25:30 -04:00
|
|
|
// IMU heater duty cycle
|
|
|
|
uint8_t heater_duty_cycle;
|
2018-01-16 06:21:59 -04:00
|
|
|
|
|
|
|
uint32_t last_servo_out_us;
|
|
|
|
|
2017-12-27 03:24:15 -04:00
|
|
|
bool corked;
|
2018-10-29 18:50:59 -03:00
|
|
|
bool do_shutdown;
|
|
|
|
bool done_shutdown;
|
2018-04-14 08:09:11 -03:00
|
|
|
|
2018-04-16 19:04:33 -03:00
|
|
|
bool crc_is_ok;
|
2019-04-21 23:54:00 -03:00
|
|
|
bool detected_io_reset;
|
2018-10-30 21:07:47 -03:00
|
|
|
bool initialised;
|
2018-11-01 03:39:24 -03:00
|
|
|
bool is_chibios_backend;
|
2018-10-30 21:07:47 -03:00
|
|
|
|
|
|
|
uint32_t protocol_fail_count;
|
2019-04-23 20:16:09 -03:00
|
|
|
uint32_t protocol_count;
|
2019-08-13 21:07:48 -03:00
|
|
|
uint32_t total_errors;
|
2019-08-13 21:56:51 -03:00
|
|
|
uint32_t num_delayed;
|
2019-04-23 22:34:20 -03:00
|
|
|
uint32_t last_iocmu_timestamp_ms;
|
2020-01-17 02:36:01 -04:00
|
|
|
uint32_t read_status_errors;
|
|
|
|
uint32_t read_status_ok;
|
2020-08-12 23:28:47 -03:00
|
|
|
uint32_t last_rc_protocols;
|
2018-04-16 19:04:33 -03:00
|
|
|
|
2018-04-14 08:09:11 -03:00
|
|
|
// firmware upload
|
|
|
|
const char *fw_name = "io_firmware.bin";
|
2020-07-11 09:22:05 -03:00
|
|
|
const char *dshot_fw_name = "io_firmware_dshot.bin";
|
2019-10-23 06:58:01 -03:00
|
|
|
const uint8_t *fw;
|
2018-07-09 03:47:35 -03:00
|
|
|
uint32_t fw_size;
|
2018-04-14 08:09:11 -03:00
|
|
|
|
2019-08-13 21:07:48 -03:00
|
|
|
size_t write_wait(const uint8_t *pkt, uint8_t len);
|
2018-07-09 03:47:35 -03:00
|
|
|
bool upload_fw(void);
|
2018-04-14 08:09:11 -03:00
|
|
|
bool recv_byte_with_timeout(uint8_t *c, uint32_t timeout_ms);
|
|
|
|
bool recv_bytes(uint8_t *p, uint32_t count);
|
|
|
|
void drain(void);
|
|
|
|
bool send(uint8_t c);
|
|
|
|
bool send(const uint8_t *p, uint32_t count);
|
|
|
|
bool get_sync(uint32_t timeout = 40);
|
|
|
|
bool sync();
|
|
|
|
bool get_info(uint8_t param, uint32_t &val);
|
|
|
|
bool erase();
|
|
|
|
bool program(uint32_t fw_size);
|
|
|
|
bool verify_rev2(uint32_t fw_size);
|
|
|
|
bool verify_rev3(uint32_t fw_size_local);
|
|
|
|
bool reboot();
|
|
|
|
|
|
|
|
bool check_crc(void);
|
2019-04-21 23:54:00 -03:00
|
|
|
void handle_repeated_failures();
|
2019-04-23 22:34:20 -03:00
|
|
|
void check_iomcu_reset();
|
|
|
|
|
2022-08-07 03:36:42 -03:00
|
|
|
void write_log(); // handle onboard logging
|
|
|
|
|
2019-11-01 23:32:48 -03:00
|
|
|
static AP_IOMCU *singleton;
|
|
|
|
|
2018-04-14 08:09:11 -03:00
|
|
|
enum {
|
|
|
|
PROTO_NOP = 0x00,
|
|
|
|
PROTO_OK = 0x10,
|
|
|
|
PROTO_FAILED = 0x11,
|
|
|
|
PROTO_INSYNC = 0x12,
|
|
|
|
PROTO_INVALID = 0x13,
|
|
|
|
PROTO_BAD_SILICON_REV = 0x14,
|
|
|
|
PROTO_EOC = 0x20,
|
|
|
|
PROTO_GET_SYNC = 0x21,
|
|
|
|
PROTO_GET_DEVICE = 0x22,
|
|
|
|
PROTO_CHIP_ERASE = 0x23,
|
|
|
|
PROTO_CHIP_VERIFY = 0x24,
|
|
|
|
PROTO_PROG_MULTI = 0x27,
|
|
|
|
PROTO_READ_MULTI = 0x28,
|
|
|
|
PROTO_GET_CRC = 0x29,
|
|
|
|
PROTO_GET_OTP = 0x2a,
|
|
|
|
PROTO_GET_SN = 0x2b,
|
|
|
|
PROTO_GET_CHIP = 0x2c,
|
|
|
|
PROTO_SET_DELAY = 0x2d,
|
|
|
|
PROTO_GET_CHIP_DES = 0x2e,
|
|
|
|
PROTO_REBOOT = 0x30,
|
|
|
|
|
|
|
|
INFO_BL_REV = 1, /**< bootloader protocol revision */
|
|
|
|
BL_REV = 5, /**< supported bootloader protocol */
|
|
|
|
INFO_BOARD_ID = 2, /**< board type */
|
|
|
|
INFO_BOARD_REV = 3, /**< board revision */
|
|
|
|
INFO_FLASH_SIZE = 4, /**< max firmware size in bytes */
|
|
|
|
|
|
|
|
PROG_MULTI_MAX = 248, /**< protocol max is 255, must be multiple of 4 */
|
|
|
|
};
|
2017-12-26 19:03:21 -04:00
|
|
|
};
|
2018-01-02 01:47:42 -04:00
|
|
|
|
2019-11-01 23:32:48 -03:00
|
|
|
namespace AP {
|
|
|
|
AP_IOMCU *iomcu(void);
|
|
|
|
};
|
|
|
|
|
2018-01-02 01:47:42 -04:00
|
|
|
#endif // HAL_WITH_IO_MCU
|