Saturday, November 17, 2012

NTSC and PAL

The other day, I captured the composite video signal coming out from an infotainment device. The device had a setting which allows you to select encoding for the output signal. By setting the device to output a particular image on the composite video RCA connector (hence the waveform shown below are of unmodulated baseband video signals) and switching the device between NTSC and PAL, I was able to capture and analyse various aspects of the two standards.

Test Pattern 

Here is the test pattern I used. I loaded the JPEG onto a USB flash drive and connected it to the infotainment device. The device was put in image playback mode and the selected file was rendered on the LCD display as well as output on the composite video out.

The image is composed of exactly 640 x 480 pixels which corresponds to an aspect ratio of 4:3.

Test Pattern


A short table of differences between the two standards

NTSC PAL
Stands for National Television System Committee Phase Alternating Line
Major Contries using it North America, Most of South America, Japan etc.
(mostly regions where AC mains is 60Hz)
India, Australia, South Asia, Eastern parts of Africa etc.
(mostly regions where AC mains is 50Hz)
Wikipedia Link http://en.wikipedia.org/wiki/NTSC http://en.wikipedia.org/wiki/PAL
frames per second (fps) 60i or 30p (59.94i incase of colour) 50i or 25p (p stands for progressive scan,
i stand for interlaced)
Tint control NTSC receivers have a tint control to perform colour correction manually. If this is not adjusted correctly, the colours may be faulty.  The PAL standard automatically cancels hue errors by phase reversal, so a tint control is unnecessary.
Scan lines 525 625
Horizontal Frequency 15.734 kHz 15.625 kHz
Aspect Ratio 4:3
Colour Subcarrier roughly (2x227+1)x15750/2 = 3.58 MHz
exactly (2x227+1)x15734.26/2 = 3.579545 MHz 
roughly (2x283+1)x15625/2 = 4.43 MHz
exactly 4.43361875 MHz
Video Bandwidth 4.2 MHz 5.0 MHz
Sound Carrier 4.5 MHz 5.5 MHz
Bandwidth 6 MHz 7 or 8 MHz


Waveforms


So here is the comparison of various sections of the signal. Practical measurement differ slightly from the ideal figures presented in the table above. The signals captured were pure baseband video signals only. No modulated audio signals were present.


 Many Lines

Single Line

Color Band signal

Color Burst, White band and Yellow band

Color Burst

Color subcarrier frequency measurement

Horizontal Frequency Measurement
 Phase change between green and magenta bands

Vertical Blanking

Spectrum Comparison

References

  1. Maxim APP 734 Video Basics - A nice short summary of all related concepts
  2. Chapter 08 of the book Video Demystified: A Handbook for the Digital Engineer by Keith Jack (PDF here)
  3. PAL vs NTSC - An article clarifying the relationship between pixel resolution and video encoding format (NTSC/PAL) with respect to DVDs.
  4. 16x9 Enhancement - An article elucidating aspect ratios with respect to videos.
  5. Vertical Blanking Interval of 625-Line Standard (PAL Colour) (PDF Diagram)
  6. World Analogue Television Standards and Waveforms 

Thursday, November 15, 2012

blinky and printf on netduino using YAGARTO

Aim

To run simple C programs on netduino using YAGARTO+Eclipse on Windows.

Software Development Tools

  1. A Windows PC with 3 free USB ports
    I used Windows 7 Home Premium 64 bit SP1
  2. Eclipse IDE for Automotive Software Developers (32 bit for Windows)
    download link | I used juno SR1 | This version includes CDT
  3. Atmel SAM-BA for Windows
    download link | I used 2.12 (sam-ba_2.12.exe)
  4. YAGARTO GNU ARM toolchain (for Windows)
    download link | I used 13.10.2012 (yagarto-bu-2.22_gcc-4.7.2-c-c++_nl-1.20.0_gdb-7.5_eabi_20121013.exe)
  5. YAGARTO Tools
    download link | I used 18.10.2012 (yagarto-tools-20121018-setup.exe)

