add remaining vendor files
This commit is contained in:
1 parent
75804f2191
commit
70719757d7
13 files changed
+1429
No files matched your search
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Sensirion AG
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name of Sensirion AG nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived from
|
||||||
|
* this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
#ifndef _SENSIRION_CORE_H_
|
||||||
|
#define _SENSIRION_CORE_H_
|
||||||
|
|
||||||
|
#include "SensirionCrc.h"
|
||||||
|
#include "SensirionErrors.h"
|
||||||
|
#include "SensirionRxFrame.h"
|
||||||
|
|
||||||
|
#include "SensirionShdlcCommunication.h"
|
||||||
|
#include "SensirionShdlcRxFrame.h"
|
||||||
|
#include "SensirionShdlcTxFrame.h"
|
||||||
|
|
||||||
|
#include "SensirionI2CCommunication.h"
|
||||||
|
#include "SensirionI2CRxFrame.h"
|
||||||
|
#include "SensirionI2CTxFrame.h"
|
||||||
|
|
||||||
|
#endif /* _SENSIRION_CORE_H_ */
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
*
|
||||||
|
* THIS IS A LEGACY FILE AND WILL BE REMOVED SOON.
|
||||||
|
*
|
||||||
|
* Copyright (c) 2020, Sensirion AG
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name of Sensirion AG nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived from
|
||||||
|
* this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
#ifndef _SENSIRION_CORE_ARDUINO_LIBRARY_H_
|
||||||
|
#define _SENSIRION_CORE_ARDUINO_LIBRARY_H_
|
||||||
|
|
||||||
|
#pragma GCC warning \
|
||||||
|
"Legacy file SensirionCoreArdunioLibrary.h included. Please include SensirionCore.h instead."
|
||||||
|
|
||||||
|
#include "SensirionErrors.h"
|
||||||
|
#include "SensirionRxFrame.h"
|
||||||
|
|
||||||
|
#include "SensirionShdlcCommunication.h"
|
||||||
|
#include "SensirionShdlcRxFrame.h"
|
||||||
|
#include "SensirionShdlcTxFrame.h"
|
||||||
|
|
||||||
|
#include "SensirionI2CCommunication.h"
|
||||||
|
#include "SensirionI2CRxFrame.h"
|
||||||
|
#include "SensirionI2CTxFrame.h"
|
||||||
|
|
||||||
|
#endif /* _SENSIRION_CORE_ARDUION_LIBRARY_H_ */
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022, Sensirion AG
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name of Sensirion AG nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived from
|
||||||
|
* this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "SensirionCrc.h"
|
||||||
|
|
||||||
|
uint8_t generateCRCGeneric(const uint8_t* data, size_t count, uint8_t init,
|
||||||
|
uint8_t polynomial) {
|
||||||
|
uint8_t crc = init;
|
||||||
|
|
||||||
|
/* calculates 8-Bit checksum with given polynomial */
|
||||||
|
for (size_t current_byte = 0; current_byte < count; ++current_byte) {
|
||||||
|
crc ^= (data[current_byte]);
|
||||||
|
for (uint8_t crc_bit = 8; crc_bit > 0; --crc_bit) {
|
||||||
|
if (crc & 0x80)
|
||||||
|
crc = (crc << 1) ^ polynomial;
|
||||||
|
else
|
||||||
|
crc = (crc << 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return crc;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t generateCRC31_ff(const uint8_t* data, size_t count) {
|
||||||
|
return generateCRCGeneric(data, count, 0xff, 0x31);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t generateCRC31_00(const uint8_t* data, size_t count) {
|
||||||
|
return generateCRCGeneric(data, count, 0x00, 0x31);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t generateCRC(const uint8_t* data, size_t count, CrcPolynomial type) {
|
||||||
|
if (CRC31_00 == type) {
|
||||||
|
return generateCRC31_00(data, count);
|
||||||
|
}
|
||||||
|
return generateCRC31_ff(data, count);
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022, Sensirion AG
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name of Sensirion AG nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived from
|
||||||
|
* this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _SENSIRION_CRC_H_
|
||||||
|
#define _SENSIRION_CRC_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
enum CrcPolynomial : uint8_t {
|
||||||
|
CRC31_00 = 0x0,
|
||||||
|
CRC31_ff = 0x1,
|
||||||
|
};
|
||||||
|
|
||||||
|
uint8_t generateCRCGeneric(const uint8_t* data, size_t count, uint8_t init,
|
||||||
|
uint8_t polynomial);
|
||||||
|
|
||||||
|
uint8_t generateCRC31_ff(const uint8_t* data, size_t count);
|
||||||
|
|
||||||
|
uint8_t generateCRC31_00(const uint8_t* data, size_t count);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Generate a crc for data given a polynomial type
|
||||||
|
*
|
||||||
|
* @param data data to calculate CRC for
|
||||||
|
* @param count the array size of data
|
||||||
|
* @param poly CRC polynomal to use
|
||||||
|
*/
|
||||||
|
uint8_t generateCRC(const uint8_t* data, size_t count, CrcPolynomial type);
|
||||||
|
|
||||||
|
#endif /* _SENSIRION_CRC_H_ */
|
||||||
@@ -0,0 +1,161 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Sensirion AG
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name of Sensirion AG nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived from
|
||||||
|
* this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
#include "SensirionErrors.h"
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
void errorToString(uint16_t error, char errorMessage[],
|
||||||
|
size_t errorMessageSize) {
|
||||||
|
|
||||||
|
uint16_t highLevelError = error & 0xFF00;
|
||||||
|
uint16_t lowLevelError = error & 0x00FF;
|
||||||
|
|
||||||
|
if (error & HighLevelError::SensorSpecificError) {
|
||||||
|
snprintf(errorMessage, errorMessageSize, "Sensor specific error: 0x%2x",
|
||||||
|
lowLevelError);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (highLevelError) {
|
||||||
|
case HighLevelError::NoError:
|
||||||
|
if (!error) {
|
||||||
|
strncpy(errorMessage, "No error", errorMessageSize);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case HighLevelError::WriteError:
|
||||||
|
switch (lowLevelError) {
|
||||||
|
case LowLevelError::Undefined:
|
||||||
|
strncpy(errorMessage, "Write error",
|
||||||
|
errorMessageSize);
|
||||||
|
return;
|
||||||
|
case LowLevelError::SerialWriteError:
|
||||||
|
strncpy(errorMessage, "Error writing to serial",
|
||||||
|
errorMessageSize);
|
||||||
|
return;
|
||||||
|
case LowLevelError::InternalBufferSizeError:
|
||||||
|
strncpy(errorMessage,
|
||||||
|
"Data too long to fit in transmit buffer",
|
||||||
|
errorMessageSize);
|
||||||
|
return;
|
||||||
|
case LowLevelError::I2cAddressNack:
|
||||||
|
strncpy(errorMessage,
|
||||||
|
"Received NACK on transmit of address",
|
||||||
|
errorMessageSize);
|
||||||
|
return;
|
||||||
|
case LowLevelError::I2cDataNack:
|
||||||
|
strncpy(errorMessage, "Received NACK on transmit of data",
|
||||||
|
errorMessageSize);
|
||||||
|
return;
|
||||||
|
case LowLevelError::I2cOtherError:
|
||||||
|
strncpy(errorMessage, "Error writing to I2C bus",
|
||||||
|
errorMessageSize);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case HighLevelError::ReadError:
|
||||||
|
switch (lowLevelError) {
|
||||||
|
case LowLevelError::Undefined:
|
||||||
|
strncpy(errorMessage, "Read error", errorMessageSize);
|
||||||
|
return;
|
||||||
|
case LowLevelError::NonemptyFrameError:
|
||||||
|
strncpy(errorMessage, "Frame already contains data",
|
||||||
|
errorMessageSize);
|
||||||
|
return;
|
||||||
|
case LowLevelError::TimeoutError:
|
||||||
|
strncpy(errorMessage, "Timeout while reading data",
|
||||||
|
errorMessageSize);
|
||||||
|
return;
|
||||||
|
case LowLevelError::ChecksumError:
|
||||||
|
strncpy(errorMessage, "Checksum is wrong",
|
||||||
|
errorMessageSize);
|
||||||
|
return;
|
||||||
|
case LowLevelError::CRCError:
|
||||||
|
strncpy(errorMessage, "Wrong CRC found", errorMessageSize);
|
||||||
|
return;
|
||||||
|
case LowLevelError::WrongNumberBytesError:
|
||||||
|
strncpy(errorMessage, "Number of bytes not a multiple of 3",
|
||||||
|
errorMessageSize);
|
||||||
|
return;
|
||||||
|
case LowLevelError::NotEnoughDataError:
|
||||||
|
strncpy(errorMessage, "Not enough data received",
|
||||||
|
errorMessageSize);
|
||||||
|
return;
|
||||||
|
case LowLevelError::InternalBufferSizeError:
|
||||||
|
strncpy(errorMessage, "Internal I2C buffer too small",
|
||||||
|
errorMessageSize);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case HighLevelError::ExecutionError: {
|
||||||
|
char format[] = "Execution error, status register: 0x%x";
|
||||||
|
snprintf(errorMessage, errorMessageSize, format, lowLevelError);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case HighLevelError::TxFrameError:
|
||||||
|
switch (lowLevelError) {
|
||||||
|
case LowLevelError::Undefined:
|
||||||
|
strncpy(errorMessage, "Tx frame error", errorMessageSize);
|
||||||
|
return;
|
||||||
|
case LowLevelError::BufferSizeError:
|
||||||
|
strncpy(errorMessage, "Not enough space in buffer",
|
||||||
|
errorMessageSize);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case HighLevelError::RxFrameError:
|
||||||
|
switch (lowLevelError) {
|
||||||
|
case LowLevelError::Undefined:
|
||||||
|
strncpy(errorMessage, "Rx frame error", errorMessageSize);
|
||||||
|
return;
|
||||||
|
case LowLevelError::BufferSizeError:
|
||||||
|
strncpy(errorMessage, "Not enough space in buffer",
|
||||||
|
errorMessageSize);
|
||||||
|
return;
|
||||||
|
case LowLevelError::NoDataError:
|
||||||
|
strncpy(errorMessage, "No more data in frame",
|
||||||
|
errorMessageSize);
|
||||||
|
return;
|
||||||
|
case LowLevelError::RxAddressError:
|
||||||
|
strncpy(errorMessage, "Wrong address in return frame",
|
||||||
|
errorMessageSize);
|
||||||
|
return;
|
||||||
|
case LowLevelError::RxCommandError:
|
||||||
|
strncpy(errorMessage, "Wrong command in return frame",
|
||||||
|
errorMessageSize);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
strncpy(errorMessage, "Error processing error", errorMessageSize);
|
||||||
|
return;
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Sensirion AG
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name of Sensirion AG nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived from
|
||||||
|
* this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
#ifndef _SENSIRION_ERRORS_H_
|
||||||
|
#define _SENSIRION_ERRORS_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
enum HighLevelError : uint16_t {
|
||||||
|
// general errors
|
||||||
|
NoError = 0,
|
||||||
|
WriteError = 0x0100,
|
||||||
|
ReadError = 0x0200,
|
||||||
|
TxFrameError = 0x0300,
|
||||||
|
RxFrameError = 0x0400,
|
||||||
|
// shdlc errors
|
||||||
|
ExecutionError = 0x0500,
|
||||||
|
// i2c errors
|
||||||
|
|
||||||
|
// Sensor specific errors. All errors higher than that are depending on the
|
||||||
|
// sensor used.
|
||||||
|
SensorSpecificError = 0x8000,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum LowLevelError : uint8_t {
|
||||||
|
// general errors
|
||||||
|
Undefined = 0,
|
||||||
|
NonemptyFrameError,
|
||||||
|
NoDataError,
|
||||||
|
BufferSizeError,
|
||||||
|
// shdlc errors
|
||||||
|
StopByteError,
|
||||||
|
ChecksumError,
|
||||||
|
TimeoutError,
|
||||||
|
RxCommandError,
|
||||||
|
RxAddressError,
|
||||||
|
SerialWriteError,
|
||||||
|
// i2c errors
|
||||||
|
WrongNumberBytesError,
|
||||||
|
CRCError,
|
||||||
|
I2cAddressNack,
|
||||||
|
I2cDataNack,
|
||||||
|
I2cOtherError,
|
||||||
|
NotEnoughDataError,
|
||||||
|
InternalBufferSizeError,
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* errorToString() - Convert error code to a human readable error message
|
||||||
|
*
|
||||||
|
* @param error Error code to be converted.
|
||||||
|
* @param errorMessage String where the error text can be
|
||||||
|
* stored.
|
||||||
|
* @param errorMessageSize Size in bytes of the string buffer for the error
|
||||||
|
* message.
|
||||||
|
*/
|
||||||
|
void errorToString(uint16_t error, char errorMessage[],
|
||||||
|
size_t errorMessageSize);
|
||||||
|
|
||||||
|
#endif /* _SENSIRION_ERRORS_H_ */
|
||||||
@@ -0,0 +1,134 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Sensirion AG
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name of Sensirion AG nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived from
|
||||||
|
* this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
#include "SensirionI2CCommunication.h"
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "Arduino.h"
|
||||||
|
#include "SensirionCrc.h"
|
||||||
|
#include "SensirionErrors.h"
|
||||||
|
#include "SensirionI2CRxFrame.h"
|
||||||
|
#include "SensirionI2CTxFrame.h"
|
||||||
|
|
||||||
|
static void clearRxBuffer(TwoWire& i2cBus) {
|
||||||
|
while (i2cBus.available()) {
|
||||||
|
(void)i2cBus.read();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionI2CCommunication::sendFrame(uint8_t address,
|
||||||
|
SensirionI2CTxFrame& frame,
|
||||||
|
TwoWire& i2cBus) {
|
||||||
|
i2cBus.beginTransmission(address);
|
||||||
|
size_t writtenBytes = i2cBus.write(frame._buffer, frame._index);
|
||||||
|
uint8_t i2c_error = i2cBus.endTransmission();
|
||||||
|
if (writtenBytes != frame._index) {
|
||||||
|
return static_cast<uint16_t>(WriteError) |
|
||||||
|
static_cast<uint8_t>(I2cOtherError);
|
||||||
|
}
|
||||||
|
// translate Arduino errors, see
|
||||||
|
// https://www.arduino.cc/en/Reference/WireEndTransmission
|
||||||
|
switch (i2c_error) {
|
||||||
|
case 0:
|
||||||
|
return NoError;
|
||||||
|
case 1:
|
||||||
|
return static_cast<uint16_t>(WriteError) |
|
||||||
|
static_cast<uint8_t>(InternalBufferSizeError);
|
||||||
|
case 2:
|
||||||
|
return static_cast<uint16_t>(WriteError) |
|
||||||
|
static_cast<uint8_t>(I2cAddressNack);
|
||||||
|
case 3:
|
||||||
|
return static_cast<uint16_t>(WriteError) |
|
||||||
|
static_cast<uint8_t>(I2cDataNack);
|
||||||
|
default:
|
||||||
|
return static_cast<uint16_t>(WriteError) |
|
||||||
|
static_cast<uint8_t>(I2cOtherError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionI2CCommunication::receiveFrame(uint8_t address,
|
||||||
|
size_t numBytes,
|
||||||
|
SensirionI2CRxFrame& frame,
|
||||||
|
TwoWire& i2cBus,
|
||||||
|
CrcPolynomial poly) {
|
||||||
|
|
||||||
|
#ifdef I2C_BUFFER_LENGTH
|
||||||
|
const uint8_t sizeBuffer =
|
||||||
|
(static_cast<uint8_t>(I2C_BUFFER_LENGTH) / static_cast<uint8_t>(3)) * 3;
|
||||||
|
#elif defined(BUFFER_LENGTH)
|
||||||
|
const uint8_t sizeBuffer =
|
||||||
|
(static_cast<uint8_t>(BUFFER_LENGTH) / static_cast<uint8_t>(3)) * 3;
|
||||||
|
#else
|
||||||
|
const uint8_t sizeBuffer = 48;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (numBytes % 3) {
|
||||||
|
return static_cast<uint16_t>(ReadError) |
|
||||||
|
static_cast<uint8_t>(WrongNumberBytesError);
|
||||||
|
}
|
||||||
|
if ((numBytes / 3) * 2 > frame._bufferSize) {
|
||||||
|
return static_cast<uint16_t>(ReadError) |
|
||||||
|
static_cast<uint8_t>(BufferSizeError);
|
||||||
|
}
|
||||||
|
size_t i = 0;
|
||||||
|
int16_t remaining = numBytes;
|
||||||
|
uint8_t bytesToRead = sizeBuffer;
|
||||||
|
bool stop = bytesToRead >= remaining;
|
||||||
|
do {
|
||||||
|
if (sizeBuffer >= remaining) {
|
||||||
|
stop = true;
|
||||||
|
bytesToRead = remaining;
|
||||||
|
}
|
||||||
|
uint8_t available = i2cBus.requestFrom(address, bytesToRead,
|
||||||
|
static_cast<uint8_t>(stop));
|
||||||
|
if (bytesToRead != available) {
|
||||||
|
return static_cast<uint16_t>(ReadError) |
|
||||||
|
static_cast<uint8_t>(NotEnoughDataError);
|
||||||
|
}
|
||||||
|
while (available > 0) {
|
||||||
|
frame._buffer[i++] = i2cBus.read();
|
||||||
|
frame._buffer[i++] = i2cBus.read();
|
||||||
|
uint8_t actualCRC = i2cBus.read();
|
||||||
|
uint8_t expectedCRC = generateCRC(&frame._buffer[i - 2], 2, poly);
|
||||||
|
if (actualCRC != expectedCRC) {
|
||||||
|
clearRxBuffer(i2cBus);
|
||||||
|
return static_cast<uint16_t>(ReadError) |
|
||||||
|
static_cast<uint8_t>(CRCError);
|
||||||
|
}
|
||||||
|
available -= 3;
|
||||||
|
}
|
||||||
|
remaining -= bytesToRead;
|
||||||
|
|
||||||
|
} while (remaining > 0);
|
||||||
|
frame._numBytes = i;
|
||||||
|
return NoError;
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Sensirion AG
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name of Sensirion AG nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived from
|
||||||
|
* this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
#ifndef SENSIRION_I2C_COMMUNICATION_H_
|
||||||
|
#define SENSIRION_I2C_COMMUNICATION_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "Arduino.h"
|
||||||
|
#include "Wire.h"
|
||||||
|
|
||||||
|
#include "SensirionI2CRxFrame.h"
|
||||||
|
#include "SensirionI2CTxFrame.h"
|
||||||
|
|
||||||
|
class SensirionI2CTxFrame;
|
||||||
|
class SensirionI2CRxFrame;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* SensirionI2CCommunication - Class which is responsible for the communication
|
||||||
|
* via a I2C bus. It provides functionality to send and receive frames from a
|
||||||
|
* Sensirion sensor. The data is sent and received in a SensirionI2cTxFrame or
|
||||||
|
* SensirionI2cRxFrame respectively.
|
||||||
|
*/
|
||||||
|
class SensirionI2CCommunication {
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* sendFrame() - Sends frame to sensor
|
||||||
|
*
|
||||||
|
* @param address I2C address of the sensor.
|
||||||
|
* @param frame Tx frame object containing a finished frame to send to
|
||||||
|
* the sensor.
|
||||||
|
* @param i2cBus TwoWire object to communicate with the sensor.
|
||||||
|
*
|
||||||
|
* @return NoError on success, an error code otherwise
|
||||||
|
*/
|
||||||
|
static uint16_t sendFrame(uint8_t address, SensirionI2CTxFrame& frame,
|
||||||
|
TwoWire& i2cBus);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* receiveFrame() - Receive Frame from sensor
|
||||||
|
*
|
||||||
|
* @param address I2C address of the sensor.
|
||||||
|
* @param numBytes Number of bytes to receive.
|
||||||
|
* @param frame Rx frame to store the received data in.
|
||||||
|
* @param i2cBus TwoWire object to communicate with the sensor.
|
||||||
|
* @param poly CRC polynomal to use. Defaults to 0x31 with init 0xFF
|
||||||
|
*
|
||||||
|
* @return NoError on success, an error code otherwise
|
||||||
|
*/
|
||||||
|
static uint16_t receiveFrame(uint8_t address, size_t numBytes,
|
||||||
|
SensirionI2CRxFrame& frame, TwoWire& i2cBus,
|
||||||
|
CrcPolynomial poly = CRC31_ff);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* SENSIRION_I2C_COMMUNICATION_H_ */
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Sensirion AG
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name of Sensirion AG nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived from
|
||||||
|
* this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
#ifndef SENSIRION_I2C_RX_FRAME_H_
|
||||||
|
#define SENSIRION_I2C_RX_FRAME_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "SensirionI2CCommunication.h"
|
||||||
|
#include "SensirionRxFrame.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SenirionI2CRxFrame - Class which decodes the through I2C received data into
|
||||||
|
* common data types. It contains a buffer which is filled by the
|
||||||
|
* SensirionI2CCommunication class. By calling the different decode function
|
||||||
|
* inherited from the SensirionRxFrame base class the raw data can be decoded
|
||||||
|
* into different data types.
|
||||||
|
*/
|
||||||
|
class SensirionI2CRxFrame : public SensirionRxFrame {
|
||||||
|
|
||||||
|
friend class SensirionI2CCommunication;
|
||||||
|
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param buffer Buffer in which the receive frame will be
|
||||||
|
* stored.
|
||||||
|
* @param bufferSize Number of bytes in the buffer for the receive frame.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
SensirionI2CRxFrame(uint8_t buffer[], size_t bufferSize)
|
||||||
|
: SensirionRxFrame(buffer, bufferSize){};
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* SENSIRION_I2C_RX_FRAME_H_ */
|
||||||
@@ -0,0 +1,147 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Sensirion AG
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name of Sensirion AG nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived from
|
||||||
|
* software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "SensirionI2CTxFrame.h"
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "SensirionCrc.h"
|
||||||
|
#include "SensirionErrors.h"
|
||||||
|
|
||||||
|
SensirionI2CTxFrame::SensirionI2CTxFrame(uint8_t buffer[], size_t bufferSize,
|
||||||
|
size_t numCommandBytes,
|
||||||
|
CrcPolynomial poly)
|
||||||
|
: _buffer(buffer), _bufferSize(bufferSize), _index(numCommandBytes),
|
||||||
|
_numCommandBytes(numCommandBytes), _polynomial_type(poly) {
|
||||||
|
}
|
||||||
|
|
||||||
|
SensirionI2CTxFrame::SensirionI2CTxFrame(uint8_t buffer[], size_t bufferSize,
|
||||||
|
CrcPolynomial poly)
|
||||||
|
: SensirionI2CTxFrame(buffer, bufferSize, 2, poly) {
|
||||||
|
}
|
||||||
|
|
||||||
|
SensirionI2CTxFrame SensirionI2CTxFrame::createWithUInt8Command(
|
||||||
|
uint8_t command, uint8_t buffer[], size_t bufferSize, CrcPolynomial poly) {
|
||||||
|
SensirionI2CTxFrame instance =
|
||||||
|
SensirionI2CTxFrame(buffer, bufferSize, 1, poly);
|
||||||
|
instance._buffer[0] = command;
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
SensirionI2CTxFrame SensirionI2CTxFrame::createWithUInt16Command(
|
||||||
|
uint16_t command, uint8_t buffer[], size_t bufferSize, CrcPolynomial poly) {
|
||||||
|
SensirionI2CTxFrame instance =
|
||||||
|
SensirionI2CTxFrame(buffer, bufferSize, 2, poly);
|
||||||
|
instance._buffer[0] = static_cast<uint8_t>((command & 0xFF00) >> 8);
|
||||||
|
instance._buffer[1] = static_cast<uint8_t>((command & 0x00FF) >> 0);
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionI2CTxFrame::addCommand(uint16_t command) {
|
||||||
|
if (_bufferSize < 2) {
|
||||||
|
return static_cast<uint16_t>(TxFrameError) |
|
||||||
|
static_cast<uint8_t>(BufferSizeError);
|
||||||
|
}
|
||||||
|
_buffer[0] = static_cast<uint8_t>((command & 0xFF00) >> 8);
|
||||||
|
_buffer[1] = static_cast<uint8_t>((command & 0x00FF) >> 0);
|
||||||
|
return NoError;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionI2CTxFrame::addUInt32(uint32_t data) {
|
||||||
|
uint16_t error = _addByte(static_cast<uint8_t>((data & 0xFF000000) >> 24));
|
||||||
|
error |= _addByte(static_cast<uint8_t>((data & 0x00FF0000) >> 16));
|
||||||
|
error |= _addByte(static_cast<uint8_t>((data & 0x0000FF00) >> 8));
|
||||||
|
error |= _addByte(static_cast<uint8_t>((data & 0x000000FF) >> 0));
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionI2CTxFrame::addInt32(int32_t data) {
|
||||||
|
return addUInt32(static_cast<uint32_t>(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionI2CTxFrame::addUInt16(uint16_t data) {
|
||||||
|
uint16_t error = _addByte(static_cast<uint8_t>((data & 0xFF00) >> 8));
|
||||||
|
error |= _addByte(static_cast<uint8_t>((data & 0x00FF) >> 0));
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionI2CTxFrame::addInt16(int16_t data) {
|
||||||
|
return addUInt16(static_cast<uint16_t>(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionI2CTxFrame::addUInt8(uint8_t data) {
|
||||||
|
return _addByte(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionI2CTxFrame::addInt8(int8_t data) {
|
||||||
|
return _addByte(static_cast<uint8_t>(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionI2CTxFrame::addBool(bool data) {
|
||||||
|
return _addByte(static_cast<uint8_t>(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionI2CTxFrame::addFloat(float data) {
|
||||||
|
union {
|
||||||
|
uint32_t uInt32Data;
|
||||||
|
float floatData;
|
||||||
|
} convert;
|
||||||
|
|
||||||
|
convert.floatData = data;
|
||||||
|
return addUInt32(convert.uInt32Data);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionI2CTxFrame::addBytes(const uint8_t data[],
|
||||||
|
size_t dataLength) {
|
||||||
|
uint16_t error = 0;
|
||||||
|
for (size_t i = 0; i < dataLength; i++) {
|
||||||
|
error |= _addByte(data[i]);
|
||||||
|
}
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionI2CTxFrame::_addByte(uint8_t data) {
|
||||||
|
if (_bufferSize <= _index) {
|
||||||
|
return static_cast<uint16_t>(TxFrameError) |
|
||||||
|
static_cast<uint8_t>(BufferSizeError);
|
||||||
|
}
|
||||||
|
_buffer[_index++] = data;
|
||||||
|
if ((_index - _numCommandBytes) % 3 == 2) {
|
||||||
|
if (_bufferSize <= _index) {
|
||||||
|
return static_cast<uint16_t>(TxFrameError) |
|
||||||
|
static_cast<uint8_t>(BufferSizeError);
|
||||||
|
}
|
||||||
|
uint8_t crc = generateCRC(&_buffer[_index - 2], 2, _polynomial_type);
|
||||||
|
_buffer[_index++] = crc;
|
||||||
|
}
|
||||||
|
return NoError;
|
||||||
|
}
|
||||||
@@ -0,0 +1,197 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Sensirion AG
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name of Sensirion AG nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived from
|
||||||
|
* this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
#ifndef SENSIRION_I2C_TX_FRAME_H_
|
||||||
|
#define SENSIRION_I2C_TX_FRAME_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "SensirionCrc.h"
|
||||||
|
#include "SensirionI2CCommunication.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* SensirionI2CTxFrame - Class which helps to build a correct I2C frame for
|
||||||
|
* Sensirion Sensors. The different addDatatype() functions add the frame data
|
||||||
|
* and the addCommand() function writes the command at the beginning. Using
|
||||||
|
* these functions one can easily construct a I2C frame for Sensirion sensors.
|
||||||
|
*/
|
||||||
|
class SensirionI2CTxFrame {
|
||||||
|
|
||||||
|
friend class SensirionI2CCommunication;
|
||||||
|
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Factory to create a SensirionI2CTxFrame using a UInt8 command.
|
||||||
|
*
|
||||||
|
* @param command Command to add to the send frame.
|
||||||
|
* @param buffer Buffer in which the send frame will be stored.
|
||||||
|
* @param bufferSize Number of bytes in the buffer for the send frame.
|
||||||
|
* @param poly CRC polynomal to use. Defaults to 0x31 with init 0xFF
|
||||||
|
*
|
||||||
|
* @return the constructed SensirionI2CTxFrame.
|
||||||
|
*/
|
||||||
|
static SensirionI2CTxFrame
|
||||||
|
createWithUInt8Command(uint8_t command, uint8_t buffer[], size_t bufferSize,
|
||||||
|
CrcPolynomial poly = CRC31_ff);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory to create a SensirionI2CTxFrame using a UInt16 command.
|
||||||
|
*
|
||||||
|
* @param command Command to add to the send frame.
|
||||||
|
* @param buffer Buffer in which the send frame will be stored.
|
||||||
|
* @param bufferSize Number of bytes in the buffer for the send frame.
|
||||||
|
* @param poly CRC polynomal to use. Defaults to 0x31 with init 0xFF
|
||||||
|
*
|
||||||
|
* @return the constructed SensirionI2CTxFrame.
|
||||||
|
*/
|
||||||
|
static SensirionI2CTxFrame
|
||||||
|
createWithUInt16Command(uint16_t command, uint8_t buffer[],
|
||||||
|
size_t bufferSize, CrcPolynomial poly = CRC31_ff);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param buffer Buffer in which the send frame will be stored.
|
||||||
|
* @param bufferSize Number of bytes in the buffer for the send frame.
|
||||||
|
* @param poly CRC polynomal to use. Defaults to 0x31 with init 0xFF
|
||||||
|
*
|
||||||
|
* @deprecated Use createWithUInt16Command() instead
|
||||||
|
*/
|
||||||
|
SensirionI2CTxFrame(uint8_t buffer[], size_t bufferSize,
|
||||||
|
CrcPolynomial poly = CRC31_ff);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* addCommand() - Add command to the send frame.
|
||||||
|
*
|
||||||
|
* @param command Command to add to the send frame.
|
||||||
|
*
|
||||||
|
* @return NoError on success, an error code otherwise
|
||||||
|
*
|
||||||
|
* @deprecated Use createWithUInt16Command() instead
|
||||||
|
*/
|
||||||
|
uint16_t addCommand(uint16_t command);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* addUInt32() - Add unsigned 32bit integer to the send frame.
|
||||||
|
*
|
||||||
|
* @param data Unsigned 32bit integer to add to the send frame.
|
||||||
|
*
|
||||||
|
* @return NoError on success, an error code otherwise
|
||||||
|
*/
|
||||||
|
uint16_t addUInt32(uint32_t data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* addInt32() - Add signed 32bit integer to the send frame.
|
||||||
|
*
|
||||||
|
* @param data Signed 32bit integer to add to the send frame.
|
||||||
|
*
|
||||||
|
* @return NoError on success, an error code otherwise
|
||||||
|
*/
|
||||||
|
uint16_t addInt32(int32_t data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* addUInt16() - Add unsigned 16bit integer to the send frame.
|
||||||
|
*
|
||||||
|
* @param data Unsigned 16bit integer to add to the send frame.
|
||||||
|
*
|
||||||
|
* @return NoError on success, an error code otherwise
|
||||||
|
*/
|
||||||
|
uint16_t addUInt16(uint16_t data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* addInt16() - Add signed 16bit integer to the send frame.
|
||||||
|
*
|
||||||
|
* @param data Signed 16bit integer to add to the send frame.
|
||||||
|
*
|
||||||
|
* @return NoError on success, an error code otherwise
|
||||||
|
*/
|
||||||
|
uint16_t addInt16(int16_t data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* addUInt8() - Add unsigned 8bit integer to the send frame.
|
||||||
|
*
|
||||||
|
* @param data Unsigned 8bit integer to add to the send frame.
|
||||||
|
*
|
||||||
|
* @return NoError on success, an error code otherwise
|
||||||
|
*/
|
||||||
|
uint16_t addUInt8(uint8_t data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* addInt8() - Add signed 8bit integer to the send frame.
|
||||||
|
*
|
||||||
|
* @param data Signed 8bit integer to add to the send frame.
|
||||||
|
*
|
||||||
|
* @return NoError on success, an error code otherwise
|
||||||
|
*/
|
||||||
|
uint16_t addInt8(int8_t data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* addBool() - Add boolean to the send frame.
|
||||||
|
*
|
||||||
|
* @param data Boolean to add to the send frame.
|
||||||
|
*
|
||||||
|
* @return NoError on success, an error code otherwise
|
||||||
|
*/
|
||||||
|
uint16_t addBool(bool data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* addFloat() - Add float to the send frame.
|
||||||
|
*
|
||||||
|
* @param data Float to add to the send frame.
|
||||||
|
*
|
||||||
|
* @return NoError on success, an error code otherwise
|
||||||
|
*/
|
||||||
|
uint16_t addFloat(float data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* addBytes() - Add byte array to the send frame.
|
||||||
|
*
|
||||||
|
* @param data Byte array to add to the send frame.
|
||||||
|
* @param dataLength Number of bytes to add to the send frame.
|
||||||
|
*
|
||||||
|
* @return NoError on success, an error code otherwise
|
||||||
|
*/
|
||||||
|
uint16_t addBytes(const uint8_t data[], size_t dataLength);
|
||||||
|
|
||||||
|
private:
|
||||||
|
SensirionI2CTxFrame(uint8_t buffer[], size_t bufferSize,
|
||||||
|
size_t numCommandBytes, CrcPolynomial poly = CRC31_ff);
|
||||||
|
|
||||||
|
uint16_t _addByte(uint8_t data);
|
||||||
|
|
||||||
|
uint8_t* _buffer;
|
||||||
|
size_t _bufferSize;
|
||||||
|
size_t _index;
|
||||||
|
size_t _numCommandBytes;
|
||||||
|
CrcPolynomial _polynomial_type;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* SENSIRION_I2C_TX_FRAME_H_ */
|
||||||
@@ -0,0 +1,160 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Sensirion AG
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name of Sensirion AG nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived from
|
||||||
|
* this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "SensirionRxFrame.h"
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "SensirionErrors.h"
|
||||||
|
|
||||||
|
SensirionRxFrame::SensirionRxFrame(uint8_t buffer[], size_t bufferSize)
|
||||||
|
: _buffer(buffer), _bufferSize(bufferSize), _index(0), _numBytes(0) {
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionRxFrame::getUInt32(uint32_t& data) {
|
||||||
|
if (_numBytes < 4) {
|
||||||
|
return static_cast<uint16_t>(RxFrameError) |
|
||||||
|
static_cast<uint8_t>(NoDataError);
|
||||||
|
}
|
||||||
|
data = static_cast<uint32_t>(_buffer[_index++]) << 24;
|
||||||
|
data |= static_cast<uint32_t>(_buffer[_index++]) << 16;
|
||||||
|
data |= static_cast<uint32_t>(_buffer[_index++]) << 8;
|
||||||
|
data |= static_cast<uint32_t>(_buffer[_index++]);
|
||||||
|
_numBytes -= 4;
|
||||||
|
return NoError;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionRxFrame::getInt32(int32_t& data) {
|
||||||
|
uint32_t ret = 0;
|
||||||
|
uint16_t error = getUInt32(ret);
|
||||||
|
data = static_cast<int32_t>(ret);
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionRxFrame::getUInt16(uint16_t& data) {
|
||||||
|
if (_numBytes < 2) {
|
||||||
|
return static_cast<uint16_t>(RxFrameError) |
|
||||||
|
static_cast<uint8_t>(NoDataError);
|
||||||
|
}
|
||||||
|
data = static_cast<uint16_t>(_buffer[_index++]) << 8;
|
||||||
|
data |= static_cast<uint16_t>(_buffer[_index++]);
|
||||||
|
_numBytes -= 2;
|
||||||
|
return NoError;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionRxFrame::getInt16(int16_t& data) {
|
||||||
|
uint16_t ret = 0;
|
||||||
|
uint16_t error = getUInt16(ret);
|
||||||
|
data = static_cast<int16_t>(ret);
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionRxFrame::getUInt8(uint8_t& data) {
|
||||||
|
if (_numBytes < 1) {
|
||||||
|
return static_cast<uint16_t>(RxFrameError) |
|
||||||
|
static_cast<uint8_t>(NoDataError);
|
||||||
|
}
|
||||||
|
data = _buffer[_index++];
|
||||||
|
_numBytes -= 1;
|
||||||
|
return NoError;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionRxFrame::getInt8(int8_t& data) {
|
||||||
|
if (_numBytes < 1) {
|
||||||
|
return static_cast<uint16_t>(RxFrameError) |
|
||||||
|
static_cast<uint8_t>(NoDataError);
|
||||||
|
}
|
||||||
|
data = static_cast<int8_t>(_buffer[_index++]);
|
||||||
|
_numBytes -= 1;
|
||||||
|
return NoError;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionRxFrame::getBool(bool& data) {
|
||||||
|
if (_numBytes < 1) {
|
||||||
|
return static_cast<uint16_t>(RxFrameError) |
|
||||||
|
static_cast<uint8_t>(NoDataError);
|
||||||
|
}
|
||||||
|
data = static_cast<bool>(_buffer[_index++]);
|
||||||
|
_numBytes -= 1;
|
||||||
|
return NoError;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionRxFrame::getFloat(float& data) {
|
||||||
|
union {
|
||||||
|
uint32_t uInt32Data;
|
||||||
|
float floatData;
|
||||||
|
} convert = {0};
|
||||||
|
uint16_t error = getUInt32(convert.uInt32Data);
|
||||||
|
data = convert.floatData;
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionRxFrame::getBytes(uint8_t data[], size_t maxBytes) {
|
||||||
|
if (_numBytes < 1) {
|
||||||
|
return static_cast<uint16_t>(RxFrameError) |
|
||||||
|
static_cast<uint8_t>(NoDataError);
|
||||||
|
}
|
||||||
|
size_t readAmount = maxBytes;
|
||||||
|
if (_numBytes < maxBytes) {
|
||||||
|
readAmount = _numBytes;
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < readAmount; i++) {
|
||||||
|
data[i] = _buffer[_index++];
|
||||||
|
}
|
||||||
|
_numBytes -= readAmount;
|
||||||
|
return NoError;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t SensirionRxFrame::getInteger(uint8_t* destination, IntegerType type,
|
||||||
|
uint8_t nrOfBytes) {
|
||||||
|
|
||||||
|
if (_numBytes < nrOfBytes) {
|
||||||
|
return static_cast<uint16_t>(RxFrameError) |
|
||||||
|
static_cast<uint8_t>(NoDataError);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nrOfBytes > type) {
|
||||||
|
return RxFrameError;
|
||||||
|
}
|
||||||
|
|
||||||
|
// pad missing bytes
|
||||||
|
uint8_t offset = type - nrOfBytes;
|
||||||
|
for (uint8_t i = 0; i < offset; i++) {
|
||||||
|
destination[type - i - 1] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint8_t i = 1; i <= nrOfBytes; i++) {
|
||||||
|
destination[type - offset - i] = _buffer[_index++];
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,175 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Sensirion AG
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name of Sensirion AG nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived from
|
||||||
|
* this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
#ifndef SENSIRION_RX_FRAME_H_
|
||||||
|
#define SENSIRION_RX_FRAME_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
typedef enum : uint16_t {
|
||||||
|
Byte = 1,
|
||||||
|
Short = 2,
|
||||||
|
Integer = 4,
|
||||||
|
LongInteger = 8
|
||||||
|
} IntegerType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SenirionRxFrame - Base class for SensirionShdlcRxFrame and
|
||||||
|
* SensirionI2cRxFrame. It decodes received data into common data types. The
|
||||||
|
* data is contained in a buffer which is filled by the one of the two
|
||||||
|
* communication classes. By calling the different decode function the raw data
|
||||||
|
* can be decoded into different data types.
|
||||||
|
*/
|
||||||
|
class SensirionRxFrame {
|
||||||
|
|
||||||
|
friend class SensirionI2CCommunication;
|
||||||
|
friend class SensirionShdlcCommunication;
|
||||||
|
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param buffer Buffer in which the receive frame will be stored.
|
||||||
|
* @param bufferSize Number of bytes in the buffer for the receive frame.
|
||||||
|
*/
|
||||||
|
SensirionRxFrame(uint8_t buffer[], size_t bufferSize);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getInteger() - Read an integer value of specified type.
|
||||||
|
*
|
||||||
|
* If number of bytes is less than the size of the specified integer, the
|
||||||
|
* value is padded with 0.
|
||||||
|
*
|
||||||
|
* @param destination Pointer to the integer number that shall be read
|
||||||
|
* @param type specified integer type
|
||||||
|
* @param nrOfBytes Number of bytes read from the buffer. If nrOfBytes is
|
||||||
|
* less than the size of the integer value, the value is
|
||||||
|
* padded with 0.
|
||||||
|
* @return NoError if the integer can be read from the receive buffer;
|
||||||
|
* Otherwise a RxFrameError is returned.
|
||||||
|
*/
|
||||||
|
uint16_t getInteger(uint8_t* destination, IntegerType type,
|
||||||
|
uint8_t nrOfBytes);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getUInt32() - Get unsigned 32bit integer from the received data.
|
||||||
|
*
|
||||||
|
* @param data Memory to store unsigned 32bit integer in.
|
||||||
|
*
|
||||||
|
* @return NoError on success, an error code otherwise
|
||||||
|
*/
|
||||||
|
uint16_t getUInt32(uint32_t& data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getInt32() - Get signed 32bit integer from the received data.
|
||||||
|
*
|
||||||
|
* @param data Memory to store signed 32bit integer in.
|
||||||
|
*
|
||||||
|
* @return NoError on success, an error code otherwise
|
||||||
|
*/
|
||||||
|
uint16_t getInt32(int32_t& data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getUInt16() - Get unsigned 16bit integer from the received data.
|
||||||
|
*
|
||||||
|
* @param data Memory to store unsigned 16bit integer in.
|
||||||
|
*
|
||||||
|
* @return NoError on success, an error code otherwise
|
||||||
|
*/
|
||||||
|
uint16_t getUInt16(uint16_t& data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getInt16() - Get signed 16bit integer from the received data.
|
||||||
|
*
|
||||||
|
* @param data Memory to store signed 16bit integer in.
|
||||||
|
*
|
||||||
|
* @return NoError on success, an error code otherwise
|
||||||
|
*/
|
||||||
|
uint16_t getInt16(int16_t& data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getUInt8() - Get unsigned 8bit integer from the received data.
|
||||||
|
*
|
||||||
|
* @param data Memory to store unsigned 8bit integer in.
|
||||||
|
*
|
||||||
|
* @return NoError on success, an error code otherwise
|
||||||
|
*/
|
||||||
|
uint16_t getUInt8(uint8_t& data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getInt8() - Get signed 8bit integer from the received data.
|
||||||
|
*
|
||||||
|
* @param data Memory to store signed 8bit integer in.
|
||||||
|
*
|
||||||
|
* @return NoError on success, an error code otherwise
|
||||||
|
*/
|
||||||
|
uint16_t getInt8(int8_t& data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getBool() - Get Boolean from the received data.
|
||||||
|
*
|
||||||
|
* @param data Memory to store boolean in.
|
||||||
|
*
|
||||||
|
* @return NoError on success, an error code otherwise
|
||||||
|
*/
|
||||||
|
uint16_t getBool(bool& data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getFloat() - Get float from the received data.
|
||||||
|
*
|
||||||
|
* @param data Memory to store float in.
|
||||||
|
*
|
||||||
|
* @return NoError on success, an error code otherwise
|
||||||
|
*/
|
||||||
|
uint16_t getFloat(float& data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getBytes() - Get an array of bytes from the received data.
|
||||||
|
*
|
||||||
|
* @param data Buffer to store the bytes in.
|
||||||
|
* @param maxBytes Maximal amount of bytes to read from the received data.
|
||||||
|
*
|
||||||
|
* @return NoError on success, an error code otherwise
|
||||||
|
*/
|
||||||
|
uint16_t getBytes(uint8_t data[], size_t maxBytes);
|
||||||
|
|
||||||
|
private:
|
||||||
|
uint8_t* _buffer = 0;
|
||||||
|
size_t _bufferSize = 0;
|
||||||
|
size_t _index = 0;
|
||||||
|
size_t _numBytes = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* SENSIRION_RX_FRAME_H_ */
|
||||||
Reference in new issue
Block a user