Skip to content

Commit

Permalink
Experimental: PINT module bindings.
Browse files Browse the repository at this point in the history
P.I.N.T

Python Implementation of Network Transports.
  • Loading branch information
Gadgetoid committed May 3, 2024
1 parent 616b1cc commit d256b9e
Show file tree
Hide file tree
Showing 12 changed files with 635 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/micropython.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
board: RPI_PICO
- name: picow
board: RPI_PICO_W
- name: pico_pint
board: RPI_PICO_PINT
- name: tiny2040_8mb
board: PIMORONI_TINY2040
- name: picolipo_4mb
Expand Down
20 changes: 20 additions & 0 deletions micropython/board/RPI_PICO_PINT/board.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"deploy": [
"../deploy.md"
],
"docs": "",
"features": [
"Breadboard friendly",
"Castellated Pads",
"Micro USB"
],
"id": "rp2-pico",
"images": [
"rp2-pico.jpg"
],
"mcu": "rp2040",
"product": "Pico",
"thumbnail": "",
"url": "https://www.raspberrypi.com/products/raspberry-pi-pico/",
"vendor": "Raspberry Pi"
}
5 changes: 5 additions & 0 deletions micropython/board/RPI_PICO_PINT/manifest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include("$(PORT_DIR)/boards/manifest.py")

require("bundle-networking")

include("../manifest_pico.py")
7 changes: 7 additions & 0 deletions micropython/board/RPI_PICO_PINT/mpconfigboard.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# cmake file for Raspberry Pi Pico
set(PICO_BOARD "pico")

# Board specific version of the frozen manifest
set(MICROPY_FROZEN_MANIFEST ${MICROPY_BOARD_DIR}/manifest.py)

set(MICROPY_C_HEAP_SIZE 4096)
15 changes: 15 additions & 0 deletions micropython/board/RPI_PICO_PINT/mpconfigboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Board and hardware specific configuration
#define MICROPY_HW_BOARD_NAME "Raspberry Pi Pico"
#define MICROPY_HW_FLASH_STORAGE_BYTES (1408 * 1024)

// Enable networking.
#define MICROPY_PY_NETWORK 1
#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "Pico"

extern const struct _mp_obj_type_t mod_network_nic_type_pint;
#define MICROPY_HW_NIC_PINT { MP_ROM_QSTR(MP_QSTR_PINT), MP_ROM_PTR(&mod_network_nic_type_pint) },

#define MICROPY_BOARD_NETWORK_INTERFACES \
MICROPY_HW_NIC_PINT

#define MICROPY_PY_SOCKET_EXTENDED_STATE 1
28 changes: 28 additions & 0 deletions micropython/board/RPI_PICO_PINT/pins.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
GP0,GPIO0
GP1,GPIO1
GP2,GPIO2
GP3,GPIO3
GP4,GPIO4
GP5,GPIO5
GP6,GPIO6
GP7,GPIO7
GP8,GPIO8
GP9,GPIO9
GP10,GPIO10
GP11,GPIO11
GP12,GPIO12
GP13,GPIO13
GP14,GPIO14
GP15,GPIO15
GP16,GPIO16
GP17,GPIO17
GP18,GPIO18
GP19,GPIO19
GP20,GPIO20
GP21,GPIO21
GP22,GPIO22
GP25,GPIO25
GP26,GPIO26
GP27,GPIO27
GP28,GPIO28
LED,GPIO25
14 changes: 14 additions & 0 deletions micropython/modules/micropython-pico_pint.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
include_directories(${CMAKE_CURRENT_LIST_DIR}/../../)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../")

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

include(micropython-common)
include(pint/micropython)

# C++ Magic Memory
include(cppmem/micropython)
82 changes: 82 additions & 0 deletions micropython/modules/pint/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# P.I.N.T

### Python Implementation of Network Transports.

Want to glue your esoteric network device into MicroPython without delving
into C or trying to figure out lwIP? PINT is just the refreshment you need.

### Reference Implementation

PINT uses a Python class to implement a socket-based networking driver.

How you implement these functions is down to you- but you must expect/return
the correct data. Here's the basic skeleton:

```python
class PINT_Socket():
"""An arbitrary structure for storing data about your sockets.
Does not have to be a class, you could just return an int with the
socket ID of your target device.
"""
def __init__(self):
pass


class PINT_NIC:
"""The actual NIC implementation.
Most of the heavy lifting is sockets based, you might want to implement
the socket_ methods on your socket class and simply delegate to them.
"""
def __init__(self) -> None:
pass

def __del__(self) -> None:
pass

def gethostbyname(self, name: str) -> tuple[int, int, int, int]:
return (127, 0, 0, 1)

def socket_socket(self) -> PINT_Socket:
return PINT_Socket()

def socket_close(self, socket: PINT_Socket) -> None:
pass

def socket_bind(self, socket: PINT_Socket, ip: tuple[int, int, int, int], port: int) -> bool:
return True

def socket_listen(self, socket: PINT_Socket, backlog: int) -> bool:
return True

def socket_accept(self, socket: PINT_Socket, socket2: PINT_Socket, ip: tuple[int, int, int, int], port: int) -> bool:
return True

def socket_connect(self, socket: PINT_Socket, ip: tuple[int, int, int, int], port) -> bool:
return True

def socket_send(self, socket: PINT_Socket, buf: bytearray) -> int:
return 0

def socket_recv(self, socket: PINT_Socket, buf: bytearray) -> int:
"""Buf is provided as a mutable bytearray, you must write into it."""
return 0

def socket_sendto(self, socket: PINT_Socket, buf: bytearray, ip, port) -> int:
return 0

def socket_recvfrom(self, socket: PINT_Socket, buf: bytearray, ip, port) -> int:
"""Buf is provided as a mutable bytearray, you must write into it."""
return 0

def socket_setsockopt(self, socket: PINT_Socket, level: int, opt: int, val: bytearray) -> bool:
return True

def socket_settimeout(self, socket: PINT_Socket, timeout_ms: int) -> bool:
return True

def socket_ioctl(self, socket: PINT_Socket, request: int, arg: int) -> bool:
return True

```
18 changes: 18 additions & 0 deletions micropython/modules/pint/micropython.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
set(MOD_NAME pint)
string(TOUPPER ${MOD_NAME} MOD_NAME_UPPER)
add_library(usermod_${MOD_NAME} INTERFACE)

target_sources(usermod_${MOD_NAME} INTERFACE
${CMAKE_CURRENT_LIST_DIR}/${MOD_NAME}.c
${CMAKE_CURRENT_LIST_DIR}/${MOD_NAME}.cpp
)

target_include_directories(usermod_${MOD_NAME} INTERFACE
${CMAKE_CURRENT_LIST_DIR}
)

target_compile_definitions(usermod_${MOD_NAME} INTERFACE
MODULE_PINT_ENABLED=1
)

target_link_libraries(usermod INTERFACE usermod_${MOD_NAME})
52 changes: 52 additions & 0 deletions micropython/modules/pint/pint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "pint.h"

MP_DEFINE_CONST_FUN_OBJ_1(network_pint__del__obj, network_pint__del__);

static const mod_network_nic_protocol_t mod_network_nic_protocol_pint = {
.gethostbyname = network_pint_gethostbyname,
.deinit = network_pint_deinit,
.socket = network_pint_socket_socket,
.close = network_pint_socket_close,
.bind = network_pint_socket_bind,
.listen = network_pint_socket_listen,
.accept = network_pint_socket_accept,
.connect = network_pint_socket_connect,
.send = network_pint_socket_send,
.recv = network_pint_socket_recv,
.sendto = network_pint_socket_sendto,
.recvfrom = network_pint_socket_recvfrom,
.setsockopt = network_pint_socket_setsockopt,
.settimeout = network_pint_socket_settimeout,
.ioctl = network_pint_socket_ioctl,
};

static const mp_rom_map_elem_t pint_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&network_pint__del__obj) },
};

static MP_DEFINE_CONST_DICT(pint_locals_dict, pint_locals_dict_table);

MP_DEFINE_CONST_OBJ_TYPE(
mod_network_nic_type_pint,
MP_QSTR_pint,
MP_TYPE_FLAG_NONE,
make_new, network_pint_make_new,
locals_dict, &pint_locals_dict,
protocol, &mod_network_nic_protocol_pint
);

#ifdef NETWORK_PINT_MODULE
static const mp_map_elem_t pint_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_pint) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_PINT), (mp_obj_t)&mod_network_nic_type_pint },
};

static MP_DEFINE_CONST_DICT(mp_module_pint_globals, pint_globals_table);

const mp_obj_module_t pint_user_cmodule = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_pint_globals,
};

MP_REGISTER_MODULE(MP_QSTR_pint, pint_user_cmodule);
#endif
Loading

0 comments on commit d256b9e

Please sign in to comment.