Hardware

  1. One no. netduino
    buy link | I used the plain one (not plus or go)
  2. Two nos. FTDI Basic Breakout - 3.3V
  3. Four nos. LEDs
    buy link | I used 3mm ones
  4. Four nos. 1Kilo-ohm resistors (current limiting for the above 4 LEDs)
    I used 1/4 watt through hole ones
  5. Two nos. Potentiometers
    buy link | I used 10 kilohom linear ones 
  6. One no. Tactile Switch


Circuit used for developing simple programs for netduino using YAGARTO+Eclipse
The tactile switch is used to apply 3.3V to the ERASE pad of netduino when required
(FZZ and PDF source for the above diagram is available from the github repository)

Instructions


  1. Install FT232RL Driver if you haven't already have them installed. (get those from here)
  2. Install SAM-BA
  3. Install YAGARTO GNU ARM toolchain
  4. Install YAGARTO Tools
    (run "arm-none-eabi-gcc -v" in a command window to ensure that gcc for ARM is installed correctly)
  5. Install Eclipse by extracting it to a folder on your hard disk (usually C:\eclipse) and create a shortcut to eclipse.exe on your desktop.
  6. Create a new folder for maintaining the Eclipse Workspace.
    (I created mine here: C:\Users\Anurag Chugh\Documents\Eclipse_yagarto)
  7. Start Eclipse and point to this folder as the workspace and click OK and go to the workbench.
  8. Switch to "C/C++ Projects" view (Window > Show View)
  9. Right click in "Project Explorer" and select New > C Project.
  10. Enter the project name as "blinky" and select "Empty Project" under "Makefile Project". Select toolchain as Cross GCC and click Finish.
  11. Use Windows Explorer and create the following structure in your "blinky" project directory populating it with the files from the archive here.

  12. Go back to eclipse and click on "blinky" and press the F5 key. The folder structure will appear there. You can double click on the various source files and look at the source code contained in them.
  13. Right click on "blinky" and select "Build Project" and .bin file will be generated which you can now program into netduino's program flash memory and watch the LED Blink.

Flashing the netduino:

AT91SAM7X512 's memory is programmed using Atmel's SAM-BA software. This software can we be used with Atmel's SAM-ICE JTAG Emulator or Segger's J-Link JTAG Emulator - but these hardware tools require special connector to be present on the target board. In our case, such a connector is not present and so we must resort to one of the two remaining ways of programming the AT91SAM7X512's memory: over USB or over Debug UART:

Method 1 - Programming over USB

  1. Power-up and erase the microcontroller - Connect the netduino board to your computer to power it up via its microUSB connector. Apply 3.3V momentarily to the ERASE pad of the netduino. You may do this using a single strand wire between the ERASE pad and the 3V3 pin. The contents of microcontroller's flash have now been erased. (In the circuit diagram above, note that I am using a tactile switch to apply 3.3V to the ERASE pad)
  2. Unplug and plug the netduino board from the computer. Windows will fail to find an appropriate driver for netduino.
  3. Load the correct driver for netduino - Right click on My Computer and select Manage. Switch to Device Manager. Scroll down and you will see an "Unknown Device" corresponding to netduino. Right click it and select "Update Driver Software". Manually browse to the folder named "drv" inside the location where you had installed SAM-BA (in my case this was: C:\Program Files (x86)\Atmel\sam-ba_2.12\drv) and load the driver from there. Windows will complain about the driver not being signed, but you must proceed to load the software anyway.
  4. netduino will now be enumerated as a virtual serial port. Verify this in the device manager and note the COM port number.
    netduino appears as an Unknown Device when first plugged in

    netduino driver successfully installed
  5. After successful installation, netduino appears as a USB to Serial convertor.
    Make sure to note the COM port number.
  6. With the netduino board still connected, fire up SAM-BA and from the "Select the connection" dropdown list select the appropriate COM port corresponding to the "AT91 USB to Serial Converter" as noted from the Device Manager. This COM port will appear in the dropdown list prefixed with "\USBserial\".
    Starting SAM-BA
  7. From the "Select your board" dropdown list, select "at91sam7x512-ek". Make sure to select this correct entry from the dropdown list, it is easy to confuse it with some other entry. Press the Connect button once done.
  8. Many flash programming software require the input file in .bin format, while others require it to be in the Intel HEX (.hex) format. The makefile tells the compiler to generate both formats during compilation. SAM-BA requires the .bin format of the file. So after SAM-BA starts up, presss the folder icon next to the "Send File Name" textbox and browse over to your Eclipse workspace path and locate the file named blinky_rom.bin. In my case this file was located at "C:\Users\Anurag Chugh\Documents\Eclipse_yagarto\blinky\blinky_rom.bin" and click open.
  9. After the path appears in the "Send File Name" textbox, click on the "Send File" Button. This will cause SAM-BA to send commands to netduino and program its flash memory. Once it is done, SAM-BA will ask you if you want to lock the programmed  region of the flash memory. You may select either Yes or No as in our case we are not concerned with securing the contents of our flash memory.
  10. Once the flash memory is program, we need to configure the micrcontroller to Boot from Flash whenever it is reset. To do this, select "Boot from Flash (GPNVM2) Script" from the Scripts dropdown list and press Execute.
  11. Now you can unplug and plug the netduino. Once you do that, you will notice the LED flashing, indicating that your program is running successfully on your netduino.

