In the last sections, we discussed the communication protocols in general and then shifted to discussing the Spi in detail. The last discussions were based on the hardware part of the MCU and Spi peripheral but from this section, we will shift our focus to the software implementation.
We have already discussed that any digital circuitry requires binary instructions i.e, we need to provide the machine language(binary) code. Now we can either write our program in assembly language which is more user-friendly than the machine code or we can write in other high-level languages such as C.The assembly code is a low-level code i.e it is much more dependent on the architecture of the processor. Hence the use of a higher-level language such as C is much more efficient as it is portable across all the architectures. There are some cases in which the use of assembly is preferred such as while writing the Reset handlers or accessing the CPU registers, but other than these cases, C is used.
When C is used in programming the embedded systems, it is often referred to as embedded C. It is basically an extension of the C programming language but there are a few differences between conventional C and embedded C such as the I/O functionalities and the resource(memory, processing power) constraints which are there in embedded systems but other than those the languages are quite similar. Moreover, in embedded C we directly program the underlying peripheral configuration registers. Each peripheral is provided with individual registers in the memory and these are used to set the configuration of the peripheral.
So, in embedded programming, we basically program these registers to perform the required operation. All the information about these registers is located in the MCU datasheet provided by the manufacturer and so it is very important for any embedded systems engineer to know how to read the datasheet. These datasheets are the heart and soul of creating firmware/drivers for any MCU and hence the first step for learning embedded systems programming is knowing how to understand a datasheet. The image attached below is a part of the Atmega 32 datasheet and provides the information about various registers for controlling GPIO.
The address of the above register can be found in the datasheet as well. Using it, we can write the data at the specified address with the help of pointers and hence can manipulate the working of GPIO. In practice, there are a number of registers for configuring each peripheral and hence we need a way to create a mapping of software over hardware. This is achieved through the use of structures which can create the exact same pattern of registers in software by the use of variables whose sizes are same as that of the corresponding registers. Then we use register pointers which are then assigned to the starting address of the peripheral and hence we can now control each and every setting of a peripheral.
There are various other techniques which are used to manipulate the hardware and these can be learned by diving deeper into the C Programming language.


Comments
Post a Comment