Friday, April 3, 2015

Daily backup of local git changes

While working on a ticket and not ready to commit, it's a good idea to backup all of our changes to an archive file.

The following script does all that and executed daily by cronjob automatically.
  1. create a file, say "backup" in /etc/cron.daily
  2. Type the following (change '<yourhome directory>' to your own login name and <git location> to the git directory where source codes are located):
backing up changed files to tarball
#!/bin/sh
pushd <your source-code location>
LIST=`git status --porcelain | sed -n -e '/^ [D|M]/p' | sed -e 's/^ [D|M] //'`
backupname=`git branch | sed -n -e "/^* /p" | sed  -e 's/** //'`
backupname=$backupname-`date "+%s"`.backup.tar.bz2
dest='<your home directory>'
if [ ! -d "$dest" ]
then
    mkdir -p $dest
fi
tar jcvf $dest/$backupname $LIST > /dev/null

Make the file executable:
chmod +x /etc/cron.daily/backup

Sunday, March 22, 2015

Block Access during certain period using EBTABLES


Say, we want to block any packets coming from a device with mac address 00:01:02:03:04:05 (in other words, our router/switch should just silently drop any packets coming from this MAC address) during period of time 00:00 (00:00 AM) to 6:00 AM, do:

#ebtables -A INPUT -s 00:01:02:03:04:05  --timestart 0:0 --timestop 06:00 -j DROP


If we just want to drop IPv4 packets for the above:

#ebtables -A INPUT -p IPv4 -s 00:01:02:03:04:05  --timestart 0:0 --timestop 06:00 -j DROP


So, parameters for ebtables are actually similar (yet subset) of iptables (netfilter).

Wednesday, March 11, 2015

To fix Mute button keyboard shortcut issue on LXDE

Create a script call "amixertoggle" and save it in /usr/local/bin:

#!/bin/bash

amixer $1 sset Headphone toggle
amixer $1 sset Speaker toggle
amixer $1 sset PCM toggle
amixer $1 sset Master toggle




Edit file $HOME/.config/openbox/lxde-rc.xml and replace block that has "XF86AudioMute" to call our script.  For example:

...
...

    <keybind key="XF86AudioMute">
      <action name="Execute">
              <command>amixertoggle -c 1 toggle</command>
      </action>


...
...

(" -c 1" above is for machine, where the mixer control is actually on card 1.  If doesn't work, we can try different number)

Reload the modified file lxde-rc.xml by doing:

openbox --reconfigure

That's it.  Everytime we press "Mute" button on our PC keyboard, the mute/unmute will toggle.

Thursday, February 26, 2015

Raspberry Pi 2

Got this New Raspberry Pi 2 I ordered a few weeks ago.  It's significantly faster than the first version.  I only wish it had USB 3.0 ports so I can have much bigger and faster storage.

Monday, October 13, 2014

Cheap ATMEL AVR ISP ICE from QinHeng

I bought this small USB stick from eBay.  It supposedly can do JTAG and many other cool stuff for ATMEL microcontrollers.

Anyway, here is the detail how to use it:

The other end of the stick has dual-line 10-pin connector.  The pins are:

Pin  Purpose
------------------------------
1    TCK
2    GND
3    TDO
4    VTref
5    TMS
6    nSRT
7    VSupply
8    (nSRT)
9    TDI
10   GND

These pins are actually JTAG pins.  The connections to the Microcontroller is as follow (the pullup resistors are all 4.7k):



The USB id:

$ lsusb
...
Bus 003 Device 006: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter
...


Upon inserting the stick to my PC's USB (running Ubuntu Linux):

[118845.955500] ch341-uart ttyUSB0: ch341-uart converter now disconnected from ttyUSB0
[118845.955541] ch341 3-2:1.0: device disconnected
[118851.692044] usb 3-2: new full-speed USB device number 6 using ohci-pci
[118851.901261] usb 3-2: New USB device found, idVendor=1a86, idProduct=7523
[118851.901271] usb 3-2: New USB device strings: Mfr=0, Product=2, SerialNumber=0
[118851.901276] usb 3-2: Product: USB2.0-Serial
[118851.904683] ch341 3-2:1.0: ch341-uart converter detected
[118851.940428] usb 3-2: ch341-uart converter now attached to ttyUSB0


To use it as programmer, we can use avrdude:

$ sudo avrdude -c avrisp -P /dev/ttyUSB0 -pm328p <other options>

To debug, we can use another opensource, AvaRICE (from ubuntu, just do "apt-get install avarice").

For example (this is just to show how to use it, as my ICE stick is not connected to any target device yet so it reports it "No configuration available for device ID: ffff"):

$ sudo avarice --jtag /dev/ttyUSB0 -1
AVaRICE version 2.11, Jan 17 2014 02:53:19

Defaulting JTAG bitrate to 250 kHz.

JTAG config starting.
Hardware Version: 0xc3
Software Version: 0x80
Reported JTAG device ID: 0xFFFF
No configuration available for device ID: ffff


The avarice can be set as a gdb server mode, so we can debug the target removely. For example, to make it as gdb-server listening on port 4242.

$ sudo avarice --jtag /dev/ttyUSB0 :4242

From another PC (or the same PC, if host and server is on the same machine), we open gdb, and connect:

$ gdb
GNU gdb (Ubuntu 7.7-0ubuntu3.1) 7.7
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) target remote 127.0.0.1:4242





Using PicKit2 Programmer to program Atmel's microcontrollers

Many hobbists are familiar with Arduino kit.  This beast uses various Microcontrollers from Atmel.  For example, Arduino UNO uses AtMega 328p.

Normally, we use the pcb kit to program the chip, as the Arduino (or the clones) use USB to serial chip to translate  USB to serial needed to program Arduino.

For people who has done hacking on PIC microcontrollers from MicroChip and wants to use bare-metal chip, they can program the Atmel chips using the existing cheap PicKit2 programmer.  What they need just the Pickit2 programmer and a software tool called avrdude.

The pin connections:

AVR   - PICKit2 (pin):
----------------------- 
RST   - VPP/MCLR (1)
VDD   - VDD Target (2) -- optional if AVR self powered
GND   - GND (3)
MISO  - PGD (4)
SCLK  - PDC (5)
MOSI  - AUX (6) 
 
 
Some of command examples are shown below:

To write our firmware to the flash in AtMega 328p:

 
$ avrdude -c pickit2 -p m328p -v -V -U flash:w:"myfirmware.hex":a 
 

To upload bootloader (e.g. to upload ARDUINO bootloader):
 
$ avrdude -c pickit2 -p m328p -v -V -U boot:w:"boot.hex":a