The toolchain
Now we have necessary hardware, but still the only thing we can do is to login into the RasPi in a slightly different way. Now we need some software tools to convert our program sources into binaries for RasPi.
I'm aware that there are some instructions floating around on how to acquire a toolchain more quickly, but I think the most correct way is to build one ourselves.
Since I'm using Debian as my primary desktop operating system for years, I'll provide instructions for it, but the same principle should be valid for any Linux-based OS. Tip for Windows users: you can run Linux inside VirtualBox.
First thing we have to ensure that our host system has all the necessary tools. Open your terminal and run:
It should either tell, that you have all the newest versions or ask your confirmation to download and install some packages.
We also need to prepare a location in our file system, where the toolchain will be located:
I did just created a directory and made it writable by me (of course replace velko with your username). You could, of course, choose a different path and not change it's owning user. But then you must already know what you're doing and should be able to adjust following commands accordingly.
Next it would be a good idea to adjust the Linux environment variables accordingly. So run:
and append :/usr/local/cross/bin to every line starting with PATH=.
Hit Ctrl-O and Ctrl-X to save and exit. Now you should log off and log back in before continuing.
If you absolutely refuse to re-login, you also can execute:
First thing we need is software package called GNU Binutils. As name suggests, it contains set of tools to deal with compiled binaries. Go to download site and look for a newest ones. Download them (2.22 for the moment of writing this, adjust following commands if there are newer versions available):
It will download several megabytes of source code. Now we should unpack it:
Create temporary directory for compilation process:
Now go into that directory, configure, compile and install:
Next we continue with GNU GCC. It's a set of compilers for turning source code of various programming languages into compiled binaries. Go to mirror selection site, choose one closest to you and download newest sources (for the moment of writing this):
Go into that directory, configure, compile and install:
We now have most essential tools ready for bare-metal development. Next we need to learn how to use them.
In the meantime you may try out David Welch's examples.
I'm aware that there are some instructions floating around on how to acquire a toolchain more quickly, but I think the most correct way is to build one ourselves.
Since I'm using Debian as my primary desktop operating system for years, I'll provide instructions for it, but the same principle should be valid for any Linux-based OS. Tip for Windows users: you can run Linux inside VirtualBox.
First thing we have to ensure that our host system has all the necessary tools. Open your terminal and run:
Note that, instead of using sudo, you could also log in as root and execute the commands without it. In this tutorial I'll however prepend sudo to every command that requires elevated privileges.sudo apt-get install build-essential wget
It should either tell, that you have all the newest versions or ask your confirmation to download and install some packages.
We also need to prepare a location in our file system, where the toolchain will be located:
sudo mkdir /usr/local/crosssudo chown velko /usr/local/cross
I did just created a directory and made it writable by me (of course replace velko with your username). You could, of course, choose a different path and not change it's owning user. But then you must already know what you're doing and should be able to adjust following commands accordingly.
Next it would be a good idea to adjust the Linux environment variables accordingly. So run:
sudo nano /etc/profile
and append :/usr/local/cross/bin to every line starting with PATH=.
Hit Ctrl-O and Ctrl-X to save and exit. Now you should log off and log back in before continuing.
If you absolutely refuse to re-login, you also can execute:
in your terminal, but bear in mind that in this case following commands must also be issued in the same terminal.export PATH=$PATH:/usr/local/cross/bin
First thing we need is software package called GNU Binutils. As name suggests, it contains set of tools to deal with compiled binaries. Go to download site and look for a newest ones. Download them (2.22 for the moment of writing this, adjust following commands if there are newer versions available):
wget http://ftp.gnu.org/gnu/binutils/binutils-2.22.tar.gz
It will download several megabytes of source code. Now we should unpack it:
tar -xzf binutils-2.22.tar.gz
Create temporary directory for compilation process:
mkdir build-binutils
Now go into that directory, configure, compile and install:
cd build-binutilsThe 3rd command can take some time to complete. You may also want to adjust the -j5 setting. For the fastest compilation, as a general rule of a thumb it should be the number of CPUs (or cores) your computer (or virtual machine) possesses + 1. But nothing will fail if you set it wrong, it will just take more time to complete. For my (quad core) Core i5 it took less than 1 minute to complete, but your mileage may vary.
../binutils-2.22/configure --prefix=/usr/local/cross \
--target=arm-none-eabi
make -j5
make install
cd ..
Next we continue with GNU GCC. It's a set of compilers for turning source code of various programming languages into compiled binaries. Go to mirror selection site, choose one closest to you and download newest sources (for the moment of writing this):
It will also download several tens of megabytes. Next we unpack it.wget http://mirrors.webhostinggeeks.com/gcc/releases/gcc-4.7.1/gcc-4.7.1.tar.bz2
GCC requires a few additional libraries, which may not be present on your system. We'll download, and move them into GCC's tree (also watch if there are newer versions available):tar -xjf gcc-4.7.1.tar.bz2
Similarly as with Binutils, create temporary directory for compilation:wget ftp://ftp.gnu.org/gnu/gmp/gmp-5.0.5.tar.bz2wget http://www.mpfr.org/mpfr-current/mpfr-3.1.1.tar.bz2wget http://www.multiprecision.org/mpc/download/mpc-1.0.tar.gzmkdir gcc-4.7.1/gmp
tar -xjf gmp-5.0.5.tar.bz2 --strip-components=1 -C gcc-4.7.1/gmpmkdir gcc-4.7.1/mpfr
tar -xjf mpfr-3.1.1.tar.bz2 --strip-components=1 -C gcc-4.7.1/mpfrmkdir gcc-4.7.1/mpctar -xzf mpc-1.0.tar.gz --strip-components=1 -C gcc-4.7.1/mpc
mkdir build-gcc
Go into that directory, configure, compile and install:
Again, some commands may take some time (up to half of an hour, on slower computers) to complete. Note that we built compiler for C programming language only. You may delete downloaded source code and temporary directories now.cd build-gcc../gcc-4.7.1/configure --prefix=/usr/local/cross \--target=arm-none-eabi --disable-nls --enable-languages=cmake -j5 all-gccmake install-gccmake -j5 all-target-libgccmake install-target-libgcc
We now have most essential tools ready for bare-metal development. Next we need to learn how to use them.
In the meantime you may try out David Welch's examples.
svētdiena, 2012. gada 5. augusts
Building a serial add-on board (part 3)
In Part 2 we etched a PCB. But our add-on board is not quite finished, there are still things left to do. Well, let's get to work.
First thing to do - we need to drill holes for board's components. I'll use Dremel-like tool with 0.7 or 0.8 mm drill bits. As for surface, I'll use old chair, that I'm not afraid to ruin (even more). You may think of something else, but remember that while drilling holes in PCB you almost certainly will make some in surface below it.
If you never did this before, it would be a good idea to practice a little on a piece of scrap FR-4. Also having few spare drill bits at hand wouldn't hurt. You never know when those tiny things break.
Note also that dust from drilling is still as hazardous as in Part 2, so again I'm doing this outside.
Done. There's however a manufacturing hiccup now. Mechanical pressure and vibrations from drilling proved to be too much for unconnected pads for 26-pin connector and almost all of them peeled off. I should probably made those pads larger, or tried a different RPM setting, or something. Fortunately those pins are not connected to anything, so apart from not looking as good as intended and being pain to solder it will not affect operation of board in any way.
Now it is time to add other parts to our board. Take a look at PCB layout again, insert necessary components and solder them. Soldering is Easy pretty much explains what needs to be done. Pay attention to polarity of electrolytic capacitors.
Done. Soldering connector's pins where pads peeled off was real pain. Instead of sticking to surface forming a nice joint, solder forms a ugly ball and sticks more to soldering iron than pin. But I applied solder anyway - if nothing else, it will keep connector in place.
Note drops of excess flux. We will need to clean them off, using small brush and acetone.
Here, our board is almost finished. Only thing missing is ribbon cable now. Gert has pretty much explained how to do it, with difference that a piece of cardboard underneath the board is needed to protect connector's pins from vice.
We are done? Well, not so fast. I would be a good idea to test it, before connecting to Raspberry Pi. Insert some wires in ribbon cable's connector and test it with multimeter to find out if there are no any short circuits from improperly connected ribbon cable. Also we can try to apply 3.3 V power to connector's pins 1 and 6 (mind the polarity) and check if the voltages are correct. There shouldn't be anything higher than 3.3V anywhere on the RasPi's side. Also there should be somewhat higher voltage on chip's pin 2 and similar negative voltage on pin 6.
Here I discovered my second mistake, I already confessed in Part 1. If you pay close attention to previous image, you may notice, that there's MAX232 chip. Everything checked out using 5 V power, but not so good with 3.3 V. I had to de-solder the chip, order correct one (MAX3232) and solder that one in place. Fortunately they were pin-compatible, so I did not need to build a new board. I was lucky this time, but from now on I'll pay more attention to part's data-sheets, before starting to build anything.
When everything checks out, it's time to wire it up.
Launch a terminal application on computer (I used minicom on Linux), configure it to right port, other settings. Insert SD card with any of provided Linux distributions. Take a deep breath and power up the Raspberry Pi. It should start as normal, but you should also see text scrolling in terminal application.
Now, when we're tested that our board works, there's only one thing left to do. Right now the tracks on our board are exposed copper. They will oxidize with time. To prevent that we should coat them with lacquer of some sort. Any non-conductive lacquer spray, designed for metal should do.
Now, when we have probably most important piece of hardware (apart from Raspberry Pi itself) for bare-metal development, we can turn our attention to software.
First thing to do - we need to drill holes for board's components. I'll use Dremel-like tool with 0.7 or 0.8 mm drill bits. As for surface, I'll use old chair, that I'm not afraid to ruin (even more). You may think of something else, but remember that while drilling holes in PCB you almost certainly will make some in surface below it.
If you never did this before, it would be a good idea to practice a little on a piece of scrap FR-4. Also having few spare drill bits at hand wouldn't hurt. You never know when those tiny things break.
Note also that dust from drilling is still as hazardous as in Part 2, so again I'm doing this outside.
Done. There's however a manufacturing hiccup now. Mechanical pressure and vibrations from drilling proved to be too much for unconnected pads for 26-pin connector and almost all of them peeled off. I should probably made those pads larger, or tried a different RPM setting, or something. Fortunately those pins are not connected to anything, so apart from not looking as good as intended and being pain to solder it will not affect operation of board in any way.
Now it is time to add other parts to our board. Take a look at PCB layout again, insert necessary components and solder them. Soldering is Easy pretty much explains what needs to be done. Pay attention to polarity of electrolytic capacitors.
Done. Soldering connector's pins where pads peeled off was real pain. Instead of sticking to surface forming a nice joint, solder forms a ugly ball and sticks more to soldering iron than pin. But I applied solder anyway - if nothing else, it will keep connector in place.
Note drops of excess flux. We will need to clean them off, using small brush and acetone.
Here, our board is almost finished. Only thing missing is ribbon cable now. Gert has pretty much explained how to do it, with difference that a piece of cardboard underneath the board is needed to protect connector's pins from vice.
We are done? Well, not so fast. I would be a good idea to test it, before connecting to Raspberry Pi. Insert some wires in ribbon cable's connector and test it with multimeter to find out if there are no any short circuits from improperly connected ribbon cable. Also we can try to apply 3.3 V power to connector's pins 1 and 6 (mind the polarity) and check if the voltages are correct. There shouldn't be anything higher than 3.3V anywhere on the RasPi's side. Also there should be somewhat higher voltage on chip's pin 2 and similar negative voltage on pin 6.
Here I discovered my second mistake, I already confessed in Part 1. If you pay close attention to previous image, you may notice, that there's MAX232 chip. Everything checked out using 5 V power, but not so good with 3.3 V. I had to de-solder the chip, order correct one (MAX3232) and solder that one in place. Fortunately they were pin-compatible, so I did not need to build a new board. I was lucky this time, but from now on I'll pay more attention to part's data-sheets, before starting to build anything.
When everything checks out, it's time to wire it up.
Launch a terminal application on computer (I used minicom on Linux), configure it to right port, other settings. Insert SD card with any of provided Linux distributions. Take a deep breath and power up the Raspberry Pi. It should start as normal, but you should also see text scrolling in terminal application.
Now, when we're tested that our board works, there's only one thing left to do. Right now the tracks on our board are exposed copper. They will oxidize with time. To prevent that we should coat them with lacquer of some sort. Any non-conductive lacquer spray, designed for metal should do.
Now, when we have probably most important piece of hardware (apart from Raspberry Pi itself) for bare-metal development, we can turn our attention to software.
pirmdiena, 2012. gada 30. jūlijs
Building a serial add-on board (part 2)
In Part 1 we discussed why we need a serial add-on board and designed one. Now it's time to actually build one.
Of course we will require some tools, materials, and chemicals, I'll describe them along the way.
First and foremost, we need a material for PCB. I'll use 1-sided FR-4 epoxy laminate. I've been lucky to acquire an enormous piece (1/2 sqm. or so) in a garage sale. But it should be available at most DIY electronics stores. As I mentioned earlier, we will require a piece with size 40 x 45 mm.
Draw the borders and cut it off with a hacksaw.
Do not worry, if you can not quite follow the line and your cuts are not very straight. You will be able to trim it to right dimensions using a file. Smooth any sharp edges as well, while you're at it.
Note, that dust from sawing and filing FR-4 might be hazardous, so I'm doing this outside.
Now it's time to clean it. I'll use a kitchen sponge with a bit of laundry detergent on it.
Rub it until you get clean, shiny copper surface. Wipe it dry afterward.
Next, we'll prepare our board image for transfer onto copper. Open Eagle, hide all unnecessary layers, leaving only Bottom, Pads, Vias and Dimension. You should see only what you are going to transfer to copper. Now print it on glossy paper using laser printer (inkjets or other printer types will not work). Use Black, Solid options in Eagles's print dialog. You should get something like this:
I used paper from marketing booklet for some software package. Check it, imagine that your board is transparent and you're looking at your PCB from component side. Now cut it out and place on piece of laminate, we prepared earlier, printed side facing copper.
Heat up an iron (regular, not soldering one) on wool to cotton setting. Place a piece of clean paper on top of the board and apply iron. Keep it still for a minute or so, and then keep "ironing" for another 10.
Remove the iron and let the board to cool off. Then place it in warm soapy water for a half of hour or so.
Paper now will be soaked and should come off easily, leaving our printed image on copper. Be sure to remove all the paper, do not forget the drilling holes. Use wooden toothpick where necessary. You should get something like this:
Now, the image is transferred onto copper and looks good. If you do not like what you see, use paper towel and acetone to clean it off and start over. To continue we will require a chemical called etchant, to remove excess copper. I'll use Ferric chloride, but there are different etchants available. FeCl3 is pretty nasty chemical, be careful.
Now we wait. In an hour or so, etchant will remove exposed copper, leaving only what's protected by toner. Here, I'm almost done:
Only few more minutes to go. Note that solution turned from orange to greenish-brown, now it's mostly CuCl2.
Once all the excess copper is gone, rinse the board thoroughly under running water. Then use paper towel and acetone to clean the toner off. Here we go:
Closeup...
Here we have our board, built at home using simple tools. In Part 3 we'll finish our board, see what else can go wrong and finally power it up.
Of course we will require some tools, materials, and chemicals, I'll describe them along the way.
First and foremost, we need a material for PCB. I'll use 1-sided FR-4 epoxy laminate. I've been lucky to acquire an enormous piece (1/2 sqm. or so) in a garage sale. But it should be available at most DIY electronics stores. As I mentioned earlier, we will require a piece with size 40 x 45 mm.
Draw the borders and cut it off with a hacksaw.
Do not worry, if you can not quite follow the line and your cuts are not very straight. You will be able to trim it to right dimensions using a file. Smooth any sharp edges as well, while you're at it.
Note, that dust from sawing and filing FR-4 might be hazardous, so I'm doing this outside.
Now it's time to clean it. I'll use a kitchen sponge with a bit of laundry detergent on it.
Rub it until you get clean, shiny copper surface. Wipe it dry afterward.
Next, we'll prepare our board image for transfer onto copper. Open Eagle, hide all unnecessary layers, leaving only Bottom, Pads, Vias and Dimension. You should see only what you are going to transfer to copper. Now print it on glossy paper using laser printer (inkjets or other printer types will not work). Use Black, Solid options in Eagles's print dialog. You should get something like this:
I used paper from marketing booklet for some software package. Check it, imagine that your board is transparent and you're looking at your PCB from component side. Now cut it out and place on piece of laminate, we prepared earlier, printed side facing copper.
Heat up an iron (regular, not soldering one) on wool to cotton setting. Place a piece of clean paper on top of the board and apply iron. Keep it still for a minute or so, and then keep "ironing" for another 10.
Remove the iron and let the board to cool off. Then place it in warm soapy water for a half of hour or so.
Paper now will be soaked and should come off easily, leaving our printed image on copper. Be sure to remove all the paper, do not forget the drilling holes. Use wooden toothpick where necessary. You should get something like this:
Now, the image is transferred onto copper and looks good. If you do not like what you see, use paper towel and acetone to clean it off and start over. To continue we will require a chemical called etchant, to remove excess copper. I'll use Ferric chloride, but there are different etchants available. FeCl3 is pretty nasty chemical, be careful.
Now we wait. In an hour or so, etchant will remove exposed copper, leaving only what's protected by toner. Here, I'm almost done:
Only few more minutes to go. Note that solution turned from orange to greenish-brown, now it's mostly CuCl2.
Once all the excess copper is gone, rinse the board thoroughly under running water. Then use paper towel and acetone to clean the toner off. Here we go:
Closeup...
Here we have our board, built at home using simple tools. In Part 3 we'll finish our board, see what else can go wrong and finally power it up.
svētdiena, 2012. gada 29. jūlijs
Building a serial add-on board (part 1)
Writing code is not everything. No program is good until it can display it's results in some way. With regular programs it is easy - you just print something and it magically appears on screen or somewhere. Bare-metal programming is different. There is no underlying operating system to handle that "magic" and we have to make it happen on our own.
And it better be simple, remember, we are practically blind-folded at this point. Trying to send something over network or display on the screen is too complicated, it would never work, since there are too many things to go wrong. There's another simple interface - we could try to blink a LED on RasPi. And then send out our messages using say, Morse code. But that doesn't look too appealing either. Fortunately, RasPi have another simple interface, called UART. Using that we can send anything we want to computer's serial port (or emulated serial port, if computer has no physical ones).
But there's a catch. We can not simply wire up Raspberry Pi to a computer's serial port. The voltages on both ends are not compatible, an we would fry RasPi instead. So we need some sort of electronics hardware in between. And that's what I'm going to build.
There's a chip designed to do exactly what we need. MAX3232 transceiver translates 3.3V UART signal to serial port (RS-232) voltage levels and back. Basically what we need is to wire up a chip and additional components to RasPi's expansion header and computer's serial port.
So, armed with MAX3232's data-sheet, RasPi expansion header's and RS-232 connector's pin-outs I fired up Cadsoft Eagle and came up with a very straight-forward schematic:
I have to confess here, that I actually designed it for chip's cheaper cousin MAX232, hence 1uF capacitors instead of 0.1uF ones. Somehow I over-looked MAX232 characteristics and thought it will also work with 3.3V. It did not! But fortunately both chips are pin-compatible, and board built for MAX232 works great with MAX3232 in it. I'll return to it...
Next we need to lay out those components on a PCB. I intend to connect my board using ribbon cable, therefore I need not to worry about RasPi's dimensions and it's connectors. In the end I came up with design for small (40x45 mm) 1-sided board:
The complete Eagle project can be downloaded here. Use it as you like, as long as you do not blame me for anything.
Now everything is set and we need to produce the board somehow. Of course I could order some manufacturer to make boards for me. But manufacturers' pricing is not very flattering near me. And shipping costs from more distant places being more expensive than boards themselves do not like me either.
From the beginning, the reason, why I designed the board to be 1-sided and used fairly wide traces and clearing, was that I intended to make one at home. Real DIY style.
How? That's up to Part 2.
And it better be simple, remember, we are practically blind-folded at this point. Trying to send something over network or display on the screen is too complicated, it would never work, since there are too many things to go wrong. There's another simple interface - we could try to blink a LED on RasPi. And then send out our messages using say, Morse code. But that doesn't look too appealing either. Fortunately, RasPi have another simple interface, called UART. Using that we can send anything we want to computer's serial port (or emulated serial port, if computer has no physical ones).
But there's a catch. We can not simply wire up Raspberry Pi to a computer's serial port. The voltages on both ends are not compatible, an we would fry RasPi instead. So we need some sort of electronics hardware in between. And that's what I'm going to build.
There's a chip designed to do exactly what we need. MAX3232 transceiver translates 3.3V UART signal to serial port (RS-232) voltage levels and back. Basically what we need is to wire up a chip and additional components to RasPi's expansion header and computer's serial port.
So, armed with MAX3232's data-sheet, RasPi expansion header's and RS-232 connector's pin-outs I fired up Cadsoft Eagle and came up with a very straight-forward schematic:
I have to confess here, that I actually designed it for chip's cheaper cousin MAX232, hence 1uF capacitors instead of 0.1uF ones. Somehow I over-looked MAX232 characteristics and thought it will also work with 3.3V. It did not! But fortunately both chips are pin-compatible, and board built for MAX232 works great with MAX3232 in it. I'll return to it...
Next we need to lay out those components on a PCB. I intend to connect my board using ribbon cable, therefore I need not to worry about RasPi's dimensions and it's connectors. In the end I came up with design for small (40x45 mm) 1-sided board:
The complete Eagle project can be downloaded here. Use it as you like, as long as you do not blame me for anything.
Now everything is set and we need to produce the board somehow. Of course I could order some manufacturer to make boards for me. But manufacturers' pricing is not very flattering near me. And shipping costs from more distant places being more expensive than boards themselves do not like me either.
From the beginning, the reason, why I designed the board to be 1-sided and used fairly wide traces and clearing, was that I intended to make one at home. Real DIY style.
How? That's up to Part 2.
svētdiena, 2012. gada 15. jūlijs
Acquired a Raspberry Pi. Now what?
I will not go into lengths to explain what a Raspberry Pi is. If you are reading this, most likely you already know.
When it finally arrived, I prepared a SD card with OS image, connected the wires and booted it up. Cool, it works! But now what?
Here I probably should explain my background a little.
I've been everyday Linux user for years. Incidentally I also run Debian on my desktop, so Raspberry Pi running the same OS, albeit on different architecture does not offer too much to explore. Of course, I could choose a different Linux distro for RasPi, but there are only minor differences between them.
Also I pretty much know, how to write code. Programming has been my hobby and day-job for even longer. Obviously I need not to learn how to program.
But I'm also interested in custom OS development and microcontroller-based DIY electronics. And Raspberry Pi offers a pretty broad field to explore at this level. Yes, that was the reason I ordered one in the first place.
So, bare-metal programming it is. Also I guess I'll build some pieces of add-on hardware along the way.
When it finally arrived, I prepared a SD card with OS image, connected the wires and booted it up. Cool, it works! But now what?
Here I probably should explain my background a little.
I've been everyday Linux user for years. Incidentally I also run Debian on my desktop, so Raspberry Pi running the same OS, albeit on different architecture does not offer too much to explore. Of course, I could choose a different Linux distro for RasPi, but there are only minor differences between them.
Also I pretty much know, how to write code. Programming has been my hobby and day-job for even longer. Obviously I need not to learn how to program.
But I'm also interested in custom OS development and microcontroller-based DIY electronics. And Raspberry Pi offers a pretty broad field to explore at this level. Yes, that was the reason I ordered one in the first place.
So, bare-metal programming it is. Also I guess I'll build some pieces of add-on hardware along the way.
0 comentarios:
Publicar un comentario