Fletcher Checksum for CP2102n configuration array

by <a href="http://community.silabs.com/t5/Welcome-and-Announcements/Community-Ranking-System-and-Recognition-Program/m-p/140490#U140490"><font color="#000000"><font size="2">Hero Employee</font></font> </a> BrianL on ‎06-12-2017 05:36 PM

The format of the CP2102N configuration byte array used by CP210x_GetConfig() and CP210x_SetConfig() to program and retrieve the CP2102N settings has the format as specified in this article: http://community.silabs.com/t5/Interface-Knowledge-Base/CP2102N-SetConfig-GetConfig-Array-Structure/...

 

The last two bytes of this configuration are a 'fletcher' checksum of the rest of the bytes of the array. That checksum is described in this wikipedia article: 

https://en.wikipedia.org/wiki/Fletcher%27s_checksum

 

 

Additionally, two implementations of this algorithm are included below.

 

First, the 'C' implementation:

// Taken from Wikipedia https://en.wikipedia.org/wiki/Fletcher%27s_checksum
// Must use generic pointer since function is called on XDATA and CODE spaces.
uint16_t fletcher16(uint8_t *dataIn, uint16_t bytes)
{
        uint16_t sum1 = 0xff, sum2 = 0xff;
        uint16_t tlen;
 
        while (bytes) {
                tlen = bytes >= 20 ? 20 : bytes;
                bytes -= tlen;
                do {
                        sum2 += sum1 += *dataIn++;
                } while (--tlen);
                sum1 = (sum1 & 0xff) + (sum1 >> 8);
                sum2 = (sum2 & 0xff) + (sum2 >> 8);
        }
        /* Second reduction step to reduce sums to 8 bits */
        sum1 = (sum1 & 0xff) + (sum1 >> 8);
        sum2 = (sum2 & 0xff) + (sum2 >> 8);
        return sum2 << 8 | sum1;
}

 

Secondly, a 'Python' implementation:

 

def generateFletcherChecksum(byteArray):
    sum1 = 0xFF
    sum2 = 0xFF
    tlen = 0

    numBytes = len(byteArray)

    index = 0
    while(numBytes):
        tlen = 20 if numBytes >= 20 else numBytes
        numBytes -= tlen
        
        while(tlen):
            sum1 += (byteArray[index] & 0xFF)
            sum1 &= 0xFFFF
            sum2 += sum1
            sum2 &= 0xFFFF
            index += 1
            tlen -= 1
            
        sum1 = ((sum1 & 0xFF) + (sum1 >> 8)) & 0xFFFF
        sum2 = ((sum2 & 0xFF) + (sum2 >> 8)) & 0xFFFF
        
    sum1 = ((sum1 & 0xFF) + (sum1 >> 8)) & 0xFFFF
    sum2 = ((sum2 & 0xFF) + (sum2 >> 8)) & 0xFFFF

    checksum = ((sum2 << 8) | sum1) & 0xFFFF

    return checksum