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
4551struct 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 ((void * )config -> seq .values .p_raw , 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,8 @@ static int pwm_nrfx_set_cycles(const struct device *dev, uint32_t channel,
243255static 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+ * cycles = config -> clock_freq ;
251260
252261 return 0 ;
253262}
@@ -333,13 +342,19 @@ static int pwm_nrfx_init(const struct device *dev)
333342#define PWM (dev_idx ) DT_NODELABEL(pwm##dev_idx)
334343#define PWM_PROP (dev_idx , prop ) DT_PROP(PWM(dev_idx), prop)
335344#define PWM_HAS_PROP (idx , prop ) DT_NODE_HAS_PROP(PWM(idx), prop)
345+ #define PWM_MEM_REGION (idx ) DT_PHANDLE(PWM(idx), memory_regions)
336346
337347#define PWM_MEMORY_SECTION (idx ) \
338348 COND_CODE_1(PWM_HAS_PROP(idx, memory_regions), \
339349 (__attribute__((__section__(LINKER_DT_NODE_REGION_NAME( \
340- DT_PHANDLE(PWM( idx), memory_regions )))))), \
350+ PWM_MEM_REGION( idx)))))), \
341351 ())
342352
353+ #define PWM_GET_MEM_ATTR (idx ) \
354+ COND_CODE_1(PWM_HAS_PROP(idx, memory_regions), \
355+ (COND_CODE_1(DT_NODE_HAS_PROP(PWM_MEM_REGION(idx), zephyr_memory_attr), \
356+ (DT_PROP(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