InEnglish

Command line loader for "Read for PIC" Board

Sunday, 24 March 2013
|
Écrit par
Grégory Soutadé

Ready for PIC board

Some weeks ago I bought a "Ready for PIC" board from MikroElektronika. These board comes with a PIC18F25K22 flashed with a bootloader that receives a program from UART and load it in memory without flashing. The prototype area is very nice for peoples that do not want to weld, it also contains a FTDI chip and can be powered by USB. Plus MikroElektronika supply a GUI software to interact with the bootloader. These GUI is available for Windows and Linux, that's great ! Contrary to microchip, MikroElektronika wants to sell their expensive C/BASIC/Pascal compiler and provide cool boards at a good price. The only thing I regret is that you cannot reset the PIC with the FTDI, you need to press the reset button before and after code is downloaded (so boring...).

Maybe the GUI is useful for electronics peoples, but I found it (on Linux) heavy and sometimes buggy. Moreover you cannot access to serial output just after code is downloaded. So, I wrote a simple console Python program that will download an HEX file with command line and optionally displays the output of UART. This allows to do quicker and simpler tests. You can find the file here, it's licensed under GPL v3.

Fast software DES implementation

Wednesday, 31 October 2012
|
Écrit par
Grégory Soutadé

This is a fast software DES implementation for 64 bits machines thats supports intel SSE instructions (SSE, SSE2, SSE3 and SSSE3). The x86_64 architecture lacks support for bit manipulation and the ones introduced by AMD are not used here. The main SSE instruction used is "pmovmskb" that will extract the most significant bit of each byte inside an XMM register. That allows to do linear permutations with few instructions, but for random permutations a simple mask-shift is used. The next generation of processors with AVX2 should includes some bit manipulation instructions that will even more speed up DES computation.

Currently, my implementation is about 10 times faster than the one in OpenSSL 1.0.1c, assembly optimized but 32 bits centered. The package includes a basic implementation in full C (des.c) for algorithm comprehension and the fast implementation in fast_des.c. fast_des.c is linked with the libcrypto.a of OpenSSL (configured with linux-x86_64) to do benchmarks. The benchmark is to do 1 000 000 full DES computation. I obtained these values :

fast_des.c Init value 123456789abcdef Fast des Time 0 29333426 85e813540f0ab405 Des Time 0 362639005 85e813540f0ab405 des.c : Init value 123456789abcdef Time 0 236431885 85e813540f0ab405

My configuration is compounded by an intel core i5 m450 @ 2.4Ghz, 4Gb of RAM, Linux 64, gcc 4.7.1. One funny thing is that if I compile fast_des.c with -O3 flag, performances dramatically falls just under OpenSSL implementation. On an core 2 duo E7500 @ 2.93GHz :

Init value 123456789abcdef Fast des Time 0 110037322 85e813540f0ab405 Des Time 0 352010444 85e813540f0ab405

Source file can be found here.

Working with OpenOffice/LibreOffice Spreadsheets with Python

Monday, 22 October 2012
|
Écrit par
Grégory Soutadé

Working with OpenOffice/LibreOffice Spreadsheets with Python One improvement of OpenOffice was to introduce Python scripting beside VBA one. You can do internal or external scripting. External scripting is done via Python UNO interface, it's like CORBA objects (...). But resources on web are poor and sparse. Only two websites have a clear and complete information :
https://www.wzdftpd.net/downloads/oowall/pyUnoServerV2.py
http://stuvel.eu/ooo-python

This is a mini HOWTO you can use in your external scripts First you have to start server side OOo/LO :

libreoffice "--accept=socket,host=localhost,port=2002;urp;" --invisible

If you don't want to see OOo/LO interface, add --headless. WARNING: You need to close ALL OOo/LO instances before starting server !

Next, load a document :

