Minimal printk in C
This commit is contained in:
parent
49f0237874
commit
f8fd8740c5
3
Makefile
3
Makefile
@ -1,6 +1,6 @@
|
||||
.POSIX:
|
||||
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_SRC=$(wildcard src/*.cpp)
|
||||
@ -19,7 +19,6 @@ environ:
|
||||
|
||||
bin/kernel.elf: environ $(A_OBJ) $(C_OBJ)
|
||||
arm-none-eabi-ld --nmagic -nostdlib -T src/linker.ld -o bin/kernel.elf obj/*.o
|
||||
# maybe -msomearch
|
||||
|
||||
obj/s_%.o: src/%.s
|
||||
$(CCX) $(CCFLAGS) -o $@ $<
|
||||
|
@ -1,3 +1,33 @@
|
||||
extern "C" int f(int a) {
|
||||
return a + 1;
|
||||
#define UART_RBR 0x01c28000
|
||||
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;
|
||||
}
|
||||
|
||||
|
58
src/kernel.s
58
src/kernel.s
@ -1,4 +1,3 @@
|
||||
.extern f
|
||||
|
||||
.text
|
||||
my_string:
|
||||
@ -9,57 +8,28 @@ my_string:
|
||||
.align 4
|
||||
.global _start
|
||||
_start:
|
||||
/* does nothing special */
|
||||
mov r13, #0x8000
|
||||
|
||||
mov r1, #16
|
||||
|
||||
1:
|
||||
ldr r0, =my_string
|
||||
bl printk
|
||||
sub r1, #1
|
||||
cmp r1, #0
|
||||
b 1b
|
||||
|
||||
lup:
|
||||
b lup
|
||||
bl main
|
||||
|
||||
|
||||
|
||||
|
||||
endless_busy_loop:
|
||||
cpsid if
|
||||
b endless_busy_loop
|
||||
|
||||
.extern c_printk
|
||||
.global printk
|
||||
printk:
|
||||
stmfd r13!, {r0, r1, r2, r3, r4, r14}
|
||||
push {lr}
|
||||
|
||||
/* disable UART interrupts */
|
||||
mov r2, #0x8004
|
||||
movt r2, #0x01c2
|
||||
|
||||
ldr r1, [r2]
|
||||
and r1, #0xfffffffc
|
||||
str r1, [r2]
|
||||
|
||||
/* mask interrupts in CPU */
|
||||
/* disables IRQ and FIR in CPU */
|
||||
cpsid if
|
||||
|
||||
/* TBR */
|
||||
mov r2, #0x8000
|
||||
movt r2, #0x01c2
|
||||
|
||||
1:
|
||||
ldr r1, [r0]
|
||||
add r0, #4
|
||||
bl c_printk
|
||||
|
||||
mov r4, #4
|
||||
2:
|
||||
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:
|
||||
/* enables IRQ and FIR in CPU */
|
||||
cpsie if
|
||||
|
||||
ldmfd r13!, {r0, r1, r2, r3, r4, r15}
|
||||
pop {pc}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user