实验4:基于IAR的Contiki系统在CC2530下的移植 (4)

核能气质少年 分享 2020-04-15 下载文档

www.iotdev.net -专业的物联网|Contiki 6Lowpan|Zigbee|RFID|M2M|无线传感器网络开发论坛

{

unsigned short i;

unsigned char cclkconcmd; unsigned char cclkconsta; // Configure clock to use XOSC SLEEPCMD &= ~OSC_PD;

while (!(SLEEPSTA & XOSC_STB)); __asm(\

/* Require 63us delay for all revs */ for (i=0; i<0x505; i++) { __asm(\} CLKCONCMD = (0x00 | OSC_32KHZ_XOSC); /* 32MHz XOSC */ while (CLKCONSTA != (0x00 | OSC_32KHZ_XOSC)); SLEEPCMD |= OSC_PD; /* turn off 16MHz RC */ /* turn on 16MHz RC and 32MHz XOSC */ /* wait for 32MHz XOSC stable */

/* chip bug workaround */

CLKCONCMD |= TICKSPD_B2 | TICKSPD_B1 | TICKSPD_B0;/*tickspeed 250 kHz*/ cclkconcmd = CLKCONCMD; do{ cclkconsta = CLKCONSTA; }while(cclkconcmd != cclkconsta); __asm(\

} 文件 sys_handler.h 中主要实现的是对 sys_handler.c 中函数的声明以及一些配

置信息,这里就不列举其中的代码实现了。

最后,主要是为 cpu\\cc2530\\clock.c 文件添加处理代码,实现操作系统的移

植。该文件主要完成系统时钟初始化、系统 tick 处理等功能。clock.c 的头文件

声明在 core\\sys\\clock.h 下。clock.c 的具体代码实现如程序清单 2.5 所示。

程序清单 2.5 clock.c 的具体代码实现

/*******************************************************************************************

** 头文件包含 *******************************************************************************************/ #include #include \IOTDev.net 物联网开发论坛编制

15

www.iotdev.net -专业的物联网|Contiki 6Lowpan|Zigbee|RFID|M2M|无线传感器网络开发论坛

#include \#include \#include \

/*******************************************************************************************

** Sleep timer runs on the 32k RC osc, One clock tick is 7.8 ms

*******************************************************************************************/

#define TICK_VAL (32768/128)

#define MAX_TICKS (~((clock_time_t)0) / 2)

/******************************************************************************************* ** 全局变量定义 *******************************************************************************************/ // Used in sleep timer interrupt for calculating the next interrupt time static unsigned long timer_value; // starts calculating the ticks right after reset static volatile clock_time_t count = 0; // calculates seconds static volatile clock_time_t seconds = 0; /******************************************************************************************* ** Function name: clock_delay ** Descriptions: 时钟延时控制,One delay is about 0.6 us, so this function delays for len * 0.6 us ** Input parameters:无 ** Output parameters: 无 ** Returned value: 无 *******************************************************************************************/ void clock_delay(unsigned int len) { unsigned int i; for(i = 0; i< len; i++) { __asm(\} } /******************************************************************************************* ** Function name: clock_wait ** Descriptions: Wait for a multiple of ~8 ms (a tick) ** Input parameters:无 ** Output parameters: 无 ** Returned value: 无 IOTDev.net 物联网开发论坛编制

16

www.iotdev.net -专业的物联网|Contiki 6Lowpan|Zigbee|RFID|M2M|无线传感器网络开发论坛

*******************************************************************************************/

void clock_wait(int i) {

clock_time_t start; start = clock_time();

while(clock_time() - start < (clock_time_t)i);

}

/*******************************************************************************************

** Function name: clock_time ** Descriptions:获取时钟计数 ** Input parameters:无 ** Output parameters: 无 ** Returned value: 当前时钟计数值 *******************************************************************************************/ clock_time_t clock_time(void) { return count; } /******************************************************************************************* ** Function name: clock_seconds ** Descriptions:获取当前时钟秒数 ** Input parameters:无 ** Output parameters: 无 ** Returned value: 当前时钟计数值,以秒为单位 CCIF unsigned long clock_seconds(void) *******************************************************************************************/ { return seconds; } /******************************************************************************************* ** Function name: clock_init ** Descriptions:时钟初始化 ** Input parameters:无 ** Output parameters: 无 ** Returned value: 无 *******************************************************************************************/ void clock_init(void) { //Initialize tick value timer_value = ST0; IOTDev.net 物联网开发论坛编制

// sleep timer 0. low bits [7:0] 17

www.iotdev.net -专业的物联网|Contiki 6Lowpan|Zigbee|RFID|M2M|无线传感器网络开发论坛

timer_value += ((unsigned long int)ST1) << 8; timer_value += TICK_VAL;

ST2 = (unsigned char) (timer_value >> 16); ST1 = (unsigned char) (timer_value >> 8); ST0 = (unsigned char) timer_value; IEN0 |= B_STIE; EA = 1;

// middle bits [15:8]

// init value 256

timer_value += ((unsigned long int)ST2) << 16; // high bits [23:16]

// interrupt enable for sleep timers.

// STIE=Interrupt mask, CPU

} /******************************************************************************************* ** Function name: cc2530_clock_ISR ** Descriptions:时钟 tick 中断服务函数 ** Input parameters:无 ** Output parameters: 无 ** Returned value: 无 ** Created by:任海波 ** Created Date: 2012-06-21 *******************************************************************************************/ #pragma vector= ST_VECTOR __interrupt void cc2530_clock_ISR( void ) { EA = 0; /*interrupt disable*/ /* When using the cooperative scheduler the timer 2 ISR is only required to increment the RTOS tick count. */ /*Read value of the ST0,ST1,ST2 and then add TICK_VAL and write it back. Next interrupt occurs after the current time + TICK_VAL*/ timer_value = ST0; timer_value += ((unsigned long int)ST1) << 8; timer_value += ((unsigned long int)ST2) << 16; timer_value += TICK_VAL; ST2 = (unsigned char) (timer_value >> 16); ST1 = (unsigned char) (timer_value >> 8); ST0 = (unsigned char) timer_value; ++count; /* Make sure the CLOCK_CONF_SECOND is a power of two, to ensure that the modulo operation below becomes a logical and and not an expensive divide. Algorithm from Wikipedia: IOTDev.net 物联网开发论坛编制

18

www.iotdev.net -专业的物联网|Contiki 6Lowpan|Zigbee|RFID|M2M|无线传感器网络开发论坛

http://en.wikipedia.org/wiki/Power_of_two */

#if (CLOCK_CONF_SECOND & (CLOCK_CONF_SECOND - 1)) != 0

#error CLOCK_CONF_SECOND must be a power of two (i.e., 1, 2, 4, 8, 16, 32, 64, ...). #error Change CLOCK_CONF_SECOND in contiki-conf.h. #endif

if(count % CLOCK_CONF_SECOND == 0) { ++seconds; }

if(etimer_pending() &&

(etimer_next_expiration_time() - count - 1) > MAX_TICKS) { /*core/sys/etimer.c*/ etimer_request_poll(); } /*IRCON.STIF = Sleep timer interrupt flag. This flag called this interrupt func, now reset it*/ IRCON &= ~B_STIF; /*interrupt enable*/ EA = 1; } 2.5 重定向 printf 接口

函数 printf 是 C 语言编程的一个标准 IO 函数,在 PC 编程中他输出信息到

控制台窗口,给用户提示应用程序的执行过程。在嵌入式编程中,可能没有 CRT

显示器,但是该函数支持重定向。例如,可以将该函数执行到从串口输出,通过

串口可以查看相应的调试信息以及应用程序执行信息。这在开发调试应用程序的

过程中是非常重要的。

2.6 编译工程

移植代码已经编写完毕,现在利用 IAR 的 IDE 编译工程,编译结果如图 2.6

所示。编译没有错误,但是有一些警告,该警告是由于一些系统兼容问题引起的,

可以通过编译器屏蔽掉,屏蔽该编译警告的方法如图 2.7 所示。

IOTDev.net 物联网开发论坛编制

19


实验4:基于IAR的Contiki系统在CC2530下的移植 (4).doc 将本文的Word文档下载到电脑

下一篇:土木工程概论复习题及参考答案

相关推荐
相关阅读
本类排行
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 7 元

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:xxxxxx QQ:xxxxxx