Skip to content

stm32: Use lib/wiznet5k instead of drivers/wiznet5k. #9021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from

Conversation

robert-hh
Copy link
Contributor

@robert-hh robert-hh commented Aug 5, 2022

This PR uses lib/wiznet5k instead of drivers/wiznet5k, like the rp2040 port already does. The change was initiated by @omogenot, and I picked up the baton for test, bug fixing and completion. See the thread at issue #9013. I tested the code change with a Wiznet W5500 breakout, with and without LWIP support. The performance is equivalent to the W5500 support for RP2040.

One Note:

The last commit in this PR consists of the combined changes of PR #8974, so testing runs well. This last commit can either be removed, or the other PR gets obsolete.

@andrewleech
Copy link
Contributor

Thanks for this @robert-hh and @omogenot. I was keen to see this happen, but between not having any stm&wiznet hardware to develop against and too many in-progress projects/pr's already it had fallen down my list.

@robert-hh
Copy link
Contributor Author

I dropped the last commit, now that PR #8974 is merged.

@dpgeorge
Copy link
Member

dpgeorge commented Aug 9, 2022

Thanks, this is good. I've tested it on a PYBv1.0 with W5200 and it works.

But the change to spi_from_mp_obj() is not correct. Instead it needs #9034.

@dpgeorge
Copy link
Member

dpgeorge commented Aug 9, 2022

Also, I think the drivers/wiznet5k/ directory can now be removed. Could you please do that as a final commit in this PR?

@robert-hh
Copy link
Contributor Author

But the change to spi_from_mp_obj() is not correct. Instead it needs #9034.

That was the part I was not sure about. Will you merge 9034 first?

Also, I think the drivers/wiznet5k/ directory can now be removed. Could you please do that as a final commit in this PR?

Can do that. I kept it for now because I was not sure whether it was needed by a port.

@dpgeorge
Copy link
Member

I have now merged #9034. So please rebase and rework this on top of that (should be easy).

If you could also delete the old drivers as a separate commit, that would be great. Then CI can test it as well.

@robert-hh
Copy link
Contributor Author

The PR is updated. I added the function mp_hal_get_spi_obj() to spi.c, but kept spi_from_mp_obj() is kept, because it is used by the 33ck driver, and I cannot test that.

@omogenot
Copy link
Contributor

@robert-hh
I found yet another little glitch in the wiznet5k driver. In the file extmod/network_wiznet5k.c the function wiznet5k_gethostbyname uses a fixed dns sever address (i.e. Google public DNS 8.8.8.8) instead of using the one provided by the ifconfig method call (whether it is fixed or set by dhcp).
The function shall become something like:

STATIC int wiznet5k_gethostbyname(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *out_ip) {
    uint8_t dns_ip[MOD_NETWORK_IPADDR_BUF_SIZE] = {8, 8, 8, 8};
    uint8_t *buf = m_new(uint8_t, MAX_DNS_BUF_SIZE);
    DNS_init(2, buf);
    if (wiznet5k_obj.netinfo.dns[0]) {
        memcpy(dns_ip, wiznet5k_obj.dns, MOD_NETWORK_IPADDR_BUF_SIZE);
    }
    mp_int_t ret = DNS_run(dns_ip, (uint8_t *)name, out_ip);
    m_del(uint8_t, buf, MAX_DNS_BUF_SIZE);
    if (ret == 1) {
        // success
        return 0;
    } else {
        // failure
        return -2;
    }
}

To set dns server address to the one provided by dhcp or fixed ifconfig, or use public Google DNS if none was given (a.k.a. the provided dns server address started by 0). I have some customers who do not allow Internet access to devices and provide their own internal dns servers instead.

The function spi_from_mp_obj() is kept, since it is used by the
cc3k driver, which I cannot test, and so it is untouched.
- Add lib/wiznet5k into the 'make submodules' step.
- Split the stm32 builds for wiznet5k and cc3k.
- Run 'make .... clean' after making the wiznet5k build.
@robert-hh
Copy link
Contributor Author

