From d937b67ee73aa0110ed45e129de241b7d3b4d42d Mon Sep 17 00:00:00 2001 From: chris-styles Date: Tue, 30 Jun 2015 03:18:36 +0100 Subject: [PATCH 1/7] Update mbed_overrides.c --- .../TARGET_FRDM/mbed_overrides.c | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/mbed_overrides.c b/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/mbed_overrides.c index c82f6cdcb0..3b2d70473a 100644 --- a/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/mbed_overrides.c +++ b/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/mbed_overrides.c @@ -34,21 +34,32 @@ void NMI_Handler(void) // Provide ethernet devices with a semi-unique MAC address from the UUID void mbed_mac_address(char *mac) { - // Fetch word 0 - uint32_t word0 = *(uint32_t *)0x40048060; - // Fetch word 1 + + // Making a random-as-possible MAC from 3 16 bit seeds and load of XOR'ing + uint16_t MAC[3]; // 3 16 bits words for the MAC + uint32_t UUID_LOC_BASE = 0x40048054; + + // three random seeds to make a MAC + MAC[0] = 0x2055; + MAC[1] = 0x5c44; + MAC[2] = 0x79fe; + + // XOR in each halfword of the UUID into each seed + for (int i=0;i<3;i++) { + for (int j=0;j<8;j++) { + MAC[i] ^= (*(uint32_t *)(UUID_LOC_BASE+(0x2*j))); + } + } + // we only want bottom 16 bits of word1 (MAC bits 32-47) - // and bit 1 forced to 1, bit 0 forced to 0 + // and bit 9 forced to 1, bit 8 forced to 0 // Locally administered MAC, reduced conflicts // http://en.wikipedia.org/wiki/MAC_address - uint32_t word1 = *(uint32_t *)0x4004805C; - word1 |= 0x00000002; - word1 &= 0x0000FFFE; + MAC[2] |= 0x0002; + MAC[2] &= 0xFFFE; - mac[0] = (word1 & 0x000000ff); - mac[1] = (word1 & 0x0000ff00) >> 8; - mac[2] = (word0 & 0xff000000) >> 24; - mac[3] = (word0 & 0x00ff0000) >> 16; - mac[4] = (word0 & 0x0000ff00) >> 8; - mac[5] = (word0 & 0x000000ff); } + + + + From c60b81202c051d2932fcbf12699f03c68fde2df0 Mon Sep 17 00:00:00 2001 From: chris-styles Date: Tue, 30 Jun 2015 06:50:38 +0100 Subject: [PATCH 2/7] Update mbed_overrides.c --- .../TARGET_MCU_K64F/TARGET_FRDM/mbed_overrides.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/mbed_overrides.c b/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/mbed_overrides.c index 3b2d70473a..e04dd08d53 100644 --- a/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/mbed_overrides.c +++ b/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/mbed_overrides.c @@ -34,7 +34,6 @@ void NMI_Handler(void) // Provide ethernet devices with a semi-unique MAC address from the UUID void mbed_mac_address(char *mac) { - // Making a random-as-possible MAC from 3 16 bit seeds and load of XOR'ing uint16_t MAC[3]; // 3 16 bits words for the MAC uint32_t UUID_LOC_BASE = 0x40048054; @@ -55,9 +54,16 @@ void mbed_mac_address(char *mac) // and bit 9 forced to 1, bit 8 forced to 0 // Locally administered MAC, reduced conflicts // http://en.wikipedia.org/wiki/MAC_address + // uint32_t word1 = *(uint32_t *)UUID_LOC_WORD1; MAC[2] |= 0x0002; MAC[2] &= 0xFFFE; + mac[0] = MAC[0]; + mac[1] = MAC[0] >> 8; + mac[2] = MAC[1]; + mac[3] = MAC[1] >> 8; + mac[4] = MAC[2]; + mac[5] = MAC[2] >> 8; } From 41ff2c877363eb93e5d8dce945a9efb73ecedd05 Mon Sep 17 00:00:00 2001 From: chris-styles Date: Tue, 30 Jun 2015 08:46:17 +0100 Subject: [PATCH 3/7] Update mbed_overrides.c --- .../TARGET_FRDM/mbed_overrides.c | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/mbed_overrides.c b/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/mbed_overrides.c index e04dd08d53..a0c8838315 100644 --- a/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/mbed_overrides.c +++ b/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/mbed_overrides.c @@ -34,6 +34,7 @@ void NMI_Handler(void) // Provide ethernet devices with a semi-unique MAC address from the UUID void mbed_mac_address(char *mac) { + // Making a random-as-possible MAC from 3 16 bit seeds and load of XOR'ing uint16_t MAC[3]; // 3 16 bits words for the MAC uint32_t UUID_LOC_BASE = 0x40048054; @@ -50,20 +51,22 @@ void mbed_mac_address(char *mac) } } + + mac[0] = MAC[2] >> 8; + mac[1] = MAC[2]; + mac[2] = MAC[1] >> 8; + mac[3] = MAC[1]; + mac[4] = MAC[0] >> 8; + mac[5] = MAC[0]; + // we only want bottom 16 bits of word1 (MAC bits 32-47) // and bit 9 forced to 1, bit 8 forced to 0 // Locally administered MAC, reduced conflicts // http://en.wikipedia.org/wiki/MAC_address // uint32_t word1 = *(uint32_t *)UUID_LOC_WORD1; - MAC[2] |= 0x0002; - MAC[2] &= 0xFFFE; - - mac[0] = MAC[0]; - mac[1] = MAC[0] >> 8; - mac[2] = MAC[1]; - mac[3] = MAC[1] >> 8; - mac[4] = MAC[2]; - mac[5] = MAC[2] >> 8; + + mac[0] |= 0x02; + mac[0] &= 0xFE; } From ee35963b3de7d1c9c412243257a690a726dbdb13 Mon Sep 17 00:00:00 2001 From: chris-styles Date: Tue, 7 Jul 2015 15:20:59 +0100 Subject: [PATCH 4/7] Update mbed_overrides.c Added calls to "crc16" routine, as well as the required #include and #define --- .../TARGET_FRDM/mbed_overrides.c | 50 +++++++++---------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/mbed_overrides.c b/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/mbed_overrides.c index a0c8838315..0d7160e010 100644 --- a/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/mbed_overrides.c +++ b/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/mbed_overrides.c @@ -15,6 +15,9 @@ */ #include "gpio_api.h" +#define CRC16 +#include "crc.h" + // called before main - implement here if board needs it ortherwise, let // the application override this if necessary //void mbed_sdk_init() @@ -35,40 +38,35 @@ void NMI_Handler(void) void mbed_mac_address(char *mac) { - // Making a random-as-possible MAC from 3 16 bit seeds and load of XOR'ing - uint16_t MAC[3]; // 3 16 bits words for the MAC - uint32_t UUID_LOC_BASE = 0x40048054; - - // three random seeds to make a MAC - MAC[0] = 0x2055; - MAC[1] = 0x5c44; - MAC[2] = 0x79fe; - - // XOR in each halfword of the UUID into each seed - for (int i=0;i<3;i++) { - for (int j=0;j<8;j++) { - MAC[i] ^= (*(uint32_t *)(UUID_LOC_BASE+(0x2*j))); - } - } + unsigned int UUID_LOC_BASE = 0x40048054; // First adddress of the 4-word UUID + char uuid[16]; // So we can take a local copy of the UUID + uint32_t MAC[3]; // 3 16 bits words for the MAC + // copy the UUID to the variable MAC[] + memcpy(uuid,(const void*)UUID_LOC_BASE,sizeof(uuid)); - mac[0] = MAC[2] >> 8; - mac[1] = MAC[2]; + // generate three CRC16's using different slices of the UUID + MAC[0] = crcSlow(uuid, 8); // most significant half-word + MAC[1] = crcSlow(uuid, 12); + MAC[2] = crcSlow(uuid, 16); // least significant half word + + // The network stack expects an array of 6 bytes + // so we copy, and shift and copy from the half-word array to the byte array + mac[0] = MAC[0] >> 8; + mac[1] = MAC[0]; mac[2] = MAC[1] >> 8; mac[3] = MAC[1]; - mac[4] = MAC[0] >> 8; - mac[5] = MAC[0]; + mac[4] = MAC[2] >> 8; + mac[5] = MAC[2]; - // we only want bottom 16 bits of word1 (MAC bits 32-47) - // and bit 9 forced to 1, bit 8 forced to 0 - // Locally administered MAC, reduced conflicts + // We want to force bits [1:0] of the most significant byte [0] + // to be "10" // http://en.wikipedia.org/wiki/MAC_address - // uint32_t word1 = *(uint32_t *)UUID_LOC_WORD1; - mac[0] |= 0x02; - mac[0] &= 0xFE; + mac[0] |= 0x02; // force bit 1 to a "1" = "Locally Administered" + mac[0] &= 0xFE; // force bit 0 to a "0" = Unicast + } - From 44992153d9c7e04d261bf54f07cb0bc251f7bf1e Mon Sep 17 00:00:00 2001 From: chris-styles Date: Tue, 7 Jul 2015 15:22:03 +0100 Subject: [PATCH 5/7] Create crc16.c --- .../TARGET_MCU_K64F/TARGET_FRDM/crc16.c | 234 ++++++++++++++++++ 1 file changed, 234 insertions(+) create mode 100644 libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/crc16.c diff --git a/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/crc16.c b/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/crc16.c new file mode 100644 index 0000000000..7e2d7e7c51 --- /dev/null +++ b/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/crc16.c @@ -0,0 +1,234 @@ +/********************************************************************** + * + * Filename: crc.c + * + * Description: Slow and fast implementations of the CRC standards. + * + * Notes: The parameters for each supported CRC standard are + * defined in the header file crc.h. The implementations + * here should stand up to further additions to that list. + * + * + * Copyright (c) 2000 by Michael Barr. This software is placed into + * the public domain and may be used for any purpose. However, this + * notice must not be changed or removed and no warranty is either + * expressed or implied by its publication or distribution. + **********************************************************************/ + +#include "crc.h" + + +/* + * Derive parameters from the standard-specific parameters in crc.h. + */ +#define WIDTH (8 * sizeof(crc)) +#define TOPBIT (1 << (WIDTH - 1)) + +#if (REFLECT_DATA == TRUE) +#undef REFLECT_DATA +#define REFLECT_DATA(X) ((unsigned char) reflect((X), 8)) +#else +#undef REFLECT_DATA +#define REFLECT_DATA(X) (X) +#endif + +#if (REFLECT_REMAINDER == TRUE) +#undef REFLECT_REMAINDER +#define REFLECT_REMAINDER(X) ((crc) reflect((X), WIDTH)) +#else +#undef REFLECT_REMAINDER +#define REFLECT_REMAINDER(X) (X) +#endif + + +/********************************************************************* + * + * Function: reflect() + * + * Description: Reorder the bits of a binary sequence, by reflecting + * them about the middle position. + * + * Notes: No checking is done that nBits <= 32. + * + * Returns: The reflection of the original data. + * + *********************************************************************/ +static unsigned long +reflect(unsigned long data, unsigned char nBits) +{ + unsigned long reflection = 0x00000000; + unsigned char bit; + + /* + * Reflect the data about the center bit. + */ + for (bit = 0; bit < nBits; ++bit) + { + /* + * If the LSB bit is set, set the reflection of it. + */ + if (data & 0x01) + { + reflection |= (1 << ((nBits - 1) - bit)); + } + + data = (data >> 1); + } + + return (reflection); + +} /* reflect() */ + + +/********************************************************************* + * + * Function: crcSlow() + * + * Description: Compute the CRC of a given message. + * + * Notes: + * + * Returns: The CRC of the message. + * + *********************************************************************/ +crc +crcSlow(unsigned char const message[], int nBytes) +{ + crc remainder = INITIAL_REMAINDER; + int byte; + unsigned char bit; + + + /* + * Perform modulo-2 division, a byte at a time. + */ + for (byte = 0; byte < nBytes; ++byte) + { + /* + * Bring the next byte into the remainder. + */ + remainder ^= (REFLECT_DATA(message[byte]) << (WIDTH - 8)); + + /* + * Perform modulo-2 division, a bit at a time. + */ + for (bit = 8; bit > 0; --bit) + { + /* + * Try to divide the current data bit. + */ + if (remainder & TOPBIT) + { + remainder = (remainder << 1) ^ POLYNOMIAL; + } + else + { + remainder = (remainder << 1); + } + } + } + + /* + * The final remainder is the CRC result. + */ + return (REFLECT_REMAINDER(remainder) ^ FINAL_XOR_VALUE); + +} /* crcSlow() */ + + +crc crcTable[256]; + + +/********************************************************************* + * + * Function: crcInit() + * + * Description: Populate the partial CRC lookup table. + * + * Notes: This function must be rerun any time the CRC standard + * is changed. If desired, it can be run "offline" and + * the table results stored in an embedded system's ROM. + * + * Returns: None defined. + * + *********************************************************************/ +void +crcInit(void) +{ + crc remainder; + int dividend; + unsigned char bit; + + + /* + * Compute the remainder of each possible dividend. + */ + for (dividend = 0; dividend < 256; ++dividend) + { + /* + * Start with the dividend followed by zeros. + */ + remainder = dividend << (WIDTH - 8); + + /* + * Perform modulo-2 division, a bit at a time. + */ + for (bit = 8; bit > 0; --bit) + { + /* + * Try to divide the current data bit. + */ + if (remainder & TOPBIT) + { + remainder = (remainder << 1) ^ POLYNOMIAL; + } + else + { + remainder = (remainder << 1); + } + } + + /* + * Store the result into the table. + */ + crcTable[dividend] = remainder; + } + +} /* crcInit() */ + + +/********************************************************************* + * + * Function: crcFast() + * + * Description: Compute the CRC of a given message. + * + * Notes: crcInit() must be called first. + * + * Returns: The CRC of the message. + * + *********************************************************************/ +crc +crcFast(unsigned char const message[], int nBytes) +{ + crc remainder = INITIAL_REMAINDER; + unsigned char data; + int byte; + + + /* + * Divide the message by the polynomial, a byte at a time. + */ + for (byte = 0; byte < nBytes; ++byte) + { + data = REFLECT_DATA(message[byte]) ^ (remainder >> (WIDTH - 8)); + remainder = crcTable[data] ^ (remainder << 8); + } + + /* + * The final remainder is the CRC. + */ + return (REFLECT_REMAINDER(remainder) ^ FINAL_XOR_VALUE); + +} /* crcFast() */ + From 671f4723abd19f909db93eec92c17628c8ccde3a Mon Sep 17 00:00:00 2001 From: chris-styles Date: Tue, 7 Jul 2015 15:22:33 +0100 Subject: [PATCH 6/7] Create crc.h --- .../TARGET_MCU_K64F/TARGET_FRDM/crc.h | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/crc.h diff --git a/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/crc.h b/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/crc.h new file mode 100644 index 0000000000..fae66ae4bc --- /dev/null +++ b/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/crc.h @@ -0,0 +1,77 @@ +/********************************************************************** + * + * Filename: crc.h + * + * Description: A header file describing the various CRC standards. + * + * Notes: + * + * + * Copyright (c) 2000 by Michael Barr. This software is placed into + * the public domain and may be used for any purpose. However, this + * notice must not be changed or removed and no warranty is either + * expressed or implied by its publication or distribution. + **********************************************************************/ + +#ifndef _crc_h +#define _crc_h + + +#define FALSE 0 +#define TRUE !FALSE + +/* + * Select the CRC standard from the list that follows. + */ +#define CRC16 + + +#if defined(CRC_CCITT) + +typedef unsigned short crc; + +#define CRC_NAME "CRC-CCITT" +#define POLYNOMIAL 0x1021 +#define INITIAL_REMAINDER 0xFFFF +#define FINAL_XOR_VALUE 0x0000 +#define REFLECT_DATA FALSE +#define REFLECT_REMAINDER FALSE +#define CHECK_VALUE 0x29B1 + +#elif defined(CRC16) + +typedef unsigned short crc; + +#define CRC_NAME "CRC-16" +#define POLYNOMIAL 0x8005 +#define INITIAL_REMAINDER 0x0000 +#define FINAL_XOR_VALUE 0x0000 +#define REFLECT_DATA TRUE +#define REFLECT_REMAINDER TRUE +#define CHECK_VALUE 0xBB3D + +#elif defined(CRC32) + +typedef unsigned long crc; + +#define CRC_NAME "CRC-32" +#define POLYNOMIAL 0x04C11DB7 +#define INITIAL_REMAINDER 0xFFFFFFFF +#define FINAL_XOR_VALUE 0xFFFFFFFF +#define REFLECT_DATA TRUE +#define REFLECT_REMAINDER TRUE +#define CHECK_VALUE 0xCBF43926 + +#else + +#error "One of CRC_CCITT, CRC16, or CRC32 must be #define'd." + +#endif + + +void crcInit(void); +crc crcSlow(unsigned char const message[], int nBytes); +crc crcFast(unsigned char const message[], int nBytes); + + +#endif /* _crc_h */ From 8b4872f08ddae2c1e2648465c2caa97c0d93245b Mon Sep 17 00:00:00 2001 From: chris-styles Date: Tue, 7 Jul 2015 15:23:24 +0100 Subject: [PATCH 7/7] Rename crc16.c to crc.c --- .../TARGET_MCU_K64F/TARGET_FRDM/{crc16.c => crc.c} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/{crc16.c => crc.c} (100%) diff --git a/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/crc16.c b/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/crc.c similarity index 100% rename from libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/crc16.c rename to libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM/crc.c