Skip to content

nrfx: twim: drop internal state (PoC) #283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 77 additions & 81 deletions nrfx/drivers/include/nrfx_twim.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,80 @@ extern "C" {
* @brief Two Wire Interface Master with EasyDMA (TWIM) peripheral driver.
*/

/** @brief TWIM driver transfer types. */
typedef enum
{
NRFX_TWIM_XFER_TX, ///< TX transfer.
NRFX_TWIM_XFER_RX, ///< RX transfer.
NRFX_TWIM_XFER_TXRX, ///< TX transfer followed by RX transfer with repeated start.
NRFX_TWIM_XFER_TXTX ///< TX transfer followed by TX transfer with repeated start.
} nrfx_twim_xfer_type_t;

/** @brief TWIM driver event types. */
typedef enum
{
NRFX_TWIM_EVT_DONE, ///< Transfer completed event.
NRFX_TWIM_EVT_ADDRESS_NACK, ///< Error event: NACK received after sending the address.
NRFX_TWIM_EVT_DATA_NACK, ///< Error event: NACK received after sending a data byte.
NRFX_TWIM_EVT_OVERRUN, ///< Error event: The unread data is replaced by new data.
NRFX_TWIM_EVT_BUS_ERROR ///< Error event: An unexpected transition occurred on the bus.
} nrfx_twim_evt_type_t;

/** @brief Structure for a TWIM transfer descriptor. */
typedef struct
{
nrfx_twim_xfer_type_t type; ///< Type of transfer.
uint8_t address; ///< Slave address.
size_t primary_length; ///< Number of bytes transferred.
size_t secondary_length; ///< Number of bytes transferred.
uint8_t * p_primary_buf; ///< Pointer to transferred data.
uint8_t * p_secondary_buf; ///< Pointer to transferred data.
} nrfx_twim_xfer_desc_t;

/** @brief Structure for a TWIM event. */
typedef struct
{
nrfx_twim_evt_type_t type; ///< Event type.
nrfx_twim_xfer_desc_t xfer_desc; ///< Transfer details.
} nrfx_twim_evt_t;

/** @brief TWIM event handler prototype. */
typedef void (* nrfx_twim_evt_handler_t)(nrfx_twim_evt_t const * p_event,
void * p_context);

// Control block - driver instance local data.
typedef struct
{
nrfx_twim_evt_handler_t handler;
void * p_context;
volatile uint32_t int_mask;
nrfy_twim_xfer_desc_t xfer_desc_primary;
nrfy_twim_xfer_desc_t xfer_desc_secondary;
uint32_t flags;
nrfx_twim_xfer_type_t xfer_type;
uint8_t address;
nrfx_drv_state_t state;
bool error;
volatile bool busy;
bool repeated;
bool hold_bus_uninit;
bool skip_gpio_cfg;
#if NRFX_CHECK(NRFX_TWIM_NRF52_ANOMALY_109_WORKAROUND_ENABLED)
nrf_twim_frequency_t bus_frequency;
#endif
} twim_control_block_t;

/** @brief Structure for the TWIM driver instance. */
typedef struct
{
NRF_TWIM_Type * p_twim; ///< Pointer to a structure with TWIM registers.
uint8_t drv_inst_idx; ///< Index of the driver instance. For internal use only.
twim_control_block_t m_cb;
} nrfx_twim_t;

/** @brief Macro for creating a TWIM driver instance. */
#define NRFX_TWIM_INSTANCE(id) \
#define NRFX_TWIM_INSTANCE(reg) \
{ \
.p_twim = NRFX_CONCAT(NRF_, TWIM, id), \
.drv_inst_idx = NRFX_CONCAT(NRFX_TWIM, id, _INST_IDX), \
.p_twim = (NRF_TWIM_Type *)reg, \
}

#ifndef __NRFX_DOXYGEN__
Expand Down Expand Up @@ -129,36 +191,6 @@ typedef struct
/** @brief Flag indicating that checks for spurious STOP condition will not be performed. */
#define NRFX_TWIM_FLAG_NO_SPURIOUS_STOP_CHECK (1UL << 6)

