Monday 24 April 2017

FPGA's

For some reason I find these things fascinating, so I've started some more projects that I'll probably never ever finish.

There are a lot of really good projects out there for learning FPGA's and some really good stuff rebuilding retro computers.

Two I've taken an interest to are the MIST and the MEGA65.  Mainly because I am a child of the 70/80's and grew up in an age when you could really understand how your computer really worked.

Now I haven't got a clue about HDL ( VHDL or Verilog ) so I'm really just stuffing about but if you can get yourself the required hardware to run the cores here are my notes on how to build them on the latest ( at the time of writing ) Ubuntu distribution. Which surprisingly wasn't that hard to do.

This was all done in Vmware workstation 12.

Installing Xilinx 14.7 and Compiling the MEGA65 Core

1) Get Ubuntu Desktop 17.04
    https://www.ubuntu.com/download/desktop
    Do a default install.

2) Install everything in ~/Documents
    cd ~/Documents
    mkdir MEGA65
    cd MEGA65

3) Install git, libpng-dev
    sudo apt-get install git
    sudo apt-get install libpng-dev

3) Get git repositories required
    git clone https://github.com/MEGA65/mega65-core.git
    git clone https://github.com/gardners/Ophis.git
    git clone https://github.com/sasq64/cbmconvert.git

4) Compile and install cmbconvert
    cd ~/Documents/MEGA65/cbmconvert
    make -f Makefile.unix
    sudo make install

5) Download Xilinx 14.7 WebPACK
    https://www.xilinx.com/support/download/index.html/content/xilinx/en/downloadNav/design-tools.html
    As I wish to use both Windows and Linux I got this file :
         Full DVD Single File Download Image (TAR/GZIP - 7.78 GB)
   
6) Create an account and get a FREE Webpack License
    https://www.xilinx.com/registration/sign-in.html?oamProtectedResource=wh%3Dwww.xilinx.com%20wu%3D%2Fmember%2Fforms%2Flicense-form.html%20wo%3D1%20rh%3Dhttp%3A%2F%2Fwww.xilinx.com%20ru%3D%252Fmember%252Fforms%252Flicense-form.html
    OR
    Wait until after the install

7) Install Xilinx 14.7
    cp Xilinx_ISE_DS_14.7_1015_1.tar ~/Documents/
    cd ~/Documents
    tar xvf Xilinx_ISE_DS_14.7_1015_1.tar
    cd ~/Documents/Xilinx_ISE_DS_14.7_1015_1
    sudo ./xsetup
    Accept multiple license agreements
    Choose ISE WebPACK from Edition List
    Don't install cable drivers . . .
    Install to default /opt/Xilinx

    You can remove Xilinx_ISE_DS_14.7_1015_1.tar and Xilinx_ISE_DS_14.7_1015_1.tar as they are no longer needed.

8) Start ISE for the first time and add your license ( or get one )
    . /opt/Xilinx/14.7/ISE_DS/settings64.sh
    ise

9) Add settings to your .bashrc so it is available every time you log in and/or start a terminal
    cd ~
    vi .bashrc
    Add this line to the end of the file
    . /opt/Xilinx/14.7/ISE_DS/settings64.sh

10) Compile the mega65 core
    cd ~/Documents/MEGA65/mega65-core
    ./compile.sh

11) Wait . . . this took about 53 minutes in the VM.  However the resulting core worked perfectly

Installing VMware Tools


1) Add source code to the list of Source repositories
    Click on the Ubuntu Icon and type Software, then click on "Software & Updates"
    Under the TAB Ubuntu Software, check the "Source Code" checkbox
    While you are there also pick a repository close to you, preferrably one you can get free downloads from
    in the "Download from:" Dropbox.

2) Install the source code
    apt-get source linux-image-$(uname -r)
   

3) Unzip and untar the tools and run the installer picking ALL of the default options
    cp VMwareTools-10.1.6-5214329.tar.gz /tmp
    cd /tmp
    gzip -d VMwareTools-10.1.6-5214329.tar.gz
    tar xvf VMwareTools-10.1.6-5214329.tar
    cd vmware-tools-distrib/
    ./vmware-install.pl

ALTERA Quartus II 13.1 - for the MIST


0) Get Altera Quartus II 13.1
    http://dl.altera.com/13.0sp1/?edition=web

    I got the Linux version, it's 4.5GB in size.

1) You will need to install the 32 bit version of the system libraries
    sudo bash
    dpkg --add-architecture i386
    apt-get update

    apt install apt-file            # We need to find 32 bit packages
    apt-file update                #  so use apt-file to search
    apt-file seach <package>        #  for them

    apt-get install libc6:i386         # Not too sure about the first 3
    apt-get install libstdc++6:i386     #
    apt-get install expat:i386        #
    apt-get install libfreetype6:i386    # But you definitely need these ones
    apt-get install libsm6:i386        #
    apt-get install libxrender1:i386    #
    apt-get install libfontconfig1:i386    #
    apt-get install libxext6:i386        #

2) Get a really old version of libpng12 both 32 and 64 bit.
   Because you can't symlink the new version and have it work as the Qt library it uses looks
   at the version number in the library.

    http://packages.ubuntu.com/xenial/amd64/libpng12-0/download
    http://packages.ubuntu.com/xenial/i386/libpng12-0/download

    dpkg -x libpng12-0_1.2.54-1ubuntu1_i386.deb outfiles
    cd outfiles/lib
    cp * /usr/lib/i386-linux-gnu/

    dpkg -x libpng12-0_1.2.54-1ubuntu1_amd64.deb outfiles54
    cd outfiles/lib/x86_64-linux-gni
    cp * /usr/lib/x86_64-linux-gnu/
   
3) Make a directory and untar the installer
    mkdir Altera
    cd Altera
    tar xvf Quartus-web-13.1.0.162-linux.tar .

4) Edit the setup file and change the first line From
    #!/bin/env bash
   To
    #!/bin/bash

5) Open a new terminal
    xhost +                  # Not sure about this as the graphical installer didn't work anyhow
    sudo bash
    ./setup.sh

6) It will go to the command line installer
    Press Enter lots
    Then hold down Enter for the agreement
    Then choose Y
    Then choose a new directory : /op/altera/13.1
    Then choose the defaults ( basically install the Free stuff )
    Wait . . .
    Do defaults

7) Start Quartus II
   Now both versions run:
    cd /opt/altera/13.1/quartus/bin
    ./quartus        # for 32 bit
    ./quartus --64bit    # for 64 bit

Compile the MIST C64 Core

1) Download and Compile C64 core for mist
    cd ~/Documents
    mkdir C64
    cd C64
    git clone https://github.com/sorgelig/C64_MIST.git

2) Start Quartus II
    Open Project and Compile

    It takes about 3mins 41 secs

    ~/Documents/C64/C64_MIST/output_files/C64_mist.rbf




Saturday 18 March 2017

Cutting code . . .

Cutting code

Probably the part I like least about this project, cutting code.  I started off my career in IT writing bits of C for spatial data manipulation and server software on Unix, but never got into UI and Windows programming, in fact I pretty much hate it.  Which is why I'm a database admin/programmer now I guess any I digress.

Finding example code to do what I wanted was surprisingly hard.  I did find this little gem but it was in Visual Basic from Hackshed.  However it did have Bluetooth code that works straight out of the box ( well after getting the baud rate right ).

With a few little tweaks I managed to get it up and running but as I have no clue how to write VB I got a little stuck on threading and adding bits and pieces to the interface.  Luckily I have proper Windows programmer friends who could had it for me.

So the interface from Hackshed was modified to do the following.

* Drive both motors from -255 to +255. 0 being off.
* Read the RFID reader output.

So currently I'm waiting for my friend to send me back a new version that he converted to C# and added some better handling between the Arduino and the PC.

So what is my aim here.  Well I want the Arduino to just be a relay, I don't want it to have any smarts at all.  The C# interface will basically set and keep track of speed and also the RFID tags it passes over.

Hopeful Operational Theory

 RFID tags being uniquely numbered means that I can use them for location and also function.

The interface will store each tag in a database.  New tags will request the user for details.

Each tag will be used to add location and build a track map using linked lists.

Function Tags will initially be for the following.

1) Reverse for end of track
2) Stop signals ( start and stop ).
3) Speed signs ( for slowing and speeding around turns etc ).
4) Time stop delay ( for stations stops ).

I don't have anything for track switches.  There is commercial product for this so I might use that as it looks nice,

New nodes should be added as they are encountered and placed in the correct location in the list.

Location Tags will identify trains on a portion of track.   This should enable the following.

1) Location
2) Direction ( after passing two tags )
3) Collusion detection and management ( either by right of way or using Stop Signals ) for multiple trains.

Don't hold your breath

Well it's taken me about two years to actually get it to this point, because there is just waayyy too much other cool stuff to do and I want to do it all.

Besides the RFID is a bit flakey so we'll see how it works in practice once I get the new code from my mate ( and I put in all my peripherals instead of his ).

Fun with RFID

Fun with the ID-12LA

So looking at the sensor tech out there that people have implemented, I'm really not a big fan of all the wires that people are using to detect where the train is.  RFID probably isn't the best solution but I think it'll suit my needs a little better . . . if I can get it working.

So the module I got was the small ID-12LA from Sparkfun.  I Initially bought the starter kit with the USB serial reader and a few cards, but save yourself some cash and just get the reader.

