Skip to content

Commit 811deae

Browse files
Merge pull request #224 from szediwy/master
Lesson 2 - Exercise 2 & Lesson 3 - Exercise 1,2
2 parents 6e5538a + 855c953 commit 811deae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+3875
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
ARMGNU ?= aarch64-linux-gnu
2+
3+
COPS = -Wall -nostdlib -nostartfiles -ffreestanding -Iinclude
4+
ASMOPS = -Iinclude
5+
6+
BUILD_DIR = build
7+
SRC_DIR = src
8+
9+
.PHONY: clean
10+
11+
all : kernel8.img
12+
13+
clean :
14+
rm -rf $(BUILD_DIR) *.img
15+
@echo "clean: [SUCCESS]"
16+
17+
$(BUILD_DIR)/%_c.o: $(SRC_DIR)/%.c
18+
mkdir -p $(@D)
19+
$(ARMGNU)-gcc $(COPS) -MMD -c $< -o $@
20+
21+
$(BUILD_DIR)/%_s.o: $(SRC_DIR)/%.S
22+
$(ARMGNU)-gcc $(ASMOPS) -MMD -c $< -o $@
23+
24+
C_FILES = $(wildcard $(SRC_DIR)/*.c)
25+
ASM_FILES = $(wildcard $(SRC_DIR)/*.S)
26+
OBJ_FILES = $(C_FILES:$(SRC_DIR)/%.c=$(BUILD_DIR)/%_c.o)
27+
OBJ_FILES += $(ASM_FILES:$(SRC_DIR)/%.S=$(BUILD_DIR)/%_s.o)
28+
29+
DEP_FILES = $(OBJ_FILES:%.o=%.d)
30+
-include $(DEP_FILES)
31+
32+
kernel8.img: $(SRC_DIR)/linker.ld $(OBJ_FILES)
33+
$(ARMGNU)-ld -T $(SRC_DIR)/linker.ld -o $(BUILD_DIR)/kernel8.elf $(OBJ_FILES)
34+
$(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: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
File: printf.h
3+
4+
Copyright (C) 2004 Kustaa Nyholm
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14+
See the GNU Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19+
20+
This library is really just two files: 'printf.h' and 'printf.c'.
21+
22+
They provide a simple and small (+200 loc) printf functionality to
23+
be used in embedded systems.
24+
25+
I've found them so usefull in debugging that I do not bother with a
26+
debugger at all.
27+
28+
They are distributed in source form, so to use them, just compile them
29+
into your project.
30+
31+
Two printf variants are provided: printf and sprintf.
32+
33+
The formats supported by this implementation are: 'd' 'u' 'c' 's' 'x' 'X'.
34+
35+
Zero padding and field width are also supported.
36+
37+
If the library is compiled with 'PRINTF_SUPPORT_LONG' defined then the
38+
long specifier is also
39+
supported. Note that this will pull in some long math routines (pun intended!)
40+
and thus make your executable noticably longer.
41+
42+
The memory foot print of course depends on the target cpu, compiler and
43+
compiler options, but a rough guestimate (based on a H8S target) is about
44+
1.4 kB for code and some twenty 'int's and 'char's, say 60 bytes of stack space.
45+
Not too bad. Your milage may vary. By hacking the source code you can
46+
get rid of some hunred bytes, I'm sure, but personally I feel the balance of
47+
functionality and flexibility versus code size is close to optimal for
48+
many embedded systems.
49+
50+
To use the printf you need to supply your own character output function,
51+
something like :
52+
53+
void putc ( void* p, char c)
54+
{
55+
while (!SERIAL_PORT_EMPTY) ;
56+
SERIAL_PORT_TX_REGISTER = c;
57+
}
58+
59+
Before you can call printf you need to initialize it to use your
60+
character output function with something like:
61+
62+
init_printf(NULL,putc);
63+
64+
Notice the 'NULL' in 'init_printf' and the parameter 'void* p' in 'putc',
65+
the NULL (or any pointer) you pass into the 'init_printf' will eventually be
66+
passed to your 'putc' routine. This allows you to pass some storage space (or
67+
anything really) to the character output function, if necessary.
68+
This is not often needed but it was implemented like that because it made
69+
implementing the sprintf function so neat (look at the source code).
70+
71+
The code is re-entrant, except for the 'init_printf' function, so it
72+
is safe to call it from interupts too, although this may result in mixed output.
73+
If you rely on re-entrancy, take care that your 'putc' function is re-entrant!
74+
75+
The printf and sprintf functions are actually macros that translate to
76+
'tfp_printf' and 'tfp_sprintf'. This makes it possible
77+
to use them along with 'stdio.h' printf's in a single source file.
78+
You just need to undef the names before you include the 'stdio.h'.
79+
Note that these are not function like macros, so if you have variables
80+
or struct members with these names, things will explode in your face.
81+
Without variadic macros this is the best we can do to wrap these
82+
fucnction. If it is a problem just give up the macros and use the
83+
functions directly or rename them.
84+
85+
For further details see source code.
86+
87+
regs Kusti, 23.10.2004
88+
*/
89+
90+
91+
#ifndef __TFP_PRINTF__
92+
#define __TFP_PRINTF__
93+
94+
#include <stdarg.h>
95+
96+
void init_printf(void* putp,void (*putf) (void*,char));
97+
98+
void tfp_printf(char *fmt, ...);
99+
void tfp_sprintf(char* s,char *fmt, ...);
100+
101+
void tfp_format(void* putp,void (*putf) (void*,char),char *fmt, va_list va);
102+
103+
void test (char c);
104+
105+
#define printf tfp_printf
106+
#define sprintf tfp_sprintf
107+
108+
#endif
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef _ENTRY_H
2+
#define _ENTRY_H
3+
4+
#define S_FRAME_SIZE 256 // size of all saved registers
5+
6+
#define SYNC_INVALID_EL1t 0
7+
#define IRQ_INVALID_EL1t 1
8+
#define FIQ_INVALID_EL1t 2
9+
#define ERROR_INVALID_EL1t 3
10+
11+
#define SYNC_INVALID_EL1h 4
12+
#define IRQ_INVALID_EL1h 5
13+
#define FIQ_INVALID_EL1h 6
14+
#define ERROR_INVALID_EL1h 7
15+
16+
#define SYNC_INVALID_EL0_64 8
17+
#define IRQ_INVALID_EL0_64 9
18+
#define FIQ_INVALID_EL0_64 10
19+
#define ERROR_INVALID_EL0_64 11
20+
21+
#define SYNC_INVALID_EL0_32 12
22+
#define IRQ_INVALID_EL0_32 13
23+
#define FIQ_INVALID_EL0_32 14
24+
#define ERROR_INVALID_EL0_32 15
25+
26+
#endif
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef _IRQ_H
2+
#define _IRQ_H
3+
4+
void enable_interrupt_controller( void );
5+
6+
void irq_vector_init( void );
7+
void enable_irq( void );
8+
void disable_irq( void );
9+
10+
#endif /*_IRQ_H */
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
void putc(void *p, char c);
9+
10+
#endif /*_MINI_UART_H */
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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef _P_BASE_H
2+
#define _P_BASE_H
3+
4+
// #define PBASE 0x3F000000
5+
// So a peripheral described in this document as being at legacy address 0x7Enn_nnnn is available in the 35-bit address
6+
// space at 0x4_7Enn_nnnn, and visible to the ARM at 0x0_FEnn_nnnn if Low Peripheral mode is enabled.
7+
// 0x7E000000 (legacy) -> 0x4_7E00_0000 (35-bit) -> 0x0_FE00_0000 (low peripheral)
8+
#define PBASE 0xFE000000
9+
10+
// The base address of the GIC-400 is 0x4c0040000. Note that, unlike other peripheral addresses in this document, this is an
11+
// ARM-only address and not a legacy master address. If Low Peripheral mode is enabled this base address becomes
12+
// 0xff840000.
13+
// The GIC-400 is configured with "NUM_CPUS=4" and "NUM_SPIS=192". For full register details, please refer to the ARM
14+
// GIC-400 documentation on the ARM Developer website.
15+
#define GIC_BASE 0xFF840000
16+
17+
// The ARMC register base address is 0x7e00b000 -> 0x4_7E00B000 -> 0x0FE00B000
18+
#define ARMC_BASE 0x0FE00B000
19+
20+
#endif /*_P_BASE_H */
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
#define GPIO_PUP_PDN_CNTRL_REG0 (PBASE+0x002000E4)
12+
#define UART0_DR (PBASE+0x00201000)
13+
#define UART0_FR (PBASE+0x00201018)
14+
#define UART0_IBRD (PBASE+0x00201024)
15+
#define UART0_FBRD (PBASE+0x00201028)
16+
#define UART0_LCRH (PBASE+0x0020102C)
17+
#define UART0_CR (PBASE+0x00201030)
18+
#define UART0_IMSC (PBASE+0x00201038)
19+
20+
#endif /*_P_GPIO_H */

0 commit comments

Comments
 (0)