2

我正在尝试将 printf 数据发送到我的uart设备。我已经适当地编写了 write_r() 函数。

我遇到的问题是,

  • 当我说printf(" My name is sam \n I am fine ");
  • 下次我说printf(" I am back ");

第一个问题:我只能看到"My name is Sam",然后光标转到下一行并停在那里。

第二个问题“我回来了”根本没有打印出来。

我尝试删除\n,仍然printf没有打印第二个。没有任何问题uart

看起来然后\到达指针丢失了。

我的代码

    int write(int file, char *ptr, int len) {

            #if !defined( OUT_8 )
        #define OUT_8(p,d)      (*(volatile char *)(p) = (char)(d))
        #endif
        #if !defined( IN_8 )
            #define IN_8(p)         ((*(volatile char *)(p)))
        #endif

        OUT_8(DUART1_UMCR1, 0x02); //Informs external modem or peripheral that the UART is ready for sending data
        OUT_8(DUART1_ULCR1, 0x03);
        OUT_8(DUART1_UIER1, 0x0f);
        OUT_8(DUART1_UFCR1, 0x8f);

        OUT_8(DUART1_UTHR, '\n');
        OUT_8(DUART1_UTHR, '\r');
        while (ptr!=NULL)
        {
            if (*ptr=='\n') // JUST A TRY to avoid \n bug
            {
             OUT_8(DUART1_UTHR, '\n');
             wait();
             *ptr++;
             *ptr++;
             OUT_8(DUART1_UTHR, *ptr++); // \n & \r when given through printf isNot working
             wait();
            }


            OUT_8(DUART1_UTHR, *ptr++); // \n & \r when given through printf is not working 
            wait(); // TODO:

            if(len==0)
                break;
            else
                len--;
        }

            OUT_8(DUART1_UMCR1, 0x00); // say that modem is not not ready. Connection over
            OUT_8(DUART1_UFCR1, 0x87);
            OUT_8(DUART1_ULCR1, 0x00); // Clear all the interrupts ! virtually shut the UART port
    errno = ENOSYS;
    return -1;
}
4

2 回答 2

1

确信 OP 的 I/O 缓冲在某处。在发送和/或
之前,输出不会出现。\r\n

#if !defined( OUT_8 )
  #define OUT_8(p,d)      (*(volatile char *)(p) = (char)(d))
#endif

// make ptr a `const char *`
int write(int file, const char *ptr, int len) {
  OUT_8(DUART1_UMCR1, 0x02); //Informs UART is ready for sending data
  OUT_8(DUART1_ULCR1, 0x03);
  OUT_8(DUART1_UIER1, 0x0f);
  OUT_8(DUART1_UFCR1, 0x8f);

  while (len-- > 0) {
    OUT_8(DUART1_UTHR, *ptr++);
    wait();
  }

  // Force an EOL at the _end_ of transmission.
  OUT_8(DUART1_UTHR, '\n');
  OUT_8(DUART1_UTHR, '\r');
  wait();

  OUT_8(DUART1_UMCR1, 0x00); // say that modem is not not ready. Connection over
  OUT_8(DUART1_UFCR1, 0x87);
  OUT_8(DUART1_ULCR1, 0x00); // Clear all interrupts! virtually shut UART port
  errno = ENOSYS;
  return -1;
}

我怀疑缓冲发生在接收 端,要么是 UART,要么更可能是在查看数据的终端中。OP说“光标转到下一行”。UART 中没有“光标”。

于 2013-09-25T12:33:21.570 回答
0

尝试fflush()在没有\n.

于 2016-01-14T12:09:18.293 回答