ZigEmbeddedGroup / regz

Generate zig code from ATDF or SVD files for microcontrollers.
MIT License
82 stars 29 forks source link

Issues with: warning: mismatching names for name-in-module #58

Closed aowron closed 1 year ago

aowron commented 1 year ago

Atdf file (with the wrong extension due to github): ATmega3209.txt

Description: Trying to generate a zig file for the ATmega3209, using the pack at: https://packs.download.microchip.com/#collapse-Microchip-ATmega-DFP-pdsc), but getting these errors:

$ regz -s atdf ATmega3209.atdf warning: mismatching names for name-in-module: AC, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: ADC, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: PORT, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: PORT, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: PORT, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: PORT, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: PORT, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: PORT, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: SPI, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: TCA, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: TCB, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: TCB, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: TCB, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: TCB, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: TWI, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: USART, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: USART, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: USART, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: USART, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: VPORT, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: VPORT, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: VPORT, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: VPORT, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: VPORT, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz warning: mismatching names for name-in-module: VPORT, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz error: NoCaption /home/aowron/Gitjunk/regz/src/Database.zig:621:29: 0x298540 in initFromAtdf (regz) return error.NoCaption, ^ /home/aowron/Gitjunk/regz/src/main.zig:157:18: 0x2cea7e in parseXmlDatabase (regz) .atdf => try Database.initFromAtdf(allocator, doc), ^ /home/aowron/Gitjunk/regz/src/main.zig:114:24: 0x28ad8c in mainImpl (regz) break :blk try parseXmlDatabase(allocator, doc, schema.?); ^ /home/aowron/Gitjunk/regz/src/main.zig:26:17: 0x28a0c1 in main (regz) else => return err,

While digging a bit into the code (without any real knowledge of the regz codebase, and just starting out with zig), around this section (line 590 in Database.zig):

while (instance_it != null) : (instance_it = xml.findNode(instance_it.?.next, "instance")) {
    const instance_nodes = instance_it.?.children orelse continue;
    const instance_name = xml.getAttribute(instance_it, "name") orelse return error.NoInstanceName;
    if (xml.findNode(instance_nodes, "register-group")) |register_group| {
        const group_name = xml.getAttribute(register_group, "name") orelse return error.NoRegisterGroupName;
        const name_in_module = xml.getAttribute(register_group, "name-in-module") orelse return error.NoNameInModule;

        if (!std.mem.eql(u8, instance_name, group_name) or !std.mem.eql(u8, group_name, name_in_module)) {
            std.log.warn("mismatching names for name-in-module: {s}, ignoring, if you see this please cut a ticket: https://github.com/ZigEmbeddedGroup/regz", .{
                name_in_module,
            });
            continue;
        }

        try peripheral_instances.put(try db.arena.allocator().dupe(u8, name_in_module), {});
    }
}

and comparing that section to the atdf file, it looks like the offending peripherals should match on at least std.mem.eql(u8, instance_name, group_name), however, it seems like they for some reason are using a different name for name-in-module and and the register-group/instance name:

<instance name="ADC0">
                  <register-group address-space="data"
                                  name="ADC0"
                                  name-in-module="ADC"
                                  offset="0x0600"/>
ikskuh commented 1 year ago

The problem here is that the ADTF file contains a single definition for, but several instances of the same module.

This means that regz needs to generate a single type ADC which is then instantiated for ADC0. Same for PORT module and instances VPORTA, VPORTB, VPORTC.

This will need some bigger refactorings, sadly

mattnite commented 1 year ago

I took a look at it tonight and will have time Saturday to implement. Luckily it completely fits within the current data model, it just requires some extra processing while parsing.