Method 2 - Programming over USB

If you look at the circuit diagram presented to you at the beginning of the post, you will note that, there are two USB to Serial converter chips (FT232) that I have connected to a few pins (USART0 pins and DBGU pins) of the netduino. I use them to send and receive text to and from a computer. The DBGU (aka Debug UART) can also be used to program the netduino. To use DBGU port to program the netduino, follow these steps:

  1. Connect the FT232 connected to the DBGU pins to a spare USB port on the computer and make sure the drivers for FT232 are installed. Note the COM port number corresponding to this FT232 chip from the Device Manager.
  2. Power up the netduino using an external power supply - using the DC Jack or cell phone charger with a USB socket. Powering the netduino is essential to ensure that when it starts up, it does not receive any data over the USB port. 
  3. Erase the flash memory of netduino by applying 3.3 volts to the ERASE pad and cycle the power to netduino
  4. After ERASE and power up, the microcontroller's Boot Program will wait for USB enumeration by the computer, if that fails, it will switch to listening for commands over DBGU serial port. Since we have powered the netduino using an external supply, the microcontroller will default to listening for commands over Debug UART.
  5. At this point start up SAM-BA and select the COM port corresponding to FT232 chip connected to the DBGU port. From there on follow the same steps as specified in the previous method of programming the netduino over USB

Notes:

  1. The part number of the microcontroller present on board the plain version of the netduino is AT91SAM7X512. It is manufactured by Atmel.
  2. The microcontroller on board the netduino runs off of a 18.432MHz crystal.
  3. When select "Build Project" in Eclipse and compiler the program, you will notice that a lot of build artifacts are generated (files with the extensions .lst, .o, .bin, .map, .elf etc.. and a folder called .dep). You can remove these files by right clicking on the blinky project and selecting "Clean Project". The .bin file will also be deleted when you clean the project.
  4. Here is the description of all the files contained in the archive above that you had extracted to your blinky project folder:
    1. startup.s - Assembly language startup functions, executed the first thing after microcontroller is powered up. Configures the internal RAM and ROM. Configures settings for various ARM modes.
    2. low_level_init.c - After executing startup.s, the code in this file is executed. Clock registers are written to. Memory Controller is configured. Watchdog timer is configured.
    3. main.c - Contains the main() function. Actual code which makes the LED blink.
    4. AT91SAM7X512.h - Contains definitions for various hardware registers of the AT91SAM7X512 microcontroller. It allows us to read and write registers by their names - this file takes care of mapping those names to actual addresses.  This file is included in low_level_init.c and main.c. 
    5. makefile - A test file containing instructions (a.k.a recipies) for compiling and linking and generating the .bin file from the C and Assembly source files. This file is read by make.exe (part of YAGARTO tools) which in turn executes the compiler and linker as per the build recipes.
    6. sam7x512_rom.ld - This is the linker script file. Linking is the last step of compilation process. The script file specifies what sections (code, data etc..) are placed where in the memory. (More info here and here)
  5. Flashing netduino with the blinky program will erase its bootloader. You will need to restore the bootloader if you want to use it with Microsoft Visual C# Express 2010 again. To know how to restore the bootloader, watch this video.

