このコードは、Microchip社のPIC32MXマイクロプロセッサ用です。彼らのコンパイラは本質的にGCC3.4です。
私はGCCの__packed__
属性を使用してビットフィールドをユニオンにパックし、後でunsigned char
SPIまたはI2Cを介して送信するための(つまり、型のパンニング)としてそれらを取得する傾向があります。この動作はすべて私の実装によって定義されており、完全に機能します。私はこれを100行ほどのマスキングとシフトよりも好みます:)
私の質問は:__packed__
以下のコードに冗長な属性がありますか?一見、トップレベルの組合員は免除できると思いますが、よくわかりません。または、ネストされた構造体でそれらを除外できますか?
// Remember that bitfields cannot straddle word boundaries!
typedef struct
{
/// Some flag #1
unsigned FlagOne : 1 __attribute__((packed));
/// Some flag #2
unsigned FlagTwo : 1 __attribute__((packed));
/// A chunk of data
unsigned SomeData : 5 __attribute__((packed));
// and so on, maybe up to 32 bits long depending on the destination
} BlobForSomeChip;
/// This kind of type-punning is implementation defined. Read Appendix A (A7, A12) of
/// the MPLAB C Compiler for PIC32 MCUs manual.
typedef union
{
/// Access the members of this union to set flags, etc
BlobForSomeChip blobdata __attribute__((packed));
/// As a byte for sending via SPI, I2C etc
unsigned char bytes[4] __attribute__((packed));
} BlobData;