1 /*
2  * Copyright (c) 2015 - 2018, 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 NRFX_TWIM_H__
33 #define NRFX_TWIM_H__
34 
35 #include <nrfx.h>
36 #include <hal/nrf_twim.h>
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 /**
43  * @defgroup nrfx_twim TWIM driver
44  * @{
45  * @ingroup nrf_twim
46  * @brief   TWIM peripheral driver.
47  */
48 
49 /**
50  * @brief Structure for the TWI master driver instance.
51  */
52 typedef struct
53 {
54     NRF_TWIM_Type * p_twim;       ///< Pointer to a structure with TWIM registers.
55     uint8_t         drv_inst_idx; ///< Driver instance index.
56 } nrfx_twim_t;
57 
58 /**
59  * @brief Macro for creating a TWI master driver instance.
60  */
61 #define NRFX_TWIM_INSTANCE(id)                               \
62 {                                                            \
63     .p_twim       = NRFX_CONCAT_2(NRF_TWIM, id),             \
64     .drv_inst_idx = NRFX_CONCAT_3(NRFX_TWIM, id, _INST_IDX), \
65 }
66 
67 enum {
68 #if NRFX_CHECK(NRFX_TWIM0_ENABLED)
69     NRFX_TWIM0_INST_IDX,
70 #endif
71 #if NRFX_CHECK(NRFX_TWIM1_ENABLED)
72     NRFX_TWIM1_INST_IDX,
73 #endif
74     NRFX_TWIM_ENABLED_COUNT
75 };
76 
77 /**
78  * @brief Structure for the TWI master driver instance configuration.
79  */
80 typedef struct
81 {
82     uint32_t             scl;                 ///< SCL pin number.
83     uint32_t             sda;                 ///< SDA pin number.
84     nrf_twim_frequency_t frequency;           ///< TWIM frequency.
85     uint8_t              interrupt_priority;  ///< Interrupt priority.
86     bool                 hold_bus_uninit;     ///< Hold pull up state on gpio pins after uninit.
87 } nrfx_twim_config_t;
88 
89 /**
90  * @brief TWI master driver instance default configuration.
91  */
92 #define NRFX_TWIM_DEFAULT_CONFIG                                                    \
93 {                                                                                   \
94     .frequency          = (nrf_twim_frequency_t)NRFX_TWIM_DEFAULT_CONFIG_FREQUENCY, \
95     .scl                = 31,                                                       \
96     .sda                = 31,                                                       \
97     .interrupt_priority = NRFX_TWIM_DEFAULT_CONFIG_IRQ_PRIORITY,                    \
98     .hold_bus_uninit    = NRFX_TWIM_DEFAULT_CONFIG_HOLD_BUS_UNINIT,                 \
99 }
100 
101 #define NRFX_TWIM_FLAG_TX_POSTINC          (1UL << 0) /**< TX buffer address incremented after transfer. */
102 #define NRFX_TWIM_FLAG_RX_POSTINC          (1UL << 1) /**< RX buffer address incremented after transfer. */
103 #define NRFX_TWIM_FLAG_NO_XFER_EVT_HANDLER (1UL << 2) /**< Interrupt after each transfer is suppressed, and the event handler is not called. */
104 #define NRFX_TWIM_FLAG_HOLD_XFER           (1UL << 3) /**< Set up the transfer but do not start it. */
105 #define NRFX_TWIM_FLAG_REPEATED_XFER       (1UL << 4) /**< Flag indicating that the transfer will be executed multiple times. */
106 #define NRFX_TWIM_FLAG_TX_NO_STOP          (1UL << 5) /**< Flag indicating that the TX transfer will not end with a stop condition. */
107 
108 /**
109  * @brief TWI master driver event types.
110  */
111 typedef enum
112 {
113     NRFX_TWIM_EVT_DONE,         ///< Transfer completed event.
114     NRFX_TWIM_EVT_ADDRESS_NACK, ///< Error event: NACK received after sending the address.
115     NRFX_TWIM_EVT_DATA_NACK     ///< Error event: NACK received after sending a data byte.
116 } nrfx_twim_evt_type_t;
117 
118 /**
119  * @brief TWI master driver transfer types.
120  */
121 typedef enum
122 {
123     NRFX_TWIM_XFER_TX,          ///< TX transfer.
124     NRFX_TWIM_XFER_RX,          ///< RX transfer.
125     NRFX_TWIM_XFER_TXRX,        ///< TX transfer followed by RX transfer with repeated start.
126     NRFX_TWIM_XFER_TXTX         ///< TX transfer followed by TX transfer with repeated start.
127 } nrfx_twim_xfer_type_t;
128 
129 /**
130  * @brief Structure for a TWI transfer descriptor.
131  */
132 typedef struct
133 {
134     nrfx_twim_xfer_type_t   type;             ///< Type of transfer.
135     uint8_t                 address;          ///< Slave address.
136     size_t                  primary_length;   ///< Number of bytes transferred.
137     size_t                  secondary_length; ///< Number of bytes transferred.
138     uint8_t *               p_primary_buf;    ///< Pointer to transferred data.
139     uint8_t *               p_secondary_buf;  ///< Pointer to transferred data.
140 } nrfx_twim_xfer_desc_t;
141 
142 
143 /**@brief Macro for setting the TX transfer descriptor. */
144 #define NRFX_TWIM_XFER_DESC_TX(addr, p_data, length) \
145     {                                                \
146         .type = NRFX_TWIM_XFER_TX,                   \
147         .address = addr,                             \
148         .primary_length = length,                    \
149         .p_primary_buf  = p_data,                    \
150     }
151 
152 /**@brief Macro for setting the RX transfer descriptor. */
153 #define NRFX_TWIM_XFER_DESC_RX(addr, p_data, length) \
154     {                                                \
155         .type           = NRFX_TWIM_XFER_RX,         \
156         .address        = addr,                      \
157         .primary_length = length,                    \
158         .p_primary_buf  = p_data,                    \
159     }
160 
161 /**@brief Macro for setting the TXRX transfer descriptor. */
162 #define NRFX_TWIM_XFER_DESC_TXRX(addr, p_tx, tx_len, p_rx, rx_len) \
163     {                                                              \
164         .type             = NRFX_TWIM_XFER_TXRX,                   \
165         .address          = addr,                                  \
166         .primary_length   = tx_len,                                \
167         .secondary_length = rx_len,                                \
168         .p_primary_buf    = p_tx,                                  \
169         .p_secondary_buf  = p_rx,                                  \
170     }
171 
172 /**@brief Macro for setting the TXTX transfer descriptor. */
173 #define NRFX_TWIM_XFER_DESC_TXTX(addr, p_tx, tx_len, p_tx2, tx_len2) \
174     {                                                                \
175         .type             = NRFX_TWIM_XFER_TXTX,                     \
176         .address          = addr,                                    \
177         .primary_length   = tx_len,                                  \
178         .secondary_length = tx_len2,                                 \
179         .p_primary_buf    = p_tx,                                    \
180         .p_secondary_buf  = p_tx2,                                   \
181     }
182 
183 /**
184  * @brief Structure for a TWI event.
185  */
186 typedef struct
187 {
188     nrfx_twim_evt_type_t  type;      ///< Event type.
189     nrfx_twim_xfer_desc_t xfer_desc; ///< Transfer details.
190 } nrfx_twim_evt_t;
191 
192 /**
193  * @brief TWI event handler prototype.
194  */
195 typedef void (* nrfx_twim_evt_handler_t)(nrfx_twim_evt_t const * p_event,
196                                          void *                  p_context);
197 
198 /**
199  * @brief Function for initializing the TWI driver instance.
200  *
201  * @param[in] p_instance      Pointer to the driver instance structure.
202  * @param[in] p_config        Pointer to the structure with initial configuration.
203  * @param[in] event_handler   Event handler provided by the user. If NULL, blocking mode is enabled.
204  * @param[in] p_context       Context passed to event handler.
205  *
206  * @retval NRFX_SUCCESS             If initialization was successful.
207  * @retval NRFX_ERROR_INVALID_STATE If the driver is in invalid state.
208  * @retval NRFX_ERROR_BUSY          If some other peripheral with the same
209  *                                  instance ID is already in use. This is
210  *                                  possible only if @ref nrfx_prs module
211  *                                  is enabled.
212  */
213 nrfx_err_t nrfx_twim_init(nrfx_twim_t const *        p_instance,
214                           nrfx_twim_config_t const * p_config,
215                           nrfx_twim_evt_handler_t    event_handler,
216                           void *                     p_context);
217 
218 /**
219  * @brief Function for uninitializing the TWI instance.
220  *
221  * @param[in] p_instance Pointer to the driver instance structure.
222  */
223 void nrfx_twim_uninit(nrfx_twim_t const * p_instance);
224 
225 /**
226  * @brief Function for enabling the TWI instance.
227  *
228  * @param[in] p_instance Pointer to the driver instance structure.
229  */
230 void nrfx_twim_enable(nrfx_twim_t const * p_instance);
231 
232 /**
233  * @brief Function for disabling the TWI instance.
234  *
235  * @param[in] p_instance Pointer to the driver instance structure.
236  */
237 void nrfx_twim_disable(nrfx_twim_t const * p_instance);
238 
239 /**
240  * @brief Function for sending data to a TWI slave.
241  *
242  * The transmission will be stopped when an error occurs. If a transfer is ongoing,
243  * the function returns the error code @ref NRFX_ERROR_BUSY.
244  *
245  * @note Peripherals using EasyDMA (including TWIM) require the transfer buffers
246  *       to be placed in the Data RAM region. If this condition is not met,
247  *       this function will fail with the error code NRFX_ERROR_INVALID_ADDR.
248  *
249  * @param[in] p_instance Pointer to the driver instance structure.
250  * @param[in] address    Address of a specific slave device (only 7 LSB).
251  * @param[in] p_data     Pointer to a transmit buffer.
252  * @param[in] length     Number of bytes to send. Maximum possible length is
253  *                       dependent on the used SoC (see the MAXCNT register
254  *                       description in the Product Specification). The driver
255  *                       checks it with assertion.
256  * @param[in] no_stop    If set, the stop condition is not generated on the bus
257  *                       after the transfer has completed successfully (allowing
258  *                       for a repeated start in the next transfer).
259  *
260  * @retval NRFX_SUCCESS                  If the procedure was successful.
261  * @retval NRFX_ERROR_BUSY               If the driver is not ready for a new transfer.
262  * @retval NRFX_ERROR_INTERNAL           If an error was detected by hardware.
263  * @retval NRFX_ERROR_INVALID_ADDR       If the provided buffer is not placed in the Data RAM region.
264  * @retval NRFX_ERROR_DRV_TWI_ERR_ANACK  If NACK received after sending the address in polling mode.
265  * @retval NRFX_ERROR_DRV_TWI_ERR_DNACK  If NACK received after sending a data byte in polling mode.
266  */
267 nrfx_err_t nrfx_twim_tx(nrfx_twim_t const * p_instance,
268                         uint8_t             address,
269                         uint8_t const *     p_data,
270                         size_t              length,
271                         bool                no_stop);
272 
273 /**
274  * @brief Function for reading data from a TWI slave.
275  *
276  * The transmission will be stopped when an error occurs. If a transfer is ongoing,
277  * the function returns the error code @ref NRFX_ERROR_BUSY.
278  *
279  * @param[in] p_instance Pointer to the driver instance structure.
280  * @param[in] address    Address of a specific slave device (only 7 LSB).
281  * @param[in] p_data     Pointer to a receive buffer.
282  * @param[in] length     Number of bytes to be received. Maximum possible length
283  *                       is dependent on the used SoC (see the MAXCNT register
284  *                       description in the Product Specification). The driver
285  *                       checks it with assertion.
286  *
287  * @retval NRFX_SUCCESS                    If the procedure was successful.
288  * @retval NRFX_ERROR_BUSY                 If the driver is not ready for a new transfer.
289  * @retval NRFX_ERROR_INTERNAL             If an error was detected by hardware.
290  * @retval NRFX_ERROR_DRV_TWI_ERR_ANACK    If NACK received after sending the address in polling mode.
291  * @retval NRFX_ERROR_DRV_TWI_ERR_DNACK    If NACK received after sending a data byte in polling mode.
292  */
293 nrfx_err_t nrfx_twim_rx(nrfx_twim_t const * p_instance,
294                         uint8_t             address,
295                         uint8_t *           p_data,
296                         size_t              length);
297 
298 /**
299  * @brief Function for preparing a TWI transfer.
300  *
301  * The following transfer types can be configured (@ref nrfx_twim_xfer_desc_t::type):
302  * - @ref NRFX_TWIM_XFER_TXRX<span></span>: Write operation followed by a read operation (without STOP condition in between).
303  * - @ref NRFX_TWIM_XFER_TXTX<span></span>: Write operation followed by a write operation (without STOP condition in between).
304  * - @ref NRFX_TWIM_XFER_TX<span></span>:   Write operation (with or without STOP condition).
305  * - @ref NRFX_TWIM_XFER_RX<span></span>:   Read operation  (with STOP condition).
306  *
307  * @note TXRX and TXTX transfers are supported only in non-blocking mode.
308  *
309  * Additional options are provided using the flags parameter:
310  * - @ref NRFX_TWIM_FLAG_TX_POSTINC and @ref NRFX_TWIM_FLAG_RX_POSTINC<span></span>: Post-incrementation of buffer addresses. Supported only by TWIM.
311  * - @ref NRFX_TWIM_FLAG_NO_XFER_EVT_HANDLER<span></span>: No user event handler after transfer completion. In most cases, this also means no interrupt at the end of the transfer.
312  * - @ref NRFX_TWIM_FLAG_HOLD_XFER<span></span>: Driver is not starting the transfer. Use this flag if the transfer is triggered externally by PPI. Supported only by TWIM.
313  *   Use @ref nrfx_twim_start_task_get to get the address of the start task.
314  * - @ref NRFX_TWIM_FLAG_REPEATED_XFER<span></span>: Prepare for repeated transfers. You can set up a number of transfers that will be triggered externally (for example by PPI).
315  *   An example is a TXRX transfer with the options @ref NRFX_TWIM_FLAG_RX_POSTINC, @ref NRFX_TWIM_FLAG_NO_XFER_EVT_HANDLER, and @ref NRFX_TWIM_FLAG_REPEATED_XFER.
316  *   After the transfer is set up, a set of transfers can be triggered by PPI that will read, for example, the same register of an
317  *   external component and put it into a RAM buffer without any interrupts. @ref nrfx_twim_stopped_event_get can be used to get the
318  *   address of the STOPPED event, which can be used to count the number of transfers. If @ref NRFX_TWIM_FLAG_REPEATED_XFER is used,
319  *   the driver does not set the driver instance into busy state, so you must ensure that the next transfers are set up
320  *   when TWIM is not active. Supported only by TWIM.
321  * - @ref NRFX_TWIM_FLAG_TX_NO_STOP<span></span>: No stop condition after TX transfer.
322  *
323  * @note
324  * Some flag combinations are invalid:
325  * - @ref NRFX_TWIM_FLAG_TX_NO_STOP with @ref nrfx_twim_xfer_desc_t::type different than @ref NRFX_TWIM_XFER_TX
326  * - @ref NRFX_TWIM_FLAG_REPEATED_XFER with @ref nrfx_twim_xfer_desc_t::type set to @ref NRFX_TWIM_XFER_TXTX
327  *
328  * If @ref nrfx_twim_xfer_desc_t::type is set to @ref NRFX_TWIM_XFER_TX and the @ref NRFX_TWIM_FLAG_TX_NO_STOP and @ref NRFX_TWIM_FLAG_REPEATED_XFER
329  * flags are set, two tasks must be used to trigger a transfer: TASKS_RESUME followed by TASKS_STARTTX. If no stop condition is generated,
330  * TWIM is in SUSPENDED state. Therefore, it must be resumed before the transfer can be started.
331  *
332  * @note Peripherals using EasyDMA (including TWIM) require the transfer buffers
333  *       to be placed in the Data RAM region. If this condition is not met,
334  *       this function will fail with the error code NRFX_ERROR_INVALID_ADDR.
335  *
336  * @param[in] p_instance        Pointer to the driver instance structure.
337  * @param[in] p_xfer_desc       Pointer to the transfer descriptor.
338  * @param[in] flags             Transfer options (0 for default settings).
339  *
340  * @retval NRFX_SUCCESS                    If the procedure was successful.
341  * @retval NRFX_ERROR_BUSY                 If the driver is not ready for a new transfer.
342  * @retval NRFX_ERROR_NOT_SUPPORTED        If the provided parameters are not supported.
343  * @retval NRFX_ERROR_INTERNAL             If an error was detected by hardware.
344  * @retval NRFX_ERROR_INVALID_ADDR         If the provided buffers are not placed in the Data RAM region.
345  * @retval NRFX_ERROR_DRV_TWI_ERR_ANACK    If NACK received after sending the address.
346  * @retval NRFX_ERROR_DRV_TWI_ERR_DNACK    If NACK received after sending a data byte.
347  */
348 nrfx_err_t nrfx_twim_xfer(nrfx_twim_t           const * p_instance,
349                           nrfx_twim_xfer_desc_t const * p_xfer_desc,
350                           uint32_t                      flags);
351 
352 /**
353  * @brief Function for checking the TWI driver state.
354  *
355  * @param[in] p_instance TWI instance.
356  *
357  * @retval true  If the TWI driver is currently busy performing a transfer.
358  * @retval false If the TWI driver is ready for a new transfer.
359  */
360 bool nrfx_twim_is_busy(nrfx_twim_t const * p_instance);
361 
362 
363 /**
364  * @brief Function for returning the address of a TWIM start task.
365  *
366  * This function should be used if @ref nrfx_twim_xfer was called with the flag @ref NRFX_TWIM_FLAG_HOLD_XFER.
367  * In that case, the transfer is not started by the driver, but it must be started externally by PPI.
368  *
369  * @param[in]  p_instance Pointer to the driver instance structure.
370  * @param[in]  xfer_type  Transfer type used in the last call of the @ref nrfx_twim_xfer function.
371  *
372  * @return     Start task address (TX or RX) depending on the value of xfer_type.
373  */
374 uint32_t nrfx_twim_start_task_get(nrfx_twim_t const * p_instance, nrfx_twim_xfer_type_t xfer_type);
375 
376 /**
377  * @brief Function for returning the address of a STOPPED TWIM event.
378  *
379  * A STOPPED event can be used to detect the end of a transfer if the @ref NRFX_TWIM_FLAG_NO_XFER_EVT_HANDLER
380  * option is used.
381  *
382  * @param[in]  p_instance Pointer to the driver instance structure.
383  *
384  * @return     STOPPED event address.
385  */
386 uint32_t nrfx_twim_stopped_event_get(nrfx_twim_t const * p_instance);
387 
388 
389 void nrfx_twim_0_irq_handler(void);
390 void nrfx_twim_1_irq_handler(void);
391 
392 
393 /** @} */
394 
395 #ifdef __cplusplus
396 }
397 #endif
398 
399 #endif // NRFX_TWIM_H__
400