Skip to content

Commit a30faa7

Browse files
authored
Merge pull request ARMmbed#17 from kegilbert/mac_addr_set2
Add MAC address set function
2 parents 8a68f5c + e76f095 commit a30faa7

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

Diff for: platform/mbed_interface.c

+16
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ WEAK int mbed_uid(char *uid) {
8181
}
8282
#endif
8383

84+
static uint8_t manual_mac_address_set = 0;
85+
static char manual_mac_address[6];
86+
87+
void mbed_set_mac_address(const char *mac, uint8_t coerce_mac_control_bits) {
88+
memcpy(manual_mac_address, mac, 6);
89+
manual_mac_address_set = 1;
90+
if(coerce_mac_control_bits) {
91+
manual_mac_address[0] |= 0x02; // force bit 1 to a "1" = "Locally Administered"
92+
manual_mac_address[0] &= 0xFE; // force bit 0 to a "0" = Unicast
93+
}
94+
}
95+
8496
WEAK void mbed_mac_address(char *mac) {
8597
#if DEVICE_SEMIHOST
8698
char uid[DEVICE_ID_LENGTH + 1];
@@ -101,12 +113,16 @@ WEAK void mbed_mac_address(char *mac) {
101113
mac[0] &= ~0x01; // reset the IG bit in the address; see IEE 802.3-2002, Section 3.2.3(b)
102114
} else { // else return a default MAC
103115
#endif
116+
if(manual_mac_address_set) {
117+
memcpy(mac, manual_mac_address, 6);
118+
} else {
104119
mac[0] = 0x00;
105120
mac[1] = 0x02;
106121
mac[2] = 0xF7;
107122
mac[3] = 0xF0;
108123
mac[4] = 0x00;
109124
mac[5] = 0x00;
125+
}
110126
#if DEVICE_SEMIHOST
111127
}
112128
#endif

Diff for: platform/mbed_interface.h

+12
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ int mbed_interface_uid(char *uid);
9898

9999
#endif
100100

101+
/** This provides a generic function to set a unique 6-byte MAC address based on the passed
102+
* parameter character array. Provides no MAC address validity checking
103+
*
104+
* mbed_mac_address() will readback this configured value
105+
*
106+
* @param mac A 6-byte array containing the desired MAC address to be set
107+
* @param coerce_mac_control_bits An 8-bit int that is used as a boolean flag to either coerce
108+
* the provided MAC address to be Locally Administered and Unicast or to use the
109+
* provided MAC address unmodified
110+
*/
111+
void mbed_set_mac_address(const char *mac, uint8_t coerce_mac_control_bits);
112+
101113
/** This returns a unique 6-byte MAC address, based on the interface UID
102114
* If the interface is not present, it returns a default fixed MAC address (00:02:F7:F0:00:00)
103115
*

0 commit comments

Comments
 (0)