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,see bsp/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 configure TIMER0 device,see bsp/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 register TIMER device, currently register TIMER0

  • Then use the find function to find the handle corresponding to the device and save it in the timer0 handle

  • Finally use device_open to open the timer0 device in interrupt mode

  • Call device_set_callback to register irq callback named timer0_irq_callback. Call device_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

1 $ cd <sdk_path>/bl_mcu_sdk
2 $ make BOARD=bl706_iot APP=timer_int

9.1.4. Experimental Phenomena