Going Further:

  1. You can now delete the Eclipse workspace folder that you had created and instead use "GitHub for Windows" to clone the following repository (which is also an Eclipse Workspace) containing a few more simple C programs for netduino:
    http://github.com/lithiumhead/netduino_eclipse_yagarto
  2. You can try out FreeRTOS on your netduino.

Sunday, November 11, 2012

Installing Arch Linux on a VitualBox VM (Spoon-fed version)

I have Windows 7 64-bit on my Laptop (it has 4GB RAM) and here is how I went about Installing Arch Linux on a virtual machine created using VirtualBox

Prepare the virtual machine:

  1. Make sure your computer has an active internet connection
  2. Download and install "VirtualBox for Windows hosts" from here.
  3. Download and install "VirtualBox Oracle VM VirtualBox Extension Pack". Just double click on  .vbox-extpack file to install it.
  4. Download the Arch Linux iso image file. The name of the latest image at the time of writing this was: archlinux-2012.11.01-dual.iso
  5. Click on "New" to create a new virtual machine for Arch Linux. Name it the same as the iso file (archlinux-2012.11.01-dual in my case) and the "Operating System" and "Version" types will automatically be selected. Set the RAM to 1024 MB, Hard Disk to VDI having capacity of 10GB.
  6. Before starting the virtual machine, right click on it and select settings and perform the following configurations:
    • Under "General>Advanced" set the Shared Clipboard to Bidirectional
    • Under "Storage>IDE Controller" point to the Arch Linux iso file for loading into the virtual CD/DVD drive. Put a tick in the Live CD/DVD checkbox.
    • Under "Network" ensure that the Adapter 1 is enabled and attached to NAT. Under the "Advanced" option for the same, change the adapter type to "PCnet-FAST III"
    • Under "Display" tick the "Enable 3D Acceleration" checkbox and increase the video memory to 128 MB.
  7. Double click on the newly created virtual machine to start it.
  8. When the system boots select "Boot Arch Linux" from the menu presented by the live CD

Format and mount the virtual hard disk:

