I2C
Ameba SDK » HAL

I2C functions. More...

## Ameba Common

enum | { I2C_ERROR_NO_SLAVE = -1, I2C_ERROR_BUS_BUSY = -2 }
typedef struct i2c_s | i2c_t
void | i2c_init (i2c_t *obj, PinName sda, PinName scl)
| Initializes the I2C device, include clock/function/I2C registers. More...
void | i2c_frequency (i2c_t *obj, int hz)
| Set i2c frequency. More...
int | i2c_start (i2c_t *obj)
| Start i2c device. More...
int | i2c_stop (i2c_t *obj)
| Stop i2c device. More...
void | i2c_reset (i2c_t *obj)
| Deinitializes the I2C device. More...
int | i2c_byte_read (i2c_t *obj, int last)
| I2C master receive single byte. More...
int | i2c_byte_write (i2c_t *obj, int data)
| I2C master send single byte. More...
void | i2c_slave_mode (i2c_t *obj, int enable_slave)
| Set i2c device to be slave. More...
int | i2c_slave_receive (i2c_t *obj)
| Get i2c slave state. More...
void | i2c_slave_address (i2c_t *obj, int idx, uint32_t address, uint32_t mask)
| Set i2c slave address. More...
int | i2c_read (i2c_t *obj, int address, char *data, int length, int stop)
| I2C master read in poll mode. More...
int | i2c_write (i2c_t *obj, int address, const char *data, int length, int stop)
| I2C master write in poll mode. More...
int | i2c_slave_read (i2c_t *obj, char *data, int length)
| I2C slave read in poll mode. More...
int | i2c_slave_write (i2c_t *obj, const char *data, int length)
| I2C slave write in poll mode. More...
int | i2c_slave_set_for_rd_req (i2c_t *obj, int set)
| Set/clear i2c slave RD_REQ interrupt mask. More...
int | i2c_slave_set_for_data_nak (i2c_t *obj, int set_nak)
| Set/clear i2c slave NAK or ACK data part in transfer. More...

## AmebaZ Only

int | i2c_repeatread (i2c_t *obj, int address, u8 *pWriteBuf, int Writelen, u8 *pReadBuf, int Readlen)
| I2C master send data and read data in poll mode. More...

## Detailed Description

I2C functions.

## Function Documentation

int i2c_byte_read | ( | i2c_t * | obj,
|  | int | last
| ) |  |

I2C master receive single byte.

Parameters

  obj | i2c object define in application software.
  last | hold the received data.

Return values

  | the received data.

int i2c_byte_write | ( | i2c_t * | obj,
|  | int | data
| ) |  |

I2C master send single byte.

Parameters

  obj | i2c object define in application software.
  data | the data to be sent.

Return values

  | result.

void i2c_frequency | ( | i2c_t * | obj,
|  | int | hz
| ) |  |

Set i2c frequency.

Parameters

  obj | i2c object define in application software.
  hz | i2c clock(unit is Hz).

Return values

  none |

void i2c_init | ( | i2c_t * | obj,
|  | PinName | sda,
|  | PinName | scl
| ) |  |

Initializes the I2C device, include clock/function/I2C registers.

Parameters

  obj | i2c object define in application software.
  sda | SDA PinName according to pinmux spec.
  scl | SCL PinName according to pinmux spec.

Return values

  none |

int i2c_read | ( | i2c_t * | obj,
|  | int | address,
|  | char * | data,
|  | int | length,
|  | int | stop
| ) |  |

I2C master read in poll mode.

Parameters

  obj | i2c object define in application software.
  address | slave address which will be transmitted.
  data | point to the buffer to hold the received data.
  length | the length of data that to be received.
  stop | specifies whether a STOP is issued after all the bytes are received.

Return values

  | the length of data received.

int i2c_repeatread | ( | i2c_t * | obj,
|  | int | address,
|  | u8 * | pWriteBuf,
|  | int | Writelen,
|  | u8 * | pReadBuf,
|  | int | Readlen
| ) |  |

I2C master send data and read data in poll mode.

Parameters

  obj | i2c object define in application software.
  address | slave address which will be transmitted.
  pWriteBuf | point to the data to be sent.
  Writelen | the length of data that to be sent.
  pReadBuf | point to the buffer to hold the received data.
  Readlen | the length of data that to be received.

Return values

  the | length of data received.

void i2c_reset | ( | i2c_t * | obj | ) |

Deinitializes the I2C device.

Parameters

  obj | i2c object define in application software.

Return values

  none |

void i2c_slave_address | ( | i2c_t * | obj,
|  | int | idx,
|  | uint32_t | address,
|  | uint32_t | mask
| ) |  |

Set i2c slave address.

Parameters

  obj | i2c object define in application software.

  idx

  i2c index, this parameter can be one of the following values:
  - 0 I2C0 Device
  - 1 I2C1 Device

  address | slave address.
  mask | the mask of address

Return values

  none |

void i2c_slave_mode | ( | i2c_t * | obj,
|  | int | enable_slave
| ) |  |

Set i2c device to be slave.

Parameters

  obj | i2c object define in application software.

  enable_slave

  enable slave function, this parameter can be one of the following values:
  - 0 disable
  - 1 enable

Return values

  none |

int i2c_slave_read | ( | i2c_t * | obj,
|  | char * | data,
|  | int | length
| ) |  |

I2C slave read in poll mode.

Parameters

  obj | i2c object define in application software.
  data | point to the buffer to hold the received data.
  length | the length of data that to be received.

Return values

  | the length of data received.

int i2c_slave_receive | ( | i2c_t * | obj | ) |

Get i2c slave state.

Parameters

  obj | i2c object define in application software.

Return values

  | the state of i2c slave.

int i2c_slave_set_for_data_nak | ( | i2c_t * | obj,
|  | int | set_nak
| ) |  |

Set/clear i2c slave NAK or ACK data part in transfer.

Parameters

  obj | i2c object define in application software.
  set_nak | set or clear for data NAK.

Return values

  1 | SUCCESS

int i2c_slave_set_for_rd_req | ( | i2c_t * | obj,
|  | int | set
| ) |  |

Set/clear i2c slave RD_REQ interrupt mask.

Parameters

  obj | i2c object define in application software.
  set | set or clear for read request.

Return values

  1 | SUCCESS

int i2c_slave_write | ( | i2c_t * | obj,
|  | const char * | data,
|  | int | length
| ) |  |

I2C slave write in poll mode.

Parameters

  obj | i2c object define in application software.
  data | point to the data to be sent.
  length | the length of data that to be sent.

Return values

  0 | FAIL
  1 | SUCCESS

int i2c_start | ( | i2c_t * | obj | ) |

Start i2c device.

Parameters

  obj | i2c object define in application software.

Return values

  0 |

int i2c_stop | ( | i2c_t * | obj | ) |

Stop i2c device.

Parameters

  obj | i2c object define in application software.

Return values

  0 |

int i2c_write | ( | i2c_t * | obj,
|  | int | address,
|  | const char * | data,
|  | int | length,
|  | int | stop
| ) |  |

I2C master write in poll mode.

Parameters

  obj | i2c object define in application software.
  address | slave address which will be transmitted.
  data | point to the data to be sent.
  length | the length of data that to be sent.
  stop | specifies whether a STOP is issued after all the bytes are sent.

Return values

  | the length of data send.