Wednesday, February 6, 2013

Kernel and Filesystem Setup for Embedded Devices (Glomation 9g20i)

NECESSARY ITEMS
First, you will need a few things
  • USB Flash Drive which will be erased, then set up with the Linux filesystem to put on the board
  • Simple three wire Serial to Pin cable made with the instructions below
  • Variable power supply 
  • Glomation 9G20i board
  • Null modem serial cable to test final flashed board for proper operation
Instructions to make the cable - via Glomation Tech Support.
The serial connection for the debug port should be as following:

Serial port on PC side                  GESBC-9G20 P0
Pin 2           ------------------              Pin 2
Pin 3           ------------------              Pin 1
Pin 5           ------------------              Pin 3

Regards,

Glomation Customer Support


Basically, pin 3 on the 9G20 is common ground, and RX/TX are swapped coming from the PC.

Once this cable is made, hook it into the serial connection on the host PC, and setup minicom(or other serial program) for 1152000, 8N1, no hardware or software flow control. Pin one of Port P0 on the board should have a 1 printed next to it to indicate which wires go where. Turning on the board should go through uBoot and bott into the default kernel and filesystem. If this is enough - congratulations! Otherwise read onward for more nitty gritty details


THE ULTIMATE SETUP
  1. Build kernel 2.6.30 (or some other compatible kernel)
  2. Create a rootfilesystem and ramdisk .img file
  3. Copy kernel 2.6.30 to internal memory
  4. Flash .img filesystem onto USB flash drive
  5. Load custom filesystem, and settings to start this code on boot.
  6. Set uBoot to boot into the USB filesystem
  7. Copy filesystem from the stick to internal storage
  8. Reset uBoot settings to boot from internal storage

BUILDING THE KERNEL
If you wish to compile a kernel from source, this is what I did.
YOU NEED 32 BIT COMPATIBILITY LIBS OR A 32 BIT INSTALL TO DO THIS! OTHERWISE YOU WILL GET WEIRD (FILE DOES NOT EXIST) ERRORS
apt-get install libc6-i386 lib32gcc1 lib32z1 lib32stdc++6 ia32-libs
sudo apt-get install uboot-mkimage
sudo apt-get install ncurses-dev


Download kernel 2.6.30 from the following website and both patch sets (at91*.patch, .exp.4)
http://www.at91.com/linux4sam/bin/view/Linux4SAM/LinuxKernel#AT91_Linux_kernel_sources_summar

You will also need to download the compiler from
http://www.glomationinc.com/support.html

I personally used the toolchain from Generic-arm_gcc-4.2.3-glibc-2.3.3.tar.bz2, though the others may work as well.