You should also get the RFID Reader Breakout Board and also some standard pin headers ( 0.1" ) and some xbee pin ( 2mm ) headers as well.

Ok next step . . . ignore the pinout example on Sparkfun's website . . . it is incomplete, to the point of being frustratingly useless.

 I eventually found this on Instructables and even added my own two cents worth in a comment for clarity and emphasis on following Part 2 of the how to properly.

I was getting the FFFFFFF issue and other intermittent read issues.
The only thing that fixed it was to follow step 2 correctly with the wiring.
Ensure that you are using the +5V and NOT +3.3V
Tie pin 1(GND) and 7(FORMAT) together and connect to Ground
Tie pin 2(RES) and 11(+5V) together and connect to +5V
Pin 9 (D0) goes to RX on the Arduino
Pin 10 (LED) should go to an appropriate resistor, LED then Ground.

However a picture is worth a thousand words so here is my break out board with wiring.  The beauty of this wiring is that you only need to use one connector and three wires.



The wire I got was single core with insulation, so it was easy to solder and didn't cause a short.

I soldered all of the headers on first.

Once complete I soldered ( re-flowed ) the BLACK wire from GND to FORM.

There is a spare BLACK wire that goes from GND to nowhere, you don't need that one, it's just in case I wanted to solder a led and resistor to the READ pin for read notifications, but I don't want to now as it was only for debugging.  Also when the card/button is read there is a blue led on the MM v2 that will light up.

I then soldered ( re-flowed ) the RED wire from RES to VCC.

I crimped up the 6 Pin connector with three wires for VCC ( +3.3V ) , D0 ( Digital Sense Pin ) and FORM ( Ground ).


Wiring up the MM v2 to PF

Wiring up the Micro Magician v2 to Power functions

So with a little help from my multimeter and this website http://www.philohome.com/pf/pf.htm
I managed to wire up the PF battery box and motor successfully.

From Philos Website this diagram showed everything required to hook it all up.


Resulting in this


The battery is connected using the outside wires.  The motor using the inside wires.

It should be noted that the C1 and C2 wires on the battery box are wired in and produce 9V, the orange switch switching positive and negative, do don't use them.  The outside wires remain the same polarity regardless of the setting of the switch.

Now to put it in the train!!

Arduino and LEGO Power Function Trains

Arduino and LEGO Power Function Trains

One of my many passions is LEGO, it is awesome.  In recent years after being totally demoralized by the unending release and re-release of the Star Wars product line, I was extremely excited to see the new Power Functions Lego Train Line.  Like probably a lot of Adults I was a little disappointed in the use of Infrared controllers and the limitations that they brought.

So like many LEGO train enthusiasts out there I started looking at Arduino and trying to integrate the Power Functions nicely into that.  After buying a few Ardunio boards and playing with them with breadboards they all had pretty bad problems with them that didn't make them ideal to run my trains.

Basically the issues I had were the following:

Too Big - Too many Shields
The trouble with nearly all boards and systems out there is that in order to get the functionality you need you need to either have a really massive board or lot of Sheilds.  So Shields are those add on boards that add functionality like battery power, motor drivers, RFID readers and wireless connections.  Even the tiny Ardunio clones needed a LOT of boards to get the functionality I needed and even then the Power issue persisted.

Power
This was probably the biggest bug bear and road block.  Pretty much every single system required that you have two power supplies one for the Arduino and one for the Motors.  I looked at building my own boards with Linear Power converters and H-Bridge chips and in the end I ended up with a massive Breadboard of despair.

The Savior, the DAGU Micro Magician v2

So I almost gave up on the whole thing until I came across the DAGU Micro Magician v2.  This wondrous little board packs in a massive amount of functionality into a tiny board.  So what makes this board so good?

Power : One battery in 3 supplies out!
This little board only requires ONE power supply up to 9 volts.  This is awesome as the trains run with 9 volt battery packs this is perfect.  Also perfect is the board provides three different power sources on the board, 3.3 volts, 5 volts and the Battery Voltage from just the one external power source.

Built in H Bridge Motor driver
There are two motor drivers built into the board.  So you can drive the train motor and add some other movement functionality ( like doors ) from the one board.

A myriad of Analog and Digital pins
There are plenty of programmable pins to add all sorts of additional functionality.

Bluetooth functionality via the tiny add on HC-06 board
Using the specially placed serial port on the board you can lay flat the HC-06 blue tooth board right on top of the DAGU board.

On top of that there is also a builtin accelerometer and also an Infrared receiver.

Once it's all together, it's tiny and very functional!


So you can see the Micro Magician v2 compared to the Lego Battery Box.  The on screen configuration is :
* 1 x motor ( out of two, although I do have both working in the VB and the sketch code ).
* 1 x HC-06 Bluetooth adapter ( the green board in the middle ).
* 1 x battery pack ( I used while building and testing ) 4 x 1.5 volt.
* 1 x RFID reader ( It's an ID-12LA from Sparkfun, but that's for another time ).

I have all of the LEGO PF cables cut up and crimped, I just need to test them first to make sure that I'm not going to blow anything up ( maybe add a diode to the power cable ).

Where did it get it?  Well being Australian I got it from Robot Gear - DAGU MMv2.  You know, buy local and all that jazz!


Perils of Wizdom . . .

Welcome to the Perils of Wizdom . . .

Hopefully my many never to be finished projects will appear here in all states of disrepair.

Enjoy my total inability to stick to just one hobby.