Skip to content

Commit fcd13aa

Browse files
[nrf fromlist] drivers: pwm: nrfx: adjust PWM driver to fast PWM120
Fast PWM120 instance works with 320MHz clock, thus pwm_nrfx_get_cycles_per_sec needs to be adjusted, applying correct clock frequency. Also, it uses cachable RAM, thus sys_cache function needs to be added to flush the cached sequence. Upstream PR #: 80672 Signed-off-by: Michał Stasiak <[email protected]>
1 parent e4cd2f3 commit fcd13aa

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

drivers/pwm/pwm_nrfx.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <hal/nrf_gpio.h>
1212
#include <stdbool.h>
1313
#include <zephyr/linker/devicetree_regions.h>
14+
#include <zephyr/cache.h>
15+
#include <zephyr/mem_mgmt/mem_attr.h>
1416

1517
#include <zephyr/logging/log.h>
1618

@@ -40,6 +42,10 @@ struct pwm_nrfx_config {
4042
nrfx_pwm_config_t initial_config;
4143
nrf_pwm_sequence_t seq;
4244
const struct pinctrl_dev_config *pcfg;
45+
uint32_t clock_freq;
46+
#ifdef CONFIG_DCACHE
47+
uint32_t mem_attr;
48+
#endif
4349
};
4450

4551
struct pwm_nrfx_data {
@@ -176,6 +182,12 @@ static int pwm_nrfx_set_cycles(const struct device *dev, uint32_t channel,
176182

177183
seq_values_ptr_get(dev)[channel] = PWM_NRFX_CH_VALUE(compare_value, inverted);
178184

185+
#ifdef CONFIG_DCACHE
186+
if (config->mem_attr & DT_MEM_CACHEABLE) {
187+
sys_cache_data_flush_range(seq_values_ptr_get(dev), config->seq.length);
188+
}
189+
#endif
190+
179191
LOG_DBG("channel %u, pulse %u, period %u, prescaler: %u.",
180192
channel, pulse_cycles, period_cycles, data->prescaler);
181193

@@ -243,11 +255,9 @@ static int pwm_nrfx_set_cycles(const struct device *dev, uint32_t channel,
243255
static int pwm_nrfx_get_cycles_per_sec(const struct device *dev, uint32_t channel,
244256
uint64_t *cycles)
245257
{
246-
/* TODO: Since this function might be removed, we will always return
247-
* 16MHz from this function and handle the conversion with prescaler,
248-
* etc, in the pin set function. See issue #6958.
249-
*/
250-
*cycles = 16ul * 1000ul * 1000ul;
258+
const struct pwm_nrfx_config *config = dev->config;
259+
260+
*cycles = config->clock_freq;
251261

252262
return 0;
253263
}
@@ -333,13 +343,18 @@ static int pwm_nrfx_init(const struct device *dev)
333343
#define PWM(dev_idx) DT_NODELABEL(pwm##dev_idx)
334344
#define PWM_PROP(dev_idx, prop) DT_PROP(PWM(dev_idx), prop)
335345
#define PWM_HAS_PROP(idx, prop) DT_NODE_HAS_PROP(PWM(idx), prop)
346+
#define PWM_MEM_REGION(idx) DT_PHANDLE(PWM(idx), memory_regions)
336347

337348
#define PWM_MEMORY_SECTION(idx) \
338349
COND_CODE_1(PWM_HAS_PROP(idx, memory_regions), \
339350
(__attribute__((__section__(LINKER_DT_NODE_REGION_NAME( \
340-
DT_PHANDLE(PWM(idx), memory_regions)))))), \
351+
PWM_MEM_REGION(idx)))))), \
341352
())
342353

354+
#define PWM_GET_MEM_ATTR(idx) \
355+
COND_CODE_1(PWM_HAS_PROP(idx, memory_regions), \
356+
(DT_PROP_OR(PWM_MEM_REGION(idx), zephyr_memory_attr, 0)), (0))
357+
343358
#define PWM_NRFX_DEVICE(idx) \
344359
NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(PWM(idx)); \
345360
static struct pwm_nrfx_data pwm_nrfx_##idx##_data; \
@@ -362,6 +377,11 @@ static int pwm_nrfx_init(const struct device *dev)
362377
.seq.values.p_raw = pwm_##idx##_seq_values, \
363378
.seq.length = NRF_PWM_CHANNEL_COUNT, \
364379
.pcfg = PINCTRL_DT_DEV_CONFIG_GET(PWM(idx)), \
380+
.clock_freq = COND_CODE_1(DT_CLOCKS_HAS_IDX(PWM(idx), 0), \
381+
(DT_PROP(DT_CLOCKS_CTLR(PWM(idx)), clock_frequency)), \
382+
(16ul * 1000ul * 1000ul)), \
383+
IF_ENABLED(CONFIG_DCACHE, \
384+
(.mem_attr = PWM_GET_MEM_ATTR(idx),)) \
365385
}; \
366386
static int pwm_nrfx_init##idx(const struct device *dev) \
367387
{ \

0 commit comments

Comments
 (0)