1 /*
2  * Copyright (c) 2015 - 2020, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  *    list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  *    contributors may be used to endorse or promote products derived from this
17  *    software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef NRF_TWIM_H__
33 #define NRF_TWIM_H__
34 
35 #include <nrfx.h>
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /**
42  * @defgroup nrf_twim_hal TWIM HAL
43  * @{
44  * @ingroup nrf_twim
45  * @brief   Hardware access layer for managing the TWIM peripheral.
46  */
47 
48 /** @brief TWIM tasks. */
49 typedef enum
50 {
51     NRF_TWIM_TASK_STARTRX = offsetof(NRF_TWIM_Type, TASKS_STARTRX), ///< Start TWI receive sequence.
52     NRF_TWIM_TASK_STARTTX = offsetof(NRF_TWIM_Type, TASKS_STARTTX), ///< Start TWI transmit sequence.
53     NRF_TWIM_TASK_STOP    = offsetof(NRF_TWIM_Type, TASKS_STOP),    ///< Stop TWI transaction.
54     NRF_TWIM_TASK_SUSPEND = offsetof(NRF_TWIM_Type, TASKS_SUSPEND), ///< Suspend TWI transaction.
55     NRF_TWIM_TASK_RESUME  = offsetof(NRF_TWIM_Type, TASKS_RESUME)   ///< Resume TWI transaction.
56 } nrf_twim_task_t;
57 
58 /** @brief TWIM events. */
59 typedef enum
60 {
61     NRF_TWIM_EVENT_STOPPED   = offsetof(NRF_TWIM_Type, EVENTS_STOPPED),   ///< TWI stopped.
62     NRF_TWIM_EVENT_ERROR     = offsetof(NRF_TWIM_Type, EVENTS_ERROR),     ///< TWI error.
63     NRF_TWIM_EVENT_SUSPENDED = 0x148,                                     ///< TWI suspended.
64     NRF_TWIM_EVENT_RXSTARTED = offsetof(NRF_TWIM_Type, EVENTS_RXSTARTED), ///< Receive sequence started.
65     NRF_TWIM_EVENT_TXSTARTED = offsetof(NRF_TWIM_Type, EVENTS_TXSTARTED), ///< Transmit sequence started.
66     NRF_TWIM_EVENT_LASTRX    = offsetof(NRF_TWIM_Type, EVENTS_LASTRX),    ///< Byte boundary, starting to receive the last byte.
67     NRF_TWIM_EVENT_LASTTX    = offsetof(NRF_TWIM_Type, EVENTS_LASTTX)     ///< Byte boundary, starting to transmit the last byte.
68 } nrf_twim_event_t;
69 
70 /** @brief TWIM shortcuts. */
71 typedef enum
72 {
73     NRF_TWIM_SHORT_LASTTX_STARTRX_MASK = TWIM_SHORTS_LASTTX_STARTRX_Msk,  ///< Shortcut between LASTTX event and STARTRX task.
74     NRF_TWIM_SHORT_LASTTX_SUSPEND_MASK = TWIM_SHORTS_LASTTX_SUSPEND_Msk,  ///< Shortcut between LASTTX event and SUSPEND task.
75     NRF_TWIM_SHORT_LASTTX_STOP_MASK    = TWIM_SHORTS_LASTTX_STOP_Msk,     ///< Shortcut between LASTTX event and STOP task.
76     NRF_TWIM_SHORT_LASTRX_STARTTX_MASK = TWIM_SHORTS_LASTRX_STARTTX_Msk,  ///< Shortcut between LASTRX event and STARTTX task.
77     NRF_TWIM_SHORT_LASTRX_STOP_MASK    = TWIM_SHORTS_LASTRX_STOP_Msk,     ///< Shortcut between LASTRX event and STOP task.
78     NRF_TWIM_ALL_SHORTS_MASK           = TWIM_SHORTS_LASTTX_STARTRX_Msk |
79                                          TWIM_SHORTS_LASTTX_SUSPEND_Msk |
80                                          TWIM_SHORTS_LASTTX_STOP_Msk    |
81                                          TWIM_SHORTS_LASTRX_STARTTX_Msk |
82                                          TWIM_SHORTS_LASTRX_STOP_Msk      ///< All TWIM shortcuts.
83 } nrf_twim_short_mask_t;
84 
85 /** @brief TWIM interrupts. */
86 typedef enum
87 {
88     NRF_TWIM_INT_STOPPED_MASK   = TWIM_INTENSET_STOPPED_Msk,   ///< Interrupt on STOPPED event.
89     NRF_TWIM_INT_ERROR_MASK     = TWIM_INTENSET_ERROR_Msk,     ///< Interrupt on ERROR event.
90     NRF_TWIM_INT_SUSPENDED_MASK = TWIM_INTENSET_SUSPENDED_Msk, ///< Interrupt on SUSPENDED event.
91     NRF_TWIM_INT_RXSTARTED_MASK = TWIM_INTENSET_RXSTARTED_Msk, ///< Interrupt on RXSTARTED event.
92     NRF_TWIM_INT_TXSTARTED_MASK = TWIM_INTENSET_TXSTARTED_Msk, ///< Interrupt on TXSTARTED event.
93     NRF_TWIM_INT_LASTRX_MASK    = TWIM_INTENSET_LASTRX_Msk,    ///< Interrupt on LASTRX event.
94     NRF_TWIM_INT_LASTTX_MASK    = TWIM_INTENSET_LASTTX_Msk,    ///< Interrupt on LASTTX event.
95     NRF_TWIM_ALL_INTS_MASK      = TWIM_INTENSET_STOPPED_Msk   |
96                                   TWIM_INTENSET_ERROR_Msk     |
97                                   TWIM_INTENSET_SUSPENDED_Msk |
98                                   TWIM_INTENSET_RXSTARTED_Msk |
99                                   TWIM_INTENSET_TXSTARTED_Msk |
100                                   TWIM_INTENSET_LASTRX_Msk    |
101                                   TWIM_INTENSET_LASTTX_Msk     ///< All TWIM interrupts.
102 } nrf_twim_int_mask_t;
103 
104 /** @brief TWIM master clock frequency. */
105 typedef enum
106 {
107     NRF_TWIM_FREQ_100K  = TWIM_FREQUENCY_FREQUENCY_K100, ///< 100 kbps.
108     NRF_TWIM_FREQ_250K  = TWIM_FREQUENCY_FREQUENCY_K250, ///< 250 kbps.
109     NRF_TWIM_FREQ_400K  = TWIM_FREQUENCY_FREQUENCY_K400, ///< 400 kbps.
110 #if defined(TWIM_FREQUENCY_FREQUENCY_K1000) || defined(__NRFX_DOXYGEN__)
111     NRF_TWIM_FREQ_1000K = TWIM_FREQUENCY_FREQUENCY_K1000 ///< 1000 kbps.
112 #endif
113 } nrf_twim_frequency_t;
114 
115 /** @brief TWIM error source. */
116 typedef enum
117 {
118     NRF_TWIM_ERROR_ADDRESS_NACK = TWIM_ERRORSRC_ANACK_Msk,  ///< NACK received after sending the address.
119     NRF_TWIM_ERROR_DATA_NACK    = TWIM_ERRORSRC_DNACK_Msk,  ///< NACK received after sending a data byte.
120     NRF_TWIM_ERROR_OVERRUN      = TWIM_ERRORSRC_OVERRUN_Msk ///< Overrun error.
121                                                             /**< A new byte was received before the previous byte was
122                                                              *   handled by peripheral. (previous data is lost). */
123 } nrf_twim_error_t;
124 
125 
126 /**
127  * @brief Function for activating the specified TWIM task.
128  *
129  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
130  * @param[in] task  Task to be activated.
131  */
132 NRF_STATIC_INLINE void nrf_twim_task_trigger(NRF_TWIM_Type * p_reg,
133                                              nrf_twim_task_t task);
134 
135 /**
136  * @brief Function for getting the address of the specified TWIM task register.
137  *
138  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
139  * @param[in] task  The specified task.
140  *
141  * @return Address of the specified task register.
142  */
143 NRF_STATIC_INLINE uint32_t nrf_twim_task_address_get(NRF_TWIM_Type const * p_reg,
144                                                      nrf_twim_task_t       task);
145 
146 /**
147  * @brief Function for clearing the specified TWIM event.
148  *
149  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
150  * @param[in] event Event to clear.
151  */
152 NRF_STATIC_INLINE void nrf_twim_event_clear(NRF_TWIM_Type *  p_reg,
153                                             nrf_twim_event_t event);
154 
155 /**
156  * @brief Function for retrieving the state of the TWIM event.
157  *
158  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
159  * @param[in] event Event to be checked.
160  *
161  * @retval true  The event has been generated.
162  * @retval false The event has not been generated.
163  */
164 NRF_STATIC_INLINE bool nrf_twim_event_check(NRF_TWIM_Type const * p_reg,
165                                             nrf_twim_event_t      event);
166 
167 /**
168  * @brief Function for getting the address of the specified TWIM event register.
169  *
170  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
171  * @param[in] event The specified event.
172  *
173  * @return Address of the specified event register.
174  */
175 NRF_STATIC_INLINE uint32_t nrf_twim_event_address_get(NRF_TWIM_Type const * p_reg,
176                                                       nrf_twim_event_t      event);
177 
178 /**
179  * @brief Function for enabling the specified shortcuts.
180  *
181  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
182  * @param[in] mask  Shortcuts to be enabled.
183  */
184 NRF_STATIC_INLINE void nrf_twim_shorts_enable(NRF_TWIM_Type * p_reg,
185                                               uint32_t        mask);
186 
187 /**
188  * @brief Function for disabling the specified shortcuts.
189  *
190  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
191  * @param[in] mask  Shortcuts to be disabled.
192  */
193 NRF_STATIC_INLINE void nrf_twim_shorts_disable(NRF_TWIM_Type * p_reg,
194                                                uint32_t        mask);
195 
196 /**
197  * @brief Function for enabling the specified interrupts.
198  *
199  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
200  * @param[in] mask  Mask of interrupts to be enabled.
201  */
202 NRF_STATIC_INLINE void nrf_twim_int_enable(NRF_TWIM_Type * p_reg,
203                                            uint32_t        mask);
204 
205 /**
206  * @brief Function for disabling the specified interrupts.
207  *
208  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
209  * @param[in] mask  Mask of interrupts to be disabled.
210  */
211 NRF_STATIC_INLINE void nrf_twim_int_disable(NRF_TWIM_Type * p_reg,
212                                             uint32_t        mask);
213 
214 /**
215  * @brief Function for checking if the specified interrupts are enabled.
216  *
217  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
218  * @param[in] mask  Mask of interrupts to be checked.
219  *
220  * @return Mask of enabled interrupts.
221  */
222 NRF_STATIC_INLINE uint32_t nrf_twim_int_enable_check(NRF_TWIM_Type const * p_reg, uint32_t mask);
223 
224 #if defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
225 /**
226  * @brief Function for setting the subscribe configuration for a given
227  *        TWIM task.
228  *
229  * @param[in] p_reg   Pointer to the structure of registers of the peripheral.
230  * @param[in] task    Task for which to set the configuration.
231  * @param[in] channel Channel through which to subscribe events.
232  */
233 NRF_STATIC_INLINE void nrf_twim_subscribe_set(NRF_TWIM_Type * p_reg,
234                                               nrf_twim_task_t task,
235                                               uint8_t         channel);
236 
237 /**
238  * @brief Function for clearing the subscribe configuration for a given
239  *        TWIM task.
240  *
241  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
242  * @param[in] task  Task for which to clear the configuration.
243  */
244 NRF_STATIC_INLINE void nrf_twim_subscribe_clear(NRF_TWIM_Type * p_reg,
245                                                 nrf_twim_task_t task);
246 
247 /**
248  * @brief Function for setting the publish configuration for a given
249  *        TWIM event.
250  *
251  * @param[in] p_reg   Pointer to the structure of registers of the peripheral.
252  * @param[in] event   Event for which to set the configuration.
253  * @param[in] channel Channel through which to publish the event.
254  */
255 NRF_STATIC_INLINE void nrf_twim_publish_set(NRF_TWIM_Type *  p_reg,
256                                             nrf_twim_event_t event,
257                                             uint8_t         channel);
258 
259 /**
260  * @brief Function for clearing the publish configuration for a given
261  *        TWIM event.
262  *
263  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
264  * @param[in] event Event for which to clear the configuration.
265  */
266 NRF_STATIC_INLINE void nrf_twim_publish_clear(NRF_TWIM_Type *  p_reg,
267                                               nrf_twim_event_t event);
268 #endif // defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
269 
270 /**
271  * @brief Function for enabling the TWIM peripheral.
272  *
273  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
274  */
275 NRF_STATIC_INLINE void nrf_twim_enable(NRF_TWIM_Type * p_reg);
276 
277 /**
278  * @brief Function for disabling the TWIM peripheral.
279  *
280  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
281  */
282 NRF_STATIC_INLINE void nrf_twim_disable(NRF_TWIM_Type * p_reg);
283 
284 /**
285  * @brief Function for configuring TWI pins.
286  *
287  * @param[in] p_reg   Pointer to the structure of registers of the peripheral.
288  * @param[in] scl_pin SCL pin number.
289  * @param[in] sda_pin SDA pin number.
290  */
291 NRF_STATIC_INLINE void nrf_twim_pins_set(NRF_TWIM_Type * p_reg,
292                                          uint32_t        scl_pin,
293                                          uint32_t        sda_pin);
294 
295 /**
296  * @brief Function for retrieving the SCL pin selection.
297  *
298  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
299  *
300  * @return SCL pin selection.
301  */
302 NRF_STATIC_INLINE uint32_t nrf_twim_scl_pin_get(NRF_TWIM_Type const * p_reg);
303 
304 /**
305  * @brief Function for retrieving the SDA pin selection.
306  *
307  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
308  *
309  * @return SDA pin selection.
310  */
311 NRF_STATIC_INLINE uint32_t nrf_twim_sda_pin_get(NRF_TWIM_Type const * p_reg);
312 
313 /**
314  * @brief Function for setting the TWI master clock frequency.
315  *
316  * @param[in] p_reg     Pointer to the structure of registers of the peripheral.
317  * @param[in] frequency TWI frequency.
318  */
319 NRF_STATIC_INLINE void nrf_twim_frequency_set(NRF_TWIM_Type *      p_reg,
320                                               nrf_twim_frequency_t frequency);
321 
322 /**
323  * @brief Function for checking the TWI error source.
324  *
325  * The error flags are cleared after reading.
326  *
327  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
328  *
329  * @return Mask with error source flags.
330  */
331 NRF_STATIC_INLINE uint32_t nrf_twim_errorsrc_get_and_clear(NRF_TWIM_Type * p_reg);
332 
333 /**
334  * @brief Function for setting the address to be used in TWI transfers.
335  *
336  * @param[in] p_reg   Pointer to the structure of registers of the peripheral.
337  * @param[in] address Address to be used in transfers.
338  */
339 NRF_STATIC_INLINE void nrf_twim_address_set(NRF_TWIM_Type * p_reg,
340                                             uint8_t         address);
341 
342 /**
343  * @brief Function for setting the transmit buffer.
344  *
345  * @param[in] p_reg    Pointer to the structure of registers of the peripheral.
346  * @param[in] p_buffer Pointer to the buffer with data to send.
347  * @param[in] length   Maximum number of data bytes to transmit.
348  */
349 NRF_STATIC_INLINE void nrf_twim_tx_buffer_set(NRF_TWIM_Type * p_reg,
350                                               uint8_t const * p_buffer,
351                                               size_t          length);
352 
353 /**
354  * @brief Function for setting the receive buffer.
355  *
356  * @param[in] p_reg    Pointer to the structure of registers of the peripheral.
357  * @param[in] p_buffer Pointer to the buffer for received data.
358  * @param[in] length   Maximum number of data bytes to receive.
359  */
360 NRF_STATIC_INLINE void nrf_twim_rx_buffer_set(NRF_TWIM_Type * p_reg,
361                                               uint8_t *       p_buffer,
362                                               size_t          length);
363 
364 /**
365  * @brief Function for setting the specified shortcuts.
366  *
367  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
368  * @param[in] mask  Shortcuts to be set.
369  */
370 NRF_STATIC_INLINE void nrf_twim_shorts_set(NRF_TWIM_Type * p_reg,
371                                            uint32_t        mask);
372 
373 /**
374  * @brief Function for getting the shortcut setting.
375  *
376  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
377  *
378  * @return Current shortcut configuration.
379  */
380 NRF_STATIC_INLINE uint32_t nrf_twim_shorts_get(NRF_TWIM_Type const * p_reg);
381 
382 /**
383  * @brief Function for getting the amount of transmitted bytes.
384  *
385  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
386  *
387  * @return Amount of transmitted bytes.
388  */
389 NRF_STATIC_INLINE size_t nrf_twim_txd_amount_get(NRF_TWIM_Type const * p_reg);
390 
391 /**
392  * @brief Function for getting the amount of received bytes.
393  *
394  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
395  *
396  * @return Amount of received bytes.
397  */
398 NRF_STATIC_INLINE size_t nrf_twim_rxd_amount_get(NRF_TWIM_Type const * p_reg);
399 
400 /**
401  * @brief Function for enabling the TX list feature.
402  *
403  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
404  */
405 NRF_STATIC_INLINE void nrf_twim_tx_list_enable(NRF_TWIM_Type * p_reg);
406 
407 /**
408  * @brief Function for disabling the TX list feature.
409  *
410  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
411  */
412 NRF_STATIC_INLINE void nrf_twim_tx_list_disable(NRF_TWIM_Type * p_reg);
413 
414 /**
415  * @brief Function for enabling the RX list feature.
416  *
417  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
418  */
419 NRF_STATIC_INLINE void nrf_twim_rx_list_enable(NRF_TWIM_Type * p_reg);
420 
421 /**
422  * @brief Function for disabling the RX list feature.
423  *
424  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
425  */
426 NRF_STATIC_INLINE void nrf_twim_rx_list_disable(NRF_TWIM_Type * p_reg);
427 
428 
429 #ifndef NRF_DECLARE_ONLY
430 
nrf_twim_task_trigger(NRF_TWIM_Type * p_reg,nrf_twim_task_t task)431 NRF_STATIC_INLINE void nrf_twim_task_trigger(NRF_TWIM_Type * p_reg,
432                                              nrf_twim_task_t task)
433 {
434     *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL;
435 }
436 
nrf_twim_task_address_get(NRF_TWIM_Type const * p_reg,nrf_twim_task_t task)437 NRF_STATIC_INLINE uint32_t nrf_twim_task_address_get(NRF_TWIM_Type const * p_reg,
438                                                      nrf_twim_task_t       task)
439 {
440     return (uint32_t)((uint8_t *)p_reg + (uint32_t)task);
441 }
442 
nrf_twim_event_clear(NRF_TWIM_Type * p_reg,nrf_twim_event_t event)443 NRF_STATIC_INLINE void nrf_twim_event_clear(NRF_TWIM_Type * p_reg,
444                                             nrf_twim_event_t event)
445 {
446     *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
447     nrf_event_readback((uint8_t *)p_reg + (uint32_t)event);
448 }
449 
nrf_twim_event_check(NRF_TWIM_Type const * p_reg,nrf_twim_event_t event)450 NRF_STATIC_INLINE bool nrf_twim_event_check(NRF_TWIM_Type const * p_reg,
451                                             nrf_twim_event_t      event)
452 {
453     return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
454 }
455 
nrf_twim_event_address_get(NRF_TWIM_Type const * p_reg,nrf_twim_event_t event)456 NRF_STATIC_INLINE uint32_t nrf_twim_event_address_get(NRF_TWIM_Type const * p_reg,
457                                                       nrf_twim_event_t      event)
458 {
459     return (uint32_t)((uint8_t *)p_reg + (uint32_t)event);
460 }
461 
nrf_twim_shorts_enable(NRF_TWIM_Type * p_reg,uint32_t mask)462 NRF_STATIC_INLINE void nrf_twim_shorts_enable(NRF_TWIM_Type * p_reg,
463                                               uint32_t mask)
464 {
465     p_reg->SHORTS |= mask;
466 }
467 
nrf_twim_shorts_disable(NRF_TWIM_Type * p_reg,uint32_t mask)468 NRF_STATIC_INLINE void nrf_twim_shorts_disable(NRF_TWIM_Type * p_reg,
469                                                uint32_t mask)
470 {
471     p_reg->SHORTS &= ~(mask);
472 }
473 
nrf_twim_int_enable(NRF_TWIM_Type * p_reg,uint32_t mask)474 NRF_STATIC_INLINE void nrf_twim_int_enable(NRF_TWIM_Type * p_reg,
475                                            uint32_t mask)
476 {
477     p_reg->INTENSET = mask;
478 }
479 
nrf_twim_int_disable(NRF_TWIM_Type * p_reg,uint32_t mask)480 NRF_STATIC_INLINE void nrf_twim_int_disable(NRF_TWIM_Type * p_reg,
481                                             uint32_t mask)
482 {
483     p_reg->INTENCLR = mask;
484 }
485 
nrf_twim_int_enable_check(NRF_TWIM_Type const * p_reg,uint32_t mask)486 NRF_STATIC_INLINE uint32_t nrf_twim_int_enable_check(NRF_TWIM_Type const * p_reg, uint32_t mask)
487 {
488     return p_reg->INTENSET & mask;
489 }
490 
491 #if defined(DPPI_PRESENT)
nrf_twim_subscribe_set(NRF_TWIM_Type * p_reg,nrf_twim_task_t task,uint8_t channel)492 NRF_STATIC_INLINE void nrf_twim_subscribe_set(NRF_TWIM_Type * p_reg,
493                                               nrf_twim_task_t task,
494                                               uint8_t        channel)
495 {
496     *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) =
497             ((uint32_t)channel | TWIM_SUBSCRIBE_STARTRX_EN_Msk);
498 }
499 
nrf_twim_subscribe_clear(NRF_TWIM_Type * p_reg,nrf_twim_task_t task)500 NRF_STATIC_INLINE void nrf_twim_subscribe_clear(NRF_TWIM_Type * p_reg,
501                                                 nrf_twim_task_t task)
502 {
503     *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) = 0;
504 }
505 
nrf_twim_publish_set(NRF_TWIM_Type * p_reg,nrf_twim_event_t event,uint8_t channel)506 NRF_STATIC_INLINE void nrf_twim_publish_set(NRF_TWIM_Type *  p_reg,
507                                             nrf_twim_event_t event,
508                                             uint8_t         channel)
509 {
510     *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) =
511             ((uint32_t)channel | TWIM_PUBLISH_STOPPED_EN_Msk);
512 }
513 
nrf_twim_publish_clear(NRF_TWIM_Type * p_reg,nrf_twim_event_t event)514 NRF_STATIC_INLINE void nrf_twim_publish_clear(NRF_TWIM_Type *  p_reg,
515                                               nrf_twim_event_t event)
516 {
517     *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) = 0;
518 }
519 #endif // defined(DPPI_PRESENT)
520 
nrf_twim_enable(NRF_TWIM_Type * p_reg)521 NRF_STATIC_INLINE void nrf_twim_enable(NRF_TWIM_Type * p_reg)
522 {
523     p_reg->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos);
524 }
525 
nrf_twim_disable(NRF_TWIM_Type * p_reg)526 NRF_STATIC_INLINE void nrf_twim_disable(NRF_TWIM_Type * p_reg)
527 {
528     p_reg->ENABLE = (TWIM_ENABLE_ENABLE_Disabled << TWIM_ENABLE_ENABLE_Pos);
529 }
530 
nrf_twim_pins_set(NRF_TWIM_Type * p_reg,uint32_t scl_pin,uint32_t sda_pin)531 NRF_STATIC_INLINE void nrf_twim_pins_set(NRF_TWIM_Type * p_reg,
532                                          uint32_t scl_pin,
533                                          uint32_t sda_pin)
534 {
535     p_reg->PSEL.SCL = scl_pin;
536     p_reg->PSEL.SDA = sda_pin;
537 }
538 
nrf_twim_scl_pin_get(NRF_TWIM_Type const * p_reg)539 NRF_STATIC_INLINE uint32_t nrf_twim_scl_pin_get(NRF_TWIM_Type const * p_reg)
540 {
541     return p_reg->PSEL.SCL;
542 }
543 
nrf_twim_sda_pin_get(NRF_TWIM_Type const * p_reg)544 NRF_STATIC_INLINE uint32_t nrf_twim_sda_pin_get(NRF_TWIM_Type const * p_reg)
545 {
546     return p_reg->PSEL.SDA;
547 }
548 
nrf_twim_frequency_set(NRF_TWIM_Type * p_reg,nrf_twim_frequency_t frequency)549 NRF_STATIC_INLINE void nrf_twim_frequency_set(NRF_TWIM_Type * p_reg,
550                                               nrf_twim_frequency_t frequency)
551 {
552     p_reg->FREQUENCY = frequency;
553 }
554 
nrf_twim_errorsrc_get_and_clear(NRF_TWIM_Type * p_reg)555 NRF_STATIC_INLINE uint32_t nrf_twim_errorsrc_get_and_clear(NRF_TWIM_Type * p_reg)
556 {
557     uint32_t error_source = p_reg->ERRORSRC;
558 
559     // [error flags are cleared by writing '1' on their position]
560     p_reg->ERRORSRC = error_source;
561 
562     return error_source;
563 }
564 
nrf_twim_address_set(NRF_TWIM_Type * p_reg,uint8_t address)565 NRF_STATIC_INLINE void nrf_twim_address_set(NRF_TWIM_Type * p_reg,
566                                             uint8_t address)
567 {
568     p_reg->ADDRESS = address;
569 }
570 
nrf_twim_tx_buffer_set(NRF_TWIM_Type * p_reg,uint8_t const * p_buffer,size_t length)571 NRF_STATIC_INLINE void nrf_twim_tx_buffer_set(NRF_TWIM_Type * p_reg,
572                                               uint8_t const * p_buffer,
573                                               size_t          length)
574 {
575     p_reg->TXD.PTR    = (uint32_t)p_buffer;
576     p_reg->TXD.MAXCNT = length;
577 }
578 
nrf_twim_rx_buffer_set(NRF_TWIM_Type * p_reg,uint8_t * p_buffer,size_t length)579 NRF_STATIC_INLINE void nrf_twim_rx_buffer_set(NRF_TWIM_Type * p_reg,
580                                               uint8_t * p_buffer,
581                                               size_t    length)
582 {
583     p_reg->RXD.PTR    = (uint32_t)p_buffer;
584     p_reg->RXD.MAXCNT = length;
585 }
586 
nrf_twim_shorts_set(NRF_TWIM_Type * p_reg,uint32_t mask)587 NRF_STATIC_INLINE void nrf_twim_shorts_set(NRF_TWIM_Type * p_reg,
588                                            uint32_t mask)
589 {
590     p_reg->SHORTS = mask;
591 }
592 
nrf_twim_shorts_get(NRF_TWIM_Type const * p_reg)593 NRF_STATIC_INLINE uint32_t nrf_twim_shorts_get(NRF_TWIM_Type const * p_reg)
594 {
595     return p_reg->SHORTS;
596 }
597 
nrf_twim_txd_amount_get(NRF_TWIM_Type const * p_reg)598 NRF_STATIC_INLINE size_t nrf_twim_txd_amount_get(NRF_TWIM_Type const * p_reg)
599 {
600     return p_reg->TXD.AMOUNT;
601 }
602 
nrf_twim_rxd_amount_get(NRF_TWIM_Type const * p_reg)603 NRF_STATIC_INLINE size_t nrf_twim_rxd_amount_get(NRF_TWIM_Type const * p_reg)
604 {
605     return p_reg->RXD.AMOUNT;
606 }
607 
nrf_twim_tx_list_enable(NRF_TWIM_Type * p_reg)608 NRF_STATIC_INLINE void nrf_twim_tx_list_enable(NRF_TWIM_Type * p_reg)
609 {
610     p_reg->TXD.LIST = TWIM_TXD_LIST_LIST_ArrayList << TWIM_TXD_LIST_LIST_Pos;
611 }
612 
nrf_twim_tx_list_disable(NRF_TWIM_Type * p_reg)613 NRF_STATIC_INLINE void nrf_twim_tx_list_disable(NRF_TWIM_Type * p_reg)
614 {
615     p_reg->TXD.LIST = TWIM_TXD_LIST_LIST_Disabled << TWIM_TXD_LIST_LIST_Pos;
616 }
617 
nrf_twim_rx_list_enable(NRF_TWIM_Type * p_reg)618 NRF_STATIC_INLINE void nrf_twim_rx_list_enable(NRF_TWIM_Type * p_reg)
619 {
620     p_reg->RXD.LIST = TWIM_RXD_LIST_LIST_ArrayList << TWIM_RXD_LIST_LIST_Pos;
621 }
622 
nrf_twim_rx_list_disable(NRF_TWIM_Type * p_reg)623 NRF_STATIC_INLINE void nrf_twim_rx_list_disable(NRF_TWIM_Type * p_reg)
624 {
625     p_reg->RXD.LIST = TWIM_RXD_LIST_LIST_Disabled << TWIM_RXD_LIST_LIST_Pos;
626 }
627 #endif // NRF_DECLARE_ONLY
628 
629 /** @} */
630 
631 #ifdef __cplusplus
632 }
633 #endif
634 
635 #endif // NRF_TWIM_H__
636