def connect(port, filename): # get the uno component context from the PyUNO runtime localContext = uno.getComponentContext() # create the UnoUrlResolver resolver = localContext.ServiceManager.createInstanceWithContext( "com.sun.star.bridge.UnoUrlResolver", localContext) # connect to the running office ctx = resolver.resolve("uno:socket,host=localhost,port=" + str(port) + ";urp;StarOffice.ComponentContext") smgr = ctx.ServiceManager # get the central desktop object DESKTOP =smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx) url = unohelper.systemPathToFileUrl( os.path.abspath(filename)) doc = DESKTOP.loadComponentFromURL(url, '_blank', 0, ()) return doc

You can get sheets inside document by creating an enumeration :

doc = connect(port, filename) sheets = doc.getSheets() sheet_enum = sheets.createEnumeration() while sheet_enum.hasMoreElements(): sheet = sheet_enum.nextElement() print sheet.getName()

Retrieve cells :

cell = sheet.getCellByPosition(col, row)

You can use following methods on cell objects : XCell

To retrieve cell type (CellContentType) :

cell.getType()

For me object (or enumeration) comparison fails, so I use string comparison :

if cell.getType().value != 'EMPTY':

cell.getValue() will return cell float value (0.0 if cell is empty or text). Most of the case you need to cast it into int value : int(cell.getValue()) or do all your code with float values !!

Be careful, sometimes cells values are formated with text but contains float/integer !! value = value_cell.getString() will return "0x45"

Now you have all basics to do a spreadsheet parser ! If you don't know how to handle an object, juste print it and look at its supportedInterfaces dictionary, OOo API doc will tells how to handle them.

Activate eSata on Sheevaplug with Debian

Monday, 08 October 2012
|
Écrit par
Grégory Soutadé

esata.png

