Minimal printk in C

This commit is contained in:
giomba 2018-12-28 10:27:49 +01:00
parent 49f0237874
commit f8fd8740c5
3 changed files with 47 additions and 48 deletions

View File

@ -1,6 +1,6 @@
.POSIX: .POSIX:
CCX=arm-none-eabi-g++ CCX=arm-none-eabi-g++
CCFLAGS=-c -Wall -fno-stack-protector -ffreestanding -march=armv7-a -nostdlib -Iinclude -g CCFLAGS=-c -Wall -fno-stack-protector -ffreestanding -fno-exceptions -march=armv7-a -nostdlib -Iinclude -g
C_HDR=$(wildcard include/*.h) C_HDR=$(wildcard include/*.h)
C_SRC=$(wildcard src/*.cpp) C_SRC=$(wildcard src/*.cpp)
@ -19,7 +19,6 @@ environ:
bin/kernel.elf: environ $(A_OBJ) $(C_OBJ) bin/kernel.elf: environ $(A_OBJ) $(C_OBJ)
arm-none-eabi-ld --nmagic -nostdlib -T src/linker.ld -o bin/kernel.elf obj/*.o arm-none-eabi-ld --nmagic -nostdlib -T src/linker.ld -o bin/kernel.elf obj/*.o
# maybe -msomearch
obj/s_%.o: src/%.s obj/s_%.o: src/%.s
$(CCX) $(CCFLAGS) -o $@ $< $(CCX) $(CCFLAGS) -o $@ $<

View File

@ -1,3 +1,33 @@
extern "C" int f(int a) { #define UART_RBR 0x01c28000
return a + 1; char* const UART_THR = (char* const)0x01c28000;
#define UART_IER 0x01c28004
char* const UART_LSR = (char* const)0x01c28014;
extern "C" int printk(const char*);
extern "C" int main(int argc, char** argv) {
char letter[2] = "A";
for (char c = 'A'; c < 'Z'; c++) {
letter[0] = c;
printk("HELLO WORLD!");
printk("\t");
printk(letter);
printk("\r\n");
}
return 0;
} }
/* Never call this function directly: always use the printk,
* which disables and reenables interrupts */
extern "C" int c_printk(const char* msg) {
while (*msg != '\0') {
while ((*UART_LSR & 0x20) == 0); /* Wait for transmission */
*UART_THR = *msg;
msg++;
}
return 0;
}

View File

@ -1,4 +1,3 @@
.extern f
.text .text
my_string: my_string:
@ -9,57 +8,28 @@ my_string:
.align 4 .align 4
.global _start .global _start
_start: _start:
/* does nothing special */ bl main
mov r13, #0x8000
mov r1, #16
1:
ldr r0, =my_string
bl printk
sub r1, #1
cmp r1, #0
b 1b
lup:
b lup
endless_busy_loop:
cpsid if
b endless_busy_loop
.extern c_printk
.global printk .global printk
printk: printk:
stmfd r13!, {r0, r1, r2, r3, r4, r14} push {lr}
/* disable UART interrupts */ /* disables IRQ and FIR in CPU */
mov r2, #0x8004
movt r2, #0x01c2
ldr r1, [r2]
and r1, #0xfffffffc
str r1, [r2]
/* mask interrupts in CPU */
cpsid if cpsid if
/* TBR */
mov r2, #0x8000
movt r2, #0x01c2
1: bl c_printk
ldr r1, [r0]
add r0, #4
mov r4, #4 /* enables IRQ and FIR in CPU */
2: cpsie if
and r3, r1, #0xff
mov r1, r1, lsr#8
cmp r3, #0
beq 3f
str r3, [r2]
sub r4, #1
cmp r4, #0
bne 2b
b 1b
3:
ldmfd r13!, {r0, r1, r2, r3, r4, r15} pop {pc}