Sunday, 26 July 2015

[Ada Example #1] Statically linking C code into an Ada program

The following is intended to be a simple "hello world" style example of how to call a C function from Ada code.  I am using Linux (Debian Jessie, with the latest and greatest GCC v4.9.3 / Gnat v4.9.2 installed).

The C code and the Ada code are compiled separately and linked statically afterwards.

triple.c contains the C function I want to call from Ada...
#include <stdio.h>

int triple(int value)
  return value * 3;

test_prog.adb contains the Ada main program that calls the triple() function...
with Ada.Integer_Text_IO, Ada.Text_IO;
use Ada.Integer_Text_IO, Ada.Text_IO;

procedure Test_Prog is
   type SInt32 is
      new Integer range -2_147_483_648 .. 2_147_483_647;

   function Triple (value : SInt32) return SInt32;
   pragma Import (C, Triple, "triple");

   A : SInt32 := 123;
   B : SInt32 := 0;
   B := Triple (A); --  call the C function
   Put ("B is ");
   Put (Integer (B));
   Put_Line ("");
end Test_Prog;

        gcc -c triple.c
        gnatmake -gnaty -c test_prog.adb
        gnatbind test_prog.ali
        gnatlink test_prog.ali triple.o

        -rm -f *.ali *.o b~*

It is also possible to do this the other way around - calling an Ada function from a main program written in C, but I suspect that this is a less common method.  The Ada program calls the C function and prints the correct result of 369.  Interestingly, the pragma Import lets you change the capitalisation on "Triple" which allows normal naming conventions in both C and Ada to be preserved.

Wednesday, 18 March 2015

Casio 4723 (WVQ-550) wrist watch takes a CR1616 battery

Casio 4723 model watches (Wave Ceptor WVQ-550) require a CR1616 Lithium 3V coin cell.

As that information seemed to be entirely absent from the internet I have secreted it here for all eternity.

Get one off ebay and install it yourself or pay £10 for the nice person at H Samuel to do it for you. Either way the waterproofing rating will no longer be reliable.

A great watch, but eats a battery in 3 years though. Meh.

Friday, 27 February 2015

Propeller - high frequency output using CTRA

Here's a quick example of how to provide a high frequency output on a Propeller's GPIO pin.

Frequencies far above the actual CPU's clock speed can be generated with excellent resolution. In experiments I have found that 180MHz is achievable, although the signal degrades from a square wave to an approximate sine wave with reduced amplitude when measured directly on the Prop's pin.
The Propeller cog can set up this output and continue performing other tasks as the frequency generation doesn't take any run-time once initiated. The code below outputs a square wave at Pi megahertz (3,141,593Hz) by setting up Counter A in PLL single ended mode (00010) and setting FRQA to the value 10541436. This value is calculated using this equation...


So, working this out using a standard 5MHz crystal, the highest PLL multiplier (pll16x) and a PLLDIV value of "divide by 1"...
10541436 = (3141593 * 4294967296) / ((5000000 * 16 * 16) / 1)
Setting the FRQA register to this value causes the Counter A subsystem in the current cog to output the desired frequency.
Each cog has 2 such counters, each of which is independent of the others. So if you wanted to you could output 16 different frequencies from the same chip while still using zero CPU time. Pretty cool.

  _clkmode = xtal1 + pll16x      '80Mhz cpu speed
  _xinfreq = 5_000_000

PUB main
  coginit(0, @ASM_ENTRY_POINT, 0)

              org       0
              or        dira, HF_PIN        'set pin as output
              mov       FRQA, FREQ          'set up the desired frequency
              mov       PHSA, #0            'set the phase back to 0 (optional)
              mov       CTRA, CTRA_MODE     'begin output of the HF signal

              waitpeq   $, #0 'sleep forever

HF_PIN        long      |< 15          'output on pin P15 (physical pin 20)
FREQ          long      10541436       '3.141MHz (see calculation)
CTRA_MODE     long      %0_00010_111_00000000_000000_000_001111


Turning the counter on and off in code will let you create a simple CW signal at your chosen frequency, if you have a Ham radio licence of course.

Changing the value of FRQA while the signal generation is running allows you to modulate the signal's frequency on the fly (as in FM radio)!

Saturday, 2 August 2014

Electric Imp - Weather Display

Found a much better use for the 2049 board - a simple, cheap weather forecast display...

This picture shows an Electric Imp device delivering a 4x4 pixel image to the 2049 board from a server on the west coast of the USA...

The 4x4 pixel image itself is simply a debug image that proves that the system works.

The next step is to harvest weather data from the Met Office (Datapoint API) and have the server Agent convert this data into a series of animations for the 4x4 display to show.

By a weird coincidence the 2049 board is a perfect size for pairing up with an Electric imp dev board ("impee-april rev3").  That was totally unplanned!

The 2049 board uses about 60mA when running normal animations.  According the the documentation, the Electric Imp module uses no more than 400mA at any time.  So I can therefore simply bus the 2049 board and the EI board power  rails together and it will always be within spec for operation from a standard USB2.0 cable.

NOTE: the piece of paper in the above photo is only there because I found it very difficult to meter the chot correctly without it.  I guess the camera can't cope with point-light sources.  *shrug*

Tuesday, 29 July 2014

2049 Hardware Complete!

This is the finished board running the firmware that I originally wrote as an Xmas tree decoration...

2049 works

Looks better in real life than this photo would suggest :)

