Merge remote-tracking branch 'origin/develop' into acs-ctrl-v1
This commit is contained in:
5
thirdparty/CMakeLists.txt
vendored
Normal file
5
thirdparty/CMakeLists.txt
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
if(EIVE_ADD_LINUX_FILES)
|
||||
add_subdirectory(tas)
|
||||
endif()
|
||||
|
||||
add_subdirectory(rapidcsv)
|
9
thirdparty/tas/CMakeLists.txt
vendored
Normal file
9
thirdparty/tas/CMakeLists.txt
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
target_sources(${OBSW_NAME} PRIVATE
|
||||
hdlc.c
|
||||
uart.c
|
||||
crc.c
|
||||
)
|
||||
|
||||
target_include_directories(${OBSW_NAME} PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
195
thirdparty/tas/crc.c
vendored
Normal file
195
thirdparty/tas/crc.c
vendored
Normal file
@ -0,0 +1,195 @@
|
||||
/***************************************************************************************
|
||||
* \copyright: 2020-2022 Thales Alenia Space Deutschland GmbH
|
||||
* \project: multiMIND
|
||||
* \file: crc.c
|
||||
* \date: 22.02.2022
|
||||
* \author: David Woodward
|
||||
* \brief: CRC algorithms
|
||||
***************************************************************************************/
|
||||
|
||||
#include <stddef.h>
|
||||
#include "tas/crc.h"
|
||||
|
||||
|
||||
const uint16_t crc16_0x1021_table[256] = {
|
||||
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
|
||||
0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
|
||||
0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
|
||||
0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
|
||||
0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
|
||||
0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
|
||||
0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
|
||||
0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
|
||||
0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
|
||||
0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
|
||||
0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
|
||||
0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
|
||||
0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
|
||||
0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
|
||||
0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
|
||||
0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
|
||||
0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
|
||||
0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
|
||||
0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
|
||||
0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
|
||||
0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
|
||||
0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
|
||||
0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
|
||||
0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
|
||||
0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
|
||||
0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
|
||||
0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
|
||||
0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
|
||||
0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
|
||||
0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
|
||||
0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
|
||||
0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
|
||||
};
|
||||
|
||||
const uint16_t crc16_0x1021_table_reverse[256] =
|
||||
{
|
||||
0x0000, 0x1189, 0x2312, 0x329B, 0x4624, 0x57AD, 0x6536, 0x74BF,
|
||||
0x8C48, 0x9DC1, 0xAF5A, 0xBED3, 0xCA6C, 0xDBE5, 0xE97E, 0xF8F7,
|
||||
0x1081, 0x0108, 0x3393, 0x221A, 0x56A5, 0x472C, 0x75B7, 0x643E,
|
||||
0x9CC9, 0x8D40, 0xBFDB, 0xAE52, 0xDAED, 0xCB64, 0xF9FF, 0xE876,
|
||||
0x2102, 0x308B, 0x0210, 0x1399, 0x6726, 0x76AF, 0x4434, 0x55BD,
|
||||
0xAD4A, 0xBCC3, 0x8E58, 0x9FD1, 0xEB6E, 0xFAE7, 0xC87C, 0xD9F5,
|
||||
0x3183, 0x200A, 0x1291, 0x0318, 0x77A7, 0x662E, 0x54B5, 0x453C,
|
||||
0xBDCB, 0xAC42, 0x9ED9, 0x8F50, 0xFBEF, 0xEA66, 0xD8FD, 0xC974,
|
||||
0x4204, 0x538D, 0x6116, 0x709F, 0x0420, 0x15A9, 0x2732, 0x36BB,
|
||||
0xCE4C, 0xDFC5, 0xED5E, 0xFCD7, 0x8868, 0x99E1, 0xAB7A, 0xBAF3,
|
||||
0x5285, 0x430C, 0x7197, 0x601E, 0x14A1, 0x0528, 0x37B3, 0x263A,
|
||||
0xDECD, 0xCF44, 0xFDDF, 0xEC56, 0x98E9, 0x8960, 0xBBFB, 0xAA72,
|
||||
0x6306, 0x728F, 0x4014, 0x519D, 0x2522, 0x34AB, 0x0630, 0x17B9,
|
||||
0xEF4E, 0xFEC7, 0xCC5C, 0xDDD5, 0xA96A, 0xB8E3, 0x8A78, 0x9BF1,
|
||||
0x7387, 0x620E, 0x5095, 0x411C, 0x35A3, 0x242A, 0x16B1, 0x0738,
|
||||
0xFFCF, 0xEE46, 0xDCDD, 0xCD54, 0xB9EB, 0xA862, 0x9AF9, 0x8B70,
|
||||
0x8408, 0x9581, 0xA71A, 0xB693, 0xC22C, 0xD3A5, 0xE13E, 0xF0B7,
|
||||
0x0840, 0x19C9, 0x2B52, 0x3ADB, 0x4E64, 0x5FED, 0x6D76, 0x7CFF,
|
||||
0x9489, 0x8500, 0xB79B, 0xA612, 0xD2AD, 0xC324, 0xF1BF, 0xE036,
|
||||
0x18C1, 0x0948, 0x3BD3, 0x2A5A, 0x5EE5, 0x4F6C, 0x7DF7, 0x6C7E,
|
||||
0xA50A, 0xB483, 0x8618, 0x9791, 0xE32E, 0xF2A7, 0xC03C, 0xD1B5,
|
||||
0x2942, 0x38CB, 0x0A50, 0x1BD9, 0x6F66, 0x7EEF, 0x4C74, 0x5DFD,
|
||||
0xB58B, 0xA402, 0x9699, 0x8710, 0xF3AF, 0xE226, 0xD0BD, 0xC134,
|
||||
0x39C3, 0x284A, 0x1AD1, 0x0B58, 0x7FE7, 0x6E6E, 0x5CF5, 0x4D7C,
|
||||
0xC60C, 0xD785, 0xE51E, 0xF497, 0x8028, 0x91A1, 0xA33A, 0xB2B3,
|
||||
0x4A44, 0x5BCD, 0x6956, 0x78DF, 0x0C60, 0x1DE9, 0x2F72, 0x3EFB,
|
||||
0xD68D, 0xC704, 0xF59F, 0xE416, 0x90A9, 0x8120, 0xB3BB, 0xA232,
|
||||
0x5AC5, 0x4B4C, 0x79D7, 0x685E, 0x1CE1, 0x0D68, 0x3FF3, 0x2E7A,
|
||||
0xE70E, 0xF687, 0xC41C, 0xD595, 0xA12A, 0xB0A3, 0x8238, 0x93B1,
|
||||
0x6B46, 0x7ACF, 0x4854, 0x59DD, 0x2D62, 0x3CEB, 0x0E70, 0x1FF9,
|
||||
0xF78F, 0xE606, 0xD49D, 0xC514, 0xB1AB, 0xA022, 0x92B9, 0x8330,
|
||||
0x7BC7, 0x6A4E, 0x58D5, 0x495C, 0x3DE3, 0x2C6A, 0x1EF1, 0x0F78
|
||||
};
|
||||
|
||||
// CRC-32 calculation from original implementation (Sarthak)
|
||||
// The used algorithm is (most likely) CRC32/BZIP2, as found here:
|
||||
// https://www.cl.cam.ac.uk/research/srg/projects/fairisle/bluebook/21/crc/node6.html
|
||||
uint32_t Crc32(const uint8_t *msg, int numBytes, uint32_t remainder) {
|
||||
|
||||
int byte;
|
||||
unsigned char bit;
|
||||
|
||||
// Perform modulo-2 division, a byte at a time.
|
||||
for (byte = 0; byte < numBytes; ++byte)
|
||||
{
|
||||
// Bring the next byte into the remainder.
|
||||
remainder ^= (*(msg + byte) << 16);
|
||||
|
||||
// Perform modulo-2 division, a bit at a time.
|
||||
for (bit = 8; bit > 0; --bit) {
|
||||
|
||||
// Try to divide the current data bit.
|
||||
if (remainder & CRC32_TOPBIT) {
|
||||
remainder = (remainder << 1) ^ CRC32_POLYNOMIAL;
|
||||
}
|
||||
else {
|
||||
remainder = (remainder << 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The final remainder is the CRC result.
|
||||
return remainder;
|
||||
}
|
||||
|
||||
// ref.: CRC-16/CCITT-FALSE, alias: CRC-16/AUTOSAR
|
||||
// https://reveng.sourceforge.io/crc-catalogue/16.htm#crc.cat.crc-16-xmodem
|
||||
// initial: 0xFFFF, xorOut: 0x0000, RefIn: false, RefOut: false, polynomial: 0x1021
|
||||
uint16_t calc_crc16_unreflected(const uint8_t *data, uint32_t len, uint16_t remainder, uint16_t final_xor)
|
||||
{
|
||||
uint16_t crc = remainder;
|
||||
uint16_t temp;
|
||||
|
||||
// unreflected
|
||||
while (len-- != 0)
|
||||
{
|
||||
temp = (*data++ ^ (crc >> 8)) & 0xff;
|
||||
crc = crc16_0x1021_table[temp] ^ (crc << 8);
|
||||
}
|
||||
|
||||
crc ^= final_xor;
|
||||
|
||||
return crc;
|
||||
}
|
||||
|
||||
void calc_crc16_byte_unreflected(uint16_t *crc16, uint8_t bt)
|
||||
{
|
||||
uint16_t temp;
|
||||
temp = *crc16;
|
||||
|
||||
// unreflected
|
||||
*crc16 = crc16_0x1021_table[((temp >> 8) ^ bt) & 0xff] ^ (temp << 8);
|
||||
}
|
||||
|
||||
// initial: 0xFFFF, xorOut: 0x0000, RefIn: false, RefOut: false, polynomial: 0x1021
|
||||
uint16_t calc_crc16_buff_unreflected(uint8_t *data, uint16_t len)
|
||||
{
|
||||
uint16_t crc16 = 0xFFFF;
|
||||
|
||||
// unreflected
|
||||
while (len-- != 0)
|
||||
{
|
||||
crc16 = crc16_0x1021_table[((crc16 >> 8) ^ *data++) & 0xff] ^ (crc16 << 8);
|
||||
}
|
||||
|
||||
return crc16;
|
||||
}
|
||||
|
||||
// ref.: CRC-16/X25
|
||||
// initial: 0xFFFF, xorOut: 0xFFFF, RefIn: true, RefOut: true, polynomial: 0x1021
|
||||
uint16_t calc_crc16_reflected(const uint8_t *data, uint32_t len, uint16_t remainder, uint16_t final_xor)
|
||||
{
|
||||
uint16_t crc16 = remainder;
|
||||
|
||||
// reflected
|
||||
while (len-- != 0)
|
||||
{
|
||||
crc16 = crc16_0x1021_table_reverse[(crc16 ^ *data++) & 0xff] ^ (crc16 >> 8);
|
||||
}
|
||||
|
||||
return (crc16 ^ final_xor);
|
||||
}
|
||||
|
||||
void calc_crc16_byte_reflected(uint16_t *crc16, uint8_t bt)
|
||||
{
|
||||
uint16_t temp;
|
||||
temp = *crc16;
|
||||
|
||||
// reflected
|
||||
*crc16 = crc16_0x1021_table_reverse[(temp ^ bt) & 0xff] ^ (temp >> 8);
|
||||
}
|
||||
|
||||
// initial: 0xFFFF, xorOut: 0xFFFF, RefIn: true, RefOut: true, polynomial: 0x1021
|
||||
uint16_t calc_crc16_buff_reflected(const uint8_t *data, uint16_t len)
|
||||
{
|
||||
uint16_t crc16 = 0xFFFF;
|
||||
|
||||
// reflected
|
||||
while (len-- != 0)
|
||||
{
|
||||
crc16 = crc16_0x1021_table_reverse[(crc16 ^ *data++) & 0xff] ^ (crc16 >> 8);
|
||||
}
|
||||
|
||||
return (crc16 ^ 0xFFFF);
|
||||
}
|
90
thirdparty/tas/hdlc.c
vendored
Normal file
90
thirdparty/tas/hdlc.c
vendored
Normal file
@ -0,0 +1,90 @@
|
||||
//**************************************************************************************
|
||||
/*! \copyright: 2020-2021 Thales Alenia Space Deutschland GmbH
|
||||
* \project: multiMIND
|
||||
* \file: (name of source file: hdlc.c)
|
||||
* \date: (09.02.2022)
|
||||
* \author: (Stelios Filippopoulos)
|
||||
* \brief: (hdlc functions)
|
||||
* \language: (C)
|
||||
**************************************************************************************
|
||||
*/
|
||||
|
||||
#include "tas/hdlc.h"
|
||||
#include "tas/crc.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void hdlc_add_byte(uint8_t ch, uint8_t *buff, size_t *pos)
|
||||
{
|
||||
size_t templen = *pos;
|
||||
|
||||
if ((ch == 0x7E) ||
|
||||
(ch == 0x7D) ||
|
||||
(ch == 0x7C))
|
||||
{
|
||||
buff[templen++] = 0x7D;
|
||||
ch ^= 0x20;
|
||||
}
|
||||
buff[templen++] = ch;
|
||||
|
||||
*pos = templen;
|
||||
}
|
||||
|
||||
void hdlc_add_framing(const uint8_t *src, size_t slen, uint8_t *dst, size_t *dlen)
|
||||
{
|
||||
size_t tlen = 0;
|
||||
uint16_t ii;
|
||||
uint16_t crc16;
|
||||
uint8_t bt;
|
||||
|
||||
// calc crc16
|
||||
crc16 = calc_crc16_buff_reflected( src, slen );
|
||||
|
||||
dst[tlen++] = 0x7E;
|
||||
for (ii = 0; ii < slen; ii++)
|
||||
{
|
||||
bt = *src++;
|
||||
hdlc_add_byte(bt, dst, &tlen);
|
||||
}
|
||||
|
||||
// hdlc crc16 is in little endian format
|
||||
// WARNING: This is not portable code! Bytes need to be swapped on a big
|
||||
// endian system
|
||||
// TODO: Fix
|
||||
hdlc_add_byte((uint8_t) (crc16 & 0xFF), dst, &tlen);
|
||||
hdlc_add_byte((uint8_t) ((crc16 >> 8) & 0xFF), dst, &tlen);
|
||||
|
||||
dst[tlen++] = 0x7C;
|
||||
*dlen = tlen;
|
||||
}
|
||||
|
||||
int hdlc_remove_framing_with_crc_check(const uint8_t *src, size_t slen, uint8_t *dst, size_t *dlen)
|
||||
{
|
||||
uint16_t tlen = 0;
|
||||
uint16_t ii;
|
||||
uint8_t bt;
|
||||
|
||||
*dlen = 0;
|
||||
if (slen < 4) return -1;
|
||||
if ((src[tlen] != 0x7E) && (src[slen-1] != 0x7C)) return -2;
|
||||
src++;
|
||||
for (ii = 1; ii < slen-1; ii++)
|
||||
{
|
||||
bt = *src++;
|
||||
|
||||
if (bt == 0x7D)
|
||||
{
|
||||
bt = *src++ ^ 0x20;
|
||||
ii++;
|
||||
}
|
||||
dst[tlen++] = bt;
|
||||
}
|
||||
// calc crc16
|
||||
if(calc_crc16_buff_reflected( dst, tlen ) != 0x0f47) {
|
||||
return 1;
|
||||
}
|
||||
*dlen = tlen - 2;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
116
thirdparty/tas/tas/crc.h
vendored
Normal file
116
thirdparty/tas/tas/crc.h
vendored
Normal file
@ -0,0 +1,116 @@
|
||||
/***************************************************************************************
|
||||
* \copyright: 2020-2022 Thales Alenia Space Deutschland GmbH
|
||||
* \project: multiMIND
|
||||
* \file: crc.c
|
||||
* \date: 22.02.2022
|
||||
* \author: David Woodward
|
||||
* \brief: CRC algorithms
|
||||
***************************************************************************************/
|
||||
|
||||
#ifndef TAS_D_C_CRC_H
|
||||
#define TAS_D_C_CRC_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// NOTE: These defines are in the header as some are needed for (initial) crc function calls
|
||||
//CRC-32/BZIP2
|
||||
#define CRC32_TOPBIT (1UL<<31)
|
||||
#define CRC32_POLYNOMIAL 0x04C11DB7
|
||||
#define CRC32_INITIAL_REMAINDER 0xFFFFFFFF
|
||||
#define CRC32_FINAL_XOR_VALUE 0xFFFFFFFF
|
||||
|
||||
// CRC-16/CCITT-FALSE
|
||||
#define CRC16_INITIAL_REMAINDER 0xFFFF
|
||||
#define CRC16_FINAL_XOR_VALUE 0x0
|
||||
|
||||
extern const uint16_t crc16_0x1021_table[256];
|
||||
|
||||
extern const uint16_t crc16_0x1021_table_reverse[256];
|
||||
|
||||
/**
|
||||
* \brief CRC-32/BZIP2 algorithm
|
||||
*/
|
||||
uint32_t Crc32(const uint8_t *msg, int numBytes, uint32_t remainder);
|
||||
|
||||
|
||||
/**
|
||||
* \brief CRC-16/CCITT-FALSE (alias CRC-16/AUTOSAR) algorithm,
|
||||
// initial: 0xFFFF, xorOut: 0x0000, RefIn: false, RefOut: false, polynomial: 0x1021
|
||||
* using a lookup table
|
||||
* \param data Data
|
||||
* \param len Data length
|
||||
* \param remainder Remainder to be used,
|
||||
* use initial remainder for non coherent/standalone calculations
|
||||
* \param final_xor The value that the final result will be xored
|
||||
* \return CRC result
|
||||
*/
|
||||
uint16_t calc_crc16_unreflected(const uint8_t *data, uint32_t len, uint16_t remainder, uint16_t final_xor);
|
||||
|
||||
/**
|
||||
* generates a 16-bit CRC for the said data
|
||||
*
|
||||
* @param data input data for CRC
|
||||
* @param len length of the data
|
||||
* @return crc Generated 16-bit CRC
|
||||
*/
|
||||
void calc_crc16_byte_unreflected(uint16_t *crc16, uint8_t bt);
|
||||
|
||||
/**
|
||||
* \brief CRC-16/CCITT-FALSE (alias CRC-16/AUTOSAR) algorithm,
|
||||
* polynomial: 0x1021, initial: 0xFFFF, final xor: 0x0,
|
||||
* using a lookup table
|
||||
* \param data Data
|
||||
* \param len Data length
|
||||
* \param remainder Remainder to be used,
|
||||
* use initial remainder for non coherent/standalone calculations
|
||||
* \param final_xor The value that the final result will be xored
|
||||
* \return CRC result
|
||||
*/
|
||||
uint16_t calc_crc16_buff_unreflected(uint8_t *data, uint16_t len);
|
||||
|
||||
/**
|
||||
* \brief CRC-16/X25 algorithm,
|
||||
* initial: 0xFFFF, xorOut: 0xFFFF, RefIn: true, RefOut: true, polynomial: 0x1021
|
||||
* using a lookup table
|
||||
* \param data Data
|
||||
* \param len Data length
|
||||
* \param remainder Remainder to be used,
|
||||
* use initial remainder for non coherent/standalone calculations
|
||||
* \param final_xor The value that the final result will be xored
|
||||
* \return CRC result
|
||||
*/
|
||||
uint16_t calc_crc16_reflected(const uint8_t *data, uint32_t len, uint16_t remainder, uint16_t final_xor);
|
||||
|
||||
/**
|
||||
* \brief CRC-16/X25 algorithm,
|
||||
* calculates the crc16 for the next byte, given an already calculated crc16
|
||||
*
|
||||
* @param *crc16 : calculated crc16 - the value will be updated
|
||||
* @param bt : next byte for crc16 calculation
|
||||
* @return none
|
||||
*/
|
||||
void calc_crc16_byte_reflected(uint16_t *crc16, uint8_t bt);
|
||||
|
||||
/**
|
||||
* \brief CRC-16/X25 algorithm,
|
||||
* initial: 0xFFFF, xorOut: 0xFFFF, RefIn: true, RefOut: true, polynomial: 0x1021
|
||||
* using a lookup table
|
||||
* \param data Data
|
||||
* \param len Data length
|
||||
* \param remainder Remainder to be used,
|
||||
* use initial remainder for non coherent/standalone calculations
|
||||
* \param final_xor The value that the final result will be xored
|
||||
* \return CRC result
|
||||
*/
|
||||
uint16_t calc_crc16_buff_reflected(const uint8_t *data, uint16_t len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
52
thirdparty/tas/tas/hdlc.h
vendored
Normal file
52
thirdparty/tas/tas/hdlc.h
vendored
Normal file
@ -0,0 +1,52 @@
|
||||
//**************************************************************************************
|
||||
/*! \copyright: 2020-2021 Thales Alenia Space Deutschland GmbH
|
||||
* \project: multiMIND
|
||||
* \file: (name of source file: hdlc.h)
|
||||
* \date: (09.02.2022)
|
||||
* \author: (Stelios Filippopoulos)
|
||||
* \brief: (hdlc header file)
|
||||
* \language: (C)
|
||||
**************************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef LIB_HDLC_H_
|
||||
#define LIB_HDLC_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define HDLC_ENABLE
|
||||
|
||||
#define HDLC_START_BYTE (0x7Eu)
|
||||
#define HDLC_ESC_BYTE (0x7Du)
|
||||
#define HDLC_END_BYTE (0x7Cu)
|
||||
#define HDLC_ESCAPE_CHAR (0x20u)
|
||||
|
||||
void hdlc_add_byte(uint8_t ch, uint8_t *buff, size_t *pos);
|
||||
|
||||
void hdlc_add_framing(const uint8_t *src, size_t slen, uint8_t *dst, size_t *dlen);
|
||||
|
||||
/**
|
||||
* Decode a HDLC frame, including CRC check and CRC removal in addition
|
||||
* to the removal of the frame markers.
|
||||
* @param src
|
||||
* @param slen
|
||||
* @param dst
|
||||
* @param dlen
|
||||
* @return
|
||||
* -1 Invalid source length
|
||||
* -2 No start marker at first byte or end marker at slen - 1
|
||||
* 1 Invalid CRC
|
||||
* 0 CRC OK, framing and CRC removed
|
||||
*/
|
||||
int hdlc_remove_framing_with_crc_check(const uint8_t *src, size_t slen, uint8_t *dst, size_t *dlen);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* LIB_HDLC_H_ */
|
124
thirdparty/tas/tas/uart.h
vendored
Normal file
124
thirdparty/tas/tas/uart.h
vendored
Normal file
@ -0,0 +1,124 @@
|
||||
//**************************************************************************************
|
||||
/*! \copyright: 2020-2021 Thales Alenia Space Deutschland GmbH
|
||||
* \project: multiMIND
|
||||
* \file: (name of source file: uart.h)
|
||||
* \date: (20.05.2021)
|
||||
* \author: (Sarthak Kelapure)
|
||||
* \brief: (UART thread to collect data on serial interface)
|
||||
* \language: (C)
|
||||
**************************************************************************************
|
||||
*/
|
||||
#ifndef LIB_UART_H
|
||||
#define LIB_UART_H
|
||||
|
||||
#define BUFF_SIZE 512
|
||||
#define POLL_TIMEOUT 2000
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <termios.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <poll.h>
|
||||
#include <errno.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct serial_s serial_t;
|
||||
|
||||
/**
|
||||
* Destroy the serial structure
|
||||
*/
|
||||
void uart_destroy(serial_t* s);
|
||||
|
||||
/**
|
||||
* Initializes the serial connection
|
||||
* @param device - serial device name.
|
||||
* @param baud - baud rate for connection.
|
||||
* @return serial structure.
|
||||
*/
|
||||
serial_t* uart_init(char device[], int baud);
|
||||
|
||||
/**
|
||||
* Send data.
|
||||
* @param s - serial structure.
|
||||
* @param data - character array to transmit.
|
||||
* @param length - size of the data array.
|
||||
*/
|
||||
uint32_t uart_length_send(serial_t* s, uint8_t data[], int length);
|
||||
|
||||
/**
|
||||
* Send a single character.
|
||||
* @param s - serial structure.
|
||||
* @param data - single character to be sent.
|
||||
*/
|
||||
void uart_send(serial_t* s, uint8_t data);
|
||||
|
||||
/**
|
||||
* Determine how much data is available
|
||||
* in the serial buffer.
|
||||
* @param s - serial structure.
|
||||
* @return number of characters available.
|
||||
*/
|
||||
int uart_available(serial_t* s);
|
||||
|
||||
/**
|
||||
* Fetch one char from the serial buffer.
|
||||
* @param s - serial structure.
|
||||
* @return character. Null if empty.
|
||||
*/
|
||||
char uart_get(serial_t* s);
|
||||
|
||||
/**
|
||||
* Fetch length of chars from the serial buffer.
|
||||
* @param s - serial structure.
|
||||
* @param buff - readback storage
|
||||
* @param len - length to get
|
||||
* @return length. zero if empty.
|
||||
*/
|
||||
int uart_length_get(serial_t* s, char* buff, int len, bool start_of_packet);
|
||||
|
||||
uint16_t uart_get_hdlc_packet(serial_t* s, uint8_t *buff, uint16_t buff_len);
|
||||
|
||||
/**
|
||||
* Fetch one char from the serial buffer.
|
||||
* Blocks until data becomes available.
|
||||
* @param s - serial structure.
|
||||
* @return character.
|
||||
*/
|
||||
char uart_blocking_get(serial_t* s);
|
||||
|
||||
/**
|
||||
* Clear the serial buffer.
|
||||
* @param s - serial structure.
|
||||
*/
|
||||
void uart_clear(serial_t* s);
|
||||
|
||||
/**
|
||||
* Close the serial port.
|
||||
* @param s - serial structure.
|
||||
* @return value of close().
|
||||
*/
|
||||
int uart_close(serial_t* s);
|
||||
|
||||
/**
|
||||
* Deinitializes the UART
|
||||
* @param s - serial structure.
|
||||
*/
|
||||
void uart_deinit(serial_t* s);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif //LIB_UART_H
|
603
thirdparty/tas/uart.c
vendored
Normal file
603
thirdparty/tas/uart.c
vendored
Normal file
@ -0,0 +1,603 @@
|
||||
//**************************************************************************************
|
||||
/*! \copyright: 2020-2021 Thales Alenia Space Deutschland GmbH
|
||||
* \project: multiMIND
|
||||
* \file: (name of source file: uart.c)
|
||||
* \date: (20.05.2021)
|
||||
* \author: (Sarthak Kelapure)
|
||||
* \brief: (UART thread to collect data on serial interface)
|
||||
* \language: (C)
|
||||
**************************************************************************************
|
||||
*/
|
||||
|
||||
#include "tas/uart.h"
|
||||
#include "tas/hdlc.h"
|
||||
|
||||
#ifdef HDLC_ENABLE
|
||||
#define HDLC_RX_STATE_IDLE (0u)
|
||||
#define HDLC_RX_STATE_RECEIVING (1u)
|
||||
#define HDLC_RX_STATE_ESCAPE (2u)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @struct Serial device structure.
|
||||
* Encapsulates a serial connection.
|
||||
*/
|
||||
struct serial_s {
|
||||
int fd; //>! Connection file descriptor.
|
||||
int state; //>! Signifies connection state.
|
||||
int running; //>! Signifies thread state.
|
||||
|
||||
char rxbuff[BUFF_SIZE]; //>! Buffer for RX data.
|
||||
int start, end; //>! Pointers to start and end of buffer.
|
||||
|
||||
pthread_t rx_thread; //>! Listening thread.
|
||||
};
|
||||
|
||||
// --------------- Internal Functions ---------------
|
||||
|
||||
/**
|
||||
* Connect to a serial device.
|
||||
* @param s - serial structure.
|
||||
* @param device - serial device name.
|
||||
* @param baud - baud rate for connection.
|
||||
* @return -ve on error, 0 on success.
|
||||
*/
|
||||
static int serial_connect(serial_t* s, char device[], int baud);
|
||||
|
||||
/**
|
||||
* Create the serial structure.
|
||||
* Convenience method to allocate memory
|
||||
* and instantiate objects.
|
||||
* @return serial structure.
|
||||
*/
|
||||
static serial_t* serial_create();
|
||||
|
||||
static int serial_resolve_baud(int baud);
|
||||
|
||||
/**
|
||||
* Recieve data.
|
||||
* Retrieves data from the serial device.
|
||||
* @param s - serial structure.
|
||||
* @param data - pointer to a buffer to read data into.
|
||||
* @param maxLength - size of input buffer.
|
||||
* @return amount of data recieved.
|
||||
*/
|
||||
static int serial_recieve(serial_t* obj, uint8_t data[], int maxLength);
|
||||
|
||||
/**
|
||||
* @brief Serial Listener Thread.
|
||||
* This blocks waiting for data to be recieved from the serial device,
|
||||
* and calls the serial_callback method with appropriate context when
|
||||
* data is recieved.
|
||||
* Exits when close method is called, or serial error occurs.
|
||||
* @param param - context passed from thread instantiation.
|
||||
*/
|
||||
static void *serial_data_listener(void *param);
|
||||
|
||||
/**
|
||||
* @brief Start the serial threads.
|
||||
* This spawns the listening and transmitting threads
|
||||
* if they are not already running.
|
||||
* @param s - serial structure.
|
||||
* @return 0 on success, or -1 on error.
|
||||
*/
|
||||
static int serial_start(serial_t* s);
|
||||
|
||||
/**
|
||||
* Stop serial listener thread.
|
||||
* @param s - serial structure.
|
||||
* @return 0;
|
||||
*/
|
||||
static int serial_stop(serial_t* s);
|
||||
|
||||
/**
|
||||
* Callback to handle recieved data.
|
||||
* Puts recieved data into the rx buffer.
|
||||
* @param s - serial structure.
|
||||
* @param data - data to be stored.
|
||||
* @param length - length of recieved data.
|
||||
*/
|
||||
static void serial_rx_callback(serial_t* s, char data[], int length);
|
||||
|
||||
// Put character in rx buffer.
|
||||
static int buffer_put(serial_t* s, char c)
|
||||
{
|
||||
//if there is space in the buffer
|
||||
if ( s->end != ((s->start + BUFF_SIZE - 1) % BUFF_SIZE)) {
|
||||
s->rxbuff[s->end] = c;
|
||||
s->end ++;
|
||||
s->end = s->end % BUFF_SIZE;
|
||||
//printf("Put: %x start: %d, end: %d\r\n", c, s->start, s->end);
|
||||
return 0; //No error
|
||||
} else {
|
||||
//buffer is full, this is a bad state
|
||||
return -1; //Report error
|
||||
}
|
||||
}
|
||||
|
||||
// Get character from rx buffer.
|
||||
static char buffer_get(serial_t* s)
|
||||
{
|
||||
char c = (char)0;
|
||||
//if there is data to process
|
||||
if (s->end != s->start) {
|
||||
c = (s->rxbuff[s->start]);
|
||||
s->start ++;
|
||||
//wrap around
|
||||
s->start = s->start % BUFF_SIZE;
|
||||
} else {
|
||||
}
|
||||
//printf("Get: %x start: %d, end: %d\r\n", c, s->start, s->end);
|
||||
return c;
|
||||
}
|
||||
|
||||
//Get data available in the rx buffer.
|
||||
static int buffer_available(serial_t* s)
|
||||
{
|
||||
return (s->end - s->start + BUFF_SIZE) % BUFF_SIZE;
|
||||
}
|
||||
|
||||
// --------------- External Functions ---------------
|
||||
|
||||
//Create serial object.
|
||||
serial_t* serial_create()
|
||||
{
|
||||
//Allocate serial object.
|
||||
serial_t* s = malloc(sizeof(serial_t));
|
||||
//Reconfigure buffer object.
|
||||
s->start = 0;
|
||||
s->end = 0;
|
||||
//Return pointer.
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
void uart_destroy(serial_t* s)
|
||||
{
|
||||
free(s);
|
||||
}
|
||||
|
||||
|
||||
//Connect to serial device.
|
||||
int serial_connect(serial_t* s, char device[], int baud)
|
||||
{
|
||||
struct termios oldtio;
|
||||
|
||||
// Resolve baud.
|
||||
int speed = serial_resolve_baud(baud);
|
||||
if (speed < 0) {
|
||||
printf("Error: Baud rate not recognized.\r\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//Open device.
|
||||
s->fd = open(device, O_RDWR | O_NOCTTY);
|
||||
//Catch file open error.
|
||||
if (s->fd < 0) {
|
||||
perror(device);
|
||||
return -2;
|
||||
}
|
||||
//Retrieve settings.
|
||||
tcgetattr(s->fd, &oldtio);
|
||||
//Set baud rate.
|
||||
cfsetspeed(&oldtio, speed);
|
||||
//Flush cache.
|
||||
tcflush(s->fd, TCIFLUSH);
|
||||
|
||||
//Set UART settings, standard ones. 8N1
|
||||
oldtio.c_cflag = (oldtio.c_cflag & ~CSIZE) | CS8; // 8-bit chars
|
||||
// disable IGNBRK for mismatched speed tests; otherwise receive break
|
||||
// as \000 chars
|
||||
oldtio.c_iflag &= ~IGNBRK; // disable break processing
|
||||
oldtio.c_lflag = 0; // no signaling chars, no echo,
|
||||
// no canonical processing
|
||||
oldtio.c_oflag = 0; // no remapping, no delays
|
||||
oldtio.c_cc[VMIN] = 0; // read doesn't block
|
||||
oldtio.c_cc[VTIME] = 5; // 0.5 seconds read timeout
|
||||
|
||||
oldtio.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
|
||||
oldtio.c_iflag &= ~(IGNCR | ICRNL | INLCR); // CR and LF characters are not affected
|
||||
|
||||
oldtio.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
|
||||
// enable reading
|
||||
oldtio.c_cflag &= ~(PARENB | PARODD); // shut off parity
|
||||
oldtio.c_cflag |= 0;
|
||||
oldtio.c_cflag &= ~CSTOPB;
|
||||
oldtio.c_cflag &= ~(020000000000);
|
||||
//Apply settings.
|
||||
if(tcsetattr(s->fd, TCSANOW, &oldtio) !=0){
|
||||
printf("ERROR: serial settings failed\r\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//Start listener thread.
|
||||
int res = serial_start(s);
|
||||
//Catch error.
|
||||
if (res < 0) {
|
||||
printf("Error: serial thread could not be spawned\r\n");
|
||||
return -3;
|
||||
}
|
||||
|
||||
//Indicate connection was successful.
|
||||
s->state = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
serial_t* uart_init(char device[], int baud)
|
||||
{
|
||||
serial_t* s = serial_create();
|
||||
if(serial_connect(s, device, baud)< 0)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
//Send data.
|
||||
uint32_t uart_length_send(serial_t* s, uint8_t data[], int length)
|
||||
{
|
||||
// uint16_t ii;
|
||||
// int res;
|
||||
// for (ii = 0; ii < length; ii++)
|
||||
// {
|
||||
// res = write(s->fd, &data[ii], 1);
|
||||
// }
|
||||
int res = write(s->fd, data, length);
|
||||
return res;
|
||||
}
|
||||
|
||||
void uart_send(serial_t* s, uint8_t data)
|
||||
{
|
||||
char arr[1];
|
||||
arr[0] = data;
|
||||
write(s->fd, arr, 1);
|
||||
}
|
||||
|
||||
//Determine characters available.
|
||||
int uart_available(serial_t* s)
|
||||
{
|
||||
return buffer_available(s);
|
||||
}
|
||||
|
||||
//Fetch a character.
|
||||
char uart_get(serial_t* s)
|
||||
{
|
||||
char c = buffer_get(s);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
int uart_length_get(serial_t* s, char* buff, int len, bool start_of_packet)
|
||||
{
|
||||
int ret = 0;
|
||||
if (len > 0 && len < BUFF_SIZE)
|
||||
{
|
||||
#ifdef HDLC_ENABLE
|
||||
uint8_t ch;
|
||||
uint8_t hdlc_rx_state;
|
||||
int rxb = 0;
|
||||
|
||||
if (start_of_packet)
|
||||
hdlc_rx_state = HDLC_RX_STATE_IDLE;
|
||||
else
|
||||
hdlc_rx_state = HDLC_RX_STATE_RECEIVING;
|
||||
|
||||
while (rxb < len)
|
||||
{
|
||||
ch = uart_blocking_get(s);
|
||||
|
||||
switch (hdlc_rx_state)
|
||||
{
|
||||
case HDLC_RX_STATE_IDLE:
|
||||
if (ch == HDLC_START_BYTE)
|
||||
{
|
||||
rxb = 0;
|
||||
ret = 0;
|
||||
hdlc_rx_state = HDLC_RX_STATE_RECEIVING;
|
||||
}
|
||||
break;
|
||||
case HDLC_RX_STATE_RECEIVING:
|
||||
if (ch == HDLC_START_BYTE)
|
||||
{
|
||||
rxb = 0;
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
if (ch == HDLC_ESC_BYTE)
|
||||
{
|
||||
hdlc_rx_state = HDLC_RX_STATE_ESCAPE;
|
||||
break;
|
||||
}
|
||||
buff[rxb++] = ch;
|
||||
ret++;
|
||||
break;
|
||||
case HDLC_RX_STATE_ESCAPE:
|
||||
if (ch == HDLC_START_BYTE)
|
||||
{
|
||||
rxb = 0;
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
buff[rxb++] = ch ^ HDLC_ESCAPE_CHAR;
|
||||
ret++;
|
||||
hdlc_rx_state = HDLC_RX_STATE_RECEIVING;
|
||||
break;
|
||||
}
|
||||
}
|
||||
#else
|
||||
for (int i=0;i<len;i++)
|
||||
{
|
||||
buff[i] = uart_blocking_get(s);
|
||||
ret++;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint16_t uart_get_hdlc_packet(serial_t* s, uint8_t *buff, uint16_t buff_len)
|
||||
{
|
||||
uint8_t hdlc_rx_state = HDLC_RX_STATE_IDLE;
|
||||
uint8_t ch;
|
||||
uint16_t buff_pos = 0u;
|
||||
|
||||
while (1)
|
||||
{
|
||||
ch = uart_blocking_get(s);
|
||||
|
||||
switch (hdlc_rx_state)
|
||||
{
|
||||
case HDLC_RX_STATE_IDLE:
|
||||
if (ch == HDLC_START_BYTE)
|
||||
{
|
||||
buff_pos = 0u;
|
||||
hdlc_rx_state = HDLC_RX_STATE_RECEIVING;
|
||||
}
|
||||
break;
|
||||
|
||||
case HDLC_RX_STATE_RECEIVING:
|
||||
if (ch == HDLC_START_BYTE)
|
||||
{
|
||||
buff_pos = 0u;
|
||||
break;
|
||||
}
|
||||
if (ch == HDLC_END_BYTE)
|
||||
{
|
||||
if (buff_pos > 2u) // do not include HDLC CRC16
|
||||
{
|
||||
return buff_pos;
|
||||
}
|
||||
buff_pos = 0u;
|
||||
hdlc_rx_state = HDLC_RX_STATE_IDLE;
|
||||
break;
|
||||
}
|
||||
if (ch == HDLC_ESC_BYTE)
|
||||
{
|
||||
hdlc_rx_state = HDLC_RX_STATE_ESCAPE;
|
||||
break;
|
||||
}
|
||||
if (buff_pos >= buff_len)
|
||||
{
|
||||
hdlc_rx_state = HDLC_RX_STATE_RECEIVING;
|
||||
break;
|
||||
}
|
||||
buff[buff_pos++] = ch;
|
||||
break;
|
||||
|
||||
case HDLC_RX_STATE_ESCAPE:
|
||||
if ((ch == HDLC_START_BYTE) || (ch == HDLC_END_BYTE))
|
||||
{
|
||||
buff_pos = 0;
|
||||
hdlc_rx_state = HDLC_RX_STATE_RECEIVING;
|
||||
break;
|
||||
}
|
||||
if (buff_pos >= buff_len)
|
||||
{
|
||||
hdlc_rx_state = HDLC_RX_STATE_RECEIVING;
|
||||
break;
|
||||
}
|
||||
buff[buff_pos++] = ch ^ HDLC_ESCAPE_CHAR;
|
||||
hdlc_rx_state = HDLC_RX_STATE_RECEIVING;
|
||||
break;
|
||||
|
||||
default:
|
||||
buff_pos = 0u;
|
||||
hdlc_rx_state = HDLC_RX_STATE_IDLE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
char uart_blocking_get(serial_t* s)
|
||||
{
|
||||
while (uart_available(s) == 0);
|
||||
return uart_get(s);
|
||||
}
|
||||
|
||||
void uart_clear(serial_t* s)
|
||||
{
|
||||
//Clear the buffer.
|
||||
while (buffer_available(s)) {
|
||||
buffer_get(s);
|
||||
}
|
||||
tcflush(s->fd, TCIFLUSH);
|
||||
}
|
||||
|
||||
//Close serial port.
|
||||
int uart_close(serial_t* s)
|
||||
{
|
||||
//Stop thread.
|
||||
serial_stop(s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void uart_deinit(serial_t* s){
|
||||
uart_clear(s);
|
||||
uart_close(s);
|
||||
uart_destroy(s);
|
||||
}
|
||||
|
||||
// --------------- Internal Functions --------------
|
||||
|
||||
//Stop serial listener thread.
|
||||
static int serial_stop(serial_t* s)
|
||||
{
|
||||
s->running = 0;
|
||||
return close(s->fd);
|
||||
}
|
||||
|
||||
// Resolves standard baud rates to linux constants.
|
||||
static int serial_resolve_baud(int baud)
|
||||
{
|
||||
int speed;
|
||||
// Switch common baud rates to temios constants.
|
||||
switch (baud) {
|
||||
case 9600:
|
||||
speed = B9600;
|
||||
break;
|
||||
case 19200:
|
||||
speed = B19200;
|
||||
break;
|
||||
case 38400:
|
||||
speed = B38400;
|
||||
break;
|
||||
case 57600:
|
||||
speed = B57600;
|
||||
break;
|
||||
case 115200:
|
||||
speed = B115200;
|
||||
break;
|
||||
case 230400:
|
||||
speed = B230400;
|
||||
break;
|
||||
case 460800:
|
||||
speed = B460800;
|
||||
break;
|
||||
case 500000:
|
||||
speed = B500000;
|
||||
break;
|
||||
case 576000:
|
||||
speed = B576000;
|
||||
break;
|
||||
case 921600:
|
||||
speed = B921600;
|
||||
break;
|
||||
case 1000000:
|
||||
speed = B1000000;
|
||||
break;
|
||||
case 1152000:
|
||||
speed = B1152000;
|
||||
break;
|
||||
case 1500000:
|
||||
speed = B1500000;
|
||||
break;
|
||||
case 2000000:
|
||||
speed = B2000000;
|
||||
break;
|
||||
case 3000000:
|
||||
speed = B3000000;
|
||||
break;
|
||||
default:
|
||||
speed = -1;
|
||||
break;
|
||||
}
|
||||
// Return.
|
||||
return speed;
|
||||
}
|
||||
|
||||
// Start serial listener.
|
||||
static int serial_start(serial_t* s)
|
||||
{
|
||||
//Only start if it is not currently running.
|
||||
if (s->running != 1) {
|
||||
//Set running.
|
||||
s->running = 1;
|
||||
//Spawn thread.
|
||||
int res;
|
||||
res = pthread_create(&s->rx_thread, NULL, serial_data_listener, (void*) s);
|
||||
if (res != 0) {
|
||||
return -2;
|
||||
}
|
||||
//Return result.
|
||||
return 0;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Recieve data.
|
||||
static int serial_recieve(serial_t* s, uint8_t data[], int maxLength)
|
||||
{
|
||||
return read(s->fd, data, maxLength);
|
||||
}
|
||||
|
||||
//Callback to store data in buffer.
|
||||
static void serial_rx_callback(serial_t* s, char data[], int length)
|
||||
{
|
||||
//Put data into buffer.
|
||||
int i;
|
||||
//Put data into buffer.
|
||||
for (i = 0; i < length; i++) {
|
||||
buffer_put(s, data[i]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Serial data listener thread.
|
||||
static void *serial_data_listener(void *param)
|
||||
{
|
||||
int res = 0;
|
||||
int err = 0;
|
||||
struct pollfd ufds;
|
||||
uint8_t buff[BUFF_SIZE];
|
||||
|
||||
//Retrieve paramaters and store locally.
|
||||
serial_t* serial = (serial_t*) param;
|
||||
int fd = serial->fd;
|
||||
|
||||
//Set up poll file descriptors.
|
||||
ufds.fd = fd; //Attach socket to watch.
|
||||
ufds.events = POLLIN; //Set events to notify on.
|
||||
|
||||
//Run until ended.
|
||||
while (serial->running != 0) {
|
||||
//Poll socket for data.
|
||||
res = poll(&ufds, 1, POLL_TIMEOUT);
|
||||
//If data was recieved.
|
||||
if (res > 0) {
|
||||
//Fetch the data.
|
||||
int count = serial_recieve(serial, buff, BUFF_SIZE - 1);
|
||||
//If data was recieved.
|
||||
if (count > 0) {
|
||||
//Pad end of buffer to ensure there is a termination symbol.
|
||||
buff[count] = '\0';
|
||||
// Call the serial callback.
|
||||
serial_rx_callback(serial, (char *)buff, count);
|
||||
//If an error occured.
|
||||
} else if (count < 0) {
|
||||
//Inform user and exit thread.
|
||||
printf("Error: Serial disconnect\r\n");
|
||||
err = 1;
|
||||
break;
|
||||
}
|
||||
//If there was an error.
|
||||
} else if (res < 0) {
|
||||
//Inform user and exit thread.
|
||||
printf("Error: Polling error in serial thread");
|
||||
err = 1;
|
||||
break;
|
||||
}
|
||||
//Otherwise, keep going around.
|
||||
}
|
||||
//If there was an error, close socket.
|
||||
if (err) {
|
||||
uart_close(serial);
|
||||
//raise(SIGLOST);
|
||||
}
|
||||
//Close file.
|
||||
res = close(serial->fd);
|
||||
|
||||
return NULL;
|
||||
}
|
Reference in New Issue
Block a user