Article count:1382 Read by:1966155

Account Entry

An AT command communication parsing module

Latest update time:2024-10-21
    Reads:

Click on the blue " Linux " in the upper left corner and select " Set as Star "


introduce

An AT command communication parsing module that supports bare metal (at_chat) and OS version (at). Suitable for modem, WIFI module , and Bluetooth communication.

Software Architecture

  • • at_chat.c at_chat.h list.h

For OS-free versions, it uses chain queues and asynchronous callback methods to process AT command sending and receiving, and supports URC processing, custom command sending and parsing operations.

  • • at.c at.h at_util.h comdef.h

For OS version, it needs to be ported according to the operating system related interfaces specified in at_util.h before use, such as providing semaphore operations, task delay and other operations.

Instructions

at_chat module (no OS)

Basic Concepts

The at_chat module uses a chain queue for management, which includes two lists, the free list and the ready list. Each of their basic working units is called a job item. The commands to be executed will be placed in the ready list. After the command is executed, it will be recycled by the free list. The definition of a job item is as follows:

/*AT作业项*/
typedefstruct {
unsignedint  state :3;
unsignedint  type  :3;/* 作业类型*/
unsignedintabort:1;
void*param;/* 通用参数*/
void*info;/* 通用信息指针*/
struct list_head node;/* 链表结点*/
}at_item_t;

The job is fixedly allocated when the AT controller is defined. No dynamic memory is used. By default, 10 job items are supported, which means that 10 AT commands can be queued for processing at the same time.

Basic interface and description

  • • at_send_singlline, send a single line command, wait for OK response by default, timeout 3S

  • • at_send_multiline, multi-line command, default wait for OK response, timeout 3S

  • • at_do_cmd, supports custom sending format and receiving matching string

  • • at_do_work, supports custom send and receive parsing

Effect Demonstration

For detailed usage, please refer to the Demo program wifi_task.c module

m169 wifi module communication effect diagram

How to use

1. Define AT controller and communication adapter interface

/* 
 * @brief   定义AT控制器
 */

staticat_obj_t at;

constat_adapter_t adap ={//AT适配器接口
//适配GPRS模块的串口读写接口
.write       = uart_write,
.read        = uart_read
...
};
  1. 1. Initialize the AT controller and put it into the task for polling (considering the real-time processing, it is recommended to be less than 20ms)

/* 
 * @brief    wifi初始化
 */

voidwifi_init(void)
{
    at_obj_init(&at,&adap);
/*...*/
}driver_init("wifi", wifi_init);

/* 
 * @brief    wifi任务(10ms 轮询1次)
 */

voidwifi_task(void)
{
    at_poll_task(&at);
}task_register("wifi", wifi_task,10);

Example Demonstration

//WIFI IO配置命令
=> AT+GPIO_TEST_EN=1\r\n

<= OK\r\n
/**
 * @brief AT执行回调处理程序
 */

staticvoidtest_gpio_callback(at_response_t *r)
{
if(r->ret == AT_RET_OK ){
printf("Execute successfully\r\n");
}else{
printf("Execute failure\r\n");
}
}
at_send_singlline(&at, test_gpio_callback,"AT+GPIO_TEST_EN=1");

at module (OS version)

Since AT command communication is a relatively complicated process, it is difficult to handle in an environment without an OS. It is also very complicated. For programs that do not allow blocking, there is no better way except using status and callback, so it is recommended to use this module.

Basic interface and description

  • • at_do_cmd, executes AT commands, and this interface can be used to further encapsulate commonly used single-line commands and multi-line commands.

  • • at_split_respond_lines, command response splitter.

  • • at_do_work, suitable for sending combined commands, such as the GPRS module needs to wait for the "<" or "CONNECT" prompt when sending SMS or socket data. This interface can be used to customize sending and receiving.

How to use

1. Define AT controller and communication adapter interface (including URC callback function table and interface buffer URC)

