Skip to content

Commit 4f7c9bf

Browse files
Merge pull request s-matyukevich#172 from stefanJi/feat/stefanji_lesson01_2
feat: implement lesson01-2
2 parents 4b151a2 + c3f4b0f commit 4f7c9bf

File tree

16 files changed

+245
-0
lines changed

16 files changed

+245
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
ARMGNU ?= aarch64-linux-gnu
2+
3+
COPS = -Wall -nostdlib -nostartfiles -ffreestanding -Iinclude -mgeneral-regs-only
4+
ASMOPS = -Iinclude
5+
6+
BUILD_DIR = build
7+
SRC_DIR = src
8+
9+
all : kernel8.img
10+
11+
clean :
12+
rm -rf $(BUILD_DIR) *.img
13+
14+
$(BUILD_DIR)/%_c.o: $(SRC_DIR)/%.c
15+
mkdir -p $(@D)
16+
$(ARMGNU)-gcc $(COPS) -MMD -c $< -o $@
17+
18+
$(BUILD_DIR)/%_s.o: $(SRC_DIR)/%.S
19+
$(ARMGNU)-gcc $(ASMOPS) -MMD -c $< -o $@
20+
21+
C_FILES = $(wildcard $(SRC_DIR)/*.c)
22+
ASM_FILES = $(wildcard $(SRC_DIR)/*.S)
23+
OBJ_FILES = $(C_FILES:$(SRC_DIR)/%.c=$(BUILD_DIR)/%_c.o)
24+
OBJ_FILES += $(ASM_FILES:$(SRC_DIR)/%.S=$(BUILD_DIR)/%_s.o)
25+
26+
DEP_FILES = $(OBJ_FILES:%.o=%.d)
27+
-include $(DEP_FILES)
28+
29+
kernel8.img: $(SRC_DIR)/linker.ld $(OBJ_FILES)
30+
$(ARMGNU)-ld -T $(SRC_DIR)/linker.ld -o $(BUILD_DIR)/kernel8.elf $(OBJ_FILES)
31+
$(ARMGNU)-objcopy $(BUILD_DIR)/kernel8.elf -O binary kernel8.img
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
docker run --rm -v %cd%:/app -w /app smatyukevich/raspberry-pi-os-builder make %1
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
docker run --rm -v $(pwd):/app -w /app smatyukevich/raspberry-pi-os-builder make $1
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef _MM_H
2+
#define _MM_H
3+
4+
#define PAGE_SHIFT 12
5+
#define TABLE_SHIFT 9
6+
#define SECTION_SHIFT (PAGE_SHIFT + TABLE_SHIFT)
7+
8+
#define PAGE_SIZE (1 << PAGE_SHIFT)
9+
#define SECTION_SIZE (1 << SECTION_SHIFT)
10+
11+
#define LOW_MEMORY (2 * SECTION_SIZE)
12+
13+
#ifndef __ASSEMBLER__
14+
15+
void memzero(unsigned long src, unsigned long n);
16+
17+
#endif
18+
19+
#endif /*_MM_H */
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef _P_BASE_H
2+
#define _P_BASE_H
3+
4+
#define PBASE 0x3F000000
5+
6+
#endif /*_P_BASE_H */
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef _P_GPIO_H
2+
#define _P_GPIO_H
3+
4+
#include "peripherals/base.h"
5+
6+
#define GPFSEL1 (PBASE+0x00200004)
7+
#define GPSET0 (PBASE+0x0020001C)
8+
#define GPCLR0 (PBASE+0x00200028)
9+
#define GPPUD (PBASE+0x00200094)
10+
#define GPPUDCLK0 (PBASE+0x00200098)
11+
12+
#endif /*_P_GPIO_H */
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef _P_UART_PL011_H
2+
#define _P_UART_PL011_H
3+
4+
#include "base.h"
5+
6+
#define PL011_BASE (PBASE+0x00201000)
7+
#define PL011_DR (PL011_BASE+0x00)
8+
#define PL011_RSRECR (PL011_BASE+0x04)
9+
#define PL011_FR (PL011_BASE+0x18)
10+
#define PL011_ILPR (PL011_BASE+0x20)
11+
#define PL011_IBRD (PL011_BASE+0x24)
12+
#define PL011_FBRD (PL011_BASE+0x28)
13+
#define PL011_LCRH (PL011_BASE+0x2c)
14+
#define PL011_CR (PL011_BASE+0x30)
15+
#define PL011_IFLS (PL011_BASE+0x34)
16+
#define PL011_IMSC (PL011_BASE+0x38)
17+
#define PL011_RIS (PL011_BASE+0x3c)
18+
#define PL011_MIS (PL011_BASE+0x40)
19+
#define PL011_ICR (PL011_BASE+0x44)
20+
#define PL011_DMACR (PL011_BASE+0x48)
21+
#define PL011_ITCR (PL011_BASE+0x80)
22+
#define PL011_ITIP (PL011_BASE+0x84)
23+
#define PL011_ITOP (PL011_BASE+0x88)
24+
#define PL011_TDR (PL011_BASE+0x8c)
25+
26+
#endif
27+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef _MINI_UART_H
2+
#define _MINI_UART_H
3+
4+
void uart_init ( void );
5+
char uart_recv ( void );
6+
void uart_send ( char c );
7+
void uart_send_string(char* str);
8+
9+
#endif /*_MINI_UART_H */
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef _BOOT_H
2+
#define _BOOT_H
3+
4+
extern void delay ( unsigned long);
5+
extern void put32 ( unsigned long, unsigned int );
6+
extern unsigned int get32 ( unsigned long );
7+
8+
#endif /*_BOOT_H */
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "mm.h"
2+
3+
.section ".text.boot"
4+
5+
.globl _start
6+
_start:
7+
mrs x0, mpidr_el1
8+
and x0, x0,#0xFF // Check processor id
9+
cbz x0, master // Hang for all non-primary CPU
10+
b proc_hang
11+
12+
proc_hang:
13+
b proc_hang
14+
15+
master:
16+
adr x0, bss_begin
17+
adr x1, bss_end
18+
sub x1, x1, x0
19+
bl memzero
20+
21+
mov sp, #LOW_MEMORY
22+
bl kernel_main
23+
b proc_hang // should never come here

0 commit comments

Comments
 (0)