Search Microcontrollers

Sunday, March 23, 2014

My first FPGA is on its way

I couldn't resist.
I am already busy with many other things (but my laser is now sat up in a lab some 100s Km from me now, to be used to run some tests), but it's  quite some time I am curious about FPGAs.
Every now and them I found myself thinking : "I should get myself an FPGA DEV board and start playing with it", then I would normally try to stick my fingers in my ears and say out loud : "lalalalalalala!".
It actually worked for a while, till a few days ago when I bought a Cyclone IV dev board.

Argh, I knew it would have happened sooner or later, I wonder if there is some kind of therapy for us geeks, to keep our urges of technology under control.

Funny enough this is one of the times where I do not have an application in mind that might justify the acquisition of the mentioned technology... it is simply curiosity at its finest.

I started reading about FPGA, downloaded the free software from the Altera website while my board is traveling from mighty China to the Swiss mountains where I play with cheese, lasers and bits.

So, if you know already a bit FPGAs, then you will not find a lot of valuable information here : I am just discovering (still you are welcome to post your comments, they might help me out in my discovery process).
On the other hand, if you are like myself, maybe a bit skilled with MCUs etc, but never played with FPGAs, then this article might actually spare you a bit of googling around and help you decide if you want to give them a try or not.

Field Programmable Gate Arrays are "a different animal" if you are coming from microprocessors or MCUs.
These devices don't come with any specific functionality.
Sounds weird, right?
I mean, when you buy an MCU, you select it for the number of bits it has, the clock frequency, internal peripherals such as serial ports, ADCs, timers etc,
None of that is available in an FPGA.

So, what do you get?
You should expect a high number of pins that can be configured as inputs or outputs, cool, but honestly not really enough.
Then you can connect those pins internally with sequences of logic gates (remember the old 74xx TTL stuff? Those, but smaller and with no need of wires or pcb traces to connect them!).

I know it does not seem much, but it turns out to be a big deal in the end.
Now, imagine you have several thousands of those Logic Elements (LE), which to be fair are a bit more flexible that the old 74xx gates.
You know that a microprocessor is just a bunch of those properly connected, right?
Technically these LEs are arranged in an Array and an internal RAM stores how they should be configured and how they should be interconnected.
Did I say RAM?
Yeah, I did, and that does mean that every time you turn off an FPGA it goes back to be a bunch of non configured and non connected LEs just the same way it was shipped out of the production line.

This means you need an external EEPROM or similar device to store the configuration that allows the functionality you planned for your project and, when the FPGA chip is powered up, this configuration data is sent with a serial protocol.

In the end, programming an FPGA simply means storing the configuration data into the non volatile memory that will feed the device at power on.

With that clarified, how do we create a complex logic (i.e. a microprocessor) using just basic gates?
I am sure there are out there skilled professional that could design a CoreI7 with a bunch of 74xx chips, and actually that would be one way (not the easiest) to go.
In fact you can design the schematics and place all your lovely AND, NAND, XOR etc in it, place the wires, connect stuff to pins and probably invent the next gen CPU with that.

I surely hope this is not the only or easiest way to go... apparently my hopes are not placed in vain.

It is possible to "describe" the hardware using a Hardware Description Language such as Verilog or VHDL.
If you are reading this post, chances are you are familiar on how source code normally looks like, so you could see the example below (VHDL snippet) and still feel you are in your comfort zone.

Ok, cool, we have code blocks with begin / end (Pascal anyone?), we have IFs.. meh, looks like source code.
What's  special there?
I guess many things, but the one that shocked me at first is that instructions are not necessarily executed in a  sequence.

Let me give you an example :
Imagine you are using an MCU, reacting to an Interrupt, you received a command over the UART and now you want to turn on an LED on pin P1.1, check the value  of pin P2.1 and if this is equal to 1 then turn on a fan connected to pin P1.2.

So you would get something like this :
gpio_P1_1 = 1;
if (gpio_P2_1==1) gpio_P1_2 = 1;

Technically this gets compiled and a series of instructions are executed sequentially, if we use the debugger we can clearly see thew first line being executed and the LED turning on, then the second one and eventually the fan turning on.
What happened in our MCU is that the same hardware (registers, whatever) meaning the same transistors in the chip where used to execute the two lines and when they were busy taking care of line 1, line 2 was waiting.

In an FPGA you do not (necessarily) create a sequence of instructions, so the same task would be accomplished by describing what should happen when the command is received.
The two lines would be executed simultaneously because they would actually be implemented by separate transistors in the chip.
Those transistors would have one single job : to deal with that specific instruction, anytime needed and regardless of any kind of clock.
A block of gates, interconnected to absolve some dedicated function is then considered a sort of black box with inputs and outputs that allow it to interact in a bigger design,

Basic and complex blocks are available in libraries, which also contain a free microprocessor (called NIOS II) that can be implemented in your design, you can even run a Linux Kernel on it (yup, with a bunch of 74xx style toys, not bad eh?).

To embed some specific and existing designs you may need to pay licenses (Intellectual Property) to the creator, at this time I am not aware of that ecosystem, so it might very well be that you can find free stuff for most of the needs or not.

Normally FPGA devices include also some memory blocks (RAM) and PLL circuitry to create clock signals you may need for the logic you implement.

But then if an FPGA is in general an empty canvas, why are there so many models?

There are different manufactures (not many actually) each one of them offering different families of devices.
Every family has some peculiarity : power consumption, input output configuration (some have high speed transducers),  maximum speed achievable, eventual internal HW CPU (typically a cortex A9 or similar) embedded etc.

Then , within a family there are plenty of models which have two main variables :
Number of LEs and Speed Grade.

Why number of LEs is important?
My dev board is equipped with a small Cyclone IV (Cyclone IV is the family), I am saying small because it has only about 6200 LEs, while the big devices might have 350.000 of them (115.000 max currently in the Cyclone IV family).
The more complex your design is, the more logic it needs which translates in a higher number of LEs required.
I think I found somewhere that, depending which configuration you need, a NIOS II processor would require from about 600 LEs.
[more info here]
Now, if you consider that my small device has 6200 of them, you can put things into scale.

The second parameter is the speed grade, I imagine that would greatly affect the maximum clock frequency you could use in your internal logic (i.e. in the NIOS II processor).

There are then some other details such as number of PLL circuits, number of internally available memory blocks etc.

The beauty of this is that, say you start your design with a small device and then it gets more complex over time, you just need to switch to a device that has more LEs (might impact your pcb tho, as number of pins might also change).

In conclusion, I cannot say I learnt a lot so far, but the few things I saw are making me more and more curious and eager to start experimenting.

I found the Altera website quite good to find resources, there is a training section which has plenty of good (and free) online training resources.

I cannot endorse one brand or another, looks like for beginners the two most obvious options are Altera and Xilinx, and I decided I'd  give Altera a go first as there are plenty of cheap and simple DEV boards on the market, lot's of literature etc.
I think that even porting from a brand to another might be quite easy, since by itself an FPGA does not provide any specific functionality, you just need enough room for your design, silicon that reacts fast enough and eventually amenities such as RAM and PLLs.

This article is already quite long, so I am cutting it here and I am not describing how the LEs work (which is quite fascinating), how they use the LUT etc.
While I might expand that in a future post, if you are curious, you can find   a lot of info here

Update : I just received the board, you can see it here (top board) compared with the Stellaris Launchpad (bottom one).
The USB Blaster manages the JTAG interface, tiny device actually and you need to install the driver found in the Quartus software distribution (also in the free one) as explained here

Should you be interested, you can find some boards here :

1 comment:

HOLGER said...

Thank you for your Doku. on Altera.