Note that you will need to manually type out the commands, copy and paste won't work between Windows host and the Arch Linux 
  1. When the command prompt is presented, execute the following command:
    # cfdisk /dev/sda
  2. Create one primary partition to use the whole 8GB hard disk.
  3. Mark the partition as Bootable. 
  4. Write the partition table
  5. Exit cfdisk.
  6. When you are back to the command prompt, execute the following command to make the file system for the first and only partition:
    # mkfs.ext4 /dev/sda1
  7. Mount the partition as root:
    # mount /dev/sda1 /mnt
  8. Install the base system (internet connection required, ~152MB will be downloaded):
    # pacstrap /mnt base base-devel
  9. Generate an fstab (fstab stores filesystem information):
    # genfstab -U -p /mnt >> /mnt/etc/fstab
  10. Generate locale:
    # locale-gen
  11. Chroot into the new system (Chroot is the process of changing of the apparent disk root directory to another root directory) :
    # arch-chroot /mnt
  12. Set time zone. Mine is India:
    # ln -s /usr/share/zoneinfo/Asia/Kolkata /etc/localtime
  13. Set hostname of your computer, I set mine to "alvm" :
    # echo alvm > /etc/hostname
  14. Set a password for the root user:
    # passwd root
  15. Add a new user. I added a user named "anurag"
    # useradd -m -g users -s /bin/bash anurag
  16. Set password for this new user:
    # passwd anurag
  17. We need to enable DHCP client services on the virtual network adapter 1 which we had configured for this virtual machine to operate in NAT mode. Execute the following four commands:
    # pacman -S ifplugd
    # cd /etc/network.d
    # ln -s examples/ethernet-dhcp .
    # systemctl enable net-auto-wired.service
    [Deleted above lines on 2014-04-27]

    Execute the command:
    # cp /etc/netctl/examples/ethernet-dhcp /etc/netctl/profile
  18. Next we need to find out the identifier name of our adapter, execute the command:
    # ip link
    you will get the output like:
    [root@tristan]# ip link show
    1: lo:  mtu 16436 qdisc noqueue 
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    2: enp3s0:  mtu 1500 qdisc pfifo_fast qlen 100
        link/ether 00:80:c8:f8:4a:51 brd ff:ff:ff:ff:ff:ff
    The name of the ethernet adapter is enp3s0.
  19. Execute the command :
    # nano /etc/netctl/profile
  20. Change all instances of eth0 to enp3s0 or whatever your ethernet adapter identifier is.
  21. WriteOut the File (Ctrl + O) and then exit.
  22. Execute the following command to start the network:
    # netctl start profile
  23. Test the network using:
    # ping www.google.com
  24. To permanently set the network to start at every boot up, execute:
    # netctl enable profile
  25. Create an initial ramdisk environment:
    # mkinitcpio -p linux
  26. Install bootloader (the one I used is Syslinux) by executing the following two commands:
    # pacman -S syslinux
    # syslinux-install_update -iam
  27. Configure the bootloader. Edit the configuration file using nano text editor:
    # nano /boot/syslinux/syslinux.cfg
  28. Change the following entry:
    APPEND root=/dev/sda3 ro
    to
    APPEND root=/dev/sda1 ro

    You will have to do this in two places. One for the Arch (LABEL arch) entry and another for the fallback (LABEL archfallback)  entry.
  29. WriteOut the file
  30. Exit nano
  31. Exit chroot:
    # exit
  32. Unmount the partitions:
    # umount /mnt/{boot,home,}
  33. Reboot the virtual machine
    # reboot

Boot system, install VirtualBox guest additions and Desktop Manager (internet connection needed for all the following steps):

  1. When the live CD menu shows up, select "Boot existing OS"
  2. Login as "root"
  3. Install VirtualBox guest additions:
    # pacman -S virtualbox-guest-utils
  4. Configure the guest addition modules to load at startup:
    # nano /etc/modules-load.d/virtualbox.conf
  5. Type the following three lines:
    vboxguest
    vboxsf
    vboxvideo
  6. WriteOut the file
  7. Exit nano
  8. Install D-Bus:
    # pacman -S dbus dbus-core
  9. Install the X Window System packages:
    # pacman -S xorg-server xorg-xinit xorg-server-utils
  10. Install mesa for 3D support:
    # pacman -S mesa
  11. Install touchpad driver (required if you are using laptop):
    # pacman -S xf86-input-synaptics
  12. Install the default environment:
    # pacman -S xorg-twm xorg-xclock xterm
  13. Install TrueType Fonts
    # pacman -S ttf-dejavu
  14. Install the GNOME Desktop Environment (~400 MB Download):
    # pacman -S gnome gnome-extra gnome-control-center
  15. Restart:
    # reboot
  16. Login as a normal user. I logged in with "anurag"
  17. Configure X Server, get a copy of the configuration file and place it in your home directory:
    # cp /etc/skel/.xinitrc ~
  18. We need to configure the GNOME to start automatically when X is started
    # nano ~/.xinitrc
  19. Remove the "#" from the head of the following line, changing
    # exec gnome-session
    to
    exec gnome-session
  20. WriteOut the file (Ctrl + O)
  21. Exit nano
  22. Use the following command whenever you want to start the Graphical Interface:
    # startx
  23. Copy and paste between Windows host and Arch Linux guest will now work.
  24. You can now shutdown the virtual machine.
  25. Detach the Arch Linux live CD from the virtual machine.
  26. If you want to always start the virtual machine in graphical mode, reboot the virtual machine and login as root and issue the following command:
    # systemctl enable gdm
The condensed instructions above were prepared using the following references: