0

在此先感谢您的帮助。

首先是前言。我一直在研究使用https://github.com/contiki-os/contiki/tree/master/core/net/mac/tsch给出的 Contiki TSCH 实现。在 Cooja 模拟器中运行一个简单的示例时(在基本代码中添加了一些日志消息,以便我可以看到发生了什么),我注意到 ASN 计数器没有增加其最高有效字节。

具体来说,ASN(TSCH 的绝对时隙编号)由结构体给出

// data type of absolute slot number
struct tsch_asn_t {
  uint32_t ls4b; /* least significant 4 bytes */
  uint8_t  ms1b; /* most significant 1 byte */
};

并且本质上可以被认为是一个索引值,表示为两个变量,最高有效字节和最低有效 4 个字节。以下宏用于将 ASN 增加一定数量。

// Here is the macro
#define TSCH_ASN_INC(asn, inc) do { \
    printf("TSCH INC1: new UNDEF, old %u, inc %u\n", (asn).ls4b, inc);  \
\
    uint32_t new_ls4b = (asn).ls4b + (inc); \
\
    printf("TSCH INC2: new %u, old %u, inc %u\n",new_ls4b, (asn).ls4b, inc);  \
\
    if(new_ls4b < (asn).ls4b) { (asn).ms1b++; } \
\
    printf("TSCH INC3: new %u, old %u, inc %u\n",new_ls4b, (asn).ls4b, inc);  \
\
    (asn).ls4b = new_ls4b; \
\
    printf("TSCH INC4: new %u, old %u, inc %u\n",new_ls4b, (asn).ls4b, inc);  \
} while(0);

暂时忽略 printf 语句(我添加了这些语句以试图了解发生了什么)。据我了解这个宏,最不重要的字节首先增加。然后,如果最低有效字节环绕最高有效字节,则递增。但是,在查看正在发生的事情的日志时,我注意到当最不重要的字节环绕时,最重要的字节并没有增加。

现在到我的实际问题。为了确定为什么最重要的字节没有被增加,我将上面的打印语句添加到宏中,并在实际宏调用的上方和下方添加了打印语句,如下所示(注意 ASN 由 tsch_current_asn 给出,timeslot_diff 是我想要的数量将 ASN 递增)。

printf("TSCH INC BEFORE: ms1b %u, ls4b %u, inc %u\n",tsch_current_asn.ms1b, tsch_current_asn.ls4b, timeslot_diff);
TSCH_ASN_INC(tsch_current_asn, timeslot_diff);
printf("TSCH INC AFTER: ms1b %u, ls4b %u, inc %u\n",tsch_current_asn.ms1b, tsch_current_asn.ls4b, timeslot_diff);

这样做产生了以下日志,这绝对让我难过

// LOG
TIME        DEVID   LOG MSG
00:28.885   ID:1    TSCH INC BEFORE: ms1b 0, ls4b 1877, inc 0
00:28.888   ID:1    TSCH INC1: new UNDEF, old 1877, inc 0
00:28.891   ID:1    TSCH INC2: new 1878, old 0, inc 1877
00:28.895   ID:1    TSCH INC3: new 1878, old 0, inc 1877
00:28.898   ID:1    TSCH INC4: new 1878, old 0, inc 1878
00:28.901   ID:1    TSCH INC AFTER: ms1b 0, ls4b 1878, inc 0

具体来说,似乎在宏 inc 中的第一个和第二个 printf 语句之间(timeslot_diff)从 0 更改为 1877。换句话说,在我看来,该语句

uint32_t new_ls4b = (asn).ls4b + (inc); \

更改了 inc 的值(timeslot_diff)。此外,在我看来,此语句将 ans.ls4b (tsch_current_asn.ls4b) 从 1877 更改为 0。

关于宏如何工作,或者这是 Contiki 的 protothreads 的影响(即被暂停并稍后恢复),我是否有一个高级的时间?

作为参考,宏调用的实际代码在 https://github.com/contiki-os/contiki/blob/master/core/net/mac/tsch/tsch-slot-operation.c的第 1035 行给出该宏在https://github.com/contiki-os/contiki/blob/master/core/net/mac/tsch/tsch-asn.h的第 67 行给出。

4

1 回答 1

2

看起来 printf 说明符与 16 位架构上发生的实际参数类型不匹配。inttypes.h建议对来自的所有整数类型使用定义为格式宏常量的说明符stdint.h

即当xuint32_t你应该使用的类型

printf("x is %"PRIu32" \n", x);

代替

printf("x is %u \n", x);
于 2018-05-22T14:10:01.803 回答