Initial commit for SensorSoftwareTest_

This commit is contained in:
Miguel Iglesias 2024-10-07 18:12:21 +02:00
commit 4d7f1a57fc
15 changed files with 581 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
# editors
*.swp
*~
# build
/build*/

16
CMakeLists.txt Normal file
View File

@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.20.0)
if(DEFINED CUSTOM_BOILERPLATE)
include(${CUSTOM_BOILERPLATE})
return()
endif()
set(COMMON_PATH ${CMAKE_CURRENT_LIST_DIR}/common)
include(${COMMON_PATH}/common.cmake)
GET_DEVICE_CONFIG_FILES(${BOARD} boards)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(SensorSoftwareTest_)
target_sources(app PRIVATE main.c)
target_include_directories(app PRIVATE common)

62
README.md Normal file
View File

@ -0,0 +1,62 @@
# TIMER counter {#timer_counter}
The sample demonstrates a basic functionality of the nrfx_timer driver in Timer and Counter modes.
## Requirements
The sample supports the following development kits:
| **Board** | **Support** |
|---------------------|:-----------:|
| nrf52dk_nrf52832 | Yes |
| nrf52833dk_nrf52833 | Yes |
| nrf52840dk_nrf52840 | Yes |
| nrf5340dk_nrf5340 | Yes |
| nrf9160dk_nrf9160 | Yes |
## Overview
Application initializes the nrfx_timer driver.
The @p timer_handler() is executed in the following situations:
* Regularly after a specified time (@p TIME_TO_WAIT_MS).
* When the counter value is equal to @p COUNTER_MAX_VAL.
> For more information, see **TIMER driver - nrfx documentation**.
## Wiring
To run this sample, no special configuration is needed.
You should monitor the output from the board to check if it is as expected.
## Building and running
To run this sample, build it for the appropriate board and then flash it as per instructions in [Building and running](@ref building_and_running) section.
## Sample output
You should see the following output:
```
- "Starting nrfx_timer basic counter example."
- "Time between timer ticks: 1000 ms"
- "Timer status: enabled"
- "Counter status: enabled"
- "Timer: 1"
- "Timer: 2"
- "Timer: 3"
- "Counter: 1 / 3"
- "Timer: 1"
- "Timer: 2"
- "Timer: 3"
- "Counter: 2 / 3"
- "Timer: 1"
- "Timer: 2"
- "Timer: 3"
- "Counter finished"
- "Counter: 3 / 3"
- "Timer status: disabled"
- "Counter status: disabled"
```
[//]: #
[Building and running]: <../../../README.md#building-and-running>

View File

@ -0,0 +1,7 @@
&timer0 {
status = "okay";
};
&timer1 {
status = "okay";
};

View File

@ -0,0 +1,7 @@
&timer0 {
status = "okay";
};
&timer1 {
status = "okay";
};

View File

@ -0,0 +1,7 @@
&timer0 {
status = "okay";
};
&timer1 {
status = "okay";
};

View File

@ -0,0 +1,7 @@
&timer0 {
status = "okay";
};
&timer1 {
status = "okay";
};

View File

@ -0,0 +1,11 @@
&dppic {
status = "okay";
};
&timer0 {
status = "okay";
};
&gpiote {
status = "okay";
};

View File

@ -0,0 +1,63 @@
/*
* This file is created to fulfill the requirement of a pinctrl-N
* property for nodes in the specific target overlay file.
*
* In the current implementation of samples, the specific peripheral
* driver instance is initialized at runtime with a configuration
* based on pin symbols defined in `nrfx_example.h`.
*
* To use values from specific property nodes from the device tree,
* appropriate values for the `psels` property must be provided
* instead of the dummy values defined in that file. Once done, these values
* can be accessed from the device tree through using the API implemented in
* `<zephyr/devicetree.h>` (see Zephyrs doc: Devicetree access from C/C++).
*
* Here is a sample of extracting node `psels` values to initialize a peripheral
* driver instance with a configuration based on those values:
*
* #define SPI_NODE DT_NODELABEL(spi1)
* #define SPI_PINCTRL_NODE DT_CHILD(DT_PINCTRL_0(SPI_NODE, 0), group1)
* #define SCK_PIN (DT_PROP_BY_IDX(SPI_PINCTRL_NODE, psels, 0) & 0x3F)
* #define MISO_PIN (DT_PROP_BY_IDX(SPI_PINCTRL_NODE, psels, 1) & 0x3F)
* #define MOSI_PIN (DT_PROP_BY_IDX(SPI_PINCTRL_NODE, psels, 2) & 0x3F)
*/
&pinctrl {
spi_dummy: spi_dummy {
group1 {
psels = <NRF_PSEL(SPIM_SCK, 0, 0)>,
<NRF_PSEL(SPIM_MISO, 0, 0)>,
<NRF_PSEL(SPIM_MOSI, 0, 0)>;
};
};
spi_master_default: spi_master_default {
group1 {
psels = <NRF_PSEL(SPIM_SCK, 0, 0)>,
<NRF_PSEL(SPIM_MISO, 0, 0)>,
<NRF_PSEL(SPIM_MOSI, 0, 0)>;
};
};
spi_slave_dummy: spi_slave_dummy {
group1 {
psels = <NRF_PSEL(SPIM_SCK, 0, 0)>,
<NRF_PSEL(SPIM_MISO, 0, 0)>,
<NRF_PSEL(SPIM_MOSI, 0, 0)>;
};
};
i2c_master_dummy: i2c_master_dummy {
group1 {
psels = <NRF_PSEL(TWIM_SDA, 0, 0)>,
<NRF_PSEL(TWIM_SCL, 0, 0)>;
};
};
i2c_slave_dummy: i2c_slave_dummy {
group1 {
psels = <NRF_PSEL(TWIM_SDA, 0, 0)>,
<NRF_PSEL(TWIM_SCL, 0, 0)>;
};
};
};

33
common/common.cmake Normal file
View File

@ -0,0 +1,33 @@
# File containing common functions, macros and variables used across all examples.
# This file should be included in each CMakeLists.txt if compiled with west tool under Zephyr.
# Add common Kconfig file to project.
list(APPEND CONF_FILE "${CMAKE_CURRENT_LIST_DIR}/common.conf")
# Add project Kconfig file if it exists.
set(PRJ_CONF_FILE "${CMAKE_CURRENT_SOURCE_DIR}/prj.conf")
if(EXISTS ${PRJ_CONF_FILE})
list(APPEND CONF_FILE ${PRJ_CONF_FILE})
endif()
# Macro adding overlay file and Kconfig file for specified board in given directory.
# If board-specific files have been found they are added to DTC_OVERLAY_FILE and CONF_FILE
# symbols that are used in west build system.
macro(GET_DEVICE_CONFIG_FILES BOARD BOARDS_DIR)
if(NOT IS_ABSOLUTE BOARDS_DIR)
set(_BOARDS_DIR ${CMAKE_CURRENT_LIST_DIR}/${BOARDS_DIR})
get_filename_component(BOARDS_DIR_ABSOLUTE "${_BOARDS_DIR}" ABSOLUTE)
else()
set(BOARDS_DIR_ABSOLUTE {BOARDS_DIR})
endif()
set(OVERLAY_FILE "${BOARDS_DIR_ABSOLUTE}/${BOARD}.overlay")
if(EXISTS ${OVERLAY_FILE})
list(APPEND DTC_OVERLAY_FILE "${OVERLAY_FILE}")
endif()
set(CONFIG_FILE "${BOARDS_DIR_ABSOLUTE}/${BOARD}.conf")
if(EXISTS ${CONFIG_FILE})
list(APPEND CONF_FILE "${CONFIG_FILE}")
endif()
endmacro()

4
common/common.conf Normal file
View File

@ -0,0 +1,4 @@
CONFIG_LOG=y
CONFIG_BOOT_BANNER=n
CONFIG_ASSERT=y
CONFIG_LOG_PROCESS_THREAD=n

109
common/nrfx_example.h Normal file
View File

@ -0,0 +1,109 @@
/*
* Copyright (c) 2022 - 2024, Nordic Semiconductor ASA
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef NRFX_EXAMPLE_H__
#define NRFX_EXAMPLE_H__
#if defined(__ZEPHYR__)
#include <zephyr/logging/log_ctrl.h>
#define NRFX_EXAMPLE_LOG_INIT() LOG_INIT()
#define NRFX_EXAMPLE_LOG_PROCESS() while (LOG_PROCESS())
#else
#define NRFX_EXAMPLE_LOG_INIT()
#define NRFX_EXAMPLE_LOG_PROCESS()
#endif
#if defined(NRF52_SERIES)
#define LOOPBACK_PIN_1A 3
#define LOOPBACK_PIN_1B 31
#define LOOPBACK_PIN_2A 4
#define LOOPBACK_PIN_2B 30
#define LOOPBACK_PIN_3A 28
#define LOOPBACK_PIN_3B 29
#define LOOPBACK_PIN_4A 26
#define LOOPBACK_PIN_4B 27
#define ANALOG_INPUT_A0 1
#define ANALOG_INPUT_A1 2
#define ANALOG_INPUT_A2 4
#if defined(NRF52832_XXAA) || defined (NRF52832_XXAB)
#define LED1_PIN 17
#define LED2_PIN 18
#define LED3_PIN 19
#define LED4_PIN 20
#else
#define LED1_PIN 13
#define LED2_PIN 14
#define LED3_PIN 15
#define LED4_PIN 16
#endif
#elif defined(NRF53_SERIES)
#define LOOPBACK_PIN_1A 4
#define LOOPBACK_PIN_1B 26
#define LOOPBACK_PIN_2A 5
#define LOOPBACK_PIN_2B 25
#define LOOPBACK_PIN_3A 6
#define LOOPBACK_PIN_3B 7
#define LOOPBACK_PIN_4A 34
#define LOOPBACK_PIN_4B 35
#define ANALOG_INPUT_A0 0
#define ANALOG_INPUT_A1 1
#define ANALOG_INPUT_A2 2
#define LED1_PIN 28
#define LED2_PIN 29
#define LED3_PIN 30
#define LED4_PIN 31
#elif defined(NRF91_SERIES)
#define LOOPBACK_PIN_1A 14
#define LOOPBACK_PIN_1B 19
#define LOOPBACK_PIN_2A 15
#define LOOPBACK_PIN_2B 18
#define LOOPBACK_PIN_3A 16
#define LOOPBACK_PIN_3B 17
#define LOOPBACK_PIN_4A 30
#define LOOPBACK_PIN_4B 31
#define ANALOG_INPUT_A0 1
#define ANALOG_INPUT_A1 2
#define ANALOG_INPUT_A2 3
#define LED1_PIN 2
#define LED2_PIN 3
#define LED3_PIN 4
#define LED4_PIN 5
#else
#error "Unknown device."
#endif
#define ANALOG_INPUT_TO_SAADC_AIN(x) ((x) + 1)
#define ANALOG_INPUT_TO_COMP_AIN(x) (x)
#endif // NRFX_EXAMPLE_H__

207
main.c Normal file
View File

@ -0,0 +1,207 @@
/*
* Copyright (c) 2022 - 2024, Nordic Semiconductor ASA
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <nrfx_example.h>
#include <nrfx_timer.h>
#define NRFX_LOG_MODULE EXAMPLE
#define NRFX_EXAMPLE_CONFIG_LOG_ENABLED 1
#define NRFX_EXAMPLE_CONFIG_LOG_LEVEL 3
#include <nrfx_log.h>
/**
* @defgroup nrfx_timer_counter_example Counter example
* @{
* @ingroup nrfx_timer_examples
*
* @brief Example showing basic functionality of nrfx_timer driver in Timer Mode and Counter Mode.
*
* @details Application initializes nrfx_timer driver. The @ref timer_handler() is executed
* regularly after specified time ( @ref TIME_TO_WAIT_MS ). Moreover, @ref timer_handler()
* is executed when the counter value is equal to @ref COUNTER_MAX_VAL.
*/
/** @brief Symbol specifying timer instance to be used in timer mode (T). */
#define TIMER_T_INST_IDX 0
/** @brief Symbol specifying timer instance to be used in counter mode (C). */
#define TIMER_C_INST_IDX 1
/** @brief Symbol specifying time in milliseconds to wait for handler execution. */
#define TIME_TO_WAIT_MS 1000UL
/** @brief Symbol specifying maximum value of the timer. */
#define TIMER_MAX_VAL 3UL
/** @brief Symbol specifying maximum value of the counter. */
#define COUNTER_MAX_VAL 3UL
/** @brief Symbol specifying the initial value of the timer. */
#define TIMER_START_VAL 1UL
/** @brief Global variable that is used to increment the value of a counter. */
static volatile bool m_counter_inc_flag = false;
/**
* @brief Function for handling TIMER driver events.
*
* @param[in] event_type Timer event.
* @param[in] p_context General purpose parameter set during initialization of
* the timer. This parameter can be used to pass
* additional information to the handler function, for
* example, the timer ID. In this example p_context is used to
* pass address of Timer instance that calls this handler.
*/
static void timer_handler(nrf_timer_event_t event_type, void * p_context)
{
nrfx_timer_t * timer_inst = p_context;
static uint32_t timer_val = TIMER_START_VAL;
/* Creating timer driver instances to ensure that timer_X_inst.instance_id field matches
* timer_inst->instance_id. For example if user enables only timer2 with timer1 and timer0
* disabled, then timer2->instance_id == 0.
*/
nrfx_timer_t timer_t_inst = NRFX_TIMER_INSTANCE(TIMER_T_INST_IDX);
nrfx_timer_t timer_c_inst = NRFX_TIMER_INSTANCE(TIMER_C_INST_IDX);
if (timer_inst->instance_id == timer_t_inst.instance_id)
{
/* Configuring timer to count form TIMER_START_VAL to TIMER_MAX_VAL */
NRFX_LOG_INFO("Timer: %u", timer_val);
(timer_val == TIMER_MAX_VAL) ? timer_val = TIMER_START_VAL : timer_val++;
if (timer_val == TIMER_START_VAL)
{
m_counter_inc_flag = true;
}
}
else if (timer_inst->instance_id == timer_c_inst.instance_id)
{
NRFX_LOG_INFO("Counter finished");
}
}
/**
* @brief Function for application main entry.
*
* @return Nothing.
*/
int main(void)
{
nrfx_err_t status;
(void)status;
#if defined(__ZEPHYR__)
IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_TIMER_INST_GET(TIMER_T_INST_IDX)), IRQ_PRIO_LOWEST,
NRFX_TIMER_INST_HANDLER_GET(TIMER_T_INST_IDX), 0, 0);
IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_TIMER_INST_GET(TIMER_C_INST_IDX)), IRQ_PRIO_LOWEST,
NRFX_TIMER_INST_HANDLER_GET(TIMER_C_INST_IDX), 0, 0);
#endif
NRFX_EXAMPLE_LOG_INIT();
NRFX_LOG_INFO("Starting nrfx_timer basic counter example.");
NRFX_EXAMPLE_LOG_PROCESS();
/* Configuring timer instances, one in timer mode and one in counter mode.
* Assigning their own addresses to p_context variable - in order to reference them
* in timer_handler() function. Setting frequency to the base frequency value of the
* specified timer instance.
*/
nrfx_timer_t timer_t_inst = NRFX_TIMER_INSTANCE(TIMER_T_INST_IDX);
uint32_t base_frequency = NRF_TIMER_BASE_FREQUENCY_GET(timer_t_inst.p_reg);
nrfx_timer_config_t config = NRFX_TIMER_DEFAULT_CONFIG(base_frequency);
config.bit_width = NRF_TIMER_BIT_WIDTH_32;
config.p_context = &timer_t_inst;
status = nrfx_timer_init(&timer_t_inst, &config, timer_handler);
NRFX_ASSERT(status == NRFX_SUCCESS);
nrfx_timer_t timer_c_inst = NRFX_TIMER_INSTANCE(TIMER_C_INST_IDX);
config.frequency = NRF_TIMER_BASE_FREQUENCY_GET(timer_c_inst.p_reg);
config.mode = NRF_TIMER_MODE_COUNTER;
config.p_context = &timer_c_inst;
status = nrfx_timer_init(&timer_c_inst, &config, timer_handler);
NRFX_ASSERT(status == NRFX_SUCCESS);
NRFX_LOG_INFO("Time between timer ticks: %lu ms", TIME_TO_WAIT_MS);
/* Creating variable desired_ticks to store the output of nrfx_timer_ms_to_ticks function. */
uint32_t desired_ticks = nrfx_timer_ms_to_ticks(&timer_t_inst, TIME_TO_WAIT_MS);
/* Setting the timer (in timer mode) channel NRF_TIMER_CC_CHANNEL0 in the extended compare
* mode to clear the timer and trigger an interrupt if internal counter register is equal
* to desired_ticks.
*/
nrfx_timer_extended_compare(&timer_t_inst, NRF_TIMER_CC_CHANNEL0, desired_ticks,
NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
/* Setting the timer (in counter mode) channel NRF_TIMER_CC_CHANNEL0 in the extended compare
* mode to stop the timer and trigger an interrupt if internal counter register is equal
* to COUNTER_MAX_VAL.
*/
nrfx_timer_extended_compare(&timer_c_inst, NRF_TIMER_CC_CHANNEL0, COUNTER_MAX_VAL,
NRF_TIMER_SHORT_COMPARE0_STOP_MASK, true);
nrfx_timer_enable(&timer_c_inst);
nrfx_timer_enable(&timer_t_inst);
NRFX_LOG_INFO("Timer status: %s",
nrfx_timer_is_enabled(&timer_t_inst) ? "enabled" : "disabled");
NRFX_LOG_INFO("Counter status: %s",
nrfx_timer_is_enabled(&timer_c_inst) ? "enabled" : "disabled");
uint32_t timer_c_curr_val = 0;
while (1)
{
if (m_counter_inc_flag)
{
nrfx_timer_increment(&timer_c_inst);
timer_c_curr_val = nrfx_timer_capture(&timer_c_inst, NRF_TIMER_CC_CHANNEL1);
NRFX_LOG_INFO("Counter: %u / %lu", timer_c_curr_val, COUNTER_MAX_VAL);
m_counter_inc_flag = false;
if (timer_c_curr_val >= COUNTER_MAX_VAL)
{
nrfx_timer_disable(&timer_t_inst);
nrfx_timer_disable(&timer_c_inst);
NRFX_LOG_INFO("Timer status: %s",
nrfx_timer_is_enabled(&timer_t_inst) ? "enabled" : "disabled");
NRFX_LOG_INFO("Counter status: %s",
nrfx_timer_is_enabled(&timer_c_inst) ? "enabled" : "disabled");
}
}
NRFX_EXAMPLE_LOG_PROCESS();
}
}
/** @} */

