Debuggin embedded python: building device drivers for an embedded python virtual machine

Cesar Lopez clopez at abo.fi
Wed Mar 22 07:43:14 EST 2000


BUILDING DEVICE DRIVERS FOR AN EMBEDDED PYTHON VIRTUAL MACHINE

1. INTRODUCTION

        In building device drivers for an embedded Python Virtual
Machine, we implement a guided method that
 simplifies the procedure of building device drivers for our embedded
Python library. Therewith, we could run
 Pythons programs in our embedded system that, get communication with
our devices system.

    This project it is the continuation of "Downsizing Python for Small
Systems" [DPSS99], made by Ra\xfal Parra Bacete
 from Politechnic University of Valencia (Spain). In this new Python
library all operating systems dependencies and
  other unused features have been erased. Finally, Ra\xfal got a reduced

Python library "libpython1.5.a" with the basic
   python features includes in it. Ra\xfal developed an environment to
interface the embedded Python library with our
   Hitachi SH-1 evaluation board. This environment allows us to run
compiled Python programs in our embedded system.

WHAT WE WANT?

We want develop a new python embedded library with new device driver
features and we need test it, too. We need
implement this things in order to get the final Python library:^M

- A C driver that Setup our device. First we try with the simples one,
the green led. led.c
- An interface in order to be able to call the C driver Setup function
with python programs. led_wrap.c
- A python device object model system. devicesystem.py
- A new python library with device driver features included.
libpython1.5.a
- A test python program that uses the device object model. test.py
- A compiled binary program to run in our embedded system. test.x: for
this issue we need an interface to embed the test.
py program in a test.c in order to compile it with the new library and
get the final test.x file . I use Raul's interface
 environment "genc" and Makefile.

First I have developed all the system for the Linux machine, EDISON and
after that I have port to our embedded system.

WHAT I HAVE DO IT?

I have:
- re-design a C driver that light on /off the green led on the SH-1
Evaluation Board. led.c
- compiled and test it on the SH-1 Evaluation Board. led.x
- develop a driver version that print on the screen on/off. led.c
- wrapped this file using SWIG and I have done it on my own using the
Python API/C. With this wrapped file
    I can call the driver Setup function with the python interpreter
that we have installed in EDISON. led_wrap.c
- built a Python device object model. devicesystem.dy
- got a copy of the installed python library and I have added these two
new files with the driver emulator and
     a new python configuration file to get a new python library.
libpython1.5.a
- built a test python program with the device object model. test.py
test.pyc
- got the test.c with test.pyc embed in it using "genc" developed by
Ra\xfal Parra.
- got an executable file, test using the library with the driver
emulator to compile it.
- run this program and works properly in EDISON

I have done the same using the SH-1 C compiler, finally, I have got a
test.x file that runs on the embedded machine.
 This programs runs to the end of the code, but the led do not light on.

 To debug test.c I\xb4m usind DDD for my SH-1, now I can debug into
Py_EvalCode(), but I don\xb4t know when this function
 call
 to my setup led function. I have inserted some brak points in Setup()
function and in _wrap_Setup(), but the
 debugger do not stop. I think that the call to this functions fail.

To add the new objects to the old library I use the next script:

#here link embedded library with my led library lib.a (led.o led_wrap.o)
and config.o
#firs compile config.c led.c led_wrap.c with the correct Include and
config.h files
#After that we link with the library


#compiling
sh-hms-gcc -g -I/home/cesar/library/emblib/raulpython/Include
-DHAVE_CONFIG_H -c config.c
sh-hms-gcc -g -I/home/cesar/library/emblib/raulpython/Include
-DHAVE_CONFIG_H -c led.c
sh-hms-gcc -g -I/home/cesar/library/emblib/raulpython/Include
-DHAVE_CONFIG_H -c led_wrap.c

#linking
sh-hms-ar cr libpython1.5.a  led.o  led_wrap.o config.o

#extrac libpython1.5.a content
sh-hms-nm -s libpython1.5.a > listlibpython.txt



[DPSS99] more info in http://www.abo.fi/~iporres/python/small.html

Here is the code of my led driver, led.h, led.c:

//led.h

int Setup(int state); // state = 1 --> ON ; state = 0 --> OFF
int static _state;


//led.c
// Defining communication with the device, Port, Adress, configuration
registers and so  on ...
#define PBIOR (*(volatile short int *) (0x5ffffc6))
#define PBCR1 (*(volatile short int *) (0x5ffffcc))
#define PBCR2 (*(volatile short int *) (0x5ffffce))
#define PBDR (*(volatile short int *) (0x5ffffc2))
#include "led.h"

int
Setup(int state)
{
PBIOR |= 0x8000;        // Set the bit for output
PBCR1 &= ~0xc000;       // Make it a normal I/0

        switch (state){
            case 1:{
                PBDR |= 0x8000; // Set the LED on
                //printf("on\n");
                //_state=1; // to read the state of the led
                return 0;
                }
            case 0:{
                PBDR &= 0x0000; // Set the LED off
                //printf("off\n");
                //_state=0;
                return 0;
                }
            default: return -1;  // Error: Invalid code
            } //switch
}

Here is my wrap file, using API/C code, no SWIG, but I use SWIG to and
doesn´t work with the embedded machine.:

#include "Python.h"
#include "led.h"


static PyObject *_wrap_Setup(PyObject *self, PyObject *args) {
    PyObject * _resultobj;
    int  _result;
    int  _arg0;

    self = self;
    if(!PyArg_ParseTuple(args,"i:Setup",&_arg0))
        return NULL;
    _result = (int )Setup(_arg0);
    _resultobj = Py_BuildValue("i",_result);
    return _resultobj;
}


static PyMethodDef ledMethods[] = {
         { "Setup", _wrap_Setup, 1 },
         { NULL, NULL }
};

void
initled()
{
          Py_InitModule("led", ledMethods);
}


Do I need to include "extern int Setup(int );" before *_wrap_Setup()?



Cesar Lopez .................... perhaps some day I will get it.






More information about the Python-list mailing list