Skip to content

Commit 8ece938

Browse files
committed
[nrf fromlist] drivers: pwm: pwm_nrf_sw: Align to use new GPPI API
Use new GPPI API. Upstream PR #: 98327 Signed-off-by: Krzysztof Chruściński <[email protected]>
1 parent 8215842 commit 8ece938

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

drivers/pwm/pwm_nrf_sw.c

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ struct pwm_config {
7272
struct pwm_data {
7373
uint32_t period_cycles;
7474
uint32_t pulse_cycles[PWM_0_MAP_SIZE];
75-
uint8_t ppi_ch[PWM_0_MAP_SIZE][PPI_PER_CH];
75+
nrfx_gppi_handle_t ppi_h[PWM_0_MAP_SIZE][PPI_PER_CH];
7676
uint8_t gpiote_ch[PWM_0_MAP_SIZE];
77+
uint32_t ppi_ch_mask[PWM_0_MAP_SIZE];
7778
};
7879

7980
static inline NRF_RTC_Type *pwm_config_rtc(const struct pwm_config *config)
@@ -130,7 +131,8 @@ static int pwm_nrf_sw_set_cycles(const struct device *dev, uint32_t channel,
130131
uint8_t active_level;
131132
uint8_t psel_ch;
132133
uint8_t gpiote_ch;
133-
const uint8_t *ppi_chs;
134+
const nrfx_gppi_handle_t *ppi_chs;
135+
uint32_t src_domain = nrfx_gppi_domain_id_get(USE_RTC ? (uint32_t)rtc : (uint32_t)timer);
134136
int ret;
135137

136138
if (channel >= config->map_size) {
@@ -171,10 +173,8 @@ static int pwm_nrf_sw_set_cycles(const struct device *dev, uint32_t channel,
171173
LOG_DBG("channel %u, period %u, pulse %u",
172174
channel, period_cycles, pulse_cycles);
173175

174-
/* clear PPI used */
175-
ppi_mask = BIT(ppi_chs[0]) | BIT(ppi_chs[1]) |
176-
(PPI_PER_CH > 2 ? BIT(ppi_chs[2]) : 0);
177-
nrfx_gppi_channels_disable(ppi_mask);
176+
/* disable PPI used */
177+
nrfx_gppi_channels_disable(src_domain, data->ppi_ch_mask[channel]);
178178

179179
active_level = (flags & PWM_POLARITY_INVERTED) ? 0 : 1;
180180

@@ -278,12 +278,10 @@ static int pwm_nrf_sw_set_cycles(const struct device *dev, uint32_t channel,
278278
nrf_rtc_compare_event_get(0));
279279

280280
#if PPI_FORK_AVAILABLE
281-
nrfx_gppi_fork_endpoint_setup(ppi_chs[1],
282-
clear_task_address);
281+
nrfx_gppi_ep_attach(ppi_chs[1], clear_task_address);
283282
#else
284-
nrfx_gppi_channel_endpoints_setup(ppi_chs[2],
285-
period_end_event_address,
286-
clear_task_address);
283+
nrfx_gppi_ep_attach(ppi_chs[2], period_end_event_address);
284+
nrfx_gppi_ep_attach(ppi_chs[2], clear_task_address);
287285
#endif
288286
} else {
289287
pulse_end_event_address =
@@ -294,13 +292,13 @@ static int pwm_nrf_sw_set_cycles(const struct device *dev, uint32_t channel,
294292
nrf_timer_compare_event_get(0));
295293
}
296294

297-
nrfx_gppi_channel_endpoints_setup(ppi_chs[0],
298-
pulse_end_event_address,
299-
pulse_end_task_address);
300-
nrfx_gppi_channel_endpoints_setup(ppi_chs[1],
301-
period_end_event_address,
302-
period_end_task_address);
303-
nrfx_gppi_channels_enable(ppi_mask);
295+
nrfx_gppi_ep_attach(ppi_chs[0], pulse_end_event_address);
296+
nrfx_gppi_ep_attach(ppi_chs[0], pulse_end_task_address);
297+
298+
nrfx_gppi_ep_attach(ppi_chs[1], period_end_event_address);
299+
nrfx_gppi_ep_attach(ppi_chs[1], period_end_task_address);
300+
301+
nrfx_gppi_channels_enable(src_domain, data->ppi_ch_mask[channel]);
304302

305303
/* start timer, hence PWM */
306304
if (USE_RTC) {
@@ -352,18 +350,29 @@ static int pwm_nrf_sw_init(const struct device *dev)
352350

353351
for (uint32_t i = 0; i < config->map_size; i++) {
354352
nrfx_err_t err;
353+
uint32_t src_domain = nrfx_gppi_domain_id_get(USE_RTC ? (uint32_t)rtc : (uint32_t)timer);
354+
uint32_t dst_domain = nrfx_gppi_domain_id_get((uint32_t)config->gpiote[i].p_reg);
355+
int rv;
355356

356357
/* Allocate resources. */
357358
for (uint32_t j = 0; j < PPI_PER_CH; j++) {
358-
err = nrfx_gppi_channel_alloc(&data->ppi_ch[i][j]);
359-
if (err != NRFX_SUCCESS) {
359+
int ch;
360+
361+
rv = nrfx_gppi_domain_conn_alloc(src_domain, dst_domain, &data->ppi_h[i][j]);
362+
if (rv < 0) {
360363
/* Do not free allocated resource. It is a fatal condition,
361364
* system requires reconfiguration.
362365
*/
363366
LOG_ERR("Failed to allocate PPI channel");
364-
return -ENOMEM;
367+
return rv;
365368
}
369+
/* Enable connection but at the end disable channel on the source domain. */
370+
nrfx_gppi_conn_enable(data->ppi_h[i][j]);
371+
ch = nrfx_gppi_domain_channel_get(data->ppi_h[i][j], src_domain);
372+
__ASSERT_NO_MSG(ch >= 0);
373+
data->ppi_ch_mask[i] |= BIT(ch);
366374
}
375+
nrfx_gppi_channels_disable(src_domain, data->ppi_ch_mask[i]);
367376

368377
err = nrfx_gpiote_channel_alloc(&config->gpiote[i],
369378
&data->gpiote_ch[i]);

0 commit comments

Comments
 (0)