2
prj.conf Normal file
View File

@ -0,0 +1,2 @@
CONFIG_NRFX_TIMER0=y
CONFIG_NRFX_TIMER1=y

40
sample.yaml Normal file
View File

@ -0,0 +1,40 @@
sample:
description: An example to showcase usage of the nrfx_timer driver when timer is in counter mode.
name: nrfx_timer counter mode example
tests:
examples.nrfx_timer.counter:
tags: timer
filter: dt_compat_enabled("nordic,nrf-timer")
platform_allow: |
nrf52dk_nrf52832 nrf52833dk_nrf52833 nrf52840dk_nrf52840
nrf5340dk_nrf5340_cpuapp nrf9160dk_nrf9160
integration_platforms:
- nrf52dk_nrf52832
- nrf52833dk_nrf52833
- nrf52840dk_nrf52840
- nrf5340dk_nrf5340_cpuapp
- nrf9160dk_nrf9160
harness: console
harness_config:
type: multi_line
ordered: true
regex:
- "Starting nrfx_timer basic counter example."
- "Time between timer ticks: 1000 ms"
- "Timer status: enabled"
- "Counter status: enabled"
- "Timer: 1"
- "Timer: 2"
- "Timer: 3"
- "Counter: 1 / 3"
- "Timer: 1"
- "Timer: 2"
- "Timer: 3"
- "Counter: 2 / 3"
- "Timer: 1"
- "Timer: 2"
- "Timer: 3"
- "Counter finished"
- "Counter: 3 / 3"
- "Timer status: disabled"
- "Counter status: disabled"