Of course I can add that. It may however also be a separate commit, since it is not related to moving the files. But in any case: how can I tell that it works? I had expected, that it is used by socket.getaddrinfo(). But it's not.

@omogenot
Copy link
Contributor

@robert-hh
You're right, this is more academic than anything else 😉. I just stumbled on it and wanted to mention it, not knowing if it was worth a PR just for this 😄.
You're right again, to test it it won't be easy. It's used by socket.getaddrinfo() only when LWIP is not used, in extmod/modusocket.c, in mod_usocket_getaddrinfo if the provided host parameter is not an IP value.
One day, someone shall take the time to make an array of all boards definitions across all ports and determine their configuration (main constants values): which is using LWIP or not, RAM size, ROM size, Ethernet/Wifi, etc. It's probably a huge task but could be helpful in the end to help testing features.

@robert-hh
Copy link
Contributor Author

To compile properly, it should be:

STATIC int wiznet5k_gethostbyname(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *out_ip) {
    uint8_t dns_ip[MOD_NETWORK_IPADDR_BUF_SIZE] = {8, 8, 8, 8};
    uint8_t *buf = m_new(uint8_t, MAX_DNS_BUF_SIZE);
    DNS_init(2, buf);
    if (wiznet5k_obj.netinfo.dns[0]) {
        memcpy(dns_ip, wiznet5k_obj.netinfo.dns, MOD_NETWORK_IPADDR_BUF_SIZE);
    }
    mp_int_t ret = DNS_run(dns_ip, (uint8_t *)name, out_ip);
    m_del(uint8_t, buf, MAX_DNS_BUF_SIZE);
    if (ret == 1) {
        // success
        return 0;
    } else {
        // failure
        return -2;
    }
}

But without LWIP, the nic does not come up. So there is another problem in this PR.

@omogenot
Copy link
Contributor

@robert-hh
Thanks for the tips fix. I don't know which config uses W5500 chip and does not uses LWIP, but for tests I temporarily removed LWIP from W5500_EVB_PICO and it works for me without IRQ as it's not supported (fixed config, I don't have a dhcp server as I use a direct cable link). I can ping the device.
Note that apparently the extmod/network_wiznet5k.c will require some future adjustments in another PR to complete some functions:

STATIC int wiznet5k_socket_setsockopt(mod_network_socket_obj_t *socket, mp_uint_t level, mp_uint_t opt, const void *optval, mp_uint_t optlen, int *_errno) {
    // TODO
    *_errno = MP_EINVAL;
    return -1;
}

STATIC int wiznet5k_socket_settimeout(mod_network_socket_obj_t *socket, mp_uint_t timeout_ms, int *_errno) {
    // TODO
    *_errno = MP_EINVAL;
    return -1;
...
}

But this is another story.
What is the configuration/board you are using for your failing tests? I doubt having one available, but one never knows...

@robert-hh
Copy link
Contributor Author

False alarm. This config requires nic.active(True), while the previous set-up did not have the active() method at all. Yes, the changed wiznet5k_gethostbyname() is used and picks up the DNS address from the wiznet structure. I cannot check the actual DNS query with wireshark, since I do not own a hub or switch, which can be configured as hub. So I cannot see the traffic between the PyBoard and the router.

Instead of the default 8.8.8.8.
The change was suggested by @omogenot.
This change could go into a separate PR, but since it is related to
this wiznet5k topic, I added it here, avoiding another PR.
@codecov-commenter
Copy link

Codecov Report

Merging #9021 (aac8e0d) into master (c616721) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master    #9021   +/-   ##
=======================================
  Coverage   98.37%   98.37%           
=======================================
  Files         156      156           
  Lines       20424    20424           
=======================================
  Hits        20092    20092           
  Misses        332      332           

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@dpgeorge
Copy link
Member

Thanks for this. Now merged in 7179240 through 8308f9c (which includes the DNS fix suggested above).

I also reverted the 1-line change to spi_from_mp_obj() which was incorrect and no longer needed.

@dpgeorge dpgeorge closed this Aug 23, 2022
@robert-hh robert-hh deleted the stm32_wizlib branch August 23, 2022 08:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants