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_TWIS_H__
33 #define NRF_TWIS_H__
34 
35 #include <nrfx.h>
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /**
42  * @defgroup nrf_twis_hal TWIS HAL
43  * @{
44  * @ingroup nrf_twis
45  * @brief   Hardware access layer for managing the Two Wire Interface Slave with EasyDMA
46  *          (TWIS) peripheral.
47  */
48 
49 /** @brief TWIS tasks. */
50 typedef enum
51 {
52     NRF_TWIS_TASK_STOP      = offsetof(NRF_TWIS_Type, TASKS_STOP),      /**< Stop TWIS transaction. */
53     NRF_TWIS_TASK_SUSPEND   = offsetof(NRF_TWIS_Type, TASKS_SUSPEND),   /**< Suspend TWIS transaction. */
54     NRF_TWIS_TASK_RESUME    = offsetof(NRF_TWIS_Type, TASKS_RESUME),    /**< Resume TWIS transaction. */
55     NRF_TWIS_TASK_PREPARERX = offsetof(NRF_TWIS_Type, TASKS_PREPARERX), /**< Prepare the TWIS slave to respond to a write command. */
56     NRF_TWIS_TASK_PREPARETX = offsetof(NRF_TWIS_Type, TASKS_PREPARETX)  /**< Prepare the TWIS slave to respond to a read command. */
57 } nrf_twis_task_t;
58 
59 /** @brief TWIS events. */
60 typedef enum
61 {
62     NRF_TWIS_EVENT_STOPPED   = offsetof(NRF_TWIS_Type, EVENTS_STOPPED),   /**< TWIS stopped. */
63     NRF_TWIS_EVENT_ERROR     = offsetof(NRF_TWIS_Type, EVENTS_ERROR),     /**< TWIS error. */
64     NRF_TWIS_EVENT_RXSTARTED = offsetof(NRF_TWIS_Type, EVENTS_RXSTARTED), /**< Receive sequence started. */
65     NRF_TWIS_EVENT_TXSTARTED = offsetof(NRF_TWIS_Type, EVENTS_TXSTARTED), /**< Transmit sequence started. */
66     NRF_TWIS_EVENT_WRITE     = offsetof(NRF_TWIS_Type, EVENTS_WRITE),     /**< Write command received. */
67     NRF_TWIS_EVENT_READ      = offsetof(NRF_TWIS_Type, EVENTS_READ)       /**< Read command received. */
68 } nrf_twis_event_t;
69 
70 /** @brief TWIS shortcuts. */
71 typedef enum
72 {
73     NRF_TWIS_SHORT_WRITE_SUSPEND_MASK   = TWIS_SHORTS_WRITE_SUSPEND_Msk,   /**< Shortcut between WRITE event and SUSPEND task. */
74     NRF_TWIS_SHORT_READ_SUSPEND_MASK    = TWIS_SHORTS_READ_SUSPEND_Msk,    /**< Shortcut between READ event and SUSPEND task. */
75 } nrf_twis_short_mask_t;
76 
77 /** @brief TWIS interrupts. */
78 typedef enum
79 {
80     NRF_TWIS_INT_STOPPED_MASK   = TWIS_INTEN_STOPPED_Msk,   /**< Interrupt on STOPPED event. */
81     NRF_TWIS_INT_ERROR_MASK     = TWIS_INTEN_ERROR_Msk,     /**< Interrupt on ERROR event. */
82     NRF_TWIS_INT_RXSTARTED_MASK = TWIS_INTEN_RXSTARTED_Msk, /**< Interrupt on RXSTARTED event. */
83     NRF_TWIS_INT_TXSTARTED_MASK = TWIS_INTEN_TXSTARTED_Msk, /**< Interrupt on TXSTARTED event. */
84     NRF_TWIS_INT_WRITE_MASK     = TWIS_INTEN_WRITE_Msk,     /**< Interrupt on WRITE event. */
85     NRF_TWIS_INT_READ_MASK      = TWIS_INTEN_READ_Msk,      /**< Interrupt on READ event. */
86 } nrf_twis_int_mask_t;
87 
88 /** @brief TWIS error source. */
89 typedef enum
90 {
91     NRF_TWIS_ERROR_OVERFLOW  = TWIS_ERRORSRC_OVERFLOW_Msk, /**< RX buffer overflow detected, and prevented. */
92     NRF_TWIS_ERROR_DATA_NACK = TWIS_ERRORSRC_DNACK_Msk,    /**< NACK sent after receiving a data byte. */
93     NRF_TWIS_ERROR_OVERREAD  = TWIS_ERRORSRC_OVERREAD_Msk  /**< TX buffer over-read detected, and prevented. */
94 } nrf_twis_error_t;
95 
96 /** @brief TWIS address matching configuration. */
97 typedef enum
98 {
99     NRF_TWIS_CONFIG_ADDRESS0_MASK  = TWIS_CONFIG_ADDRESS0_Msk, /**< Enable or disable address matching on ADDRESS[0]. */
100     NRF_TWIS_CONFIG_ADDRESS1_MASK  = TWIS_CONFIG_ADDRESS1_Msk, /**< Enable or disable address matching on ADDRESS[1]. */
101     NRF_TWIS_CONFIG_ADDRESS01_MASK = TWIS_CONFIG_ADDRESS0_Msk | TWIS_CONFIG_ADDRESS1_Msk /**< Enable both address matching. */
102 } nrf_twis_config_addr_mask_t;
103 
104 /**
105  * @brief Smallest variable type to hold the TWI address.
106  *
107  * Variable of the minimum size that can hold a single TWI address.
108  *
109  * @note Defined to make it simple to change if the new TWI supports for example
110  *       10 bit addressing mode.
111  */
112 typedef uint8_t nrf_twis_address_t;
113 
114 /**
115  * @brief Function for activating the specified TWIS task.
116  *
117  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
118  * @param[in] task  Task to be activated.
119  */
120 NRF_STATIC_INLINE void nrf_twis_task_trigger(NRF_TWIS_Type * p_reg, nrf_twis_task_t task);
121 
122 /**
123  * @brief Function for returning the address of the specified TWIS task register.
124  *
125  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
126  * @param[in] task  The specified task.
127  *
128  * @return Task address.
129  */
130 NRF_STATIC_INLINE uint32_t nrf_twis_task_address_get(NRF_TWIS_Type const * p_reg,
131                                                      nrf_twis_task_t       task);
132 
133 /**
134  * @brief Function for clearing the specified event.
135  *
136  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
137  * @param[in] event The specified event.
138  */
139 NRF_STATIC_INLINE void nrf_twis_event_clear(NRF_TWIS_Type *  p_reg,
140                                             nrf_twis_event_t event);
141 
142 /**
143  * @brief Function for retrieving the state of the TWIS event.
144  *
145  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
146  * @param[in] event Event to be checked.
147  *
148  * @retval true  The event has been generated.
149  * @retval false The event has not been generated.
150  */
151 NRF_STATIC_INLINE bool nrf_twis_event_check(NRF_TWIS_Type const * p_reg,
152                                             nrf_twis_event_t      event);
153 
154 /**
155  * @brief Function for getting and clearing the state of the specified event.
156  *
157  * This function checks the state of the event and clears it.
158  *
159  * @param[in,out] p_reg Pointer to the structure of registers of the peripheral.
160  * @param[in]     event Event.
161  *
162  * @retval true  The event was set.
163  * @retval false The event was not set.
164  */
165 NRF_STATIC_INLINE bool nrf_twis_event_get_and_clear(NRF_TWIS_Type *  p_reg,
166                                                     nrf_twis_event_t event);
167 
168 /**
169  * @brief Function for returning the address of the specified TWIS event register.
170  *
171  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
172  * @param[in] event Event.
173  *
174  * @return Address.
175  */
176 NRF_STATIC_INLINE uint32_t nrf_twis_event_address_get(NRF_TWIS_Type const * p_reg,
177                                                       nrf_twis_event_t      event);
178 
179 /**
180  * @brief Function for setting a shortcut.
181  *
182  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
183  * @param[in] mask  Mask of shortcuts to be enabled.
184  */
185 NRF_STATIC_INLINE void nrf_twis_shorts_enable(NRF_TWIS_Type * p_reg, uint32_t mask);
186 
187 /**
188  * @brief Function for clearing shortcuts.
189  *
190  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
191  * @param[in] mask  Mask of shortcuts to be disabled.
192  */
193 NRF_STATIC_INLINE void nrf_twis_shorts_disable(NRF_TWIS_Type * p_reg, uint32_t mask);
194 
195 /**
196  * @brief Function for getting the shorts mask.
197  *
198  * Function returns shorts register.
199  *
200  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
201  *
202  * @return Flags of currently enabled shortcuts
203  */
204 NRF_STATIC_INLINE uint32_t nrf_twis_shorts_get(NRF_TWIS_Type const * p_reg);
205 
206 /**
207  * @brief Function for enabling the specified interrupts.
208  *
209  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
210  * @param[in] mask  Mask of interrupts to be enabled.
211  */
212 NRF_STATIC_INLINE void nrf_twis_int_enable(NRF_TWIS_Type * p_reg, 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_twis_int_enable_check(NRF_TWIS_Type const * p_reg, uint32_t mask);
223 
224 /**
225  * @brief Function for disabling the specified interrupts.
226  *
227  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
228  * @param[in] mask  Mask of interrupts to be disabled.
229  */
230 NRF_STATIC_INLINE void nrf_twis_int_disable(NRF_TWIS_Type * p_reg, uint32_t mask);
231 
232 #if defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
233 /**
234  * @brief Function for setting the subscribe configuration for a given
235  *        TWIS task.
236  *
237  * @param[in] p_reg   Pointer to the structure of registers of the peripheral.
238  * @param[in] task    Task for which to set the configuration.
239  * @param[in] channel Channel through which to subscribe events.
240  */
241 NRF_STATIC_INLINE void nrf_twis_subscribe_set(NRF_TWIS_Type * p_reg,
242                                               nrf_twis_task_t task,
243                                               uint8_t         channel);
244 
245 /**
246  * @brief Function for clearing the subscribe configuration for a given
247  *        TWIS task.
248  *
249  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
250  * @param[in] task  Task for which to clear the configuration.
251  */
252 NRF_STATIC_INLINE void nrf_twis_subscribe_clear(NRF_TWIS_Type * p_reg,
253                                                 nrf_twis_task_t task);
254 
255 /**
256  * @brief Function for setting the publish configuration for a given
257  *        TWIS event.
258  *
259  * @param[in] p_reg   Pointer to the structure of registers of the peripheral.
260  * @param[in] event   Event for which to set the configuration.
261  * @param[in] channel Channel through which to publish the event.
262  */
263 NRF_STATIC_INLINE void nrf_twis_publish_set(NRF_TWIS_Type *  p_reg,
264                                             nrf_twis_event_t event,
265                                             uint8_t         channel);
266 
267 /**
268  * @brief Function for clearing the publish configuration for a given
269  *        TWIS event.
270  *
271  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
272  * @param[in] event Event for which to clear the configuration.
273  */
274 NRF_STATIC_INLINE void nrf_twis_publish_clear(NRF_TWIS_Type *  p_reg,
275                                               nrf_twis_event_t event);
276 #endif // defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
277 
278 /**
279  * @brief Function for retrieving and clearing the TWIS error source.
280  *
281  * @attention Error sources are cleared after read.
282  *
283  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
284  *
285  * @return Error source mask with values from @ref nrf_twis_error_t.
286  */
287 NRF_STATIC_INLINE uint32_t nrf_twis_error_source_get_and_clear(NRF_TWIS_Type * p_reg);
288 
289 /**
290  * @brief Function for getting information about which of the addresses matched.
291  *
292  * Function returns index in the address table
293  * that points to the address that already matched.
294  *
295  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
296  *
297  * @return Index of matched address.
298  */
299 NRF_STATIC_INLINE uint_fast8_t nrf_twis_match_get(NRF_TWIS_Type const * p_reg);
300 
301 /**
302  * @brief Function for enabling TWIS.
303  *
304  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
305  */
306 NRF_STATIC_INLINE void nrf_twis_enable(NRF_TWIS_Type * p_reg);
307 
308 /**
309  * @brief Function for disabling TWIS.
310  *
311  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
312  */
313 NRF_STATIC_INLINE void nrf_twis_disable(NRF_TWIS_Type * p_reg);
314 
315 /**
316  * @brief Function for configuring TWIS pins.
317  *
318  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
319  * @param[in] scl   SCL pin number.
320  * @param[in] sda   SDA pin number.
321  */
322 NRF_STATIC_INLINE void nrf_twis_pins_set(NRF_TWIS_Type * p_reg, uint32_t scl, uint32_t sda);
323 
324 /**
325  * @brief Function for retrieving the SCL pin selection.
326  *
327  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
328  *
329  * @return SCL pin selection.
330  */
331 NRF_STATIC_INLINE uint32_t nrf_twis_scl_pin_get(NRF_TWIS_Type const * p_reg);
332 
333 /**
334  * @brief Function for retrieving the SDA pin selection.
335  *
336  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
337  *
338  * @return SDA pin selection.
339  */
340 NRF_STATIC_INLINE uint32_t nrf_twis_sda_pin_get(NRF_TWIS_Type const * p_reg);
341 
342 /**
343  * @brief Function for setting the receive buffer.
344  *
345  * @param[in] p_reg  Pointer to the structure of registers of the peripheral.
346  * @param[in] p_buf  Pointer to the buffer for received data.
347  * @param[in] length Maximum number of data bytes to receive.
348  */
349 NRF_STATIC_INLINE void nrf_twis_rx_buffer_set(NRF_TWIS_Type * p_reg,
350                                               uint8_t       * p_buf,
351                                               size_t          length);
352 
353 /**
354  * @brief Function that prepares TWIS for receiving
355  *
356  * This function sets receive buffer and then sets NRF_TWIS_TASK_PREPARERX task.
357  *
358  * @param[in] p_reg  Pointer to the structure of registers of the peripheral.
359  * @param[in] p_buf  Pointer to the buffer for received data.
360  * @param[in] length Maximum number of data bytes to receive.
361  */
362 NRF_STATIC_INLINE void nrf_twis_rx_prepare(NRF_TWIS_Type * p_reg,
363                                            uint8_t       * p_buf,
364                                            size_t          length);
365 
366 /**
367  * @brief Function for getting number of bytes received in the last transaction.
368  *
369  * @param[in] p_reg TWIS instance.
370  *
371  * @return Amount of bytes received.
372  * */
373 NRF_STATIC_INLINE size_t nrf_twis_rx_amount_get(NRF_TWIS_Type const * p_reg);
374 
375 /**
376  * @brief Function for setting the transmit buffer.
377  *
378  * @param[in] p_reg  Pointer to the structure of registers of the peripheral.
379  * @param[in] p_buf  Pointer to the buffer with data to send.
380  * @param[in] length Maximum number of data bytes to transmit.
381  */
382 NRF_STATIC_INLINE void nrf_twis_tx_buffer_set(NRF_TWIS_Type * p_reg,
383                                               uint8_t const * p_buf,
384                                               size_t          length);
385 
386 /**
387  * @brief Function for preparing TWIS for transmitting.
388  *
389  * This function sets transmit buffer and then sets NRF_TWIS_TASK_PREPARETX task.
390  *
391  * @param[in] p_reg  Pointer to the structure of registers of the peripheral.
392  * @param[in] p_buf  Pointer to the buffer with data to send.
393  * @param[in] length Maximum number of data bytes to transmit.
394  */
395 NRF_STATIC_INLINE void nrf_twis_tx_prepare(NRF_TWIS_Type * p_reg,
396                                            uint8_t const * p_buf,
397                                            size_t          length);
398 
399 /**
400  * @brief Function for getting the number of bytes transmitted in the last transaction.
401  *
402  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
403  *
404  * @return Amount of bytes transmitted.
405  */
406 NRF_STATIC_INLINE size_t nrf_twis_tx_amount_get(NRF_TWIS_Type const * p_reg);
407 
408 /**
409  * @brief Function for setting the slave address.
410  *
411  * Function sets the selected address for this TWI interface.
412  *
413  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
414  * @param[in] n     Index of address to be set.
415  * @param[in] addr  Addres to be set.
416  *
417  * @sa nrf_twis_config_address_set
418  * @sa nrf_twis_config_address_get
419  */
420 NRF_STATIC_INLINE void nrf_twis_address_set(NRF_TWIS_Type *    p_reg,
421                                             uint_fast8_t       n,
422                                             nrf_twis_address_t addr);
423 
424 /**
425  * @brief Function for retrieving configured slave address.
426  *
427  * Function gets the selected address for this TWI interface.
428  *
429  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
430  * @param[in] n     Index of address to get.
431  *
432  * @return Configured slave address.
433  */
434 NRF_STATIC_INLINE nrf_twis_address_t nrf_twis_address_get(NRF_TWIS_Type const * p_reg,
435                                                           uint_fast8_t          n);
436 
437 /**
438  * @brief Function for setting the device address configuration.
439  *
440  * @param[in] p_reg     Pointer to the structure of registers of the peripheral.
441  * @param[in] addr_mask Mask of address indexes of what device should answer to.
442  *
443  * @sa nrf_twis_address_set
444  */
445 NRF_STATIC_INLINE void nrf_twis_config_address_set(NRF_TWIS_Type *             p_reg,
446                                                    nrf_twis_config_addr_mask_t addr_mask);
447 
448 /**
449  * @brief Function for retrieving the device address configuration.
450  *
451  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
452  *
453  * @return Mask of address indexes of what device should answer to.
454  */
455 NRF_STATIC_INLINE nrf_twis_config_addr_mask_t nrf_twis_config_address_get(
456         NRF_TWIS_Type const * p_reg);
457 
458 /**
459  * @brief Function for setting the over-read character.
460  *
461  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
462  * @param[in] orc   Over-read character. Character clocked out in case of
463  *                  over-read of the TXD buffer.
464  */
465 NRF_STATIC_INLINE void nrf_twis_orc_set(NRF_TWIS_Type * p_reg,
466                                         uint8_t         orc);
467 
468 /**
469  * @brief Function for setting the over-read character.
470  *
471  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
472  *
473  * @return Over-read character configured for selected instance.
474  */
475 NRF_STATIC_INLINE uint8_t nrf_twis_orc_get(NRF_TWIS_Type const * p_reg);
476 
477 #if defined(TWIS_TXD_LIST_LIST_Msk) || defined(__NRFX_DOXYGEN__)
478 /**
479  * @brief Function for enabling the TX list feature.
480  *
481  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
482  */
483 NRF_STATIC_INLINE void nrf_twis_tx_list_enable(NRF_TWIS_Type * p_reg);
484 
485 /**
486  * @brief Function for disabling the TX list feature.
487  *
488  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
489  */
490 NRF_STATIC_INLINE void nrf_twis_tx_list_disable(NRF_TWIS_Type * p_reg);
491 #endif // defined(TWIS_TXD_LIST_LIST_Msk) || defined(__NRFX_DOXYGEN__)
492 
493 #if defined(TWIS_RXD_LIST_LIST_Msk) || defined(__NRFX_DOXYGEN__)
494 /**
495  * @brief Function for enabling the RX list feature.
496  *
497  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
498  */
499 NRF_STATIC_INLINE void nrf_twis_rx_list_enable(NRF_TWIS_Type * p_reg);
500 
501 /**
502  * @brief Function for disabling the RX list feature.
503  *
504  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
505  */
506 NRF_STATIC_INLINE void nrf_twis_rx_list_disable(NRF_TWIS_Type * p_reg);
507 #endif // defined(TWIS_RXD_LIST_LIST_Msk) || defined(__NRFX_DOXYGEN__)
508 
509 /** @} */ /*  End of nrf_twis_hal */
510 
511 #ifndef NRF_DECLARE_ONLY
512 
513 /* ------------------------------------------------------------------------------------------------
514  *  Internal functions
515  */
516 
517 /**
518  * @internal
519  * @brief Internal function for getting task or event register address.
520  *
521  * @param[in] p_reg  Pointer to the structure of registers of the peripheral.
522  * @param[in] offset Offset of the register from the beginning of the instance.
523  *
524  * @attention Offset must be modulo 4 value. In other case, hardware fault can occur.
525  * @return Pointer to the register.
526  */
nrf_twis_getRegPtr(NRF_TWIS_Type * p_reg,uint32_t offset)527 NRF_STATIC_INLINE volatile uint32_t* nrf_twis_getRegPtr(NRF_TWIS_Type * p_reg, uint32_t offset)
528 {
529     return (volatile uint32_t*)((uint8_t *)p_reg + (uint32_t)offset);
530 }
531 
532 /**
533  * @internal
534  * @brief Internal function for getting task/event register address - constant version.
535  *
536  * @param[in] p_reg  Pointer to the structure of registers of the peripheral.
537  * @param[in] offset Offset of the register from the beginning of the instance.
538  *
539  * @attention Offset must be modulo 4 value. In other case, hardware fault can occur.
540  * @return Pointer to the register.
541  */
nrf_twis_getRegPtr_c(NRF_TWIS_Type const * p_reg,uint32_t offset)542 NRF_STATIC_INLINE volatile const uint32_t* nrf_twis_getRegPtr_c(NRF_TWIS_Type const * p_reg,
543                                                                 uint32_t              offset)
544 {
545     return (volatile const uint32_t*)((uint8_t *)p_reg + (uint32_t)offset);
546 }
547 
548 
549 /* ------------------------------------------------------------------------------------------------
550  *  Interface functions definitions
551  */
552 
553 
nrf_twis_task_trigger(NRF_TWIS_Type * p_reg,nrf_twis_task_t task)554 NRF_STATIC_INLINE void nrf_twis_task_trigger(NRF_TWIS_Type * p_reg, nrf_twis_task_t task)
555 {
556     *(nrf_twis_getRegPtr(p_reg, (uint32_t)task)) = 1UL;
557 }
558 
nrf_twis_task_address_get(NRF_TWIS_Type const * p_reg,nrf_twis_task_t task)559 NRF_STATIC_INLINE uint32_t nrf_twis_task_address_get(NRF_TWIS_Type const * p_reg,
560                                                      nrf_twis_task_t       task)
561 {
562     return (uint32_t)nrf_twis_getRegPtr_c(p_reg, (uint32_t)task);
563 }
564 
nrf_twis_event_clear(NRF_TWIS_Type * p_reg,nrf_twis_event_t event)565 NRF_STATIC_INLINE void nrf_twis_event_clear(NRF_TWIS_Type * p_reg, nrf_twis_event_t event)
566 {
567     *(nrf_twis_getRegPtr(p_reg, (uint32_t)event)) = 0UL;
568     nrf_event_readback((uint8_t *)p_reg + (uint32_t)event);
569 }
570 
nrf_twis_event_check(NRF_TWIS_Type const * p_reg,nrf_twis_event_t event)571 NRF_STATIC_INLINE bool nrf_twis_event_check(NRF_TWIS_Type const * p_reg, nrf_twis_event_t event)
572 {
573     return (bool)*nrf_twis_getRegPtr_c(p_reg, (uint32_t)event);
574 }
575 
nrf_twis_event_get_and_clear(NRF_TWIS_Type * p_reg,nrf_twis_event_t event)576 NRF_STATIC_INLINE bool nrf_twis_event_get_and_clear(NRF_TWIS_Type * p_reg, nrf_twis_event_t event)
577 {
578     bool ret = nrf_twis_event_check(p_reg, event);
579     if (ret)
580     {
581         nrf_twis_event_clear(p_reg, event);
582     }
583     return ret;
584 }
585 
nrf_twis_event_address_get(NRF_TWIS_Type const * p_reg,nrf_twis_event_t event)586 NRF_STATIC_INLINE uint32_t nrf_twis_event_address_get(NRF_TWIS_Type const * p_reg,
587                                                       nrf_twis_event_t      event)
588 {
589     return (uint32_t)nrf_twis_getRegPtr_c(p_reg, (uint32_t)event);
590 }
591 
nrf_twis_shorts_enable(NRF_TWIS_Type * p_reg,uint32_t mask)592 NRF_STATIC_INLINE void nrf_twis_shorts_enable(NRF_TWIS_Type * p_reg, uint32_t mask)
593 {
594     p_reg->SHORTS |= mask;
595 }
596 
nrf_twis_shorts_disable(NRF_TWIS_Type * p_reg,uint32_t mask)597 NRF_STATIC_INLINE void nrf_twis_shorts_disable(NRF_TWIS_Type * p_reg, uint32_t mask)
598 {
599     if (~0U == mask)
600     {
601         /* Optimized version for "disable all" */
602         p_reg->SHORTS = 0;
603     }
604     else
605     {
606         p_reg->SHORTS &= ~mask;
607     }
608 }
609 
nrf_twis_shorts_get(NRF_TWIS_Type const * p_reg)610 NRF_STATIC_INLINE uint32_t nrf_twis_shorts_get(NRF_TWIS_Type const * p_reg)
611 {
612     return p_reg->SHORTS;
613 }
614 
nrf_twis_int_enable(NRF_TWIS_Type * p_reg,uint32_t mask)615 NRF_STATIC_INLINE void nrf_twis_int_enable(NRF_TWIS_Type * p_reg, uint32_t mask)
616 {
617     p_reg->INTENSET = mask;
618 }
619 
nrf_twis_int_enable_check(NRF_TWIS_Type const * p_reg,uint32_t mask)620 NRF_STATIC_INLINE uint32_t nrf_twis_int_enable_check(NRF_TWIS_Type const * p_reg, uint32_t mask)
621 {
622     return p_reg->INTENSET & mask;
623 }
624 
nrf_twis_int_disable(NRF_TWIS_Type * p_reg,uint32_t mask)625 NRF_STATIC_INLINE void nrf_twis_int_disable(NRF_TWIS_Type * p_reg, uint32_t mask)
626 {
627     p_reg->INTENCLR = mask;
628 }
629 
630 #if defined(DPPI_PRESENT)
nrf_twis_subscribe_set(NRF_TWIS_Type * p_reg,nrf_twis_task_t task,uint8_t channel)631 NRF_STATIC_INLINE void nrf_twis_subscribe_set(NRF_TWIS_Type * p_reg,
632                                               nrf_twis_task_t task,
633                                               uint8_t         channel)
634 {
635     *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) =
636             ((uint32_t)channel | TWIS_SUBSCRIBE_STOP_EN_Msk);
637 }
638 
nrf_twis_subscribe_clear(NRF_TWIS_Type * p_reg,nrf_twis_task_t task)639 NRF_STATIC_INLINE void nrf_twis_subscribe_clear(NRF_TWIS_Type * p_reg,
640                                                 nrf_twis_task_t task)
641 {
642     *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) = 0;
643 }
644 
nrf_twis_publish_set(NRF_TWIS_Type * p_reg,nrf_twis_event_t event,uint8_t channel)645 NRF_STATIC_INLINE void nrf_twis_publish_set(NRF_TWIS_Type *  p_reg,
646                                             nrf_twis_event_t event,
647                                             uint8_t          channel)
648 {
649     *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) =
650             ((uint32_t)channel | TWIS_PUBLISH_STOPPED_EN_Msk);
651 }
652 
nrf_twis_publish_clear(NRF_TWIS_Type * p_reg,nrf_twis_event_t event)653 NRF_STATIC_INLINE void nrf_twis_publish_clear(NRF_TWIS_Type *  p_reg,
654                                               nrf_twis_event_t event)
655 {
656     *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) = 0;
657 }
658 #endif // defined(DPPI_PRESENT)
659 
nrf_twis_error_source_get_and_clear(NRF_TWIS_Type * p_reg)660 NRF_STATIC_INLINE uint32_t nrf_twis_error_source_get_and_clear(NRF_TWIS_Type * p_reg)
661 {
662     uint32_t ret = p_reg->ERRORSRC;
663     p_reg->ERRORSRC = ret;
664     return ret;
665 }
666 
nrf_twis_match_get(NRF_TWIS_Type const * p_reg)667 NRF_STATIC_INLINE uint_fast8_t nrf_twis_match_get(NRF_TWIS_Type const * p_reg)
668 {
669     return (uint_fast8_t)p_reg->MATCH;
670 }
671 
nrf_twis_enable(NRF_TWIS_Type * p_reg)672 NRF_STATIC_INLINE void nrf_twis_enable(NRF_TWIS_Type * p_reg)
673 {
674     p_reg->ENABLE = (TWIS_ENABLE_ENABLE_Enabled << TWIS_ENABLE_ENABLE_Pos);
675 }
676 
nrf_twis_disable(NRF_TWIS_Type * p_reg)677 NRF_STATIC_INLINE void nrf_twis_disable(NRF_TWIS_Type * p_reg)
678 {
679     p_reg->ENABLE = (TWIS_ENABLE_ENABLE_Disabled << TWIS_ENABLE_ENABLE_Pos);
680 }
681 
nrf_twis_pins_set(NRF_TWIS_Type * p_reg,uint32_t scl,uint32_t sda)682 NRF_STATIC_INLINE void nrf_twis_pins_set(NRF_TWIS_Type * p_reg, uint32_t scl, uint32_t sda)
683 {
684     p_reg->PSEL.SCL = scl;
685     p_reg->PSEL.SDA = sda;
686 }
687 
nrf_twis_scl_pin_get(NRF_TWIS_Type const * p_reg)688 NRF_STATIC_INLINE uint32_t nrf_twis_scl_pin_get(NRF_TWIS_Type const * p_reg)
689 {
690     return p_reg->PSEL.SCL;
691 }
692 
nrf_twis_sda_pin_get(NRF_TWIS_Type const * p_reg)693 NRF_STATIC_INLINE uint32_t nrf_twis_sda_pin_get(NRF_TWIS_Type const * p_reg)
694 {
695     return p_reg->PSEL.SDA;
696 }
697 
nrf_twis_rx_buffer_set(NRF_TWIS_Type * p_reg,uint8_t * p_buf,size_t length)698 NRF_STATIC_INLINE void nrf_twis_rx_buffer_set(NRF_TWIS_Type * p_reg,
699                                               uint8_t *       p_buf,
700                                               size_t          length)
701 {
702     p_reg->RXD.PTR    = (uint32_t)p_buf;
703     p_reg->RXD.MAXCNT = length;
704 }
705 
nrf_twis_rx_prepare(NRF_TWIS_Type * p_reg,uint8_t * p_buf,size_t length)706 NRF_STATIC_INLINE void nrf_twis_rx_prepare(NRF_TWIS_Type *   p_reg,
707                                            uint8_t *         p_buf,
708                                            size_t length)
709 {
710     nrf_twis_rx_buffer_set(p_reg, p_buf, length);
711     nrf_twis_task_trigger(p_reg, NRF_TWIS_TASK_PREPARERX);
712 }
713 
nrf_twis_rx_amount_get(NRF_TWIS_Type const * p_reg)714 NRF_STATIC_INLINE size_t nrf_twis_rx_amount_get(NRF_TWIS_Type const * p_reg)
715 {
716     return p_reg->RXD.AMOUNT;
717 }
718 
nrf_twis_tx_buffer_set(NRF_TWIS_Type * p_reg,uint8_t const * p_buf,size_t length)719 NRF_STATIC_INLINE void nrf_twis_tx_buffer_set(NRF_TWIS_Type * p_reg,
720                                               uint8_t const * p_buf,
721                                               size_t          length)
722 {
723     p_reg->TXD.PTR    = (uint32_t)p_buf;
724     p_reg->TXD.MAXCNT = length;
725 }
726 
nrf_twis_tx_prepare(NRF_TWIS_Type * p_reg,uint8_t const * p_buf,size_t length)727 NRF_STATIC_INLINE void nrf_twis_tx_prepare(NRF_TWIS_Type * p_reg,
728                                            uint8_t const * p_buf,
729                                            size_t          length)
730 {
731     nrf_twis_tx_buffer_set(p_reg, p_buf, length);
732     nrf_twis_task_trigger(p_reg, NRF_TWIS_TASK_PREPARETX);
733 }
734 
nrf_twis_tx_amount_get(NRF_TWIS_Type const * p_reg)735 NRF_STATIC_INLINE size_t nrf_twis_tx_amount_get(NRF_TWIS_Type const * p_reg)
736 {
737     return p_reg->TXD.AMOUNT;
738 }
739 
nrf_twis_address_set(NRF_TWIS_Type * p_reg,uint_fast8_t n,nrf_twis_address_t addr)740 NRF_STATIC_INLINE void nrf_twis_address_set(NRF_TWIS_Type *    p_reg,
741                                             uint_fast8_t       n,
742                                             nrf_twis_address_t addr)
743 {
744     p_reg->ADDRESS[n] = addr;
745 }
746 
nrf_twis_address_get(NRF_TWIS_Type const * p_reg,uint_fast8_t n)747 NRF_STATIC_INLINE nrf_twis_address_t nrf_twis_address_get(NRF_TWIS_Type const * p_reg,
748                                                           uint_fast8_t          n)
749 {
750     return (nrf_twis_address_t)p_reg->ADDRESS[n];
751 }
nrf_twis_config_address_set(NRF_TWIS_Type * p_reg,nrf_twis_config_addr_mask_t addr_mask)752 NRF_STATIC_INLINE void nrf_twis_config_address_set(NRF_TWIS_Type *             p_reg,
753                                                    nrf_twis_config_addr_mask_t addr_mask)
754 {
755     /* This is the only configuration in TWIS - just write it without masking */
756     p_reg->CONFIG = addr_mask;
757 }
758 
nrf_twis_config_address_get(NRF_TWIS_Type const * p_reg)759 NRF_STATIC_INLINE nrf_twis_config_addr_mask_t nrf_twis_config_address_get(
760         NRF_TWIS_Type const * p_reg)
761 {
762     return (nrf_twis_config_addr_mask_t)(p_reg->CONFIG & TWIS_ADDRESS_ADDRESS_Msk);
763 }
764 
nrf_twis_orc_set(NRF_TWIS_Type * p_reg,uint8_t orc)765 NRF_STATIC_INLINE void nrf_twis_orc_set(NRF_TWIS_Type * p_reg, uint8_t orc)
766 {
767     p_reg->ORC = orc;
768 }
769 
nrf_twis_orc_get(NRF_TWIS_Type const * p_reg)770 NRF_STATIC_INLINE uint8_t nrf_twis_orc_get(NRF_TWIS_Type const * p_reg)
771 {
772     return (uint8_t)p_reg->ORC;
773 }
774 
775 #if defined(TWIS_TXD_LIST_LIST_Msk)
nrf_twis_tx_list_enable(NRF_TWIS_Type * p_reg)776 NRF_STATIC_INLINE void nrf_twis_tx_list_enable(NRF_TWIS_Type * p_reg)
777 {
778     p_reg->TXD.LIST = TWIS_TXD_LIST_LIST_ArrayList << TWIS_TXD_LIST_LIST_Pos;
779 }
780 
nrf_twis_tx_list_disable(NRF_TWIS_Type * p_reg)781 NRF_STATIC_INLINE void nrf_twis_tx_list_disable(NRF_TWIS_Type * p_reg)
782 {
783     p_reg->TXD.LIST = TWIS_TXD_LIST_LIST_Disabled << TWIS_TXD_LIST_LIST_Pos;
784 }
785 
786 #endif // defined(TWIS_TXD_LIST_LIST_Msk)
787 
788 #if defined(TWIS_RXD_LIST_LIST_Msk)
nrf_twis_rx_list_enable(NRF_TWIS_Type * p_reg)789 NRF_STATIC_INLINE void nrf_twis_rx_list_enable(NRF_TWIS_Type * p_reg)
790 {
791     p_reg->RXD.LIST = TWIS_RXD_LIST_LIST_ArrayList << TWIS_RXD_LIST_LIST_Pos;
792 }
793 
nrf_twis_rx_list_disable(NRF_TWIS_Type * p_reg)794 NRF_STATIC_INLINE void nrf_twis_rx_list_disable(NRF_TWIS_Type * p_reg)
795 {
796     p_reg->RXD.LIST = TWIS_RXD_LIST_LIST_Disabled << TWIS_RXD_LIST_LIST_Pos;
797 }
798 #endif // defined(TWIS_RXD_LIST_LIST_Msk)
799 
800 #endif /* NRF_DECLARE_ONLY */
801 
802 
803 #ifdef __cplusplus
804 }
805 #endif
806 
807 #endif /* NRF_TWIS_H__ */
808