小编Mup*_*man的帖子

在没有uint8_t数据类型的MCU上使用uint8_t进行结构化

我是嵌入式软件开发人员,我想与外部设备连接.该器件通过SPI发送数据.该数据的结构是从外部设备制造商预定义的,无法编辑.制造商提供一些Header文件,其中包含通过SPI发送的所有数据的许多typedef.制造商还提供了一个API来以正确的方式处理收到的数据包(我可以访问该API的源代码).

现在我的问题:typedefed结构包含许多uint8_t数据类型.不幸的是,我们的MCU不支持uint8_t数据类型,因为最小的类型是16bit宽(因此,即使一个字符具有16位).

要正确使用API​​,必须使用通过SPI接收的数据填充结构.由于输入的数据字节的数据包,我们不能仅仅将这些数据复制到结构,因为我们的结构使用16位为那些8位类型.因此,我们需要执行许多位移操作来正确分配接收的数据.

示例:(制造商typedef结构)

typedef struct NETX_COMMUNICATION_CHANNEL_INFOtag
{
  uint8_t   bChannelType;              //uint16_t in our system
  uint8_t   bChannelId;                //uint16_t in our system
  uint8_t   bSizePositionOfHandshake;  //uint16_t in our system
  uint8_t   bNumberOfBlocks;           //uint16_t in our system
  uint32_t  ulSizeOfChannel;           
  uint16_t  usCommunicationClass;      
  uint16_t  usProtocolClass;           
  uint16_t  usProtocolConformanceClass;
  uint8_t   abReserved[2];             //uint16_t in our system
} NETX_COMMUNICATION_CHANNEL_INFO;
Run Code Online (Sandbox Code Playgroud)

任何人都可以想到这个问题的简单解决方法吗?我真的不想为每个接收的数据包类型写一个单独的bitshift操作.(性能/时间/空间浪费)

我的想法 (使用位域将2xuint8_t填充到uint16_t或4xuint8_t到uint32_t)

typedef struct NETX_COMMUNICATION_CHANNEL_INFOtag
{
  struct packet_uint8{
    uint32_t  bChannelType              :8;
    uint32_t  bChannelId                :8;
    uint32_t  bSizePositionOfHandshake  :8;
    uint32_t  bNumberOfBlocks           :8;
  }packet_uint8;
  uint32_t  ulSizeOfChannel;               
  uint16_t  usCommunicationClass;          
  uint16_t  usProtocolClass;               
  uint16_t  usProtocolConformanceClass;    
  uint16_t …
Run Code Online (Sandbox Code Playgroud)

c embedded uint8t

6
推荐指数
1
解决办法
216
查看次数

c中的"私人"变量

嵌入式软件,我可以使用C.

我正在从EEPROM中读取电路板的当前版本.

根据该修订号,可以定义负载的最大支持电流.

现在我需要代码中的大量位置信息(但只读).只允许读取EEPROM的函数写入值.所有其他功能等不得更改此值,错误可能会导致负载或电路板损坏.

有没有办法在c中这样做?(不是C++)

c

2
推荐指数
1
解决办法
609
查看次数

vtable 何时创建/填充?

我很难弄清楚为什么我的代码不起作用。我将问题追溯到 vtable 生成/填充。

这是我的代码的简化版本:

#include <iostream>
#include <functional>
#include <thread>

using namespace std;

typedef std::function<void (void)> MyCallback;

MyCallback clb = NULL;

void callCallbackFromThread(void){
    while(clb == NULL);
    clb();
}

int someLongTask(void){
    volatile unsigned int i = 0;
    cout << "Start someLongTask" << endl;
    while(i < 100000000)
        i++;
    cout << "End someLongTask" << endl;
    return i;
}

class Base{
public:
    Base(){
        clb = std::bind(&Base::_methodToCall, this);
        cout << "Base-Constructor" << endl;
    }
protected:
    virtual void methodToCall(void){
        cout << "Base methodToCall got called!" << …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance multithreading vtable

0
推荐指数
1
解决办法
131
查看次数

标签 统计

c ×2

c++ ×1

embedded ×1

inheritance ×1

multithreading ×1

uint8t ×1

vtable ×1