Feetech STS Driver
Loading...
Searching...
No Matches
sts_protocol.h
Go to the documentation of this file.
1
29#pragma once
30
31#include <stdint.h>
32
33/* --- Protocol Structural Constants --- */
34#define STS_HEADER 0xFFU
35#define STS_HEADER_SIZE 2U
36#define STS_CHECKSUM_SIZE 1U
37#define STS_LENGTH_FIXED_OVERHEAD 2U /* ID + Checksum */
38#define STS_PKT_FIXED_TOTAL 4U /* Header(2) + ID(1) + Length(1) */
39#define STS_MIN_PACKET_SIZE 6U /* Header(2)+ID(1)+Len(1)+Inst(1)+CS(1) */
40#define STS_MAX_PACKET_SIZE 255U /* Max total byte limit */
41
42/* --- Packet Layout Indices --- */
43#define STS_IDX_HEADER_1 0U
44#define STS_IDX_HEADER_2 1U
45#define STS_IDX_ID 2U
46#define STS_IDX_LENGTH 3U
47#define STS_IDX_INSTRUCTION 4U
48#define STS_IDX_STATUS 4U
49#define STS_IDX_PARAM_START 5U
50
51/* --- Protocol Limits --- */
52#define STS_MAX_ID 254U
53#define STS_MAX_PARAM_LEN 253U
54#define STS_HARDWARE_OK 0x00U
55#define STS_MAX_INSTRUCTION 0x05U /* Range: 0x01 (Ping) to 0x05 (Reset) */
56#define STS_MIN_PKT_LEN_VAL 2U /* Min 'Length' byte: Status + Checksum */
57
58/* --- Protocol Element Sizes --- */
59#define STS_ACK_BASE_LEN 6U
60#define STS_REG_ADDR_LEN 1U
61#define STS_DATA_LEN_8BIT 1U
62#define STS_DATA_LEN_16BIT 2U
63
64/* --- Derived Parameter Lengths --- */
65#define STS_WRITE8_PARAM_LEN (STS_REG_ADDR_LEN + STS_DATA_LEN_8BIT) /* 2U */
66#define STS_WRITE16_PARAM_LEN (STS_REG_ADDR_LEN + STS_DATA_LEN_16BIT) /* 3U */
67#define STS_READ_CMD_PARAM_LEN (STS_REG_ADDR_LEN + STS_DATA_LEN_8BIT) /* 2U */
88
98sts_result_t sts_calculate_checksum(const uint8_t* pkt_buf, uint16_t pkt_len, uint8_t* checksum_out);
99
111sts_result_t sts_create_packet(uint8_t id, uint8_t instruction, const uint8_t* param_buf, uint16_t param_len, uint8_t* pkt_buf, uint16_t pkt_buf_size);
112
126sts_result_t sts_parse_response(uint8_t expected_id, const uint8_t* rx_buf, uint16_t rx_len, uint8_t* param_buf, uint16_t param_buf_size,uint16_t* param_len);
sts_result_t sts_calculate_checksum(const uint8_t *pkt_buf, uint16_t pkt_len, uint8_t *checksum_out)
Calculates the Feetech STS Checksum. Sums ID, Length, Instruction, and Parameters (skipping the initi...
Definition sts_protocol.c:31
sts_result_t sts_parse_response(uint8_t expected_id, const uint8_t *rx_buf, uint16_t rx_len, uint8_t *param_buf, uint16_t param_buf_size, uint16_t *param_len)
Parses a response packet from a servo, searching for valid headers in the buffer.
Definition sts_protocol.c:165
sts_result_t
Return codes for STS protocol operations.
Definition sts_protocol.h:71
@ STS_ERR_BUSY
Definition sts_protocol.h:85
@ STS_ERR_INVALID_LEN
Definition sts_protocol.h:74
@ STS_ERR_HARDWARE
Definition sts_protocol.h:82
@ STS_ERR_ID_MISMATCH
Definition sts_protocol.h:79
@ STS_ERR_CHECKSUM
Definition sts_protocol.h:80
@ STS_ERR_MALFORMED
Definition sts_protocol.h:81
@ STS_ERR_TIMEOUT
Definition sts_protocol.h:77
@ STS_ERR_TX_FAIL
Definition sts_protocol.h:83
@ STS_ERR_HEADER
Definition sts_protocol.h:78
@ STS_ERR_BUF_TOO_SMALL
Definition sts_protocol.h:76
@ STS_ERR_NULL_PTR
Definition sts_protocol.h:73
@ STS_OK
Definition sts_protocol.h:72
@ STS_ERR_INVALID_PARAM
Definition sts_protocol.h:75
sts_result_t sts_create_packet(uint8_t id, uint8_t instruction, const uint8_t *param_buf, uint16_t param_len, uint8_t *pkt_buf, uint16_t pkt_buf_size)
Constructs an STS packet in the provided buffer.
Definition sts_protocol.c:50