Productivity Sync Just another WordPress weblog

February 7, 2009

LPCUSP jtag, python-usb, and main_custom.c

Filed under: Uncategorized — admin @ 4:36 pm

I’ve spent a little time playing with my LPC-P2148 Olimex dev board, ARM-USB-OCD JTAG debugger and the lpcusb software to start hacking up a data capture project I’ve been neglecting.  This blog captures some of the  tidbits that took time to get.

Links:

  • svn checkout svn://svn.berlios.de/openocd/trunk  (tips break often.)
  • svn co https://lpcusb.svn.sourceforge.net/svnroot/lpcusb/trunk (I’m using version 177)

Open OCD

mgross@mgross-test:~/_dev/new/ahrms$ sudo openocd -f openocd.cfg
Open On-Chip Debugger 1.0 (2009-02-02-19:40) svn:1363

  • svn update -r 1363  (version 1363 works for me lately, but 1366 did not compile)
  • ./bootstrap    <– takes forever!
  • ./configure –enable-ft2232_ftd2xx
  • see ./doc/INSTALL.txt and be sure to first insall the ftd crap – libftd2xx (http://www.ftdichip.com/Drivers/D2XX.htm)

one gotcha to watch out for: don’t forget to include the calc_checksum in the flash bank config line:

  • flash bank lpc2000 0x0 0x80000 0 0 0 lpc2000_v2 12000 calc_checksum

Other than that the rest of my config is just a concatintaion of the openocd installed sample configs:

  • /usr/local/lib/openocd/target/lpc2148.config (with updated flash bank line)
  • /usr/local/lib/openocd/interface/olimex-arm-usb-ocd.cfg

running open ocd:

  • sudo openocd -f openocd.cfg (in one xterm)
  • telnet localhost 4444 (in an other xterm)

To upload stuff I’m using a scrip:

  • > script /home/mgross/_dev/new/script

The contents of my scrip:

mgross@mgross-test:~$ cat /home/mgross/_dev/new/script
halt
wait_halt
poll
arm7_9 dcc_downloads enable

reg cpsr 0x13
flash probe 0
flash info 0
flash erase_sector 0 0 26
mdh 0x0 100
mww 0xe01fc040 1
flash write_image erase /home/mgross/_dev/new/ahrms/trunk/target/examples/custom.bin 0x0 bin
mdh 0x0 100
#reset run

Now what about the LPCUSB code:

Im hacking on the examples main_custom.c file to setup a custom endpoint to transfer data over the USB to a python progam.  The only gotcha I had was the py-usb implementation does not seem to have a close opperation so I had to rewrite my test code otherwise things where flaky.  I’m useing py-usb to access the usb.  See the example files that get installed in /usr/share/doc/python-usb/example for some help

import usb
import struct
import time

class TargetDevice:

    def __init__(self, PID=0x0004, VID=0xffff):
        self.vendor_id = VID
        self.product_id = PID
        buses = usb.busses()
        for bus in buses :
            for device in bus.devices :
                if device.idVendor == self.vendor_id :
                    if device.idProduct == self.product_id :
                        self.dev = device

        self.handle = self.dev.open()
        #self.handle.reset()

    def read(self, packet_count):
        val = self.handle.controlMsg(requestType = (2<<5),
                                       request = 1,
                                       buffer = None,
                                       value = packet_count)
        data = []
        for i in xrange(packet_count):
            data.append(self.handle.bulkRead(0x82, 64))

        return data

    def bandwidth(self,loops):
        start = time.time()
        for count in xrange(loops):
            data=self.read(256)
        stop = time.time()

        print (64*256*loops)/(stop-start), stop-start, 64*256*loops

My hack to main_custom.c

/*
	LPCUSB, an USB device driver for LPC microcontrollers
	Copyright (C) 2006 Bertrik Sikken (bertrik@sikken.nl)

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright
	   notice, this list of conditions and the following disclaimer.
	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.
	3. The name of the author may not be used to endorse or promote products
	   derived from this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
	IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
	OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
	THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*
	This is a very simple custom device (not belonging to a specific USB
	class). It implements primitive read and write in the ARM memory space.

	Each transfer is initiated by a control transfer to inform the device
	about the address and size of the following data transfer.
	The data transfer takes place over a bulk endpoint (BULK_IN_EP for
	reads and BULK_OUT_EP for writes).

	This example can be used to measure USB transfer speed.
*/

#include "type.h"
#include "debug.h"
#include "hal.h"
#include "console.h"
#include "usbapi.h"
#include "armVIC.h"

#include <string.h>

#define BULK_IN_EP		0x82
#define BULK_OUT_EP		0x05

#define MAX_PACKET_SIZE	64

#define LE_WORD(x)		((x)&0xFF),((x)>>8)

static const U8 abDescriptors[] = {

/* Device descriptor */
	0x12,
	DESC_DEVICE,
	LE_WORD(0x0200),		// bcdUSB
	0xFF,              		// bDeviceClass
	0x00,              		// bDeviceSubClass
	0x00,              		// bDeviceProtocol
	MAX_PACKET_SIZE0,  		// bMaxPacketSize
	LE_WORD(0xFFFF),		// idVendor
	LE_WORD(0x0004),		// idProduct
	LE_WORD(0x0100),		// bcdDevice
	0x01,              		// iManufacturer
	0x02,              		// iProduct
	0x03,              		// iSerialNumber
	0x01,              		// bNumConfigurations

// configuration
	0x09,
	DESC_CONFIGURATION,
	LE_WORD(0x20),  		// wTotalLength
	0x01,  					// bNumInterfaces
	0x01,  					// bConfigurationValue
	0x00,  					// iConfiguration
	0x80,  					// bmAttributes
	0x32,  					// bMaxPower

// interface
	0x09,
	DESC_INTERFACE,
	0x00,  		 			// bInterfaceNumber
	0x00,   				// bAlternateSetting
	0x02,   				// bNumEndPoints
	0xFF,   				// bInterfaceClass
	0x00,   				// bInterfaceSubClass
	0x00,   				// bInterfaceProtocol
	0x00,   				// iInterface

// bulk in
	0x07,
	DESC_ENDPOINT,
	BULK_IN_EP,				// bEndpointAddress
	0x02,   				// bmAttributes = BULK
	LE_WORD(MAX_PACKET_SIZE),// wMaxPacketSize
	0,						// bInterval

// bulk out
	0x07,
	DESC_ENDPOINT,
	BULK_OUT_EP,			// bEndpointAddress
	0x02,   				// bmAttributes = BULK
	LE_WORD(MAX_PACKET_SIZE),// wMaxPacketSize
	0,						// bInterval

// string descriptors
	0x04,
	DESC_STRING,
	LE_WORD(0x0409),

	// manufacturer string
	0x0E,
	DESC_STRING,
	'L', 0, 'P', 0, 'C', 0, 'U', 0, 'S', 0, 'B', 0,

	// product string
	0x1A,
	DESC_STRING,
	'M', 0, 'e', 0, 'm', 0, 'o', 0, 'r', 0, 'y', 0, 'A', 0, 'c', 0, 'c', 0, 'e', 0, 's', 0, 's', 0,

	// serial number string
	0x12,
	DESC_STRING,
	'D', 0, 'E', 0, 'A', 0, 'D', 0, 'C', 0, '0', 0, 'D', 0, 'E', 0,

	// terminator
	0
};

typedef struct {
	U32		dwCount;
} TCmd;

static TCmd	Cmd;
static U8 	abVendorReqData[sizeof(TCmd)];
static U8 	in_buffer[MAX_PACKET_SIZE];
static U8	accel_data[3*2];
static U8	rotation_data[3*2];
static U8	magnet_data[3*2];
static U8	temp_data[3*2];
static U8	vref_data[3*2];

static void _HandleBulkIn(U8 bEP, U8 bEPStatus)
{

	if (Cmd.dwCount > 0) {
		// send next part
		USBHwEPWrite(bEP, in_buffer, MAX_PACKET_SIZE);

		Cmd.dwCount -= 1;
		DBG("READ: count=%X \n", Cmd.dwCount);

	} else {
		DBG("done\n");
	}

}

static void _HandleBulkOut(U8 bEP, U8 bEPStatus)
{
	int iChunk;

	// get next part
	iChunk = USBHwEPRead(bEP, NULL, 0);

	//donothing for now...
}

/*************************************************************************
	HandleVendorRequest
	===================
		Handles vendor specific requests

	Control transfer fields:
	* request:	0x01 = prepare memory read
				0x02 = prepare memory write
	* index:	ignored
	* value:	ignored
	* data:		U32 dwCount

**************************************************************************/
static BOOL HandleVendorRequest(TSetupPacket *pSetup, int *piLen, U8 **ppbData)
{
	switch (pSetup->bRequest) {

	// prepare read
	case 0x01:
		Cmd.dwCount = pSetup->wValue;
		DBG("vender READ: count=%X \n", Cmd.dwCount);
		// send initial packet
		_HandleBulkIn(BULK_IN_EP, 0);
		*piLen = 0;
		break;

	// prepare write
	case 0x02:
		Cmd.dwCount = pSetup->wValue;
		DBG("vender WRITE: count =%X, \n", Cmd.dwCount);
		*piLen = 0;
		break;

	default:
		DBG("Unhandled class %X\n", pSetup->bRequest);
		return FALSE;
	}
	return TRUE;
}

void updateAccelData(void)
{
	int i = 0;
	static U8 dummy1;
	/* called from timer interrupt
	 * call some spi magic and update buffer
	 */

	for (i=0; i< sizeof(accel_data); i++){
		accel_data[i] = dummy1;
	}
	dummy1++;
}

void updateRotationData(void)
{
	int i = 0;
	static U8 dummy2;
	/* called from timer interrupt
	 * call some spi magic and update buffer
	 */

	for (i=0; i< sizeof(rotation_data); i++){
		rotation_data[i] = dummy2;
	}
	dummy2++;
}

void updateMagnetData(void)
{
	int i = 0;
	static U8 dummy3;
	/* called from timer interrupt
	 * call some spi magic and update buffer
	 */

	for (i=0; i< sizeof(magnet_data); i++){
		magnet_data[i] = dummy3;
	}
	dummy3++;
}

void updateTempData(void)
{
	int i = 0;
	static U8 dummy4;
	/* called from timer interrupt
	 * call some spi magic and update buffer
	 */

	for (i=0; i< sizeof(temp_data); i++){
		temp_data[i] = dummy4;
	}
	dummy4++;
}

void updateVrefData(void)
{
	int i = 0;
	static U8 dummy;
	/* called from timer interrupt
	 * call some spi magic and update buffer
	 */

	for (i=0; i< sizeof(vref_data); i++){
		vref_data[i] = dummy;
	}
	dummy++;
}

void DoWorkUpdateInBuffer(void)
{
	static int count;
	int offset=0;

	/* call functions to grab spi data from accel,rot, mag,etc...*/

	disableIRQ();
	/* copy the data to the buffer with interrupts off.*/

	memcpy((in_buffer+offset), accel_data, sizeof(accel_data)); offset += sizeof(accel_data);
	memcpy((in_buffer+offset), rotation_data, sizeof(rotation_data)); offset += sizeof(rotation_data);
	memcpy((in_buffer+offset), magnet_data, sizeof(magnet_data)); offset += sizeof(magnet_data);
	memcpy((in_buffer+offset), temp_data, sizeof(temp_data)); offset += sizeof(temp_data);
	memcpy((in_buffer+offset), vref_data, sizeof(vref_data)); offset += sizeof(vref_data);

	memcpy((in_buffer+offset), &count, sizeof(int)); offset += sizeof(int);

	enableIRQ();
	count ++;
}

#define BAUD_RATE	38400

/*************************************************************************
	main
	====
**************************************************************************/
int main(void)
{
	int i;

	// PLL and MAM
	HalSysInit();

	for (i=0; i<MAX_PACKET_SIZE;i++)
		in_buffer[i]=i;

#ifdef LPC214x
	// init DBG
	ConsoleInit(60000000 / (16 * BAUD_RATE));
#else
	// init DBG
	ConsoleInit(72000000 / (16 * BAUD_RATE));
#endif

	DBG("Initialising USB stack\n");

	// initialise stack
	USBInit();

	// register device descriptors
	USBRegisterDescriptors(abDescriptors);

	// override standard request handler
	USBRegisterRequestHandler(REQTYPE_TYPE_VENDOR, HandleVendorRequest, abVendorReqData);

	// register endpoints
	USBHwRegisterEPIntHandler(BULK_IN_EP, _HandleBulkIn);
	USBHwRegisterEPIntHandler(BULK_OUT_EP, _HandleBulkOut);

	DBG("Starting USB communication\n");

	// connect to bus
	USBHwConnect(TRUE);

	// call USB interrupt handler continuously
	while (1) {
		updateAccelData();
		updateRotationData();
		updateMagnetData();
		updateTempData();
		updateVrefData();
		DoWorkUpdateInBuffer();
		USBHwISR();
	}

	return 0;
}

February 1, 2009

bugs with python web development with django chapter 2

Filed under: Uncategorized — admin @ 12:37 pm

Why do I always hit issues working though these books?  I’m using Django-1.0.2

sample chapter 2 here: http://www.informit.com/articles/article.aspx?p=1273658

I worked through the blog demo application, verbatim (ok, almost verbatum), and sure enough I hit errors near the end of the chapter.  I only changed the project and app names.

It seems that the admin.register code in the modles.py is getting executed mutliple times.

http://adil.2scomplement.com/2008/09/django-the-model-mymodel-is-already-registered/

I found 2 workarounds to this issue one makes sense the other no-so-much.

1) change the import lines from the form:

from project.app.modles import BlogPost

to

from app.models import BlogPost

everywhere in my app.  (note: this is what the source code from www.withpthon.com has)

2) move the admin.site.register code into a new file called admin.py within my app.  This is the workaround that seems to be community recommended and demonstrated in the official Django tutorial.

http://docs.djangoproject.com/en/dev/intro/tutorial02/#make-the-poll-app-modifiable-in-the-admin

My problem with this (beyond the fact the code in the book failed me) is why does the first work around work at all?

Powered by WordPress