WizNet5k MAC-Address

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
infiniteWays
Posts: 2
Joined: Tue Sep 29, 2020 7:47 pm

WizNet5k MAC-Address

Post by infiniteWays » Tue Sep 29, 2020 8:00 pm

Good evening, morning, day micropython community!

I'm currently looking for a way to set a MAC-address when using micropython and the W5500 chip.
Is there currently an easy way to do that or do I have to fiddle with the driver or sth. like that?

Might also be a stupid question, but I would appreciate some insight into this!

Thank you very much :)
Dennis

infiniteWays
Posts: 2
Joined: Tue Sep 29, 2020 7:47 pm

Re: WizNet5k MAC-Address

Post by infiniteWays » Wed Sep 30, 2020 8:51 pm

So after a bit of research, I didn't find an easy way to do this, so - with my very, very limited C knowledge - I found the respective position in the wiznet code where I could embed a new module. It might not be the prettiest, but it works and maybe somebody can use it.

Syntax is similar to the ifconfig:

Code: Select all

network.mac([ mac ])
Leaving the argument empty returns the currently set MAC, passing a list with the different parts of the MAC sets it.

It's integrated into the modnwwiznet5k.c file at /ports/stm32:

Code: Select all

STATIC mp_obj_t wiznet5k_mac(size_t n_args, const mp_obj_t *args){
  wiz_NetInfo netinfo;
  ctlnetwork(CN_GET_NETINFO, &netinfo);
  if (n_args == 1) {
      // get
      for(int i = 0; i < 6; i++){
        printf("%02X", netinfo.mac[i]);
        if(i < 5){
          printf(":");
        }
      }
      printf("\n");
      return mp_const_none;
  } else {
      // set
      mp_obj_t *rawmac;
      mp_obj_get_array_fixed_n(args[1], 6, &rawmac);
      netinfo.mac[0] = mp_obj_get_int(rawmac[0]);
      netinfo.mac[1] = mp_obj_get_int(rawmac[1]);
      netinfo.mac[2] = mp_obj_get_int(rawmac[2]);
      netinfo.mac[3] = mp_obj_get_int(rawmac[3]);
      netinfo.mac[4] = mp_obj_get_int(rawmac[4]);
      netinfo.mac[5] = mp_obj_get_int(rawmac[5]);
      ctlnetwork(CN_SET_NETINFO, &netinfo);
      return mp_const_none;
  }
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(wiznet5k_mac_obj, 1, 2, wiznet5k_mac);
as well as an addition in the wiznet5k_locals_dict_table constant:

Code: Select all

    { MP_ROM_QSTR(MP_QSTR_mac), MP_ROM_PTR(&wiznet5k_mac_obj) }
    

Post Reply