Hi,
I recently finished some code which uses I2C driver. The I2C driver implementation in MQX is different from what I used before.
I added a thin layer. So far, I didn't find any problems yet. ( I use I2C to talk to an one-wire device controller )
Below is the code:
////////////////////////////////////////////////////////////
uint_32 I2C_Init(void)
{
// Open the I2C driver
i2c_fd = fopen ("i2c0:", NULL);
if (i2c_fd == NULL)
{ // Error Handling }
// Set up ....................
}
////////////////////////////////////////////////////////////
uint_32 I2C_stop(void)
{
errorMQX = ioctl (i2c_fd, IO_IOCTL_I2C_STOP, NULL);
if (I2C_OK != errorMQX)
{ // Error Handling }
return errorCode;
}
uint_32 I2C_start()
{
return I2C_OK;
}
////////////////////////////////////////////////////////////
uint_32 I2C_send_address(unsigned char addr)
{
uint_32 param;
uint_8 mem;
param = addr;
if (I2C_OK != (errorMQX = ioctl(i2c_fd, IO_IOCTL_I2C_SET_DESTINATION_ADDRESS, ¶m)) )
{ // Error Handling }
param = 0x00;
if (I2C_OK != (errorMQX = ioctl (i2c_fd, IO_IOCTL_I2C_GET_DESTINATION_ADDRESS, ¶m)) )
{ // Error Handling }
/* Initiate start and send I2C bus address */
fwrite (&mem, 1, 0, i2c_fd);
/* Check ack (device exists) */
if (I2C_OK == (errorMQX = ioctl(i2c_fd, IO_IOCTL_FLUSH_OUTPUT, ¶m)) )
{
if (param)
{
/* Stop I2C transfer */
if (I2C_OK != (errorMQX = ioctl(i2c_fd, IO_IOCTL_I2C_STOP, NULL)) )
{ // Error Handling }
// Error Handling
}
}
else
{ // Error Handling
}
return errorCode;
}
////////////////////////////////////////////////////////////
uint_32 I2C_rep_start()
{
errorMQX = ioctl (i2c_fd, IO_IOCTL_I2C_REPEATED_START, NULL);
if (I2C_OK != errorMQX )
{ // Error Handling }
return errorCode;
}
////////////////////////////////////////////////////////////
uint_32 I2C_write_byte(unsigned char data, int expect_ack)
{
uint_32 param;
uint_32 n;
n = fwrite (&data, 1, 1, i2c_fd);
if (1 != n)
{ // Error Handling }
/* Check ack */
if (I2C_OK == (errorMQX = ioctl(i2c_fd, IO_IOCTL_FLUSH_OUTPUT, ¶m)) )
{
if (param != expect_ack)
{
if (expect_ack == EXPECT_ACK)
{ // Error Handling }
else
{ // Error Handling }
}
}
else
{ // Error Handling
}
return errorCode;
}
////////////////////////////////////////////////////////////
uint_32 I2C_read_byte(uint_8 * buf, int requestBytes)
{
uint_32 n;
if (I2C_OK != ( errorMQX = ioctl (i2c_fd, IO_IOCTL_I2C_SET_RX_REQUEST, &requestBytes)) )
{ // Error Handling }
/* Read all data */
n = fread (buf, 1, requestBytes, i2c_fd);
if (n != requestBytes)
{ // Error Handling }
/* Wait for completion */
if (MQX_OK != (errorMQX = fflush (i2c_fd)) )
{ // Error Handling }
return errorCode;
}
// example /////////////////////////////////////////////////////////
I2C_start();
I2C_send_address(DS2482_I2C_ADDRESS);
I2C_write_byte(DS2482_CMD_1WT, EXPECT_ACK);
I2C_write_byte(search_direction ? 0x80 : 0x00, EXPECT_ACK);
I2C_stop();
///////////////////////////////////////////////
I2C_start();
I2C_send_address(DS2482_I2C_ADDRESS);
I2C_write_byte(DS2482_CMD_DRST, EXPECT_ACK);
I2C_rep_start();
I2C_read_byte(&status, 1);
I2C_stop();