KERNEL PATCHING AND BUILD
Put the patch files for the kernel in the root of the kernel tree (referred to here as $\$$KERNEL_ROOT), and make sure the gcc-arm executable is at the FRONT of your $\$$PATH variable
patch -p1 < 2.6.30-at91.patch
for p in 2.6.30-at91-exp.4/*; do patch -p1 < $\$$p ; done
make ARCH=arm CROSS_COMPILE=arm-unknown-linux-gnu- at91sam9g20ek_defconfig
make ARCH=arm CROSS_COMPILE=arm-unknown-linux-gnu- menuconfig
----> Device Drivers
              ----> GPIO Support
                      ----> Press spacebar to enable /sys/class/gpio/
              ----> I2C Support
                      ----> Change I2C Device interface from <M> (module) to <*>
                     
make ARCH=arm CROSS_COMPILE=arm-unknown-linux-gnu- uImage


Once the kernel is compiled, it can be found at $\$$KERNEL_ROOT/arch/arm/boot/uImage
The following commands can also be used to open different visualizations of the same configuration
make ARCH=arm CROSS_COMPILE=arm-unknown-linux-gnu- config
make ARCH=arm CROSS_COMPILE=arm-unknown-linux-gnu- xconfig

 
BUILDING KERNEL MODULES
If later you need a kernel module (.ko) to put on the board, building a single module is fairly easy. For example, to build i2c-dev.ko
make ARCH=arm CROSS_COMPILE=arm-unknown-linux-gnu- drivers/i2c/i2c-dev.ko 


To try to compile all modules, use:
make ARCH=arm CROSS_COMPILE=arm-unknown-linux-gnu- modules

If the module still won't build, and fails to build silently instead of barfing a compilation error, check that the config is set for module support (CONFIG_I2C=m) rather than builtin i2c module support (CONFIG_I2C=y). This can be set in the curses configuration environment seen earlier, or directly in the kernel config file $\$$KERNEL_ROOT/.config


CREATION OF SYSTEM FILES
I took the install of debian-lenny (both the base ramdisk.gz file and the debian-arm-linux.tar.gz) from the Glomation website (http://www.glomationinc.com/support.html) and chopped out many things, primarily focusing on areas with high disk usage. manpages, /var/cache, /usr/share/, and locales were all removed to make space. Remember, the image needs to be combined with a ramdisk to work properly!

Have the ramdisk file unzipped THEN copy the base distro on top - doing it the other way will make the ramdisk files overwrite your configs and the system will not boot properly!

To look at the size of files, try using the command
sudo du -h --max-depth=0 *

I wiped out the following to reduce size:
var/cache/apt/
var/lib/apt/lists/ftp.us.debian

usr/share/locales *almost everything, used this command: sudo rm -rf $(ls | grep -v en | grep -v uk)
usr/share/info
usr/share/man
usr/share/man-db
usr/share/doc


Also had to change a few things to get everything working
mkdir -p var/cache/apt/archives/partial

Change etc/apt/sources.list to use archives.debian.org instead of ftp.us.debian
Edit /etc/network/interfaces to contain the following text:
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
hwaddress ether (MAC address of your choice).


It is important to note that the first octet (XX:YY:YY:YY:YY:YY) of your MAC address must be even!


CREATING A .IMG FILE
Now that there is a rootfilesystem and a ramdisk, we need to create a .img file for this filesystem. Begin by creating a blank file, with no filesytem or structure with the following command:
dd if=/dev/zero of=root.img bs=1024000 count=128

This will create a 128 MB blank file - for different file sizes change the value for count. Now that a blank file has been created, we need to put a filesystem in place over the top of that blank structure using mke2fs - I choose to use ext2 due to its compatibility with most linux distributions.
mke2fs -t ext2 root.img

Now mount this file to a directory (such as /mnt) using
mount -o loop root.img /mnt

Copy all the files created during the previous step to /mnt, then unount using
umount /mnt

Finally, zip the image to create a bootable initrd.img
gzip -c root.img > initrd.img

http://www.linuxquestions.org/questions/linux-newbie-8/make-a-img-file-for-custom-livecd-dd-also-how-to-make-your-system-very-small-441412/


SETTING UP USB DRIVE WITH EXISTING .IMG FILE
Now, perform the next steps on the computer of your choice. You will probably want that computer to have a serial connector, but USB to Serial adapters may work. Run the command
dmesg
and look for output with [sdx], where x is some letter between a and z. It will sometimes take a second for the usb to be recognized, try dmesg again if you don't see it the first time.

WARNING! The next step will remove all the files from the flash drive - make sure there is nothing you hold dear/
sudo mkfs.ext2 /dev/sdx1
sudo dd if=rootfs.img of=/dev/sdx1
This will take some time, but once it is done there should be a bootable linux filesystem on your flash drive


LOAD UP
To load the kernel, you will need the host PC to be running a TFTP daemon. There is information at the link below on how to set one up
http://www.davidsudjiman.info/2006/03/27/installing-and-setting-tftpd-in-ubuntu/

Once tftpd is running, put the kernel image in the tftpd directory on the host PC (in my case, /tftpboot), then enter uBoot on the microcontroller by pressing Enter at the "Press any key to stop autoboot". Once in uBoot, run the following commands to load the kernel into temporary memory, where SERVER_IP should be replaced by the IP of the computer running tftpd.
The uImage file used below can have any name on the tftp server,  but the name after "t 0x21000000" must match the filename on the server.
set serverip SERVER_IP


You will have to CTRL-C the t command - the board does not have an IP yet! But when you dhcp it will get the image for you also.
t 0x21000000 uImage
dhcp

Once the kernel image is loaded into temorary memory, run these commands to erase the existing kernel, and copy in the new.
nand erase 0x100000 0x200000
nand write.jffs2 0x21000000 0x100000 0x200000


Plug the USB drive into the board, and set the boot device to USB using this line:
set bootargs console=ttyS0,115200 root=/dev/sda1 rootfstype=ext2 mtdparts=atmel_nand:1M(bootloader),3M(kernel),-(rootfs) rootdelay=10
 
If you want to erase the existing filesystem:
nand erase 0x400000 0x7c00000

Now boot into the USB filesystem
boot

Use these commands on the board to copy from the USB drive to the jffs memory
mount -t jffs2 /dev/mtdblock2 /mnt
cd /
for i in bin boot dev etc home lib media opt root sbin selinux srv usr var; do cp -a $\$$i /mnt; done
for i in mnt proc sys tmp; do mkdir /mnt/$i; done


Finally, change bootdevice back to /dev/mtdblock2, and you can also remove the rootdelay for faster boot. Use printenv to see what uBoot settings are currently being used. Remember to saveenv if you make changes.

To boot from jump drive
set bootargs console=ttyS0,115200 root=/dev/sda1 rootfstype=ext2 mtdparts=atmel_nand:1M(bootloader),3M(kernel),-(rootfs) rootdelay=10

To boot from flash once FS is installed, and see output over debug serial
set bootargs console=ttyS1,115200 root=/dev/mtdblock2 rootfstype=jffs2 mtdparts=atmel_nand:1M(bootloader),3M(kernel),-(rootfs)

Final configuration to see output and login, etc. over regular serial connector
set bootargs console=ttyS1,115200 root=/dev/mtdblock2 rootfstype=jffs2 mtdparts=atmel_nand:1M(bootloader),3M(kernel),-(rootfs)


FINALIZATION OF OUR REALIZATION
Next it will be necessary to boot into the device, connect ethernet, install dropbear(ssh client), and set ssh and login passwords.
apt-get update
apt-get install dropbear


I had some issues with serial login using /etc/shadow and ssh using /etc/passwd or vice-versa. Quick solution was to set both the same.
echo -e "password\npassword" | passwd
echo root:password | chpasswd


Make sure the /etc/inittab has a getty setting for both debug serial and for actual serial
T0:23:respawn:/sbin/getty -L ttyS0 115200 vt100
T1:23:respawn:/sbin/getty -L ttyS1 115200 vt100



USING APT-GET ON THE BOARD
There are two ways to apt-get when the main filesystem is jffs2. One way is to mount ram as a tmpfs
mount -t tmpfs none /var/cache/apt
mkdir -p /var/cache/apt/archives/partial
apt-get update ; apt-get install <package-name>
umount /var/cache/apt


Unfortunately, this is not always big enough. I was forced to mount a USB stick as /var/cache/apt
mount -t ext2 /dev/sda1 /var/cache/apt
mkdir -p /var/cache/apt/archives/partial
apt-get update ; apt-get install <package-name>
umount /var/cache/apt



ENABLING i2c
For communicating with other hardware, a protocol known is i2c is often used. There is an excellent walkthrough for another Glomation board below, but the basics of it for me were to compile the kernel with builtin i2c support, then create the i2c device using mknod /dev/i2c-0 c 89 0
One can install i2c-tools using apt-get to use i2cget, i2cset, and i2cdetect for troubleshooting.
For example, i2cdetect 0x00 will show all i2c devices connected to i2c-0.

Walkthrough with another board
http://www.linuxquestions.org/questions/linux-kernel-70/i2c-character-device-file-not-created-in-linux-2-6-24-on-at91sam9263ek-677375/

Simple i2c code
http://e2e.ti.com/support/dsp/davinci_digital_media_processors/f/100/t/8508.aspx

Further links on GPIO and i2c
http://glomation.net/smf/index.php/topic,1335.0.html
https://www.ridgerun.com/developer/wiki/index.php/How_to_use_GPIO_signals
http://glomation.net/smf/index.php?action=printpage;topic=27.0
http://glomation.net/smf/index.php?action=dlattach;topic=27.0;attach=20

Monday, February 4, 2013

LyX and Latex Tips

INSTALLING THE LATEST LATEX
The simplest way is to download this file for Linux
http://mirror.ctan.org/systems/texlive/tlnet/install-tl-unx.tar.gz

Unzip, then run the install-tl file. This will do a network install of the latest version of latex.
http://www.tug.org/texlive/quickinstall.html

FLOAT FIGURES, APPROXIMATELY WHERE YOU WANT THEM
\usepackage{float}
    \begin{figure}[H]
        % figure stuff
    \end{figure}



ADDING LINUX LIBERTINE FONT TO UBUNTU
http://sourceforge.net/projects/linuxlibertine/files/linuxlibertine/5.3.0/

CAN'T FIND pdflatex.fmt!
sudo fmtutil --all
http://ubuntuforums.org/showthread.php?t=1474358

INSTALL NEW TEX .CLS FILE FOR CV
Download LaTeX and LyX .zip files from this link.
Unzip the xetexCV.zip file, then copy the resulting LaTeX file xetexCV.cls to
~/texmf/tex/latex/local/, then run sudo texhash ~/texmf (in Ubuntu 12.10). Now unzip the xetexCV-LyX.zip file, and copy the xetexCV.layout file to /usr/share/lyx/layouts/
Open LyX, and then select Tools->Reconfigure. Restart LyX and there should now be a document style for curriculum vitae (xetexCV)
To get the compatible font (to avoid hacking xetexCV.cls), go to this link and this link, downloading Fontin and Fontin Sans fonts. Unzip the ensuing downloads, and copy the .ttf and .otf files to /usr/local/share/fonts by running sudo cp *.*tf /usr/local/share/fonts. Check that the fonts were installed correctly with  fc-list | grep -i fontin . To view the file, simply go to View->Other Formats->PDF(XeTeX)
http://tex.stackexchange.com/questions/27659/how-to-use-downloaded-fonts-with-xetex-on-ubuntu
http://www.exljbris.com/fontin.html
http://tex.stackexchange.com/questions/1137/where-do-i-place-my-own-sty-files-to-make-them-available-to-all-my-tex-files
http://stefaanlippens.net/customLaTeXclassesinLyX
http://blog.oak-tree.us/index.php/2010/05/19/latex-cv-part4
http://blog.oak-tree.us/index.php/2009/11/25/latex-cv-part1

INSTALL KOMA SCRIPT
If you installed texlive-full it is already there! Add this to your .bashrc
export TEXINPUTS=.:/usr/share/texlive/texmf-dist/tex/latex/koma-script:

If you leave off the trailing colon bad things (like not searching the system directories!) happens
You may also need to add
\usepackage[english]{babel}
above
\begin{document}
in your .tex file

EXCELLENT TUTORIAL ON COVER LETTERS AND CV
http://stefano.italians.nl/archives/55
http://stefano.italians.nl/archives/50

SOME GREAT SHORTCUTS
http://wiki.lyx.org/Tips/FrequentlyUsedShortcuts

ADD A CV IMAGE
Using the CV(xetex) document, one can add
\cvimage{<FULL PATH TO IMAGE>}
to the LaTeX preamble (Document->Settings->LaTeX Preamble)

ADVANCED TRICKS
http://www.youtube.com/watch?v=bUyQcG8eIwA
http://www.dedoimedo.com/computers/latex-lyx-tips-more.html

FONT PACKAGES MISSING
In Ubuntu, sudo apt-get install texlive-full will install basically every package, which will usually eliminate font package errors.

ADD MULTIPLE LINES TO CASES IN LYX
Pressing CTRL+Enter when using cases in LyX will add a new line.
http://superuser.com/questions/76829/using-the-cases-environment-in-lyx

GOOD VIDEO TUTORIALS
http://www.youtube.com/watch?v=sJpfyydhAzo
http://www.youtube.com/watch?v=HvhdLJ0C6pk&feature=watch&lr=1

USING \LARGE
\large
\Large
\LARGE
\huge
\HUGE

To do only part, do {\large my_text}, as typefaces apply to all following text
http://www.personal.ceu.hu/tex/typeface.htm
http://tex.stackexchange.com/questions/7312/how-to-use-large-and-its-variants

COMMON MATH SYMBOLS
\, is a small space
\: is a medium space
\; is a large space
\approx is $\approx$
\equiv is $\equiv$ 
\sim is $\sim$
\sum\limits_{i=1}^n is $\sum\limits_{i=1}^n$
\cdots is $\cdots$
\ddots is $\ddots$
\ldots is $\ldots$
\vdots is $\vdots$
\mathcal{N} is $\mathcal{N}$
\left and \right are $\left|and \right|$
To do only one bar, do \left or \right in a \left \right pair on a period i.e.
\left. \right is $\left. \right|$

http://tex.stackexchange.com/questions/6969/symbol-for-gaussian-distribution
http://everythingyouforgetaboutlatex.blogspot.com/2008/11/another-quick-post-this-time-about-math.html
http://www.personal.ceu.hu/tex/symbols.htm
http://www-users.york.ac.uk/~pjh503/LaTeX/equations.html
http://www.personal.ceu.hu/tex/symbols.htm
http://en.wikibooks.org/wiki/LaTeX/Mathematics#Brackets.2C_braces_and_delimiters

CODE SNIPPETS
Adding code snippets
http://tex.stackexchange.com/questions/28554/is-there-a-way-to-enable-syntax-highlighting-coloring-in-lyx-for-program-listing

Thursday, January 3, 2013

Connecting to Checkpoint/using SNX in Ubuntu 12.10 64bit

GET CONNECTED, NOT INFECTED
Get snx, either by going to your Checkpoint site, clicking Manual Download for Linux, and running the bash script downloaded, or by going to the Checkpoint website listed under Links and following the instructions there.

To get snx working properly, we will need to install some prerequisites by doing:
sudo apt-get install icedtea-7-plugin openjdk-7-jre ia32-libs libpam0g:i386

To connect to your Checkpoint site, run
snx -s <your_checkpoint_server> -u <username>, where <your_checkpoint_server> is the IP (i.e. 192.192.192.66) or DNS name (i.e. checkpointserver.example.org) of the Checkpoint server, and <username> is your domain login.

Once connected, you should be able to type ifconfig, and see a new interface called tunsnx with your VPN IP. You will probably need to remember the IP of at least one computer on the other side of the VPN in order to SSH - your company/school/whatever DNS probably won't resolve to right IP when you connect over VPN. I usually ssh to a "homebase" computer on the other side of the VPN, then ssh to other places as needed.

ssh me@192.192.192.67 - YES ----> ssh me@myworkserver - YES
ssh me@myworkdestop - DOUBTFUL

To disconnect, simply do sudo ifconfig tunsnx down; sudo pkill snx
For better disconnection, use snx -d ,as mentioned by kholis in the comments - thanks!


LINKS
https://supportcenter.checkpoint.com/supportcenter/portal?eventSubmit_doGoviewsolutiondetails=&solutionid=sk65210

http://kenfallon.com/check-point-ssl-network-extender/

Wednesday, November 21, 2012

Loading Ubuntu 12.10 on a Macbook Air

INSTALLATION
To create a live USB for installation, I had to follow the instructions at the link below. The basic steps were to convert the .iso to a .img using diskutils, then dd that image onto a USB driver, which was formatted as FAT_32 in my case. I think other formatting schemes may work as well.


Update: Trying to do this from non-Mac platforms, one can try
mkfs.hfsplus to create a .dmg file. To do this, you will need to install the hfsplus hfsprogs and hfsutils packages. you can then install dmg2img to convert this to .img format by installing dmg2img.
The whole command is sudo apt-get install hfsplus hfsprogs hfsutils dmg2img 
See http://stackoverflow.com/questions/286419/how-to-build-a-dmg-mac-os-x-file-on-a-non-mac-platform for more details

Create a blank file of the size needed
dd if=/dev/zero of=/path/to/output.dmg bs=1 count=0 seek=590M
mkfs.hfsplus -v 'Description of DMG file' output.dmg

mount it, copy all files from ISO to DMG, then unmount.http://www.garrettcpa.net/dokuwiki/doku.php?id=linux:dmg

https://help.ubuntu.com/community/How%20to%20install%20Ubuntu%20on%20MacBook%20using%20USB%20Stick

Once the live USB is created, simply reboot the Macbook Air while holding the alt (option) key, then select the yellow USB UEFI icon as the boot device. Make sure to install 3rd party sources when running the installer, as the Broadcom wireless device uses a 3rd party firmware blob.

After running the Ubuntu installer, everything should be running, but there are still a few usability issues to fix. I will cover these issues under separate headings below.

XMONAD
Installing XMonad is the next crucial step for me - I don't install a wireless manager, doing wifi management with wpa_supplicant instead.

sudo apt-get install xmonad xmobar suckless-tools

Suckless-tools is a package of helpful tools for xmonad, such as dmenu. I use dmenu for launching graphical programs like firefox or
I have two example configs for a MacBook Air running XUbuntu 12.04, located at http://www.github.com/kastnerkyle/Configs .Simply put the ~.xmobarrc file at ~/.xmobarrc, and put the xmonad.hs file in ~/.xmonad/xmonad.hs

http://www.haskell.org/haskellwiki/Xmonad/Config_archive/John_Goerzen%27s_Configuration

WIRELESS
Wireless in XMonad is ...interesting. Here I will document some common wireless connectivity scenarios, and how get on the net.

Easiest - install trayer by doing sudo apt-get install trayer
Now simply do
trayer & 
then
nm-applet
to start the network manager applet in the bottom left hand corner. Simply click and connect as normal. When finished, kill trayer by doing pkill trayer
http://ubuntuforums.org/showthread.php?t=843341


TOUCHPAD
After installing Ubuntu 12.10 and configuring XMonad, things appeared pretty good. The only issue so far is that the "right click" of the mousepad is two finger click, instead of clicking in the bottom right hand corner of the touchpad. 

http://ubuntuforums.org/archive/index.php/t-1966016.html

Tuesday, November 20, 2012

Using the Symmetricom bc635PCIe card as an NTP timesource

INTRODUCTION
NTP timeservers are a crucial part of any large-scale, distributed system, especially if the components of the system are spread across multiple sites. There are many commercial and homebrew options for setting up an NTP timesource. Some are fairly simple to set up, and others (like the Symmetricom bc635 PCIe card) require a little more work.

I have recently set up an NTP server using the aforementioned card, and after all my troubles to get it set up, I decided to document the procedures used in this blog post.

PROBLEM
The default ntpd that comes with CentOS 6.2 does not have support for the BANCOMM/Symmetricom PCIe GPS card.

SETUP
Download the latest ntp source from http://www.ntp.org/downloads.html .For this tutorial, I used version 4.2.6p5

STEPS
There is a PDF from Symmetricom support that outlines most of the steps necessary to fix the driver. This list is largely a copy of their "how-to". However, I have elaborated on a few of the steps where I had issues.
  1. Install the bc635/637 driver. To do this, either run make install OR cp the windrvr6.ko and windrvr6_usb.ko files into the /lib/modules/drivers/ area, then do a depmod -a. There is a script called wdreg that will load up the driver, wdreg windrvr6. I added a call to the wdreg script in /etc/rc.local in order to get the module loading on boot, one could also add a rule to /etc/modules.conf.
  2. To verify, run the bc63xPCIcfg file that comes with the driver, from the   sample/ folder.
  3. After rebooting the computer, the sample program should still work.
  4. Copy the libbcsdk.so file from the sample/ folder to /usr/lib/ (or another library directory of your choice).
  5. Make a link to the device driver, ln -s /dev/windrvr6 /dev/btfp0 (btfp0 is the   old /dev file that ntpd looks for)
  6. If you haven't already done so, download the ntpd source code from www.ntp.org/downloads.html
  7. I used the ntp-4.6.2p5 source for this, but the 4.2.6p2 source has been used by others
  8. Extract the source, then modify the Makefile.in file in the ntpd/ folder. Find the segment that has ntpd_LDADD = $(LDADD) $(LIBOPTS_LDADD) ../libntp/libntp.a -lm @LCRYPTO@ @LSCF@, and add -lbcsdk  
  9. Run this command in the ntpd/ directory - ./configure --enable-BANCOMM --enable-linuxcaps
  10. Look at the Makefile (NOT Makefile.in). There should be a line like  ntpd_LDADD = $(LDADD) $(LIBOPTS_LDADD) ../libntp/libntp.a -lm -lbcsdk If not, add the -lbcsdk here, but know that running ./configure again will overwrite this Makefile.
  11. Edit the refclock_bancomm.c file as follows
At line 182
extern uint32_t __attribute__ ((weak)) bcReadBinTimeEx(SYMMT_PCI_HANDLE, uint64_t *, uint64_t*, uint32_t*, uint8_t*);

At line 440
  case 2:                         /* Linux/Windows, PCI, 2 32bit time words */
                        {
                            struct tm temp;
                            uint64_t btm[2];
                            uint32_t nano;
                            uint8_t dmy;
                            if (bcReadBinTimeEx(stfp_handle, &btm[1], &btm[0], &nano, &dmy) == 0)
                            {
                                msyslog(LOG_ERR, "get_datumtime error: %m");
                                return(NULL);
                            }
                            temp = *gmtime((time_t *)&btm[1]);
                            time_vme->year = (unsigned short)temp.tm_year+1900;
                            time_vme->day = (unsigned short)temp.tm_yday+1;
                            time_vme->hr = (unsigned short)temp.tm_hour;
                            time_vme->mn = (unsigned short)temp.tm_min;
                            time_vme->sec = (unsigned short)temp.tm_sec;
                            time_vme->frac = (btm[0] & 0x000FFFFF) * 1000;
                            time_vme->frac += (nano & 0x0000000F) * 100;

                            time_vme->status = (unsigned short)dmy;

                            #ifdef DEBUG
                            if(debug)
                            {
                                printf("Epoch time read from the BANCOMM card -> %ld\n", btm[1]);

                                printf("Decimal time read from the BANCOMM card -> Day:%d Year:%d H%02d:M%02d:S%02d.%06lu%d Status:%d\n",
                                temp.tm_yday+1,
                                temp.tm_year+1900,
                                temp.tm_hour,
                                temp.tm_min,
                                temp.tm_sec,
                                btm[0],
                                nano,
                                dmy);

                                printf("Decimal time read into time_vme -> Day:%hu Year:%hu H%hu:M%hu:S%hu.%lu Status:%hu\n",
                                time_vme->day,
                                time_vme->year,
                                time_vme->hr,
                                time_vme->mn,
                                time_vme->sec,
                                time_vme->frac,
                                time_vme->status);
                            }
                            #endif
                            //tvme_fill(time_vme, btm);
                        }
                        break;

