Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
March 28, 2024, 10:14:24 22:14


Login with username, password and session length


Pages: [1]
Print
Author Topic: SMBUS CRC calculator on PIC 24  (Read 3849 times)
0 Members and 1 Guest are viewing this topic.
ehnonymouse
Newbie
*
Offline Offline

Posts: 28

Thank You
-Given: 15
-Receive: 13


« on: November 13, 2012, 08:50:40 20:50 »

It uses the PIC24's built in CRC engine, and despite AN1148A it took me a while to get going. It's fully tested and in use, so I hope it saves someone some time.

Code:
//---------------------------------------------------------------------
//
//  FUNCTION :  pec_calculation
//
//  PARAMETERS:  pui8Buffer is a buffer up to 255 bytes long
//               ui8Length is the length of the message
//  DESCRIPTION: 8 Bit CRC; Polynomial is X^8+X^2+X+1
//
//  RETURNS: The checksum of the SMBus (I2C) message
//
//---------------------------------------------------------------------
UINT8 pec_calculation(UINT8 *pu8Buffer, UINT8 u8Length)
{
#define POYNOMIAL_LENGTH 8

    CRCCONbits.PLEN = POYNOMIAL_LENGTH -1;
    CRCXOR = 0x0006;                            // XOR polynomial is (X^8)+(X^2)+X+1, the final 1 isn't written
    CRCCONbits.CSIDL = 1;                       // Stop module when device enters idle mode
    CRCWDAT = 0;                                // Pre-load value
    CRCCONbits.CRCGO = 1;                       // Start CRC module

    do
    {
        while(CRCCONbits.CRCFUL){};             // Do not overflow FIFO
        *(UINT8*) &CRCDAT = *pu8Buffer++;      // Need to byte write this register or it doesn't work
    } while (--u8Length != 0);
    while(CRCCONbits.CRCFUL){};                 // Wait for FIFO to empty
    *(UINT8*) &CRCDAT = 0;
    Nop();
    Nop();
    while (!CRCCONbits.CRCMPT){};               // Wait for calculation to finish

    CRCCONbits.CRCGO = 0;                       // Stop CRC module
    return (UINT8)(CRCWDAT & 0x00FF);


#undef POYNOMIAL_LENGTH
}
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC