gpt4 book ai didi

c++ - Cryptoauthlib - 匿名 union 只能有非静态数据成员 - 段错误

转载 作者:搜寻专家 更新时间:2023-10-31 02:04:14 26 4
gpt4 key购买 nike

我正在尝试使用 cryptoauthlib 编译一个 c++ 程序,但我在 atca_iface.h 中遇到了这个(匿名 union 只能有非静态数据成员)错误。我认为这是一个 c11 特性,与 c++ 不兼容:

未命名 union 体中的所有命名结构都会抛出错误。我删除了以下结构的名称:

typedef struct
{

ATCAIfaceType iface_type; // active iface - how to interpret the union below
ATCADeviceType devtype; // explicit device type

union // each instance of an iface cfg defines a single type of interface
{
struct //ATCAI2C
{
uint8_t slave_address; // 8-bit slave address
uint8_t bus; // logical i2c bus number, 0-based - HAL will map this to a pin pair for SDA SCL
uint32_t baud; // typically 400000
} atcai2c;

struct //ATCASWI
{
uint8_t bus; // logical SWI bus - HAL will map this to a pin or uart port
} atcaswi;

struct //ATCAUART
{
int port; // logic port number
uint32_t baud; // typically 115200
uint8_t wordsize; // usually 8
uint8_t parity; // 0 == even, 1 == odd, 2 == none
uint8_t stopbits; // 0,1,2
} atcauart;

struct //ATCAHID
{
int idx; // HID enumeration index
uint32_t vid; // Vendor ID of kit (0x03EB for CK101)
uint32_t pid; // Product ID of kit (0x2312 for CK101)
uint32_t packetsize; // Size of the USB packet
uint8_t guid[16]; // The GUID for this HID device
} atcahid;

struct //ATCACUSTOM
{
ATCA_STATUS (*halinit)(void *hal, void *cfg);
ATCA_STATUS (*halpostinit)(void *iface);
ATCA_STATUS (*halsend)(void *iface, uint8_t *txdata, int txlength);
ATCA_STATUS (*halreceive)(void *iface, uint8_t* rxdata, uint16_t* rxlength);
ATCA_STATUS (*halwake)(void *iface);
ATCA_STATUS (*halidle)(void *iface);
ATCA_STATUS (*halsleep)(void *iface);
ATCA_STATUS (*halrelease)(void* hal_data);
} atcacustom;

};

uint16_t wake_delay; // microseconds of tWHI + tWLO which varies based on chip type
int rx_retries; // the number of retries to attempt for receiving bytes
void * cfg_data; // opaque data used by HAL in device discovery
} ATCAIfaceCfg;

我将这些结构更改为未命名的结构并编译,但不幸的是,在调用 init 函数时出现段错误 ATCA_STATUS atinit(ATCAIface ca_iface);...我没有直接调用此函数.我调用 atcab_init(&cfg_ateccx08a_i2c_default);

有没有办法像我一样在不修改界面的情况下使用它,为什么会出现段错误?

我的硬件设置是带有连接到 I2C 1 的 ATECC608a 的 CM3。接口(interface)已打开,我可以查询它。我是否需要将默认接口(interface)修改为正确的设备类型和正确的 I2C 接口(interface)?当我这样做时,我会遇到同样的段错误。

编辑:

示例代码

#include <iostream>
#include "cryptoauthlib.h"


int main(int argc, char *argv[])
{

uint8_t random_number;
atcab_init(&cfg_ateccx08a_i2c_default);
ATCA_STATUS status = atcab_random(&random_number);

std::cout << "status: " << (int)status << std::endl;

return 0;
}

编辑2:

github 上有人告诉我必须使用 -DATCA_HAL_I2C 作为 C 编译器标志。但是我现在得到 undefined reference to hal_i2c_init 错误

编辑3:

问题似乎出在 cryptoauthlib 的 cmake 配置上,因为我在 windows 上为 armhf 交叉编译。

查看 cmake 配置

编辑4:

重现最新的错误:

here 获取 cryptoauthlib|从 here 获取 VisualGDB安装 VS 2017安装 VisualGDB使用 VisualGDB 创建一个新项目 -> CMAKE 项目从 here 下载最新的工具链在 VisualGDB 中使用它添加 cryptoauthlib 作为对项目的引用在项目的CMakeLists.txt中的project()后添加如下CMAKE命令:

unset(WIN32)
unset(APPLE)
set(UNIX)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fms-extensions -std=c11 -DATCA_HAL_I2C=on -DATCAPRINTF=on")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -DATCA_HAL_I2C=on -DATCAPRINTF=on")

构建。

编辑5:

发现我必须在我的 CMAKE 项目中显式地执行 set(ATCA_HAL_I2C TRUE) ...作为一个选项,它对我不起作用,不幸的是。

现在我在这里再次遇到段错误:

ATCA_STATUS atcab_random(uint8_t *rand_out)
{
...
ATCACommand ca_cmd = _gDevice->mCommands;

...
}

我将其打开为 issue在 github 上。

最佳答案

我发现了错误。硬件抽象层无法连接,因为 i2c 总线 .atcai2c.bus 的默认总线设置设置为 2,它需要为 1,否则将找不到 i2c 设备。

ATCAIfaceCfg cfg_ateccx08a_i2c_default = {
.iface_type = ATCA_I2C_IFACE,
.devtype = ATECC608A,
.atcai2c.slave_address = 0xC0,
.atcai2c.bus = 1,
.atcai2c.baud = 400000,
//.atcai2c.baud = 100000,
.wake_delay = 1500,
.rx_retries = 20
};

我现在可以通过以下方式从芯片中获取修订版、序列号和随机数:

#include <iostream>
#include "cryptoauthlib.h"


int main(int argc, char *argv[])
{

ATCAIfaceCfg *atca_cfg;
atca_cfg = &cfg_ateccx08a_i2c_default;

ATCA_STATUS status = atcab_init(atca_cfg);

std::cout << "status: " << (int)status << std::endl;

uint8_t revision[4];
status = atcab_info(revision);
if (status != ATCA_SUCCESS)
{
return -1;
}
std::cout << "revision: " << (int)revision << std::endl;

uint8_t serial[ATCA_SERIAL_NUM_SIZE];
status = atcab_read_serial_number(serial);
if (status != ATCA_SUCCESS)
{
return - 1;
}
std::cout << "serial: " << (int)serial << std::endl;

uint8_t num[32];
status = atcab_random(num);
if (status != ATCA_SUCCESS)
{
return - 1;
}
std::cout << "random: " << (int)num << std::endl;
return 0;
}

我会在 github 上继续这个。

关于c++ - Cryptoauthlib - 匿名 union 只能有非静态数据成员 - 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53817885/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com