Ant*_*nco 5 c embedded gcc microchip bit-fields
我使用的是Microchip微控制器,它定义了以下结合:
__extension__ typedef struct tagT1CONBITS {
union {
struct {
uint16_t :1;
uint16_t TCS:1;
uint16_t TSYNC:1;
uint16_t :1;
uint16_t TCKPS:2;
uint16_t TGATE:1;
uint16_t :6;
uint16_t TSIDL:1;
uint16_t :1;
uint16_t TON:1;
};
struct {
uint16_t :4;
uint16_t TCKPS0:1;
uint16_t TCKPS1:1;
};
};
} T1CONBITS;
extern volatile T1CONBITS T1CONbits __attribute__((__sfr__));
Run Code Online (Sandbox Code Playgroud)
在我的代码中的某处我将变量定义为8位无符号整数,我想将其分配给上面联合的一个字段.有点如下:
uint8_t tckps;
// The value of tckps is calculated here by some magic formula
tckps = magicformula();
// We asign the value of tckps to the uC register
T1CONbits.TCKPS = tckps;
Run Code Online (Sandbox Code Playgroud)
我-Wconversion在gcc中启用了选项,导致以下警告:
warning: conversion to 'volatile unsigned char:2' from 'uint8_t' may alter its value
Run Code Online (Sandbox Code Playgroud)
我能理解为什么gcc警告我.我目前正在对tckps变量进行一次值检查,然后将其设置为所以我知道数据丢失不会成为问题,但我不知道如何满足gcc转换检查以便它不会在此警告我特殊案例.
我该如何修复警告?
提前致谢!
编辑:添加了工具链信息.
Microchip Language Tool Shell Version 1.33 (Build date: Oct 9 2017).
Copyright (c) 2012-2016 Microchip Technology Inc. All rights reserved
*** Executing: "C:\Program Files (x86)\Microchip\xc16\v1.33\bin\bin/elf-gcc.exe"
"-v"
Using built-in specs.
COLLECT_GCC=C:\Program Files (x86)\Microchip\xc16\v1.33\bin\bin/elf-gcc.exe
Target: pic30-elf
Configured with: /home/xc16/release-builds/build_20171009/src/XC_GCC/gcc/configure --build=i386-linux --host=i386-mingw32 --target=pic30-elf --disable-lto --disable-threads --disable-libmudflap --disable-libssp --disable-libstdcxx-pch --disable-hosted-libstdcxx --with-gnu-as --with-gnu-ld --enable-languages=c --disable-nls --disable-libgomp --without-headers --disable-libffi --disable-bootstrap --prefix=/bin --libexecdir=/bin --program-prefix=pic30- --with-libelf=/home/xc16/release-builds/build_20171009/bin/XC_GCC-elf-mingw32-xclm/host-libs/ --with-dwarf2 --with-gmp=/home/xc16/release-builds/build_20171009/bin/XC_GCC-elf-mingw32-xclm/host-libs --with-ppl=/home/xc16/release-builds/build_20171009/bin/XC_GCC-elf-mingw32-xclm/host-libs --with-cloog=/home/xc16/release-builds/build_20171009/bin/XC_GCC-elf-mingw32-xclm/host-libs --with-zlib=/home/xc16/release-builds/build_20171009/bin/XC_GCC-elf-mingw32-xclm/host-libs --with-bugurl=http://www.microchip.com/support --with-host-libstdcxx=-Wl,-Bstatic,-lstdc++,-Bdynamic,-lm
Thread model: single
gcc version 4.5.1 (XC16, Microchip v1.33) Build date: Oct 9 2017 (Microchip Technology)
Run Code Online (Sandbox Code Playgroud)
这可以让 met 摆脱警告:
#include <stdint.h>
typedef struct tagT1CONBITS {
union {
struct {
uint16_t :1;
uint16_t TCS:1;
uint16_t TSYNC:1;
uint16_t :1;
uint16_t TCKPS:2;
uint16_t TGATE:1;
uint16_t :6;
uint16_t TSIDL:1;
uint16_t :1;
uint16_t TON:1;
};
struct {
uint16_t :4;
uint16_t TCKPS0:1;
uint16_t TCKPS1:1;
};
};
} T1CONBITS;
volatile T1CONBITS T1CONbits;
uint8_t (*magicformula)(void);
int main(void)
{
uint8_t tckps;
// The value of tckps is calculated here by some magic formula
tckps = magicformula() ;
// We asign the value of tckps to the uC register
T1CONbits.TCKPS = (uint8_t)(tckps & 3); // This fixes the warning
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我用以下方法编译它:
gcc -Wall -Wconversion test2.c
Run Code Online (Sandbox Code Playgroud)
我看到的问题是编译器无法检查函数边界是否超出变量的范围。如果您在使用时执行此操作,编译器可以检查这一点。
强制转换是为了避免在表达式提升为 int 时出现警告。