diff --git a/3rd/periphery/src/periphery.c b/3rd/periphery/src/periphery.c index ebe9b662..32f4a5d1 100644 --- a/3rd/periphery/src/periphery.c +++ b/3rd/periphery/src/periphery.c @@ -1644,11 +1644,9 @@ static bool cfunc__serial_set_parity(int argc, py_Ref argv) { serial_t* _0; if(!py_checkint(py_arg(0))) return false; _0 = (serial_t*)py_toint(py_arg(0)); - enum serial_parity _1; - do { - if(!py_checktype(py_arg(1), tp_user_enum serial_parity)) return false; - _1 = *(enum serial_parity*)py_touserdata(py_arg(1)); - } while(0); + serial_parity_t _1; + if(!py_checkint(py_arg(1))) return false; + _1 = (serial_parity_t)py_toint(py_arg(1)); int res = serial_set_parity(_0, _1); py_newint(py_retval(), res); return true; diff --git a/ffigen/ffigen/meta/parser.py b/ffigen/ffigen/meta/parser.py index cd69c3fc..bcc12a76 100644 --- a/ffigen/ffigen/meta/parser.py +++ b/ffigen/ffigen/meta/parser.py @@ -33,7 +33,7 @@ class Header: self.type_aliases[k] = v def build_enum(self, node: c_ast.Enum, alias_name: str | None = None): - enum = Enum(node.name) + enum = Enum(node.name, alias_name) for item in node.values.enumerators: enum.values.append(item.name) self.types.append(enum) diff --git a/ffigen/ffigen/meta/schema.py b/ffigen/ffigen/meta/schema.py index 3157f4af..49b65e73 100644 --- a/ffigen/ffigen/meta/schema.py +++ b/ffigen/ffigen/meta/schema.py @@ -23,8 +23,9 @@ class Struct(NamedFields): pass class Union(NamedFields): pass class Enum: - def __init__(self, name: str): + def __init__(self, name: str, typedef_name: str | None = None): self.name = name + self.typedef_name = typedef_name self.values = [] # type: list[str] def __repr__(self): diff --git a/ffigen/gen_periphery.py b/ffigen/gen_periphery.py index 13acb453..a2d4f450 100644 --- a/ffigen/gen_periphery.py +++ b/ffigen/gen_periphery.py @@ -10,6 +10,17 @@ file_dir = os.path.dirname(os.path.abspath(__file__)) path = '3rd/periphery/include/periphery.h' code = pcpp.CmdPreprocessor([None, path, '-o', 'tmp.h', '-I', os.path.join(file_dir, 'libc_include')]) +mapping = { + 'enum serial_parity parity': 'serial_parity_t parity', +} +# remap tmp.h +with open('tmp.h', 'r') as f: + content = f.read() +for k, v in mapping.items(): + content = content.replace(k, v) +with open('tmp.h', 'w') as f: + f.write(content) + ast = pycparser.parse_file('tmp.h') os.remove('tmp.h') diff --git a/include/typings/periphery.pyi b/include/typings/periphery.pyi index d967056c..0ae1548e 100644 --- a/include/typings/periphery.pyi +++ b/include/typings/periphery.pyi @@ -352,8 +352,8 @@ def serial_set_baudrate(serial: intptr, baudrate: int, /) -> int: def serial_set_databits(serial: intptr, databits: int, /) -> int: """Wraps `int serial_set_databits(serial_t* serial, unsigned databits)`""" -def serial_set_parity(serial: intptr, parity: enum serial_parity, /) -> int: - """Wraps `int serial_set_parity(serial_t* serial, enum serial_parity parity)`""" +def serial_set_parity(serial: intptr, parity: int, /) -> int: + """Wraps `int serial_set_parity(serial_t* serial, serial_parity_t parity)`""" def serial_set_stopbits(serial: intptr, stopbits: int, /) -> int: """Wraps `int serial_set_stopbits(serial_t* serial, unsigned stopbits)`"""