我是嵌入式 C 新手。
我的树莓派有一个功能正常的 uart(读写)。
这是我的.h 文件
#ifndef _UART_H
#define _UART_H
#define IO_BASE_ADDR 0x3f200000
#define UART0_DR IO_BASE_ADDR + 0x1000
#define UART0_FR IO_BASE_ADDR + 0x1018
#define UART0_CR IO_BASE_ADDR + 0x1030
#define UART0_LCRH IO_BASE_ADDR + 0x102c
#define UART0_FBRD IO_BASE_ADDR + 0x1028
#define UART0_IBRD IO_BASE_ADDR + 0x1024
#define UART0_IMSC IO_BASE_ADDR + 0x1038
unsigned char read_char(void);
void write_char(unsigned char c);
void write_string(const char *string);
void init_uart(void);
#endif
这是我的.c 文件
#include "uart.h"
#include "lib.h"
void write_char(unsigned char c)
{
while (in_word(UART0_FR) & (1 << 5)) { }
out_word(UART0_DR, c);
}
unsigned char read_char(void)
{
while (in_word(UART0_FR) & (1 << 4)) { }
return in_word(UART0_DR);
}
void write_string(const char *string)
{
for (int i = 0; string[i] != '\0'; i++) {
write_char(string[i]);
}
}
void init_uart(void)
{
out_word(UART0_CR, 0);
out_word(UART0_IBRD, 26);
out_word(UART0_FBRD, 0);
out_word(UART0_LCRH, (1 << 4) | (1 << 5) | (1 << 6));
out_word(UART0_IMSC, 0);
out_word(UART0_CR, (1 << 0) | (1 << 8) | (1 << 9));
}
我可以调用另一个c文件中的读写函数。
我不明白init_uart是在哪里调用的?
谢谢 阿卡什
所以我在其他地方调用 init_uart,感谢 grep -rn init_uart。