Exporting Functions in PSLab Firmware

Computer programs consist of several hundreds line of code. Smaller programs contain around few hundreds while larger programs like PSLab firmware contains lines of code expanding over 2000 lines. The major drawback with a code with lots of lines is the difficulty to debug and hardship for a new developer to understand the code. As a solution modularization techniques can be used to have the long code broken down to small set of codes. In C programming this can be achieved using different .c files and relevant .h(header) files. The task is tricky as the main code contains a lot of interrelated functions and variables. Lots of errors were encountered when the large code in pslab-firmware being modularized into application specific files. In a scenario like this, global variables or static variables were a much of a help. Global Variables in C A global variable in C is a variable whose scope spans wide across the entire program. Updation to the variable will be reflected everywhere it is used. extern TYPE VARIABLE = VALUE; Static Variables in C This type of variables preserve their content regardless of the scope they are in. static TYPE VARIABLE = VALUE; Both the variables preserve their values but the memory usage is different depending on the implementation. This can be explained using a simple example. Suppose a variable is required in different C files and it is defined in one of the header files as a local variable. The header file is then added to several other c files. When the program is compiled the compiler will create several copies of the same variable which will throw a compilation error as variable is already declared. In PSLab firmware, a variable or a method from one library has a higher probability of it being used in another one or many libraries. This issue can be addressed in two different ways. They are by using static variables or global variables. Depending on the selection, the implementation is different. The first implementation is using static variables. This type of variables at the time of compilation, create different copies of himself in different c files. Due to the fact that C language treats every C file as a separate program, if we include a header file with static variables in two c files, it will create two copies of the same variable. This will avoid the error messages with variables redeclared. Even though this fixes the issue in firmware, the memory allocation will be of a wastage. This is the first implementation used in PSLab firmware. The memory usage was very high due to duplicate variables taking much memory than they should take. This lead to the second implementation. first_header.h #ifndef FIRST_HEADER_H #define FIRST_HEADER_H static int var_1; #endif /* FIRST_HEADER_H */ second_header.h #ifndef SECOND_HEADER_H #define SECOND_HEADER_H static int var_1; #endif /* SECOND_HEADER_H */ first_header.c #include <stdio.h> #include "first_header.h" #include "second_header.h" int main() { var_1 = 10; printf("%d", var_1); } The next implementation uses global variables. This type of…

Continue ReadingExporting Functions in PSLab Firmware

How to Read PIC Data-Sheet and Add a New Functionality to PSLab Firmware

Reading data-sheets is not a fun task. Going through tens of hundreds of pages with numerical, mathematical and scientific data is not fun reading. This blog post attempts to simplify reading the available data-sheets related to PIC24 micro-controller used in the PSLab device to help reader with implementing a new feature in PSLab firmware. There are many features available in the PSLab device, such as; UART, SPI, I2C, ADC and Basic I/O reading. The "basic" implementation techniques do not vary much from one feature to other. That being stated this blog will carry out the basic implementation techniques one should follow and basic knowledge on PIC micro-controller programming to save himself from the trouble going through the 500+ pages in PIC data-sheets. PIC Basics: Before go into implementation there are few facts one should know about PIC programming. - In the micro-controller values are saved in a memory block known as Registers. The values saved in these registers are volatile as they are all set to 0 regardless the value they were assigned when the power is off. - Micro-controller configurations are made by setting values to these registers. Even turning on and off a whole feature like UART in PSLab device can be done using setting 0 to UARTEN register bit. - When it comes to I/O ports, there are two different types of registers called TRIS and LAT/PORT. By setting 1 to TRIS ports will make the relevant pin an input pin. Setting it to 0 will make it an output pin. Easy way to remember this is think 1 as I in input and 0 as O in output. In UART implementation of PSLab, pin RP40 is set as an input pin to receive the data stream and pin RP39 is set as an output pin to send the data stream out. These settings are made using TRIS port settings. PORT registers save the value received by the relevant input pin attached to it. The above figure extracted from mikroe learning materials, illustrates different stages an I/O pin can handle. As an extra point, ANSEL register makes the pin support digital signals or analog signals as per user requirements. - In PIC, some registers such as PORT, TRIS and registers with similar functionalities are combined together. To access the value of each individual register can be done using dot notation. Assume the program requires to access the 8th register in TRISB register set. Note that the registers are indexed from zero. This implies that the 8th register will have the index 8 in the register sub-set. The following syntax is used to access the register; TRISBbits.TRISB7   The above points cover the basic knowledge one should have when developing firmware to PSLab device. How to implement a feature like UART in PSLab firmware? The first thing to know when implementing a new feature is that the developer needs to be familiar with the relevant hardware protocols. As an example, to implement UART; relevant protocol is RS232. If…

Continue ReadingHow to Read PIC Data-Sheet and Add a New Functionality to PSLab Firmware