gpt4 book ai didi

c - 驱动程序使用寄存器映射通过 c 中的 uart 读取数据字符串

转载 作者:行者123 更新时间:2023-11-30 19:35:25 25 4
gpt4 key购买 nike

我正在尝试编写一个驱动程序来通过 UART 发送和接收字符串(在 fpga 上实现)。这是 UART 寄存器映射的图片。 enter image description here我已经能够编写一个函数来发送字符串。它检查状态寄存器中的发送读取位 (trdy),如果已设置,则将数据放入 txdata 寄存器。

#define UART_BASE 0x00021000

int putstring(char *str) {

volatile uint32_t *uart = (volatile uint32_t*) UART_BASE;

while (1)
{
while (*str != '\0')
{
while ((uart[2] & (1<<6)) == 0);
uart[1] = *str;
str++;
}
}
}

我的问题是,我现在正在尝试读取字符串,但我在逻辑上遇到了困难。我希望它继续读取字符,直到用户按 Enter 键。这是一个菜单。

我想知道是否有人可以提供建议?

void getstring(char *str) {

volatile uint32_t *uart = (volatile uint32_t*) UART_BASE;

char c = 0;
do
{
while ((uart[2] & (1<<5)) == 0);
c = uart[0];
*str++ = c;
}
while (c!='\0');


}

这是我的主要功能。它将单词“test”输出到控制台。然后我期待输入a b Ctrl - @然后我期望它显示 ab在控制台上但它不起作用。

我是不是做错了什么愚蠢的事情?

谢谢

int main(void)
{
char output[] = "Test";
char input[2];

putstring(output);

getstring(input);

putstring(input);

return 0;
}

最佳答案

这应该将字符串读取到 str;直到收到符号'\0'。

char c = 0;
do
{
while ((uart[2] & (1<<5)) == 0);
c = uart[0];
*str++ = c;
} while (c!='\0');

关于c - 驱动程序使用寄存器映射通过 c 中的 uart 读取数据字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42692451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com