Hello, I'm an engineer living in Japan.
【Development environment】
"SDK:nRF Connect SDK v2.4.2"
"IDE:VSCode 1.83.1"
"PCA10040(nRF52832)" x 1 (central)
"PTM215B" EnOcean × 1 (peripheral)
"PTM535BZ" + "ECO260" × 1 (peripheral)
I am creating a program that uses k_timer to sound a buzzer.
In the old environment, SKD17.1.0, an application timer (app_timer_start) was used to sound the buzzer.
NCS uses k_timer since the application timer has been discontinued.
However, the timer was not working repeatedly and the expected buzzer could not be sounded.
There was a description of k_timer in the following DevZone article, so I referred to it, but it also only worked once.
Reference URL)
devzone.nordicsemi.com/.../386454
I tried k_timer in two ways
Method 1)
①struct k_timer m_buzzer_play;
②K_TIMER_DEFINE(m_buzzer_play, Buzzer_Play_handler, NULL);
③void Buzzer_Hz_handler(struct k_timer *timer_id)
{
GPIO port ON/OFF
}
④k_timer_start(&m_buzzer_hz, K_MSEC( 0.23 ), K_MSEC( 0.23 ));
Method 2)
①struct k_timer m_buzzer_hz;
②k_timer_init(&m_buzzer_hz, Buzzer_Hz_handler, NULL);
③void Buzzer_Hz_handler(struct k_timer *timer_id)
{
GPIO port ON/OFF
}
④k_timer_start(&m_buzzer_hz, K_MSEC( 0.23 ), K_MSEC( 0.23 ));
In both methods, the handler function was called once and worked, but I was unable to get it to work repeatedly.
In the code I referenced, there was a description that if you specify the time as the third argument,
the timer will run repeatedly, and if you specify "K_NO_WAIT", it will run only once.
Therefore, we believe that it is not a simple code error.
k_timer_start(&timer_a, K_MSEC(1000), K_NO_WAIT); // Will only fire once
k_timer_start(&timer_b, K_MSEC(1000), K_MSEC(1000)); // Will fire every second
Also, when I wrote a timer start in the handler as shown below, it worked continuously, but the buzzer did not sound correctly.
void Buzzer_Hz_handler(struct k_timer *timer_id)
{
GPIO port ON/OFF
k_timer_start(&m_buzzer_hz, K_MSEC( 0.23 ), K_MSEC( 0.23 ));
}
Question 1)
How can I make the timer operate at regular intervals?
It would be helpful if you could tell me the program code.
Question 2)
Please let me know if you understand why the code explained in the text cannot be repeated.
Thank you.