Mario.Tapilouw

Tuesday, June 14, 2016

Playing with SoC!

I'm becoming Hardware and Software programmer now. I am developing System on Chip software running on ARM processor. I installed Linux on the board and write the program on it. Since the chip is Altera, I have to use Altera Quartus for developing the FPGA software. I am using evaluation license for testing my software at the moment. Here is the board that I am using.



The chip is Cyclone V SoC. The FPGA core is Cyclone V with 110 LEs and the ARM processor is Dual Core ARM Cortex A9. The board has 2 GB of memory, 1 GB in the FPGA side and 1 GB in the ARM side.

The first thing to be prepared is to install these softwares:
1. Altera Quartus II, the current version is 16.0. This software is mainly for developing the hardware (FPGA).
2. Altera SoC Embedded Design Suite for developing the software (ARM). This software also needs a license from ARM if we would like to enable software debugging using ARM's compiler which is very fancy to use. I use 30 days evaluation version. Then I use GNU's compiler instead of the ARM compiler.

I got a lot of resources mainly from rocketboards.org for setting up the board until it can run Linux and I can write my software for it.

In order to build the software, we have to make a Makefile. An example of working Makefile is as following:

1:  #  
2:  ALT_DEVICE_FAMILY ?= soc_cv_av  
3:  TARGET = ccs_hps  
4:  #  
5:  CROSS_COMPILE = arm-linux-gnueabihf-  
6:  CFLAGS = -static -g -Wall -I${SOCEDS_DEST_ROOT}/ip/altera/hps/altera_hps/hwlib/include -Ilcdlib -Dsoc_cv_av   
7:  LDFLAGS = -g -Wall -Llcdlib  
8:  CC = $(CROSS_COMPILE)gcc  
9:  ARCH= arm  
10:  build: $(TARGET)  
11:  $(TARGET): ccshps.o lcdlib/terasic_lib.o lcdlib/LCD_Lib.o lcdlib/LCD_Driver.o lcdlib/LCD_Hw.o lcdlib/lcd_graphic.o lcdlib/font.o  
12:      $(CC) $(LDFLAGS)  $^ -o $@  -lrt -lm -lpthread  
13:  %.o : %.c  
14:      $(CC) $(CFLAGS) -c $< -o $@  
15:  .PHONY: clean  
16:  clean:  
17:      rm -f $(TARGET) *.a *.o *~ *.bmp  

I'm still a newbie with this board. When I start using this board I felt lost and didn't know what to do first. So if you are a newbie as well and trying to find a direction for using this board, feel free to ask me.

Happy Coding!

Labels: , , , , , ,

Monday, February 01, 2016

Going into Hardware

Got something new in my projects now. I'm going to play with FPGA again!

Wednesday, December 23, 2015

Raspberry Pi

I've been playing with Raspberry Pi for a while, it's quite fun. Once we know how to program it well, then things are much more easier. For me, I use C as my language, it supports C++ but it needs c++ library to be installed, C is enough powerful for my application. So, here's what I have done so far:

  1. I/O, I use BCM2835 library (http://www.airspayce.com/mikem/bcm2835/) for the GPIO and SPI. This library is quite straightforward, easy to use, and understand. About this library, only the pinout assignment a little bit tricky, it seems that the pinouts for Raspberry Pi v2 is not usable, I have to use RPI_BPLUS_GPIO_J8_XX for pins more than 26, when I tried to use RPI_V2_GPIO_P1_XX I got a compilation error showing that it is not defined. When using this library, make sure to add "-lbcm2835" for compiling your program.
  2. Threads. In most of my program, I always use threads for doing the task because I don't want to have one task blocking the other tasks. If the tasks can be performed separately, then it must be run under threads. Critical sections must be used for making all the threads safe. For thread, I use pthread library. Make sure to add "-lpthread" for compiling your program.
  3. Sockets. For socket, I use standard unix socket programming, socket transfer is run in a thread with a state machine for controlling the connection state. This is quite straightforward. 
Unfortunately, at the moment I don't have any simple application that I could share here. If you have any difficulties just feel free to ask, I might be able to give you a clue. 

Happy Coding!:)


Labels: ,

Tuesday, December 22, 2015

Debugging Multi-Thread Application

Most of the application I develop are running more than one thread with different task for each thread. It might sound ok if the threads are running properly, all the critical sections are not dead locked, and memory are well controlled.

But, sometimes due to deadlines, we forget some parts of the codes and it leads to application crash, memory leak, and the most difficult part is data race problem. These are sorted based on difficulty in debugging.

