From 0c998363fb8b710ac2d6a9a20b90784707911e15 Mon Sep 17 00:00:00 2001 From: Florian Pose Date: Thu, 9 Oct 2008 13:24:17 +0000 Subject: [PATCH] Moved byteorder macros to ecrt.h. --- include/ecrt.h | 44 ++++++++++++++++++++++++++++++++++++++-- tool/CommandAlias.cpp | 3 +-- tool/CommandConfig.cpp | 5 ++--- tool/CommandDownload.cpp | 9 ++++---- tool/CommandPhyWrite.cpp | 1 - tool/CommandSiiRead.cpp | 7 +++---- tool/CommandSiiWrite.cpp | 7 +++---- tool/CommandUpload.cpp | 9 ++++---- tool/Makefile.am | 1 - tool/byteorder.h | 40 ------------------------------------ 10 files changed, 59 insertions(+), 67 deletions(-) delete mode 100644 tool/byteorder.h diff --git a/include/ecrt.h b/include/ecrt.h index 7556be25..542723a7 100644 --- a/include/ecrt.h +++ b/include/ecrt.h @@ -99,11 +99,11 @@ #ifndef __ECRT_H__ #define __ECRT_H__ -#include - #ifdef __KERNEL__ +#include #include #else +#include // for size_t #include #endif @@ -1057,6 +1057,46 @@ ec_request_state_t ecrt_voe_handler_execute( else *((uint8_t *) (DATA)) &= ~(1 << (POS)); \ } while (0) +/****************************************************************************** + * Byte-swapping functions for user space + *****************************************************************************/ + +#ifndef __KERNEL__ + +#if __BYTE_ORDER == __LITTLE_ENDIAN + +#define le16_to_cpu(x) x +#define le32_to_cpu(x) x + +#define cpu_to_le16(x) x +#define cpu_to_le32(x) x + +#elif __BYTE_ORDER == __BIG_ENDIAN + +#define swap16(x) \ + ((uint16_t)( \ + (((uint16_t)(x) & 0x00ffU) << 8) | \ + (((uint16_t)(x) & 0xff00U) >> 8) )) +#define swap32(x) \ + ((uint32_t)( \ + (((uint32_t)(x) & 0x000000ffUL) << 24) | \ + (((uint32_t)(x) & 0x0000ff00UL) << 8) | \ + (((uint32_t)(x) & 0x00ff0000UL) >> 8) | \ + (((uint32_t)(x) & 0xff000000UL) >> 24) )) + +#define le16_to_cpu(x) swap16(x) +#define le32_to_cpu(x) swap32(x) + +#define cpu_to_le16(x) swap16(x) +#define cpu_to_le32(x) swap32(x) + +#endif + +#define le16_to_cpup(x) le16_to_cpu(*((uint16_t *)(x))) +#define le32_to_cpup(x) le32_to_cpu(*((uint32_t *)(x))) + +#endif /* ifndef __KERNEL__ */ + /****************************************************************************** * Read macros *****************************************************************************/ diff --git a/tool/CommandAlias.cpp b/tool/CommandAlias.cpp index 2c0de921..e749e56d 100644 --- a/tool/CommandAlias.cpp +++ b/tool/CommandAlias.cpp @@ -11,7 +11,6 @@ using namespace std; #include "CommandAlias.h" #include "sii_crc.h" -#include "byteorder.h" /*****************************************************************************/ @@ -130,7 +129,7 @@ void CommandAlias::writeSlaveAlias( } // write new alias address in word 4 - data.words[4] = cputole16(alias); + data.words[4] = cpu_to_le16(alias); // calculate checksum over words 0 to 6 crc = calcSiiCrc((const uint8_t *) data.words, 14); diff --git a/tool/CommandConfig.cpp b/tool/CommandConfig.cpp index ed4747e8..4c7772d4 100644 --- a/tool/CommandConfig.cpp +++ b/tool/CommandConfig.cpp @@ -11,7 +11,6 @@ using namespace std; #include "CommandConfig.h" -#include "byteorder.h" /*****************************************************************************/ @@ -174,11 +173,11 @@ void CommandConfig::showDetailedConfigs( break; case 2: cout << "0x" << setw(4) - << le16tocpu(*(uint16_t *) &sdo.data); + << le16_to_cpup(&sdo.data); break; case 4: cout << "0x" << setw(8) - << le32tocpu(*(uint32_t *) &sdo.data); + << le32_to_cpup(&sdo.data); break; default: cout << "???"; diff --git a/tool/CommandDownload.cpp b/tool/CommandDownload.cpp index 718bddc4..3925735b 100644 --- a/tool/CommandDownload.cpp +++ b/tool/CommandDownload.cpp @@ -9,7 +9,6 @@ using namespace std; #include "CommandDownload.h" -#include "byteorder.h" /*****************************************************************************/ @@ -150,14 +149,14 @@ void CommandDownload::execute(MasterDevice &m, const StringVector &args) { int16_t val; strValue >> val; - *(int16_t *) data.data = cputole16(val); + *(int16_t *) data.data = cpu_to_le16(val); break; } case 0x0004: // int32 { int32_t val; strValue >> val; - *(int32_t *) data.data = cputole32(val); + *(int32_t *) data.data = cpu_to_le32(val); break; } case 0x0005: // uint8 @@ -173,14 +172,14 @@ void CommandDownload::execute(MasterDevice &m, const StringVector &args) { uint16_t val; strValue >> val; - *(uint16_t *) data.data = cputole16(val); + *(uint16_t *) data.data = cpu_to_le16(val); break; } case 0x0007: // uint32 { uint32_t val; strValue >> val; - *(uint32_t *) data.data = cputole32(val); + *(uint32_t *) data.data = cpu_to_le32(val); break; } case 0x0009: // string diff --git a/tool/CommandPhyWrite.cpp b/tool/CommandPhyWrite.cpp index c07b54f1..6b4b9d0a 100644 --- a/tool/CommandPhyWrite.cpp +++ b/tool/CommandPhyWrite.cpp @@ -11,7 +11,6 @@ using namespace std; #include "CommandPhyWrite.h" #include "sii_crc.h" -#include "byteorder.h" /*****************************************************************************/ diff --git a/tool/CommandSiiRead.cpp b/tool/CommandSiiRead.cpp index 9985fa8a..430ec83f 100644 --- a/tool/CommandSiiRead.cpp +++ b/tool/CommandSiiRead.cpp @@ -9,7 +9,6 @@ using namespace std; #include "CommandSiiRead.h" -#include "byteorder.h" /*****************************************************************************/ @@ -99,7 +98,7 @@ void CommandSiiRead::execute(MasterDevice &m, const StringVector &args) if (data.nwords > 0x0040U) { // cycle through categories categoryHeader = data.words + 0x0040U; - categoryType = le16tocpu(*categoryHeader); + categoryType = le16_to_cpup(categoryHeader); while (categoryType != 0xffff) { cout << "SII Category 0x" << hex << setw(4) << categoryType @@ -109,7 +108,7 @@ void CommandSiiRead::execute(MasterDevice &m, const StringVector &args) err << "SII data seem to be corrupted!"; throwCommandException(err); } - categorySize = le16tocpu(*(categoryHeader + 1)); + categorySize = le16_to_cpup(categoryHeader + 1); cout << ", " << dec << categorySize << " words" << flush; if (categoryHeader + 2 + categorySize @@ -136,7 +135,7 @@ void CommandSiiRead::execute(MasterDevice &m, const StringVector &args) throwCommandException(err); } categoryHeader += 2 + categorySize; - categoryType = le16tocpu(*categoryHeader); + categoryType = le16_to_cpup(categoryHeader); } } } else { diff --git a/tool/CommandSiiWrite.cpp b/tool/CommandSiiWrite.cpp index fc34d8d6..64827943 100644 --- a/tool/CommandSiiWrite.cpp +++ b/tool/CommandSiiWrite.cpp @@ -11,7 +11,6 @@ using namespace std; #include "CommandSiiWrite.h" #include "sii_crc.h" -#include "byteorder.h" /*****************************************************************************/ @@ -173,14 +172,14 @@ void CommandSiiWrite::checkSiiData( // cycle through categories to detect corruption categoryHeader = data->words + 0x0040U; - categoryType = le16tocpu(*categoryHeader); + categoryType = le16_to_cpup(categoryHeader); while (categoryType != 0xffff) { if (categoryHeader + 1 > data->words + data->nwords) { err << "SII data seem to be corrupted! " << "Use --force to write anyway."; throwCommandException(err); } - categorySize = le16tocpu(*(categoryHeader + 1)); + categorySize = le16_to_cpup(categoryHeader + 1); if (categoryHeader + 2 + categorySize + 1 > data->words + data->nwords) { err << "SII data seem to be corrupted! " @@ -188,7 +187,7 @@ void CommandSiiWrite::checkSiiData( throwCommandException(err); } categoryHeader += 2 + categorySize; - categoryType = le16tocpu(*categoryHeader); + categoryType = le16_to_cpup(categoryHeader); } } diff --git a/tool/CommandUpload.cpp b/tool/CommandUpload.cpp index 78ba56e0..717bf4a2 100644 --- a/tool/CommandUpload.cpp +++ b/tool/CommandUpload.cpp @@ -9,7 +9,6 @@ using namespace std; #include "CommandUpload.h" -#include "byteorder.h" /*****************************************************************************/ @@ -159,11 +158,11 @@ void CommandUpload::execute(MasterDevice &m, const StringVector &args) cout << sval << " 0x" << hex << setw(2) << sval << endl; break; case 0x0003: // int16 - sval = le16tocpu(*(int16_t *) data.target); + sval = le16_to_cpup(data.target); cout << sval << " 0x" << hex << setw(4) << sval << endl; break; case 0x0004: // int32 - sval = le32tocpu(*(int32_t *) data.target); + sval = le32_to_cpup(data.target); cout << sval << " 0x" << hex << setw(8) << sval << endl; break; case 0x0005: // uint8 @@ -171,11 +170,11 @@ void CommandUpload::execute(MasterDevice &m, const StringVector &args) cout << uval << " 0x" << hex << setw(2) << uval << endl; break; case 0x0006: // uint16 - uval = le16tocpu(*(uint16_t *) data.target); + uval = le16_to_cpup(data.target); cout << uval << " 0x" << hex << setw(4) << uval << endl; break; case 0x0007: // uint32 - uval = le32tocpu(*(uint32_t *) data.target); + uval = le32_to_cpup(data.target); cout << uval << " 0x" << hex << setw(8) << uval << endl; break; case 0x0009: // string diff --git a/tool/Makefile.am b/tool/Makefile.am index d11bda7f..42db97ea 100644 --- a/tool/Makefile.am +++ b/tool/Makefile.am @@ -82,7 +82,6 @@ noinst_HEADERS = \ CommandXml.h \ MasterDevice.h \ SdoCommand.h \ - byteorder.h \ sii_crc.h REV = `if test -s $(srcdir)/../svnrevision; then \ diff --git a/tool/byteorder.h b/tool/byteorder.h deleted file mode 100644 index be61569d..00000000 --- a/tool/byteorder.h +++ /dev/null @@ -1,40 +0,0 @@ -/***************************************************************************** - * - * $Id$ - * - ****************************************************************************/ - -#include - -/*****************************************************************************/ - -#define swap16(x) \ - ((uint16_t)( \ - (((uint16_t)(x) & 0x00ffU) << 8) | \ - (((uint16_t)(x) & 0xff00U) >> 8) )) -#define swap32(x) \ - ((uint32_t)( \ - (((uint32_t)(x) & 0x000000ffUL) << 24) | \ - (((uint32_t)(x) & 0x0000ff00UL) << 8) | \ - (((uint32_t)(x) & 0x00ff0000UL) >> 8) | \ - (((uint32_t)(x) & 0xff000000UL) >> 24) )) - -#if __BYTE_ORDER == __LITTLE_ENDIAN - -#define le16tocpu(x) x -#define le32tocpu(x) x - -#define cputole16(x) x -#define cputole32(x) x - -#elif __BYTE_ORDER == __BIG_ENDIAN - -#define le16tocpu(x) swap16(x) -#define le32tocpu(x) swap32(x) - -#define cputole16(x) swap16(x) -#define cputole32(x) swap32(x) - -#endif - -/****************************************************************************/