rust_lib_demo: Use common console code
authorAnton Blanchard <anton@linux.ibm.com>
Thu, 16 Apr 2020 07:14:18 +0000 (17:14 +1000)
committerAnton Blanchard <anton@ozlabs.org>
Thu, 16 Apr 2020 07:26:21 +0000 (17:26 +1000)
Use a symlink to share the console code in hello_world. Not ideal,
but we can improve on it later.

Signed-off-by: Anton Blanchard <anton@linux.ibm.com>
rust_lib_demo/Makefile
rust_lib_demo/console.c [new symlink]
rust_lib_demo/console.h [new symlink]
rust_lib_demo/hello_world.c

index 990a865c137eb8e4ffff238b69b1e966a1ee4497..26aebf89e72087e6993a0a88e39406889b7d47a7 100644 (file)
@@ -1,15 +1,15 @@
 ARCH = $(shell uname -m)
 ifneq ("$(ARCH)", "ppc64")
 ifneq ("$(ARCH)", "ppc64le")
-        CROSS_COMPILE = powerpc64le-linux-gnu-
-        endif
-        endif
+       CROSS_COMPILE ?= powerpc64le-linux-
+endif
+endif
 
 CC = $(CROSS_COMPILE)gcc
 LD = $(CROSS_COMPILE)ld
 OBJCOPY = $(CROSS_COMPILE)objcopy
 
-CFLAGS = -Os -g -Wall -std=c99 -msoft-float -mno-string -mno-multiple -mno-vsx -mno-altivec -mlittle-endian -fno-stack-protector -mstrict-align -ffreestanding -fdata-sections -ffunction-sections 
+CFLAGS = -Os -g -Wall -std=c99 -msoft-float -mno-string -mno-multiple -mno-vsx -mno-altivec -mlittle-endian -fno-stack-protector -mstrict-align -ffreestanding -fdata-sections -ffunction-sections
 ASFLAGS = $(CFLAGS)
 LDFLAGS = -T powerpc.lds
 
@@ -20,7 +20,7 @@ all: hello_world.hex
 run:
        -ln -sf hello_world.bin main_ram.bin
        ../core_tb > /dev/null
-       
+
 $(RUSTLIB): src/lib.rs
        RUSTFLAGS="-C target-feature=-vsx,-altivec,-hard-float" xargo build --release
 
@@ -30,14 +30,14 @@ size:
 dump:
        powerpc64le-linux-gnu-objdump -S hello_world.elf | less
 
-hello_world.elf: hello_world.o head.o $(RUSTLIB)
-       $(LD) $(LDFLAGS) -o hello_world.elf hello_world.o head.o $(RUSTLIB)
+hello_world.elf: hello_world.o console.o head.o $(RUSTLIB)
+       $(LD) $(LDFLAGS) -o $@ $^
 
 hello_world.bin: hello_world.elf
-       $(OBJCOPY) -O binary hello_world.elf hello_world.bin
+       $(OBJCOPY) -O binary $^ $@
 
 hello_world.hex: hello_world.bin
-       ./bin2hex.py hello_world.bin > hello_world.hex
+       ../scripts/bin2hex.py $^ > $@
 
 clean:
        cargo clean
diff --git a/rust_lib_demo/console.c b/rust_lib_demo/console.c
new file mode 120000 (symlink)
index 0000000..112b085
--- /dev/null
@@ -0,0 +1 @@
+../hello_world/console.c
\ No newline at end of file
diff --git a/rust_lib_demo/console.h b/rust_lib_demo/console.h
new file mode 120000 (symlink)
index 0000000..fbe66d5
--- /dev/null
@@ -0,0 +1 @@
+../hello_world/console.h
\ No newline at end of file
index 95b4d09dea1449ce946109d51a56f13754ae21e7..f46140d185533b39c2dd74f4065e444ff3a54d0e 100644 (file)
@@ -1,135 +1,7 @@
-#include <unistd.h>
-#include <string.h>
 #include <stdint.h>
 #include <stdbool.h>
 
-/*
- * Core UART functions to implement for a port
- */
-
-static uint64_t potato_uart_base;
-
-#define PROC_FREQ 50000000
-#define UART_FREQ 115200
-#define UART_BASE 0xc0002000
-
-#define POTATO_CONSOLE_TX              0x00
-#define POTATO_CONSOLE_RX              0x08
-#define POTATO_CONSOLE_STATUS          0x10
-#define   POTATO_CONSOLE_STATUS_RX_EMPTY               0x01
-#define   POTATO_CONSOLE_STATUS_TX_EMPTY               0x02
-#define   POTATO_CONSOLE_STATUS_RX_FULL                        0x04
-#define   POTATO_CONSOLE_STATUS_TX_FULL                        0x08
-#define POTATO_CONSOLE_CLOCK_DIV       0x18
-#define POTATO_CONSOLE_IRQ_EN          0x20
-
-static uint64_t potato_uart_reg_read(int offset)
-{
-       uint64_t addr;
-       uint64_t val;
-
-       addr = potato_uart_base + offset;
-
-       val = *(volatile uint64_t *)addr;
-
-       return val;
-}
-
-static void potato_uart_reg_write(int offset, uint64_t val)
-{
-       uint64_t addr;
-
-       addr = potato_uart_base + offset;
-
-       *(volatile uint64_t *)addr = val;
-}
-
-static int potato_uart_rx_empty(void)
-{
-       uint64_t val;
-
-       val = potato_uart_reg_read(POTATO_CONSOLE_STATUS);
-
-       if (val & POTATO_CONSOLE_STATUS_RX_EMPTY)
-               return 1;
-
-       return 0;
-}
-
-static int potato_uart_tx_full(void)
-{
-       uint64_t val;
-
-       val = potato_uart_reg_read(POTATO_CONSOLE_STATUS);
-
-       if (val & POTATO_CONSOLE_STATUS_TX_FULL)
-               return 1;
-
-       return 0;
-}
-
-static char potato_uart_read(void)
-{
-       uint64_t val;
-
-       val = potato_uart_reg_read(POTATO_CONSOLE_RX);
-
-       return (char)(val & 0x000000ff);
-}
-
-static void potato_uart_write(char c)
-{
-       uint64_t val;
-
-       val = c;
-
-       potato_uart_reg_write(POTATO_CONSOLE_TX, val);
-}
-
-static unsigned long potato_uart_divisor(unsigned long proc_freq, unsigned long uart_freq)
-{
-       return proc_freq / (uart_freq * 16) - 1;
-}
-
-void potato_uart_init(void)
-{
-       potato_uart_base = UART_BASE;
-
-       potato_uart_reg_write(POTATO_CONSOLE_CLOCK_DIV, potato_uart_divisor(PROC_FREQ, UART_FREQ));
-}
-
-int getchar(void)
-{
-       while (potato_uart_rx_empty())
-               /* Do nothing */ ;
-
-       return potato_uart_read();
-}
-
-void putchar(unsigned char c)
-{
-       while (potato_uart_tx_full())
-               /* Do Nothing */;
-
-       potato_uart_write(c);
-}
-
-void putstr(const char *str, unsigned long len)
-{
-       for (unsigned long i = 0; i < len; i++) {
-               putchar(str[i]);
-       }
-}
-
-size_t strlen(const char *s)
-{
-       size_t len = 0;
-
-       while (*s++)
-               len++;
-
-       return len;
-}
+#include "console.h"
 
 void rust_main();
 
@@ -148,7 +20,6 @@ void init_bss()
        }
 }
 
-
 #define HELLO_WORLD "Hello World\r\n"
 
 int main(void)