freeRTOS操作系统机器人实现
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
5.8 KiB

3 days ago
/******************************************************************************
(C), 2018-2099, Radkil
******************************************************************************
: ATParser.h
: 稿
: Radkil
: 2026314
:
: ATParser.c
:
1. : 2026314
: Radkil
:
******************************************************************************/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
#ifndef __ATPARSER_H__
#define __ATPARSER_H__
#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* __cplusplus */
/*==============================================*
* include header files *
*----------------------------------------------*/
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
/*==============================================*
* constants or macros define *
*----------------------------------------------*/
#define AT_RX_BUFFER_SIZE 256 // 接收缓冲区大小
#define AT_CMD_MAX_LEN 64 // 单条命令最大长度
#define AT_MAX_RESPONSES 8 // 支持的最大响应回调数量
/**
* @brief AT
*/
typedef enum {
AT_OK, // 成功
AT_ERROR, // 失败 (ERROR)
AT_TIMEOUT, // 超时
AT_BUSY, // 模块忙
AT_UNKNOWN // 未知响应
} at_status_t;
/**
* @brief
*/
typedef struct {
const char *prefix; // 响应前缀,例如 "+CSQ:" 或 "OK"
void (*callback)(const char *data, int len); // 匹配到该前缀时的回调
} at_response_item_t;
/**
* @brief AT
*/
typedef struct {
uint8_t rx_buffer[AT_RX_BUFFER_SIZE]; // 环形缓冲区
uint16_t head; // 写指针
uint16_t tail; // 读指针
char cmd_buffer[AT_CMD_MAX_LEN]; // 当前正在解析的命令缓冲
uint16_t cmd_len; // 当前命令长度
bool is_waiting_response; // 是否正在等待响应
uint32_t wait_start_time; // 等待开始的时间戳 (ms)
uint32_t timeout_ms; // 超时时间设置
at_status_t last_status; // 最后一次执行状态
// 注册的响应列表
at_response_item_t responses[AT_MAX_RESPONSES];
uint8_t response_count;
// 硬件抽象层回调 (用户需实现)
void (*hw_send)(const uint8_t *data, uint16_t len);
uint32_t (*hw_get_tick)(void); // 获取系统毫秒时间
} at_parser_t;
/*==============================================*
* project-wide global variables *
*----------------------------------------------*/
/*==============================================*
* routines' or functions' implementations *
*----------------------------------------------*/
/**
* @brief
* @param parser
* @param send_cb ()
* @param tick_cb ()
*/
void at_parser_init(at_parser_t *parser,
void (*send_cb)(const uint8_t*, uint16_t),
uint32_t (*tick_cb)(void));
/**
* @brief
* @param parser
* @param prefix ( "+CSQ:")
* @param cb
* @return true false
*/
bool at_parser_register_response(at_parser_t *parser, const char *prefix, void (*cb)(const char*, int));
/**
* @brief ()
* @param parser
* @param data
*/
void at_parser_feed(at_parser_t *parser, uint8_t data);
/**
* @brief AT ()
* @param parser
* @param cmd ( \r\n)
* @param timeout_ms
* @return AT_OK, AT_ERROR, AT_TIMEOUT
*/
at_status_t at_parser_send_cmd(at_parser_t *parser, const char *cmd, uint32_t timeout_ms);
/**
* @brief ()
* @param parser
* @param data
* @param len
*/
void at_parser_send_raw(at_parser_t *parser, const uint8_t *data, uint16_t len);
/**
* @brief ()
* @param parser
*/
void at_parser_process(at_parser_t *parser);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
#endif /* __ATPARSER_H__ */