The original blog is in Turkish and in www.dincay.blogspot.com. This is the English translation of it.

30 Ekim 2010 Cumartesi

Serial commnication with Ftdi VCP Driver

I had performed serial communication with libftdi. I did it again with ftdi vcp drivers instead of libftdi.

The reason that I wanted to use vcp drivers is I want to use other programming languages than C. I thought that libftdi would give me greater flexibility. But I forgot that nearly all programming languages can communicate with serial port. Instead of a specific library like libftdi, working with native serial port libraties is better I think.

I downloaded the vcp drivers from ftdi's web site. When I gave make command, driver gave me this error

cc1: fatal error: /lib/modules/2.6.35-22-generic/build/include/linux/modversions.h: No such file or directory

After a little googling, I understood that modversion.h is found in older linux versions (I am using ubuntu 10.04 now). In http://ftdi-usb-sio.sourceforge.net/#sec6 it says you can test your vcp drivers if they installed correctly by these ways. When I did the tests, I understood that driver was installed succesfully : Probably ubuntu has the drivers in it already.

I wrote a small program in python to check the vcp drivers. Python is already integrated to ubuntu. But I needed to install pySerial: python serial communication module. I downloaded the module from http://pypi.python.org/pypi/pyserial. In downloded folder I give

sudo python setup.py install

command and setup was completed. Very easy.

Here is the program I used:

#python serial read-write

#Dincay Akcoren
import serial

#baud rate = 19200, timeout = 10 sec
ser = serial.Serial('/dev/ttyUSB0', 19200, timeout=10)
ser.write("5")
x = ser.read()
print(x)
ser.close()


Now I can communicate with my DE0 from pc. I can send commands and receive responses. Next I will write a command decoder/encoder that eases giving/receiving commands.

24 Ekim 2010 Pazar

Altera DE0 – Programming the Serial Configuration Chip (EPCS)


DE0 has a special 4 kb serial EEPROM. It's name is epsc4 in Altera's documents. When the board is powered, if the programming switch is in Run position, fpga loads the configuration data stored in epcs. I don't want to see the test program every time I powered the board, so I decided to re-program epcs according to my needs.

There is a section in DE0 user manual about programming the epcs (p.20-21) but the way described is not complete. Epsc won't be programmed, if only the instructions in user manual is followed. This aim of this article is to provide a complete description about how to program the epsc on DE0.

The configuration file that is loaded to fpga has .sof extension. It is .bit in Xilinx. This .sof file is for fpga only, it needs to be changed for epsc. To do this I opened

  • File → Convert Programming Files

In opened window I changed these

  • Programming file type -> Programmer Object File (.pof)
  • Configuration Device -> ECPS4
  • Input files to convert -> Click "SOF data” -> Add File -> select .sof file to be changed

After all these steps the window will be like this
Create the file by clicking Generate.


Second step is load this newly created .pof file to epcs. In order to do this I opened the Programmer in Quartus. I performed these steps in Programmer window.
  • Hardware Setup -> USB Blaster
  • Mode -> Active Serial Programming -> 'Yes'
  • Choose “Enable real-time ISP to allow background programming (for Max2 devices)”
  • Add Device -> EPCS4 -> OK
  • Click the new symbol -> Change file -> Choose .pof file
  • Put a tick in Program/Configure option
  • Start
My window looks like this just before I clicked start.

After following all these steps, I could program epcs. Every time the board is powered, the clock application is displayed.

7 Ekim 2010 Perşembe

PC – FPGA Communication with RS232 – 2

There is a need for software program in addition to the logic implemented in fpga. There are two libraries that can be used to control ftdi chips: D2XX library and libftdi library. The first is official one, it has good documentation but it is no open-source. The latter one is open-source but it has poor documentation. I couldn't install the D2XX library in Ubuntu. However libftdi is listed in repositories, so installation is very easy with ant package manages (aptitude, synaptic etc...). Maybe the only website about libftdi is its documentation web site http://www.intra2net.com/en/developer/libftdi/documentation/index.html. Also there are a few samples in /usr/share/doc/libftdi-dev/examples folder. Among these examples, simple.c and serial_read.c helped me a lot.


Since the software is a trial only, I didn't try to write a meaningful thing. Software first reads data entered from keyboard and send to fpga then waits data from fpga in a loop. When data is received, it echoes data to screen.


Software can be divided into 3 parts: initialization, write and read. Since I am writing about software, all directions are named with respect to software i.e: write is write to fpga.


I took the initialization part from sample.c example. The communication speed, and data format is defined here. Since the fpga logic operates at 19200 baud with 1+8+1 bit data, software is adjusted with the same settings.


Write part is simple. Fpga is always ready for a received data to fpga. Only thing to do is call ftdi_write_data function.

Reading is the hardest part. It requires some knowledge about ftdi chips and software. There is only one line of explanation in documentation. serial_read.c example helped me a lot. Read function doesn't wait for data and returns 0 if no data is available to read. In fact, read function returns amount of read bytes. Therefore I called this function in a while loop until its output is >0.

One thing I noticed during testing is, the chip has a buffer in it. If more than 1 byte of data is send, the unread bytes are stored in chip and next time a data is read from the computer, these stored data are send. I noticed this when the send button didn't have a debounce filter.

I will try to do the same thing with VCP (Virtual COM Port) drivers at the next time.


Program codes can be downloaded from here.

6 Ekim 2010 Çarşamba

PC – FPGA Communication with RS232 – 1


I managed to transfer data between fpga and PC in both directions with rs232 protocol.

I wrote a software for this application. It asks a number from the user and send it to fpga. Fpga displays this number in 3rd and 4th digits of its 7 segment display. After that, software waits for a data from fpga. In order to send data from fpga, button 1 should be pressed. When the button is pressed the number coded with the switches is send to PC. The good point abut this system is it uses USB port other than old rs232 port. Ftdi chip converts the usb data to appropriate rs232 data. There is no need a separate program to control the ftdi chip. There is a library for it. By calling appropriate functions in a c program, I can control ftdi chip as I want. This gives a great flexibility to develop an application. I had made a game by using ftdi library before. It can be reached from that link.

The verilog code consists of 3 main parts: Baud generator, transmitter and receiver. I used baud rate of 19200. Since there is no separate clock line in rs232, baud generator creates 19200x16 Hz pulses to precisely adjust the sampling point. Transmitter and receiver counts the pulses. Each bit lasts 16 pulses. So transmitter advances it output in each 16 pulses after start signal is received. Receiver counts to 8 when detects a data is started to send to sample the data in middle of bit. I copy the algorithm from the book but the coding is mine.

This diagram shows the main blocks and their connections inside the fpga.

There are RS ffs after transmitter and receiver block. Most of the time rs232 module will be a peripheral for another module. Therefore it requires some flags for easy operation. Two control signals newdata and busy are generated inside the rs232 module with these 2 RS ffs.

I will explain the software in my next entry.

Program codes can be downloaded from here.

27 Eylül 2010 Pazartesi

Clock


I made a simple clock application in DE0. It shows hours, minutes and seconds. I use counters for each. They are operating with 50MHz clock. I use a 1 Hz pulse generator. It creates a single pulse every 1 second. Binary counter output is converted to BCD by sing add-to-6 method. I used a readily available code from internet. Displaying BCD data in 7 segment displays is a simple work. In DE0, all of the pins of 4 digit 7 segment leds are connected to the separate pins of the fpga i.e 4x7=28 pins are used for 7 segment leds, so there is no need to do time multiplexing.
Some pictures:

This video shows the operation of the clock. It also serves as a user-guide.

Program codes can be downloaded from here.