Skip to content

Commit

Permalink
[driver] add some error handle for i2c
Browse files Browse the repository at this point in the history
  • Loading branch information
YuzukiTsuru committed Dec 31, 2023
1 parent c67a14d commit 9546ab5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/drivers/sys-i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ typedef struct {
uint32_t speed;
gpio_mux_t gpio_scl;
gpio_mux_t gpio_sda;
bool status;
} sunxi_i2c_t;

enum {
Expand Down
2 changes: 1 addition & 1 deletion include/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ enum {
};

#ifndef LOG_LEVEL_DEFAULT
#define LOG_LEVEL_DEFAULT LOG_LEVEL_INFO
#define LOG_LEVEL_DEFAULT LOG_LEVEL_DEBUG
#endif

void set_timer_count();
Expand Down
13 changes: 13 additions & 0 deletions src/drivers/sys-i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,13 +388,24 @@ static int _sunxi_i2c_write(sunxi_i2c_t *i2c_dev, uint8_t chip, uint32_t addr, i
}

int sunxi_i2c_write(sunxi_i2c_t *i2c_dev, uint8_t addr, uint32_t reg, uint8_t data) {
if (!i2c_dev->status)
return I2C_NOK;

return _sunxi_i2c_write(i2c_dev, addr, reg, 1, &data, 1);
}

int sunxi_i2c_read(sunxi_i2c_t *i2c_dev, uint8_t addr, uint32_t reg, uint8_t *data) {
if (!i2c_dev->status)
return I2C_NOK;

return _sunxi_i2c_read(i2c_dev, addr, reg, 1, data, 1);
}

/**
* @brief Set the clock frequency of sunxi_i2c.
*
* @param i2c_dev Pointer to sunxi_i2c_t structure.
*/
static void sunxi_i2c_set_clock(sunxi_i2c_t *i2c_dev) {
struct sunxi_twi_reg *i2c = (struct sunxi_twi_reg *) i2c_dev->base;

Expand Down Expand Up @@ -496,4 +507,6 @@ void sunxi_i2c_init(sunxi_i2c_t *i2c_dev) {

printk(LOG_LEVEL_DEBUG, "I2C: Bus open done.\n");
sunxi_i2c_set_clock(i2c_dev);

i2c_dev->status = true;
}

0 comments on commit 9546ab5

Please sign in to comment.