00. 目录
文章目录
01. 概述
The Clock Module
The clock module provides functions for handling system time.
The API for the Contiki clock module is shown below. The function clock_time()
returns the current system time in clock ticks. The number of clock ticks per second is platform dependent and is specified with the constant CLOCK_SECOND
. The system time is specified as the platform dependent type clock_time_t
and in most platforms this is a limited unsigned value which wraps around when getting too large. The clock module also provides a function clock_seconds()
for getting the system time in seconds as an unsigned long and this time value can become much larger before it wraps around (136 years on MSP430 based platforms). The system time starts from zero when the Contiki system starts.
The clock module provides two functions for blocking the CPU: clock_delay()
, which blocks the CPU for a specified delay, and clock_wait()
, which blocks the CPU for a specified number of clock ticks. These functions are normally only used in low-level drivers where it sometimes is necessary to wait a short time without giving up the control over the CPU.
The function clock_init()
is called by the system during the boot-up procedure to initialize the clock module.
The Clock Module API:
clock_time_t clock_time(); // Get the system time.
unsigned long clock_seconds(); // Get the system time in seconds.
void clock_delay(unsigned int delay); // Delay the CPU.
void clock_wait(int delay); // Delay the CPU for a number of clock ticks.
void clock_init(void); // Initialize the clock module.
CLOCK_SECOND; // The number of ticks per second.
Porting the Clock Module
The clock module is platform dependent and is implemented in the file clock.c
. Since the clock module handles the system time, the clock module implementation usually also handles the notifications to the etimer library when it is time to check for expired event timers.
02. functions for handling system time
这些函数都是platform dependent的,我们是在stm8中实现的。
clock_time_t clock_time(void);//return the current system time in clock ticks
unsigned long clock_seconds(void);//return the system time in seconds
void clock_set_seconds(unsigned long ec);//set the value of the platform seconds
#if USE_RTC_CLK
#if USE_LSE // 32768Hz
#define CLOCK_CONF_SECOND 1024
#else // 38000Hz
#define CLOCK_CONF_SECOND 1000
#endif
#else
#define CLOCK_CONF_SECOND 1024
#endif
typedef unsigned long clock_time_t;
/**
* A second, measured in system clock time.
*
* \hideinitializer
*/
#ifdef CLOCK_CONF_SECOND
#define CLOCK_SECOND CLOCK_CONF_SECOND
#else
#define CLOCK_SECOND (clock_time_t)32
#endif
其中我们的clock_time_t是unsigned long型的,在stm8中unsigned long是32bit,最大数值是4294967295。
The number of clock ticks per second is specified with the constant CLOCK_SECOND.
CLOCK_SECOND 按1024来算,clock_time函数wrap around的时间是:
4294967295/1024/(606024*365) = 0.133年
clock_seconds函数wrap aound的时间是:
4294967295/(606024*365) = 136.2年
The system time starts from zero when the Contiki system starts.
03. functions for blocking the CPU
/**
* Wait for a given number of ticks.
* \param t How many ticks.
*
*/
void clock_wait(clock_time_t t);
/**
* Delay a given number of microseconds.
* \param dt How many microseconds to delay.
*
* \note Interrupts could increase the delay by a variable amount.
*/
void clock_delay_usec(uint16_t dt);
These functions are normally only used in low-level drivers where it sometimes is necessary to wait a short time without giving up the control over the CPU.
The function clock_init()
is called by the system during the boot-up procedure to initialize the clock module.
main函数:
int
main(void)
{
reset_sr = RST->SR;
RST->SR = 0xFF;
clock_init();
leds_init();
leds_on(LEDS_GREEN);
HALT_LED_ON();
rs232_init(RS232_PORT_0, USART_BAUD_9600, USART_PARITY_NONE);
node_id_restore();
node_init();
process_init();
process_start(&etimer_process, NULL);
ctimer_init();
略……
return 0;
}
04. Porting the Clock Module
The clock module is platform dependent and is implemented in the file clock.c
. Since the clock module handles the system time, the clock module implementation usually also handles the notifications to the etimer library when it is time to check for expired event timers.
具体相关代码如下:
if(etimer_pending() && etimer_next_expiration_time() <= current_clock) {
etimer_request_poll();
}