9.1. TIMER - Second Timing Interrupt
This demo is based on TIMER interrupt mode with second timing.
9.1.1. Hardware Connection
None
9.1.2. Software Implementation
See
examples/timer/timer_int
for the software code
1#define BSP_TIMER0_CLOCK_SOURCE ROOT_CLOCK_SOURCE_FCLK
2#define BSP_TIMER0_CLOCK_DIV 0
Configure
TIMER
device clock source,seebsp/board/bl706_iot/clock_config.h
1#define BSP_USING_TIMER0
2
3#if defined(BSP_USING_TIMER0)
4#ifndef TIMER0_CONFIG
5#define TIMER0_CONFIG \
6 { \
7 .id = 0, \
8 .cnt_mode = TIMER_CNT_PRELOAD, \
9 .trigger = TIMER_PRELOAD_TRIGGER_COMP2, \
10 .reload = 0, \
11 .timeout1 = 1000000, \
12 .timeout2 = 2000000, \
13 .timeout3 = 3000000, \
14 }
15#endif
16#endif
Enable
BSP_USING_TIMER0
and configureTIMER0
device,seebsp/board/bl706_iot/peripheral_config.h
1timer_register(TIMER0_INDEX, "timer0");
2
3timer0 = device_find("timer0");
4
5if (timer0) {
6 device_open(timer0, DEVICE_OFLAG_INT_TX); /* 1s,2s,3s timing*/
7 device_set_callback(timer0, timer0_irq_callback);
8 device_control(timer0, DEVICE_CTRL_SET_INT, (void *)(TIMER_COMP0_IT | TIMER_COMP1_IT | TIMER_COMP2_IT));
9} else {
10 MSG("timer device open failed! \n");
11}
Call
timer_register
function to registerTIMER
device, currently registerTIMER0
Then use the
find
function to find the handle corresponding to the device and save it in thetimer0
handleFinally use
device_open
to open thetimer0
device in interrupt modeCall
device_set_callback
to register irq callback namedtimer0_irq_callback
. Calldevice_control
to enable irq and configure timing period.
1void timer0_irq_callback(struct device *dev, void *args, uint32_t size, uint32_t state)
2{
3 if (state == TIMER_EVENT_COMP0) {
4 MSG("timer event comp0! \r\n");
5 } else if (state == TIMER_EVENT_COMP1) {
6 MSG("timer event comp1! \r\n");
7 } else if (state == TIMER_EVENT_COMP2) {
8 BL_CASE_SUCCESS;
9 timer_timeout_cfg_t cfg = { 2, 12000000 }; /*modify compare id 2 timeout 12s*/
10 device_write(dev, 0, &cfg, sizeof(timer_timeout_cfg_t));
11 MSG("timer event comp2! \r\n");
12 }
13}
In irq callback,try to determine whether compare id flag is coming.
Call
device_write
to modify compare id 2 timeout with 12s.
9.1.3. Compile and Program
CDK compilation
Open project:timer_int.cdkproj
Refer to the steps of Guide to using CDK (like MDK Keil) under Windows to compile and download
Command compilation
1 $ cd <sdk_path>/bl_mcu_sdk
2 $ make BOARD=bl706_iot APP=timer_int
Program