Skip to content

Commit

Permalink
Merge pull request whitecatboard#312 from the0ne/i2c_fix
Browse files Browse the repository at this point in the history
fix i2c bug
  • Loading branch information
jolivepetrus authored Dec 19, 2019
2 parents 3a3d1cc + d7023b9 commit 61351b7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
35 changes: 30 additions & 5 deletions components/lua/modules/hw/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,27 +258,52 @@ static int li2c_address(lua_State* L) {
return 0;
}

#define MAX_DATA_BYTES 128
static int li2c_read(lua_State* L) {
driver_error_t *error;
i2c_user_data_t *user_data;
char data;

// Get user data
user_data = (i2c_user_data_t *) luaL_checkudata(L, 1, "i2c.trans");
luaL_argcheck(L, user_data, 1, "i2c transaction expected");

if ((error = i2c_read(user_data->unit, &user_data->transaction, &data, 1))) {
uint8_t length = luaL_optinteger(L, 2, 1 );
if (length < 1) {
length = 1; //minimum length
}
else if (length > MAX_DATA_BYTES) {
length = MAX_DATA_BYTES; //maximum length
}

uint8_t asString = 0;
// Check if user wants result as integers or as string
if (lua_gettop(L) == 3) {
luaL_checktype(L, 3, LUA_TBOOLEAN);
if (lua_toboolean(L, 3)) {
asString = 1;
}
}

char data[length]; // with length being a minimum of 1 and a maximum of MAX_DATA_BYTES

if ((error = i2c_read(user_data->unit, &user_data->transaction, data, length))) {
return luaL_driver_error(L, error);
}

// We need to flush because we need to return reaad data now
// We need to flush because we need to return read data now
if ((error = i2c_flush(user_data->unit, &user_data->transaction, 1))) {
return luaL_driver_error(L, error);
}

lua_pushinteger(L, (int) data);
if (asString == 1) {
lua_pushlstring(L, data, length);
return 1;
}

return 1;
for(uint8_t i=0; i<length; i++) {
lua_pushinteger(L, (int) data[i]);
}
return length;
}

static int li2c_write(lua_State* L) {
Expand Down
10 changes: 3 additions & 7 deletions components/sys/drivers/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@

#define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/
#define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */
#define ACK_VAL 0x0 /*!< I2C ack value */
#define NACK_VAL 0x1 /*!< I2C nack value */

// Register driver and messages
static void i2c_init();
Expand Down Expand Up @@ -566,8 +564,7 @@ driver_error_t *i2c_stop(int deviceid, int *transaction) {

if (i2c[unit].device[device].reading) {
uint8_t dummy;

i2c_master_read_byte(cmd, (uint8_t *) (&dummy), NACK_VAL);
i2c_master_read_byte(cmd, (uint8_t *) (&dummy), I2C_MASTER_NACK);
}

i2c_master_stop(cmd);
Expand Down Expand Up @@ -696,10 +693,9 @@ driver_error_t *i2c_read(int deviceid, int *transaction, char *data, int len) {
}

if (len > 1) {
i2c_master_read(cmd, (uint8_t *) data, len - 1, ACK_VAL);
i2c_master_read_byte(cmd, (uint8_t *) (data + len - 1), NACK_VAL);
i2c_master_read(cmd, (uint8_t *) data, len, I2C_MASTER_LAST_NACK);
} else {
i2c_master_read_byte(cmd, (uint8_t *) (data + len - 1), ACK_VAL);
i2c_master_read_byte(cmd, (uint8_t *) data, I2C_MASTER_ACK);
}

i2c_unlock(unit);
Expand Down

0 comments on commit 61351b7

Please sign in to comment.