parent
38183534a3
commit
c4b07e8a00
@ -0,0 +1,74 @@
|
||||
// added @lab5_1.
|
||||
|
||||
#include <string.h>
|
||||
#include "uart.h"
|
||||
//add
|
||||
#include "util/types.h"
|
||||
#include "spike_interface/dts_parse.h"
|
||||
|
||||
volatile uint32* uart;
|
||||
|
||||
|
||||
void uart_putchar(uint8 ch)
|
||||
{
|
||||
volatile uint32 *status = (void*)(uintptr_t)0x60000008;
|
||||
volatile uint32 *tx = (void*)(uintptr_t)0x60000004;
|
||||
while (*status & 0x00000008);
|
||||
*tx = ch;
|
||||
}
|
||||
|
||||
int uart_getchar()
|
||||
{
|
||||
volatile uint32 *rx = (void*)(uintptr_t)0x60000000;
|
||||
volatile uint32 *status = (void*)(uintptr_t)0x60000008;
|
||||
while (!(*status & 0x00000001));
|
||||
int32_t ch = *rx;
|
||||
return ch;
|
||||
}
|
||||
|
||||
struct uart_scan
|
||||
{
|
||||
int compat;
|
||||
uint64 reg;
|
||||
};
|
||||
|
||||
static void uart_open(const struct fdt_scan_node *node, void *extra)
|
||||
{
|
||||
struct uart_scan *scan = (struct uart_scan *)extra;
|
||||
memset(scan, 0, sizeof(*scan));
|
||||
}
|
||||
|
||||
static void uart_prop(const struct fdt_scan_prop *prop, void *extra)
|
||||
{
|
||||
struct uart_scan *scan = (struct uart_scan *)extra;
|
||||
if (!strcmp(prop->name, "compatible") && fdt_string_list_index(prop, "sifive,uart0") >= 0) {
|
||||
scan->compat = 1;
|
||||
} else if (!strcmp(prop->name, "reg")) {
|
||||
fdt_get_address(prop->node->parent, prop->value, &scan->reg);
|
||||
}
|
||||
}
|
||||
|
||||
static void uart_done(const struct fdt_scan_node *node, void *extra)
|
||||
{
|
||||
struct uart_scan *scan = (struct uart_scan *)extra;
|
||||
if (!scan->compat || !scan->reg || uart) return;
|
||||
|
||||
// Enable Rx/Tx channels
|
||||
uart = (void*)(uintptr_t)scan->reg;
|
||||
uart[UART_REG_TXCTRL] = UART_TXEN;
|
||||
uart[UART_REG_RXCTRL] = UART_RXEN;
|
||||
}
|
||||
|
||||
void query_uart(uintptr_t fdt)
|
||||
{
|
||||
struct fdt_cb cb;
|
||||
struct uart_scan scan;
|
||||
|
||||
memset(&cb, 0, sizeof(cb));
|
||||
cb.open = uart_open;
|
||||
cb.prop = uart_prop;
|
||||
cb.done = uart_done;
|
||||
cb.extra = &scan;
|
||||
|
||||
fdt_scan(fdt, &cb);
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
// added @lab5_1.
|
||||
|
||||
#ifndef _RISCV_UART_H
|
||||
#define _RISCV_UART_H
|
||||
|
||||
#include <stdint.h>
|
||||
//add
|
||||
#include "util/types.h"
|
||||
|
||||
extern volatile uint32* uart;
|
||||
|
||||
#define UART_REG_TXFIFO 0
|
||||
#define UART_REG_RXFIFO 1
|
||||
#define UART_REG_TXCTRL 2
|
||||
#define UART_REG_RXCTRL 3
|
||||
#define UART_REG_IE 4
|
||||
#define UART_REG_IP 5
|
||||
#define UART_REG_DIV 6
|
||||
|
||||
#define UART_TXEN 0x1
|
||||
#define UART_RXEN 0x1
|
||||
|
||||
void uart_putchar(uint8_t ch);
|
||||
int uart_getchar();
|
||||
void query_uart(uintptr_t dtb);
|
||||
|
||||
#endif
|
@ -1,94 +0,0 @@
|
||||
#include "user_lib.h"
|
||||
#include "util/string.h"
|
||||
#include "util/types.h"
|
||||
|
||||
void ls(char *path) {
|
||||
int dir_fd = opendir_u(path);
|
||||
printu("------------------------------\n");
|
||||
printu("ls \"%s\":\n", path);
|
||||
printu("[name] [inode_num]\n");
|
||||
struct dir dir;
|
||||
int width = 20;
|
||||
while(readdir_u(dir_fd, &dir) == 0) {
|
||||
// we do not have %ms :(
|
||||
char name[width + 1];
|
||||
memset(name, ' ', width + 1);
|
||||
name[width] = '\0';
|
||||
if (strlen(dir.name) < width) {
|
||||
strcpy(name, dir.name);
|
||||
name[strlen(dir.name)] = ' ';
|
||||
printu("%s %d\n", name, dir.inum);
|
||||
}
|
||||
else
|
||||
printu("%s %d\n", dir.name, dir.inum);
|
||||
}
|
||||
printu("------------------------------\n");
|
||||
closedir_u(dir_fd);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int MAXBUF = 512;
|
||||
char str[] = "hello world";
|
||||
char buf[MAXBUF];
|
||||
int fd1, fd2;
|
||||
|
||||
printu("\n======== establish the file ========\n");
|
||||
|
||||
fd1 = open("/RAMDISK0/ramfile", O_RDWR | O_CREAT);
|
||||
printu("create file: /RAMDISK0/ramfile\n");
|
||||
close(fd1);
|
||||
|
||||
printu("\n======== Test 1: hard link ========\n");
|
||||
|
||||
link_u("/RAMDISK0/ramfile", "/RAMDISK0/ramfile2");
|
||||
printu("create hard link: /RAMDISK0/ramfile2 -> /RAMDISK0/ramfile\n");
|
||||
|
||||
fd1 = open("/RAMDISK0/ramfile", O_RDWR);
|
||||
fd2 = open("/RAMDISK0/ramfile2", O_RDWR);
|
||||
|
||||
printu("file descriptor fd1 (ramfile): %d\n", fd1);
|
||||
printu("file descriptor fd2 (ramfile2): %d\n", fd2);
|
||||
|
||||
// check the number of hard links to ramfile on disk
|
||||
struct istat st;
|
||||
disk_stat_u(fd1, &st);
|
||||
printu("ramfile hard links: %d\n", st.st_nlinks);
|
||||
if (st.st_nlinks != 2) {
|
||||
printu("ERROR: the number of hard links to ramfile should be 2, but it is %d\n",
|
||||
st.st_nlinks);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
write_u(fd1, str, strlen(str));
|
||||
printu("/RAMDISK0/ramfile write content: \n%s\n", str);
|
||||
|
||||
read_u(fd2, buf, MAXBUF);
|
||||
printu("/RAMDISK0/ramfile2 read content: \n%s\n", buf);
|
||||
|
||||
close(fd1);
|
||||
close(fd2);
|
||||
|
||||
printu("\n======== Test 2: unlink ========\n");
|
||||
|
||||
ls("/RAMDISK0");
|
||||
|
||||
unlink_u("/RAMDISK0/ramfile");
|
||||
printu("unlink: /RAMDISK0/ramfile\n");
|
||||
|
||||
ls("/RAMDISK0");
|
||||
|
||||
// check the number of hard links to ramfile2 on disk
|
||||
fd2 = open("/RAMDISK0/ramfile2", O_RDWR);
|
||||
disk_stat_u(fd2, &st);
|
||||
printu("ramfile2 hard links: %d\n", st.st_nlinks);
|
||||
if (st.st_nlinks != 1) {
|
||||
printu("ERROR: the number of hard links to ramfile should be 1, but it is %d\n",
|
||||
st.st_nlinks);
|
||||
exit(-1);
|
||||
}
|
||||
close(fd2);
|
||||
|
||||
printu("\nAll tests passed!\n\n");
|
||||
exit(0);
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Below is the given application for lab5_1.
|
||||
* The goal of this app is to control the car via Bluetooth.
|
||||
*/
|
||||
|
||||
#include "user_lib.h"
|
||||
#include "util/types.h"
|
||||
|
||||
int main(void) {
|
||||
printu("please input the instruction through bluetooth!\n");
|
||||
while(1)
|
||||
{
|
||||
char temp = (char)uartgetchar();
|
||||
if(temp == 'q')
|
||||
break;
|
||||
car_control(temp);
|
||||
}
|
||||
exit(0);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in new issue