Move i2c support to common
This commit is contained in:
53
src/common/i2c.c
Normal file
53
src/common/i2c.c
Normal file
@@ -0,0 +1,53 @@
|
||||
#include <common/i2c.h>
|
||||
|
||||
int i2c_recv(uint8_t addr, uint8_t* data, int length) {
|
||||
int res = 0;
|
||||
|
||||
res = i2c_start(addr, true);
|
||||
if (res < 0) return res;
|
||||
|
||||
res = i2c_read(data, length);
|
||||
if (res < 0) return res;
|
||||
|
||||
i2c_stop();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
int i2c_send(uint8_t addr, uint8_t* data, int length) {
|
||||
int res = 0;
|
||||
|
||||
res = i2c_start(addr, false);
|
||||
if (res < 0) return res;
|
||||
|
||||
res = i2c_write(data, length);
|
||||
if (res < 0) return res;
|
||||
|
||||
i2c_stop();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
int i2c_get(uint8_t addr, uint8_t reg, uint8_t* data, int length) {
|
||||
int res = 0;
|
||||
|
||||
res = i2c_start(addr, false);
|
||||
if (res < 0) return res;
|
||||
|
||||
res = i2c_write(®, 1);
|
||||
if (res < 0) return res;
|
||||
|
||||
return i2c_recv(addr, data, length);
|
||||
}
|
||||
|
||||
int i2c_set(uint8_t addr, uint8_t reg, uint8_t* data, int length) {
|
||||
int res = 0;
|
||||
|
||||
res = i2c_start(addr, false);
|
||||
if (res < 0) return res;
|
||||
|
||||
res = i2c_write(®, 1);
|
||||
if (res < 0) return res;
|
||||
|
||||
return i2c_send(addr, data, length);
|
||||
}
|
35
src/common/include/common/i2c.h
Normal file
35
src/common/include/common/i2c.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef _COMMON_I2C_H
|
||||
#define _COMMON_I2C_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// Start i2c transaction
|
||||
// Must be defined by arch, board, or ec
|
||||
int i2c_start(uint8_t addr, bool read);
|
||||
|
||||
// Stop i2c transaction
|
||||
// Must be defined by arch, board, or ec
|
||||
void i2c_stop(void);
|
||||
|
||||
// Send a byte on i2c bus
|
||||
// Must be defined by arch, board, or ec
|
||||
int i2c_write(uint8_t * data, int length);
|
||||
|
||||
// Read bytes from bus
|
||||
// Must be defined by arch, board, or ec
|
||||
int i2c_read(uint8_t * data, int length);
|
||||
|
||||
// Read multiple bytes from address in one transaction
|
||||
int i2c_recv(uint8_t addr, uint8_t* data, int length);
|
||||
|
||||
// Write multiple bytes to address in one transaction
|
||||
int i2c_send(uint8_t addr, uint8_t* data, int length);
|
||||
|
||||
// Read multiple bytes from a register in one transaction
|
||||
int i2c_get(uint8_t addr, uint8_t reg, uint8_t* data, int length);
|
||||
|
||||
// Write multiple bytes to a register in one transaction
|
||||
int i2c_set(uint8_t addr, uint8_t reg, uint8_t* data, int length);
|
||||
|
||||
#endif // _COMMON_I2C_H
|
Reference in New Issue
Block a user