/** @brief TWIM driver event types. */
typedef enum
{
NRFX_TWIM_EVT_DONE, ///< Transfer completed event.
NRFX_TWIM_EVT_ADDRESS_NACK, ///< Error event: NACK received after sending the address.
NRFX_TWIM_EVT_DATA_NACK, ///< Error event: NACK received after sending a data byte.
NRFX_TWIM_EVT_OVERRUN, ///< Error event: The unread data is replaced by new data.
NRFX_TWIM_EVT_BUS_ERROR ///< Error event: An unexpected transition occurred on the bus.
} nrfx_twim_evt_type_t;

/** @brief TWIM driver transfer types. */
typedef enum
{
NRFX_TWIM_XFER_TX, ///< TX transfer.
NRFX_TWIM_XFER_RX, ///< RX transfer.
NRFX_TWIM_XFER_TXRX, ///< TX transfer followed by RX transfer with repeated start.
NRFX_TWIM_XFER_TXTX ///< TX transfer followed by TX transfer with repeated start.
} nrfx_twim_xfer_type_t;

/** @brief Structure for a TWIM transfer descriptor. */
typedef struct
{
nrfx_twim_xfer_type_t type; ///< Type of transfer.
uint8_t address; ///< Slave address.
size_t primary_length; ///< Number of bytes transferred.
size_t secondary_length; ///< Number of bytes transferred.
uint8_t * p_primary_buf; ///< Pointer to transferred data.
uint8_t * p_secondary_buf; ///< Pointer to transferred data.
} nrfx_twim_xfer_desc_t;

