From 5da7beb8f53c42945b166ec504a6c29592153788 Mon Sep 17 00:00:00 2001 From: Alessandro Simovic Date: Wed, 11 Apr 2018 15:08:47 +0200 Subject: [PATCH] tap_esc_common: docs & function return types etc. --- src/drivers/tap_esc/drv_tap_esc.h | 2 + src/drivers/tap_esc/tap_esc.cpp | 4 +- src/drivers/tap_esc/tap_esc_common.cpp | 38 ++++++++--------- src/drivers/tap_esc/tap_esc_common.h | 56 +++++++++++++++++++++----- 4 files changed, 69 insertions(+), 31 deletions(-) diff --git a/src/drivers/tap_esc/drv_tap_esc.h b/src/drivers/tap_esc/drv_tap_esc.h index c7dadc5212..e2e99bbc37 100644 --- a/src/drivers/tap_esc/drv_tap_esc.h +++ b/src/drivers/tap_esc/drv_tap_esc.h @@ -41,6 +41,8 @@ #define TAP_ESC_MAX_PACKET_LEN 20 #define TAP_ESC_MAX_MOTOR_NUM 8 +#define PACKET_HEAD 0xfe + /* ESC_POS maps the values stored in the channelMapTable to reorder the ESC's * id so that that match the mux setting, so that the ressonder's data * will be read. diff --git a/src/drivers/tap_esc/tap_esc.cpp b/src/drivers/tap_esc/tap_esc.cpp index 5b669f2a78..f3bbee17bd 100644 --- a/src/drivers/tap_esc/tap_esc.cpp +++ b/src/drivers/tap_esc/tap_esc.cpp @@ -302,7 +302,7 @@ TAP_ESC::init() tap_esc_common::read_data_from_uart(_uart_fd, &uartbuf); - if (tap_esc_common::parse_tap_esc_feedback(&uartbuf, &_packet)) { + if (!tap_esc_common::parse_tap_esc_feedback(&uartbuf, &_packet)) { valid = (_packet.msg_id == ESCBUS_MSG_ID_CONFIG_INFO_BASIC && _packet.d.rspConfigInfoBasic.channelID == cid && 0 == memcmp(&_packet.d.rspConfigInfoBasic.resp, &config, sizeof(ConfigInfoBasicRequest))); @@ -600,7 +600,7 @@ TAP_ESC::cycle() send_esc_outputs(motor_out, esc_count); tap_esc_common::read_data_from_uart(_uart_fd, &uartbuf); - if (tap_esc_common::parse_tap_esc_feedback(&uartbuf, &_packet) == true) { + if (!tap_esc_common::parse_tap_esc_feedback(&uartbuf, &_packet)) { if (_packet.msg_id == ESCBUS_MSG_ID_RUN_INFO) { RunInfoRepsonse &feed_back_data = _packet.d.rspRunInfo; diff --git a/src/drivers/tap_esc/tap_esc_common.cpp b/src/drivers/tap_esc/tap_esc_common.cpp index a5837beca7..6961f22686 100644 --- a/src/drivers/tap_esc/tap_esc_common.cpp +++ b/src/drivers/tap_esc/tap_esc_common.cpp @@ -54,14 +54,6 @@ namespace tap_esc_common static uint8_t crc8_esc(uint8_t *p, uint8_t len); static uint8_t crc_packet(EscPacket &p); -/**************************************************************************** - * Name: select_responder - * - * Description: - * Select tap esc responder for serial interface (device 74hct151). - * GPIOs to be defined in board_config.h - * - ****************************************************************************/ void select_responder(uint8_t sel) { #if defined(GPIO_S0) @@ -108,12 +100,18 @@ int initialise_uart(const char *const device, int &uart_fd) PX4_WARN("hardware flow disable failed"); } - return uart_fd; + return 0; } int deinitialise_uart(int &uart_fd) { - return close(uart_fd); + int ret = close(uart_fd); + + if (ret == 0) { + uart_fd = -1; + } + + return ret; } int enable_flow_control(int uart_fd, bool enabled) @@ -153,7 +151,7 @@ int send_packet(int uart_fd, EscPacket &packet, int responder) return ret; } -void read_data_from_uart(int uart_fd, ESC_UART_BUF *const uart_buf) +int read_data_from_uart(int uart_fd, ESC_UART_BUF *const uart_buf) { uint8_t tmp_serial_buf[UART_BUFFER_SIZE]; @@ -168,10 +166,15 @@ void read_data_from_uart(int uart_fd, ESC_UART_BUF *const uart_buf) uart_buf->tail = 0; } } + + } else if (len < 0) { + return len; } + + return 0; } -bool parse_tap_esc_feedback(ESC_UART_BUF *const serial_buf, EscPacket *const packetdata) +int parse_tap_esc_feedback(ESC_UART_BUF *const serial_buf, EscPacket *const packetdata) { static PARSR_ESC_STATE state = HEAD; static uint8_t data_index = 0; @@ -183,8 +186,8 @@ bool parse_tap_esc_feedback(ESC_UART_BUF *const serial_buf, EscPacket *const pac for (int i = 0; i < count; i++) { switch (state) { case HEAD: - if (serial_buf->esc_feedback_buf[serial_buf->head] == 0xFE) { - packetdata->head = 0xFE; //just_keep the format + if (serial_buf->esc_feedback_buf[serial_buf->head] == PACKET_HEAD) { + packetdata->head = PACKET_HEAD; //just_keep the format state = LEN; } @@ -234,7 +237,7 @@ bool parse_tap_esc_feedback(ESC_UART_BUF *const serial_buf, EscPacket *const pac serial_buf->dat_cnt--; state = HEAD; - return true; + return 0; } state = HEAD; @@ -251,13 +254,10 @@ bool parse_tap_esc_feedback(ESC_UART_BUF *const serial_buf, EscPacket *const pac } serial_buf->dat_cnt--; - } - - } - return false; + return -1; } static uint8_t crc8_esc(uint8_t *p, uint8_t len) diff --git a/src/drivers/tap_esc/tap_esc_common.h b/src/drivers/tap_esc/tap_esc_common.h index 195dcc6959..59ee6111ba 100644 --- a/src/drivers/tap_esc/tap_esc_common.h +++ b/src/drivers/tap_esc/tap_esc_common.h @@ -42,29 +42,65 @@ #include "drv_tap_esc.h" - namespace tap_esc_common { -/**************************************************************************** - * Name: select_responder - * - * Description: - * Select tap esc responder data for serial interface by 74hct151 - * - ****************************************************************************/ +/** + * Select tap esc responder data for serial interface by 74hct151. GPIOs to be + * defined in board_config.h + * @param sel ID of the ESC (responder) of which feedback is requested + */ void select_responder(uint8_t sel); +/** + * Opens a device for use as UART. + * @param device UNIX path of UART device + * @param uart_fd file-descriptor of UART device + * @return 0 on success, -1 on error + */ int initialise_uart(const char *const device, int &uart_fd); +/** + * Closes a device previously opened with initialise_uart(). + * @param uart_fd file-descriptor of UART device as provided by initialise_uart() + * @return 0 on success, -1 on error + */ int deinitialise_uart(int &uart_fd); +/** + * Enables/disables flow control for open UART device. + * @param uart_fd file-descriptor of UART device + * @param enabled Set true for enabling and false for disabling flow control + * @return 0 on success, -1 on error + */ int enable_flow_control(int uart_fd, bool enabled); +/** + * Sends a packet to all ESCs and requests a specific ESC to respond + * @param uart_fd file-descriptor of UART device + * @param packet Packet to be sent to ESCs. CRC information will be added. + * @param responder ID of the ESC (responder) that should return feedback + * @return On success number of bytes written, on error -1 + */ int send_packet(int uart_fd, EscPacket &packet, int responder); -void read_data_from_uart(int uart_fd, ESC_UART_BUF *const uart_buf); +/** + * Read data from the UART into a buffer + * @param uart_fd file-descriptor of UART device + * @param uart_buf Buffer where incoming data will be stored + * @return 0 on success, -1 on error + */ +int read_data_from_uart(int uart_fd, ESC_UART_BUF *const uart_buf); -bool parse_tap_esc_feedback(ESC_UART_BUF *const serial_buf, EscPacket *const packetdata); +/** + * Parse feedback from an ESC + * @param serial_buf Buffer where incoming data will be stored + * @param packetdata Packet that will be populated with information from buffer + * @return 0 on success, -1 on error + */ +int parse_tap_esc_feedback(ESC_UART_BUF *const serial_buf, EscPacket *const packetdata); +/** + * Lookup-table for faster CRC computation when sending ESC packets. + */ extern const uint8_t crc_table[]; } /* tap_esc_common */