From da4f5bdaff32aefc7b364706cff952c8ad26c570 Mon Sep 17 00:00:00 2001 From: Marcel Moolenaar Date: Sun, 30 Jun 2019 02:29:12 +0000 Subject: [PATCH] Add support for Python 3 and make it the default. Python 2.7 will retire on Januari 1, 2020. --- tools/bus_space/Python/Makefile | 8 ++- tools/bus_space/Python/lang.c | 68 +++++++++++++++++++---- tools/bus_space/examples/am79c900_diag.py | 22 ++++---- 3 files changed, 75 insertions(+), 23 deletions(-) diff --git a/tools/bus_space/Python/Makefile b/tools/bus_space/Python/Makefile index 5bc9b77140ac..2097c8e3824a 100644 --- a/tools/bus_space/Python/Makefile +++ b/tools/bus_space/Python/Makefile @@ -3,7 +3,11 @@ SHLIB_NAME= bus.so SRCS= lang.c -CFLAGS+= -I${.CURDIR}/.. -I/usr/local/include/python2.7 -LDFLAGS+= -L/usr/local/lib -lpython2.7 +# Set PYTHON to the version to compile against. +# E.g. "python2.7", "python3.6", etc... +PYTHON= python3.6m + +CFLAGS+= -I${.CURDIR}/.. -I/usr/local/include/${PYTHON} +LDFLAGS+= -L/usr/local/lib -l${PYTHON} .include diff --git a/tools/bus_space/Python/lang.c b/tools/bus_space/Python/lang.c index 0fde8fc1bb62..4fe3e6ee3d13 100644 --- a/tools/bus_space/Python/lang.c +++ b/tools/bus_space/Python/lang.c @@ -412,6 +412,12 @@ busdma_sync_range(PyObject *self, PyObject *args) Py_RETURN_NONE; } +/* + * Module methods and initialization. + */ + +static char bus_docstr[] = "Access to H/W bus memory and register areas."; + static PyMethodDef bus_methods[] = { { "read_1", bus_read_1, METH_VARARGS, "Read a 1-byte data item." }, { "read_2", bus_read_2, METH_VARARGS, "Read a 2-byte data item." }, @@ -431,6 +437,9 @@ static PyMethodDef bus_methods[] = { { NULL, NULL, 0, NULL } }; +static char busdma_docstr[] = "A bus- and device-independent interface" + " to Direct Memory Access (DMA) mechanisms."; + static PyMethodDef busdma_methods[] = { { "tag_create", busdma_tag_create, METH_VARARGS, "Create a root tag." }, @@ -470,18 +479,12 @@ static PyMethodDef busdma_methods[] = { { NULL, NULL, 0, NULL } }; -PyMODINIT_FUNC -initbus(void) +static PyObject * +module_initialize(PyObject *bus, PyObject *busdma) { - PyObject *bus, *busdma; - bus = Py_InitModule("bus", bus_methods); - if (bus == NULL) - return; - busdma = Py_InitModule("busdma", busdma_methods); - if (busdma == NULL) - return; - PyModule_AddObject(bus, "dma", busdma); + if (bus == NULL || busdma == NULL) + return (NULL); PyModule_AddObject(busdma, "MD_BUS_SPACE", Py_BuildValue("i", 0)); PyModule_AddObject(busdma, "MD_PHYS_SPACE", Py_BuildValue("i", 1)); @@ -491,4 +494,49 @@ initbus(void) PyModule_AddObject(busdma, "SYNC_POSTREAD", Py_BuildValue("i", 2)); PyModule_AddObject(busdma, "SYNC_PREWRITE", Py_BuildValue("i", 4)); PyModule_AddObject(busdma, "SYNC_POSTWRITE", Py_BuildValue("i", 8)); + + PyModule_AddObject(bus, "dma", busdma); + return (bus); } + +#if PY_MAJOR_VERSION >= 3 + +static struct PyModuleDef bus_module = { + PyModuleDef_HEAD_INIT, + "bus", + bus_docstr, + -1, + bus_methods, +}; + +static struct PyModuleDef busdma_module = { + PyModuleDef_HEAD_INIT, + "busdma", + busdma_docstr, + -1, + busdma_methods, +}; + +PyMODINIT_FUNC +PyInit_bus(void) +{ + PyObject *bus, *busdma; + + bus = PyModule_Create(&bus_module); + busdma = PyModule_Create(&busdma_module); + return (module_initialize(bus, busdma)); +} + +#else /* PY_MAJOR_VERSION >= 3 */ + +PyMODINIT_FUNC +initbus(void) +{ + PyObject *bus, *busdma; + + bus = Py_InitModule3("bus", bus_methods, bus_docstr); + busdma = Py_InitModule3("busdma", busdma_methods, busdma_docstr); + (void)module_initialize(bus, busdma); +} + +#endif /* PY_MAJOR_VERSION >= 3 */ diff --git a/tools/bus_space/examples/am79c900_diag.py b/tools/bus_space/examples/am79c900_diag.py index bcea31b32460..3a15f60a91b8 100644 --- a/tools/bus_space/examples/am79c900_diag.py +++ b/tools/bus_space/examples/am79c900_diag.py @@ -54,7 +54,7 @@ sys.path.append('/usr/lib') import bus -import busdma +from bus import dma as busdma # ILACC initialization block definition @@ -161,7 +161,7 @@ def ip_str(a): def mac_is(l, r): - for i in xrange(6): + for i in range(6): if l[i] != r[i]: return False return True @@ -203,7 +203,7 @@ def stop(): mac = () bcast = () -for o in xrange(6): +for o in range(6): mac += (bus.read_1(io, o),) bcast += (0xff,) logging.info('ethernet address = ' + MACFMT % mac) @@ -237,23 +237,23 @@ def stop(): ib = initblock.from_address(addr_initblock) ib.mode = ((ffs(ntxbufs) - 1) << 28) | ((ffs(nrxbufs) - 1) << 20) -for i in xrange(len(mac)): +for i in range(len(mac)): ib.hwaddr[i] = mac[i] -for i in xrange(4): +for i in range(4): ib.filter[i] = 0xffff ib.rxdesc = busaddr + (addr_rxdesc - cpuaddr) ib.txdesc = busaddr + (addr_txdesc - cpuaddr) ib._pad1_ = 0 ib._pad2_ = 0 -for i in xrange(nrxbufs): +for i in range(nrxbufs): bd = bufdesc.from_address(addr_rxdesc + ctypes.sizeof(bufdesc) * i) bd.buffer = busaddr + (addr_rxbufs - cpuaddr) + bufsize * i bd.flags = (1 << 31) | (15 << 12) | (-bufsize & 0xfff) bd.length = 0 bd._pad_ = 0 -for i in xrange(ntxbufs): +for i in range(ntxbufs): bd = bufdesc.from_address(addr_txdesc + ctypes.sizeof(bufdesc) * i) bd.buffer = busaddr + (addr_txbufs - cpuaddr) + bufsize * i bd.flags = (15 << 12) @@ -280,10 +280,10 @@ def stop(): pkt = packet.from_address(addr_txbufs) ctypes.memset(addr_txbufs, 0, ctypes.sizeof(pkt)) options = [53, 1, 1] -for i in xrange(len(options)): +for i in range(len(options)): pkt.dhcp_options[i] = options[i] pkt.dhcp_magic = 0x63825363 -for i in xrange(6): +for i in range(6): pkt.bootp_chaddr[i] = mac[i] pkt.bootp_hlen = 6 pkt.bootp_htype = 1 @@ -298,7 +298,7 @@ def stop(): pkt.ip_len = ctypes.sizeof(pkt) - 14 pkt.ip_vl = 0x45 pkt.eth_type = 0x0800 -for i in xrange(6): +for i in range(6): pkt.eth_src[i] = mac[i] pkt.eth_dest[i] = bcast[i] pktlen = ctypes.sizeof(pkt) @@ -325,7 +325,7 @@ def stop(): busdma.sync_range(dmamem, busdma.SYNC_PREWRITE, addr_rxdesc - cpuaddr, ctypes.sizeof(bufdesc) * nrxbufs) -for i in xrange(nrxbufs): +for i in range(nrxbufs): bd = bufdesc.from_address(addr_rxdesc + ctypes.sizeof(bufdesc) * i) if (bd.flags & 0x80000000): continue