This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nRF9160 Timer issue

Dear Sir.

I am using the Timer of nRF916 .

In a simple project the timer is working fine ( see attached code ).

A buzzer is connected to PWM output pin 12.

I can here the Buzzer beeps as expected .

/*
 * Copyright (c) 2016 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */
#include <nrf9160.h>
#include <zephyr.h>
#include <device.h>
#include <drivers/pwm.h>
#include <drivers/gpio.h>

//#define LED		12
#define PWM_IO		12
struct device *pwm_dev;

/* 1000 msec = 1 sec */
#define SLEEP_TIME	100

struct k_timer bz_timer;

void bz_func(struct k_timer *timer_id) 
{      
    static unsigned char k = 0;
    static unsigned int time = 0;
 
     // Siren
//     if(time == 0)
//       pwm_pin_set_usec(pwm_dev,PWM_IO, 260, 130,0);
//     if(time == 10)
//      pwm_pin_set_usec(pwm_dev,PWM_IO, 400, 200,0);
//     if(++time >= 20)
//       time = 0;

     // Short Beep
     if(time == 0)
       pwm_pin_set_usec(pwm_dev,PWM_IO, 260, 130,0);
     if(time == 7)
       pwm_pin_set_usec(pwm_dev,PWM_IO, 400, 0,0);
     if(++time >= 100)
       time = 0;

     // Long Beep
//     if(time == 0)
//       pwm_pin_set_usec(pwm_dev,PWM_IO, 260, 130,0);
//     if(time == 20)
//       pwm_pin_set_usec(pwm_dev,PWM_IO, 400, 0,0);
//     if(++time >= 150)
//       time = 0;

       // Train
//       if(k < 100)
//       {
//         if(time == 0)
//           pwm_pin_set_usec(pwm_dev,PWM_IO, 260, 130,0);
//         if(time == 5)
//           pwm_pin_set_usec(pwm_dev,PWM_IO, 400, 0,0);
//         if(++time >= 20)
//         {
//           time = 0;
//           if(++k > 20) 
//            k = 100;
//         }
//       }
}

void main(void)
{
	u32_t cnt = 0;
	struct device *dev_io;
        

        printk("PWM Application has started!\r\n");

        pwm_dev = device_get_binding("PWM_0");     
        if (!pwm_dev) {
		printk("Cannot find %s!\n", "PWM_0");        
		return;
	}

        k_timer_init(&bz_timer, bz_func, NULL);
        k_timer_start(&bz_timer, K_SECONDS(1), K_MSEC(50));

        while(1)
        {
            k_sleep(K_MSEC(800));
        }
}

In another larger project , I insert the Timer code , and it behaves erratically .

The time between each entry to the timer is seconds.

What can cause this behavior ?.

Please Advise

struct k_timer bz_timer;


void bz_func(struct k_timer *timer_id) 
{
  
    unsigned char k;
     static unsigned int time = 0;

   
     // Siren
     if(time == 0)
       pwm_pin_set_usec(pwm_dev,PWM_IO, 260, 130,0);
     if(time == 10)
      pwm_pin_set_usec(pwm_dev,PWM_IO, 400, 200,0);
     if(++time >= 20)
       time = 0;

}