One can remove all the commented lines, as well as the lines surrounded by
#ifdef DEBUG, but I have left them here for completeness.

Now run make. This should build a ntpd executable that will work with the libbcsdk.so library in order to read time from the Symmetricom PCIe GPS. If anyone knows how to handle the nanoseconds field, please let me know and I will update this post!

MODIFYING NTP.CONF
I then had to modify ntp.conf to use the device

# No restrictions on loopback
restrict 127.0.0.1

#Allow systems to sync
restrict 10.0.0.0 mask 255.0.0.0 nomodify notrap
restrict 192.168.0.0 mask 255.255.0.0 nomodify notrap

#Setup GPS PCIe Clock
restrict 127.127.0.0 mask 255.255.0.0
server 127.127.16.0 prefer mode 2 iburst minpoll 4 maxpoll 4

#Local clock source
#server 127.127.1.0
#fudge 127.127.1.0 stratum 8

driftfile /var/lib/ntp/drift
authenticate no


Here the server 127.127.16.x is a special subnet which refers to the Symmetricom(or BANCOMM) hardware clock when ntpd is compiled to support this radio.

The mode keyword is specific to each hardware radio. For Symmetricom, I have been unable to find information on what this mode means, but for Meinberg
see http://www.meinbergglobal.com/english/info/ntp.htm

