Skip to content
This repository was archived by the owner on Oct 23, 2025. It is now read-only.

Commit 9e594da

Browse files
auroraslbwopu-ot
authored andcommitted
Add some more nrfx timer apis
Signed-off-by: Sletnes Bjørlo, Aurora <[email protected]>
1 parent 0d89959 commit 9e594da

File tree

4 files changed

+288
-0
lines changed

4 files changed

+288
-0
lines changed

src/HW_models/NRF_TIMER.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ void nrf_timer_regw_sideeffects_TASKS_STOP(int t);
8181
void nrf_timer_regw_sideeffects_TASKS_START(int t);
8282
void nrf_timer_regw_sideeffects_TASKS_CLEAR(int t);
8383
void nrf_timer_regw_sideeffects_TASKS_CAPTURE(int t, int cc_n);
84+
void nrf_timer_regw_sideeffects_INTENSET(int t);
85+
void nrf_timer_regw_sideeffects_INTENCLR(int t);
8486
void nrf_timer_regw_sideeffects_CC(int t, int cc_n);
8587

8688
#ifdef __cplusplus

src/nrfx/hal/nrf_rtc.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,15 @@ void nrf_rtc_event_enable(NRF_RTC_Type * p_reg, uint32_t mask);
221221
*/
222222
void nrf_rtc_event_disable(NRF_RTC_Type * p_reg, uint32_t event);
223223

224+
/**
225+
* @brief Function for getting the COMPARE event associated with the specified compare channel.
226+
*
227+
* @param[in] index Compare channel index.
228+
*
229+
* @return Requested COMPARE event.
230+
*/
231+
NRF_STATIC_INLINE nrf_rtc_event_t nrf_rtc_compare_event_get(uint8_t index);
232+
224233
/*****************************/
225234
/* Inlined functions bodies: */
226235
/*****************************/
@@ -262,6 +271,11 @@ NRF_STATIC_INLINE uint32_t nrf_rtc_task_address_get(NRF_RTC_Type const * p_reg,
262271
return (uint32_t)p_reg + task;
263272
}
264273

274+
NRF_STATIC_INLINE nrf_rtc_event_t nrf_rtc_compare_event_get(uint8_t index)
275+
{
276+
return (nrf_rtc_event_t)NRFX_OFFSETOF(NRF_RTC_Type, EVENTS_COMPARE[index]);
277+
}
278+
265279
#ifdef __cplusplus
266280
}
267281
#endif

src/nrfx/hal/nrf_timer.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,29 @@ void nrf_timer_task_trigger(NRF_TIMER_Type * p_reg,
6666
(int) task);
6767
}
6868
}
69+
70+
void nrf_timer_int_enable(NRF_TIMER_Type * p_reg,
71+
uint32_t mask)
72+
{
73+
int i = timer_number_from_ptr(p_reg);
74+
p_reg->INTENSET = mask;
75+
nrf_timer_regw_sideeffects_INTENSET(i);
76+
}
77+
78+
void nrf_timer_int_disable(NRF_TIMER_Type * p_reg,
79+
uint32_t mask)
80+
{
81+
int i = timer_number_from_ptr(p_reg);
82+
p_reg->INTENCLR = mask;
83+
nrf_timer_regw_sideeffects_INTENCLR(i);
84+
}
85+
86+
void nrf_timer_mode_set(NRF_TIMER_Type * p_reg,
87+
nrf_timer_mode_t mode)
88+
{
89+
if (mode == NRF_TIMER_MODE_COUNTER) {
90+
bs_trace_warning_line_time("Counter mode is not fully supported\n");
91+
}
92+
p_reg->MODE = (p_reg->MODE & ~TIMER_MODE_MODE_Msk) |
93+
((mode << TIMER_MODE_MODE_Pos) & TIMER_MODE_MODE_Msk);
94+
}

src/nrfx/hal/nrf_timer.h

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,61 @@ typedef enum
7979
/*lint -restore*/
8080
} nrf_timer_task_t;
8181

