Monday, 2 February 2015

VGA Output from an FPGA

So after the simple 8 bit counter on the fpga I decided to get started with generating VGA output from my FPGA. Because that seemed to be a reasonably simple yet really cool looking project. Initially I thought I'd go the cheap way and solder up my own VGA circuit. But I just ended up wasting a lot of time and effort to get a really substandard circuit that would die on me all the time. So I got myself one of these VGA modules from the Numato Labs website.

First a bit about VGA signals. VGA was originally intended to work with CRT monitors. So the standard was built around the working of a CRT monitor. These type of monitors use a Cathode Ray Tube(CRT). Inside every CRT is an electron gun that produces a beam of really fast electrons. Electric fields are used to deflect this beam so that it can fall on different positions on a screen. The screen has dots(pixels) with phosphors that glow when hit by electrons and when all of these dots glow.

CRT monitors build up an image by scanning this electron beam horizontally across the screen in one line, then moving the beam a short distance downward and scanning again and again. The image is built up line by line.

So a VGA signal has two clocks called Vertical Sync (VS) and Horizontal Sync (HS). The HS pulses every time the screen starts scanning a new line. And the VS pulses every time the whole screen is scanned once. For more details on VGA signals, visit this page.

The R, G and B, signals have nominal values of 0.7 volts. So 0 volts on the R line means no Red component and 0.7 volts means maximum red component. The inputs have impedances of 75 ohms.

I took a look at the schematic of the VGA module.

You'll notice that there are three pins connecting to the red and green input signal and 2 connecting to the blue input signal. The pins are connected through resistors whose values rise exponentially. This is a simple way to perform DAC. I decided to work through the equations to see what input voltage levels were available to me.

For the Red and Green channels, I took the value of the smallest resistor as $R$ and the input voltage as V and went from there.

$\frac{V-V_1}{4R} + \frac{V-V_2}{2R} + \frac{V-V_3}{R} + \frac{V}{R_L} = 0$ (Using KCL)

Rearranging in terms of $V$ we get:

$V = \frac{V_{cc}}{14 + \frac{4R}{R_L}} (2b_0 + 4b_1 + 8b_3)$ where I set $V_i = V_{CC} b_i$

So after substituting all the numerical values. $(V_{CC} = 3.3V, R_L = 75 \varOmega R = 500 \varOmega)$ I get:

