SDK สำหรับ TESAIoT Dev Kit
คู่มืออ้างอิง API และ Tutorial (MTB & µPython)
Loading...
Searching...
No Matches
tacp.h
Go to the documentation of this file.
1/*******************************************************************************
2 * File Name: tacp.h
3 *
4 * Description: TESAIoT Control Protocol (TACP) — header file.
5 * Defines magic bytes, command IDs, ring buffer, and the new
6 * binary file transfer sub-protocol for fast IDE uploads.
7 *
8 * Protocol Summary:
9 * Control commands: 0xAA 0x55 <CMD> (3 bytes, instant)
10 * File transfer: 0xAA 0x55 0x20 <frame> (binary bulk, see below)
11 *
12 * Binary File Transfer Sub-Protocol (TACP_CMD_FILE_XFER = 0x20):
13 * ───────────────────────────────────────────────────────────────
14 * After the 3-byte magic, the host sends a binary frame:
15 *
16 * Byte 0 : path_len (1 byte, max 127)
17 * Byte 1..N : path (path_len bytes, UTF-8, e.g. "/main.py")
18 * Byte N+1..4 : file_len (4 bytes, little-endian uint32)
19 * Byte N+5..M : file_data (file_len bytes, raw content)
20 * Byte M+1..2 : crc16 (2 bytes, little-endian, CRC-CCITT)
21 *
22 * Device responds over UART with a single-line ASCII response:
23 * "TACP:FILE_OK <path> <size>\r\n" on success
24 * "TACP:FILE_ERR <code> <msg>\r\n" on failure
25 *
26 * Error codes:
27 * 1 = path too long 4 = CRC mismatch
28 * 2 = file too large 5 = filesystem write error
29 * 3 = timeout 6 = out of memory
30 *
31 * Flow control: after receiving the header (path_len + path + file_len),
32 * the device sends "TACP:FILE_RDY\r\n" to signal it is ready for data.
33 * The host should wait for this before streaming file_data bytes.
34 *
35 * Timeout: 5 seconds between any two bytes during transfer.
36 *
37 * Copyright (c) 2026 TESAIoT
38 ******************************************************************************/
39
40#ifndef TACP_H
41#define TACP_H
42
43#include <stdint.h>
44#include <stdbool.h>
45
46#ifdef __cplusplus
47extern "C" {
48#endif
49
50/*******************************************************************************
51 * Magic Bytes & Command IDs
52 ******************************************************************************/
53#define TACP_MAGIC_0 0xAA
54#define TACP_MAGIC_1 0x55
55
56/* Existing control commands (3-byte: AA 55 CMD) */
57#define TACP_CMD_TERMINATE 0x01 /* Ctrl-C: KeyboardInterrupt */
58#define TACP_CMD_PROGRAM_MODE 0x02 /* Safe boot + soft reset for upload */
59#define TACP_CMD_CONNECT 0x03 /* IDE ping (no side effects) */
60#define TACP_CMD_STATUS 0x04 /* Show USB icon on LCD */
61
62/* Firmware version query (AA 55 05) — responds with build info */
63#define TACP_CMD_VERSION_QUERY 0x05 /* Query firmware build info */
64
65/* Hard reset (AA 55 06) — NVIC_SystemReset(), all cores restart */
66#define TACP_CMD_HARD_RESET 0x06 /* Force hardware reset immediately */
67
68/* Binary file transfer (AA 55 20 <frame>) */
69#define TACP_CMD_FILE_XFER 0x20 /* Start binary file upload */
70
71/* BentoClaw agent commands (AA 55 30 <sub-protocol>) */
72#define TACP_CMD_BENTOCLAW 0x30 /* BentoClaw binary sub-protocol */
73
74/* BentoClaw sub-commands (byte after 0x30) — v2 protocol:
75 * Request frame (IDE → Device):
76 * [0xAA][0x55][0x30][sub_cmd][seq][len_lo][len_hi][payload][crc_lo][crc_hi]
77 *
78 * Response frame (Device → IDE):
79 * [0xAA][0x55][0x30][sub_cmd][seq][len_lo][len_hi][payload][crc_lo][crc_hi]
80 * sub_cmd matches request for success; 0xFF for errors.
81 * seq echoes the request's sequence number (0 for unsolicited like READY).
82 *
83 * CRC v2: CRC-16/CCITT (init 0xFFFF, poly 0x1021) over:
84 * sub_cmd + seq + len_lo + len_hi + payload
85 *
86 * Version negotiation: READY frame includes {"proto":2} — IDE auto-selects.
87 */
88#define TACP_CLAW_READY 0x00 /* FW→IDE: boot complete, ready */
89#define TACP_CLAW_EXEC 0x01 /* Execute tool */
90#define TACP_CLAW_ASK 0x02 /* Send prompt to AI backend */
91#define TACP_CLAW_STATUS 0x03 /* Get agent status */
92#define TACP_CLAW_TOOLS 0x04 /* List available tools */
93#define TACP_CLAW_HISTORY 0x05 /* Get session history */
94#define TACP_CLAW_REMEMBER 0x06 /* Store memory key=value */
95#define TACP_CLAW_CLEAR 0x07 /* Clear session */
96#define TACP_CLAW_CONNECT 0x08 /* Connect to HTTPS backend */
97#define TACP_CLAW_DISCONNECT 0x09 /* Disconnect from backend */
98#define TACP_CLAW_ERROR 0xFF /* Error response (payload=message) */
99
100#define TACP_CLAW_PAYLOAD_MAX (3072) /* Max payload size for sub-protocol */
101
102/*******************************************************************************
103 * Board SKU Registry
104 * Format: KIT_<chip>_<variant>_<revision>
105 * ─────────────────────────────────────────────
106 * KIT_PSE84_AI_001 AI Kit Sensor Hub
107 * KIT_PSE84_AI_002 AI Kit Game Console
108 * KIT_PSE84_EVAL_EPC2_001 Eva Kit Sensor Hub
109 * ─────────────────────────────────────────────
110 * Add new boards by incrementing revision number.
111 * SKU is defined in mpconfigboard.h (default) or overridden
112 * via -DMICROPY_HW_BOARD_SKU in per-project Makefile.micropython.
113 ******************************************************************************/
114
115/*******************************************************************************
116 * Binary File Transfer Limits
117 ******************************************************************************/
118#define TACP_FILE_PATH_MAX 127 /* Max path length in bytes */
119#define TACP_FILE_SIZE_MAX (512 * 1024) /* 512 KB max file size */
120#define TACP_FILE_CHUNK_SIZE 512 /* Internal write chunk size */
121#define TACP_FILE_TIMEOUT_MS 5000 /* Byte-level timeout in ms */
122
123/*******************************************************************************
124 * Ring Buffer
125 ******************************************************************************/
126#define TACP_RING_BUF_SIZE 256 /* Must be power of 2 */
127
128typedef struct {
130 volatile uint16_t head;
131 volatile uint16_t tail;
133
134/*******************************************************************************
135 * Public API
136 ******************************************************************************/
137
139void tacp_init(void);
140
145
148bool tacp_poll_uart(void);
149
153
156
160void tacp_claw_respond(uint8_t sub_cmd, const char *payload, uint16_t payload_len);
161
162#ifdef __cplusplus
163}
164#endif
165
166#endif /* TACP_H */
bool tacp_poll_uart(void)
pump UART; ต้องเรียกจากลูปรอแบบบล็อกทุกลูป
void tacp_init(void)
รีเซ็ต state machine, ล้าง ring, ระบาย FIFO; ครั้งเดียวต่อหนึ่ง soft reset
int tacp_ring_buf_read(void)
ดึงไบต์ของ REPL ออกมาหนึ่งไบต์ หรือคืน -1 เมื่อว่าง ไม่มีผู้เรียกที่มีอยู่จริงในของที่ส่งมอบ
bool tacp_ring_buf_readable(void)
การทดสอบแบบไม่บล็อกว่ามีข้อมูลพร้อมหรือไม่ ไม่มีผู้เรียกที่มีอยู่จริงในของที่ส่งมอบ
void tacp_request_delete_main_from_isr(void)
ทางออกฉุกเฉินที่เรียกจาก ISR ได้: หยุดสคริปต์, สั่ง soft reset, ลบ /main.py
void tacp_claw_respond(uint8_t sub_cmd, const char *payload, uint16_t payload_len)
ส่งเฟรมคำตอบไบนารีของ BentoClaw หนึ่งเฟรมออกทาง UART
Definition tacp.h:128
uint8_t buf[TACP_RING_BUF_SIZE]
Definition tacp.h:129
volatile uint16_t tail
Definition tacp.h:131
volatile uint16_t head
Definition tacp.h:130
#define TACP_RING_BUF_SIZE
Definition tacp.h:126