版本:Contiki 3.x
平台:TI SmartRF06 + cc2538EM
网络:Rime
----------------------------------------史上最牛逼分割线-----------------------------------------------
文档说明:
本文档主要描述Contiki3.X源码里边对设备节点的设置。
----------------------------------------史上最牛逼分割线-----------------------------------------------
文档更新记录:
创建文档2016/03/19
-------------------------------------------干货分割线-----------------------------------------------------
源码:函数set_rf_params(Contiki-main.c)
static void
set_rf_params(void)
{
uint16_t short_addr;
uint8_t ext_addr[8];
ieee_addr_cpy_to(ext_addr, 8);
short_addr = ext_addr[7];
short_addr |= ext_addr[6] << 8;
/* Populate linkaddr_node_addr. Maintain endianness */
memcpy(&linkaddr_node_addr, &ext_addr[8 - LINKADDR_SIZE], LINKADDR_SIZE);
#if STARTUP_CONF_VERBOSE
{
int i;
printf("Rime configured with address ");
for(i = 0; i < LINKADDR_SIZE - 1; i++) {
printf("%02x:", linkaddr_node_addr.u8[i]);
}
printf("%02x\n", linkaddr_node_addr.u8[i]);
}
#endif
NETSTACK_RADIO.set_value(RADIO_PARAM_PAN_ID, IEEE802154_PANID);
NETSTACK_RADIO.set_value(RADIO_PARAM_16BIT_ADDR, short_addr);
NETSTACK_RADIO.set_value(RADIO_PARAM_CHANNEL, CC2538_RF_CHANNEL);
NETSTACK_RADIO.set_object(RADIO_PARAM_64BIT_ADDR, ext_addr, 8);
}
分析:
1、函数set_rf_params(void)是在Contiki的main函数里调用。
2、函数set_rf_params(void)主要实现射频相关参数的初始化,包括 PANID、CHANNEL、linkaddr等。
3、通过ieee_addr_cpy_to(ext_addr, 8);将地址节点的唯一地址读取到ext_addr数组里,参考下文。
4、#define LINKADDR_SIZE 2,
节点地址宽度默认被定义为2BYTE。可以在Contiki-main.h文件里定义这里的宽度,只支持2\8两种格式。
其中 通过memcpy函数将 ext_addr里的地址赋值给linkaddr_node_addr变量也就是节点地址
5、通过串口打印我们可以很容易的看到节点的Rime网络地址。
源码:函数ieee_addr_cpy_to(ieee-addr.c)
void
ieee_addr_cpy_to(uint8_t *dst, uint8_t len)
{
if(IEEE_ADDR_CONF_HARDCODED) {
uint8_t ieee_addr_hc[8] = IEEE_ADDR_CONF_ADDRESS;
memcpy(dst, &ieee_addr_hc[8 - len], len);
} else {
/*
* By default, we assume that the IEEE address is stored on flash using
* little-endian byte order.
*
* However, some SoCs ship with a different byte order, whereby the first
* four bytes are flipped with the four last ones.
*
* Using this address as an example: 00 12 4B 00 01 02 03 04
* We expect it stored as: 04 03 02 01 00 4B 12 00
* But it is also possible to encounter: 00 4B 12 00 04 03 02 01
*
* Thus: read locations [3, 2, 1] and if we encounter the TI OUI, flip the
* order of the two 4-byte sequences. Each of the 4-byte sequences is still
* little-endian.0x0027FFCC
*/
int i;
uint8_t oui_ti[3] = IEEE_ADDR_OUI_TI;
if(((uint8_t *)IEEE_ADDR_LOCATION)[3] == oui_ti[0]
&& ((uint8_t *)IEEE_ADDR_LOCATION)[2] == oui_ti[1]
&& ((uint8_t *)IEEE_ADDR_LOCATION)[1] == oui_ti[2]) {
for(i = 0; i < len / 2; i++) {
dst[i] = ((uint8_t *)IEEE_ADDR_LOCATION)[len / 2 - 1 - i];
}
for(i = 0; i < len / 2; i++) {
dst[i + len / 2] = ((uint8_t *)IEEE_ADDR_LOCATION)[len - 1 - i];
}
} else {
for(i = 0; i < len; i++) {
dst[i] = ((uint8_t *)IEEE_ADDR_LOCATION)[len - 1 - i];
}
}
}
#if IEEE_ADDR_NODE_ID
dst[len - 1] = IEEE_ADDR_NODE_ID & 0xFF;
dst[len - 2] = IEEE_ADDR_NODE_ID >> 8;
#endif
}
分析:
1、#define IEEE_ADDR_CONF_HARDCODED 0
2、#define IEEE_ADDR_OUI_TI { 0x00, 0x12, 0x4B } /**< TI OUI */
3、#define IEEE_ADDR_LOCATION_SECONDARY 0x0027FFCC /
4、IEEE_ADDR_NODE_ID宏未定义。
5、最终的地址来源为0x0027FFCC 地址处存放的8字节设备地址,TI的cc2538采用的是小端特殊顺序。
eg:设备地址 : 00 12 4B 00 01 02 03 04
小端地址: 04 03 02 01 00 4B 12 00
TI芯片专用: 00 4B 12 00 04 03 02 01