Assuming that Symmetricom has the same convention, this would mean it is using a time string to send the information.
iburst allows the computer to sync more quickly with GPS time.
minpoll and maxpoll specify a time in seconds to poll the timesource for
time.

INSTALLING
For CentOS 6.2, I had to replace the existing ntpd in /usr/sbin/ntpd with the custom built version.

I have also had problems with ntpd not starting on boot -
I had to manually add a call starting ntpd to /etc/rc.local even though chkconfig showed ntpd on. It seems that ntpd starts but crashes? To be continued as I explore the issue further.

CHECKING THAT IT WORKS
Running ntpq -p should show the GPS clock, and you should see the reach column increase as the computer syncs with the card. If you see things working, but then an x appears in front of the entry for the card, that means there is another ntpd source that disagrees time-wise.

I have seen this in the past, which worries me a little, as I am supposedly pulling time straight from GPS. Maybe there is a bug in my code - or maybe the other server was wrong? In any case, using the GPS card as the only time source works great and has greatly decreased the footprint of my system.
    LINKS
    http://www.linuxquestions.org/questions/linux-networking-3/ntp-conf-need-settings-for-a-refclock-using-symmetricom-bc635pci-timing-card-787257/#post4471512
    http://www.eecis.udel.edu/~mills/ntp/html/ntpd.html
    http://www.eecis.udel.edu/~mills/ntp/html/debug.html
    http://compgroups.net/comp.protocols.time.ntp/proprietary-hardware-clock-as-ntp-ref/168850
    https://support.ntp.org/bugs/show_bug.cgi?id=1674
    https://support.ntp.org/bugs/show_bug.cgi?format=multiple&id=786

    Sunday, September 16, 2012

    Programming the ATTiny25 with Ubuntu

    SETUP 
    To get all of the libraries and packages necessary to put a basic program on your AVR chip, run the following command.
    sudo apt-get install gcc-avr avrdude avr-libc

    ATTiny25 is plugged into a 40 pin ZIF socket, which is wired for HVSP mode. See this video for ZIF socket installation instructions https://www.youtube.com/watch?v=yJo29VMXt90

    Other programming modes for AVR chips include PP and ISP - see this [PDF] link for pin wiring in order to program various AVR chips.

    FULL COMPILATION COMMANDS
    avr-gcc -Os -o hello.elf -mmcu=avr25 hello.c
    avr-objcopy -j .text -j.data -O ihex hello.elf hello.hex
    sudo avrdude -p t25 -c dragon_hvsp -P usb -e -B10 -U flash:w:hello.hex

    If wired for ISP or PP mode, you should replace dragon_hvsp in the last step with dragon_isp or dragon_pp .

    If you see this error:
    jtagmkII_close(): bad response to GO command: RSP_ILLEGAL_EMULATOR_MODE

    This error is actually OK - apparently HVSP mode shouldn't respond to this command, but avrdude sends it in ALL programming modes - see this link

    ADDITIONAL INFO
    /etc/avrdude.conf has a lot of (specific) information about each supported chip.


    MORE LINKS
    http://www.instructables.com/id/Getting-started-with-ubuntu-and-the-AVR-dragon/
    http://tldp.org/HOWTO/Avr-Microcontrollers-in-Linux-Howto/x207.html
    http://www.nongnu.org/avr-libc/user-manual/using_avrprog.html
    http://www.homebuilthardware.com/index.php/avr/linux-avrdragon-tutorial-1/