82+
/** @brief Timer events. */
83+
typedef enum
84+
{
85+
NRF_TIMER_EVENT_COMPARE0 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[0]), ///< Event from compare channel 0.
86+
NRF_TIMER_EVENT_COMPARE1 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[1]), ///< Event from compare channel 1.
87+
NRF_TIMER_EVENT_COMPARE2 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[2]), ///< Event from compare channel 2.
88+
NRF_TIMER_EVENT_COMPARE3 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[3]), ///< Event from compare channel 3.
89+
#if defined(TIMER_INTENSET_COMPARE4_Msk) || defined(__NRFX_DOXYGEN__)
90+
NRF_TIMER_EVENT_COMPARE4 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[4]), ///< Event from compare channel 4.
91+
#endif
92+
#if defined(TIMER_INTENSET_COMPARE5_Msk) || defined(__NRFX_DOXYGEN__)
93+
NRF_TIMER_EVENT_COMPARE5 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[5]), ///< Event from compare channel 5.
94+
#endif
95+
#if defined(TIMER_INTENSET_COMPARE6_Msk) || defined(__NRFX_DOXYGEN__)
96+
NRF_TIMER_EVENT_COMPARE6 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[6]), ///< Event from compare channel 6.
97+
#endif
98+
#if defined(TIMER_INTENSET_COMPARE7_Msk) || defined(__NRFX_DOXYGEN__)
99+
NRF_TIMER_EVENT_COMPARE7 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[7]), ///< Event from compare channel 7.
100+
#endif
101+
} nrf_timer_event_t;
102+
103+
/** @brief Timer modes. */
104+
typedef enum
105+
{
106+
NRF_TIMER_MODE_TIMER = TIMER_MODE_MODE_Timer, ///< Timer mode: timer.
107+
NRF_TIMER_MODE_COUNTER = TIMER_MODE_MODE_Counter, ///< Timer mode: counter.
108+
#if defined(TIMER_MODE_MODE_LowPowerCounter) || defined(__NRFX_DOXYGEN__)
109+
NRF_TIMER_MODE_LOW_POWER_COUNTER = TIMER_MODE_MODE_LowPowerCounter, ///< Timer mode: low-power counter.
110+
#endif
111+
} nrf_timer_mode_t;
112+
113+
/** @brief Timer bit width. */
114+
typedef enum
115+
{
116+
NRF_TIMER_BIT_WIDTH_8 = TIMER_BITMODE_BITMODE_08Bit, ///< Timer bit width 8 bit.
117+
NRF_TIMER_BIT_WIDTH_16 = TIMER_BITMODE_BITMODE_16Bit, ///< Timer bit width 16 bit.
118+
NRF_TIMER_BIT_WIDTH_24 = TIMER_BITMODE_BITMODE_24Bit, ///< Timer bit width 24 bit.
119+
NRF_TIMER_BIT_WIDTH_32 = TIMER_BITMODE_BITMODE_32Bit ///< Timer bit width 32 bit.
120+
} nrf_timer_bit_width_t;
121+
122+
/** @brief Timer prescalers. */
123+
typedef enum
124+
{
125+
NRF_TIMER_FREQ_16MHz = 0, ///< Timer frequency 16 MHz.
126+
NRF_TIMER_FREQ_8MHz, ///< Timer frequency 8 MHz.
127+
NRF_TIMER_FREQ_4MHz, ///< Timer frequency 4 MHz.
128+
NRF_TIMER_FREQ_2MHz, ///< Timer frequency 2 MHz.
129+
NRF_TIMER_FREQ_1MHz, ///< Timer frequency 1 MHz.
130+
NRF_TIMER_FREQ_500kHz, ///< Timer frequency 500 kHz.
131+
NRF_TIMER_FREQ_250kHz, ///< Timer frequency 250 kHz.
132+
NRF_TIMER_FREQ_125kHz, ///< Timer frequency 125 kHz.
133+
NRF_TIMER_FREQ_62500Hz, ///< Timer frequency 62500 Hz.
134+
NRF_TIMER_FREQ_31250Hz ///< Timer frequency 31250 Hz.
135+
} nrf_timer_frequency_t;
136+
82137
/**
83138
* @brief Timer capture/compare channels.
84139
*/
@@ -105,6 +160,100 @@ typedef enum
105160
void nrf_timer_task_trigger(NRF_TIMER_Type * p_reg,
106161
nrf_timer_task_t task);
107162

