12
12
#include <nrfx_gpiote.h>
13
13
#include <nrfx_ppi.h>
14
14
#include <hal/nrf_gpio.h>
15
- #include <nrf_peripherals.h>
15
+ #include <hal/nrf_rtc.h>
16
+ #include <hal/nrf_timer.h>
16
17
17
18
#include <zephyr/logging/log.h>
18
19
@@ -162,12 +163,12 @@ static int pwm_nrf5_sw_set_cycles(const struct device *dev, uint32_t channel,
162
163
channel , period_cycles , pulse_cycles );
163
164
164
165
/* clear GPIOTE config */
165
- NRF_GPIOTE -> CONFIG [ gpiote_ch ] = 0 ;
166
+ nrf_gpiote_te_default ( NRF_GPIOTE , gpiote_ch ) ;
166
167
167
168
/* clear PPI used */
168
169
ppi_mask = BIT (ppi_chs [0 ]) | BIT (ppi_chs [1 ]) |
169
170
(PPI_PER_CH > 2 ? BIT (ppi_chs [2 ]) : 0 );
170
- NRF_PPI -> CHENCLR = ppi_mask ;
171
+ nrf_ppi_channels_disable ( NRF_PPI , ppi_mask ) ;
171
172
172
173
active_level = (flags & PWM_POLARITY_INVERTED ) ? 0 : 1 ;
173
174
@@ -193,34 +194,38 @@ static int pwm_nrf5_sw_set_cycles(const struct device *dev, uint32_t channel,
193
194
194
195
/* No PWM generation needed, stop the timer. */
195
196
if (USE_RTC ) {
196
- rtc -> TASKS_STOP = 1 ;
197
+ nrf_rtc_task_trigger ( rtc , NRF_RTC_TASK_STOP ) ;
197
198
} else {
198
- timer -> TASKS_STOP = 1 ;
199
+ nrf_timer_task_trigger ( timer , NRF_TIMER_TASK_STOP ) ;
199
200
}
200
201
201
202
return 0 ;
202
203
}
203
204
204
205
/* configure RTC / TIMER */
205
206
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 ));
208
211
209
212
/*
210
213
* '- 1' adjusts pulse and period cycles to the fact that CLEAR
211
214
* task event is generated always one LFCLK cycle after period
212
215
* COMPARE value is reached.
213
216
*/
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 ) ;
217
220
} 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 );
224
229
}
225
230
226
231
/* 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,
231
236
((uint32_t )active_level << GPIOTE_CONFIG_OUTINIT_Pos );
232
237
233
238
/* 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 ));
234
243
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
+
243
254
#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 );
246
258
#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 ) ;
251
263
#endif
252
264
} 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 ));
261
271
}
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 );
263
282
264
283
/* start timer, hence PWM */
265
284
if (USE_RTC ) {
266
- rtc -> TASKS_START = 1 ;
285
+ nrf_rtc_task_trigger ( rtc , NRF_RTC_TASK_START ) ;
267
286
} else {
268
- timer -> TASKS_START = 1 ;
287
+ nrf_timer_task_trigger ( timer , NRF_TIMER_TASK_START ) ;
269
288
}
270
289
271
290
/* store the period and pulse cycles */
@@ -341,19 +360,18 @@ static int pwm_nrf5_sw_init(const struct device *dev)
341
360
342
361
if (USE_RTC ) {
343
362
/* 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 );
350
368
} else {
351
369
/* 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 ) ;
357
375
}
358
376
359
377
return 0 ;
0 commit comments