From 36a2ffdf5a5a5a7e8b0835f02ca3d00a18d5cdf2 Mon Sep 17 00:00:00 2001 From: liangkangnan Date: Mon, 6 Apr 2020 20:20:31 +0800 Subject: [PATCH] example: add uart_tx Signed-off-by: liangkangnan --- tests/example/uart_tx/Makefile | 25 ++ tests/example/uart_tx/README.md | 1 + tests/example/uart_tx/link.ld | 147 ++++++++ tests/example/uart_tx/main.c | 32 ++ tests/example/uart_tx/start.S | 46 +++ tests/example/uart_tx/uart_tx | Bin 0 -> 9640 bytes tests/example/uart_tx/uart_tx.bin | Bin 0 -> 1852 bytes tests/example/uart_tx/uart_tx.dump | 563 +++++++++++++++++++++++++++++ tests/example/uart_tx/xprintf.c | 157 ++++++++ tests/example/uart_tx/xprintf.h | 19 + 10 files changed, 990 insertions(+) create mode 100644 tests/example/uart_tx/Makefile create mode 100644 tests/example/uart_tx/README.md create mode 100644 tests/example/uart_tx/link.ld create mode 100644 tests/example/uart_tx/main.c create mode 100644 tests/example/uart_tx/start.S create mode 100644 tests/example/uart_tx/uart_tx create mode 100644 tests/example/uart_tx/uart_tx.bin create mode 100644 tests/example/uart_tx/uart_tx.dump create mode 100644 tests/example/uart_tx/xprintf.c create mode 100644 tests/example/uart_tx/xprintf.h diff --git a/tests/example/uart_tx/Makefile b/tests/example/uart_tx/Makefile new file mode 100644 index 0000000..c3d981d --- /dev/null +++ b/tests/example/uart_tx/Makefile @@ -0,0 +1,25 @@ + +RISCV_ARCH := rv32im +RISCV_ABI := ilp32 + +RISCV_PATH := ../../../tools/gnu-mcu-eclipse-riscv-none-gcc-8.2.0-2.2-20190521-0004-win64/ + +CFLAGS += -march=$(RISCV_ARCH) +CFLAGS += -mabi=$(RISCV_ABI) +CFLAGS += -static -mcmodel=medany -fvisibility=hidden -nostdlib -nostartfiles + +RISCV_GCC := $(abspath $(RISCV_PATH)/bin/riscv-none-embed-gcc) +RISCV_AS := $(abspath $(RISCV_PATH)/bin/riscv-none-embed-as) +RISCV_GXX := $(abspath $(RISCV_PATH)/bin/riscv-none-embed-g++) +RISCV_OBJDUMP := $(abspath $(RISCV_PATH)/bin/riscv-none-embed-objdump) +RISCV_GDB := $(abspath $(RISCV_PATH)/bin/riscv-none-embed-gdb) +RISCV_AR := $(abspath $(RISCV_PATH)/bin/riscv-none-embed-ar) +RISCV_OBJCOPY := $(abspath $(RISCV_PATH)/bin/riscv-none-embed-objcopy) +RISCV_READELF := $(abspath $(RISCV_PATH)/bin/riscv-none-embed-readelf) + + +.PHONY: all +all: + $(RISCV_GCC) $(CFLAGS) start.S main.c xprintf.c -Tlink.ld -o uart_tx + $(RISCV_OBJCOPY) -O binary uart_tx uart_tx.bin + $(RISCV_OBJDUMP) --disassemble-all uart_tx > uart_tx.dump diff --git a/tests/example/uart_tx/README.md b/tests/example/uart_tx/README.md new file mode 100644 index 0000000..c9d5bc8 --- /dev/null +++ b/tests/example/uart_tx/README.md @@ -0,0 +1 @@ +串口发送例程。 \ No newline at end of file diff --git a/tests/example/uart_tx/link.ld b/tests/example/uart_tx/link.ld new file mode 100644 index 0000000..22f9450 --- /dev/null +++ b/tests/example/uart_tx/link.ld @@ -0,0 +1,147 @@ +OUTPUT_ARCH( "riscv" ) +ENTRY(_start) + + +MEMORY +{ + flash (wxa!ri) : ORIGIN = 0x00000000, LENGTH = 4K + ram (wxa!ri) : ORIGIN = 0x10000000, LENGTH = 2K +} + + +SECTIONS +{ + __stack_size = DEFINED(__stack_size) ? __stack_size : 1K; + + .init : + { + KEEP (*(SORT_NONE(.init))) + } >flash AT>flash + + .text : + { + *(.text.unlikely .text.unlikely.*) + *(.text.startup .text.startup.*) + *(.text .text.*) + *(.gnu.linkonce.t.*) + } >flash AT>flash + + . = ALIGN(4); + + PROVIDE (__etext = .); + PROVIDE (_etext = .); + PROVIDE (etext = .); + + .preinit_array : + { + PROVIDE_HIDDEN (__preinit_array_start = .); + KEEP (*(.preinit_array)) + PROVIDE_HIDDEN (__preinit_array_end = .); + } >flash AT>flash + + .init_array : + { + PROVIDE_HIDDEN (__init_array_start = .); + KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) + KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) + PROVIDE_HIDDEN (__init_array_end = .); + } >flash AT>flash + + .fini_array : + { + PROVIDE_HIDDEN (__fini_array_start = .); + KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) + KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) + PROVIDE_HIDDEN (__fini_array_end = .); + } >flash AT>flash + + .ctors : + { + /* gcc uses crtbegin.o to find the start of + the constructors, so we make sure it is + first. Because this is a wildcard, it + doesn't matter if the user does not + actually link against crtbegin.o; the + linker won't look for a file to match a + wildcard. The wildcard also means that it + doesn't matter which directory crtbegin.o + is in. */ + KEEP (*crtbegin.o(.ctors)) + KEEP (*crtbegin?.o(.ctors)) + /* We don't want to include the .ctor section from + the crtend.o file until after the sorted ctors. + The .ctor section from the crtend file contains the + end of ctors marker and it must be last */ + KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) + KEEP (*(SORT(.ctors.*))) + KEEP (*(.ctors)) + } >flash AT>flash + + .dtors : + { + KEEP (*crtbegin.o(.dtors)) + KEEP (*crtbegin?.o(.dtors)) + KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) + KEEP (*(SORT(.dtors.*))) + KEEP (*(.dtors)) + } >flash AT>flash + + .lalign : + { + . = ALIGN(4); + PROVIDE( _data_lma = . ); + } >flash AT>flash + + .dalign : + { + . = ALIGN(4); + PROVIDE( _data = . ); + } >ram AT>flash + + .data : + { + *(.rdata) + *(.rodata .rodata.*) + *(.gnu.linkonce.r.*) + *(.data .data.*) + *(.gnu.linkonce.d.*) + . = ALIGN(8); + PROVIDE( __global_pointer$ = . + 0x800 ); + *(.sdata .sdata.*) + *(.gnu.linkonce.s.*) + . = ALIGN(8); + *(.srodata.cst16) + *(.srodata.cst8) + *(.srodata.cst4) + *(.srodata.cst2) + *(.srodata .srodata.*) + } >ram AT>flash + + . = ALIGN(4); + PROVIDE( _edata = . ); + PROVIDE( edata = . ); + + PROVIDE( _fbss = . ); + PROVIDE( __bss_start = . ); + .bss : + { + *(.sbss*) + *(.gnu.linkonce.sb.*) + *(.bss .bss.*) + *(.gnu.linkonce.b.*) + *(COMMON) + . = ALIGN(4); + } >ram AT>ram + + . = ALIGN(8); + PROVIDE( _end = . ); + PROVIDE( end = . ); + + .stack ORIGIN(ram) + LENGTH(ram) - __stack_size : + { + PROVIDE( _heap_end = . ); + . = __stack_size; + PROVIDE( _sp = . ); + } >ram AT>ram + +} diff --git a/tests/example/uart_tx/main.c b/tests/example/uart_tx/main.c new file mode 100644 index 0000000..29b1cbc --- /dev/null +++ b/tests/example/uart_tx/main.c @@ -0,0 +1,32 @@ +#include + +#include "xprintf.h" + +// UART0 regs +#define UART0_BASE (0x30000000) +#define UART0_CTRL (UART0_BASE + (0x00)) +#define UART0_STATUS (UART0_BASE + (0x04)) +#define UART0_BAUD (UART0_BASE + (0x08)) +#define UART0_TXDATA (UART0_BASE + (0x0c)) + +#define UART0_REG(addr) (*((volatile uint32_t *)addr)) + + + +static void uart_putc(uint8_t d) +{ + while (UART0_REG(UART0_STATUS) & 0x1); + UART0_REG(UART0_TXDATA) = d; +} + + +int main() +{ + UART0_REG(UART0_CTRL) = 0x1; + + xdev_out(uart_putc); + + xprintf("%d hello world\n", 1); + + while (1); +} diff --git a/tests/example/uart_tx/start.S b/tests/example/uart_tx/start.S new file mode 100644 index 0000000..87074dd --- /dev/null +++ b/tests/example/uart_tx/start.S @@ -0,0 +1,46 @@ + .section .init; + .globl _start; + .type _start,@function + + +_start: + j _reset_handler + +_reset_handler: +.option push +.option norelax + la gp, __global_pointer$ +.option pop + la sp, _sp + li x26, 0x00 + li x27, 0x00 + + /* Load data section */ + la a0, _data_lma + la a1, _data + la a2, _edata + bgeu a1, a2, 2f +1: + lw t0, (a0) + sw t0, (a1) + addi a0, a0, 4 + addi a1, a1, 4 + bltu a1, a2, 1b +2: + + /* Clear bss section */ + la a0, __bss_start + la a1, _end + bgeu a0, a1, 2f +1: + sw zero, (a0) + addi a0, a0, 4 + bltu a0, a1, 1b +2: + + call main + + li x26, 0x01 + +loop: + j loop diff --git a/tests/example/uart_tx/uart_tx b/tests/example/uart_tx/uart_tx new file mode 100644 index 0000000000000000000000000000000000000000..f0d21b9bab6cbb81a6e58d9af960c2f0ec41f1ef GIT binary patch literal 9640 zcmeHN-ESOM6+iQ_z9FURB!ln+piVuTIIY|zl!%W4lBw-3l|ofYLm#lTS;XGIV;667CR$ue-745IJPh0s17}6YH(S zgXk{<@yxIYa_R3#y~Vm3NS+`!^)*MzEXd(Ovy>7mHb^P&^kkiRPEVA#sj!fynH-9cJ;PDHWn8X5Uc_O`H)Tbq^y@@|RsK><4b}Jvm82SI4 zV*hLZ<$Sy|LRWl;`S8pjA+BYMYwx+vKjQqFZJcje*|q*7{eCV~9<7@O{1$Ky`?R1o zfp#uyMs~jcEWl+e4|o3y5#nhJ!&f4^HAwFM)o9_GAojnp_D+Z8%&(ji62Bed-2yfy zurYyk#?8I3?vA{?4!>J6UT$@r{hz<5v$2p5dfuWKH?W4{Dc#%MMXo_F-s}E%tJHkV z&6yiJ`NlqEcze63C$P^U-VS>(HnHt>-Lu%%xudP?m^eF`#;_TM0(dVC)1?;bw=_t0 zi*XEkmqeFP-=6DqqMi1O_gd4v589{?pX;2l@H;$(UqVb8;P?wnw?xG9m~P*)#Zk>W*0pTp9`}xTlXK=iQ2ZOrA8+D3!;7d1 z_8vh^pmz^+HryeJ0qQ>;_5@A3@YVe-y%%<;Hy zhE~}xT6wpfjQ*3cMSVf_r7qzynU(lKDlSx$N_ns6@-K z<6>{MC;BU?*Y8-qa$am~_02mk?btVKh2QfX&H3=k6Zevdd(On2ol$o--%C2~B^~#Y zfqMzP{xa_HE9jfI(EF?8KGeatU}|peQ(7oYJ#6J8?jx>Mh;teHuAq zr>27kov-8`$sPQdW00X73OrYFFgLmlesDt z>g7f`D4Zx(N`ARcezjVonc{RM=h1Aj9u#V`fk$(-`gA2Y23d7Bs0DSJ`#~(BLgBby zohtf;S``-MdKQvKQ1ng~8q+^2)7-JyidTSB5(4XN(4a!22E(aFgN^Hf+)=7LDwL2c z70PO zSDl$DS5V};9iM?A_pRz~S)JsY;TWvr@c?FG$9f-CJi6($*=97X?ATVm^>IM-jU?XZ zA!Azc7Qg@LABo+UA!J&8trj`5XEsfful5^|@S7pF?t_emC=^t@NUuMWs}+;e$q5HJlx$x4%4+b~1n&}|Vb7yDlex)KI8 zYJDLnObXQdOenp#M^-IXax)^1vr5|rFwE181w(-r1SUwWu|k5z|Fg2?=M;WMf39`)Yr}8 zfl!Q>)wV)^_zls2yJX=V?%34R_+Yt{JkOa4pY2Jf5`c_Ux-Jo+RE5#ivyaA!|FYWhWfjk^UfsMe%EI1 zz!-;7?Nm;DCwy>~-x8WxwPj#Zj`3Uk9SFty`a3#w#k+?0ev)^=U3*9Tz?|6!+4_54 zU-wZ@xs9IS?s@bC=knEfBxh0$(EkM$a{p&y1wF>R=Df7`p9=N}{xdQU2A}_w){vCR zyTzCb+BY+O&QraI(a4&$$B6rNlC!k`TwFf_rY}!&3X|k~=7%3~{z}@vxtL?EQ26jC ze|OQ>&Fq`-O@`jD8mwEE{~dB()?91kef)g=4XRpN->U7sgj!GNgLCL|LbLhfo**af zXM61ZmSV#zgQ~>2?s1UzSe9()(uSTpYHKwy)1Dr$uVMW!-UfMblzG<|-X-`>+%QoC zzu}?fF?%!ZeYveQFF|wcWA0e)YH~iz*+*N!W0E%`Z~(8Hf^avyJ)oY79&0TE@_Ydw zPB2`7JoMTX`(~D_*atekz;g*4$kJRWtpWcHw8q`(oLIW6V^8tKA1NRBD>;%S#>L&) zo$%LjzrN!A-Yqej_~yZvQ++d7`i5sT^Wir?m`gV1oQ;|7WHX!Rl8L!wVlG*jOYr(V z%: + 0: 0040006f j 4 <_reset_handler> + +00000004 <_reset_handler>: + 4: 10001197 auipc gp,0x10001 + 8: 86c18193 addi gp,gp,-1940 # 10000870 <__global_pointer$> + c: f9018113 addi sp,gp,-112 # 10000800 <_sp> + 10: 00000d13 li s10,0 + 14: 00000d93 li s11,0 + 18: 6cc00513 li a0,1740 + 1c: 10000597 auipc a1,0x10000 + 20: fe458593 addi a1,a1,-28 # 10000000 <_data> + 24: 10000617 auipc a2,0x10000 + 28: 04c60613 addi a2,a2,76 # 10000070 <__bss_start> + 2c: 00c5fc63 bgeu a1,a2,44 <_reset_handler+0x40> + 30: 00052283 lw t0,0(a0) + 34: 0055a023 sw t0,0(a1) + 38: 00450513 addi a0,a0,4 + 3c: 00458593 addi a1,a1,4 + 40: fec5e8e3 bltu a1,a2,30 <_reset_handler+0x2c> + 44: 10000517 auipc a0,0x10000 + 48: 02c50513 addi a0,a0,44 # 10000070 <__bss_start> + 4c: 80818593 addi a1,gp,-2040 # 10000078 <_end> + 50: 00b57863 bgeu a0,a1,60 <_reset_handler+0x5c> + 54: 00052023 sw zero,0(a0) + 58: 00450513 addi a0,a0,4 + 5c: feb56ce3 bltu a0,a1,54 <_reset_handler+0x50> + 60: 058000ef jal ra,b8
+ 64: 00100d13 li s10,1 + +00000068 : + 68: 0000006f j 68 + +Disassembly of section .text: + +0000006c : + 6c: fe010113 addi sp,sp,-32 + 70: 00812e23 sw s0,28(sp) + 74: 02010413 addi s0,sp,32 + 78: 00050793 mv a5,a0 + 7c: fef407a3 sb a5,-17(s0) + 80: 00000013 nop + 84: 300007b7 lui a5,0x30000 + 88: 00478793 addi a5,a5,4 # 30000004 <__global_pointer$+0x1ffff794> + 8c: 0007a783 lw a5,0(a5) + 90: 0017f793 andi a5,a5,1 + 94: fe0798e3 bnez a5,84 + 98: 300007b7 lui a5,0x30000 + 9c: 00c78793 addi a5,a5,12 # 3000000c <__global_pointer$+0x1ffff79c> + a0: fef44703 lbu a4,-17(s0) + a4: 00e7a023 sw a4,0(a5) + a8: 00000013 nop + ac: 01c12403 lw s0,28(sp) + b0: 02010113 addi sp,sp,32 + b4: 00008067 ret + +000000b8
: + b8: ff010113 addi sp,sp,-16 + bc: 00112623 sw ra,12(sp) + c0: 00812423 sw s0,8(sp) + c4: 01010413 addi s0,sp,16 + c8: 300007b7 lui a5,0x30000 + cc: 00100713 li a4,1 + d0: 00e7a023 sw a4,0(a5) # 30000000 <__global_pointer$+0x1ffff790> + d4: 10000797 auipc a5,0x10000 + d8: fa078793 addi a5,a5,-96 # 10000074 + dc: 00000717 auipc a4,0x0 + e0: f9070713 addi a4,a4,-112 # 6c + e4: 00e7a023 sw a4,0(a5) + e8: 00100593 li a1,1 + ec: 10000517 auipc a0,0x10000 + f0: f1450513 addi a0,a0,-236 # 10000000 <_data> + f4: 578000ef jal ra,66c + f8: 0000006f j f8 + +000000fc : + fc: fe010113 addi sp,sp,-32 + 100: 00112e23 sw ra,28(sp) + 104: 00812c23 sw s0,24(sp) + 108: 02010413 addi s0,sp,32 + 10c: 00050793 mv a5,a0 + 110: fef407a3 sb a5,-17(s0) + 114: 10000797 auipc a5,0x10000 + 118: f5c78793 addi a5,a5,-164 # 10000070 <__bss_start> + 11c: 0007a783 lw a5,0(a5) + 120: 02078663 beqz a5,14c + 124: 10000797 auipc a5,0x10000 + 128: f4c78793 addi a5,a5,-180 # 10000070 <__bss_start> + 12c: 0007a783 lw a5,0(a5) + 130: 00178693 addi a3,a5,1 + 134: 10000717 auipc a4,0x10000 + 138: f3c70713 addi a4,a4,-196 # 10000070 <__bss_start> + 13c: 00d72023 sw a3,0(a4) + 140: fef44703 lbu a4,-17(s0) + 144: 00e78023 sb a4,0(a5) + 148: 02c0006f j 174 + 14c: 10000797 auipc a5,0x10000 + 150: f2878793 addi a5,a5,-216 # 10000074 + 154: 0007a783 lw a5,0(a5) + 158: 00078e63 beqz a5,174 + 15c: 10000797 auipc a5,0x10000 + 160: f1878793 addi a5,a5,-232 # 10000074 + 164: 0007a703 lw a4,0(a5) + 168: fef44783 lbu a5,-17(s0) + 16c: 00078513 mv a0,a5 + 170: 000700e7 jalr a4 + 174: 01c12083 lw ra,28(sp) + 178: 01812403 lw s0,24(sp) + 17c: 02010113 addi sp,sp,32 + 180: 00008067 ret + +00000184 : + 184: fe010113 addi sp,sp,-32 + 188: 00112e23 sw ra,28(sp) + 18c: 00812c23 sw s0,24(sp) + 190: 02010413 addi s0,sp,32 + 194: fea42623 sw a0,-20(s0) + 198: 01c0006f j 1b4 + 19c: fec42783 lw a5,-20(s0) + 1a0: 00178713 addi a4,a5,1 + 1a4: fee42623 sw a4,-20(s0) + 1a8: 0007c783 lbu a5,0(a5) + 1ac: 00078513 mv a0,a5 + 1b0: f4dff0ef jal ra,fc + 1b4: fec42783 lw a5,-20(s0) + 1b8: 0007c783 lbu a5,0(a5) + 1bc: fe0790e3 bnez a5,19c + 1c0: 00000013 nop + 1c4: 01c12083 lw ra,28(sp) + 1c8: 01812403 lw s0,24(sp) + 1cc: 02010113 addi sp,sp,32 + 1d0: 00008067 ret + +000001d4 : + 1d4: fb010113 addi sp,sp,-80 + 1d8: 04112623 sw ra,76(sp) + 1dc: 04812423 sw s0,72(sp) + 1e0: 05010413 addi s0,sp,80 + 1e4: faa42e23 sw a0,-68(s0) + 1e8: fab42c23 sw a1,-72(s0) + 1ec: fbc42783 lw a5,-68(s0) + 1f0: 00178713 addi a4,a5,1 + 1f4: fae42e23 sw a4,-68(s0) + 1f8: 0007c783 lbu a5,0(a5) + 1fc: fcf40ba3 sb a5,-41(s0) + 200: fd744783 lbu a5,-41(s0) + 204: 44078463 beqz a5,64c <__stack_size+0x24c> + 208: fd744703 lbu a4,-41(s0) + 20c: 02500793 li a5,37 + 210: 00f70a63 beq a4,a5,224 + 214: fd744783 lbu a5,-41(s0) + 218: 00078513 mv a0,a5 + 21c: ee1ff0ef jal ra,fc + 220: 4280006f j 648 <__stack_size+0x248> + 224: fc042e23 sw zero,-36(s0) + 228: fbc42783 lw a5,-68(s0) + 22c: 00178713 addi a4,a5,1 + 230: fae42e23 sw a4,-68(s0) + 234: 0007c783 lbu a5,0(a5) + 238: fcf40ba3 sb a5,-41(s0) + 23c: fd744703 lbu a4,-41(s0) + 240: 03000793 li a5,48 + 244: 02f71263 bne a4,a5,268 + 248: 00100793 li a5,1 + 24c: fcf42e23 sw a5,-36(s0) + 250: fbc42783 lw a5,-68(s0) + 254: 00178713 addi a4,a5,1 + 258: fae42e23 sw a4,-68(s0) + 25c: 0007c783 lbu a5,0(a5) + 260: fcf40ba3 sb a5,-41(s0) + 264: 02c0006f j 290 + 268: fd744703 lbu a4,-41(s0) + 26c: 02d00793 li a5,45 + 270: 02f71063 bne a4,a5,290 + 274: 00200793 li a5,2 + 278: fcf42e23 sw a5,-36(s0) + 27c: fbc42783 lw a5,-68(s0) + 280: 00178713 addi a4,a5,1 + 284: fae42e23 sw a4,-68(s0) + 288: 0007c783 lbu a5,0(a5) + 28c: fcf40ba3 sb a5,-41(s0) + 290: fe042023 sw zero,-32(s0) + 294: 0400006f j 2d4 + 298: fe042703 lw a4,-32(s0) + 29c: 00070793 mv a5,a4 + 2a0: 00279793 slli a5,a5,0x2 + 2a4: 00e787b3 add a5,a5,a4 + 2a8: 00179793 slli a5,a5,0x1 + 2ac: 00078713 mv a4,a5 + 2b0: fd744783 lbu a5,-41(s0) + 2b4: 00f707b3 add a5,a4,a5 + 2b8: fd078793 addi a5,a5,-48 + 2bc: fef42023 sw a5,-32(s0) + 2c0: fbc42783 lw a5,-68(s0) + 2c4: 00178713 addi a4,a5,1 + 2c8: fae42e23 sw a4,-68(s0) + 2cc: 0007c783 lbu a5,0(a5) + 2d0: fcf40ba3 sb a5,-41(s0) + 2d4: fd744703 lbu a4,-41(s0) + 2d8: 02f00793 li a5,47 + 2dc: 00e7f863 bgeu a5,a4,2ec + 2e0: fd744703 lbu a4,-41(s0) + 2e4: 03900793 li a5,57 + 2e8: fae7f8e3 bgeu a5,a4,298 + 2ec: fd744703 lbu a4,-41(s0) + 2f0: 06c00793 li a5,108 + 2f4: 00f70863 beq a4,a5,304 + 2f8: fd744703 lbu a4,-41(s0) + 2fc: 04c00793 li a5,76 + 300: 02f71263 bne a4,a5,324 + 304: fdc42783 lw a5,-36(s0) + 308: 0047e793 ori a5,a5,4 + 30c: fcf42e23 sw a5,-36(s0) + 310: fbc42783 lw a5,-68(s0) + 314: 00178713 addi a4,a5,1 + 318: fae42e23 sw a4,-68(s0) + 31c: 0007c783 lbu a5,0(a5) + 320: fcf40ba3 sb a5,-41(s0) + 324: fd744783 lbu a5,-41(s0) + 328: 32078663 beqz a5,654 <__stack_size+0x254> + 32c: fd744783 lbu a5,-41(s0) + 330: fcf40b23 sb a5,-42(s0) + 334: fd644703 lbu a4,-42(s0) + 338: 06000793 li a5,96 + 33c: 00e7f863 bgeu a5,a4,34c + 340: fd644783 lbu a5,-42(s0) + 344: fe078793 addi a5,a5,-32 + 348: fcf40b23 sb a5,-42(s0) + 34c: fd644783 lbu a5,-42(s0) + 350: fbe78793 addi a5,a5,-66 + 354: 01600713 li a4,22 + 358: 10f76663 bltu a4,a5,464 <__stack_size+0x64> + 35c: 00279713 slli a4,a5,0x2 + 360: 10000797 auipc a5,0x10000 + 364: cb078793 addi a5,a5,-848 # 10000010 <_data+0x10> + 368: 00f707b3 add a5,a4,a5 + 36c: 0007a703 lw a4,0(a5) + 370: 10000797 auipc a5,0x10000 + 374: ca078793 addi a5,a5,-864 # 10000010 <_data+0x10> + 378: 00f707b3 add a5,a4,a5 + 37c: 00078067 jr a5 + 380: fb842783 lw a5,-72(s0) + 384: 00478713 addi a4,a5,4 + 388: fae42c23 sw a4,-72(s0) + 38c: 0007a783 lw a5,0(a5) + 390: fcf42823 sw a5,-48(s0) + 394: fe042223 sw zero,-28(s0) + 398: 0100006f j 3a8 + 39c: fe442783 lw a5,-28(s0) + 3a0: 00178793 addi a5,a5,1 + 3a4: fef42223 sw a5,-28(s0) + 3a8: fd042703 lw a4,-48(s0) + 3ac: fe442783 lw a5,-28(s0) + 3b0: 00f707b3 add a5,a4,a5 + 3b4: 0007c783 lbu a5,0(a5) + 3b8: fe0792e3 bnez a5,39c + 3bc: 00c0006f j 3c8 + 3c0: 02000513 li a0,32 + 3c4: d39ff0ef jal ra,fc + 3c8: fdc42783 lw a5,-36(s0) + 3cc: 0027f793 andi a5,a5,2 + 3d0: 00079c63 bnez a5,3e8 + 3d4: fe442783 lw a5,-28(s0) + 3d8: 00178713 addi a4,a5,1 + 3dc: fee42223 sw a4,-28(s0) + 3e0: fe042703 lw a4,-32(s0) + 3e4: fce7eee3 bltu a5,a4,3c0 + 3e8: fd042503 lw a0,-48(s0) + 3ec: d99ff0ef jal ra,184 + 3f0: 00c0006f j 3fc + 3f4: 02000513 li a0,32 + 3f8: d05ff0ef jal ra,fc + 3fc: fe442783 lw a5,-28(s0) + 400: 00178713 addi a4,a5,1 + 404: fee42223 sw a4,-28(s0) + 408: fe042703 lw a4,-32(s0) + 40c: fee7e4e3 bltu a5,a4,3f4 + 410: 2380006f j 648 <__stack_size+0x248> + 414: fb842783 lw a5,-72(s0) + 418: 00478713 addi a4,a5,4 + 41c: fae42c23 sw a4,-72(s0) + 420: 0007a783 lw a5,0(a5) + 424: 0ff7f793 andi a5,a5,255 + 428: 00078513 mv a0,a5 + 42c: cd1ff0ef jal ra,fc + 430: 2180006f j 648 <__stack_size+0x248> + 434: 00200793 li a5,2 + 438: fef42623 sw a5,-20(s0) + 43c: 0380006f j 474 <__stack_size+0x74> + 440: 00800793 li a5,8 + 444: fef42623 sw a5,-20(s0) + 448: 02c0006f j 474 <__stack_size+0x74> + 44c: 00a00793 li a5,10 + 450: fef42623 sw a5,-20(s0) + 454: 0200006f j 474 <__stack_size+0x74> + 458: 01000793 li a5,16 + 45c: fef42623 sw a5,-20(s0) + 460: 0140006f j 474 <__stack_size+0x74> + 464: fd744783 lbu a5,-41(s0) + 468: 00078513 mv a0,a5 + 46c: c91ff0ef jal ra,fc + 470: 1d80006f j 648 <__stack_size+0x248> + 474: fdc42783 lw a5,-36(s0) + 478: 0047f793 andi a5,a5,4 + 47c: 00078c63 beqz a5,494 <__stack_size+0x94> + 480: fb842783 lw a5,-72(s0) + 484: 00478713 addi a4,a5,4 + 488: fae42c23 sw a4,-72(s0) + 48c: 0007a783 lw a5,0(a5) + 490: 0340006f j 4c4 <__stack_size+0xc4> + 494: fd644703 lbu a4,-42(s0) + 498: 04400793 li a5,68 + 49c: 00f71c63 bne a4,a5,4b4 <__stack_size+0xb4> + 4a0: fb842783 lw a5,-72(s0) + 4a4: 00478713 addi a4,a5,4 + 4a8: fae42c23 sw a4,-72(s0) + 4ac: 0007a783 lw a5,0(a5) + 4b0: 0140006f j 4c4 <__stack_size+0xc4> + 4b4: fb842783 lw a5,-72(s0) + 4b8: 00478713 addi a4,a5,4 + 4bc: fae42c23 sw a4,-72(s0) + 4c0: 0007a783 lw a5,0(a5) + 4c4: fcf42c23 sw a5,-40(s0) + 4c8: fd644703 lbu a4,-42(s0) + 4cc: 04400793 li a5,68 + 4d0: 02f71263 bne a4,a5,4f4 <__stack_size+0xf4> + 4d4: fd842783 lw a5,-40(s0) + 4d8: 0007de63 bgez a5,4f4 <__stack_size+0xf4> + 4dc: fd842783 lw a5,-40(s0) + 4e0: 40f007b3 neg a5,a5 + 4e4: fcf42c23 sw a5,-40(s0) + 4e8: fdc42783 lw a5,-36(s0) + 4ec: 0087e793 ori a5,a5,8 + 4f0: fcf42e23 sw a5,-36(s0) + 4f4: fe042423 sw zero,-24(s0) + 4f8: fd842703 lw a4,-40(s0) + 4fc: fec42783 lw a5,-20(s0) + 500: 02f777b3 remu a5,a4,a5 + 504: fcf40b23 sb a5,-42(s0) + 508: fd842703 lw a4,-40(s0) + 50c: fec42783 lw a5,-20(s0) + 510: 02f757b3 divu a5,a4,a5 + 514: fcf42c23 sw a5,-40(s0) + 518: fd644703 lbu a4,-42(s0) + 51c: 00900793 li a5,9 + 520: 02e7f463 bgeu a5,a4,548 <__stack_size+0x148> + 524: fd744703 lbu a4,-41(s0) + 528: 07800793 li a5,120 + 52c: 00f71663 bne a4,a5,538 <__stack_size+0x138> + 530: 02700793 li a5,39 + 534: 0080006f j 53c <__stack_size+0x13c> + 538: 00700793 li a5,7 + 53c: fd644703 lbu a4,-42(s0) + 540: 00e787b3 add a5,a5,a4 + 544: fcf40b23 sb a5,-42(s0) + 548: fe842783 lw a5,-24(s0) + 54c: 00178713 addi a4,a5,1 + 550: fee42423 sw a4,-24(s0) + 554: fd644703 lbu a4,-42(s0) + 558: 03070713 addi a4,a4,48 + 55c: 0ff77713 andi a4,a4,255 + 560: ff040693 addi a3,s0,-16 + 564: 00f687b3 add a5,a3,a5 + 568: fce78823 sb a4,-48(a5) + 56c: fd842783 lw a5,-40(s0) + 570: 00078863 beqz a5,580 <__stack_size+0x180> + 574: fe842703 lw a4,-24(s0) + 578: 00f00793 li a5,15 + 57c: f6e7fee3 bgeu a5,a4,4f8 <__stack_size+0xf8> + 580: fdc42783 lw a5,-36(s0) + 584: 0087f793 andi a5,a5,8 + 588: 02078063 beqz a5,5a8 <__stack_size+0x1a8> + 58c: fe842783 lw a5,-24(s0) + 590: 00178713 addi a4,a5,1 + 594: fee42423 sw a4,-24(s0) + 598: ff040713 addi a4,s0,-16 + 59c: 00f707b3 add a5,a4,a5 + 5a0: 02d00713 li a4,45 + 5a4: fce78823 sb a4,-48(a5) + 5a8: fe842783 lw a5,-24(s0) + 5ac: fef42223 sw a5,-28(s0) + 5b0: fdc42783 lw a5,-36(s0) + 5b4: 0017f793 andi a5,a5,1 + 5b8: 00078663 beqz a5,5c4 <__stack_size+0x1c4> + 5bc: 03000793 li a5,48 + 5c0: 0080006f j 5c8 <__stack_size+0x1c8> + 5c4: 02000793 li a5,32 + 5c8: fcf40b23 sb a5,-42(s0) + 5cc: 0100006f j 5dc <__stack_size+0x1dc> + 5d0: fd644783 lbu a5,-42(s0) + 5d4: 00078513 mv a0,a5 + 5d8: b25ff0ef jal ra,fc + 5dc: fdc42783 lw a5,-36(s0) + 5e0: 0027f793 andi a5,a5,2 + 5e4: 00079c63 bnez a5,5fc <__stack_size+0x1fc> + 5e8: fe442783 lw a5,-28(s0) + 5ec: 00178713 addi a4,a5,1 + 5f0: fee42223 sw a4,-28(s0) + 5f4: fe042703 lw a4,-32(s0) + 5f8: fce7ece3 bltu a5,a4,5d0 <__stack_size+0x1d0> + 5fc: fe842783 lw a5,-24(s0) + 600: fff78793 addi a5,a5,-1 + 604: fef42423 sw a5,-24(s0) + 608: fe842783 lw a5,-24(s0) + 60c: ff040713 addi a4,s0,-16 + 610: 00f707b3 add a5,a4,a5 + 614: fd07c783 lbu a5,-48(a5) + 618: 00078513 mv a0,a5 + 61c: ae1ff0ef jal ra,fc + 620: fe842783 lw a5,-24(s0) + 624: fc079ce3 bnez a5,5fc <__stack_size+0x1fc> + 628: 00c0006f j 634 <__stack_size+0x234> + 62c: 02000513 li a0,32 + 630: acdff0ef jal ra,fc + 634: fe442783 lw a5,-28(s0) + 638: 00178713 addi a4,a5,1 + 63c: fee42223 sw a4,-28(s0) + 640: fe042703 lw a4,-32(s0) + 644: fee7e4e3 bltu a5,a4,62c <__stack_size+0x22c> + 648: ba5ff06f j 1ec + 64c: 00000013 nop + 650: 0080006f j 658 <__stack_size+0x258> + 654: 00000013 nop + 658: 00000013 nop + 65c: 04c12083 lw ra,76(sp) + 660: 04812403 lw s0,72(sp) + 664: 05010113 addi sp,sp,80 + 668: 00008067 ret + +0000066c : + 66c: fb010113 addi sp,sp,-80 + 670: 02112623 sw ra,44(sp) + 674: 02812423 sw s0,40(sp) + 678: 03010413 addi s0,sp,48 + 67c: fca42e23 sw a0,-36(s0) + 680: 00b42223 sw a1,4(s0) + 684: 00c42423 sw a2,8(s0) + 688: 00d42623 sw a3,12(s0) + 68c: 00e42823 sw a4,16(s0) + 690: 00f42a23 sw a5,20(s0) + 694: 01042c23 sw a6,24(s0) + 698: 01142e23 sw a7,28(s0) + 69c: 02040793 addi a5,s0,32 + 6a0: fe478793 addi a5,a5,-28 + 6a4: fef42623 sw a5,-20(s0) + 6a8: fec42783 lw a5,-20(s0) + 6ac: 00078593 mv a1,a5 + 6b0: fdc42503 lw a0,-36(s0) + 6b4: b21ff0ef jal ra,1d4 + 6b8: 00000013 nop + 6bc: 02c12083 lw ra,44(sp) + 6c0: 02812403 lw s0,40(sp) + 6c4: 05010113 addi sp,sp,80 + 6c8: 00008067 ret + +Disassembly of section .data: + +10000000 <__global_pointer$-0x870>: +10000000: 6425 lui s0,0x9 +10000002: 6820 flw fs0,80(s0) +10000004: 6c65 lui s8,0x19 +10000006: 6f6c flw fa1,92(a4) +10000008: 7720 flw fs0,104(a4) +1000000a: 646c726f jal tp,100c7650 <__global_pointer$+0xc6de0> +1000000e: 000a c.slli zero,0x2 +10000010: 0424 addi s1,sp,520 +10000012: f000 fsw fs0,32(s0) +10000014: 0404 addi s1,sp,512 +10000016: f000 fsw fs0,32(s0) +10000018: 043c addi a5,sp,520 +1000001a: f000 fsw fs0,32(s0) +1000001c: 0454 addi a3,sp,516 +1000001e: f000 fsw fs0,32(s0) +10000020: 0454 addi a3,sp,516 +10000022: f000 fsw fs0,32(s0) +10000024: 0454 addi a3,sp,516 +10000026: f000 fsw fs0,32(s0) +10000028: 0454 addi a3,sp,516 +1000002a: f000 fsw fs0,32(s0) +1000002c: 0454 addi a3,sp,516 +1000002e: f000 fsw fs0,32(s0) +10000030: 0454 addi a3,sp,516 +10000032: f000 fsw fs0,32(s0) +10000034: 0454 addi a3,sp,516 +10000036: f000 fsw fs0,32(s0) +10000038: 0454 addi a3,sp,516 +1000003a: f000 fsw fs0,32(s0) +1000003c: 0454 addi a3,sp,516 +1000003e: f000 fsw fs0,32(s0) +10000040: 0454 addi a3,sp,516 +10000042: f000 fsw fs0,32(s0) +10000044: 0430 addi a2,sp,520 +10000046: f000 fsw fs0,32(s0) +10000048: 0454 addi a3,sp,516 +1000004a: f000 fsw fs0,32(s0) +1000004c: 0454 addi a3,sp,516 +1000004e: f000 fsw fs0,32(s0) +10000050: 0454 addi a3,sp,516 +10000052: f000 fsw fs0,32(s0) +10000054: 0370 addi a2,sp,396 +10000056: f000 fsw fs0,32(s0) +10000058: 0454 addi a3,sp,516 +1000005a: f000 fsw fs0,32(s0) +1000005c: 043c addi a5,sp,520 +1000005e: f000 fsw fs0,32(s0) +10000060: 0454 addi a3,sp,516 +10000062: f000 fsw fs0,32(s0) +10000064: 0454 addi a3,sp,516 +10000066: f000 fsw fs0,32(s0) +10000068: 0448 addi a0,sp,516 +1000006a: f000 fsw fs0,32(s0) +1000006c: 0000 unimp + ... + +Disassembly of section .bss: + +10000070 <__bss_start>: +10000070: 0000 unimp + ... + +10000074 : +10000074: 0000 unimp + ... + +Disassembly of section .stack: + +10000400 <_sp-0x400>: + ... + +Disassembly of section .comment: + +00000000 <.comment>: + 0: 3a434347 fmsub.d ft6,ft6,ft4,ft7,rmm + 4: 2820 fld fs0,80(s0) + 6: 20554e47 fmsub.s ft8,fa0,ft5,ft4,rmm + a: 434d li t1,19 + c: 2055 jal b0 + e: 6345 lui t1,0x11 + 10: 696c flw fa1,84(a0) + 12: 7370 flw fa2,100(a4) + 14: 2065 jal bc + 16: 4952 lw s2,20(sp) + 18: 562d4353 0x562d4353 + 1c: 4520 lw s0,72(a0) + 1e: 626d lui tp,0x1b + 20: 6465 lui s0,0x19 + 22: 6564 flw fs1,76(a0) + 24: 2064 fld fs1,192(s0) + 26: 2c434347 0x2c434347 + 2a: 3620 fld fs0,104(a2) + 2c: 2d34 fld fa3,88(a0) + 2e: 6962 flw fs2,24(sp) + 30: 2974 fld fa3,208(a0) + 32: 3820 fld fs0,112(s0) + 34: 322e fld ft4,232(sp) + 36: 302e fld ft0,232(sp) + ... diff --git a/tests/example/uart_tx/xprintf.c b/tests/example/uart_tx/xprintf.c new file mode 100644 index 0000000..748adf9 --- /dev/null +++ b/tests/example/uart_tx/xprintf.c @@ -0,0 +1,157 @@ +/*------------------------------------------------------------------------/ +/ Universal string handler for user console interface +/-------------------------------------------------------------------------/ +/ +/ Copyright (C) 2011, ChaN, all right reserved. +/ +/ * This software is a free software and there is NO WARRANTY. +/ * No restriction on use. You can use, modify and redistribute it for +/ personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY. +/ * Redistributions of source code must retain the above copyright notice. +/ +/-------------------------------------------------------------------------*/ + +#include "xprintf.h" + +#include + + +void (*xfunc_out)(unsigned char); /* Pointer to the output stream */ +static char *outptr; + +/*----------------------------------------------*/ +/* Put a character */ +/*----------------------------------------------*/ + +void xputc (char c) +{ + if (_CR_CRLF && c == '\n') xputc('\r'); /* CR -> CRLF */ + + if (outptr) { + *outptr++ = (unsigned char)c; + return; + } + + if (xfunc_out) xfunc_out((unsigned char)c); +} + + +/*----------------------------------------------*/ +/* Put a null-terminated string */ +/*----------------------------------------------*/ + +void xputs ( /* Put a string to the default device */ + const char* str /* Pointer to the string */ +) +{ + while (*str) + xputc(*str++); +} + + +/*----------------------------------------------*/ +/* Formatted string output */ +/*----------------------------------------------*/ +/* xprintf("%d", 1234); "1234" + xprintf("%6d,%3d%%", -200, 5); " -200, 5%" + xprintf("%-6u", 100); "100 " + xprintf("%ld", 12345678L); "12345678" + xprintf("%04x", 0xA3); "00a3" + xprintf("%08LX", 0x123ABC); "00123ABC" + xprintf("%016b", 0x550F); "0101010100001111" + xprintf("%s", "String"); "String" + xprintf("%-4s", "abc"); "abc " + xprintf("%4s", "abc"); " abc" + xprintf("%c", 'a'); "a" + xprintf("%f", 10.0); +*/ + +static +void xvprintf ( + const char* fmt, /* Pointer to the format string */ + va_list arp /* Pointer to arguments */ +) +{ + unsigned int r, i, j, w, f; + unsigned long v; + char s[16], c, d, *p; + + + for (;;) { + c = *fmt++; /* Get a char */ + if (!c) break; /* End of format? */ + if (c != '%') { /* Pass through it if not a % sequense */ + xputc(c); continue; + } + f = 0; + c = *fmt++; /* Get first char of the sequense */ + if (c == '0') { /* Flag: '0' padded */ + f = 1; c = *fmt++; + } else { + if (c == '-') { /* Flag: left justified */ + f = 2; c = *fmt++; + } + } + for (w = 0; c >= '0' && c <= '9'; c = *fmt++) /* Minimum width */ + w = w * 10 + c - '0'; + if (c == 'l' || c == 'L') { /* Prefix: Size is long int */ + f |= 4; c = *fmt++; + } + if (!c) break; /* End of format? */ + d = c; + if (d >= 'a') d -= 0x20; + switch (d) { /* Type is... */ + case 'S' : /* String */ + p = va_arg(arp, char*); + for (j = 0; p[j]; j++) ; + while (!(f & 2) && j++ < w) xputc(' '); + xputs(p); + while (j++ < w) xputc(' '); + continue; + case 'C' : /* Character */ + xputc((char)va_arg(arp, int)); continue; + case 'B' : /* Binary */ + r = 2; break; + case 'O' : /* Octal */ + r = 8; break; + case 'D' : /* Signed decimal */ + case 'U' : /* Unsigned decimal */ + r = 10; break; + case 'X' : /* Hexdecimal */ + r = 16; break; + default: /* Unknown type (passthrough) */ + xputc(c); continue; + } + + /* Get an argument and put it in numeral */ + v = (f & 4) ? va_arg(arp, long) : ((d == 'D') ? (long)va_arg(arp, int) : (long)va_arg(arp, unsigned int)); + if (d == 'D' && (v & 0x80000000)) { + v = 0 - v; + f |= 8; + } + i = 0; + do { + d = (char)(v % r); v /= r; + if (d > 9) d += (c == 'x') ? 0x27 : 0x07; + s[i++] = d + '0'; + } while (v && i < sizeof(s)); + if (f & 8) s[i++] = '-'; + j = i; d = (f & 1) ? '0' : ' '; + while (!(f & 2) && j++ < w) xputc(d); + do xputc(s[--i]); while(i); + while (j++ < w) xputc(' '); + } +} + +void xprintf ( /* Put a formatted string to the default device */ + const char* fmt, /* Pointer to the format string */ + ... /* Optional arguments */ +) +{ + va_list arp; + + + va_start(arp, fmt); + xvprintf(fmt, arp); + va_end(arp); +} diff --git a/tests/example/uart_tx/xprintf.h b/tests/example/uart_tx/xprintf.h new file mode 100644 index 0000000..5024dff --- /dev/null +++ b/tests/example/uart_tx/xprintf.h @@ -0,0 +1,19 @@ +/*------------------------------------------------------------------------*/ +/* Universal string handler for user console interface (C)ChaN, 2011 */ +/*------------------------------------------------------------------------*/ + +#ifndef _XPRINTF_H_ +#define _XPRINTF_H_ + +#define _CR_CRLF 0 /* 1: Convert \n ==> \r\n in the output char */ + +#define xdev_out(func) xfunc_out = (void(*)(unsigned char))(func) +extern void (*xfunc_out)(unsigned char); +void xputc (char c); +void xputs (const char* str); +void xprintf (const char* fmt, ...); +#define DW_CHAR sizeof(char) +#define DW_SHORT sizeof(short) +#define DW_LONG sizeof(long) + +#endif