Skip to content

Commit

Permalink
Lesson 3 - Exercise 2
Browse files Browse the repository at this point in the history
  • Loading branch information
szediwy committed Jan 27, 2021
1 parent 59325fa commit 855c953
Show file tree
Hide file tree
Showing 30 changed files with 1,424 additions and 0 deletions.
34 changes: 34 additions & 0 deletions exercises/lesson03/2/szediwy/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
ARMGNU ?= aarch64-linux-gnu

COPS = -Wall -nostdlib -nostartfiles -ffreestanding -Iinclude
ASMOPS = -Iinclude

BUILD_DIR = build
SRC_DIR = src

.PHONY: clean

all : kernel8.img

clean :
rm -rf $(BUILD_DIR) *.img
@echo "clean: [SUCCESS]"

$(BUILD_DIR)/%_c.o: $(SRC_DIR)/%.c
mkdir -p $(@D)
$(ARMGNU)-gcc $(COPS) -MMD -c $< -o $@

$(BUILD_DIR)/%_s.o: $(SRC_DIR)/%.S
$(ARMGNU)-gcc $(ASMOPS) -MMD -c $< -o $@

C_FILES = $(wildcard $(SRC_DIR)/*.c)
ASM_FILES = $(wildcard $(SRC_DIR)/*.S)
OBJ_FILES = $(C_FILES:$(SRC_DIR)/%.c=$(BUILD_DIR)/%_c.o)
OBJ_FILES += $(ASM_FILES:$(SRC_DIR)/%.S=$(BUILD_DIR)/%_s.o)

DEP_FILES = $(OBJ_FILES:%.o=%.d)
-include $(DEP_FILES)

kernel8.img: $(SRC_DIR)/linker.ld $(OBJ_FILES)
$(ARMGNU)-ld -T $(SRC_DIR)/linker.ld -o $(BUILD_DIR)/kernel8.elf $(OBJ_FILES)
$(ARMGNU)-objcopy $(BUILD_DIR)/kernel8.elf -O binary kernel8.img
3 changes: 3 additions & 0 deletions exercises/lesson03/2/szediwy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Readme

Solved the issue for the Raspberry Pi 4B. The solution has both timer active. The system timer 1 and the local timer.
1 change: 1 addition & 0 deletions exercises/lesson03/2/szediwy/build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker run --rm -v %cd%:/app -w /app smatyukevich/raspberry-pi-os-builder make %1
3 changes: 3 additions & 0 deletions exercises/lesson03/2/szediwy/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

docker run --rm -v $(pwd):/app -w /app smatyukevich/raspberry-pi-os-builder make $1
119 changes: 119 additions & 0 deletions exercises/lesson03/2/szediwy/include/custom_printf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
File: printf.h
Copyright (C) 2004 Kustaa Nyholm
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
This library is really just two files: 'printf.h' and 'printf.c'.
They provide a simple and small (+200 loc) printf functionality to
be used in embedded systems.
I've found them so usefull in debugging that I do not bother with a
debugger at all.
They are distributed in source form, so to use them, just compile them
into your project.
Two printf variants are provided: printf and sprintf.
The formats supported by this implementation are: 'd' 'u' 'c' 's' 'x' 'X'.
Zero padding and field width are also supported.
If the library is compiled with 'PRINTF_SUPPORT_LONG' defined then the
long specifier is also
supported. Note that this will pull in some long math routines (pun intended!)
and thus make your executable noticably longer.
The memory foot print of course depends on the target cpu, compiler and
compiler options, but a rough guestimate (based on a H8S target) is about
1.4 kB for code and some twenty 'int's and 'char's, say 60 bytes of stack space.
Not too bad. Your milage may vary. By hacking the source code you can
get rid of some hunred bytes, I'm sure, but personally I feel the balance of
functionality and flexibility versus code size is close to optimal for
many embedded systems.
To use the printf you need to supply your own character output function,
something like :
void putc ( void* p, char c)
{
while (!SERIAL_PORT_EMPTY) ;
SERIAL_PORT_TX_REGISTER = c;
}
Before you can call printf you need to initialize it to use your
character output function with something like:
init_printf(NULL,putc);
Notice the 'NULL' in 'init_printf' and the parameter 'void* p' in 'putc',
the NULL (or any pointer) you pass into the 'init_printf' will eventually be
passed to your 'putc' routine. This allows you to pass some storage space (or
anything really) to the character output function, if necessary.
This is not often needed but it was implemented like that because it made
implementing the sprintf function so neat (look at the source code).
The code is re-entrant, except for the 'init_printf' function, so it
is safe to call it from interupts too, although this may result in mixed output.
If you rely on re-entrancy, take care that your 'putc' function is re-entrant!
The printf and sprintf functions are actually macros that translate to
'tfp_printf' and 'tfp_sprintf'. This makes it possible
to use them along with 'stdio.h' printf's in a single source file.
You just need to undef the names before you include the 'stdio.h'.
Note that these are not function like macros, so if you have variables
or struct members with these names, things will explode in your face.
Without variadic macros this is the best we can do to wrap these
fucnction. If it is a problem just give up the macros and use the
functions directly or rename them.
For further details see source code.
regs Kusti, 23.10.2004
*/


#ifndef __TFP_PRINTF__
#define __TFP_PRINTF__

#include <stdarg.h>

void init_printf(void* putp,void (*putf) (void*,char));

void tfp_printf(char *fmt, ...);
void tfp_sprintf(char* s,char *fmt, ...);

void tfp_format(void* putp,void (*putf) (void*,char),char *fmt, va_list va);

void test (char c);

#define printf tfp_printf
#define sprintf tfp_sprintf

#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
#define BYTE_TO_BINARY(byte) \
(byte & 0x80 ? '1' : '0'), \
(byte & 0x40 ? '1' : '0'), \
(byte & 0x20 ? '1' : '0'), \
(byte & 0x10 ? '1' : '0'), \
(byte & 0x08 ? '1' : '0'), \
(byte & 0x04 ? '1' : '0'), \
(byte & 0x02 ? '1' : '0'), \
(byte & 0x01 ? '1' : '0')

#endif
26 changes: 26 additions & 0 deletions exercises/lesson03/2/szediwy/include/entry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef _ENTRY_H
#define _ENTRY_H

#define S_FRAME_SIZE 256 // size of all saved registers

#define SYNC_INVALID_EL1t 0
#define IRQ_INVALID_EL1t 1
#define FIQ_INVALID_EL1t 2
#define ERROR_INVALID_EL1t 3

#define SYNC_INVALID_EL1h 4
#define IRQ_INVALID_EL1h 5
#define FIQ_INVALID_EL1h 6
#define ERROR_INVALID_EL1h 7

#define SYNC_INVALID_EL0_64 8
#define IRQ_INVALID_EL0_64 9
#define FIQ_INVALID_EL0_64 10
#define ERROR_INVALID_EL0_64 11

#define SYNC_INVALID_EL0_32 12
#define IRQ_INVALID_EL0_32 13
#define FIQ_INVALID_EL0_32 14
#define ERROR_INVALID_EL0_32 15

#endif
13 changes: 13 additions & 0 deletions exercises/lesson03/2/szediwy/include/irq.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _IRQ_H
#define _IRQ_H

void enable_interrupt_controller( void );

void enable_interrupt(unsigned int irq);
void assign_target(unsigned int irq, unsigned int cpu);

void irq_vector_init( void );
void enable_irq( void );
void disable_irq( void );

#endif /*_IRQ_H */
12 changes: 12 additions & 0 deletions exercises/lesson03/2/szediwy/include/mini_uart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef _MINI_UART_H
#define _MINI_UART_H

void uart_init(void);
char uart_recv(void);
void uart_send(char c);
void uart_send_string(char *str);
void putc(void *p, char c);

void handle_uart_interrupt(void );

#endif /*_MINI_UART_H */
19 changes: 19 additions & 0 deletions exercises/lesson03/2/szediwy/include/mm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef _MM_H
#define _MM_H

#define PAGE_SHIFT 12
#define TABLE_SHIFT 9
#define SECTION_SHIFT (PAGE_SHIFT + TABLE_SHIFT)

#define PAGE_SIZE (1 << PAGE_SHIFT)
#define SECTION_SIZE (1 << SECTION_SHIFT)

#define LOW_MEMORY (2 * SECTION_SIZE)

#ifndef __ASSEMBLER__

void memzero(unsigned long src, unsigned long n);

#endif

#endif /*_MM_H */
22 changes: 22 additions & 0 deletions exercises/lesson03/2/szediwy/include/peripherals/base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef _P_BASE_H
#define _P_BASE_H

// #define PBASE 0x3F000000
// So a peripheral described in this document as being at legacy address 0x7Enn_nnnn is available in the 35-bit address
// space at 0x4_7Enn_nnnn, and visible to the ARM at 0x0_FEnn_nnnn if Low Peripheral mode is enabled.
// 0x7E000000 (legacy) -> 0x4_7E00_0000 (35-bit) -> 0x0_FE00_0000 (low peripheral)
#define PBASE 0xFE000000

// The base address of the GIC-400 is 0x4c0040000. Note that, unlike other peripheral addresses in this document, this is an
// ARM-only address and not a legacy master address. If Low Peripheral mode is enabled this base address becomes
// 0xff840000.
// The GIC-400 is configured with "NUM_CPUS=4" and "NUM_SPIS=192". For full register details, please refer to the ARM
// GIC-400 documentation on the ARM Developer website.
#define GIC_BASE 0xFF840000

#define ARM_LOCAL_BASE 0xFF800000

// The ARMC register base address is 0x7e00b000 -> 0x4_7E00B000 -> 0x0FE00B000
#define ARMC_BASE 0x0FE00B000

#endif /*_P_BASE_H */
13 changes: 13 additions & 0 deletions exercises/lesson03/2/szediwy/include/peripherals/gpio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _P_GPIO_H
#define _P_GPIO_H

#include "peripherals/base.h"

#define GPFSEL1 (PBASE+0x00200004)
// #define GPSET0 (PBASE+0x0020001C)
// #define GPCLR0 (PBASE+0x00200028)
// #define GPPUD (PBASE+0x00200094)
// #define GPPUDCLK0 (PBASE+0x00200098)
#define GPIO_PUP_PDN_CNTRL_REG0 (PBASE+0x002000E4)

#endif /*_P_GPIO_H */
52 changes: 52 additions & 0 deletions exercises/lesson03/2/szediwy/include/peripherals/irq.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#ifndef _P_IRQ_H
#define _P_IRQ_H

#include "peripherals/base.h"



#define GIC_DIST_BASE (GIC_BASE+0x00001000)
#define GIC_CPU_BASE (GIC_BASE+0x00002000)



// For interrupt ID m, when DIV and MOD are the integer division and modulo operations:
// • the corresponding GICD_ISENABLER number, n, is given by n = m DIV 32
// • the offset of the required GICD_ISENABLER is (0x100 + (4*n))
// • the bit number of the required Set-enable bit in this register is m MOD 32.
//
// VC Timer 1 = 1 => 97 / 32 = 3 => offset 0x10C
//
#define GIC_ENABLE_IRQ_BASE (GIC_DIST_BASE+0x00000100)
#define GIC_ENABLE_IRQ_3 (GIC_DIST_BASE+0x0000010C)

#define GICC_IAR (GIC_CPU_BASE+0x0000000C)
#define GICC_EOIR (GIC_CPU_BASE+0x00000010)

// For interrupt ID m, when DIV and MOD are the integer division and modulo operations:
// • the corresponding GICD_ITARGETSRn number, n, is given by n = m DIV 4
// • the offset of the required GICD_ITARGETSR is (0x800 + (4*n))
// • the byte offset of the required Priority field in this register is m MOD 4, where:
// — byte offset 0 refers to register bits [7:0]
// — byte offset 1 refers to register bits [15:8]
// — byte offset 2 refers to register bits [23:16]
// — byte offset 3 refers to register bits [31:24].
#define GIC_IRQ_TARGET_BASE (GIC_DIST_BASE+0x00000800)

// 97 / 4 => n = 24
// 0x800 + 4*24
// 97 % 4 => m = 1
// byte offset = 1<<8
#define GIC_IRQ_TARGET_24 (GIC_DIST_BASE+0x00000860)

#define LOCAL_TIMER_IRQ (0x35) // Local timer IRQ

#define SYSTEM_TIMER_IRQ_0 (0x60) // VC IRQ 0
#define SYSTEM_TIMER_IRQ_1 (0x61) // VC IRQ 1
#define SYSTEM_TIMER_IRQ_2 (0x62) // VC IRQ 2
#define SYSTEM_TIMER_IRQ_3 (0x63) // VC IRQ 3

//
#define UART_IRQ (0x99) // VC IRQ 57

#endif /*_P_IRQ_H */
Loading

0 comments on commit 855c953

Please sign in to comment.