These several months, I've been struggling hard to read others' code, interpret what they are trying to achieve with the code, how the threads work, how do they synchronize, and how to add another feature to the existing program. And here because it's almost the end of the year, I want to summarize what I learned so far this year.

  1. Application crash is the easiest to debug among all of the three problems I mentioned above. This can be traced quite easily by setting debug points on object creations, every function calls, and object destructions. Usually the error messages gives a clue on what to debug, it takes experience to know what the problem is from the error messages. The more mistakes you make, the more experience you have in interpreting error codes :), but this is not a justification for making mistakes.
  2. Memory leaks is more difficult when it comes to multi threaded applications because the problem might spread with the thread instantiation. The only way to debug this is by checking the source code line by line, making sure that there's always a delete or delete [] for every new or new[].
  3. The data race can be traced by checking all the critical sections and writing a log file for every threads. This is difficult to check because the more threads in the software the more complicated the debugging process is. It become more complicated when the thread is dealing with arrays. Some compilers are not very good in debugging multi threaded application, because the application itself might crash. So, what I did is writing the algorithm into dll, then write some debugging string then use another software to catch the debug strings. In this way, the main threads are not interrupted during debugging and it's much easier to debug the software. The software I am using is called DebugView (https://technet.microsoft.com/en-us/sysinternals/debugview.aspx). It's very convenient to use in debugging algorithm, especially when it is run in thread.

Labels: , , , , ,

Monday, November 24, 2014

Mixing OpenGL and OpenCV

Here's the idea, I want to combine 3D image drawn using OpenGL and adding some information into it. So what I have to do is to draw what I need into OpenGL, capture it, then draw it into bitmap, and then add some information on the bitmap.

There are so many information about drawing 3D surfaces using OpenGL on the web so I don't want to discuss it here. After the 3D surface is drawn, the window can be captured using glReadBuffer() and glReadPixels() functions, if you have no idea about how to use those functions, here's a snippets:

 // capture window  
   unsigned char * image;  
     
   image = new unsigned char[640*640*3];  
     
   glPixelStorei(GL_PACK_ALIGNMENT, 1);  
   glReadBuffer(GL_FRONT_RIGHT);  
   glReadPixels(0, 0, 640, 640, GL_RGB, GL_UNSIGNED_BYTE, image);  
     
   IplImage *imagecv = cvCreateImage(cvSize(800, 640), IPL_DEPTH_8U, 3);  
     
   imagecv->origin = 0;  
     
   for(int row=0;row<640;row++)  
   {  
     for(int col=0;col<800;col++)  
     {  
       CvScalar pixVal;  
         
       if(col<640)  
       {  
         pixVal.val[2] = image[3*row*640+3*col+0];  
         pixVal.val[1] = image[3*row*640+3*col+1];  
         pixVal.val[0] = image[3*row*640+3*col+2];  
       }  
       else  
       {  
         pixVal.val[2] = 255;  
         pixVal.val[1] = 255;  
         pixVal.val[0] = 255;  
       }  
         
       cvSet2D(imagecv, row, col, pixVal);  
     }  
   }  
   

Basically what we need is an unsigned char array for storing the pixel values of the window (window size is 640x640). Then the rest are manipulation of the image.

Here's an example of 3D window captured from OpenGL, the data is a plane circular surface tilted at certain angle:

And here's an example of the image after the adding some information onto it:




Sunday, November 23, 2014

XCoding..

So I start XCoding. The first impression is that the code editor is fast and very intelligent. If we don't make all the configuration right, it won't even compile. I found color scheme that looks pretty:


I tried to code some GLUT and OpenGL and found out that some functions have been deprecated. But I still can use them anyway, so there's no problem. Here's what I made at the first attempt, still have many things to improve but so far I like it.


I'm still using C++, not the Objective C. Have to squeeze some time learning Objective C and make a better UI.



Sunday, November 09, 2014

Coding in Mac!

Yeah! After a year I finally start coding in Mac. Just for my spare time because I am not working with Mac in the office. So far what I do is just trying simple things that I might need in my real program. This is a practice to make a platform independent code, maintaining portability across compilers.

So, what I have done so far? I have installed OpenCV 2.4.9 in my Mac, many bloggers have already posted the steps and thanks to that I have managed to install and run it on my Mac, Yeaaah!!

Another library that I have installed is fftw, this is a cool library which can be used almost everywhere, so I will make use of it.

The code is similar with those coding in Windows, but there's one thing which is different, the directory naming and environment settings. Let's see what I can do in the future.

I'll squeeze some time to post some exciting blog entries about this...