static at_obj_t at;//定义AT控制器对象

staticchar urc_buf[128];//URC主动上报缓冲区

utc_item_t utc_tbl[]={//定义URC表
"+CSQ: ", csq_updated_handler
}

constat_adapter_t adap ={//AT适配器接口
.urc_buf     = urc_buf,
.urc_bufsize =sizeof(urc_buf),
.utc_tbl     = utc_tbl,
.urc_tbl_count =sizeof(utc_tbl)/sizeof(utc_item_t),
//debug调试接口
.debug       = at_debug,
//适配GPRS模块的串口读写接口
.write       = uart_write,
.read        = uart_read
};

2. Create an AT controller and create a polling processing thread

void at_thread(void)
{
    at_obj_create(&at, &adap);
    while (1) {        
        at_process(&at);
    }
}

Example Demonstration

Example 1 (Query the signal quality of the wireless module)

/** at_do_cmd 接口使用演示
    查询GPRS模组信号质量命令
    => AT+CSQ
    
    <= +CSQ: 24, 0
    <= OK
*/


/* 
 * @brief    获取csq值
 */

boolread_csq_value(at_obj_t *at, int *rssi, int *error_rate)
{
//接收缓冲区
unsignedchar recvbuf[32];
//AT应答
at_respond_t r ={"OK", recvbuf,sizeof(recvbuf),3000};
//
if(at_do_cmd(at,&r,"AT+CSQ")!= AT_RET_OK)
returnfalse;
//提取出响应数据
return(sscanf(recv,"%*[^+]+CSQ: %d,%d", rssi, error_rate)==2);

}

Example 2 (Sending TCP data)

/** at_do_work 接口使用演示
    参考自hl8518模组Socket 数据发送命令
    => AT+KTCPSND=<session_id>,<ndata>
    
    <= CONNECT
    
    => <data>
    
    <= OK
*/


/*
 * @brief       数据发送处理
 * @retval      none
 */

staticboolsocket_send_handler(at_work_ctx_t *e)
{
struct socket_info *i =(struct socket_info *)e->params;
struct ril_sock *s    = i->s;

if(s->type == SOCK_TYPE_TCP)
        e->printf(e,"AT+KTCPSND=%d,%d", s->session, i->bufsize);
else
        e->printf(e,"AT+KUDPSND=%d,%s,%d,%d",s->session, s->host,
        s->port, i->bufsize);

if(e->wait_resp(e,"CONNECT",5000)!= AT_RET_OK){//等待提示符
gotoError;
}
    e->write(i->buf, i->bufsize);//发送数据

    e->write("--EOF--Pattern--",strlen("--EOF--Pattern--"));//发送结束符

if(e->wait_resp(e,"OK",5000)== AT_RET_OK)
returntrue;
else{
Error:
        e->write("--EOF--Pattern--",strlen("--EOF--Pattern--"));
returnfalse;
}
}

/**
 * @brief       socket 数据发送
 * @param[in]   s   - socket
 * @param[in]   buf - 数据缓冲区
 * @param[in]   len - 缓冲区长度
 */

staticboolhl8518_sock_send(ril_obj_t *r, struct ril_sock *s, const void *buf, 
                       unsigned int len)
{
struct socket_info info ={s,(unsignedchar*)buf, len,0};
if(len ==0)
returnfalse;
return at_do_work(&r->at,(at_work)socket_send_handler,&info);
}

Source: https://toscode.gitee.com/smtian/AT-Command


end



A bite of Linux


Follow and reply【 1024 】 to get a large amount of Linux information


Collection of wonderful articles

Recommended articles

【Album】 ARM
【Album】 Fan Q&A
【Album】 All original
Album Getting started with Linux
Special Computer Network
Album Linux Driver




Latest articles about

 
EEWorld WeChat Subscription

 
EEWorld WeChat Service Number

 
AutoDevelopers

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building,Block B, 18 Zhongguancun Street, Haidian District,Beijing, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号