For Christmas i had planned a little electronics project, a credit card sized pcb with a christmas tree on the silk screen layer and a series of 12 leds of different colours that twinkle in a pleasing pattern. i planned to make about 5 and give them to friends and family as a sillt little gift for fun. I had got as far as a pcb layout using a soic pic which i planned to send to a chinese pcb manufacturer such as seeed studio.
However my plan was ruined. I managed to slip on the floor while walking and fell on my arm and ended up with a very nasty break in my right arm which required surgery to fix and puts me out of action until mid or end of january assuming all goes well. Even typing left handed is a challenge!
I’ve worked in London for the last year which involves a long working day and more than three hours of commuting there and back so I hardly have any time to do any fun stuff any more, let alone write about it.
Also this blog got broken. The hosting company decided a few months ago to move their VPS servers to a different part of the company. When they did that they lost the ability to automatically charge my credit card, instead expecting me to log onto the control panel and make a manual payment each month. Which is annoying enough but they didn’t even tell me so the firts I knew was when my server was suspended for non payment.
I paid what I owed, but they had also charged me late payment fees, which wasn’t exactly fair for something they hadn’t even told me I owed them!
Anyway, it’s all sorted out now and the feed refunded. I’m reasonably happy with how they sorted it out, if not that they had to! I don’t imagine I’ll stay there long. I already have a different VPS with http://www.bytemark.co.uk/ which just works and I’ve never had any problems at all with them and will likely move this blog to them soon, either on my existing VPS or a new one.
Which brings me to the other thing.
This blog has been neglected far too long and is old and outdated. I really enjoy the electronics stuff but just don’t have time for it any more at all. I have a little time for programming projects which can be done more quickly so I might start a new blog for that. I doubt anyone would miss this one, it has stuff I’ve done, but it’s mostly links to temporary projects that don’t exist any more, and they dont contain much technical information so I might just scrap it entirely and start a new one. Not sure…
Ultimately i’d like to build my own computer using an fpga, with vga output and a custom CPU.
This has inspired me a great deal to work on it some more I have a load to learn still though
Just a small project tonight that I spent an hour on after work -
I bought a little infra red sensor for sensing remote control signals. I decided to hook it up to my fpga board to see what i could do with it. It’s just 3 pins, vcc, gnd, and a data signal so plugged then directly into power and a data pin on my board.
I wrote some verilog to read the bits at the right speed and poke them into the video memory of my previous experiment with VGA video and got this.
Pressing 0 on my remote…
Pressing 1 on my remote… (sorry the photo is blurry)
Pressing 8 on my remote
I looked at the RC-5 protocol here and it looks exactly what I would expect.
I imagine I could easily actually decode the values now and make it control something, but that’s for my next project
One of the reasons I’ve not posted anything here for about 2 weeks is that I reached a problem I could not solve.
The fpga evaluation board has rs232 hardware attached to a few of the pins of the fpga so I decided that a nice small project would be to create a UART in the fpga to send serial data to the port and display it on my pc.
This involves creating a circuit to divide down the 50MHZ clock to the baud rate clock you want.
Creating a little state machine that steps through the bits of the byte to send, and a lookup table to output the correct bit for the correct state.
I implemented all that and despite everything I couldn’t get it to output anything at all that I could read on my PCs serial port. I looked at the code hundreds of times over two weeks and nothing worked. Eventually I gave up and posted my code on http://electronics.stackexchange.com/questions/13718/implement-serial-port-on-fpga-verilog to ask for help. I got some good advice about reset signals but the problem was pointed out to me by one kind person was that I’d put in code to send a ’0′ for a rs232 stop but not a ’1′. I knew it was supposed to be 1 of course but I looked at the code hundreds and hundreds of times and only every saw the correct code there…
As soon as I corrected the code it worked perfectly.
The code is posted in that stackexchange post if anyone is interested. Note that there are very much better serial port components available for free, so this isn’t something to use, just perhaps interesting to one or two of you.
It’s not immediately obvious how to get the fpga “program” onto the evaluation board. One reason for this is that there are *many* places to store the code. In general fgpa chips don’t have any rom so need programming on startup every time. This means an external memory needs to be attached. The evaluation board I have contains several different kinds of memory, and also the spartan-3an chip ‘N’ version contains some internal program memory to this particular device can actually store it’s own program.
On the evaluation board there are several hardware jumpers which control where the initial program is loaded from.
For the purposes of this article though I’ll show what I do to program the board without storing the program anywhere. You need to repeat this if you reset or power off the board.
First of all I assume you’ve already created a program using ISE Design Studio and produced a .BIT file with the program in.
In the IDE the option you need is disguised as “Manage Configuration Project (iMPACT)” on the menu.
Choose to run that and you’ll launch a separate program iSE iMPACT. On the menu on the left choose “Boundary Scan” (After powering up the board and connecting it via USB to your computer of course).
Then right click the right hand panel on the screen where it says to and choose “Initialize Chain”. It will scan for jtag devices.
I cancel all of the dialogs that appear afterwards so I can set up the programming file manually. You should end up with something like this -
The fpga is the xc3s700an highlighed in green. Right click it and choose “Assign New Configuration File”.
Find your .BIT file and select it.
The final step is to right click the same node again and choose “Program FPGA only”. Once that completes the board will running your “program” immediately.
The layout here is horrible, sorry. I intend to redo this blog some time soon to make it much more readable
Getting started
I’ve been learning how to use my new spartan FPGA board and it’s been quite difficult to understand how to program the board. What software you need and how to do all the things that need to be done so I thought I’d write it up here starting with a simple example of how to flash an LED.
The first thing to notice is that the board comes with software on a CD but mine was very out of date being at least 3 versions behind the current one. The other thing it doesn’t seem to say anywhere is that you have to pay to use the full version, it’s only the cut down version that’s free. However the good news is that it appears to only be cut down so that it doesn’t work with the very high end fpga’s and will work fine for this one.
You need to go to the xilinx website and download the current version of ISE design suite. (Which was 13 when I wrote this). It’s not far of 4GB in size as a download though! I’ve not idea what takes up so much space. The free version is called ISE WebPACK so choose that when you install it.
Starting the software
The software will put a link on the desktop called Xilinx ISE Design Suite 13.1 or similar and it’s that you want to run.
Choose to create a new project either from the main menu or the main screen and you’ll get something like this -
Fill in the name of a project and where to put it. Also make sure you choose HDL as your source type. Then click “Next”
I then chose my board type “Spartan-3AN Starter Kit” and chose Verilog as the Preferred language. It will ask you to confirm your choices then create an empty project.
You can right click the top left part of the main window now where it says “Empty View” and then choose New Source. Select Verilog Module and give it a name like led.v
Finally you get a screen like this which allows you to define what your inputs and outputs to your module are to be. Don’t worry, it’s easy to change them later! For this new top level module these will be hardware signals so you need to enter a “clock” as an input and an “led” as an output. These are just names and can be anything you like.
This will create a new verilog module for you. If remove the header comments and reformat it slightly it contains only this -
module led(input clock, output led);
endmodule
This is verilog code for a module with one input called clock and one output called led
The default clock on this board is 50Mhz which is a bit too fast to flash an LED so we need to slow it down. The best way to do this is to make a counter which counts up on the positive edge of each clock pulse and then use the most significant bit of the counter to output to the LED.
If we create a counter with 24 bits then that will divide by 2^24 = 16777216 which will give us a visible flash rate for the led.
So first we need to define a register big enough to hold the counter. In verilog we do that like this :-
reg [23:0] counter;
That declares a register with 24 bits numbered 23 to 0.
Now we need to cause the counter to be incremented each clock cycle.
always @(posedge clock)
begin
counter <= counter + 1;
end
The always command with the condition means run this block whenever the condition happens. In this case when the signal “clock” has a positive edge. So it will run the contents of this once every clock cycle, and increment the counter we declared above.
Finally we need to output the highest bit of the couter to the port that the LED is attached to, like this
assign LED = counter[23];
Which simply says that LED is connected to bit 23 of the counter. Think of this like a wire, it’s not executable code, it’s simply showing the connection.
So that’s the whole program -
module led(input clock, output led);
reg [23:0] counter;
always @(posedge clock)
begin
counter <= counter + 1;
end
assign led = counter[23];
endmodule
The final thing we need to do it link the names of clock and led to the correct hardware ports on the FPGA. To do this create new source file of type “Implementation Constraints File” called main.ucf and put the following lines in
NET "clock" LOC = E12;
NET "led" LOC = R20;
The pin numbers and other information can be found in the user guide.
Part 2 of this will describe how to load the program onto the board but for now here is a short video -
The code follows below (Understand this is my first attempt at verilog so it’s very much a work in progress and in huge need of proper layout and commenting – it’s not up to any decent standard!)
My previous experiment just output video by altering the R, G and B lines at various points in the screen. To do anything worthwhile I’m going to need to generate video using a proper video frame buffer. It has taken me the best part of two days to work out how to make use of the blockram on the fpga chip as a video buffer but I finally managed it.
My big problem was seeing verilog though the eyes of a C++ programmer still, and seeing it as a program, not as a hardware description. When i saw video[addr] I saw it indexing into the video memory array using addr as an address. Which is is, but unlike in C you can’t read or write the memory more than once in the whole program. Think of video as memory chip and addr as being the wires leading into the address pins – when you think of it like that it’s obvious that you can’t just wire up a second set of address wires to the same pins and the output to somewhere else, at least without some logic to multiplex them properly.
I’ve finally got it working nicely though. At the moment i’ve used 4096 bits of memory for a simple 64 x 64 pixel frame buffer and initialised that memory with a hex file containing a pixel map of what to display. The results don’t look very impressive butI’m pretty pleased that now I can configure hardware VGA output on my fpga.
I intend to do a lot more with this! My first plans are to
Increase the size of the frame buffer to higher than 64 x 64 pixels. There is 360Kbits of block ram on the FPGA which it enough to do 640×400 in black and white or maybe 320×200 in colour. It’s possible also to use some of the general gates on there as ram too I believe.
There are 64MB of external ram on the board too which I ought to be able to use of course. But it’s not as simple as just using ram, you have to actually generate all the signals to the ram chips in code. There is a utility to generate code to do that though so it shouldn’t be impossible.
I received my mbed that I talked about in my last post but what I didn’t mention is that I also ordered a board for learning (ok, playing about…) with FPGAs.
Specifically I got a Spartan 3AN evaluation board. Not that cheap at about £180 (I think…) but it includes RAM, flash memory, an LCD display and all those hard to connect up connectors already connected so I figured it was worth it… And what’s my new job for if not to let me buy gadgets
As you can possibly see there are several switches in the center of the near side of the board.
The connector on the left is the USB programming connector, and you can just about see the VGA plugged in at the back. The text on the lcd isn’t from my program, it’s left over text from the program that runs when you turn it on (At least until I overwrite the ROM with my own program rather than just uploading and running it like I do now).
So, I wrote a simple program for it. (Is program even the right work for configuring an fpga?). I looked at the various languages and liked verilog best so I used that. I based my code on some things I found on the internet but had to modify it a lot.
Basically I generate a VGA image consisting of red, blue and green blocks at various frequencies, and three of the switches at the front let you turn off the various colours.
it’s not very exciting for now, but I’m *really* pleased with it. It takes quite a lot of understanding to work out how to generate the video and get it onto the device. When you are used to programming it’s hard to get your head around the fact that all your blocks happen at the same time. Spending 100% of your time incrementing a counter to time the horizontal sync pulses only uses *that piece* of hardware 100% of the time and all the rest of the system is doing whatever you’ve set that to do. I think the more programming you’ve done the harder verilog is to get good at.
This is what you see with all the switched in the “ON” position
And this is what you see with only the “Red” switch enabled.