/** @brief Macro for setting the transfer descriptor. */
#define NRFX_TWIM_XFER_DESC(transfer, addr, p_buf1, buf_len1, p_buf2, buf_len2) \
{ \
Expand Down Expand Up @@ -186,17 +218,6 @@ typedef struct
#define NRFX_TWIM_XFER_DESC_TXTX(addr, p_tx, tx_len, p_tx2, tx_len2) \
NRFX_TWIM_XFER_DESC(NRFX_TWIM_XFER_TXTX, addr, p_tx, tx_len, p_tx2, tx_len2)

/** @brief Structure for a TWIM event. */
typedef struct
{
nrfx_twim_evt_type_t type; ///< Event type.
nrfx_twim_xfer_desc_t xfer_desc; ///< Transfer details.
} nrfx_twim_evt_t;

/** @brief TWIM event handler prototype. */
typedef void (* nrfx_twim_evt_handler_t)(nrfx_twim_evt_t const * p_event,
void * p_context);

/**
* @brief Function for initializing the TWIM driver instance.
*
Expand All @@ -218,7 +239,7 @@ typedef void (* nrfx_twim_evt_handler_t)(nrfx_twim_evt_t const * p_event,
* possible only if @ref nrfx_prs module
* is enabled.
*/
nrfx_err_t nrfx_twim_init(nrfx_twim_t const * p_instance,
nrfx_err_t nrfx_twim_init(nrfx_twim_t * p_instance,
nrfx_twim_config_t const * p_config,
nrfx_twim_evt_handler_t event_handler,
void * p_context);
Expand All @@ -234,15 +255,15 @@ nrfx_err_t nrfx_twim_init(nrfx_twim_t const * p_instance,
* @retval NRFX_ERROR_INVALID_STATE The driver is uninitialized.
* @retval NRFX_ERROR_INVALID_PARAM Requested frequency is not available on the specified pins.
*/
nrfx_err_t nrfx_twim_reconfigure(nrfx_twim_t const * p_instance,
nrfx_err_t nrfx_twim_reconfigure(nrfx_twim_t * p_instance,
nrfx_twim_config_t const * p_config);

/**
* @brief Function for uninitializing the TWIM instance.
*
* @param[in] p_instance Pointer to the driver instance structure.
*/
void nrfx_twim_uninit(nrfx_twim_t const * p_instance);
void nrfx_twim_uninit(nrfx_twim_t * p_instance);

/**
* @brief Function for checking if the TWIM driver instance is initialized.
Expand All @@ -252,21 +273,21 @@ void nrfx_twim_uninit(nrfx_twim_t const * p_instance);
* @retval true Instance is already initialized.
* @retval false Instance is not initialized.
*/
bool nrfx_twim_init_check(nrfx_twim_t const * p_instance);
bool nrfx_twim_init_check(nrfx_twim_t * p_instance);

/**
* @brief Function for enabling the TWIM instance.
*
* @param[in] p_instance Pointer to the driver instance structure.
*/
void nrfx_twim_enable(nrfx_twim_t const * p_instance);
void nrfx_twim_enable(nrfx_twim_t * p_instance);

/**
* @brief Function for disabling the TWIM instance.
*
* @param[in] p_instance Pointer to the driver instance structure.
*/
void nrfx_twim_disable(nrfx_twim_t const * p_instance);
void nrfx_twim_disable(nrfx_twim_t * p_instance);

/**
* @brief Function for performing a TWIM transfer.
Expand Down Expand Up @@ -323,7 +344,7 @@ void nrfx_twim_disable(nrfx_twim_t const * p_instance);
* @retval NRFX_ERROR_DRV_TWI_ERR_ANACK NACK is received after sending the address.
* @retval NRFX_ERROR_DRV_TWI_ERR_DNACK NACK is received after sending a data byte.
*/
nrfx_err_t nrfx_twim_xfer(nrfx_twim_t const * p_instance,
nrfx_err_t nrfx_twim_xfer(nrfx_twim_t * p_instance,
nrfx_twim_xfer_desc_t const * p_xfer_desc,
uint32_t flags);

Expand All @@ -335,7 +356,7 @@ nrfx_err_t nrfx_twim_xfer(nrfx_twim_t const * p_instance,
* @retval true The TWIM driver is currently busy performing a transfer.
* @retval false The TWIM driver is ready for a new transfer.
*/
bool nrfx_twim_is_busy(nrfx_twim_t const * p_instance);
bool nrfx_twim_is_busy(nrfx_twim_t * p_instance);


/**
Expand All @@ -349,7 +370,7 @@ bool nrfx_twim_is_busy(nrfx_twim_t const * p_instance);
*
* @return Start task address (TX or RX) depending on the value of xfer_type.
*/
uint32_t nrfx_twim_start_task_address_get(nrfx_twim_t const * p_instance,
uint32_t nrfx_twim_start_task_address_get(nrfx_twim_t * p_instance,
nrfx_twim_xfer_type_t xfer_type);

/**
Expand All @@ -362,7 +383,7 @@ uint32_t nrfx_twim_start_task_address_get(nrfx_twim_t const * p_instance,
*
* @return STOPPED event address.
*/
uint32_t nrfx_twim_stopped_event_address_get(nrfx_twim_t const * p_instance);
uint32_t nrfx_twim_stopped_event_address_get(nrfx_twim_t * p_instance);

/**
* @brief Function for recovering the bus.
Expand All @@ -388,32 +409,7 @@ NRFX_STATIC_INLINE nrfx_err_t nrfx_twim_bus_recover(uint32_t scl_pin, uint32_t s
}
#endif

/**
* @brief Macro returning TWIM interrupt handler.
*
* param[in] idx TWIM index.
*
* @return Interrupt handler.
*/
#define NRFX_TWIM_INST_HANDLER_GET(idx) NRFX_CONCAT_3(nrfx_twim_, idx, _irq_handler)

/** @} */

/*
* Declare interrupt handlers for all enabled driver instances in the following format:
* nrfx_\<periph_name\>_\<idx\>_irq_handler (for example, nrfx_twim_0_irq_handler).
*
* A specific interrupt handler for the driver instance can be retrieved by using
* the NRFX_TWIM_INST_HANDLER_GET macro.
*
* Here is a sample of using the NRFX_TWIM_INST_HANDLER_GET macro to map an interrupt handler
* in a Zephyr application:
*
* IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_TWIM_INST_GET(\<instance_index\>)), \<priority\>,
* NRFX_TWIM_INST_HANDLER_GET(\<instance_index\>), 0, 0);
*/
NRFX_INSTANCE_IRQ_HANDLERS_DECLARE(TWIM, twim)

void nrfx_twim_irq_handler(nrfx_twim_t * p_instance);

#ifdef __cplusplus
}
Expand Down
Loading