$V = 0.09802(b_0 + 2b_1 + 4b_2)$ where $b_2 b_1 b_0$ is a 3 digit binary number. This allows me to have 8 different shades of red! Following a similar procedure I can get expressions for the other colors. And in total I need 8 bits (3 for red, 3 for green, 2 for blue (poor blue :( (I like using nested parantheses. (I should probably stop now.)))) to represent the color of one pixel. So I have a palette of 256 colors to work with.

Now it's time to get started with generating the VGA sync signals.

I'm learning from this textbook called FPGA Prototyping by Verilog Examples by P. Chu. It has a brilliant section on generating VGA output. However, I couldn't just copy the code in there and check if the circuit was working. I have a small external monitor which I use for testing purposes. It turns out that this monitor (because it's a very cheap monitor) only supports one video mode. 1368 x 768 @ 60 Hz refresh rate. Nothing else works. Following the signal timing information about this resolution from the tinyVGA website, I modified the code (actually I typed it out line by line on my own so I could understand what was going on in each line. I highly recommend this technique when you're learning a new language.)

It took quite a while for me to get the output working properly. First I tried using the ipcore wizard to generate a pixel clock that was exactly the same as the recommended one. But when I tried to get the ISE to compile the code, it complained about timing issues. So I used the ipcore wizard to generate a clock that was exactly twice that of the pixel clock and divided this clock by 2 inside the VGA synchronization module.

Here's a video of the working VGA output:

Now I think I'll get started on generating something interesting on the fpga. Maybe some fractals?

Monday, 26 January 2015

i3 Window Manager - After 7 months.

So I've been using the  i3 window manager for quite a while now. And once in a while I miss some of the functionality that certain programs in Ubuntu provided. Especially some of the functionality of the network settings tool. So just for one evening I decided to switch over to the Gnome Desktop Environment.

I had to switch over to i3 within a few hours because after the wonderfully customizable and workflow friendly key combinations that I had set up in i3 fiddling about with the mouse on the desktop environment felt really clunky and frustrating. It took me more than twice as much time to do a simple task like open a text editor and start writing some C code.

Rich desktop environments like Gnome and Unity are good if you're a beginner or if you don't plan on using your computer for anything other than web browsing. But if you're into programming or doing anything even remotely technical with your computer, keyboard friendly tiling window managers like i3 will definitely increase your productivity.

Tuesday, 6 January 2015

Setting Up a Cool Dr. Who Wallpaper That Changes Every Hour on Linux

I came across this really cool idea by someone on reddit to take this awesome wallpaper of the 12 regenerations of the doctor and highlight each Doctor by the hour. This is easy enough to do on Windows which has wallpapers which can change every minutes. Being a user of linux, I was interested in trying to get a similar thing to work on linux.

First here is the set of all the wallpapers.

Download the wallpapers and put it all in a folder somewhere on your system. Say you put it in /home/ash/Pictures/whopaper/

Rename each picture so that the filename corresponds the the regeneration number. The first doctor's highlighted picture should be named 01.jpg and the second doctor's should be named 02.jpg and so on till 12.jpg.

Then set up a cronjob that is executed every hour that checks the hour on the clock and sets the background to that. For doing this I installed a program called "feh" (sudo apt-get install feh) which allows me to set my desktop wallpaper from the terminal. I also installed a program called gnome-schedule (sudo apt-get install gnome-schedule) which allows me to easily edit the crontab file.

Run gnome-schedule and click on "New" to add a new cronjob. Select "A task that launches recurrently". Add a short description (like "Cool Doctor Who Wallpaper" and in the command field enter the following command:

env DISPLAY=:0 /usr/bin/feh --bg-scale "/home/rationalash/Pictures/Wallpapers/DRWHO/$(date +\%I).jpg">>sys.log

Replace the path to the wallpaper in the command to the folder in which you saved the wallpapers.

Under the  "Time & Date" section select "Basic" and set the task to run every hour.

And that's it! Now if you don't keep your computer on all the time the wall paper might be wrong for some time because the cronjob only runs every hour. If you want to be sure that the wallpaper is always the same as the hour on the clock even if your turn your computer on a lot edit the frequency of the cronjob so that it runs more often. (Say once a minute).  The wrong wallpaper won't be there for longer than a minute.

Monday, 8 December 2014

Using the ESP8266 WiFi module with an Arduino

So I was pretty excited when I discovered the new cheap ESP8266 wifi module. It seemed too good to be true. I kept feeling that it's got way too many features for it's cost. There had to be a catch somewhere.

But no! I had mine delivered to me a couple of days ago and it's the real thing! Of course, it's true that it's a bit difficult to get it working as the documentation available is mostly in Chinese but there are plenty of people working on a translation. I had a little trouble getting the thing working properly. So I'll give a short tutorial on how to get the module working with an Arduino so that others find it easier.

First of all, here's the pinout of the module:
That's the pinout when looking at the module from the side of the antenna. The antenna is that crazy trace that looks like a square wave.

The module works on 3.3v. So wire up the Vcc pin of the module to the 3.3v pin of the Arduino.
GND goes to ground.
Both RST and CH_PD also goes to 3.3v.

Now here's the tricky bit. The arduino is fine with reading incoming 3.3v signals on its digital input pins but the ESP8266 is NOT ok with incoming 5V signals from the Arduino. So use a simple voltage divider. (1k, 2k resistor) to step down the 5V signal from Arduino to 3.3V.

Another point to note: The RXD of the ESP8266 goes to the RX pin of the Arduino (pin 0) and the TXD of the ESP8266 goes to the TX pin of the Arduino (pin 1). It looks like the Arduino has the labels the other way round. This is because when the Atmega IC is removed, the TX and RX pins are the RX and TX pins of the USB to UART bridge. When the IC is actually in the socket, I suspect that the TX of the module should go to the RX and the RX of the module to the TX of the Arduino board.

I then removed the Atmega328 IC from the Arduino's socket and turned on the power. You should see a red LED light up on the ESP8266 indicating that the board is powered. A blue LED should briefly flash. That blue LED indicates serial communication. Now go to the Arduino IDE and open up the Serial monitor. Set the line ending to "Both NL and CR".

Some ESP8266 modules come with old firmware that requires you to set the baud rate to 115200. Some modules come with the new version of the firmware and a default baud rate of 9600. So try out different baud rates if you get gibberish on the monitor. Once you find the correct baud rate try typing in "AT+RST" into the serial monitor. You should get some output. Here's what I get:

The wifi module is controlled using "AT commands". There is a list of the available commands here. Say you want to connect to your home wifi network.

First type in "AT+CWMODE?". This gives you the current mode the module is operating in. You need to set it to mode 3 if you want to connect to your home router. To set the mode to 3 use the command, "AT+CWMODE=3".

Then type in "AT+CWLAP"(List Access Points). This will give you a list of available wifi networks. Hopefully, you see yours on the list.

To join an Access Point I have to use the command "AT+CWJAP"(Join Access Point).
The syntax is "AT+CWJAP="SSID","password".

And there you have it! You've connected to your router with the ESP8266. There's all sorts of cool things you can do after this. You can set up a TCP or UDP server and start streaming sensor data for example. Maybe I'll write about that in the next blogpost.

Wednesday, 5 November 2014

FPGA : A minor hiccup

So I was happily working on my FPGA for a while on my Linux system. One day, due to some work I had to do on a piece of software that only worked on Windows, I had to boot into my windows partition.

When one of my friends came over to take a look at the FPGA I plugged it right into the laptop because I already had a binary config file in the FPGA and all I needed was for the board to be powered up. Big mistake. The moment I plugged the fpga in, the red light on the board (which usually only comes on when the board i being programmed) came on and stayed on. And no matter how many times I re installed the drivers, the board just would not cooperate. I didn't really think much of it at the time. I thought that this was just a minor issue with the drivers on the windows side and thought that everything would be fine when I plugged my board in when I booted back into my Linux partition.

The next day, I booted back into Linux and the board was doing the same thing. The red programming led was coming on but the board wouldn't work.

I have no clue what happened but I wonder if it is related to that recent incident where the FTDI company pushed a driver update to windows that would brick fake ftdi chips that acted as a USB to UART bridge. The funny thing is, the Numato labs board doesn't have an ftdi chip on board. I think it has a PIC microcontroller programmed to do the USB to UART bridging. I wonder if the PIC device was emulating the ftdi chip? Or it could just be a coincidence. Whatever the case, the board was bricked.

I sent an email to the Numato labs customer service people about the issue without really expecting a response. To their credit however, not only did they respond, they offered to replace my board with a new one if I shipped my bricked board back to them! :D Never expected that! So the new board arrived today and I plugged it in and everything seems to be working fine.

Time to resume work on that project! :D

Tuesday, 21 October 2014

An 8 bit counter on an FPGA

So I finally got around to implementing an 8 bit counter on the FPGA that I recently acquired. Turned out to be a lot more difficult than I thought it would be mostly because I was a bit new to writing modules in verilog. I'm still at that stage of learning where I make silly mistakes which are simple but tedious to rectify.

Also, the clock of the FPGA is 100MHz. So I also had to divide the clock down to some frequency that was visible to the human eye. But in the end I got it working! The FPGA itself is only just barely visible but I couldn't turn up the lights without completely washing out the blinking LEDs. Oh well ...


I have this problem where I try to go to the advanced sections of new stuff too quickly and then give up because it looks too complicated. I have to keep telling myself to take things one step at a time and build up slowly to more advanced/complicated stuff.

So now that this counter is done I think I'll search around for something that's a little bit more complicated than the counter but still within my reach. And slowly build up to something more complicated like Finite State Machines or VGA controllers.

Sunday, 14 September 2014

Quality of YouTube Comments

So it's common knowledge by now that if you want to see the most mind numbing, narrow minded side of humanity all you need to do is visit the comments section on an even slightly controversial YouTube video.

However, I've noticed that the comments are not so bad at the start. I subscribe to a lot of popular channels on YouTube. That means I sometimes get to see the videos as soon as they're out. I've noticed that the comments are usually really nice and supportive at the start. But as the video gets more and more popular the racist/sexist/homophobic/narrow minded comments start pouring in. It's probably because at the start, most of the people who see the video are those who subscribed to the channel. And these will be people who actually like the person who makes the videos and appreciates them for putting in effort to make good content. Once the video percolates through to the general public however, mean people start spamming the section with comments. 

It's actually kind of sad to see the comment section slowly become filled with hateful negative comments and overwhelm the nice ones. 

Friday, 29 August 2014

Getting Started with FPGAs

So after a lot of deliberation I've decided to start working with FPGAs. Ever since the Mojo came out I've been thinking about this. This semester I finally decided to go ahead and get an FPGA. I initially thought about getting the Mojo. I was reluctant because despite the claim that it is for hobbyists, it's not exactly cheap at Rs. 7500. So I decided to search for alternatives that used the same FPGA IC.

After a lot of searching I found this FPGA development board called "Mimas" from Numato Labs. They were selling this board at Rs. 3000 on Amazon. That was less that half the cost of the Mojo! So after a lot of searching and comparison of specs I finally decided that I'd get Mimas instead of Mojo. This board also has the additional advantage of 4 on board switches. The Mojo has none. Here's the FPGA in all it's glory! :D

After I got the board the next step was getting the Xilinx ISE from their website. This was a big Pain In The Ass. The installation file was 6.1 GB and would take ages to download using my sort of slow internet dongle. The download was resumable but something would always go wrong at about 4 GB and the download would start over. In the end I had to start the download at 4.00 am in the night (When the college wifi is fast) and I had it downloaded in about 2 hours. I think it's a real shame that Xilinx doesn't distribute the software over a protocol that is more suited to resuming large downloads (maybe a torrent?). Getting my hands on the FPGA was way simpler that downloading the software.

Anyway, I started the installation after checking the md5sum of the download and everything seemed to be fine! I implemented some basic combinational logic circuits on the board to check if everything was working fine. I'll be implementing something a little bit more interesting (like a counter maybe?) and then I'll upload a video of the board working! :D