Topic

FAQ
Login Register
ZigBee SDK - timer in TIMER_MODE_GPIO
May 20, 2024 13:28

/

A

/A

I neeed to use timer in TIMER_MODE_GPIO_TRIGGER and gpio POL_RISING.
For example what I used in BLE sdk:
timer2_gpio_init(GPIO_HALL, POL_RISING);
timer2_set_mode(TIMER_MODE_GPIO_TRIGGER, 0, 2);
timer.h has the following defines for the mode
/**
* @brief Mode of Timer
*/
typedef enum{
TIMER_MODE_SYSCLK =0,
TIMER_MODE_GPIO_TRIGGER =1,
TIMER_MODE_GPIO_WIDTH =2,
TIMER_MODE_TICK =3,
}TIMER_ModeTypeDef;

ZigBee SDK has drv_timer.h in which there is the following defines:
/**
* hardware_timer_mode Hardware Timer Mode
*/
#define TIMER_MODE_SCLK 0 //!< Timer running in the system clock mode, it will free run from 0 to 0xffffffff
#define TIMER_MODE_GPIO 1
#define TIMER_MODE_WIDTH_GPIO 2
#define TIMER_MODE_TICK_MODE 3


There is no function where you set polarity for the GPIO trigger for this timer mode in ZigBee SDK!
I assume that I have to use 2 functions
hwTimerInit(0, TIMER_MODE_GPIO)
gpio_set_interrupt(GPIO_HALL, POL_RISING);
Following the irq functions in ZigBee SDK I found:
In irq_handler.c in function _attribute_ram_code_ void irq_handler(void) there is
if((src & FLD_IRQ_TMR0_EN)){
 reg_irq_src = FLD_IRQ_TMR0_EN;
  reg_tmr_sta = FLD_TMR_STA_TMR0;
  drv_timer_irq0_handler();
}
if((src & FLD_IRQ_GPIO_EN)==FLD_IRQ_GPIO_EN){
  reg_irq_src = FLD_IRQ_GPIO_EN;
  T_DBG_irqTest[5]++;
  drv_gpio_irq_handler();
}

And then for timer
- function drv_hwTmr_irq_process() that refers to the timer callback function.
For Gpio trigger
- function drv_gpio_irq_process(drv_gpioIrqMode_t mode) that refers to gpio callback function
How to do it when I have TIMER_MODE_GPIO and gpio trigger polarity POL_RISING  and if there are 2 irq trigger sources in irq handler and 2 callback functions
How does timer know which gpio pin trigers it? There is nothing in any timer init functions that I can see it.
What if I have other gpio pins that use interrupts - for example buttons.


