samples/drivers: Provides basic sample for counter alarm

Provide basic sample to exercise counter alarm API.

Signed-off-by: Erwan Gouriou <erwan.gouriou@linaro.org>
This commit is contained in:
Erwan Gouriou 2018-11-30 10:17:22 +01:00 committed by Anas Nashif
commit da744314ca
5 changed files with 143 additions and 0 deletions

View file

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.13.1)
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(counter)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

View file

@ -0,0 +1,52 @@
.. _alarm:
counter alarm sample
#####################
Overview
********
This sample provides an example of alarm application using counter API.
It sets an alarm with an initial delay of 2 seconds. At each alarm
expiry, a new alarm is configured with a delay multiplied by 2.
Requirements
************
This sample requires the support of a timer IP compatible with alarm setting.
References
**********
- :ref:`disco_l475_iot1`
Building and Running
********************
.. zephyr-app-commands::
:zephyr-app: samples/counter/alarm
:host-os: unix
:board: disco_l475_iot1
:goals: run
:compact:
Sample Output
=============
.. code-block:: console
Counter alarm sample
Set alarm in 2 sec
!!! Alarm !!!
Now: 2
Set alarm in 4 sec
!!! Alarm !!!
Now: 6
Set alarm in 8 sec
!!! Alarm !!!
Now: 14
Set alarm in 16 sec
!!! Alarm !!!
Now: 30
<repeats endlessly>

View file

@ -0,0 +1,2 @@
CONFIG_PRINTK=y
CONFIG_COUNTER=y

View file

@ -0,0 +1,17 @@
sample:
name: Counter RTC Driver Sample
tests:
test:
tags: drivers
harness: console
platform_whitelist: nucleo_f746zg
harness_config:
type: multi_line
ordered: true
regex:
- "Counter alarm sample"
-
- "Set alarm in 2 sec"
- "!!! Alarm !!!"
- "Now: 2"
depends_on: counter

View file

@ -0,0 +1,66 @@
/*
* Copyright (c) 2019 Linaro Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <device.h>
#include <counter.h>
#include <misc/printk.h>
#define DELAY 2000000
#define ALARM_CHANNEL_ID 0
struct counter_alarm_cfg alarm_cfg;
static void test_counter_interrupt_fn(struct device *counter_dev,
u8_t chan_id, u32_t ticks, void *user_data)
{
u32_t now_ticks = counter_read(counter_dev);
u64_t now_usec = counter_ticks_to_us(counter_dev, now_ticks);
int now_sec = (int)(now_usec / USEC_PER_SEC);
struct counter_alarm_cfg *config = user_data;
printk("!!! Alarm !!!\n");
printk("Now: %d\n", now_sec);
/* Set a new alarm with a double lenght duration */
config->ticks = 2 * config->ticks;
printk("Set alarm in %d sec\n", config->ticks);
counter_set_channel_alarm(counter_dev, ALARM_CHANNEL_ID, user_data);
}
void main(void)
{
struct device *counter_dev;
int err = 0;
printk("Counter alarm sample\n\n");
counter_dev = device_get_binding(DT_RTC_0_NAME);
counter_start(counter_dev);
alarm_cfg.absolute = false;
alarm_cfg.ticks = counter_us_to_ticks(counter_dev, DELAY);
alarm_cfg.callback = test_counter_interrupt_fn;
alarm_cfg.user_data = &alarm_cfg;
err = counter_set_channel_alarm(counter_dev, ALARM_CHANNEL_ID,
&alarm_cfg);
printk("Set alarm in %d sec\n", alarm_cfg.ticks);
if (-EINVAL == err) {
printk("Alarm settings invalid\n");
} else if (-ENOTSUP == err) {
printk("Alarm setting request not supported\n");
} else if (err != 0) {
printk("Error\n");
}
while (1) {
k_sleep(K_FOREVER);
}
}