This repository gives a few example functions in C and assembly to show how to implement a delay function and configure the System timer(SysTick).
- Medium knowledge in assembly for arm cortex m4
- Medium knowledge in C language
- Basic knowledge in ARM architecture
The systick is a very useful peripheral, because it's peripheral inside the microcontroller with low latency, we can perform several things using systick timer but there are two that comes on my mind right now and they are RTOS timer functions and famous delay() in Arduino, in this tutorial, we will focus on how to implement the delay() in TM4C1294NCPDT(ARM Cortex M4) but we will detail more about the process how to build from the scratch.
Let's start from the EQU directives in assembly code, observe that all the assembly files needed are into Assembly folder and all the implementations in C are into C folder, first of all I declared some directives to improve the readability of the code and do some operations too.
The ARM CORTEX M4 has some interesting features(into the processor), by default these features are mapped at address 0xE000E000 to 0xE000EFFF, this space is commonly called space of Core Peripherals Addresses or System Control Space in the following image, we can visualize better how these core peripherals are mapped.
and to TM4C1294NCPDT we have the following addresses, the unused spaces are reserved by the chip manufacturer.
Now that we have an overview how the peripherals are mapped, so let's go to the code and see the implementation and how it works.
Note that we start charging the base address of SysTick because if we need to improve the readability of the code we can declare masks before start the code and use OR mask, XOR mask, AND mask, etc(we will discuss a little bit more about this later), we also know that each register has a specific address in memory and these registers has 4 bytes of space between them in most of the case they follow this standard, for example, if we see the MPU registers in the image below we can note that the next registers are 4 bytes spaced until finish the set of registers
In the first part of the code we are setting up the base address of Core Peripherals, it is necessary because to access a specific register we just need to do the sum: BASE ADDRESS + OFFSET(wanted register). The LDR r0, =SysTick_BASE instruction holds the base address, and STR r1, [r0, #SysTick_CTRL] do the sum BASE ADDRESS + OFFSET and store it with the value into the R1 that is 0x04, initially what we want in the register STCTRL is filling it with zeros and set the system clock as our clock source exactly such as the image below.