![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Bluetooth Low Energy for Smart Plug
Dependencies: BLE_API mbed nRF51822
Revision 3:aaa92c61931a, committed 2015-07-13
- Comitter:
- Slepnir
- Date:
- Mon Jul 13 13:14:34 2015 +0000
- Parent:
- 2:6db5c9a2894c
- Commit message:
- SPCommunication class Added
Changed in this revision
--- a/BLECommunication.h Wed Jul 08 07:25:11 2015 +0000 +++ b/BLECommunication.h Mon Jul 13 13:14:34 2015 +0000 @@ -6,7 +6,7 @@ #include "MVC.h" #include "mbed.h" -static const char DEVICE_NAME[] = "NRFTEST"; +static const char DEVICE_NAME[] = "SmartPlug"; static const uint16_t list[] = {SPS_UUID_SERVICE}; class BLECommunication
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SPCommunication.cpp Mon Jul 13 13:14:34 2015 +0000 @@ -0,0 +1,221 @@ +#include "SPCommunication.h" + +SPCommunication::SPCommunication():SPUart(p9,p11) +{ + SPUart.baud(115200); + SPUart.attach(this,&SPCommunication::Rxint,Serial::RxIrq); + size=0; + r = 0; + w = 0; + for (int i=0;i<128;i++) RxBuffer[i]=0; +} +SPCommunication::SPCommunication(PinName tx,PinName rx,uint32_t bRate): SPUart(tx,rx){ + + SPUart.baud(bRate); + SPUart.attach(this,&SPCommunication::Rxint,Serial::RxIrq); + size=0; + r = 0; + w = 0; + for (int i=0;i<128;i++) RxBuffer[i]=0; + +} +void SPCommunication::Rxint() +{ + // SPUart.putc(SPUart.getc()); + + while (SPUart.readable()){ + uint8_t temp = SPUart.getc(); + //buff.addData(temp); + RxBuffer[w] = temp; + if (++w >= 128) + w = 0; + } +} +//Get crc remainder from table +uint8_t SPCommunication::getCRC(uint8_t val) +{ + return crc8_table[val]; +} + +// print all data store in ArrData in class +void SPCommunication::printData(){ + //SPUart.printf("szie is %02X ",size); + for(int i=0;i<size;i++) + SPUart.printf("%02X ",DataArr[i]); + } + + +uint8_t* SPCommunication::RequestData(uint8_t* data) +{ + + int i=0; + do // Send request Data + { + SPUart.printf("%c",data[i]); + i++; + } + while(data[i]!='\0'); + wait(1); + uint8_t temp = readData(); + //SPUart.printf("\r\n in int %02x",temp); + + if((uint8_t) temp == 0x7E){ + uint8_t err = handleData(); + if(err) + handleData(); + } + + return 0; + +} + + +uint8_t SPCommunication::handleData(){ + delete[] DataArr; + + uint8_t message; + uint8_t crccheck; + + size=readData(); + //size/=2; + // SPUart.printf("\r\n Size is %02X",size); + DataArr = new uint8_t[size]; + uint8_t test[size+1]; + test[0]=size; + for (uint8_t i=0;i<size;i++) + { + + message = readData(); + test[i+1]=(uint8_t) message; + DataArr[i] = (uint8_t) message; + + // SPUart.printf("\r\n bool Data is %d value in table %d value in ctc check %d" ,message,crc8_table[message],crccheck ); + + } +// printf("arr Data"); +// for (int i=0;i<size;i++) +// printf("%02X ",DataArr[i]); + + crccheck=DataArr[size-1]; + + //printf( "\r\n val crc in arr is %02X recall fn val ",crccheck ); + if( crccheck == (uint8_t) crc8Compute(test,size) ) + { + //SPUart.printf("hi"); + SPUart.printf("\r\nComplete"); + + // SPUart.printf("\r\nin crc fask condition %02X add to arrData",DataArr[i]); + // sum+=DataArr[i]; + } + else + { + SPUart.printf("\r\nfail"); + return 1; + + } + + return 0; +} + +//*********************************************************************************************************************************** + //Getter method for request information abour uart +//*********************************************************************************************************************************** + +uint32_t SPCommunication::getVoltage() +{ + uint8_t com[2]={0x02,0x56}; + uint8_t command[4] = {0x7E,0x02,0x56,(uint8_t) crc8Compute(com,2)}; + RequestData(command); + uint32_t Recv ; + //crc8Compute(com,2); + memcpy(&Recv,DataArr,32); + + + return Recv; + //printData(); + +} +uint32_t SPCommunication::getEnergy() +{ + uint8_t com[2]={ 0x02,'E'}; + uint8_t command[4] = {0x7E,0x02,'E',(uint8_t) crc8Compute(com,2)}; + +} +uint32_t SPCommunication:: getCurrent() +{ + uint8_t com[2]={ 0x02,'I'}; + uint8_t command[4] = {0x7E,0x02,'I',(uint8_t) crc8Compute(com,2)}; + RequestData(command); + uint32_t Recv ; + //crc8Compute(com,2); + memcpy(&Recv,DataArr+1,32); + return Recv; +} + + +uint32_t SPCommunication::getPower() +{ + uint8_t com[2]={ 0x02,'P'}; + uint8_t command[4] = {0x7E,0x02,'P',(uint8_t) crc8Compute(com,2)} ; + RequestData(command); + + uint32_t Recv ; + //crc8Compute(com,2); + memcpy(&Recv,DataArr+1,32); + return Recv; +} + +uint32_t SPCommunication::getPowerFactor() +{ + uint8_t com[2]={ 0x02,'C'}; + uint8_t command[4] = {0x7E,0x02,'C',(uint8_t) crc8Compute(com,2) }; + RequestData(command); + uint32_t Recv ; + //crc8Compute(com,2); + memcpy(&Recv,DataArr+1,32); + return Recv; + +} + +bool SPCommunication::getStateRelay() +{ + uint8_t com[2]={ 0x02,'S'}; + uint8_t command[4] = {0x7E,0x02,'S',(uint8_t) crc8Compute(com,2) }; + RequestData(command); + + return DataArr[1]; +} + +bool SPCommunication::setStateRelay(uint8_t st) +{ + uint8_t* command; + if (st ==1){ + uint8_t com[3]={ 0x03,'S',0x01}; + + uint8_t temp[] = {0x7E,0x03,'S',0x01,(uint8_t) crc8Compute(com,2) }; + command=temp; + } + else{ + uint8_t com[3]={ 0x03,'S',0x00}; + uint8_t temp[] = {0x7E,0x03,'S',0x00,(uint8_t) crc8Compute(com,2) }; + command=temp; + } + RequestData(command); + if(DataArr[0] == command[2] && DataArr[1] == command[3] ) + return true; + + return false; + +} +uint8_t SPCommunication::readData() +{ + + uint8_t temp=RxBuffer[r]; +// if (temp == 0) +// return 0; + r++; + if(r>=128) + r=0; + return temp; + +}
--- a/SPCommunication.h Wed Jul 08 07:25:11 2015 +0000 +++ b/SPCommunication.h Mon Jul 13 13:14:34 2015 +0000 @@ -1,13 +1,72 @@ -#ifndef SP_COMMUNICATION_H -#define SP_COMMUNICATION_H +#ifndef SPCOMMUNICATION +#define SPCOMMUNICATION +#include "crc8.h" + +#define CRC8INIT 0x00 +#define CRC8POLY 0x18 //0X18 = X^8+X^5+X^4+X^0 +#include <stdint.h> +#include "mbed.h" +#include <stdlib.h> + + + +const uint8_t crc8_table[256]={ +0x00,0x07,0x0E,0x09,0x1C,0x1B,0x12,0x15,0x38,0x3F,0x36,0x31,0x24,0x23,0x2A,0x2D, +0x70,0x77,0x7E,0x79,0x6C,0x6B,0x62,0x65,0x48,0x4F,0x46,0x41,0x54,0x53,0x5A,0x5D, +0xE0,0xE7,0xEE,0xE9,0xFC,0xFB,0xF2,0xF5,0xD8,0xDF,0xD6,0xD1,0xC4,0xC3,0xCA,0xCB, +0x90,0x97,0x9E,0x99,0x8C,0x8B,0x82,0x85,0xA8,0xAF,0xA6,0xA1,0xB4,0xB3,0xBA,0xBD, +0xC7,0xC0,0xC9,0xCE,0xDB,0xDC,0xD5,0xD2,0xFF,0xF8,0xF1,0xF6,0xE3,0xE4,0xED,0xEA, +0xB7,0xB0,0xB9,0xBE,0xAB,0xAC,0xA5,0xA2,0x8F,0x88,0x81,0x86,0x93,0x94,0x9D,0x9A, +0x27,0x20,0x29,0x2E,0x3B,0x3C,0x35,0x32,0x1F,0x18,0x11,0x16,0x03,0x04,0x0D,0x0A, +0x57,0x50,0x59,0x5E,0x4B,0x4C,0x45,0x42,0x6F,0x68,0x61,0x66,0x73,0x74,0x7D,0x7A, +0x89,0x8E,0x87,0x80,0x95,0x92,0x9B,0x9C,0xB1,0xB6,0xBF,0xB8,0xAD,0xAA,0xA3,0xA4, +0xF9,0xFE,0xF7,0xF0,0xE5,0xE2,0xEB,0xEC,0xC1,0xC6,0xCF,0xC8,0xDD,0xDA,0xD3,0xD4, +0x69,0x6E,0x67,0x60,0x75,0x72,0x7B,0x7C,0x51,0x56,0x5F,0x58,0x4D,0x4A,0x43,0x44, +0x19,0x1E,0x17,0x10,0x05,0x02,0x0B,0x0C,0x21,0x26,0x2F,0x28,0x3D,0x3A,0x33,0x34, +0x4E,0x49,0x40,0x47,0x52,0x55,0x5C,0x5B,0x76,0x71,0x78,0x7F,0x6A,0x6D,0x64,0x63, +0x3E,0x39,0x30,0x37,0x22,0x25,0x2C,0x2B,0x06,0x01,0x08,0x0F,0x1A,0x1D,0x14,0x13, +0xAE,0xA9,0xA0,0xA7,0xB2,0xB5,0xBC,0xBB,0x96,0x91,0x98,0x9F,0x8A,0x8D,0x84,0x83, +0xDE,0xD9,0xD0,0xD7,0xC2,0xC5,0xCC,0xCB,0xE6,0xE1,0xE8,0xEF,0xFA,0xFD,0xF4,0xF3 + +}; class SPCommunication { - public: - uint32_t getVoltage(){return 220;} - uint32_t getCurrent(){return 10;} - uint32_t getPower(){return 10;} - uint32_t getPowerFactor(){return 10;} + public: + //callback function + void Rxint(); + + uint8_t handleData(); + //constructor + SPCommunication(); + SPCommunication(PinName tx,PinName rx,uint32_t bRate); + uint8_t * RequestData(uint8_t* data); + + uint32_t getPower(); + bool getStateRelay(); + bool setStateRelay(uint8_t st); + uint32_t getVoltage(); + uint32_t getCurrent(); + uint32_t getPowerFactor(); + uint32_t getEnergy(); + void printData(); + uint8_t getCRC(uint8_t val); + uint8_t readData(); + + private: + uint8_t size; + uint8_t *DataArr; + uint8_t checkSum(uint8_t const message[], int nBytes); + Serial SPUart; + + uint8_t RxBuffer[128]; + + uint8_t r; + uint8_t w; + + + + + }; - #endif
--- a/Services/SmartPlugService.cpp Wed Jul 08 07:25:11 2015 +0000 +++ b/Services/SmartPlugService.cpp Mon Jul 13 13:14:34 2015 +0000 @@ -36,12 +36,7 @@ system.updateData(); } else if(params->handle == relayChar.getValueHandle()) - {//like stack - printf("SmartPlugService::onDataWritten params->len = %d\r\n",params->len); - for(int i=0;i<params->len;i++) - { - printf("data = 0x%08x\r\n",params->data[i]); - } + { system.onRelayWrite(params->data); } } @@ -54,7 +49,7 @@ updatePower(sp->getPower()); updatePowerFactor(sp->getPowerFactor()); updateEnergy(sp->getEnergy()); - updateRelay(&sp->getRelay()); + updateRelay(sp->getRelay()); } void SmartPlugService::updateVoltage(uint32_t v) @@ -106,18 +101,7 @@ { if (ble.getGapState().connected) { - if(relay->getState()) - { - led = 0; - printf("Open\r\n"); - } - else - { - led = 1; - printf("Close\r\n"); - } relayValue.updateData(relay); - //convert(relayValue.getDataPointer(),*relayValue.getDataPointer()); ble.updateCharacteristicValue(relayChar.getValueHandle(),relayValue.getDataPointer(), relayValue.getLenBytes()); }
--- a/Services/SmartPlugService.h Wed Jul 08 07:25:11 2015 +0000 +++ b/Services/SmartPlugService.h Mon Jul 13 13:14:34 2015 +0000 @@ -62,6 +62,7 @@ void updateData(Relay* relay) { + printf("updateData:: min = %d,hour = %d,state = %d\r\n",relay->getMinCounter(),relay->getHrCounter(),relay->getState()); data[STATE_BYTE_INDEX] |= ((relay->getState())<<STATE_BIT_INDEX); data[HOUR_TIMER_BYTE_INDEX] |= relay->getHrCounter(); data[MINUTE_TIMER_BYTE_INDEX] = relay->getMinCounter(); @@ -77,13 +78,12 @@ void reverse(uint8_t* ptr) { - swap(&ptr[0],&ptr[3]); - swap(&ptr[1],&ptr[2]); + swap(&ptr[0],&ptr[1]); } uint8_t* getDataPointer() { - reverse(data); + //reverse(data); return data; }
--- a/SmartPlug.h Wed Jul 08 07:25:11 2015 +0000 +++ b/SmartPlug.h Mon Jul 13 13:14:34 2015 +0000 @@ -48,13 +48,14 @@ class SmartPlug { public: - SmartPlug():voltage(0),current(0),power(0),powerFactor(0),energy(0) + bool isCounting; + + SmartPlug():isCounting(false),voltage(0),current(0),power(0),powerFactor(0),energy(0) { } uint32_t getVoltage() { - printf("in getVoltage in smartPlug\r\n"); return voltage; } @@ -78,9 +79,9 @@ return energy; } - Relay getRelay() + Relay* getRelay() { - return relay; + return &relay; } void setVoltage(uint32_t data) @@ -108,11 +109,18 @@ energy = data; } - void setRelay(const uint8_t *data) + void setRelay(uint8_t state,uint8_t hour, uint8_t min) { - relay.setState(data[0]>>7); - relay.setHrCounter(data[0]&0x0FFFFFFF); - relay.setMinCounter(data[1]); + relay.setState(state); + relay.setHrCounter(hour); + relay.setMinCounter(min); + printf("hour = %d,min = %d\r\n",hour,min); + if(hour != 0 || min != 0) + isCounting = true; + else if(state) + { + isCounting = false; + } } private: uint32_t voltage;
--- a/SmartPlugBLE.h Wed Jul 08 07:25:11 2015 +0000 +++ b/SmartPlugBLE.h Mon Jul 13 13:14:34 2015 +0000 @@ -5,63 +5,122 @@ #include "MVC.h" #include "mbed.h" #include "SPCommunication.h" -#include <vector> +#include <vector> class SmartPlugBLE: public Observable { - public: - SmartPlugBLE() +public: + SmartPlugBLE():counter(60),led(LED3,0) + { + ticker.attach(this,&SmartPlugBLE::periodicCallback, 1); // blink LED every second + } + + void onRelayWrite(const uint8_t *data) + { + counter = 60; + updateRelay(data); + notifyObservers(); + } + + void updateRelay(const uint8_t *data) + { + uint8_t state = data[0]>>7; + uint8_t hour = data[0]&0x7F; + uint8_t min = data[1]; + //instant behavior + if(state) { - ticker.attach(this,&SmartPlugBLE::periodicCallback, 1); // blink LED every second - } - - void onRelayWrite(const uint8_t *data) + printf("Open!\r\n"); + led = 0; + } + else if(!state && hour == 0 && min == 0) { - updateRelay(data); - notifyObservers(); + printf("Close!\r\n"); + led = 1; } - - void updateRelay(const uint8_t *data) + // + smartPlug.setRelay(state,hour,min); + } + + void notifyObservers() + { + for(int i=0; i<observers.size(); i++) { - smartPlug.setRelay(data); + observers[i]->updateObserver((void*)&smartPlug); } - - void notifyObservers() - { - for(int i=0;i<observers.size();i++) + } + + void addObserver(Observer* o) + { + observers.push_back(o); + } + + void periodicCallback(void) + { + if(smartPlug.isCounting) + { + counter--; + if(counter > 60) { - observers[i]->updateObserver((void*)&smartPlug); + smartPlug.getRelay()->setMinCounter(smartPlug.getRelay()->getMinCounter()-1); + if(smartPlug.getRelay()->getMinCounter() == 0) + { + smartPlug.getRelay()->setHrCounter(smartPlug.getRelay()->getHrCounter()-1); + smartPlug.getRelay()->setMinCounter(0); + } + if(smartPlug.getRelay()->getHrCounter() >128) + { + smartPlug.getRelay()->setHrCounter(0); + //spComm.setRelay(smartPlug.getRelay()->getState()); + if(smartPlug.getRelay()->getState()) + printf("Open!\r\n"); + else + { + printf("Close!\r\n"); + led = 1; + smartPlug.isCounting = false; + } + } + counter = 60; } } - - void addObserver(Observer* o) - { - observers.push_back(o); - } - - void periodicCallback(void) - { - //led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ - } - - void updateData(void) - { - smartPlug.setVoltage(smartPlug.getVoltage()+1);//(spComm.getVoltage()); - smartPlug.setCurrent(smartPlug.getCurrent()+1);//(spComm.getCurrent()); - smartPlug.setPower(smartPlug.getPower()+1);//(spComm.getPower()); - smartPlug.setPowerFactor(smartPlug.getPowerFactor()+1);//(spComm.getPowerFactor()); - smartPlug.setEnergy(smartPlug.getEnergy()+1); - notifyObservers(); - } - - private: - SmartPlug smartPlug; - Ticker ticker; - //DigitalOut led1; - SPCommunication spComm; - //BLECommunication bleComm; - vector<Observer*> observers; + else + counter = 60; + } + + void updateData(void) { + smartPlug.setVoltage/*(smartPlug.getVoltage()+1);*/(spComm.getVoltage()); + smartPlug.setCurrent/*(smartPlug.getCurrent()+1);*/(spComm.getCurrent()); + smartPlug.setPower/*(smartPlug.getPower()+1);*/(spComm.getPower()); + smartPlug.setPowerFactor/*(smartPlug.getPowerFactor()+1);*/(spComm.getPowerFactor()); + smartPlug.setEnergy/*(smartPlug.getEnergy()+1);*/(spComm.getEnergy()); + //smartPlug.getRelay()->setState(spComm.getRelay()); + notifyObservers(); + } + + SmartPlug getSmartPlug() + { + return smartPlug; + } + + uint8_t getCounter() + { + return counter; + } + + void setCounter(uint8_t val) + { + counter = val; + } +private: + SmartPlug smartPlug; + Ticker ticker; + uint8_t counter; + DigitalOut led; + SPCommunication spComm; + //BLECommunication bleComm; + vector<Observer*> observers; }; #endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/crc2.cpp Mon Jul 13 13:14:34 2015 +0000 @@ -0,0 +1,219 @@ + + +#include "crc2.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() +{ + 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() */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/crc2.h Mon Jul 13 13:14:34 2015 +0000 @@ -0,0 +1,78 @@ +/********************************************************************** + * + * 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 _crc2_h +#define _crc2_h + + +#define FALSE 0 +#define TRUE !FALSE + +/* + * Select the CRC standard from the list that follows. + */ +#define CRC_CCITT + + +#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(); +crc crcSlow(unsigned char const message[], int nBytes); +crc crcFast(unsigned char const message[], int nBytes); + + +#endif /* _crc_h */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/crc8.c Mon Jul 13 13:14:34 2015 +0000 @@ -0,0 +1,99 @@ +//================ File Despciption =========================================// +//=== File name : crc8.c +//=== +//#include "crc8.h" +//===========================================================================// + +//================ Index ====================================================// +// +//================ Include Header ===========================================// +// +//================ PUBLIC METHOD ============================================// +// extern function from another file +// +//================ PRIVATE METHOD ===========================================// +// non extern function +// +//================ PUBLIC DATA ==============================================// +// extern data +// +//================ PRIVATE DATA =============================================// +// non extern data +// +//================ PRIVATE DEFINE ===========================================// +// +//================ PRIVATE MACRO ============================================// +// +//================ SOURCE CODE ==============================================// +// + //This procedure calculates the cumulative Dallas Semiconductor +// 1-Wire CRC of all bytes passed to it. The result +//accumulates in the global variable CRC. +const unsigned char crc8_table[256]={ +0x00,0x07,0x0E,0x09,0x1C,0x1B,0x12,0x15,0x38,0x3F,0x36,0x31,0x24,0x23,0x2A,0x2D, +0x70,0x77,0x7E,0x79,0x6C,0x6B,0x62,0x65,0x48,0x4F,0x46,0x41,0x54,0x53,0x5A,0x5D, +0xE0,0xE7,0xEE,0xE9,0xFC,0xFB,0xF2,0xF5,0xD8,0xDF,0xD6,0xD1,0xC4,0xC3,0xCA,0xCB, +0x90,0x97,0x9E,0x99,0x8C,0x8B,0x82,0x85,0xA8,0xAF,0xA6,0xA1,0xB4,0xB3,0xBA,0xBD, +0xC7,0xC0,0xC9,0xCE,0xDB,0xDC,0xD5,0xD2,0xFF,0xF8,0xF1,0xF6,0xE3,0xE4,0xED,0xEA, +0xB7,0xB0,0xB9,0xBE,0xAB,0xAC,0xA5,0xA2,0x8F,0x88,0x81,0x86,0x93,0x94,0x9D,0x9A, +0x27,0x20,0x29,0x2E,0x3B,0x3C,0x35,0x32,0x1F,0x18,0x11,0x16,0x03,0x04,0x0D,0x0A, +0x57,0x50,0x59,0x5E,0x4B,0x4C,0x45,0x42,0x6F,0x68,0x61,0x66,0x73,0x74,0x7D,0x7A, +0x89,0x8E,0x87,0x80,0x95,0x92,0x9B,0x9C,0xB1,0xB6,0xBF,0xB8,0xAD,0xAA,0xA3,0xA4, +0xF9,0xFE,0xF7,0xF0,0xE5,0xE2,0xEB,0xEC,0xC1,0xC6,0xCF,0xC8,0xDD,0xDA,0xD3,0xD4, +0x69,0x6E,0x67,0x60,0x75,0x72,0x7B,0x7C,0x51,0x56,0x5F,0x58,0x4D,0x4A,0x43,0x44, +0x19,0x1E,0x17,0x10,0x05,0x02,0x0B,0x0C,0x21,0x26,0x2F,0x28,0x3D,0x3A,0x33,0x34, +0x4E,0x49,0x40,0x47,0x52,0x55,0x5C,0x5B,0x76,0x71,0x78,0x7F,0x6A,0x6D,0x64,0x63, +0x3E,0x39,0x30,0x37,0x22,0x25,0x2C,0x2B,0x06,0x01,0x08,0x0F,0x1A,0x1D,0x14,0x13, +0xAE,0xA9,0xA0,0xA7,0xB2,0xB5,0xBC,0xBB,0x96,0x91,0x98,0x9F,0x8A,0x8D,0x84,0x83, +0xDE,0xD9,0xD0,0xD7,0xC2,0xC5,0xCC,0xCB,0xE6,0xE1,0xE8,0xEF,0xFA,0xFD,0xF4,0xF3 + +}; + + +inline unsigned char crc8Compute(unsigned char *p,unsigned char length) +{ + unsigned char crc8; + unsigned char i; + unsigned char offset; + + crc8 = 0; + for(i = 0; i < length; i++) + { + offset = crc8 ^ *(p + i); + crc8 = crc8_table[offset]; + } + return crc8; + //return crc8 ^ 0x55; +} +/* +* Function: Do_CRC8 +* * Description: +* Computes the CRC value given the byte and the old CRC value as a static value +* +* Return Value: CRC value +* Parameters: Input data byte to be CRC'ed +* Remarks: +* uses a table driven method to speed things up. +* +* */ +inline unsigned char crc8OneByte(unsigned char xx,unsigned char ucCRC) +{ + ucCRC = crc8_table[ucCRC ^ xx]; + + return (ucCRC); +} + +unsigned char Crc8(unsigned char* xx,const int size, unsigned char ucCRC) +{ + + int i; + for(i=0; i<size; i++) + { + ucCRC=crc8OneByte(xx[i],ucCRC); + } + return ucCRC; +} +//================ END OF FILE ==============================================// +// + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/crc8.h Mon Jul 13 13:14:34 2015 +0000 @@ -0,0 +1,42 @@ +//================ File Despciption =========================================// +//=== File name : crc8.h +//=== +//===========================================================================// + +#ifndef CRC8__H +#define CRC8__H + +//================ Include Header ===========================================// +// + +//================ PULBIC DEFINE ============================================// +// +//================ PUBLIC MACRO =============================================// +// +//================ TYPEDEF DATA TYPE DEFINITION =============================// +// +//================ ENUMERATOR DEFINITION ====================================// +// +//================ TYPEDEF FUNCTION TYPE DEFFINITION ========================// +// +//================ TYPEDEF STRUCT/UNION =====================================// +// +//================ EXTERN FUNCTION ==========================================// + + +extern "C"{ + unsigned char Crc8(unsigned char* xx,const int size, unsigned char ucCRC); + inline unsigned char crc8OneByte(unsigned char xdata,unsigned char ucCRC); + inline unsigned char crc8Compute(unsigned char *p,unsigned char length); +}; +//================ EXTERN FUNCTION POINTER ==================================// +// +//================ EXTERN VARIABLE ==========================================// +// +//================ EXTERN QUEUE =============================================// +// +//================ END OF FILE ==============================================// +#endif +//#ifndef FILE_NAME_H + +