Can you show some code sample, please.
I had a look in the example in the telink_b85m_driver_sdk (which doesn't use drv_timer.c), but even there it is not clear to me
how TIMER_MODE_GPIO_TRIGGER works.
The code in irq_handler is the same for any timer mode. There is no check what gpio triggered it or what polarity of gpio trigger was. There is no clearing gpio irq status. That's not really a good code that should serve as an example - in my opinion.
if(timer_get_interrupt_status(FLD_TMR_STA_TMR0))
{
  timer_clear_interrupt_status(FLD_TMR_STA_TMR0); //clear irq status
  timer0_irq_cnt ++;
  gpio_toggle(LED2);
}

And, by the way, I can't use this code because there is different way of handling interrupts in ZigBee SDK via callback functions and in driver code it is directly in the irq_handler function.




7 replies
TL_Soyo May 20, 2024 14:46
0
/A

Do you need to use both TIMER-MODE_GPIO-TRIGGER and gpio POL-RISING interrupts simultaneously? These are two different interrupt sources, so both can be placed in the interrupt function, and then the interrupt source can be used to determine what triggered them.

wes58 [Author] May 21, 2024 05:41
0
/A

Hi Soyo,

Yes I need to use both TIMER_MODE_GPIO_TRIGGER and POL_RISING. There is only 1 interrupt source - timer interrupt that I should worry about.

I had a look at the datasheet for TLSR8258:

5.1.3 Mode1 (GPIO Trigger Mode)
In Mode 1, GPIO is employed as clock source. The “m0”/“m1”/“m2” register specifies the GPIO
which generates counting signal
for Timer0/Timer1/Timer2.

Note: Refer to Section 7.1.2 for corresponding “m0”, “m1”, “m2” and “Polarity” register address


7.1.2 Connection relationship between GPIO and related modules

Counting (Mode 1) or control (Mode 2) signal for Timer0 = | ((input ^ polarity) & m0);


1. Where are those registers set in the timer.c or drv_timer.c?

If they are not, how to set them?


From the API user guide for TLSR 8258

Timer0/Timer1: general timer is provided for user, it supports 4 modes:

- GPIO Trigger Mode: Timer Tick is increased by 1 on each positive or negative (configurable) edge of GPIO from initial Tick value


When you look at the functions in timer.h (timer.c) in BLE SDK and in the B85_Driver Demo SDK there are:

/**
* @brief initiate GPIO for gpio trigger and gpio width mode of timer0.

* @param[in] pin - select pin for timer0.

* @param[in] pol - select polarity for gpio trigger and gpio width

* @return none
*/

extern void timer0_gpio_init(GPIO_PinTypeDef pin, GPIO_PolTypeDef pol);


/**
* @brief set mode, initial tick and capture of timer0.

* @param[in] mode - select mode for timer0.

* @param[in] init_tick - initial tick.

* @param[in] cap_tick - tick of capture.

* @return none
*/

extern void timer0_set_mode(TIMER_ModeTypeDef mode,unsigned int init_tick, unsigned int cap_tick);

Also in ZigBee SDK in platform/Chip_8258/ folder timer.h has the same functions.


2. How does the code in the example in B85_driver works?

timer setup:

#elif(TIMER_MODE==TIMER_GPIO_TRIGGER_MODE)

/**** timer0 POL_FALLING TIMER_GPIO link LED3 **/

timer0_gpio_init(TIMER_GPIO, POL_FALLING);

irq_enable();

timer0_set_mode(TIMER_MODE_GPIO_TRIGGER,0,3);

timer_start(TIMER0);


and in interrupt handler:

if(timer_get_interrupt_status(FLD_TMR_STA_TMR0))
{
  timer_clear_interrupt_status(FLD_TMR_STA_TMR0); //clear irq status
  timer0_irq_cnt ++;
  gpio_toggle(LED2);
}


3. How does the timer know which gpio should trigger it?

@param[in] pin - select pin for timer0.

Refer to my comment above for setting register m0.

But I can t see anything linking this pin to the timer in timer.c code

I can only see in setting the mode:

reg_tmr0_capt = cap_tick;

reg_tmr_ctrl16 &= (~FLD_TMR0_MODE);

reg_tmr_ctrl16 |= (TIMER_MODE_GPIO_TRIGGER<<1);


4. If it works, how to implement it in ZigBee SDK interrupt handler. As I wrote before, there is a different way of handling interrupts in ZigBee SDK?


ZigBee SDK using drv_timer.h (drv_timer.c) functions

In drv_timer.h there is only 1 function:
/**
* @brief Initialize the specified hardware timer

*
* @param[in] tmrIdx - Index of timer @ref hardware_timer_index

* @param[in] mode - Specify the timer running mode @ref hardware_timer_mode

*
* @return None

*/

void drv_hwTmr_init(u8 tmrIdx, u8 mode); This function still calls timer init function in timer.c

There is no function to set the gpio pin to trigger the timer?


5. How do timer modes TIMER_MODE_GPIO or TIMER_MODE_WIDTH_GPIO work if there is no gpio pin assigned to trigger the timer and you need to set gpio trigger polarity - as per datasheet.

I don't believe the ZigBee SDK timer support those modes.


6. How to do it?

I can t see how Timer modes gpio trigger or gpio width can work. There is no example code showing how to do it?




TL_Soyo May 21, 2024 13:41
0
/A

Please refer to the driver SDK for accuracy, and you can also refer to B91's driver handbook of this feature.http://wiki.telink-semi.cn/wiki/chip-series/TLSR921x-Series/



wes58 [Author] May 22, 2024 06:29
0
/A

Hi Soyo,

What are you talking about? Your answer has nothing to do with any of my questions.

TL_Soyo May 22, 2024 09:57
0
/A

This document explains how the GPIO Trigger Mode is triggered

https://wiki.telink-semi.cn/doc/an/AN-21010600-E_Telink%20Driver%20SDK%20Developer%20Handbook.pdf
10.2.2 GPIO Trigger Mode

wes58 [Author] May 22, 2024 13:03
0
/A

I read the datasheet, I read this document, I checked the timer.c and drv_timer.c but why don't you answer the questions?

I asked the questions because I can't find an answer or I can't understand something.


Question 3 was:

Q 3. How does the timer know which gpio should trigger it?

@param[in] pin - select pin for timer0.

Refer to my comment above for setting register m0 and Datasheet.

But I can t see anything linking this pin to the timer in timer.c code

I can only see in setting the mode:

reg_tmr0_capt = cap_tick;

reg_tmr_ctrl16 &= (~FLD_TMR0_MODE);

reg_tmr_ctrl16 |= (TIMER_MODE_GPIO_TRIGGER<<1);


5.1.3 Mode1 (GPIO Trigger Mode) In Mode 1, GPIO is employed as clock source. The “m0”/“m1”/“m2” register specifies the GPIO which generates counting signal for Timer0/Timer1/Timer2.

Note: Refer to Section 7.1.2 for corresponding “m0”, “m1”, “m2” and “Polarity” register address

7.1.2 Connection relationship between GPIO and related modules Counting (Mode 1) or control (Mode 2) signal for Timer0 = | ((input ^ polarity) & m0);

Q3.1 Where in the timer.c in function timer0_gpio_init(GPIO_PinTypeDef pin, GPIO_PolTypeDef pol) is register m0 set?


Question 5 was:

Q 5. How do timer modes TIMER_MODE_GPIO or TIMER_MODE_WIDTH_GPIO work if there is no gpio pin assigned to trigger the timer and you need to set gpio trigger polarity - as per datasheet.

I don't believe the ZigBee SDK timer support those modes.

ZigBee SDK using drv_timer.c has only one function void drv_hwTmr_init(u8 tmrIdx, u8 mode); This function still calls timer init function in timer.c 

There is no function to set the gpio pin to trigger the timer?

How can it work?


wes58 [Author] May 22, 2024 13:44
0
/A


Is this the call that sets register m0 BM_SET(reg_gpio_irq_risc0_en(pin), bit) in function timer0_gpio_init(GPIO_PinTypeDef pin, GPIO_PolTypeDef pol)?

BM_SET(reg_gpio_irq_risc0_en(pin), bit) is -> reg_gpio_irq_risc0_en(i) REG_ADDR8(0x5b8 + (i >> 8))



And there is no call for this function in drv_timer.c, so this timer mode doesn t work.