2018-05-04 05:40:12 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018, Cue Health Inc
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
#include <nrfx_pwm.h>
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/drivers/pwm.h>
|
|
|
|
#include <zephyr/pm/device.h>
|
|
|
|
#include <zephyr/drivers/pinctrl.h>
|
2022-03-09 17:54:05 +08:00
|
|
|
#include <soc.h>
|
2018-12-05 00:44:45 +08:00
|
|
|
#include <hal/nrf_gpio.h>
|
|
|
|
#include <stdbool.h>
|
2024-04-23 17:48:22 +08:00
|
|
|
#include <zephyr/linker/devicetree_regions.h>
|
2018-05-04 05:40:12 +08:00
|
|
|
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/logging/log.h>
|
2022-06-17 22:15:11 +08:00
|
|
|
|
2022-06-17 20:12:12 +08:00
|
|
|
LOG_MODULE_REGISTER(pwm_nrfx, CONFIG_PWM_LOG_LEVEL);
|
2018-05-04 05:40:12 +08:00
|
|
|
|
2023-10-04 17:17:20 +08:00
|
|
|
/* NRFX_PWM_NRF52_ANOMALY_109_WORKAROUND_ENABLED can be undefined or defined
|
|
|
|
* to 0 or 1, hence the use of #if IS_ENABLED().
|
|
|
|
*/
|
|
|
|
#if IS_ENABLED(NRFX_PWM_NRF52_ANOMALY_109_WORKAROUND_ENABLED)
|
|
|
|
#define ANOMALY_109_EGU_IRQ_CONNECT(idx) _EGU_IRQ_CONNECT(idx)
|
|
|
|
#define _EGU_IRQ_CONNECT(idx) \
|
|
|
|
extern void nrfx_egu_##idx##_irq_handler(void); \
|
|
|
|
IRQ_CONNECT(DT_IRQN(DT_NODELABEL(egu##idx)), \
|
|
|
|
DT_IRQ(DT_NODELABEL(egu##idx), priority), \
|
|
|
|
nrfx_isr, nrfx_egu_##idx##_irq_handler, 0)
|
|
|
|
#else
|
|
|
|
#define ANOMALY_109_EGU_IRQ_CONNECT(idx)
|
|
|
|
#endif
|
|
|
|
|
2022-07-21 23:12:52 +08:00
|
|
|
#define PWM_NRFX_CH_POLARITY_MASK BIT(15)
|
|
|
|
#define PWM_NRFX_CH_COMPARE_MASK BIT_MASK(15)
|
|
|
|
#define PWM_NRFX_CH_VALUE(compare_value, inverted) \
|
2022-07-27 17:02:14 +08:00
|
|
|
(compare_value | (inverted ? 0 : PWM_NRFX_CH_POLARITY_MASK))
|
2018-05-04 05:40:12 +08:00
|
|
|
|
|
|
|
struct pwm_nrfx_config {
|
|
|
|
nrfx_pwm_t pwm;
|
2018-12-05 00:44:45 +08:00
|
|
|
nrfx_pwm_config_t initial_config;
|
2018-05-04 05:40:12 +08:00
|
|
|
nrf_pwm_sequence_t seq;
|
2022-03-09 17:54:05 +08:00
|
|
|
const struct pinctrl_dev_config *pcfg;
|
2018-05-04 05:40:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct pwm_nrfx_data {
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t period_cycles;
|
2022-07-27 17:52:41 +08:00
|
|
|
/* Bit mask indicating channels that need the PWM generation. */
|
|
|
|
uint8_t pwm_needed;
|
2020-05-28 00:26:57 +08:00
|
|
|
uint8_t prescaler;
|
2022-07-21 23:12:52 +08:00
|
|
|
bool stop_requested;
|
2018-05-04 05:40:12 +08:00
|
|
|
};
|
2022-07-27 17:52:41 +08:00
|
|
|
/* Ensure the pwm_needed bit mask can accommodate all available channels. */
|
|
|
|
#if (NRF_PWM_CHANNEL_COUNT > 8)
|
|
|
|
#error "Current implementation supports maximum 8 channels."
|
|
|
|
#endif
|
2018-05-04 05:40:12 +08:00
|
|
|
|
2024-04-23 17:48:22 +08:00
|
|
|
static uint16_t *seq_values_ptr_get(const struct device *dev)
|
|
|
|
{
|
|
|
|
const struct pwm_nrfx_config *config = dev->config;
|
|
|
|
|
|
|
|
return (uint16_t *)config->seq.values.p_raw;
|
|
|
|
}
|
2018-12-05 00:44:45 +08:00
|
|
|
|
2024-09-20 16:34:13 +08:00
|
|
|
static void pwm_handler(nrfx_pwm_evt_type_t event_type, void *p_context)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(event_type);
|
|
|
|
ARG_UNUSED(p_context);
|
|
|
|
}
|
|
|
|
|
2022-07-21 23:12:52 +08:00
|
|
|
static bool pwm_period_check_and_set(const struct device *dev,
|
|
|
|
uint32_t channel, uint32_t period_cycles)
|
|
|
|
{
|
|
|
|
const struct pwm_nrfx_config *config = dev->config;
|
|
|
|
struct pwm_nrfx_data *data = dev->data;
|
2020-05-28 00:26:57 +08:00
|
|
|
uint8_t prescaler;
|
|
|
|
uint32_t countertop;
|
2018-05-04 05:40:12 +08:00
|
|
|
|
2022-07-21 23:12:52 +08:00
|
|
|
/* If the currently configured period matches the requested one,
|
|
|
|
* nothing more needs to be done.
|
2018-12-05 00:44:45 +08:00
|
|
|
*/
|
2022-07-21 23:12:52 +08:00
|
|
|
if (period_cycles == data->period_cycles) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If any other channel is driven by the PWM peripheral, the period
|
|
|
|
* that is currently set cannot be changed, as this would influence
|
|
|
|
* the output for that channel.
|
|
|
|
*/
|
2022-07-27 17:52:41 +08:00
|
|
|
if ((data->pwm_needed & ~BIT(channel)) != 0) {
|
2022-07-21 23:12:52 +08:00
|
|
|
LOG_ERR("Incompatible period.");
|
|
|
|
return false;
|
2019-11-04 20:12:28 +08:00
|
|
|
}
|
2018-12-05 00:44:45 +08:00
|
|
|
|
2019-11-04 20:12:28 +08:00
|
|
|
/* Try to find a prescaler that will allow setting the requested period
|
|
|
|
* after prescaling as the countertop value for the PWM peripheral.
|
|
|
|
*/
|
|
|
|
prescaler = 0;
|
|
|
|
countertop = period_cycles;
|
|
|
|
do {
|
|
|
|
if (countertop <= PWM_COUNTERTOP_COUNTERTOP_Msk) {
|
|
|
|
data->period_cycles = period_cycles;
|
|
|
|
data->prescaler = prescaler;
|
|
|
|
|
2023-04-21 18:31:57 +08:00
|
|
|
nrf_pwm_configure(config->pwm.p_reg,
|
2019-11-04 20:12:28 +08:00
|
|
|
data->prescaler,
|
|
|
|
config->initial_config.count_mode,
|
2022-07-21 23:12:52 +08:00
|
|
|
(uint16_t)countertop);
|
|
|
|
return true;
|
2018-05-04 05:40:12 +08:00
|
|
|
}
|
2018-12-05 00:44:45 +08:00
|
|
|
|
2019-11-04 20:12:28 +08:00
|
|
|
countertop >>= 1;
|
|
|
|
++prescaler;
|
|
|
|
} while (prescaler <= PWM_PRESCALER_PRESCALER_Msk);
|
2018-12-05 00:44:45 +08:00
|
|
|
|
2019-11-04 20:12:28 +08:00
|
|
|
LOG_ERR("Prescaler for period_cycles %u not found.", period_cycles);
|
2018-12-05 00:44:45 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-20 15:00:00 +08:00
|
|
|
static bool channel_psel_get(uint32_t channel, uint32_t *psel,
|
|
|
|
const struct pwm_nrfx_config *config)
|
|
|
|
{
|
2023-04-21 18:31:57 +08:00
|
|
|
*psel = nrf_pwm_pin_get(config->pwm.p_reg, (uint8_t)channel);
|
2022-04-20 15:00:00 +08:00
|
|
|
|
|
|
|
return (((*psel & PWM_PSEL_OUT_CONNECT_Msk) >> PWM_PSEL_OUT_CONNECT_Pos)
|
|
|
|
== PWM_PSEL_OUT_CONNECT_Connected);
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:06:43 +08:00
|
|
|
static int pwm_nrfx_set_cycles(const struct device *dev, uint32_t channel,
|
|
|
|
uint32_t period_cycles, uint32_t pulse_cycles,
|
|
|
|
pwm_flags_t flags)
|
2018-12-05 00:44:45 +08:00
|
|
|
{
|
|
|
|
/* We assume here that period_cycles will always be 16MHz
|
|
|
|
* peripheral clock. Since pwm_nrfx_get_cycles_per_sec() function might
|
|
|
|
* be removed, see ISSUE #6958.
|
|
|
|
* TODO: Remove this comment when issue has been resolved.
|
|
|
|
*/
|
2020-05-29 02:44:16 +08:00
|
|
|
const struct pwm_nrfx_config *config = dev->config;
|
2020-05-29 03:23:02 +08:00
|
|
|
struct pwm_nrfx_data *data = dev->data;
|
2022-07-21 23:12:52 +08:00
|
|
|
uint16_t compare_value;
|
2022-04-20 15:00:00 +08:00
|
|
|
bool inverted = (flags & PWM_POLARITY_INVERTED);
|
2022-07-21 23:12:52 +08:00
|
|
|
bool needs_pwm = false;
|
2018-12-05 00:44:45 +08:00
|
|
|
|
2022-04-13 17:44:36 +08:00
|
|
|
if (channel >= NRF_PWM_CHANNEL_COUNT) {
|
|
|
|
LOG_ERR("Invalid channel: %u.", channel);
|
2018-12-05 00:44:45 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2018-05-04 05:40:12 +08:00
|
|
|
|
2019-04-24 23:05:54 +08:00
|
|
|
/* If this PWM is in center-aligned mode, pulse and period lengths
|
|
|
|
* are effectively doubled by the up-down count, so halve them here
|
|
|
|
* to compensate.
|
|
|
|
*/
|
|
|
|
if (config->initial_config.count_mode == NRF_PWM_MODE_UP_AND_DOWN) {
|
|
|
|
period_cycles /= 2;
|
|
|
|
pulse_cycles /= 2;
|
|
|
|
}
|
|
|
|
|
2022-07-21 23:12:52 +08:00
|
|
|
if (pulse_cycles == 0) {
|
|
|
|
/* Constantly inactive (duty 0%). */
|
|
|
|
compare_value = 0;
|
|
|
|
} else if (pulse_cycles >= period_cycles) {
|
|
|
|
/* Constantly active (duty 100%). */
|
|
|
|
/* This value is always greater than or equal to COUNTERTOP. */
|
|
|
|
compare_value = PWM_NRFX_CH_COMPARE_MASK;
|
|
|
|
} else {
|
|
|
|
/* PWM generation needed. Check if the requested period matches
|
|
|
|
* the one that is currently set, or the PWM peripheral can be
|
|
|
|
* reconfigured accordingly.
|
|
|
|
*/
|
|
|
|
if (!pwm_period_check_and_set(dev, channel, period_cycles)) {
|
|
|
|
return -EINVAL;
|
2019-11-04 20:12:28 +08:00
|
|
|
}
|
2022-07-21 23:12:52 +08:00
|
|
|
|
|
|
|
compare_value = (uint16_t)(pulse_cycles >> data->prescaler);
|
|
|
|
needs_pwm = true;
|
2018-05-04 05:40:12 +08:00
|
|
|
}
|
|
|
|
|
2024-04-23 17:48:22 +08:00
|
|
|
seq_values_ptr_get(dev)[channel] = PWM_NRFX_CH_VALUE(compare_value, inverted);
|
2018-12-05 00:44:45 +08:00
|
|
|
|
2022-04-13 17:44:36 +08:00
|
|
|
LOG_DBG("channel %u, pulse %u, period %u, prescaler: %u.",
|
|
|
|
channel, pulse_cycles, period_cycles, data->prescaler);
|
2019-11-04 20:12:28 +08:00
|
|
|
|
2022-07-21 23:12:52 +08:00
|
|
|
/* If this channel does not need to be driven by the PWM peripheral
|
|
|
|
* because its state is to be constant (duty 0% or 100%), set properly
|
2019-11-04 20:12:28 +08:00
|
|
|
* the GPIO configuration for its output pin. This will provide
|
|
|
|
* the correct output state for this channel when the PWM peripheral
|
2022-07-21 23:12:52 +08:00
|
|
|
* is stopped.
|
2018-05-04 05:40:12 +08:00
|
|
|
*/
|
2022-07-21 23:12:52 +08:00
|
|
|
if (!needs_pwm) {
|
2022-04-20 15:00:00 +08:00
|
|
|
uint32_t psel;
|
|
|
|
|
|
|
|
if (channel_psel_get(channel, &psel, config)) {
|
2022-07-21 23:12:52 +08:00
|
|
|
uint32_t out_level = (pulse_cycles == 0) ? 0 : 1;
|
2018-12-05 00:44:45 +08:00
|
|
|
|
2022-07-21 23:12:52 +08:00
|
|
|
if (inverted) {
|
|
|
|
out_level ^= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
nrf_gpio_pin_write(psel, out_level);
|
2019-11-04 20:12:28 +08:00
|
|
|
}
|
2022-07-27 17:52:41 +08:00
|
|
|
|
|
|
|
data->pwm_needed &= ~BIT(channel);
|
|
|
|
} else {
|
|
|
|
data->pwm_needed |= BIT(channel);
|
2022-07-21 23:12:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If the PWM generation is not needed for any channel (all are set
|
|
|
|
* to constant inactive or active state), stop the PWM peripheral.
|
|
|
|
* Otherwise, request a playback of the defined sequence so that
|
|
|
|
* the PWM peripheral loads `seq_values` into its internal compare
|
|
|
|
* registers and drives its outputs accordingly.
|
|
|
|
*/
|
2022-07-27 17:52:41 +08:00
|
|
|
if (data->pwm_needed == 0) {
|
2022-07-21 23:12:52 +08:00
|
|
|
/* Don't wait here for the peripheral to actually stop. Instead,
|
|
|
|
* ensure it is stopped before starting the next playback.
|
2018-12-05 00:44:45 +08:00
|
|
|
*/
|
2022-07-21 23:12:52 +08:00
|
|
|
nrfx_pwm_stop(&config->pwm, false);
|
|
|
|
data->stop_requested = true;
|
|
|
|
} else {
|
|
|
|
if (data->stop_requested) {
|
|
|
|
data->stop_requested = false;
|
|
|
|
|
|
|
|
/* After a stop is requested, the PWM peripheral stops
|
|
|
|
* pulse generation at the end of the current period,
|
|
|
|
* and till that moment, it ignores any start requests,
|
|
|
|
* so ensure here that it is stopped.
|
2020-03-16 03:32:02 +08:00
|
|
|
*/
|
2023-06-29 15:36:25 +08:00
|
|
|
while (!nrfx_pwm_stopped_check(&config->pwm)) {
|
2021-03-25 07:39:15 +08:00
|
|
|
}
|
2019-11-04 20:12:28 +08:00
|
|
|
}
|
2022-07-21 23:12:52 +08:00
|
|
|
|
|
|
|
/* It is sufficient to play the sequence once without looping.
|
|
|
|
* The PWM generation will continue with the loaded values
|
|
|
|
* until another playback is requested (new values will be
|
|
|
|
* loaded then) or the PWM peripheral is stopped.
|
|
|
|
*/
|
2024-09-20 16:34:13 +08:00
|
|
|
nrfx_pwm_simple_playback(&config->pwm, &config->seq, 1,
|
|
|
|
NRFX_PWM_FLAG_NO_EVT_FINISHED);
|
2018-12-05 00:44:45 +08:00
|
|
|
}
|
2018-05-04 05:40:12 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-04 22:35:22 +08:00
|
|
|
static int pwm_nrfx_get_cycles_per_sec(const struct device *dev, uint32_t channel,
|
2020-05-28 00:26:57 +08:00
|
|
|
uint64_t *cycles)
|
2018-05-04 05:40:12 +08:00
|
|
|
{
|
2018-12-05 00:44:45 +08:00
|
|
|
/* TODO: Since this function might be removed, we will always return
|
|
|
|
* 16MHz from this function and handle the conversion with prescaler,
|
|
|
|
* etc, in the pin set function. See issue #6958.
|
|
|
|
*/
|
|
|
|
*cycles = 16ul * 1000ul * 1000ul;
|
2018-05-04 05:40:12 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct pwm_driver_api pwm_nrfx_drv_api_funcs = {
|
2022-04-01 17:06:43 +08:00
|
|
|
.set_cycles = pwm_nrfx_set_cycles,
|
2018-05-04 05:40:12 +08:00
|
|
|
.get_cycles_per_sec = pwm_nrfx_get_cycles_per_sec,
|
|
|
|
};
|
|
|
|
|
2024-09-20 19:17:19 +08:00
|
|
|
static void pwm_resume(const struct device *dev)
|
2018-05-04 05:40:12 +08:00
|
|
|
{
|
2020-05-29 02:44:16 +08:00
|
|
|
const struct pwm_nrfx_config *config = dev->config;
|
2023-02-21 20:36:06 +08:00
|
|
|
uint8_t initially_inverted = 0;
|
2023-10-04 17:17:20 +08:00
|
|
|
|
2024-09-20 19:17:19 +08:00
|
|
|
(void)pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
|
2022-03-09 17:54:05 +08:00
|
|
|
|
2024-04-23 17:48:22 +08:00
|
|
|
for (size_t i = 0; i < NRF_PWM_CHANNEL_COUNT; i++) {
|
2022-04-20 15:00:00 +08:00
|
|
|
uint32_t psel;
|
|
|
|
|
|
|
|
if (channel_psel_get(i, &psel, config)) {
|
|
|
|
/* Mark channels as inverted according to what initial
|
|
|
|
* state of their outputs has been set by pinctrl (high
|
|
|
|
* idle state means that the channel is inverted).
|
|
|
|
*/
|
2023-02-21 20:36:06 +08:00
|
|
|
initially_inverted |= nrf_gpio_pin_out_read(psel) ?
|
|
|
|
BIT(i) : 0;
|
2022-03-09 17:54:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 17:48:22 +08:00
|
|
|
for (size_t i = 0; i < NRF_PWM_CHANNEL_COUNT; i++) {
|
2023-02-21 20:36:06 +08:00
|
|
|
bool inverted = initially_inverted & BIT(i);
|
2021-09-16 23:55:16 +08:00
|
|
|
|
2024-04-23 17:48:22 +08:00
|
|
|
seq_values_ptr_get(dev)[i] = PWM_NRFX_CH_VALUE(0, inverted);
|
2022-03-09 17:54:05 +08:00
|
|
|
}
|
2018-05-04 05:40:12 +08:00
|
|
|
}
|
|
|
|
|
2024-09-20 19:17:19 +08:00
|
|
|
static void pwm_suspend(const struct device *dev)
|
2018-10-11 21:14:17 +08:00
|
|
|
{
|
2020-05-29 02:44:16 +08:00
|
|
|
const struct pwm_nrfx_config *config = dev->config;
|
2018-10-11 21:14:17 +08:00
|
|
|
|
2024-09-20 19:17:19 +08:00
|
|
|
nrfx_pwm_stop(&config->pwm, false);
|
|
|
|
while (!nrfx_pwm_stopped_check(&config->pwm)) {
|
|
|
|
}
|
2021-06-17 23:23:55 +08:00
|
|
|
|
|
|
|
memset(dev->data, 0, sizeof(struct pwm_nrfx_data));
|
2024-09-20 19:17:19 +08:00
|
|
|
(void)pinctrl_apply_state(config->pcfg, PINCTRL_STATE_SLEEP);
|
2018-10-11 21:14:17 +08:00
|
|
|
}
|
|
|
|
|
2021-11-02 23:19:41 +08:00
|
|
|
static int pwm_nrfx_pm_action(const struct device *dev,
|
|
|
|
enum pm_device_action action)
|
2018-10-11 21:14:17 +08:00
|
|
|
{
|
2024-09-20 19:17:19 +08:00
|
|
|
if (action == PM_DEVICE_ACTION_RESUME) {
|
|
|
|
pwm_resume(dev);
|
|
|
|
} else if (IS_ENABLED(CONFIG_PM_DEVICE) && (action == PM_DEVICE_ACTION_SUSPEND)) {
|
|
|
|
pwm_suspend(dev);
|
|
|
|
} else {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
2018-10-11 21:14:17 +08:00
|
|
|
|
2024-09-20 19:17:19 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2022-03-09 17:54:05 +08:00
|
|
|
|
2024-09-20 19:17:19 +08:00
|
|
|
static int pwm_nrfx_init(const struct device *dev)
|
|
|
|
{
|
|
|
|
const struct pwm_nrfx_config *config = dev->config;
|
|
|
|
nrfx_err_t err;
|
2022-03-09 17:54:05 +08:00
|
|
|
|
2024-09-20 19:17:19 +08:00
|
|
|
ANOMALY_109_EGU_IRQ_CONNECT(NRFX_PWM_NRF52_ANOMALY_109_EGU_INSTANCE);
|
2022-03-09 17:54:05 +08:00
|
|
|
|
2024-09-20 19:17:19 +08:00
|
|
|
if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
|
|
|
|
(void)pinctrl_apply_state(config->pcfg, PINCTRL_STATE_SLEEP);
|
2018-10-11 21:14:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-20 19:17:19 +08:00
|
|
|
err = nrfx_pwm_init(&config->pwm, &config->initial_config, pwm_handler, dev->data);
|
|
|
|
if (err != NRFX_SUCCESS) {
|
|
|
|
LOG_ERR("Failed to initialize device: %s", dev->name);
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
2018-10-11 21:14:17 +08:00
|
|
|
|
2024-09-20 19:17:19 +08:00
|
|
|
return pm_device_driver_init(dev, pwm_nrfx_pm_action);
|
|
|
|
}
|
2018-05-04 05:40:12 +08:00
|
|
|
|
2020-04-18 07:03:55 +08:00
|
|
|
#define PWM(dev_idx) DT_NODELABEL(pwm##dev_idx)
|
|
|
|
#define PWM_PROP(dev_idx, prop) DT_PROP(PWM(dev_idx), prop)
|
2024-04-23 17:48:22 +08:00
|
|
|
#define PWM_HAS_PROP(idx, prop) DT_NODE_HAS_PROP(PWM(idx), prop)
|
|
|
|
|
|
|
|
#define PWM_MEMORY_SECTION(idx) \
|
|
|
|
COND_CODE_1(PWM_HAS_PROP(idx, memory_regions), \
|
|
|
|
(__attribute__((__section__(LINKER_DT_NODE_REGION_NAME( \
|
|
|
|
DT_PHANDLE(PWM(idx), memory_regions)))))), \
|
|
|
|
())
|
2020-04-18 07:03:55 +08:00
|
|
|
|
2018-05-04 05:40:12 +08:00
|
|
|
#define PWM_NRFX_DEVICE(idx) \
|
2023-02-21 21:06:12 +08:00
|
|
|
NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(PWM(idx)); \
|
2023-02-21 20:36:06 +08:00
|
|
|
static struct pwm_nrfx_data pwm_nrfx_##idx##_data; \
|
2024-04-23 17:48:22 +08:00
|
|
|
static uint16_t pwm_##idx##_seq_values[NRF_PWM_CHANNEL_COUNT] \
|
|
|
|
PWM_MEMORY_SECTION(idx); \
|
2023-02-21 20:36:06 +08:00
|
|
|
PINCTRL_DT_DEFINE(PWM(idx)); \
|
2022-05-20 00:52:26 +08:00
|
|
|
static const struct pwm_nrfx_config pwm_nrfx_##idx##_config = { \
|
2018-05-04 05:40:12 +08:00
|
|
|
.pwm = NRFX_PWM_INSTANCE(idx), \
|
2018-12-05 00:44:45 +08:00
|
|
|
.initial_config = { \
|
2023-02-21 20:36:06 +08:00
|
|
|
.skip_gpio_cfg = true, \
|
|
|
|
.skip_psel_cfg = true, \
|
2019-11-08 05:07:47 +08:00
|
|
|
.base_clock = NRF_PWM_CLK_1MHz, \
|
2022-03-09 17:54:05 +08:00
|
|
|
.count_mode = (PWM_PROP(idx, center_aligned) \
|
|
|
|
? NRF_PWM_MODE_UP_AND_DOWN \
|
|
|
|
: NRF_PWM_MODE_UP), \
|
2019-11-08 05:07:47 +08:00
|
|
|
.top_value = 1000, \
|
2018-05-04 05:40:12 +08:00
|
|
|
.load_mode = NRF_PWM_LOAD_INDIVIDUAL, \
|
|
|
|
.step_mode = NRF_PWM_STEP_TRIGGERED, \
|
|
|
|
}, \
|
2024-04-23 17:48:22 +08:00
|
|
|
.seq.values.p_raw = pwm_##idx##_seq_values, \
|
2022-03-09 17:54:05 +08:00
|
|
|
.seq.length = NRF_PWM_CHANNEL_COUNT, \
|
2023-02-21 20:36:06 +08:00
|
|
|
.pcfg = PINCTRL_DT_DEV_CONFIG_GET(PWM(idx)), \
|
2018-05-04 05:40:12 +08:00
|
|
|
}; \
|
2023-10-04 17:17:20 +08:00
|
|
|
static int pwm_nrfx_init##idx(const struct device *dev) \
|
|
|
|
{ \
|
2024-09-20 16:34:13 +08:00
|
|
|
IRQ_CONNECT(DT_IRQN(PWM(idx)), DT_IRQ(PWM(idx), priority), \
|
|
|
|
nrfx_isr, nrfx_pwm_##idx##_irq_handler, 0); \
|
2023-10-04 17:17:20 +08:00
|
|
|
return pwm_nrfx_init(dev); \
|
|
|
|
}; \
|
2021-10-13 18:25:06 +08:00
|
|
|
PM_DEVICE_DT_DEFINE(PWM(idx), pwm_nrfx_pm_action); \
|
2020-12-11 20:36:12 +08:00
|
|
|
DEVICE_DT_DEFINE(PWM(idx), \
|
2023-10-04 17:17:20 +08:00
|
|
|
pwm_nrfx_init##idx, PM_DEVICE_DT_GET(PWM(idx)), \
|
2022-03-09 17:54:05 +08:00
|
|
|
&pwm_nrfx_##idx##_data, \
|
2022-05-20 00:52:26 +08:00
|
|
|
&pwm_nrfx_##idx##_config, \
|
2022-09-21 17:22:06 +08:00
|
|
|
POST_KERNEL, CONFIG_PWM_INIT_PRIORITY, \
|
2022-03-09 17:54:05 +08:00
|
|
|
&pwm_nrfx_drv_api_funcs)
|
2018-05-04 05:40:12 +08:00
|
|
|
|
2024-10-01 14:54:56 +08:00
|
|
|
#define COND_PWM_NRFX_DEVICE(unused, prefix, i, _) \
|
|
|
|
IF_ENABLED(CONFIG_HAS_HW_NRF_PWM##prefix##i, (PWM_NRFX_DEVICE(prefix##i);))
|
2024-04-03 18:40:50 +08:00
|
|
|
|
2024-10-01 14:54:56 +08:00
|
|
|
NRFX_FOREACH_PRESENT(PWM, COND_PWM_NRFX_DEVICE, (), (), _)
|