Wednesday, 23 July 2014

Knocking it out of the (osh) park!

My "2049" game PCBs have arrived, a mere 17 days after I submitted the Gerber files!

As usual they look awesome.  The quality is top-notch and the gold plated copper on dark purple with white silkscreen is really eyecatching and distinctive.

Here's a picture to look at with your eyes...

oshpark (links to viewer)

I was particularly impressed with the large complex pattern I had put on the bottomside silkscreen layer. To create that I did a basic pattern in MSPaint, took that into GIMP, rotated and blurred it heavily, then desaturated it and applied the "newspaper"filter to halftone the pattern into solid blocks of white. Then took that into Inkscape and traced it into vector format. Saved it as a WMF file and copy/pasted it into Diptrace.

Sounds complicated but it went very smoothly and took about 10 minutes.  The finished boards (to both OSHPark's and Diptrace's credit) look EXACTLY like the Diptrace 3D preview.

Super happy with these boards, and I cannot recommend OSHPark highly enough.  Spectacular job guys.

Thursday, 17 July 2014

Rocket Surgery!

I am a late-comer to Kerbal Space Program.  For shame.

Been playing it for a week or two now and just now I managed to successfully dock two hand-built vessels in LKO (low Kerbin orbit).

To those unfamiliar with KSP, this may sound like a trivial accomplishment.  But no.  To do such a thing requires a lot of thought and sadly a lot of stout and stupid courageous Kerbonauts.  Bless them all.

I now have a fully functional (and extensible) space station in orbit and thus a platform to refuel and launch future long-distance missions from.

Here's a couple of screenshots from my mission...

Before docking (the main space-station vessel is on the left, the other ship is the "shunting wagon" which will perform refuelling duties)..

And a NASA press-corps image from after the docking process (which was fraught with stress and much fine-pitch thruster action)...

If you are a science nut like I am and love to learn about orbital mechanics, you should have a go at KSP.

Oh, and forget about going outside ever again if you do. :)

Sunday, 6 July 2014

"2049" game PCB goes out for manufacture!

The 2049 game (as I've now decided to call it) is the 3rd PCB job that I've sent to OSHPark.

6 boards for less than £16 with free international shipping.  In the past the boards have been in my hands within 5 weeks of submitting the designs.  That is *fast* turnaround.  At work I'm used to 8-12 week turnaround times for commercial boards.  5 weeks without having to pay for accelerated service is spectacular.

The second best thing about OSH Park in my opinion the awesome user interface on their website.  Upload your zip file full of Gerbers and drillfiles and the website processes it while you wait and then shows you their own interpretations of your Gerber files.  You can review them and then decide whether or not to continue or go back and fix any problems with your design.  Absolutely brilliant system.

Here's a couple of screenshots of the finished board as sent to OSHPark just now...

Wednesday, 2 July 2014

2048 -- Sketchup Pro nails it

It's great when things just work unexpectedly.

Just now I threw together a rounded corner version of the board outline I want to use for the 2048 game in Sketchup Pro.  That went without a hitch.

Imported it into Diptrace.  Also without a hitch.  Hmm.

Surely the copper pours will screw up and not understand what I'm trying to do.  Turns out it's all fine.  I'm seriously impressed with Diptrace. :)

Took about 120 seconds to re-make the board outline with 3mm filleted corners from a DXF generated by Sketchup and brought into Diptrace.  Splendid.

2048 -- Nearly there!

Finished the routing for the most part.  Still need to ensure my DRC is compatible with the OSH Park preferred settings.  All that is left is to add some embellishments to the rear silkscreen.  I'll add the Open Source Hardware logo before I send off the Gerber files to Laen.  Also I'll round off the corners, for ergonomics' sake :)