Add missing nrfx source files
This commit is contained in:
1 parent
1eae55ee87
commit
1a1eef6761
4 files changed
+1744
No files matched your search
@@ -0,0 +1,878 @@
|
||||
/*
|
||||
* Copyright (c) 2015 - 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 <nrfx.h>
|
||||
|
||||
#if NRFX_CHECK(NRFX_GPIOTE_ENABLED)
|
||||
|
||||
#include <nrfx_gpiote.h>
|
||||
#include "nrf_bitmask.h"
|
||||
#include <string.h>
|
||||
|
||||
#define NRFX_LOG_MODULE GPIOTE
|
||||
#include <nrfx_log.h>
|
||||
|
||||
|
||||
#define FORBIDDEN_HANDLER_ADDRESS ((nrfx_gpiote_evt_handler_t)UINT32_MAX)
|
||||
#define PIN_NOT_USED (-1)
|
||||
#define PIN_USED (-2)
|
||||
#define NO_CHANNELS (-1)
|
||||
#define POLARITY_FIELD_POS (6)
|
||||
#define POLARITY_FIELD_MASK (0xC0)
|
||||
|
||||
/* Check if every pin can be encoded on provided number of bits. */
|
||||
NRFX_STATIC_ASSERT(NUMBER_OF_PINS <= (1 << POLARITY_FIELD_POS));
|
||||
|
||||
/*lint -save -e571*/ /* Suppress "Warning 571: Suspicious cast" */
|
||||
typedef struct
|
||||
{
|
||||
nrfx_gpiote_evt_handler_t handlers[GPIOTE_CH_NUM + NRFX_GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS];
|
||||
int8_t pin_assignments[NUMBER_OF_PINS];
|
||||
int8_t port_handlers_pins[NRFX_GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS];
|
||||
uint8_t configured_pins[((NUMBER_OF_PINS)+7) / 8];
|
||||
nrfx_drv_state_t state;
|
||||
} gpiote_control_block_t;
|
||||
|
||||
static gpiote_control_block_t m_cb;
|
||||
|
||||
static bool pin_in_use(uint32_t pin)
|
||||
{
|
||||
return (m_cb.pin_assignments[pin] != PIN_NOT_USED);
|
||||
}
|
||||
|
||||
|
||||
static bool pin_in_use_as_non_task_out(uint32_t pin)
|
||||
{
|
||||
return (m_cb.pin_assignments[pin] == PIN_USED);
|
||||
}
|
||||
|
||||
|
||||
static bool pin_in_use_by_te(uint32_t pin)
|
||||
{
|
||||
return (m_cb.pin_assignments[pin] >= 0 && m_cb.pin_assignments[pin] < GPIOTE_CH_NUM) ?
|
||||
true : false;
|
||||
}
|
||||
|
||||
|
||||
static bool pin_in_use_by_port(uint32_t pin)
|
||||
{
|
||||
return (m_cb.pin_assignments[pin] >= GPIOTE_CH_NUM);
|
||||
}
|
||||
|
||||
|
||||
static bool pin_in_use_by_gpiote(uint32_t pin)
|
||||
{
|
||||
return (m_cb.pin_assignments[pin] >= 0);
|
||||
}
|
||||
|
||||
|
||||
static void pin_in_use_by_te_set(uint32_t pin,
|
||||
uint32_t channel_id,
|
||||
nrfx_gpiote_evt_handler_t handler,
|
||||
bool is_channel)
|
||||
{
|
||||
m_cb.pin_assignments[pin] = channel_id;
|
||||
m_cb.handlers[channel_id] = handler;
|
||||
if (!is_channel)
|
||||
{
|
||||
m_cb.port_handlers_pins[channel_id - GPIOTE_CH_NUM] = (int8_t)pin;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void pin_in_use_set(uint32_t pin)
|
||||
{
|
||||
m_cb.pin_assignments[pin] = PIN_USED;
|
||||
}
|
||||
|
||||
|
||||
static void pin_in_use_clear(uint32_t pin)
|
||||
{
|
||||
m_cb.pin_assignments[pin] = PIN_NOT_USED;
|
||||
}
|
||||
|
||||
|
||||
static void pin_configured_set(uint32_t pin)
|
||||
{
|
||||
nrf_bitmask_bit_set(pin, m_cb.configured_pins);
|
||||
}
|
||||
|
||||
static void pin_configured_clear(uint32_t pin)
|
||||
{
|
||||
nrf_bitmask_bit_clear(pin, m_cb.configured_pins);
|
||||
}
|
||||
|
||||
static bool pin_configured_check(uint32_t pin)
|
||||
{
|
||||
return 0 != nrf_bitmask_bit_is_set(pin, m_cb.configured_pins);
|
||||
}
|
||||
|
||||
static int8_t channel_port_get(uint32_t pin)
|
||||
{
|
||||
return m_cb.pin_assignments[pin];
|
||||
}
|
||||
|
||||
|
||||
static nrfx_gpiote_evt_handler_t channel_handler_get(uint32_t channel)
|
||||
{
|
||||
return m_cb.handlers[channel];
|
||||
}
|
||||
|
||||
static nrfx_gpiote_pin_t port_handler_pin_get(uint32_t handler_idx)
|
||||
{
|
||||
uint8_t pin_and_polarity = (uint8_t)m_cb.port_handlers_pins[handler_idx];
|
||||
return (nrfx_gpiote_pin_t)(pin_and_polarity & ~POLARITY_FIELD_MASK);
|
||||
}
|
||||
|
||||
static nrf_gpiote_polarity_t port_handler_polarity_get(uint32_t handler_idx)
|
||||
{
|
||||
uint8_t pin_and_polarity = (uint8_t)m_cb.port_handlers_pins[handler_idx];
|
||||
return (nrf_gpiote_polarity_t)((pin_and_polarity & POLARITY_FIELD_MASK) >> POLARITY_FIELD_POS);
|
||||
}
|
||||
|
||||
static int8_t channel_port_alloc(uint32_t pin, nrfx_gpiote_evt_handler_t handler, bool channel)
|
||||
{
|
||||
int8_t channel_id = NO_CHANNELS;
|
||||
uint32_t i;
|
||||
|
||||
uint32_t start_idx = channel ? 0 : GPIOTE_CH_NUM;
|
||||
uint32_t end_idx =
|
||||
channel ? GPIOTE_CH_NUM : (GPIOTE_CH_NUM + NRFX_GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS);
|
||||
|
||||
// critical section
|
||||
|
||||
for (i = start_idx; i < end_idx; i++)
|
||||
{
|
||||
if (m_cb.handlers[i] == FORBIDDEN_HANDLER_ADDRESS)
|
||||
{
|
||||
pin_in_use_by_te_set(pin, i, handler, channel);
|
||||
channel_id = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// critical section
|
||||
return channel_id;
|
||||
}
|
||||
|
||||
|
||||
static void channel_free(uint8_t channel_id)
|
||||
{
|
||||
m_cb.handlers[channel_id] = FORBIDDEN_HANDLER_ADDRESS;
|
||||
if (channel_id >= GPIOTE_CH_NUM)
|
||||
{
|
||||
m_cb.port_handlers_pins[channel_id - GPIOTE_CH_NUM] = (int8_t)PIN_NOT_USED;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
nrfx_err_t nrfx_gpiote_init(uint8_t interrupt_priority)
|
||||
{
|
||||
nrfx_err_t err_code;
|
||||
|
||||
if (m_cb.state != NRFX_DRV_STATE_UNINITIALIZED)
|
||||
{
|
||||
err_code = NRFX_ERROR_INVALID_STATE;
|
||||
NRFX_LOG_WARNING("Function: %s, error code: %s.",
|
||||
__func__,
|
||||
NRFX_LOG_ERROR_STRING_GET(err_code));
|
||||
return err_code;
|
||||
}
|
||||
|
||||
uint8_t i;
|
||||
|
||||
for (i = 0; i < NUMBER_OF_PINS; i++)
|
||||
{
|
||||
pin_in_use_clear(i);
|
||||
}
|
||||
|
||||
for (i = 0; i < (GPIOTE_CH_NUM + NRFX_GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS); i++)
|
||||
{
|
||||
channel_free(i);
|
||||
}
|
||||
|
||||
memset(m_cb.configured_pins, 0, sizeof(m_cb.configured_pins));
|
||||
|
||||
NRFX_IRQ_PRIORITY_SET(nrfx_get_irq_number(NRF_GPIOTE), interrupt_priority);
|
||||
NRFX_IRQ_ENABLE(nrfx_get_irq_number(NRF_GPIOTE));
|
||||
nrf_gpiote_event_clear(NRF_GPIOTE, NRF_GPIOTE_EVENT_PORT);
|
||||
nrf_gpiote_int_enable(NRF_GPIOTE, GPIOTE_INTENSET_PORT_Msk);
|
||||
m_cb.state = NRFX_DRV_STATE_INITIALIZED;
|
||||
|
||||
err_code = NRFX_SUCCESS;
|
||||
NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
|
||||
return err_code;
|
||||
}
|
||||
|
||||
|
||||
bool nrfx_gpiote_is_init(void)
|
||||
{
|
||||
return (m_cb.state != NRFX_DRV_STATE_UNINITIALIZED) ? true : false;
|
||||
}
|
||||
|
||||
|
||||
void nrfx_gpiote_uninit(void)
|
||||
{
|
||||
NRFX_ASSERT(m_cb.state != NRFX_DRV_STATE_UNINITIALIZED);
|
||||
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < NUMBER_OF_PINS; i++)
|
||||
{
|
||||
if (pin_in_use_as_non_task_out(i))
|
||||
{
|
||||
nrfx_gpiote_out_uninit(i);
|
||||
}
|
||||
else if ( pin_in_use_by_gpiote(i))
|
||||
{
|
||||
/* Disable gpiote_in is having the same effect on out pin as gpiote_out_uninit on
|
||||
* so it can be called on all pins used by GPIOTE.
|
||||
*/
|
||||
nrfx_gpiote_in_uninit(i);
|
||||
}
|
||||
}
|
||||
m_cb.state = NRFX_DRV_STATE_UNINITIALIZED;
|
||||
NRFX_LOG_INFO("Uninitialized.");
|
||||
}
|
||||
|
||||
|
||||
nrfx_err_t nrfx_gpiote_out_init(nrfx_gpiote_pin_t pin,
|
||||
nrfx_gpiote_out_config_t const * p_config)
|
||||
{
|
||||
NRFX_ASSERT(pin < NUMBER_OF_PINS);
|
||||
NRFX_ASSERT(m_cb.state == NRFX_DRV_STATE_INITIALIZED);
|
||||
NRFX_ASSERT(p_config);
|
||||
|
||||
nrfx_err_t err_code = NRFX_SUCCESS;
|
||||
|
||||
if (pin_in_use(pin))
|
||||
{
|
||||
err_code = NRFX_ERROR_BUSY;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (p_config->task_pin)
|
||||
{
|
||||
int8_t channel = channel_port_alloc(pin, NULL, true);
|
||||
|
||||
if (channel != NO_CHANNELS)
|
||||
{
|
||||
nrf_gpiote_task_configure(NRF_GPIOTE,
|
||||
(uint32_t)channel,
|
||||
pin,
|
||||
p_config->action,
|
||||
p_config->init_state);
|
||||
}
|
||||
else
|
||||
{
|
||||
err_code = NRFX_ERROR_NO_MEM;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
pin_in_use_set(pin);
|
||||
}
|
||||
|
||||
if (err_code == NRFX_SUCCESS)
|
||||
{
|
||||
if (p_config->init_state == NRF_GPIOTE_INITIAL_VALUE_HIGH)
|
||||
{
|
||||
nrf_gpio_pin_set(pin);
|
||||
}
|
||||
else
|
||||
{
|
||||
nrf_gpio_pin_clear(pin);
|
||||
}
|
||||
|
||||
nrf_gpio_cfg_output(pin);
|
||||
pin_configured_set(pin);
|
||||
}
|
||||
}
|
||||
|
||||
NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
|
||||
return err_code;
|
||||
}
|
||||
|
||||
|
||||
void nrfx_gpiote_out_uninit(nrfx_gpiote_pin_t pin)
|
||||
{
|
||||
NRFX_ASSERT(pin < NUMBER_OF_PINS);
|
||||
NRFX_ASSERT(pin_in_use(pin));
|
||||
|
||||
if (pin_in_use_by_te(pin))
|
||||
{
|
||||
channel_free((uint8_t)channel_port_get(pin));
|
||||
nrf_gpiote_te_default(NRF_GPIOTE, (uint32_t)channel_port_get(pin));
|
||||
}
|
||||
pin_in_use_clear(pin);
|
||||
|
||||
if (pin_configured_check(pin))
|
||||
{
|
||||
nrf_gpio_cfg_default(pin);
|
||||
pin_configured_clear(pin);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void nrfx_gpiote_out_set(nrfx_gpiote_pin_t pin)
|
||||
{
|
||||
NRFX_ASSERT(pin < NUMBER_OF_PINS);
|
||||
NRFX_ASSERT(pin_in_use(pin));
|
||||
NRFX_ASSERT(!pin_in_use_by_te(pin));
|
||||
|
||||
nrf_gpio_pin_set(pin);
|
||||
}
|
||||
|
||||
|
||||
void nrfx_gpiote_out_clear(nrfx_gpiote_pin_t pin)
|
||||
{
|
||||
NRFX_ASSERT(pin < NUMBER_OF_PINS);
|
||||
NRFX_ASSERT(pin_in_use(pin));
|
||||
NRFX_ASSERT(!pin_in_use_by_te(pin));
|
||||
|
||||
nrf_gpio_pin_clear(pin);
|
||||
}
|
||||
|
||||
|
||||
void nrfx_gpiote_out_toggle(nrfx_gpiote_pin_t pin)
|
||||
{
|
||||
NRFX_ASSERT(pin < NUMBER_OF_PINS);
|
||||
NRFX_ASSERT(pin_in_use(pin));
|
||||
NRFX_ASSERT(!pin_in_use_by_te(pin));
|
||||
|
||||
nrf_gpio_pin_toggle(pin);
|
||||
}
|
||||
|
||||
|
||||
void nrfx_gpiote_out_task_enable(nrfx_gpiote_pin_t pin)
|
||||
{
|
||||
NRFX_ASSERT(pin < NUMBER_OF_PINS);
|
||||
NRFX_ASSERT(pin_in_use(pin));
|
||||
NRFX_ASSERT(pin_in_use_by_te(pin));
|
||||
|
||||
nrf_gpiote_task_enable(NRF_GPIOTE, (uint32_t)m_cb.pin_assignments[pin]);
|
||||
}
|
||||
|
||||
|
||||
void nrfx_gpiote_out_task_disable(nrfx_gpiote_pin_t pin)
|
||||
{
|
||||
NRFX_ASSERT(pin < NUMBER_OF_PINS);
|
||||
NRFX_ASSERT(pin_in_use(pin));
|
||||
NRFX_ASSERT(pin_in_use_by_te(pin));
|
||||
|
||||
nrf_gpiote_task_disable(NRF_GPIOTE, (uint32_t)m_cb.pin_assignments[pin]);
|
||||
}
|
||||
|
||||
|
||||
nrf_gpiote_task_t nrfx_gpiote_out_task_get(nrfx_gpiote_pin_t pin)
|
||||
{
|
||||
NRFX_ASSERT(pin < NUMBER_OF_PINS);
|
||||
NRFX_ASSERT(pin_in_use_by_te(pin));
|
||||
|
||||
return nrf_gpiote_out_task_get((uint8_t)channel_port_get(pin));
|
||||
}
|
||||
|
||||
|
||||
uint32_t nrfx_gpiote_out_task_addr_get(nrfx_gpiote_pin_t pin)
|
||||
{
|
||||
nrf_gpiote_task_t task = nrfx_gpiote_out_task_get(pin);
|
||||
return nrf_gpiote_task_address_get(NRF_GPIOTE, task);
|
||||
}
|
||||
|
||||
|
||||
#if defined(GPIOTE_FEATURE_SET_PRESENT)
|
||||
nrf_gpiote_task_t nrfx_gpiote_set_task_get(nrfx_gpiote_pin_t pin)
|
||||
{
|
||||
NRFX_ASSERT(pin < NUMBER_OF_PINS);
|
||||
NRFX_ASSERT(pin_in_use_by_te(pin));
|
||||
|
||||
return nrf_gpiote_set_task_get((uint8_t)channel_port_get(pin));
|
||||
}
|
||||
|
||||
|
||||
uint32_t nrfx_gpiote_set_task_addr_get(nrfx_gpiote_pin_t pin)
|
||||
{
|
||||
nrf_gpiote_task_t task = nrfx_gpiote_set_task_get(pin);
|
||||
return nrf_gpiote_task_address_get(NRF_GPIOTE, task);
|
||||
}
|
||||
#endif // defined(GPIOTE_FEATURE_SET_PRESENT)
|
||||
|
||||
|
||||
#if defined(GPIOTE_FEATURE_CLR_PRESENT)
|
||||
nrf_gpiote_task_t nrfx_gpiote_clr_task_get(nrfx_gpiote_pin_t pin)
|
||||
{
|
||||
NRFX_ASSERT(pin < NUMBER_OF_PINS);
|
||||
NRFX_ASSERT(pin_in_use_by_te(pin));
|
||||
|
||||
return nrf_gpiote_clr_task_get((uint8_t)channel_port_get(pin));
|
||||
}
|
||||
|
||||
|
||||
uint32_t nrfx_gpiote_clr_task_addr_get(nrfx_gpiote_pin_t pin)
|
||||
{
|
||||
nrf_gpiote_task_t task = nrfx_gpiote_clr_task_get(pin);
|
||||
return nrf_gpiote_task_address_get(NRF_GPIOTE, task);
|
||||
}
|
||||
#endif // defined(GPIOTE_FEATURE_CLR_PRESENT)
|
||||
|
||||
|
||||
void nrfx_gpiote_out_task_force(nrfx_gpiote_pin_t pin, uint8_t state)
|
||||
{
|
||||
NRFX_ASSERT(pin < NUMBER_OF_PINS);
|
||||
NRFX_ASSERT(pin_in_use(pin));
|
||||
NRFX_ASSERT(pin_in_use_by_te(pin));
|
||||
|
||||
nrf_gpiote_outinit_t init_val =
|
||||
state ? NRF_GPIOTE_INITIAL_VALUE_HIGH : NRF_GPIOTE_INITIAL_VALUE_LOW;
|
||||
nrf_gpiote_task_force(NRF_GPIOTE, (uint32_t)m_cb.pin_assignments[pin], init_val);
|
||||
}
|
||||
|
||||
|
||||
void nrfx_gpiote_out_task_trigger(nrfx_gpiote_pin_t pin)
|
||||
{
|
||||
NRFX_ASSERT(pin < NUMBER_OF_PINS);
|
||||
NRFX_ASSERT(pin_in_use(pin));
|
||||
NRFX_ASSERT(pin_in_use_by_te(pin));
|
||||
|
||||
nrf_gpiote_task_t task = nrf_gpiote_out_task_get((uint8_t)channel_port_get(pin));
|
||||
nrf_gpiote_task_trigger(NRF_GPIOTE, task);
|
||||
}
|
||||
|
||||
|
||||
#if defined(GPIOTE_FEATURE_SET_PRESENT)
|
||||
void nrfx_gpiote_set_task_trigger(nrfx_gpiote_pin_t pin)
|
||||
{
|
||||
NRFX_ASSERT(pin < NUMBER_OF_PINS);
|
||||
NRFX_ASSERT(pin_in_use(pin));
|
||||
NRFX_ASSERT(pin_in_use_by_te(pin));
|
||||
|
||||
nrf_gpiote_task_t task = nrf_gpiote_set_task_get((uint8_t)channel_port_get(pin));
|
||||
nrf_gpiote_task_trigger(NRF_GPIOTE, task);
|
||||
}
|
||||
|
||||
|
||||
#endif // defined(GPIOTE_FEATURE_SET_PRESENT)
|
||||
|
||||
#if defined(GPIOTE_FEATURE_CLR_PRESENT)
|
||||
void nrfx_gpiote_clr_task_trigger(nrfx_gpiote_pin_t pin)
|
||||
{
|
||||
NRFX_ASSERT(pin < NUMBER_OF_PINS);
|
||||
NRFX_ASSERT(pin_in_use(pin));
|
||||
NRFX_ASSERT(pin_in_use_by_te(pin));
|
||||
|
||||
nrf_gpiote_task_t task = nrf_gpiote_clr_task_get((uint8_t)channel_port_get(pin));
|
||||
nrf_gpiote_task_trigger(NRF_GPIOTE, task);
|
||||
}
|
||||
|
||||
|
||||
#endif // defined(GPIOTE_FEATURE_CLR_PRESENT)
|
||||
|
||||
nrfx_err_t nrfx_gpiote_in_init(nrfx_gpiote_pin_t pin,
|
||||
nrfx_gpiote_in_config_t const * p_config,
|
||||
nrfx_gpiote_evt_handler_t evt_handler)
|
||||
{
|
||||
NRFX_ASSERT(pin < NUMBER_OF_PINS);
|
||||
NRFX_ASSERT(m_cb.state == NRFX_DRV_STATE_INITIALIZED);
|
||||
NRFX_ASSERT(p_config);
|
||||
|
||||
nrfx_err_t err_code = NRFX_SUCCESS;
|
||||
|
||||
/* Only one GPIOTE channel can be assigned to one physical pin. */
|
||||
if (pin_in_use_by_gpiote(pin))
|
||||
{
|
||||
err_code = NRFX_ERROR_BUSY;
|
||||
}
|
||||
else
|
||||
{
|
||||
int8_t channel = channel_port_alloc(pin, evt_handler, p_config->hi_accuracy);
|
||||
if (channel != NO_CHANNELS)
|
||||
{
|
||||
if (!p_config->skip_gpio_setup)
|
||||
{
|
||||
if (p_config->is_watcher)
|
||||
{
|
||||
nrf_gpio_cfg_watcher(pin);
|
||||
}
|
||||
else
|
||||
{
|
||||
nrf_gpio_cfg_input(pin, p_config->pull);
|
||||
}
|
||||
pin_configured_set(pin);
|
||||
}
|
||||
|
||||
if (p_config->hi_accuracy)
|
||||
{
|
||||
nrf_gpiote_event_configure(NRF_GPIOTE, (uint32_t)channel, pin, p_config->sense);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_cb.port_handlers_pins[channel - GPIOTE_CH_NUM] |= (p_config->sense) <<
|
||||
POLARITY_FIELD_POS;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
err_code = NRFX_ERROR_NO_MEM;
|
||||
}
|
||||
}
|
||||
|
||||
NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
|
||||
return err_code;
|
||||
}
|
||||
|
||||
void nrfx_gpiote_in_event_enable(nrfx_gpiote_pin_t pin, bool int_enable)
|
||||
{
|
||||
NRFX_ASSERT(pin < NUMBER_OF_PINS);
|
||||
NRFX_ASSERT(pin_in_use_by_gpiote(pin));
|
||||
if (pin_in_use_by_port(pin))
|
||||
{
|
||||
nrf_gpiote_polarity_t polarity =
|
||||
port_handler_polarity_get(channel_port_get(pin) - GPIOTE_CH_NUM);
|
||||
nrf_gpio_pin_sense_t sense;
|
||||
if (polarity == NRF_GPIOTE_POLARITY_TOGGLE)
|
||||
{
|
||||
/* read current pin state and set for next sense to oposit */
|
||||
sense = (nrf_gpio_pin_read(pin)) ?
|
||||
NRF_GPIO_PIN_SENSE_LOW : NRF_GPIO_PIN_SENSE_HIGH;
|
||||
}
|
||||
else
|
||||
{
|
||||
sense = (polarity == NRF_GPIOTE_POLARITY_LOTOHI) ?
|
||||
NRF_GPIO_PIN_SENSE_HIGH : NRF_GPIO_PIN_SENSE_LOW;
|
||||
}
|
||||
nrf_gpio_cfg_sense_set(pin, sense);
|
||||
}
|
||||
else if (pin_in_use_by_te(pin))
|
||||
{
|
||||
int32_t channel = (int32_t)channel_port_get(pin);
|
||||
nrf_gpiote_event_t event = nrf_gpiote_in_event_get((uint8_t)channel);
|
||||
|
||||
nrf_gpiote_event_enable(NRF_GPIOTE, (uint32_t)channel);
|
||||
|
||||
nrf_gpiote_event_clear(NRF_GPIOTE, event);
|
||||
if (int_enable)
|
||||
{
|
||||
nrfx_gpiote_evt_handler_t handler = channel_handler_get((uint32_t)channel_port_get(pin));
|
||||
// Enable the interrupt only if event handler was provided.
|
||||
if (handler)
|
||||
{
|
||||
nrf_gpiote_int_enable(NRF_GPIOTE, 1 << channel);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void nrfx_gpiote_in_event_disable(nrfx_gpiote_pin_t pin)
|
||||
{
|
||||
NRFX_ASSERT(pin < NUMBER_OF_PINS);
|
||||
NRFX_ASSERT(pin_in_use_by_gpiote(pin));
|
||||
if (pin_in_use_by_port(pin))
|
||||
{
|
||||
nrf_gpio_cfg_sense_set(pin, NRF_GPIO_PIN_NOSENSE);
|
||||
}
|
||||
else if (pin_in_use_by_te(pin))
|
||||
{
|
||||
int32_t channel = (int32_t)channel_port_get(pin);
|
||||
nrf_gpiote_event_disable(NRF_GPIOTE, (uint32_t)channel);
|
||||
nrf_gpiote_int_disable(NRF_GPIOTE, 1 << channel);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void nrfx_gpiote_in_uninit(nrfx_gpiote_pin_t pin)
|
||||
{
|
||||
NRFX_ASSERT(pin < NUMBER_OF_PINS);
|
||||
NRFX_ASSERT(pin_in_use_by_gpiote(pin));
|
||||
nrfx_gpiote_in_event_disable(pin);
|
||||
if (pin_in_use_by_te(pin))
|
||||
{
|
||||
nrf_gpiote_te_default(NRF_GPIOTE, (uint32_t)channel_port_get(pin));
|
||||
}
|
||||
if (pin_configured_check(pin))
|
||||
{
|
||||
nrf_gpio_cfg_default(pin);
|
||||
pin_configured_clear(pin);
|
||||
}
|
||||
channel_free((uint8_t)channel_port_get(pin));
|
||||
pin_in_use_clear(pin);
|
||||
}
|
||||
|
||||
|
||||
bool nrfx_gpiote_in_is_set(nrfx_gpiote_pin_t pin)
|
||||
{
|
||||
NRFX_ASSERT(pin < NUMBER_OF_PINS);
|
||||
return nrf_gpio_pin_read(pin) ? true : false;
|
||||
}
|
||||
|
||||
|
||||
nrf_gpiote_event_t nrfx_gpiote_in_event_get(nrfx_gpiote_pin_t pin)
|
||||
{
|
||||
NRFX_ASSERT(pin < NUMBER_OF_PINS);
|
||||
NRFX_ASSERT(pin_in_use_by_port(pin) || pin_in_use_by_te(pin));
|
||||
|
||||
if (pin_in_use_by_te(pin))
|
||||
{
|
||||
return nrf_gpiote_in_event_get((uint8_t)channel_port_get(pin));
|
||||
}
|
||||
|
||||
return NRF_GPIOTE_EVENT_PORT;
|
||||
}
|
||||
|
||||
|
||||
uint32_t nrfx_gpiote_in_event_addr_get(nrfx_gpiote_pin_t pin)
|
||||
{
|
||||
nrf_gpiote_event_t event = nrfx_gpiote_in_event_get(pin);
|
||||
return nrf_gpiote_event_address_get(NRF_GPIOTE, event);
|
||||
}
|
||||
|
||||
#if defined(NRF_GPIO_LATCH_PRESENT)
|
||||
static bool latch_pending_read_and_check(uint32_t * latch)
|
||||
{
|
||||
nrf_gpio_latches_read_and_clear(0, GPIO_COUNT, latch);
|
||||
|
||||
for (uint32_t port_idx = 0; port_idx < GPIO_COUNT; port_idx++)
|
||||
{
|
||||
if (latch[port_idx])
|
||||
{
|
||||
/* If any of the latch bits is still set, it means another edge has been captured
|
||||
* before or during the interrupt processing. Therefore event-processing loop
|
||||
* should be executed again. */
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void port_event_handle(uint32_t * latch)
|
||||
{
|
||||
do {
|
||||
for (uint32_t i = 0; i < NRFX_GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS; i++)
|
||||
{
|
||||
if (m_cb.port_handlers_pins[i] == PIN_NOT_USED)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Process pin further only if LATCH bit associated with this pin was set. */
|
||||
nrfx_gpiote_pin_t pin = port_handler_pin_get(i);
|
||||
if (nrf_bitmask_bit_is_set(pin, latch))
|
||||
{
|
||||
nrf_gpiote_polarity_t polarity = port_handler_polarity_get(i);
|
||||
nrf_gpio_pin_sense_t sense = nrf_gpio_pin_sense_get(pin);
|
||||
|
||||
NRFX_LOG_DEBUG("PORT event for pin: %d, polarity: %d.", pin, polarity);
|
||||
|
||||
/* Reconfigure sense to the opposite level, so the internal PINx.DETECT signal
|
||||
* can be deasserted. Therefore PORT event generated again,
|
||||
* unless some other PINx.DETECT signal is still active. */
|
||||
nrf_gpio_pin_sense_t next_sense =
|
||||
(sense == NRF_GPIO_PIN_SENSE_HIGH) ? NRF_GPIO_PIN_SENSE_LOW :
|
||||
NRF_GPIO_PIN_SENSE_HIGH;
|
||||
nrf_gpio_cfg_sense_set(pin, next_sense);
|
||||
|
||||
/* Try to clear LATCH bit corresponding to currently processed pin.
|
||||
* This may not succeed if the pin's state changed during the interrupt processing
|
||||
* and now it matches the new sense configuration. In such case,
|
||||
* the pin will be processed again in another iteration of the outer loop. */
|
||||
nrf_gpio_pin_latch_clear(pin);
|
||||
|
||||
/* Invoke user handler only if the sensed pin level
|
||||
* matches its polarity configuration. */
|
||||
nrfx_gpiote_evt_handler_t handler =
|
||||
channel_handler_get((uint32_t)channel_port_get(pin));
|
||||
if (handler &&
|
||||
((polarity == NRF_GPIOTE_POLARITY_TOGGLE) ||
|
||||
(sense == NRF_GPIO_PIN_SENSE_HIGH && polarity == NRF_GPIOTE_POLARITY_LOTOHI) ||
|
||||
(sense == NRF_GPIO_PIN_SENSE_LOW && polarity == NRF_GPIOTE_POLARITY_HITOLO)))
|
||||
{
|
||||
handler(pin, polarity);
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (latch_pending_read_and_check(latch));
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
static bool input_read_and_check(uint32_t * input, uint32_t * pins_to_check)
|
||||
{
|
||||
bool process_inputs_again;
|
||||
uint32_t new_input[GPIO_COUNT];
|
||||
|
||||
nrf_gpio_ports_read(0, GPIO_COUNT, new_input);
|
||||
|
||||
process_inputs_again = false;
|
||||
for (uint32_t port_idx = 0; port_idx < GPIO_COUNT; port_idx++)
|
||||
{
|
||||
/* Execute XOR to find out which inputs have changed. */
|
||||
uint32_t input_diff = input[port_idx] ^ new_input[port_idx];
|
||||
input[port_idx] = new_input[port_idx];
|
||||
if (input_diff)
|
||||
{
|
||||
/* If any differences among inputs were found, mark those pins
|
||||
* to be processed again. */
|
||||
pins_to_check[port_idx] = input_diff;
|
||||
process_inputs_again = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
pins_to_check[port_idx] = 0;
|
||||
}
|
||||
}
|
||||
return process_inputs_again;
|
||||
}
|
||||
|
||||
static void port_event_handle(uint32_t * input)
|
||||
{
|
||||
uint32_t pins_to_check[GPIO_COUNT];
|
||||
|
||||
for (uint32_t port_idx = 0; port_idx < GPIO_COUNT; port_idx++)
|
||||
{
|
||||
pins_to_check[port_idx] = 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
do {
|
||||
for (uint32_t i = 0; i < NRFX_GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS; i++)
|
||||
{
|
||||
if (m_cb.port_handlers_pins[i] == PIN_NOT_USED)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
nrfx_gpiote_pin_t pin = port_handler_pin_get(i);
|
||||
if (nrf_bitmask_bit_is_set(pin, pins_to_check))
|
||||
{
|
||||
nrf_gpiote_polarity_t polarity = port_handler_polarity_get(i);
|
||||
nrf_gpio_pin_sense_t sense = nrf_gpio_pin_sense_get(pin);
|
||||
bool pin_state = nrf_bitmask_bit_is_set(pin, input);
|
||||
|
||||
/* Process pin further only if its state matches its sense level. */
|
||||
if ((pin_state && (sense == NRF_GPIO_PIN_SENSE_HIGH)) ||
|
||||
(!pin_state && (sense == NRF_GPIO_PIN_SENSE_LOW)) )
|
||||
{
|
||||
/* Reconfigure sense to the opposite level, so the internal PINx.DETECT signal
|
||||
* can be deasserted. Therefore PORT event can be generated again,
|
||||
* unless some other PINx.DETECT signal is still active. */
|
||||
NRFX_LOG_DEBUG("PORT event for pin: %d, polarity: %d.", pin, polarity);
|
||||
nrf_gpio_pin_sense_t next_sense =
|
||||
(sense == NRF_GPIO_PIN_SENSE_HIGH) ? NRF_GPIO_PIN_SENSE_LOW :
|
||||
NRF_GPIO_PIN_SENSE_HIGH;
|
||||
nrf_gpio_cfg_sense_set(pin, next_sense);
|
||||
|
||||
/* Invoke user handler only if the sensed pin level
|
||||
* matches its polarity configuration. */
|
||||
nrfx_gpiote_evt_handler_t handler =
|
||||
channel_handler_get((uint32_t)channel_port_get(pin));
|
||||
if (handler &&
|
||||
((polarity == NRF_GPIOTE_POLARITY_TOGGLE) ||
|
||||
(sense == NRF_GPIO_PIN_SENSE_HIGH &&
|
||||
polarity == NRF_GPIOTE_POLARITY_LOTOHI) ||
|
||||
(sense == NRF_GPIO_PIN_SENSE_LOW &&
|
||||
polarity == NRF_GPIOTE_POLARITY_HITOLO)))
|
||||
{
|
||||
handler(pin, polarity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (input_read_and_check(input, pins_to_check));
|
||||
}
|
||||
#endif // defined(NRF_GPIO_LATCH_PRESENT)
|
||||
|
||||
void nrfx_gpiote_irq_handler(void)
|
||||
{
|
||||
uint32_t status = 0;
|
||||
uint32_t input[GPIO_COUNT] = {0};
|
||||
|
||||
/* collect status of all GPIOTE pin events. Processing is done once all are collected and cleared.*/
|
||||
uint32_t i;
|
||||
nrf_gpiote_event_t event = NRF_GPIOTE_EVENT_IN_0;
|
||||
uint32_t mask = (uint32_t)NRF_GPIOTE_INT_IN0_MASK;
|
||||
|
||||
for (i = 0; i < GPIOTE_CH_NUM; i++)
|
||||
{
|
||||
if (nrf_gpiote_event_check(NRF_GPIOTE, event) &&
|
||||
nrf_gpiote_int_enable_check(NRF_GPIOTE, mask))
|
||||
{
|
||||
nrf_gpiote_event_clear(NRF_GPIOTE, event);
|
||||
status |= mask;
|
||||
}
|
||||
mask <<= 1;
|
||||
/* Incrementing to next event, utilizing the fact that events are grouped together
|
||||
* in ascending order. */
|
||||
event = (nrf_gpiote_event_t)((uint32_t)event + sizeof(uint32_t));
|
||||
}
|
||||
|
||||
/* collect PORT status event, if event is set read pins state. Processing is postponed to the
|
||||
* end of interrupt. */
|
||||
if (nrf_gpiote_event_check(NRF_GPIOTE, NRF_GPIOTE_EVENT_PORT))
|
||||
{
|
||||
nrf_gpiote_event_clear(NRF_GPIOTE, NRF_GPIOTE_EVENT_PORT);
|
||||
status |= (uint32_t)NRF_GPIOTE_INT_PORT_MASK;
|
||||
#if defined(NRF_GPIO_LATCH_PRESENT)
|
||||
nrf_gpio_latches_read_and_clear(0, GPIO_COUNT, input);
|
||||
#else
|
||||
nrf_gpio_ports_read(0, GPIO_COUNT, input);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Process pin events. */
|
||||
if (status & NRF_GPIOTE_INT_IN_MASK)
|
||||
{
|
||||
mask = (uint32_t)NRF_GPIOTE_INT_IN0_MASK;
|
||||
|
||||
for (i = 0; i < GPIOTE_CH_NUM; i++)
|
||||
{
|
||||
if (mask & status)
|
||||
{
|
||||
nrfx_gpiote_pin_t pin = nrf_gpiote_event_pin_get(NRF_GPIOTE, i);
|
||||
NRFX_LOG_DEBUG("Event in number: %d.", i);
|
||||
nrf_gpiote_polarity_t polarity = nrf_gpiote_event_polarity_get(NRF_GPIOTE, i);
|
||||
nrfx_gpiote_evt_handler_t handler = channel_handler_get(i);
|
||||
NRFX_LOG_DEBUG("Pin: %d, polarity: %d.", pin, polarity);
|
||||
if (handler)
|
||||
{
|
||||
handler(pin, polarity);
|
||||
}
|
||||
}
|
||||
mask <<= 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Process PORT event. */
|
||||
if (status & (uint32_t)NRF_GPIOTE_INT_PORT_MASK)
|
||||
{
|
||||
port_event_handle(input);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*lint -restore*/
|
||||
#endif // NRFX_CHECK(NRFX_GPIOTE_ENABLED)
|
||||
@@ -0,0 +1,517 @@
|
||||
/*
|
||||
* Copyright (c) 2015 - 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 <nrfx.h>
|
||||
|
||||
#if NRFX_CHECK(NRFX_PWM_ENABLED)
|
||||
|
||||
#if !(NRFX_CHECK(NRFX_PWM0_ENABLED) || NRFX_CHECK(NRFX_PWM1_ENABLED) || \
|
||||
NRFX_CHECK(NRFX_PWM2_ENABLED) || NRFX_CHECK(NRFX_PWM3_ENABLED))
|
||||
#error "No enabled PWM instances. Check <nrfx_config.h>."
|
||||
#endif
|
||||
|
||||
#include <nrfx_pwm.h>
|
||||
#include <hal/nrf_gpio.h>
|
||||
|
||||
#define NRFX_LOG_MODULE PWM
|
||||
#include <nrfx_log.h>
|
||||
|
||||
#if NRFX_CHECK(NRFX_PWM_NRF52_ANOMALY_109_WORKAROUND_ENABLED)
|
||||
// The workaround uses interrupts to wake up the CPU and ensure it is active
|
||||
// when PWM is about to start a DMA transfer. For initial transfer, done when
|
||||
// a playback is started via PPI, a specific EGU instance is used to generate
|
||||
// an interrupt. During the playback, the PWM interrupt triggered on SEQEND
|
||||
// event of a preceding sequence is used to protect the transfer done for
|
||||
// the next sequence to be played.
|
||||
#include <hal/nrf_egu.h>
|
||||
#define USE_DMA_ISSUE_WORKAROUND
|
||||
#endif
|
||||
#if defined(USE_DMA_ISSUE_WORKAROUND)
|
||||
#define EGU_IRQn(i) EGU_IRQn_(i)
|
||||
#define EGU_IRQn_(i) SWI##i##_EGU##i##_IRQn
|
||||
#define EGU_IRQHandler(i) EGU_IRQHandler_(i)
|
||||
#define EGU_IRQHandler_(i) nrfx_egu_##i##_irq_handler
|
||||
#define DMA_ISSUE_EGU_IDX NRFX_PWM_NRF52_ANOMALY_109_EGU_INSTANCE
|
||||
#define DMA_ISSUE_EGU NRFX_CONCAT_2(NRF_EGU, DMA_ISSUE_EGU_IDX)
|
||||
#define DMA_ISSUE_EGU_IRQn EGU_IRQn(DMA_ISSUE_EGU_IDX)
|
||||
#define DMA_ISSUE_EGU_IRQHandler EGU_IRQHandler(DMA_ISSUE_EGU_IDX)
|
||||
#endif
|
||||
|
||||
// Control block - driver instance local data.
|
||||
typedef struct
|
||||
{
|
||||
#if defined(USE_DMA_ISSUE_WORKAROUND)
|
||||
uint32_t starting_task_address;
|
||||
#endif
|
||||
nrfx_pwm_handler_t handler;
|
||||
void * p_context;
|
||||
nrfx_drv_state_t volatile state;
|
||||
uint8_t flags;
|
||||
} pwm_control_block_t;
|
||||
static pwm_control_block_t m_cb[NRFX_PWM_ENABLED_COUNT];
|
||||
|
||||
static void configure_pins(nrfx_pwm_t const * p_instance,
|
||||
nrfx_pwm_config_t const * p_config)
|
||||
{
|
||||
uint32_t out_pins[NRF_PWM_CHANNEL_COUNT];
|
||||
uint8_t i;
|
||||
|
||||
for (i = 0; i < NRF_PWM_CHANNEL_COUNT; ++i)
|
||||
{
|
||||
uint8_t output_pin = p_config->output_pins[i];
|
||||
if (output_pin != NRFX_PWM_PIN_NOT_USED)
|
||||
{
|
||||
bool inverted = output_pin & NRFX_PWM_PIN_INVERTED;
|
||||
out_pins[i] = output_pin & ~NRFX_PWM_PIN_INVERTED;
|
||||
|
||||
if (!p_config->skip_gpio_cfg)
|
||||
{
|
||||
if (inverted)
|
||||
{
|
||||
nrf_gpio_pin_set(out_pins[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
nrf_gpio_pin_clear(out_pins[i]);
|
||||
}
|
||||
|
||||
nrf_gpio_cfg_output(out_pins[i]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
out_pins[i] = NRF_PWM_PIN_NOT_CONNECTED;
|
||||
}
|
||||
}
|
||||
|
||||
nrf_pwm_pins_set(p_instance->p_registers, out_pins);
|
||||
}
|
||||
|
||||
|
||||
nrfx_err_t nrfx_pwm_init(nrfx_pwm_t const * p_instance,
|
||||
nrfx_pwm_config_t const * p_config,
|
||||
nrfx_pwm_handler_t handler,
|
||||
void * p_context)
|
||||
{
|
||||
NRFX_ASSERT(p_config);
|
||||
|
||||
nrfx_err_t err_code;
|
||||
|
||||
pwm_control_block_t * p_cb = &m_cb[p_instance->drv_inst_idx];
|
||||
|
||||
if (p_cb->state != NRFX_DRV_STATE_UNINITIALIZED)
|
||||
{
|
||||
err_code = NRFX_ERROR_INVALID_STATE;
|
||||
NRFX_LOG_WARNING("Function: %s, error code: %s.",
|
||||
__func__,
|
||||
NRFX_LOG_ERROR_STRING_GET(err_code));
|
||||
return err_code;
|
||||
}
|
||||
|
||||
p_cb->handler = handler;
|
||||
p_cb->p_context = p_context;
|
||||
|
||||
configure_pins(p_instance, p_config);
|
||||
|
||||
nrf_pwm_enable(p_instance->p_registers);
|
||||
nrf_pwm_configure(p_instance->p_registers,
|
||||
p_config->base_clock, p_config->count_mode, p_config->top_value);
|
||||
nrf_pwm_decoder_set(p_instance->p_registers,
|
||||
p_config->load_mode, p_config->step_mode);
|
||||
|
||||
nrf_pwm_shorts_set(p_instance->p_registers, 0);
|
||||
nrf_pwm_int_set(p_instance->p_registers, 0);
|
||||
nrf_pwm_event_clear(p_instance->p_registers, NRF_PWM_EVENT_LOOPSDONE);
|
||||
nrf_pwm_event_clear(p_instance->p_registers, NRF_PWM_EVENT_SEQEND0);
|
||||
nrf_pwm_event_clear(p_instance->p_registers, NRF_PWM_EVENT_SEQEND1);
|
||||
nrf_pwm_event_clear(p_instance->p_registers, NRF_PWM_EVENT_STOPPED);
|
||||
|
||||
// The workaround for nRF52 Anomaly 109 "protects" DMA transfers by handling
|
||||
// interrupts generated on SEQEND0 and SEQEND1 events (this ensures that
|
||||
// the 64 MHz clock is ready when data for the next sequence to be played
|
||||
// is read). Therefore, the PWM interrupt must be enabled even if the event
|
||||
// handler is not used.
|
||||
#if defined(USE_DMA_ISSUE_WORKAROUND)
|
||||
NRFX_IRQ_PRIORITY_SET(DMA_ISSUE_EGU_IRQn, p_config->irq_priority);
|
||||
NRFX_IRQ_ENABLE(DMA_ISSUE_EGU_IRQn);
|
||||
#else
|
||||
if (p_cb->handler)
|
||||
#endif
|
||||
{
|
||||
NRFX_IRQ_PRIORITY_SET(nrfx_get_irq_number(p_instance->p_registers),
|
||||
p_config->irq_priority);
|
||||
NRFX_IRQ_ENABLE(nrfx_get_irq_number(p_instance->p_registers));
|
||||
}
|
||||
|
||||
p_cb->state = NRFX_DRV_STATE_INITIALIZED;
|
||||
|
||||
err_code = NRFX_SUCCESS;
|
||||
NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
|
||||
return err_code;
|
||||
}
|
||||
|
||||
|
||||
void nrfx_pwm_uninit(nrfx_pwm_t const * p_instance)
|
||||
{
|
||||
pwm_control_block_t * p_cb = &m_cb[p_instance->drv_inst_idx];
|
||||
NRFX_ASSERT(p_cb->state != NRFX_DRV_STATE_UNINITIALIZED);
|
||||
|
||||
NRFX_IRQ_DISABLE(nrfx_get_irq_number(p_instance->p_registers));
|
||||
#if defined(USE_DMA_ISSUE_WORKAROUND)
|
||||
NRFX_IRQ_DISABLE(DMA_ISSUE_EGU_IRQn);
|
||||
#endif
|
||||
|
||||
nrf_pwm_disable(p_instance->p_registers);
|
||||
|
||||
p_cb->state = NRFX_DRV_STATE_UNINITIALIZED;
|
||||
}
|
||||
|
||||
|
||||
static uint32_t start_playback(nrfx_pwm_t const * p_instance,
|
||||
pwm_control_block_t * p_cb,
|
||||
uint8_t flags,
|
||||
nrf_pwm_task_t starting_task)
|
||||
{
|
||||
p_cb->state = NRFX_DRV_STATE_POWERED_ON;
|
||||
p_cb->flags = flags;
|
||||
|
||||
if (p_cb->handler)
|
||||
{
|
||||
// The notification about finished playback is by default enabled,
|
||||
// but this can be suppressed.
|
||||
// The notification that the peripheral has stopped is always enabled.
|
||||
uint32_t int_mask = NRF_PWM_INT_LOOPSDONE_MASK |
|
||||
NRF_PWM_INT_STOPPED_MASK;
|
||||
|
||||
// The workaround for nRF52 Anomaly 109 "protects" DMA transfers by
|
||||
// handling interrupts generated on SEQEND0 and SEQEND1 events (see
|
||||
// 'nrfx_pwm_init'), hence these events must be always enabled
|
||||
// to generate interrupts.
|
||||
// However, the user handler is called for them only when requested
|
||||
// (see 'irq_handler').
|
||||
#if defined(USE_DMA_ISSUE_WORKAROUND)
|
||||
int_mask |= NRF_PWM_INT_SEQEND0_MASK | NRF_PWM_INT_SEQEND1_MASK;
|
||||
#else
|
||||
if (flags & NRFX_PWM_FLAG_SIGNAL_END_SEQ0)
|
||||
{
|
||||
int_mask |= NRF_PWM_INT_SEQEND0_MASK;
|
||||
}
|
||||
if (flags & NRFX_PWM_FLAG_SIGNAL_END_SEQ1)
|
||||
{
|
||||
int_mask |= NRF_PWM_INT_SEQEND1_MASK;
|
||||
}
|
||||
#endif
|
||||
if (flags & NRFX_PWM_FLAG_NO_EVT_FINISHED)
|
||||
{
|
||||
int_mask &= ~NRF_PWM_INT_LOOPSDONE_MASK;
|
||||
}
|
||||
|
||||
nrf_pwm_int_set(p_instance->p_registers, int_mask);
|
||||
}
|
||||
#if defined(USE_DMA_ISSUE_WORKAROUND)
|
||||
else
|
||||
{
|
||||
nrf_pwm_int_set(p_instance->p_registers,
|
||||
NRF_PWM_INT_SEQEND0_MASK | NRF_PWM_INT_SEQEND1_MASK);
|
||||
}
|
||||
#endif
|
||||
|
||||
nrf_pwm_event_clear(p_instance->p_registers, NRF_PWM_EVENT_STOPPED);
|
||||
|
||||
if (flags & NRFX_PWM_FLAG_START_VIA_TASK)
|
||||
{
|
||||
uint32_t starting_task_address =
|
||||
nrf_pwm_task_address_get(p_instance->p_registers, starting_task);
|
||||
|
||||
#if defined(USE_DMA_ISSUE_WORKAROUND)
|
||||
// To "protect" the initial DMA transfer it is required to start
|
||||
// the PWM by triggering the proper task from EGU interrupt handler,
|
||||
// it is not safe to do it directly via PPI.
|
||||
p_cb->starting_task_address = starting_task_address;
|
||||
nrf_egu_int_enable(DMA_ISSUE_EGU, nrf_egu_channel_int_get(p_instance->drv_inst_idx));
|
||||
return nrf_egu_task_address_get(DMA_ISSUE_EGU,
|
||||
nrf_egu_trigger_task_get(p_instance->drv_inst_idx));
|
||||
#else
|
||||
return starting_task_address;
|
||||
#endif
|
||||
}
|
||||
|
||||
nrf_pwm_task_trigger(p_instance->p_registers, starting_task);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
uint32_t nrfx_pwm_simple_playback(nrfx_pwm_t const * p_instance,
|
||||
nrf_pwm_sequence_t const * p_sequence,
|
||||
uint16_t playback_count,
|
||||
uint32_t flags)
|
||||
{
|
||||
pwm_control_block_t * p_cb = &m_cb[p_instance->drv_inst_idx];
|
||||
NRFX_ASSERT(p_cb->state != NRFX_DRV_STATE_UNINITIALIZED);
|
||||
NRFX_ASSERT(playback_count > 0);
|
||||
NRFX_ASSERT(nrfx_is_in_ram(p_sequence->values.p_raw));
|
||||
|
||||
// To take advantage of the looping mechanism, we need to use both sequences
|
||||
// (single sequence can be played back only once).
|
||||
nrf_pwm_sequence_set(p_instance->p_registers, 0, p_sequence);
|
||||
nrf_pwm_sequence_set(p_instance->p_registers, 1, p_sequence);
|
||||
bool odd = (playback_count & 1);
|
||||
nrf_pwm_loop_set(p_instance->p_registers,
|
||||
(playback_count / 2) + (odd ? 1 : 0));
|
||||
|
||||
uint32_t shorts_mask;
|
||||
if (flags & NRFX_PWM_FLAG_STOP)
|
||||
{
|
||||
shorts_mask = NRF_PWM_SHORT_LOOPSDONE_STOP_MASK;
|
||||
}
|
||||
else if (flags & NRFX_PWM_FLAG_LOOP)
|
||||
{
|
||||
shorts_mask = odd ? NRF_PWM_SHORT_LOOPSDONE_SEQSTART1_MASK
|
||||
: NRF_PWM_SHORT_LOOPSDONE_SEQSTART0_MASK;
|
||||
}
|
||||
else
|
||||
{
|
||||
shorts_mask = 0;
|
||||
}
|
||||
nrf_pwm_shorts_set(p_instance->p_registers, shorts_mask);
|
||||
|
||||
NRFX_LOG_INFO("Function: %s, sequence length: %d.",
|
||||
__func__,
|
||||
p_sequence->length);
|
||||
NRFX_LOG_DEBUG("Sequence data:");
|
||||
NRFX_LOG_HEXDUMP_DEBUG((uint8_t *)p_sequence->values.p_raw,
|
||||
p_sequence->length * sizeof(uint16_t));
|
||||
return start_playback(p_instance, p_cb, flags,
|
||||
odd ? NRF_PWM_TASK_SEQSTART1 : NRF_PWM_TASK_SEQSTART0);
|
||||
}
|
||||
|
||||
|
||||
uint32_t nrfx_pwm_complex_playback(nrfx_pwm_t const * p_instance,
|
||||
nrf_pwm_sequence_t const * p_sequence_0,
|
||||
nrf_pwm_sequence_t const * p_sequence_1,
|
||||
uint16_t playback_count,
|
||||
uint32_t flags)
|
||||
{
|
||||
pwm_control_block_t * p_cb = &m_cb[p_instance->drv_inst_idx];
|
||||
NRFX_ASSERT(p_cb->state != NRFX_DRV_STATE_UNINITIALIZED);
|
||||
NRFX_ASSERT(playback_count > 0);
|
||||
NRFX_ASSERT(nrfx_is_in_ram(p_sequence_0->values.p_raw));
|
||||
NRFX_ASSERT(nrfx_is_in_ram(p_sequence_1->values.p_raw));
|
||||
|
||||
nrf_pwm_sequence_set(p_instance->p_registers, 0, p_sequence_0);
|
||||
nrf_pwm_sequence_set(p_instance->p_registers, 1, p_sequence_1);
|
||||
nrf_pwm_loop_set(p_instance->p_registers, playback_count);
|
||||
|
||||
uint32_t shorts_mask;
|
||||
if (flags & NRFX_PWM_FLAG_STOP)
|
||||
{
|
||||
shorts_mask = NRF_PWM_SHORT_LOOPSDONE_STOP_MASK;
|
||||
}
|
||||
else if (flags & NRFX_PWM_FLAG_LOOP)
|
||||
{
|
||||
shorts_mask = NRF_PWM_SHORT_LOOPSDONE_SEQSTART0_MASK;
|
||||
}
|
||||
else
|
||||
{
|
||||
shorts_mask = 0;
|
||||
}
|
||||
nrf_pwm_shorts_set(p_instance->p_registers, shorts_mask);
|
||||
|
||||
NRFX_LOG_INFO("Function: %s, sequence 0 length: %d.",
|
||||
__func__,
|
||||
p_sequence_0->length);
|
||||
NRFX_LOG_INFO("Function: %s, sequence 1 length: %d.",
|
||||
__func__,
|
||||
p_sequence_1->length);
|
||||
NRFX_LOG_DEBUG("Sequence 0 data:");
|
||||
NRFX_LOG_HEXDUMP_DEBUG(p_sequence_0->values.p_raw,
|
||||
p_sequence_0->length * sizeof(uint16_t));
|
||||
NRFX_LOG_DEBUG("Sequence 1 data:");
|
||||
NRFX_LOG_HEXDUMP_DEBUG(p_sequence_1->values.p_raw,
|
||||
p_sequence_1->length * sizeof(uint16_t));
|
||||
return start_playback(p_instance, p_cb, flags, NRF_PWM_TASK_SEQSTART0);
|
||||
}
|
||||
|
||||
|
||||
bool nrfx_pwm_stop(nrfx_pwm_t const * p_instance,
|
||||
bool wait_until_stopped)
|
||||
{
|
||||
NRFX_ASSERT(m_cb[p_instance->drv_inst_idx].state != NRFX_DRV_STATE_UNINITIALIZED);
|
||||
|
||||
bool ret_val = false;
|
||||
|
||||
// Deactivate shortcuts before triggering the STOP task, otherwise the PWM
|
||||
// could be immediately started again if the LOOPSDONE event occurred in
|
||||
// the same peripheral clock cycle as the STOP task was triggered.
|
||||
nrf_pwm_shorts_set(p_instance->p_registers, 0);
|
||||
|
||||
// Trigger the STOP task even if the PWM appears to be already stopped.
|
||||
// It won't harm, but it could help if for some strange reason the stopped
|
||||
// status was not reported correctly.
|
||||
nrf_pwm_task_trigger(p_instance->p_registers, NRF_PWM_TASK_STOP);
|
||||
|
||||
if (nrfx_pwm_is_stopped(p_instance))
|
||||
{
|
||||
ret_val = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
do {
|
||||
if (nrfx_pwm_is_stopped(p_instance))
|
||||
{
|
||||
ret_val = true;
|
||||
break;
|
||||
}
|
||||
} while (wait_until_stopped);
|
||||
}
|
||||
|
||||
NRFX_LOG_INFO("%s returned %d.", __func__, ret_val);
|
||||
return ret_val;
|
||||
}
|
||||
|
||||
|
||||
bool nrfx_pwm_is_stopped(nrfx_pwm_t const * p_instance)
|
||||
{
|
||||
pwm_control_block_t * p_cb = &m_cb[p_instance->drv_inst_idx];
|
||||
NRFX_ASSERT(p_cb->state != NRFX_DRV_STATE_UNINITIALIZED);
|
||||
|
||||
bool ret_val = false;
|
||||
|
||||
// If the event handler is used (interrupts are enabled), the state will
|
||||
// be changed in interrupt handler when the STOPPED event occurs.
|
||||
if (p_cb->state != NRFX_DRV_STATE_POWERED_ON)
|
||||
{
|
||||
ret_val = true;
|
||||
}
|
||||
// If interrupts are disabled, we must check the STOPPED event here.
|
||||
if (nrf_pwm_event_check(p_instance->p_registers, NRF_PWM_EVENT_STOPPED))
|
||||
{
|
||||
p_cb->state = NRFX_DRV_STATE_INITIALIZED;
|
||||
NRFX_LOG_INFO("Disabled.");
|
||||
ret_val = true;
|
||||
}
|
||||
|
||||
NRFX_LOG_INFO("%s returned %d.", __func__, ret_val);
|
||||
return ret_val;
|
||||
}
|
||||
|
||||
|
||||
static void irq_handler(NRF_PWM_Type * p_pwm, pwm_control_block_t * p_cb)
|
||||
{
|
||||
// The user handler is called for SEQEND0 and SEQEND1 events only when the
|
||||
// user asks for it (by setting proper flags when starting the playback).
|
||||
if (nrf_pwm_event_check(p_pwm, NRF_PWM_EVENT_SEQEND0))
|
||||
{
|
||||
nrf_pwm_event_clear(p_pwm, NRF_PWM_EVENT_SEQEND0);
|
||||
if ((p_cb->flags & NRFX_PWM_FLAG_SIGNAL_END_SEQ0) && p_cb->handler)
|
||||
{
|
||||
p_cb->handler(NRFX_PWM_EVT_END_SEQ0, p_cb->p_context);
|
||||
}
|
||||
}
|
||||
if (nrf_pwm_event_check(p_pwm, NRF_PWM_EVENT_SEQEND1))
|
||||
{
|
||||
nrf_pwm_event_clear(p_pwm, NRF_PWM_EVENT_SEQEND1);
|
||||
if ((p_cb->flags & NRFX_PWM_FLAG_SIGNAL_END_SEQ1) && p_cb->handler)
|
||||
{
|
||||
p_cb->handler(NRFX_PWM_EVT_END_SEQ1, p_cb->p_context);
|
||||
}
|
||||
}
|
||||
// For LOOPSDONE the handler is called by default, but the user can disable
|
||||
// this (via flags).
|
||||
if (nrf_pwm_event_check(p_pwm, NRF_PWM_EVENT_LOOPSDONE))
|
||||
{
|
||||
nrf_pwm_event_clear(p_pwm, NRF_PWM_EVENT_LOOPSDONE);
|
||||
if (!(p_cb->flags & NRFX_PWM_FLAG_NO_EVT_FINISHED) && p_cb->handler)
|
||||
{
|
||||
p_cb->handler(NRFX_PWM_EVT_FINISHED, p_cb->p_context);
|
||||
}
|
||||
}
|
||||
|
||||
// The STOPPED event is always propagated to the user handler.
|
||||
if (nrf_pwm_event_check(p_pwm, NRF_PWM_EVENT_STOPPED))
|
||||
{
|
||||
nrf_pwm_event_clear(p_pwm, NRF_PWM_EVENT_STOPPED);
|
||||
|
||||
p_cb->state = NRFX_DRV_STATE_INITIALIZED;
|
||||
if (p_cb->handler)
|
||||
{
|
||||
p_cb->handler(NRFX_PWM_EVT_STOPPED, p_cb->p_context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#if defined(USE_DMA_ISSUE_WORKAROUND)
|
||||
// See 'start_playback' why this is needed.
|
||||
void DMA_ISSUE_EGU_IRQHandler(void)
|
||||
{
|
||||
for (uint8_t i = 0; i < NRFX_PWM_ENABLED_COUNT; i++)
|
||||
{
|
||||
nrf_egu_event_t event = nrf_egu_triggered_event_get(i);
|
||||
if (nrf_egu_event_check(DMA_ISSUE_EGU, event))
|
||||
{
|
||||
nrf_egu_event_clear(DMA_ISSUE_EGU, event);
|
||||
*(volatile uint32_t *)(m_cb[i].starting_task_address) = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#if NRFX_CHECK(NRFX_PWM0_ENABLED)
|
||||
void nrfx_pwm_0_irq_handler(void)
|
||||
{
|
||||
irq_handler(NRF_PWM0, &m_cb[NRFX_PWM0_INST_IDX]);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if NRFX_CHECK(NRFX_PWM1_ENABLED)
|
||||
void nrfx_pwm_1_irq_handler(void)
|
||||
{
|
||||
irq_handler(NRF_PWM1, &m_cb[NRFX_PWM1_INST_IDX]);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if NRFX_CHECK(NRFX_PWM2_ENABLED)
|
||||
void nrfx_pwm_2_irq_handler(void)
|
||||
{
|
||||
irq_handler(NRF_PWM2, &m_cb[NRFX_PWM2_INST_IDX]);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if NRFX_CHECK(NRFX_PWM3_ENABLED)
|
||||
void nrfx_pwm_3_irq_handler(void)
|
||||
{
|
||||
irq_handler(NRF_PWM3, &m_cb[NRFX_PWM3_INST_IDX]);
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // NRFX_CHECK(NRFX_PWM_ENABLED)
|
||||
@@ -0,0 +1,337 @@
|
||||
/*
|
||||
* Copyright (c) 2015 - 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 <nrfx.h>
|
||||
|
||||
#if NRFX_CHECK(NRFX_TIMER_ENABLED)
|
||||
|
||||
#if !(NRFX_CHECK(NRFX_TIMER0_ENABLED) || NRFX_CHECK(NRFX_TIMER1_ENABLED) || \
|
||||
NRFX_CHECK(NRFX_TIMER2_ENABLED) || NRFX_CHECK(NRFX_TIMER3_ENABLED) || \
|
||||
NRFX_CHECK(NRFX_TIMER4_ENABLED))
|
||||
#error "No enabled TIMER instances. Check <nrfx_config.h>."
|
||||
#endif
|
||||
|
||||
#if NRFX_CHECK(NRFX_TIMER0_ENABLED) && ((1 << 0) & NRFX_TIMERS_USED)
|
||||
#error "TIMER instance 0 is reserved for use outside of nrfx."
|
||||
#endif
|
||||
#if NRFX_CHECK(NRFX_TIMER1_ENABLED) && ((1 << 1) & NRFX_TIMERS_USED)
|
||||
#error "TIMER instance 1 is reserved for use outside of nrfx."
|
||||
#endif
|
||||
#if NRFX_CHECK(NRFX_TIMER2_ENABLED) && ((1 << 2) & NRFX_TIMERS_USED)
|
||||
#error "TIMER instance 2 is reserved for use outside of nrfx."
|
||||
#endif
|
||||
#if NRFX_CHECK(NRFX_TIMER3_ENABLED) && ((1 << 3) & NRFX_TIMERS_USED)
|
||||
#error "TIMER instance 3 is reserved for use outside of nrfx."
|
||||
#endif
|
||||
#if NRFX_CHECK(NRFX_TIMER4_ENABLED) && ((1 << 4) & NRFX_TIMERS_USED)
|
||||
#error "TIMER instance 4 is reserved for use outside of nrfx."
|
||||
#endif
|
||||
|
||||
#include <nrfx_timer.h>
|
||||
|
||||
#define NRFX_LOG_MODULE TIMER
|
||||
#include <nrfx_log.h>
|
||||
|
||||
/**@brief Timer control block. */
|
||||
typedef struct
|
||||
{
|
||||
nrfx_timer_event_handler_t handler;
|
||||
void * context;
|
||||
nrfx_drv_state_t state;
|
||||
} timer_control_block_t;
|
||||
|
||||
static timer_control_block_t m_cb[NRFX_TIMER_ENABLED_COUNT];
|
||||
|
||||
nrfx_err_t nrfx_timer_init(nrfx_timer_t const * p_instance,
|
||||
nrfx_timer_config_t const * p_config,
|
||||
nrfx_timer_event_handler_t timer_event_handler)
|
||||
{
|
||||
timer_control_block_t * p_cb = &m_cb[p_instance->instance_id];
|
||||
#ifdef SOFTDEVICE_PRESENT
|
||||
NRFX_ASSERT(p_instance->p_reg != NRF_TIMER0);
|
||||
#endif
|
||||
NRFX_ASSERT(p_config);
|
||||
NRFX_ASSERT(timer_event_handler);
|
||||
|
||||
nrfx_err_t err_code;
|
||||
|
||||
if (p_cb->state != NRFX_DRV_STATE_UNINITIALIZED)
|
||||
{
|
||||
err_code = NRFX_ERROR_INVALID_STATE;
|
||||
NRFX_LOG_WARNING("Function: %s, error code: %s.",
|
||||
__func__,
|
||||
NRFX_LOG_ERROR_STRING_GET(err_code));
|
||||
return err_code;
|
||||
}
|
||||
|
||||
/* Warning 685: Relational operator '<=' always evaluates to 'true'"
|
||||
* Warning in NRF_TIMER_IS_BIT_WIDTH_VALID macro. Macro validate timers resolution.
|
||||
* Not necessary in nRF52 based systems. Obligatory in nRF51 based systems.
|
||||
*/
|
||||
|
||||
/*lint -save -e685 */
|
||||
|
||||
NRFX_ASSERT(NRF_TIMER_IS_BIT_WIDTH_VALID(p_instance->p_reg, p_config->bit_width));
|
||||
|
||||
//lint -restore
|
||||
|
||||
p_cb->handler = timer_event_handler;
|
||||
p_cb->context = p_config->p_context;
|
||||
|
||||
uint8_t i;
|
||||
for (i = 0; i < p_instance->cc_channel_count; ++i)
|
||||
{
|
||||
nrf_timer_event_clear(p_instance->p_reg,
|
||||
nrf_timer_compare_event_get(i));
|
||||
}
|
||||
|
||||
NRFX_IRQ_PRIORITY_SET(nrfx_get_irq_number(p_instance->p_reg),
|
||||
p_config->interrupt_priority);
|
||||
NRFX_IRQ_ENABLE(nrfx_get_irq_number(p_instance->p_reg));
|
||||
|
||||
nrf_timer_mode_set(p_instance->p_reg, p_config->mode);
|
||||
nrf_timer_bit_width_set(p_instance->p_reg, p_config->bit_width);
|
||||
nrf_timer_frequency_set(p_instance->p_reg, p_config->frequency);
|
||||
|
||||
p_cb->state = NRFX_DRV_STATE_INITIALIZED;
|
||||
|
||||
err_code = NRFX_SUCCESS;
|
||||
NRFX_LOG_INFO("Function: %s, error code: %s.",
|
||||
__func__,
|
||||
NRFX_LOG_ERROR_STRING_GET(err_code));
|
||||
return err_code;
|
||||
}
|
||||
|
||||
void nrfx_timer_uninit(nrfx_timer_t const * p_instance)
|
||||
{
|
||||
NRFX_IRQ_DISABLE(nrfx_get_irq_number(p_instance->p_reg));
|
||||
|
||||
#define DISABLE_ALL UINT32_MAX
|
||||
nrf_timer_shorts_disable(p_instance->p_reg, DISABLE_ALL);
|
||||
nrf_timer_int_disable(p_instance->p_reg, DISABLE_ALL);
|
||||
#undef DISABLE_ALL
|
||||
|
||||
nrfx_timer_disable(p_instance);
|
||||
|
||||
m_cb[p_instance->instance_id].state = NRFX_DRV_STATE_UNINITIALIZED;
|
||||
NRFX_LOG_INFO("Uninitialized instance: %d.", p_instance->instance_id);
|
||||
}
|
||||
|
||||
void nrfx_timer_enable(nrfx_timer_t const * p_instance)
|
||||
{
|
||||
NRFX_ASSERT(m_cb[p_instance->instance_id].state == NRFX_DRV_STATE_INITIALIZED);
|
||||
nrf_timer_task_trigger(p_instance->p_reg, NRF_TIMER_TASK_START);
|
||||
m_cb[p_instance->instance_id].state = NRFX_DRV_STATE_POWERED_ON;
|
||||
NRFX_LOG_INFO("Enabled instance: %d.", p_instance->instance_id);
|
||||
}
|
||||
|
||||
void nrfx_timer_disable(nrfx_timer_t const * p_instance)
|
||||
{
|
||||
NRFX_ASSERT(m_cb[p_instance->instance_id].state != NRFX_DRV_STATE_UNINITIALIZED);
|
||||
nrf_timer_task_trigger(p_instance->p_reg, NRF_TIMER_TASK_SHUTDOWN);
|
||||
m_cb[p_instance->instance_id].state = NRFX_DRV_STATE_INITIALIZED;
|
||||
NRFX_LOG_INFO("Disabled instance: %d.", p_instance->instance_id);
|
||||
}
|
||||
|
||||
bool nrfx_timer_is_enabled(nrfx_timer_t const * p_instance)
|
||||
{
|
||||
NRFX_ASSERT(m_cb[p_instance->instance_id].state != NRFX_DRV_STATE_UNINITIALIZED);
|
||||
return (m_cb[p_instance->instance_id].state == NRFX_DRV_STATE_POWERED_ON);
|
||||
}
|
||||
|
||||
void nrfx_timer_resume(nrfx_timer_t const * p_instance)
|
||||
{
|
||||
NRFX_ASSERT(m_cb[p_instance->instance_id].state != NRFX_DRV_STATE_UNINITIALIZED);
|
||||
nrf_timer_task_trigger(p_instance->p_reg, NRF_TIMER_TASK_START);
|
||||
NRFX_LOG_INFO("Resumed instance: %d.", p_instance->instance_id);
|
||||
}
|
||||
|
||||
void nrfx_timer_pause(nrfx_timer_t const * p_instance)
|
||||
{
|
||||
NRFX_ASSERT(m_cb[p_instance->instance_id].state != NRFX_DRV_STATE_UNINITIALIZED);
|
||||
nrf_timer_task_trigger(p_instance->p_reg, NRF_TIMER_TASK_STOP);
|
||||
NRFX_LOG_INFO("Paused instance: %d.", p_instance->instance_id);
|
||||
}
|
||||
|
||||
void nrfx_timer_clear(nrfx_timer_t const * p_instance)
|
||||
{
|
||||
NRFX_ASSERT(m_cb[p_instance->instance_id].state != NRFX_DRV_STATE_UNINITIALIZED);
|
||||
nrf_timer_task_trigger(p_instance->p_reg, NRF_TIMER_TASK_CLEAR);
|
||||
}
|
||||
|
||||
void nrfx_timer_increment(nrfx_timer_t const * p_instance)
|
||||
{
|
||||
NRFX_ASSERT(m_cb[p_instance->instance_id].state != NRFX_DRV_STATE_UNINITIALIZED);
|
||||
NRFX_ASSERT(nrf_timer_mode_get(p_instance->p_reg) != NRF_TIMER_MODE_TIMER);
|
||||
|
||||
nrf_timer_task_trigger(p_instance->p_reg, NRF_TIMER_TASK_COUNT);
|
||||
}
|
||||
|
||||
uint32_t nrfx_timer_capture(nrfx_timer_t const * p_instance,
|
||||
nrf_timer_cc_channel_t cc_channel)
|
||||
{
|
||||
NRFX_ASSERT(m_cb[p_instance->instance_id].state != NRFX_DRV_STATE_UNINITIALIZED);
|
||||
NRFX_ASSERT(cc_channel < p_instance->cc_channel_count);
|
||||
|
||||
nrf_timer_task_trigger(p_instance->p_reg,
|
||||
nrf_timer_capture_task_get(cc_channel));
|
||||
return nrf_timer_cc_get(p_instance->p_reg, cc_channel);
|
||||
}
|
||||
|
||||
void nrfx_timer_compare(nrfx_timer_t const * p_instance,
|
||||
nrf_timer_cc_channel_t cc_channel,
|
||||
uint32_t cc_value,
|
||||
bool enable_int)
|
||||
{
|
||||
nrf_timer_int_mask_t timer_int = nrf_timer_compare_int_get(cc_channel);
|
||||
|
||||
if (enable_int)
|
||||
{
|
||||
nrf_timer_event_clear(p_instance->p_reg, nrf_timer_compare_event_get(cc_channel));
|
||||
nrf_timer_int_enable(p_instance->p_reg, timer_int);
|
||||
}
|
||||
else
|
||||
{
|
||||
nrf_timer_int_disable(p_instance->p_reg, timer_int);
|
||||
}
|
||||
|
||||
nrf_timer_cc_set(p_instance->p_reg, cc_channel, cc_value);
|
||||
NRFX_LOG_INFO("Timer id: %d, capture value set: %lu, channel: %d.",
|
||||
p_instance->instance_id,
|
||||
cc_value,
|
||||
cc_channel);
|
||||
}
|
||||
|
||||
void nrfx_timer_extended_compare(nrfx_timer_t const * p_instance,
|
||||
nrf_timer_cc_channel_t cc_channel,
|
||||
uint32_t cc_value,
|
||||
nrf_timer_short_mask_t timer_short_mask,
|
||||
bool enable_int)
|
||||
{
|
||||
nrf_timer_shorts_disable(p_instance->p_reg,
|
||||
(TIMER_SHORTS_COMPARE0_STOP_Msk << cc_channel) |
|
||||
(TIMER_SHORTS_COMPARE0_CLEAR_Msk << cc_channel));
|
||||
|
||||
nrf_timer_shorts_enable(p_instance->p_reg, timer_short_mask);
|
||||
|
||||
nrfx_timer_compare(p_instance,
|
||||
cc_channel,
|
||||
cc_value,
|
||||
enable_int);
|
||||
NRFX_LOG_INFO("Timer id: %d, capture value set: %lu, channel: %d.",
|
||||
p_instance->instance_id,
|
||||
cc_value,
|
||||
cc_channel);
|
||||
}
|
||||
|
||||
void nrfx_timer_compare_int_enable(nrfx_timer_t const * p_instance,
|
||||
uint32_t channel)
|
||||
{
|
||||
NRFX_ASSERT(m_cb[p_instance->instance_id].state != NRFX_DRV_STATE_UNINITIALIZED);
|
||||
NRFX_ASSERT(channel < p_instance->cc_channel_count);
|
||||
|
||||
nrf_timer_event_clear(p_instance->p_reg,
|
||||
nrf_timer_compare_event_get(channel));
|
||||
nrf_timer_int_enable(p_instance->p_reg,
|
||||
nrf_timer_compare_int_get(channel));
|
||||
}
|
||||
|
||||
void nrfx_timer_compare_int_disable(nrfx_timer_t const * p_instance,
|
||||
uint32_t channel)
|
||||
{
|
||||
NRFX_ASSERT(m_cb[p_instance->instance_id].state != NRFX_DRV_STATE_UNINITIALIZED);
|
||||
NRFX_ASSERT(channel < p_instance->cc_channel_count);
|
||||
|
||||
nrf_timer_int_disable(p_instance->p_reg,
|
||||
nrf_timer_compare_int_get(channel));
|
||||
}
|
||||
|
||||
static void irq_handler(NRF_TIMER_Type * p_reg,
|
||||
timer_control_block_t * p_cb,
|
||||
uint8_t channel_count)
|
||||
{
|
||||
uint8_t i;
|
||||
for (i = 0; i < channel_count; ++i)
|
||||
{
|
||||
nrf_timer_event_t event = nrf_timer_compare_event_get(i);
|
||||
nrf_timer_int_mask_t int_mask = nrf_timer_compare_int_get(i);
|
||||
|
||||
if (nrf_timer_event_check(p_reg, event) &&
|
||||
nrf_timer_int_enable_check(p_reg, int_mask))
|
||||
{
|
||||
nrf_timer_event_clear(p_reg, event);
|
||||
NRFX_LOG_DEBUG("Compare event, channel: %d.", i);
|
||||
p_cb->handler(event, p_cb->context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if NRFX_CHECK(NRFX_TIMER0_ENABLED)
|
||||
void nrfx_timer_0_irq_handler(void)
|
||||
{
|
||||
irq_handler(NRF_TIMER0, &m_cb[NRFX_TIMER0_INST_IDX],
|
||||
NRF_TIMER_CC_CHANNEL_COUNT(0));
|
||||
}
|
||||
#endif
|
||||
|
||||
#if NRFX_CHECK(NRFX_TIMER1_ENABLED)
|
||||
void nrfx_timer_1_irq_handler(void)
|
||||
{
|
||||
irq_handler(NRF_TIMER1, &m_cb[NRFX_TIMER1_INST_IDX],
|
||||
NRF_TIMER_CC_CHANNEL_COUNT(1));
|
||||
}
|
||||
#endif
|
||||
|
||||
#if NRFX_CHECK(NRFX_TIMER2_ENABLED)
|
||||
void nrfx_timer_2_irq_handler(void)
|
||||
{
|
||||
irq_handler(NRF_TIMER2, &m_cb[NRFX_TIMER2_INST_IDX],
|
||||
NRF_TIMER_CC_CHANNEL_COUNT(2));
|
||||
}
|
||||
#endif
|
||||
|
||||
#if NRFX_CHECK(NRFX_TIMER3_ENABLED)
|
||||
void nrfx_timer_3_irq_handler(void)
|
||||
{
|
||||
irq_handler(NRF_TIMER3, &m_cb[NRFX_TIMER3_INST_IDX],
|
||||
NRF_TIMER_CC_CHANNEL_COUNT(3));
|
||||
}
|
||||
#endif
|
||||
|
||||
#if NRFX_CHECK(NRFX_TIMER4_ENABLED)
|
||||
void nrfx_timer_4_irq_handler(void)
|
||||
{
|
||||
irq_handler(NRF_TIMER4, &m_cb[NRFX_TIMER4_INST_IDX],
|
||||
NRF_TIMER_CC_CHANNEL_COUNT(4));
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // NRFX_CHECK(NRFX_TIMER_ENABLED)
|
||||
@@ -14,6 +14,18 @@
|
||||
#define NRFX_SPIM1_ENABLED 0 // used as I2C
|
||||
#define NRFX_SPIM2_ENABLED 1
|
||||
|
||||
#define NRFX_PWM_ENABLED 0
|
||||
#define NRFX_PWM0_ENABLED 0
|
||||
#define NRFX_PWM1_ENABLED 0
|
||||
#define NRFX_PWM2_ENABLED 0
|
||||
#define NRFX_PWM3_ENABLED 0
|
||||
|
||||
#define NRFX_TIMER_ENABLED 0
|
||||
#define NRFX_TIMER0_ENABLED 0
|
||||
#define NRFX_TIMER1_ENABLED 0
|
||||
#define NRFX_TIMER2_ENABLED 0
|
||||
#define NRFX_TIMER3_ENABLED 0
|
||||
|
||||
#ifdef NRF52840_XXAA
|
||||
#define NRFX_QSPI_ENABLED 1
|
||||
#define NRFX_SPIM3_ENABLED 1
|
||||
|
||||
Reference in new issue
Block a user