Skip to content

Commit b04fd97

Browse files
ananglcarlescufi
authored andcommitted
drivers: pwm_nrf5_sw: Use nrfx HALs instead of direct register accesses
This makes the code easier to maintain. Signed-off-by: Andrzej Głąbek <[email protected]>
1 parent 3c1394c commit b04fd97

File tree

1 file changed

+70
-52
lines changed

1 file changed

+70
-52
lines changed

drivers/pwm/pwm_nrf5_sw.c

Lines changed: 70 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
#include <nrfx_gpiote.h>
1313
#include <nrfx_ppi.h>
1414
#include <hal/nrf_gpio.h>
15-
#include <nrf_peripherals.h>
15+
#include <hal/nrf_rtc.h>
16+
#include <hal/nrf_timer.h>
1617

1718
#include <zephyr/logging/log.h>
1819

@@ -162,12 +163,12 @@ static int pwm_nrf5_sw_set_cycles(const struct device *dev, uint32_t channel,
162163
channel, period_cycles, pulse_cycles);
163164

164165
/* clear GPIOTE config */
165-
NRF_GPIOTE->CONFIG[gpiote_ch] = 0;
166+
nrf_gpiote_te_default(NRF_GPIOTE, gpiote_ch);
166167

167168
/* clear PPI used */
168169
ppi_mask = BIT(ppi_chs[0]) | BIT(ppi_chs[1]) |
169170
(PPI_PER_CH > 2 ? BIT(ppi_chs[2]) : 0);
170-
NRF_PPI->CHENCLR = ppi_mask;
171+
nrf_ppi_channels_disable(NRF_PPI, ppi_mask);
171172

172173
active_level = (flags & PWM_POLARITY_INVERTED) ? 0 : 1;
173174

@@ -193,34 +194,38 @@ static int pwm_nrf5_sw_set_cycles(const struct device *dev, uint32_t channel,
193194

194195
/* No PWM generation needed, stop the timer. */
195196
if (USE_RTC) {
196-
rtc->TASKS_STOP = 1;
197+
nrf_rtc_task_trigger(rtc, NRF_RTC_TASK_STOP);
197198
} else {
198-
timer->TASKS_STOP = 1;
199+
nrf_timer_task_trigger(timer, NRF_TIMER_TASK_STOP);
199200
}
200201

201202
return 0;
202203
}
203204

204205
/* configure RTC / TIMER */
205206
if (USE_RTC) {
206-
rtc->EVENTS_COMPARE[1 + channel] = 0;
207-
rtc->EVENTS_COMPARE[0] = 0;
207+
nrf_rtc_event_clear(rtc,
208+
nrf_rtc_compare_event_get(1 + channel));
209+
nrf_rtc_event_clear(rtc,
210+
nrf_rtc_compare_event_get(0));
208211

209212
/*
210213
* '- 1' adjusts pulse and period cycles to the fact that CLEAR
211214
* task event is generated always one LFCLK cycle after period
212215
* COMPARE value is reached.
213216
*/
214-
rtc->CC[1 + channel] = pulse_cycles - 1;
215-
rtc->CC[0] = period_cycles - 1;
216-
rtc->TASKS_CLEAR = 1;
217+
nrf_rtc_cc_set(rtc, 1 + channel, pulse_cycles - 1);
218+
nrf_rtc_cc_set(rtc, 0, period_cycles - 1);
219+
nrf_rtc_task_trigger(rtc, NRF_RTC_TASK_CLEAR);
217220
} else {
218-
timer->EVENTS_COMPARE[1 + channel] = 0;
219-
timer->EVENTS_COMPARE[0] = 0;
220-
221-
timer->CC[1 + channel] = pulse_cycles;
222-
timer->CC[0] = period_cycles;
223-
timer->TASKS_CLEAR = 1;
221+
nrf_timer_event_clear(timer,
222+
nrf_timer_compare_event_get(1 + channel));
223+
nrf_timer_event_clear(timer,
224+
nrf_timer_compare_event_get(0));
225+
226+
nrf_timer_cc_set(timer, 1 + channel, pulse_cycles);
227+
nrf_timer_cc_set(timer, 0, period_cycles);
228+
nrf_timer_task_trigger(timer, NRF_TIMER_TASK_CLEAR);
224229
}
225230

