Skip to content

Commit

Permalink
firmware: drivers: ds277Xg: Created alternative I2C write function fo…
Browse files Browse the repository at this point in the history
…r testing #157
  • Loading branch information
ramonborba committed May 26, 2022
1 parent 35bfc33 commit ef06fd2
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
117 changes: 117 additions & 0 deletions firmware/drivers/i2c/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,121 @@ int i2c_read(i2c_port_t port, i2c_slave_adr_t adr, uint8_t *data, uint16_t len)
return err;
}


int i2c_write_test(i2c_port_t port, i2c_slave_adr_t adr, uint8_t *data, uint16_t len)
{
int err = 0;

/* Verifies if the slave address is lesser than 7-bits */
if (adr <= 128U)
{
uint16_t base_address = UINT16_MAX;

switch(port)
{
case I2C_PORT_0: base_address = USCI_B0_BASE; break;
case I2C_PORT_1: base_address = USCI_B1_BASE; break;
case I2C_PORT_2: base_address = USCI_B2_BASE; break;
default:
#if defined(CONFIG_DRIVERS_DEBUG_ENABLED) && (CONFIG_DRIVERS_DEBUG_ENABLED == 1)
sys_log_print_event_from_module(SYS_LOG_ERROR, I2C_MODULE_NAME, "Invalid port during write!");
sys_log_new_line();
#endif /* CONFIG_DRIVERS_DEBUG_ENABLED */
err = -1; /* Invalid I2C port */
break;
}

if (err == 0)
{
USCI_B_I2C_setSlaveAddress(base_address, adr);

USCI_B_I2C_setMode(base_address, USCI_B_I2C_TRANSMIT_MODE);

USCI_B_I2C_enable(base_address);

if (len == 1U) /* Single byte */
{
if (USCI_B_I2C_masterSendMultiByteStartWithTimeout(base_address, data[0], I2C_SLAVE_TIMEOUT) == STATUS_SUCCESS)
{
/* Delay until transmission completes */
while(USCI_B_I2C_isBusBusy(base_address) > 0)
{
;
}
}
else
{
#if defined(CONFIG_DRIVERS_DEBUG_ENABLED) && (CONFIG_DRIVERS_DEBUG_ENABLED == 1)
sys_log_print_event_from_module(SYS_LOG_WARNING, I2C_MODULE_NAME, "Timeout reached during write!");
sys_log_new_line();
#endif /* CONFIG_DRIVERS_DEBUG_ENABLED */
err = -1; /* Timeout reached! */
}
}
else /* Multiple bytes */
{
/* Initiate start and send first character */
if (USCI_B_I2C_masterSendMultiByteStartWithTimeout(base_address, data[0], I2C_SLAVE_TIMEOUT) == STATUS_SUCCESS)
{
uint16_t i = 0;
for(i = 1; i < len; i++)
{
if (USCI_B_I2C_masterSendMultiByteNextWithTimeout(base_address, data[i], I2C_SLAVE_TIMEOUT) != STATUS_SUCCESS)
{
#if defined(CONFIG_DRIVERS_DEBUG_ENABLED) && (CONFIG_DRIVERS_DEBUG_ENABLED == 1)
sys_log_print_event_from_module(SYS_LOG_WARNING, I2C_MODULE_NAME, "Timeout reached during write!");
sys_log_new_line();
#endif /* CONFIG_DRIVERS_DEBUG_ENABLED */
err = -1; /* Timeout reached! */
break;
}
}

if (err == 0)
{
/* Initiate stop only */
if (USCI_B_I2C_masterSendMultiByteStopWithTimeout(base_address, I2C_SLAVE_TIMEOUT) == STATUS_SUCCESS)
{
/* Delay until transmission completes */
while(USCI_B_I2C_isBusBusy(base_address) > 0)
{
;
}
}
else
{
#if defined(CONFIG_DRIVERS_DEBUG_ENABLED) && (CONFIG_DRIVERS_DEBUG_ENABLED == 1)
sys_log_print_event_from_module(SYS_LOG_WARNING, I2C_MODULE_NAME, "Timeout reached during write!");
sys_log_new_line();
#endif /* CONFIG_DRIVERS_DEBUG_ENABLED */
err = -1; /* Timeout reached! */
}
}
}
else
{
#if defined(CONFIG_DRIVERS_DEBUG_ENABLED) && (CONFIG_DRIVERS_DEBUG_ENABLED == 1)
sys_log_print_event_from_module(SYS_LOG_WARNING, I2C_MODULE_NAME, "Timeout reached during write!");
sys_log_new_line();
#endif /* CONFIG_DRIVERS_DEBUG_ENABLED */
err = -1; /* Timeout reached! */
}
}

USCI_B_I2C_disable(base_address);
}
}
else
{
#if defined(CONFIG_DRIVERS_DEBUG_ENABLED) && (CONFIG_DRIVERS_DEBUG_ENABLED == 1)
sys_log_print_event_from_module(SYS_LOG_ERROR, I2C_MODULE_NAME, "Invalid slave address during write (");
sys_log_print_hex(adr);
sys_log_print_msg(")!");
sys_log_new_line();
#endif /* CONFIG_DRIVERS_DEBUG_ENABLED */
err = -1; /* Invalid slave address */
}

return err;
}
/** \} End of i2c group */
2 changes: 2 additions & 0 deletions firmware/drivers/i2c/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ int i2c_write(i2c_port_t port, i2c_slave_adr_t adr, uint8_t *data, uint16_t len)
*/
int i2c_read(i2c_port_t port, i2c_slave_adr_t adr, uint8_t *data, uint16_t len);

int i2c_write_test(i2c_port_t port, i2c_slave_adr_t adr, uint8_t *data, uint16_t len);

#endif /* I2C_H_ */

/** \} End of i2c group */

0 comments on commit ef06fd2

Please sign in to comment.