void main(void)
{
      int err;
      static unsigned int i = 0;

 
      k_work_init(&work_top, work_top_func);
      k_work_init(&work_spi, spi_work_func);    
      k_work_init(&work_adp5360, adp5360_work_func);

     

      initStructures();
       // Ephraim 26_5_2020
       _AFE4900_AQUIRE_MODE.aquire_mode =  afe4900_aquire_sample;

      k_timer_init(&bz_timer, bz_func, NULL);
      k_timer_start(&bz_timer, K_SECONDS(1), K_MSEC(50));

  
      io_init();
      pwm_init();
      
 
      while(1) {
 
          k_sleep(K_MSEC(2000));
   
          k_sleep(K_MSEC(2000));
   
    
      };

Parents Reply Children
  • Hi.

    One more thing 

    /* install my_isr() as interrupt handler for the device (not shown) */
    ...

    How do I set thid handler ?.

    Thanks
  • Hi,

     

    In this case, my_isr() is equivalent to your k_timer handler (bz_func).

     

    Kind regards,

    Håkon

  • Hi.

    I stripped all unnecessary code .

    Still the same error ( reset over and over.)

    Please Advise

    /*
     * Copyright (c) 2018 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
     */
    
    #include <zephyr.h>
    #include <stdio.h>
    #include <drivers/uart.h>
    #include <string.h>
    #include <stdlib.h>
    #include <main.h>
    
    #include <drivers/adc.h>
    #include <drivers/spi.h>
    #include <drivers/i2c.h>
    #include <drivers/gpio.h>
    #include <drivers/pwm.h>
    //#include <drivers/timer.h>
    
    #include <net/mqtt.h>
    #include <net/socket.h>
    #include <lte_lc.h>
    #if defined(CONFIG_LWM2M_CARRIER)
    #include <lwm2m_carrier.h>
    #endif
    
    #include <kernel.h>
    #include <c:/zephyr/v1.2.0/zephyr/include/power/reboot.h>
    
    
    #include <MT25QL128ABA.h>
    #include <afe4900.h>
    #include <adp536x.h>
    #include <lis2dh12.h>
    #include <init.h>
    
    #include <hal/nrf_saadc.h>
    #include <stdarg.h>
     
    
    #define TIEMR_INTERVAL_SEC 1
    #define TIEMR_INTERVAL_mSEC 10
     
    struct k_timer bz_timer;
    
     
    struct device * pwm_dev; 
    
    
    
    #define MY_STACK_SIZE 512
    #define MY_PRIORITY 5
    K_THREAD_STACK_DEFINE(my_stack_area, MY_STACK_SIZE);
    struct k_work_q my_work_q;
    
    struct device_info {
        struct k_work work;
        char name[16]
    } my_device;
    
      
      
    
      
    
    void bz_func(struct k_timer *timer_id) 
    {
         k_work_submit(&my_device.work);
    }
    
    
    void activate_buzzer(struct k_work *item)
    {
         unsigned char k;
         static unsigned int time = 0;
    
        struct device_info *the_device =
            CONTAINER_OF(item, struct device_info, work);
       
       
       
         if(time == 0)
         {
           printk("Fisrt PWM set \n" );
           pwm_pin_set_usec(pwm_dev,PWM_IO, 260, 130,0);
           SetLed(BLUE,ON); 
           SetLed(GREEN,OFF); 
         }
        if(time == 10)
        {
           printk("Second PWM set \n" );
           pwm_pin_set_usec(pwm_dev,PWM_IO, 400, 200,0);
           SetLed(BLUE,OFF); 
           SetLed(GREEN,ON); 
         }
        if(++time >= 20)
           time = 0;
    }
    
    
    
    void main(void)
    {
          int err;
          static unsigned int i = 0;
     
    
     
      //    initStructures();
      
      //     _AFE4900_AQUIRE_MODE.aquire_mode =  afe4900_aquire_sample;
    
          k_timer_init(&bz_timer, bz_func, NULL);
          k_timer_start(&bz_timer, K_SECONDS(1), K_MSEC(100));
    
          k_work_q_start(&my_work_q, my_stack_area,
                  K_THREAD_STACK_SIZEOF(my_stack_area), MY_PRIORITY); 
    
          strcpy(my_device.name, "FOO_dev");
          k_work_init(&my_device.work, activate_buzzer);
    
    
    
          io_init();
          pwm_init();
          
    
     
          while(1) {
    
              k_sleep(K_MSEC(2000));
        
          };
    }
    
      

  • Hi,

     

    Could you try starting the timer instance after initializing io_init and pwm_init?

     

    Kind regards,

    Håkon

Related