From 9f40270e88065c99a0a276559903343048c9f731 Mon Sep 17 00:00:00 2001 From: neotje Date: Sat, 22 Oct 2022 19:25:06 +0200 Subject: [PATCH 1/6] nrfx_temp --- .../nRF5/nordic/nrfx/drivers/src/nrfx_temp.c | 146 ++++++++++++++++++ cores/nRF5/nordic/nrfx_config.h | 4 + 2 files changed, 150 insertions(+) create mode 100644 cores/nRF5/nordic/nrfx/drivers/src/nrfx_temp.c diff --git a/cores/nRF5/nordic/nrfx/drivers/src/nrfx_temp.c b/cores/nRF5/nordic/nrfx/drivers/src/nrfx_temp.c new file mode 100644 index 000000000..98ebb2125 --- /dev/null +++ b/cores/nRF5/nordic/nrfx/drivers/src/nrfx_temp.c @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2019 - 2020, Nordic Semiconductor ASA + * All rights reserved. + * + * 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 + +#if NRFX_CHECK(NRFX_TEMP_ENABLED) + +#include + +#if !defined(USE_WORKAROUND_FOR_TEMP_OFFSET_ANOMALY) && defined(NRF51) +// Enable workaround for nRF51 series anomaly 28 +// (TEMP: Temperature offset value has to be manually loaded to the TEMP module). +#define USE_WORKAROUND_FOR_TEMP_OFFSET_ANOMALY 1 +#endif + +/** @brief Time of one check attempt.*/ +#define NRFX_TEMP_TIME_US 4 + +/** @brief Maximum attempts to check whether conversion passed.*/ +#define NRFX_TEMP_ATTEMPTS 10 + +/** @brief Internal state of TEMP driver. */ +static nrfx_drv_state_t m_temp_state; + +/** @brief Pointer to handler to be called from interrupt routine. */ +static nrfx_temp_data_handler_t m_data_handler; + +nrfx_err_t nrfx_temp_init(nrfx_temp_config_t const * p_config, nrfx_temp_data_handler_t handler) +{ + NRFX_ASSERT(p_config); + + if (m_temp_state != NRFX_DRV_STATE_UNINITIALIZED) + { + return NRFX_ERROR_ALREADY_INITIALIZED; + } + +#if NRFX_CHECK(USE_WORKAROUND_FOR_TEMP_OFFSET_ANOMALY) + *(uint32_t volatile *)0x4000C504 = 0; +#endif + + m_data_handler = handler; + + if (m_data_handler) + { + nrf_temp_int_enable(NRF_TEMP, NRF_TEMP_INT_DATARDY_MASK); + NRFX_IRQ_PRIORITY_SET(TEMP_IRQn, p_config->interrupt_priority); + NRFX_IRQ_ENABLE(TEMP_IRQn); + } + + m_temp_state = NRFX_DRV_STATE_INITIALIZED; + return NRFX_SUCCESS; +} + +void nrfx_temp_uninit(void) +{ + NRFX_ASSERT(m_temp_state == NRFX_DRV_STATE_INITIALIZED); + nrf_temp_task_trigger(NRF_TEMP, NRF_TEMP_TASK_STOP); + + if (m_data_handler) + { + nrf_temp_int_disable(NRF_TEMP, NRF_TEMP_INT_DATARDY_MASK); + NRFX_IRQ_DISABLE(TEMP_IRQn); + } + + m_temp_state = NRFX_DRV_STATE_UNINITIALIZED; +} + +int32_t nrfx_temp_calculate(int32_t raw_measurement) +{ + /* Raw temperature is a 2's complement signed value. Moreover, it is represented + * by 0.25[C] intervals, so division by 4 is needed. To preserve + * fractional part, raw value is multiplied by 100 before division.*/ + + return (raw_measurement * 100) / 4; +} + +nrfx_err_t nrfx_temp_measure(void) +{ + NRFX_ASSERT(m_temp_state == NRFX_DRV_STATE_INITIALIZED); + + nrfx_err_t result = NRFX_SUCCESS; + nrf_temp_event_clear(NRF_TEMP, NRF_TEMP_EVENT_DATARDY); + nrf_temp_task_trigger(NRF_TEMP, NRF_TEMP_TASK_START); + + if (!m_data_handler) + { + bool ev_result; + NRFX_WAIT_FOR(nrf_temp_event_check(NRF_TEMP, NRF_TEMP_EVENT_DATARDY), + NRFX_TEMP_ATTEMPTS, + NRFX_TEMP_TIME_US, + ev_result); + if (!ev_result) + { + result = NRFX_ERROR_INTERNAL; + } + else + { + nrf_temp_event_clear(NRF_TEMP, NRF_TEMP_EVENT_DATARDY); + } + nrf_temp_task_trigger(NRF_TEMP, NRF_TEMP_TASK_STOP); + } + + return result; +} + +void nrfx_temp_irq_handler(void) +{ + NRFX_ASSERT(m_data_handler); + + nrf_temp_task_trigger(NRF_TEMP, NRF_TEMP_TASK_STOP); + nrf_temp_event_clear(NRF_TEMP, NRF_TEMP_EVENT_DATARDY); + + uint32_t raw_temp = nrfx_temp_result_get(); + + m_data_handler(raw_temp); +} + +#endif // NRFX_CHECK(NRFX_TEMP_ENABLED) diff --git a/cores/nRF5/nordic/nrfx_config.h b/cores/nRF5/nordic/nrfx_config.h index 0c1a9a77b..da4a37018 100644 --- a/cores/nRF5/nordic/nrfx_config.h +++ b/cores/nRF5/nordic/nrfx_config.h @@ -34,5 +34,9 @@ #define NRFX_SPIM3_ENABLED 0 #endif +// NRFX temp +#define NRFX_TEMP_ENABLED 1 +#define NRFX_TEMP_DEFAULT_CONFIG_IRQ_PRIORITY 7 + #endif // NRFX_CONFIG_H__ From 1ecc92f012702533527260162cf8ece55a1cce0c Mon Sep 17 00:00:00 2001 From: neotje Date: Sat, 22 Oct 2022 23:19:00 +0200 Subject: [PATCH 2/6] TEMP only enabled for nRF52840 --- cores/nRF5/nordic/nrfx_config.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cores/nRF5/nordic/nrfx_config.h b/cores/nRF5/nordic/nrfx_config.h index da4a37018..9ffbe52ba 100644 --- a/cores/nRF5/nordic/nrfx_config.h +++ b/cores/nRF5/nordic/nrfx_config.h @@ -35,8 +35,9 @@ #endif // NRFX temp -#define NRFX_TEMP_ENABLED 1 -#define NRFX_TEMP_DEFAULT_CONFIG_IRQ_PRIORITY 7 - +#ifdef NRF52840_XXAA + #define NRFX_TEMP_ENABLED 1 + #define NRFX_TEMP_DEFAULT_CONFIG_IRQ_PRIORITY 7 +#endif #endif // NRFX_CONFIG_H__ From 68a1443840017d29eccb59a291d124fefa3c6726 Mon Sep 17 00:00:00 2001 From: neotje Date: Sun, 23 Oct 2022 12:25:37 +0200 Subject: [PATCH 3/6] All the socs supported by this bsp have the TEMP peripheral --- cores/nRF5/nordic/nrfx_config.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cores/nRF5/nordic/nrfx_config.h b/cores/nRF5/nordic/nrfx_config.h index 9ffbe52ba..db8091c42 100644 --- a/cores/nRF5/nordic/nrfx_config.h +++ b/cores/nRF5/nordic/nrfx_config.h @@ -35,9 +35,7 @@ #endif // NRFX temp -#ifdef NRF52840_XXAA - #define NRFX_TEMP_ENABLED 1 - #define NRFX_TEMP_DEFAULT_CONFIG_IRQ_PRIORITY 7 -#endif +#define NRFX_TEMP_ENABLED 1 +#define NRFX_TEMP_DEFAULT_CONFIG_IRQ_PRIORITY 7 #endif // NRFX_CONFIG_H__ From c0ad3cf6f06f702eb722df7b1a352177f99a5b4c Mon Sep 17 00:00:00 2001 From: neotje Date: Sun, 23 Oct 2022 12:45:03 +0200 Subject: [PATCH 4/6] nrfx_temp non-blocking example --- .../nrfx_temp_non-blocking.ino | 24 +++++++++++++++++++ libraries/nrfx_examples/library.properties | 9 +++++++ 2 files changed, 33 insertions(+) create mode 100644 libraries/nrfx_examples/examples/nrfx_temp_non-blocking/nrfx_temp_non-blocking.ino create mode 100644 libraries/nrfx_examples/library.properties diff --git a/libraries/nrfx_examples/examples/nrfx_temp_non-blocking/nrfx_temp_non-blocking.ino b/libraries/nrfx_examples/examples/nrfx_temp_non-blocking/nrfx_temp_non-blocking.ino new file mode 100644 index 000000000..a180e1c99 --- /dev/null +++ b/libraries/nrfx_examples/examples/nrfx_temp_non-blocking/nrfx_temp_non-blocking.ino @@ -0,0 +1,24 @@ +/* + This is example usage of the nrfx_temp driver +*/ + +#include +#include + +nrfx_temp_config_t config = NRFX_TEMP_DEFAULT_CONFIG; + +// this function is called when the temperature measurement is ready +void temp_handler(int32_t raw_temp) { + Serial.println(nrfx_temp_calculate(raw_temp)); +} + +void setup() { + Serial.begin(9600); + nrfx_temp_init(&config, &temp_handler); +} + +void loop() { + nrfx_temp_measure(); + + delay(1000); +} diff --git a/libraries/nrfx_examples/library.properties b/libraries/nrfx_examples/library.properties new file mode 100644 index 000000000..88677392a --- /dev/null +++ b/libraries/nrfx_examples/library.properties @@ -0,0 +1,9 @@ +name=nrfx examples +version=1.0 +author=Neotje +maintainer=Neotje +sentence=Examples for the Adafruit nRF52 Arduino platform using nrfx +paragraph=Examples for the Adafruit nRF52 Arduino platform using nrfx +category=other +url=https://github.com/adafruit/Adafruit_nRF52_Arduino +architectures=* From ab6229051e1f72f4ac3312924150ae7d3e94d587 Mon Sep 17 00:00:00 2001 From: neotje Date: Sun, 23 Oct 2022 13:24:35 +0200 Subject: [PATCH 5/6] nrfx_temp blocking example --- .../nrfx_temp_blocking/nrfx_temp_blocking.ino | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 libraries/nrfx_examples/examples/nrfx_temp_blocking/nrfx_temp_blocking.ino diff --git a/libraries/nrfx_examples/examples/nrfx_temp_blocking/nrfx_temp_blocking.ino b/libraries/nrfx_examples/examples/nrfx_temp_blocking/nrfx_temp_blocking.ino new file mode 100644 index 000000000..9d15880b9 --- /dev/null +++ b/libraries/nrfx_examples/examples/nrfx_temp_blocking/nrfx_temp_blocking.ino @@ -0,0 +1,22 @@ +/* + This is example usage of the nrfx_temp driver +*/ + +#include +#include + +nrfx_temp_config_t config = NRFX_TEMP_DEFAULT_CONFIG; + +void setup() { + Serial.begin(9600); + // setting the handler to NULL will initialize the driver in blocking mode + nrfx_temp_init(&config, NULL); +} + +void loop() { + nrfx_temp_measure(); // In blocking mode: this function waits until the measurement is finished. + int32_t raw_temp = nrfx_temp_result_get(); + Serial.println(nrfx_temp_calculate(raw_temp)); + + delay(1000); +} From 145041c4e9a4b843b21d9040bfc848e0064fc6a0 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 30 Nov 2023 16:34:15 +0700 Subject: [PATCH 6/6] move example to Bluefruit52Lib/Hardware, update example with float for readability --- .../temp_measure_blocking/temp_measure_blocking.ino} | 5 ++++- .../temp_measure_non_blocking.ino} | 4 +++- libraries/nrfx_examples/library.properties | 9 --------- 3 files changed, 7 insertions(+), 11 deletions(-) rename libraries/{nrfx_examples/examples/nrfx_temp_blocking/nrfx_temp_blocking.ino => Bluefruit52Lib/examples/Hardware/temp_measure_blocking/temp_measure_blocking.ino} (85%) rename libraries/{nrfx_examples/examples/nrfx_temp_non-blocking/nrfx_temp_non-blocking.ino => Bluefruit52Lib/examples/Hardware/temp_measure_non_blocking/temp_measure_non_blocking.ino} (83%) delete mode 100644 libraries/nrfx_examples/library.properties diff --git a/libraries/nrfx_examples/examples/nrfx_temp_blocking/nrfx_temp_blocking.ino b/libraries/Bluefruit52Lib/examples/Hardware/temp_measure_blocking/temp_measure_blocking.ino similarity index 85% rename from libraries/nrfx_examples/examples/nrfx_temp_blocking/nrfx_temp_blocking.ino rename to libraries/Bluefruit52Lib/examples/Hardware/temp_measure_blocking/temp_measure_blocking.ino index 9d15880b9..426af9e46 100644 --- a/libraries/nrfx_examples/examples/nrfx_temp_blocking/nrfx_temp_blocking.ino +++ b/libraries/Bluefruit52Lib/examples/Hardware/temp_measure_blocking/temp_measure_blocking.ino @@ -16,7 +16,10 @@ void setup() { void loop() { nrfx_temp_measure(); // In blocking mode: this function waits until the measurement is finished. int32_t raw_temp = nrfx_temp_result_get(); - Serial.println(nrfx_temp_calculate(raw_temp)); + float temp_c = raw_temp / 4.0; + + Serial.print(temp_c); + Serial.println(" C"); delay(1000); } diff --git a/libraries/nrfx_examples/examples/nrfx_temp_non-blocking/nrfx_temp_non-blocking.ino b/libraries/Bluefruit52Lib/examples/Hardware/temp_measure_non_blocking/temp_measure_non_blocking.ino similarity index 83% rename from libraries/nrfx_examples/examples/nrfx_temp_non-blocking/nrfx_temp_non-blocking.ino rename to libraries/Bluefruit52Lib/examples/Hardware/temp_measure_non_blocking/temp_measure_non_blocking.ino index a180e1c99..e55947ef6 100644 --- a/libraries/nrfx_examples/examples/nrfx_temp_non-blocking/nrfx_temp_non-blocking.ino +++ b/libraries/Bluefruit52Lib/examples/Hardware/temp_measure_non_blocking/temp_measure_non_blocking.ino @@ -9,7 +9,9 @@ nrfx_temp_config_t config = NRFX_TEMP_DEFAULT_CONFIG; // this function is called when the temperature measurement is ready void temp_handler(int32_t raw_temp) { - Serial.println(nrfx_temp_calculate(raw_temp)); + float temp_c = raw_temp / 4.0; + Serial.print(temp_c); + Serial.println(" C"); } void setup() { diff --git a/libraries/nrfx_examples/library.properties b/libraries/nrfx_examples/library.properties deleted file mode 100644 index 88677392a..000000000 --- a/libraries/nrfx_examples/library.properties +++ /dev/null @@ -1,9 +0,0 @@ -name=nrfx examples -version=1.0 -author=Neotje -maintainer=Neotje -sentence=Examples for the Adafruit nRF52 Arduino platform using nrfx -paragraph=Examples for the Adafruit nRF52 Arduino platform using nrfx -category=other -url=https://github.com/adafruit/Adafruit_nRF52_Arduino -architectures=*