163+
/**
164+
* @brief Function for clearing the specified timer event.
165+
*
166+
* @param[in] p_reg Pointer to the structure of registers of the peripheral.
167+
* @param[in] event Event to clear.
168+
*/
169+
NRF_STATIC_INLINE void nrf_timer_event_clear(NRF_TIMER_Type * p_reg,
170+
nrf_timer_event_t event);
171+
172+
/**
173+
* @brief Function for retrieving the state of the TIMER event.
174+
*
175+
* @param[in] p_reg Pointer to the structure of registers of the peripheral.
176+
* @param[in] event Event to be checked.
177+
*
178+
* @retval true The event has been generated.
179+
* @retval false The event has not been generated.
180+
*/
181+
NRF_STATIC_INLINE bool nrf_timer_event_check(NRF_TIMER_Type const * p_reg,
182+
nrf_timer_event_t event);
183+
184+
/**
185+
* @brief Function for enabling the specified shortcuts.
186+
*
187+
* @param[in] p_reg Pointer to the structure of registers of the peripheral.
188+
* @param[in] mask Shortcuts to be enabled.
189+
*/
190+
NRF_STATIC_INLINE void nrf_timer_shorts_enable(NRF_TIMER_Type * p_reg,
191+
uint32_t mask);
192+
193+
/**
194+
* @brief Function for disabling the specified shortcuts.
195+
*
196+
* @param[in] p_reg Pointer to the structure of registers of the peripheral.
197+
* @param[in] mask Shortcuts to be disabled.
198+
*/
199+
NRF_STATIC_INLINE void nrf_timer_shorts_disable(NRF_TIMER_Type * p_reg,
200+
uint32_t mask);
201+
202+
/**
203+
* @brief Function for enabling the specified interrupts.
204+
*
205+
* @param[in] p_reg Pointer to the structure of registers of the peripheral.
206+
* @param[in] mask Mask of interrupts to be enabled.
207+
*/
208+
void nrf_timer_int_enable(NRF_TIMER_Type * p_reg,
209+
uint32_t mask);
210+
211+
/**
212+
* @brief Function for disabling the specified interrupts.
213+
*
214+
* @param[in] p_reg Pointer to the structure of registers of the peripheral.
215+
* @param[in] mask Mask of interrupts to be disabled.
216+
*/
217+
void nrf_timer_int_disable(NRF_TIMER_Type * p_reg,
218+
uint32_t mask);
219+
220+
/**
221+
* @brief Function for checking if the specified interrupts are enabled.
222+
*
223+
* @param[in] p_reg Pointer to the structure of registers of the peripheral.
224+
* @param[in] mask Mask of interrupts to be checked.
225+
*
226+
* @return Mask of enabled interrupts.
227+
*/
228+
NRF_STATIC_INLINE uint32_t nrf_timer_int_enable_check(NRF_TIMER_Type const * p_reg, uint32_t mask);
229+
230+
/**
231+
* @brief Function for setting the timer mode.
232+
*
233+
* @param[in] p_reg Pointer to the structure of registers of the peripheral.
234+
* @param[in] mode Timer mode.
235+
*/
236+
void nrf_timer_mode_set(NRF_TIMER_Type * p_reg,
237+
nrf_timer_mode_t mode);
238+
239+
/**
240+
* @brief Function for setting the timer bit width.
241+
*
242+
* @param[in] p_reg Pointer to the structure of registers of the peripheral.
243+
* @param[in] bit_width Timer bit width.
244+
*/
245+
NRF_STATIC_INLINE void nrf_timer_bit_width_set(NRF_TIMER_Type * p_reg,
246+
nrf_timer_bit_width_t bit_width);
247+
248+
/**
249+
* @brief Function for setting the timer frequency.
250+
*
251+
* @param[in] p_reg Pointer to the structure of registers of the peripheral.
252+
* @param[in] frequency Timer frequency.
253+
*/
254+
NRF_STATIC_INLINE void nrf_timer_frequency_set(NRF_TIMER_Type * p_reg,
255+
nrf_timer_frequency_t frequency);
256+
108257
/**
109258
* @brief Function for setting the capture/compare register for the specified channel.
110259
*
@@ -116,6 +265,103 @@ void nrf_timer_cc_set(NRF_TIMER_Type * p_reg,
116265
nrf_timer_cc_channel_t cc_channel,
117266
uint32_t cc_value);
118267

268+
/**
269+
* @brief Function for retrieving the capture/compare value for a specified channel.
270+
*
271+
* @param[in] p_reg Pointer to the structure of registers of the peripheral.
272+
* @param[in] cc_channel The specified capture/compare channel.
273+
*
274+
* @return Value from the specified capture/compare register.
275+
*/
276+
NRF_STATIC_INLINE uint32_t nrf_timer_cc_get(NRF_TIMER_Type const * p_reg,
277+
nrf_timer_cc_channel_t cc_channel);
278+
279+
/**
280+
* @brief Function for getting the specified timer capture task.
281+
*
282+
* @param[in] channel Capture channel.
283+
*
284+
* @return Capture task.
285+
*/
286+
NRF_STATIC_INLINE nrf_timer_task_t nrf_timer_capture_task_get(uint32_t channel);
287+
288+
/**
289+
* @brief Function for getting the specified timer compare event.
290+
*
291+
* @param[in] channel Compare channel.
292+
*
293+
* @return Compare event.
294+
*/
295+
NRF_STATIC_INLINE nrf_timer_event_t nrf_timer_compare_event_get(uint32_t channel);
296+
297+
/*****************************/
298+
/* Inlined functions bodies: */
299+
/*****************************/
300+
301+
NRF_STATIC_INLINE void nrf_timer_event_clear(NRF_TIMER_Type * p_reg,
302+
nrf_timer_event_t event)
303+
{
304+
*((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
305+
#if __CORTEX_M == 0x04
306+
volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event));
307+
(void)dummy;
308+
#endif
309+
}
310+
311+
NRF_STATIC_INLINE bool nrf_timer_event_check(NRF_TIMER_Type const * p_reg,
312+
nrf_timer_event_t event)
313+
{
314+
return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
315+
}
316+
317+
NRF_STATIC_INLINE void nrf_timer_shorts_enable(NRF_TIMER_Type * p_reg,
318+
uint32_t mask)
319+
{
320+
p_reg->SHORTS |= mask;
321+
}
322+
323+
NRF_STATIC_INLINE void nrf_timer_shorts_disable(NRF_TIMER_Type * p_reg,
324+
uint32_t mask)
325+
{
326+
p_reg->SHORTS &= ~(mask);
327+
}
328+
329+
NRF_STATIC_INLINE uint32_t nrf_timer_int_enable_check(NRF_TIMER_Type const * p_reg, uint32_t mask)
330+
{
331+
return p_reg->INTENSET & mask;
332+
}
333+
334+
NRF_STATIC_INLINE void nrf_timer_bit_width_set(NRF_TIMER_Type * p_reg,
335+
nrf_timer_bit_width_t bit_width)
336+
{
337+
p_reg->BITMODE = (p_reg->BITMODE & ~TIMER_BITMODE_BITMODE_Msk) |
338+
((bit_width << TIMER_BITMODE_BITMODE_Pos) &
339+
TIMER_BITMODE_BITMODE_Msk);
340+
}
341+
342+
NRF_STATIC_INLINE void nrf_timer_frequency_set(NRF_TIMER_Type * p_reg,
343+
nrf_timer_frequency_t frequency)
344+
{
345+
p_reg->PRESCALER = (p_reg->PRESCALER & ~TIMER_PRESCALER_PRESCALER_Msk) |
346+
((frequency << TIMER_PRESCALER_PRESCALER_Pos) &
347+
TIMER_PRESCALER_PRESCALER_Msk);
348+
}
349+
350+
NRF_STATIC_INLINE uint32_t nrf_timer_cc_get(NRF_TIMER_Type const * p_reg,
351+
nrf_timer_cc_channel_t cc_channel)
352+
{
353+
return (uint32_t)p_reg->CC[cc_channel];
354+
}
355+
356+
NRF_STATIC_INLINE nrf_timer_task_t nrf_timer_capture_task_get(uint32_t channel)
357+
{
358+
return (nrf_timer_task_t)NRFX_OFFSETOF(NRF_TIMER_Type, TASKS_CAPTURE[channel]);
359+
}
360+
361+
NRF_STATIC_INLINE nrf_timer_event_t nrf_timer_compare_event_get(uint32_t channel)
362+
{
363+
return (nrf_timer_event_t)NRFX_OFFSETOF(NRF_TIMER_Type, EVENTS_COMPARE[channel]);
364+
}
119365

120366
#ifdef __cplusplus
121367
}

0 commit comments

Comments
 (0)