Activate eSata on Sheevaplug with Debian I recently bought an external hard disk with an eSata interface, it was not easy to find (almost are with USB2/3, other are expensive advanced NAS), but I did. The purpose of this disk is to make backups. But, on my Sheevaplug, the main partitions (/root, /boot...) are on an USB key (Toshiba 16 GB) running Debian stable. When I plugged my new hdd it was not recognized ! Actually I first configured my sheevaplug following some tutorials (http://www.cyrius.com/debian/kirkwood/sheevaplug/ for example). It was said to set the boot variable "arcNumber" to 2097. Why ? In facts ARM SoC doesn't have peripherals discovery mode, so you need to tell which board you're running on.

After looking a bit into Debian's kernel, it seems that eSata interface is activated only if arcNumber is set to 2678 ! If I do that, original Ubuntu on NAND flash (factory installation) doesn't recognize the current SoC because arcNumber 2678 is a patch from Debian (in original installation, eSata is activated by default). The second point is that if you set the board as an eSata board, Debian will try to boot on the eSata hard disk (even if you specify different kernel root=XX values).

So what to do ? The solution is to specify your partitions not using classic /dev/sdXXX format, but using UUID numbers. They are not human readable, nevertheless they refer to an unique partition ! The first step consists in listing your partitions UUID :

ls -l /dev/disk/by-uuid/ lrwxrwxrwx 1 root root 10 Sep 27 07:34 1642ad57-77aa-494c-aa77-6998d420eb8f -> ../../sda3 lrwxrwxrwx 1 root root 10 Sep 27 07:34 198239b4-ff16-4dda-8df0-37b106005817 -> ../../sda1 lrwxrwxrwx 1 root root 10 Sep 27 07:34 2e0cd399-3839-4e4e-bc57-5e6628841bc1 -> ../../sda2 lrwxrwxrwx 1 root root 10 Sep 27 07:34 dd27350b-2522-46a6-862e-0cbc072b535f -> ../../sda4

Then, edit /etc/fstab to use UUID and not /dev/sdXXX (it's fastidious I know) After that, you need to reboot with the serial console connected and stop automatic boot (type a key) to edit uBoot configuration. We'll set arcNumber to 2678 by default.

setenv arcNumber 2678

Then edit bootargs_options (for me it's bootargs_options_usb) to set correct UUID value

setenv usb_bootargs_root "root=UUID=2e0cd399-3839-4e4e-bc57-5e6628841bc1"

Last step is to edit the global boot_cmd to set arcNumber to 2097 before booting to NAND (in my case, if USB boot fails it will try to boot on MMC then on NAND) :

setenv bootcmd 'setenv arcNumber 2678; saveenv; run usb_boot; setenv arcNumber 2097; saveenv; run bootcmd_mmc; run bootcmd_nand'

Finally save environment variables to flash and boot

saveenv boot

My final environment variables

ethact=egiga0 bootargs_root=ubi.mtd=1 root=ubi0:rootfs rootfstype=ubifs mtdpartitions=mtdparts=orion_nand:0x400000@0x100000(uImage),0x1fb00000@0x500000(rootfs) ethaddr=00:50:43:01:4C:56 bootargs_console=console=ttyS0,115200 bootargs_root_nand=ubi.mtd=1 root=ubi0:rootfs rootfstype=ubifs bootcmd_nand=setenv bootargs $(bootargs_console) $(mtdpartitions) $(bootargs_root_nand); \ nand read.e 0x00800000 0x00100000 0x00400000; bootm 0x00800000 bootargs_root_mmc=root=/dev/mmcblk0p2 rootdelay=5 bootcmd_mmc=setenv bootargs $(bootargs_console) $(bootargs_root_mmc); mmcinit;\ ext2load mmc 0:1 0x800000 /uImage; bootm 0x00800000 real_bootcmd=run bootcmd_mmc; run bootcmd_nand filesize=32D62A usb_bootargs_console=console=ttyS0,115200 usb_bootcmd_usb=usb start; ext2load usb 0:1 0x01100000 /uInitrd; ext2load usb 0:1 0x00800000 /uImage usb_boot=setenv bootargs $(usb_bootargs_console) $(usb_bootargs_root); run usb_bootcmd_usb;\ bootm 0x00800000 0x01100000 mainlineLinus=yes bustargs_root_usbroot=/dev/sda2 usb_bootargs="root=UUID=2e0cd399-3839-4e4e-bc57-5e6628841bc1" stdin=serial stdout=serial stderr=serial mainlineLinux=yes enaMonExt=no enaCpuStream=no enaWrAllo=no pexMode=RC disL2Cache=no setL2CacheWT=yes disL2Prefetch=yes enaICPref=yes enaDCPref=yes sata_dma_mode=yes netbsd_en=no vxworks_en=no bootdelay=3 disaMvPnp=no enaAutoRecovery=yes

I added a rule in fstab to mount my hdd at startup

UUID=590f30b1-7727-4d0a-a86a-2360ec0b3f88 /media/backup ext4 defaults 0 1

A simple backup script based on rsync that power down disk after backup is done.

How to load UTF8 data with python minidom ?

Wednesday, 22 August 2012
|
Écrit par
Grégory Soutadé

For the dynastie project, I need to load data encoded in UTF-8 with Python minidom XML parser. But when I wrote node.toxml('utf-8') to display the XML tree, I get this error :

UnicodeDecodeError at /generate/1

'ascii' codec can't decode byte 0xc2 in position 187: ordinal not in range(128)

In facts Python thinks that all data in XML tree are in ASCII and try to encode it into UTF-8 (or anything else you supplied). The solution is to use your own writer that will convert all non utf-8 strings in unicode string which can be then re-encoded in every format (like utf-8). This doesn't appears in Python 3 because, in Python 3, all strings are already in unicode. Add the following class to your code :

class UnicodeWriter(codecs.StreamWriter): encode = codecs.utf_8_encode def __init__(self): self.value = u'' def write(self, object): if not type(object) == unicode: self.value = self.value + unicode(object, 'utf-8') else: self.value = self.value + object return self.value def reset(self): self.value = u'' def getvalue(self): return self.value

And our node.toxml('utf-8') becomes :

writer = UnicodeWriter() node.writexml(writer) writer.getvalue().encode('utf-8')