No ACL setup yet! Denying access to everyone.
en:edu:zmitac:s2:aih:lab:nrf52zephyrtimers

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:edu:zmitac:s2:aih:lab:nrf52zephyrtimers [2023/12/06 23:01] – [Zephyr timers and work queues] ktokarzen:edu:zmitac:s2:aih:lab:nrf52zephyrtimers [2023/12/06 23:05] (current) – [Timer use] ktokarz
Line 13: Line 13:
 The timer object must be defined and initialized. Its function is automatically added to the system work queue as the task, so we don't need to do anything with the queue. Let's try to modify the blinky code from the first laboratory on GPIOs to add a second LED blinking as the result of timer expiration. First, we need to add the timer object declaration. The timer object must be defined and initialized. Its function is automatically added to the system work queue as the task, so we don't need to do anything with the queue. Let's try to modify the blinky code from the first laboratory on GPIOs to add a second LED blinking as the result of timer expiration. First, we need to add the timer object declaration.
 <code> <code>
-        struct k_timer led_timer;+struct k_timer led_timer;
 </code> </code>
 Next, we add the expiry function which will toggle the state of LED every call. Next, we add the expiry function which will toggle the state of LED every call.
 <code> <code>
 void led_timer_function(struct k_timer *timer_id) void led_timer_function(struct k_timer *timer_id)
- { +{ 
- gpio_pin_set(led_dev, LED1_PIN, (int)led1_is_on); +   gpio_pin_set(led_dev, LED1_PIN, (int)led1_is_on); 
- led1_is_on = !led1_is_on; +   led1_is_on = !led1_is_on; 
- }+}
 </code> </code>
 In the main, we have to add two functions: one to initialize the timer, and a second for starting it. In the main, we have to add two functions: one to initialize the timer, and a second for starting it.
 <code> <code>
- k_timer_init(&led_timer, led_timer_function, NULL); +k_timer_init(&led_timer, led_timer_function, NULL); 
- k_timer_start(&led_timer, K_MSEC(200), K_MSEC(300));+k_timer_start(&led_timer, K_MSEC(200), K_MSEC(300));
 </code> </code>
 Of course, there is a need to add and configure a second LED. This can be done as LED0 in the blinky example so we are not going to repeat the detailed description here. Of course, there is a need to add and configure a second LED. This can be done as LED0 in the blinky example so we are not going to repeat the detailed description here.
Line 74: Line 74:
 #if DT_NODE_HAS_STATUS(LED0_NODE, okay) #if DT_NODE_HAS_STATUS(LED0_NODE, okay)
 #define LED0 DT_GPIO_LABEL(LED0_NODE, gpios) #define LED0 DT_GPIO_LABEL(LED0_NODE, gpios)
-#define PIN     DT_GPIO_PIN(LED0_NODE, gpios)+#define PIN DT_GPIO_PIN(LED0_NODE, gpios)
 #define FLAGS DT_GPIO_FLAGS(LED0_NODE, gpios) #define FLAGS DT_GPIO_FLAGS(LED0_NODE, gpios)
 #else #else
Line 157: Line 157:
 then the function for the work item must be defined: then the function for the work item must be defined:
 <code> <code>
-void my_work_item_function(struct k_work *my_work_item){ +void my_work_item_function(struct k_work *my_work_item) 
- printk("Work item executed\n");+{ 
 +   printk("Work item executed\n");
 } }
 </code> </code>
Line 178: Line 179:
 <code> <code>
 for (int i=0; i<40; i++)  for (int i=0; i<40; i++) 
-   { +{ 
-      printk("counter = %d \n", i); +   printk("counter = %d \n", i); 
-   }+}
 </code> </code>
 3. Observe the results while adjusting the max iteration value, and explain observations in the report.\\ 3. Observe the results while adjusting the max iteration value, and explain observations in the report.\\
-4. Add at the end of led_timer_function the line shown below and observe the behaviour of the program. <code> k_msleep(1); </code>+4. Add at the end of led_timer_function the line shown below and observe the behaviour of the program.  
 +<code>k_msleep(1);</code>
 5. Define two work items with their functions. One for the code from the timer function, and the second for the code from the main function. Instead of executing the full code in the timer and main function submit corresponding work items. The code for the timer work item can look as follows (the code for the main function as well as the work items definition and initialization write on your own). Experiment with the maximal iteration value in the "for" loop. Describe observation results in the report. 5. Define two work items with their functions. One for the code from the timer function, and the second for the code from the main function. Instead of executing the full code in the timer and main function submit corresponding work items. The code for the timer work item can look as follows (the code for the main function as well as the work items definition and initialization write on your own). Experiment with the maximal iteration value in the "for" loop. Describe observation results in the report.
 <code> <code>
Line 197: Line 199:
    for (int i=0; i<40; i++)     for (int i=0; i<40; i++) 
    {    {
- printk("counter = %d \n", i); +       printk("counter = %d \n", I); 
- k_msleep(1);+       k_msleep(1);
    }    }
 } }
en/edu/zmitac/s2/aih/lab/nrf52zephyrtimers.1701900117.txt.gz · Last modified: by ktokarz