226231
/* Configure GPIOTE - toggle task with proper initial output value. */
@@ -231,41 +236,55 @@ static int pwm_nrf5_sw_set_cycles(const struct device *dev, uint32_t channel,
231236
((uint32_t)active_level << GPIOTE_CONFIG_OUTINIT_Pos);
232237

233238
/* setup PPI */
239+
uint32_t pulse_end_event_address, period_end_event_address;
240+
uint32_t gpiote_out_task_address =
241+
nrf_gpiote_task_address_get(NRF_GPIOTE,
242+
nrf_gpiote_out_task_get(gpiote_ch));
234243
if (USE_RTC) {
235-
NRF_PPI->CH[ppi_chs[0]].EEP =
236-
(uint32_t) &rtc->EVENTS_COMPARE[1 + channel];
237-
NRF_PPI->CH[ppi_chs[0]].TEP =
238-
(uint32_t) &NRF_GPIOTE->TASKS_OUT[gpiote_ch];
239-
NRF_PPI->CH[ppi_chs[1]].EEP =
240-
(uint32_t) &rtc->EVENTS_COMPARE[0];
241-
NRF_PPI->CH[ppi_chs[1]].TEP =
242-
(uint32_t) &NRF_GPIOTE->TASKS_OUT[gpiote_ch];
244+
uint32_t clear_task_address =
245+
nrf_rtc_event_address_get(rtc, NRF_RTC_TASK_CLEAR);
246+
247+
pulse_end_event_address =
248+
nrf_rtc_event_address_get(rtc,
249+
nrf_rtc_compare_event_get(1 + channel));
250+
period_end_event_address =
251+
nrf_rtc_event_address_get(rtc,
252+
nrf_rtc_compare_event_get(0));
253+
243254
#if defined(PPI_FEATURE_FORKS_PRESENT)
244-
NRF_PPI->FORK[ppi_chs[1]].TEP =
245-
(uint32_t) &rtc->TASKS_CLEAR;
255+
nrf_ppi_fork_endpoint_setup(NRF_PPI,
256+
ppi_chs[1],
257+
clear_task_address);
246258
#else
247-
NRF_PPI->CH[ppi_chs[2]].EEP =
248-
(uint32_t) &rtc->EVENTS_COMPARE[0];
249-
NRF_PPI->CH[ppi_chs[2]].TEP =
250-
(uint32_t) &rtc->TASKS_CLEAR;
259+
nrf_ppi_channel_endpoint_setup(NRF_PPI,
260+
ppi_chs[2],
261+
period_end_event_address,
262+
clear_task_address);
251263
#endif
252264
} else {
253-
NRF_PPI->CH[ppi_chs[0]].EEP =
254-
(uint32_t) &timer->EVENTS_COMPARE[1 + channel];
255-
NRF_PPI->CH[ppi_chs[0]].TEP =
256-
(uint32_t) &NRF_GPIOTE->TASKS_OUT[gpiote_ch];
257-
NRF_PPI->CH[ppi_chs[1]].EEP =
258-
(uint32_t) &timer->EVENTS_COMPARE[0];
259-
NRF_PPI->CH[ppi_chs[1]].TEP =
260-
(uint32_t) &NRF_GPIOTE->TASKS_OUT[gpiote_ch];
265+
pulse_end_event_address =
266+
nrf_timer_event_address_get(timer,
267+
nrf_timer_compare_event_get(1 + channel));
268+
period_end_event_address =
269+
nrf_timer_event_address_get(timer,
270+
nrf_timer_compare_event_get(0));
261271
}
262-
NRF_PPI->CHENSET = ppi_mask;
272+
273+
nrf_ppi_channel_endpoint_setup(NRF_PPI,
274+
ppi_chs[0],
275+
pulse_end_event_address,
276+
gpiote_out_task_address);
277+
nrf_ppi_channel_endpoint_setup(NRF_PPI,
278+
ppi_chs[1],
279+
period_end_event_address,
280+
gpiote_out_task_address);
281+
nrf_ppi_channels_enable(NRF_PPI, ppi_mask);
263282

264283
/* start timer, hence PWM */
265284
if (USE_RTC) {
266-
rtc->TASKS_START = 1;
285+
nrf_rtc_task_trigger(rtc, NRF_RTC_TASK_START);
267286
} else {
268-
timer->TASKS_START = 1;
287+
nrf_timer_task_trigger(timer, NRF_TIMER_TASK_START);
269288
}
270289

271290
/* store the period and pulse cycles */
@@ -341,19 +360,18 @@ static int pwm_nrf5_sw_init(const struct device *dev)
341360

342361
if (USE_RTC) {
343362
/* setup RTC */
344-
rtc->PRESCALER = 0;
345-
346-
rtc->EVTENSET = (RTC_EVTENSET_COMPARE0_Msk |
347-
RTC_EVTENSET_COMPARE1_Msk |
348-
RTC_EVTENSET_COMPARE2_Msk |
349-
RTC_EVTENSET_COMPARE3_Msk);
363+
nrf_rtc_prescaler_set(rtc, 0);
364+
nrf_rtc_event_enable(rtc, NRF_RTC_INT_COMPARE0_MASK |
365+
NRF_RTC_INT_COMPARE1_MASK |
366+
NRF_RTC_INT_COMPARE2_MASK |
367+
NRF_RTC_INT_COMPARE3_MASK);
350368
} else {
351369
/* setup HF timer */
352-
timer->MODE = TIMER_MODE_MODE_Timer;
353-
timer->PRESCALER = config->prescaler;
354-
timer->BITMODE = TIMER_BITMODE_BITMODE_16Bit;
355-
356-
timer->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Msk;
370+
nrf_timer_mode_set(timer, NRF_TIMER_MODE_TIMER);
371+
nrf_timer_prescaler_set(timer, config->prescaler);
372+
nrf_timer_bit_width_set(timer, NRF_TIMER_BIT_WIDTH_16);
373+
nrf_timer_shorts_enable(timer,
374+
NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK);
357375
}
358376

359377
return 0;

0 commit comments

Comments
 (0)