UART ISR Tx Rx 架构 [英] UART ISR Tx Rx Architecture

查看:18
本文介绍了UART ISR Tx Rx 架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是不是把事情复杂化了?

Am I complicating things?

我正在构建我的代码,以便通过 UART 从 8051 微型计算机与外围设备通信.外设响应来自主机的命令,一次只能响应一个命令.这是一个简单的发送和接收协议.(tx1, rx1, tx2, rx2, tx3, rx3) 每个 TX 消息以 CR 终止,每个响应以 > 终止.在收到对最后一条消息的响应之前,我无法发送新消息.如果启用该选项,响应还可以在开始时回显打印原始 TX 消息(但这会导致更多流量)

I'm architecting my code to talk from a 8051 micro to a peripheral device over UART. The peripheral responds to commands from the host and can only respond to one command at a time. It's a simple send and receive protocol. (tx1, rx1, tx2, rx2, tx3, rx3) Each TX message is terminated with a CR, each response is terminated with a >. I can't send a new message until I receive the response to the last one. Responses can also echo print the original TX message in the beginning if I enable that option (but this causes more traffic)

一个示例消息是:

  • TX:你好
  • RX:世界!>

或者使用 echo 选项...

Or with echo option...

  • TX:你好
  • RX:Hello\rWorld!>

选项 A诸如 getHello 之类的函数将包含发送和接收.并行 ISR 例程将收集传入的字节并在接收到>"字符时抛出一个标志.

Option A A function such as getHello would consist of both the send and receive. A parallel ISR routine would gather the incoming bytes and throw a flag when the '>' character is received.

char* getHello(char * buf){
    sendMsg("Hello\r");
    delay(10ms); //wait a little bit

    //wait for receive to come in or timeout to occur
    while(!receiveFlag || !timeoutFlag);  //thrown by ISR
    receiveMsg(buf);
    //parse the message and do some other stuff
    return buf;
}

优点:

  • 一切都包含在一个函数中.
  • 更容易调试

缺点:

  • 如果外设从未响应,此函数会阻塞并且可能挂起,因此必须实现超时.
  • 消息不能乱序接收,必须是串行的(即tx1、rx1、tx2、rx2、tx3、rx3)

选项 B采用并行方法.将创建两个单独的功能.一个发送消息,一个在收到来自 ISR 的响应时会被顶点.

Option B A parallel approach is taken. Two separate functions would created. one to send the message, and one that would vertexed upon receiving a response from the ISR.

void sendHello(){
    sendMsg("Hello\r");
    //do some other stuff if needed
}

char* receiveMsg(char * buf){
    //figure out from echo print what the tx message was
    //use a switch statement to decide which response parser to call
    switch(txMessage){ //pseudo code
    case "Hello":
        receiveMsg(buf);
        //parse the message and do some other stuff
        break;
    }
    return buf;
}

优点:

  • 可以处理无序返回的并行消息,因为它依赖于 tx 消息的回显打印来确定如何解析它.(即,tx1、tx2、tx3、rx1、rx2、rx3)

缺点:

  • 很难调试
  • 产生多个线程
  • 大量额外代码
  • 不值得,因为消息肯定会按顺序返回

现在,我正在做选项 B,但是当我继续这个项目时,我开始觉得这变得过于复杂了.我很好奇你们的想法.

Right now, I'm doing Option B, but as I continue on with the project, I begin to feel like this is getting overly complex. I'm curious what you guys think.

谢谢!

推荐答案

我倾向于做这种事情,不过,Id 倾向于有一个单独的串行端口类"(结构 + 函数)和一个协议类在串行端口的顶部.我一直在我的嵌入式系统中使用这些.这为您提供了两全其美的方法,即阻塞式同步调用和异步调用,以便您可以执行伪多任务.

I tend to do this kind of stuff, though, Id tend to have a separate serial port "class" ( struct + functions ) and a protocol class that lives over the top of serial port. I used these all the time in my embedded systems. This gives you the best of both worlds, a blocking synchronous call and an async call so you can pseudo multitask.

typedef struct serial_port_s serial_port;
typedef void (*serial_on_recived_proc)(serial_port* p);
typedef struct serial_port_s{
    bool timeoutFlag;
    bool receiveFlag;
    void* context;
    serial_on_recived_proc response_handler;
};

void send_serial(serial_port* p, char* message)
{
    //SendMsg?
}
void receive_serial(serial_port* p, char* response)
{
    //receiveMsg?
}

bool has_data(serial_port* p)
{
    return p->receiveFlag;
}

bool has_timed_out(serial_port* p)
{
    return p->timeoutFlag;
}
bool is_serial_finished(serial_port* p)
{
    return has_data(p) || has_timed_out(p); 
}

bool serial_check(serial_port* p)
{
    if(is_serial_finished(p) && p->response_handler != NULL)
    {
       p->response_handler(p)
       p-> response_handler = NULL;
       return true;
    }
    return false;
}

void send(serial_port* p, char* message, char* response)
{
    p->response_handler=NULL;
    send_serial(p, message);
    while(!is_serial_finished(p));
    receive_serial(p, response);
}

void sendAsync(serial_port* p, char* message, serial_on_recived_proc handler, void* context)
{
    p->response_handler = handler;
    p->context = context;
    send_serial(p, message);
}

void pow_response(serial_port* p)
{
    // could pass a pointer to a struct, or anything depending on what you want to do
    char* r = (char*)p->context;  
    receive_serial(p, r);
    // do stuff with the pow response
}

typedef struct
{
   char text[100];       
   int x;
   bool has_result;
} bang_t;

void bang_parse(bang_t* bang)
{
   bang->x = atoi(bang->text);
}

void bang_response(serial_port* p)
{
    bang_t* bang = (bang_t*)p->context;  
    receive_serial(p, bang->text);
    bang_parse(bang);
    bang->has_result=true;
}

void myFunc();
{
    char response[100];
    char pow[100];
    bang_t bang1;
    bang_t bang2;
    serial_port p; //
    int state = 1;
    // whatever you need to do to set the serial port

    // sends and blocks till a response/timeout
    send(&p, "Hello", response);
    // do what you like with the response

    // alternately, lets do an async send...
    sendAsync(&p, "Pow", pow_response, pow);       

    while(true)
    {
        // non block check, will process the response when it arrives               
        if(serial_check(p))
            {
              // it has responded to something, we can send something else...

              // using a very simple state machine, work out what to send next.
              // in practice I'd use enum for states, and functions for managing state
              // transitions, but for this example I'm just using an int which
              // I just increment to move to the next state
              switch(state)
              {
              case 1: 
                 // bang1 is the context, and will receive the data
                 sendAsync(&p, "Bang1", bang_response, &bang1);
                 state++; 
                 break;
              case 2:
                 // now bang2 is the context and will get the data...
                 sendAsync(&p, "Bang2", bang_response, &bang2);
                 state++; 
                 break;
              default:
                 //nothing more to send....
                 break;
              }
            }
        // do other stuff you want to do in parallel
    }
};

这篇关于UART ISR Tx Rx 架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