diff options
author | Uros Majstorovic <majstor@majstor.org> | 2021-12-01 01:20:10 +0100 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2021-12-01 01:20:10 +0100 |
commit | 7ded8116d90081851e0ddb1b4bdd77b645f6535c (patch) | |
tree | dc89dbc4269fc0d2e3ac0e43ca8a533371d21531 /fw | |
parent | 5506d89d97db87f78ab2ba4715c4248a3d49f98f (diff) |
i2c added 16 bit register read/write
Diffstat (limited to 'fw')
-rw-r--r-- | fw/fe310/eos/i2c.c | 71 | ||||
-rw-r--r-- | fw/fe310/eos/i2c.h | 14 | ||||
-rw-r--r-- | fw/fe310/eos/i2c/bq25895.c | 22 | ||||
-rw-r--r-- | fw/fe310/eos/i2c/ov2640.c | 4 | ||||
-rw-r--r-- | fw/fe310/test/cam.c | 4 | ||||
-rw-r--r-- | fw/fe310/test/test.c | 12 |
6 files changed, 92 insertions, 35 deletions
diff --git a/fw/fe310/eos/i2c.c b/fw/fe310/eos/i2c.c index 37c5cf8..972deae 100644 --- a/fw/fe310/eos/i2c.c +++ b/fw/fe310/eos/i2c.c @@ -10,33 +10,31 @@ #include "i2c.h" int eos_i2c_init(uint8_t wakeup_cause) { - GPIO_REG(GPIO_IOF_SEL) &= ~IOF0_I2C0_MASK; - + eos_i2c_stop(); + eos_i2c_speed(EOS_I2C_SPEED); return EOS_OK; } -int eos_i2c_start(uint32_t baud_rate) { +int eos_i2c_start(void) { if (eos_i2s_running()) return EOS_ERR_BUSY; - eos_i2c_set_baud_rate(baud_rate); - GPIO_REG(GPIO_IOF_EN) |= IOF0_I2C0_MASK; + GPIO_REG(GPIO_IOF_EN) |= IOF0_I2C0_MASK; + I2C0_REGB(I2C_CONTROL) |= I2C_CONTROL_EN; return EOS_OK; } void eos_i2c_stop(void) { - if (eos_i2s_running()) return; - GPIO_REG(GPIO_IOF_EN) &= ~IOF0_I2C0_MASK; + GPIO_REG(GPIO_IOF_EN) &= ~IOF0_I2C0_MASK; + I2C0_REGB(I2C_CONTROL) &= ~I2C_CONTROL_EN; } -void eos_i2c_set_baud_rate(uint32_t baud_rate) { +void eos_i2c_speed(uint32_t baud_rate) { unsigned long clock_rate = PRCI_get_cpu_freq(); uint16_t prescaler = (clock_rate / (baud_rate * 5)) - 1; - I2C0_REGB(I2C_CONTROL) &= ~I2C_CONTROL_EN; I2C0_REGB(I2C_PRESCALE_LOW) = prescaler & 0xFF; I2C0_REGB(I2C_PRESCALE_HIGH) = (prescaler >> 8) & 0xFF; - I2C0_REGB(I2C_CONTROL) |= I2C_CONTROL_EN; } @@ -60,7 +58,7 @@ static int i2c_addr(uint8_t addr, uint8_t rw_flag) { return i2c_write(I2C_CMD_START, ((addr & 0x7F) << 1) | (rw_flag & 0x1)); } -int eos_i2c_read(uint8_t addr, uint8_t reg, uint8_t *buffer, uint16_t len) { +int eos_i2c_read8(uint8_t addr, uint8_t reg, uint8_t *buffer, uint16_t len) { int rv; int i; @@ -83,7 +81,33 @@ int eos_i2c_read(uint8_t addr, uint8_t reg, uint8_t *buffer, uint16_t len) { return EOS_OK; } -int eos_i2c_write(uint8_t addr, uint8_t reg, uint8_t *buffer, uint16_t len) { +int eos_i2c_read16(uint8_t addr, uint16_t reg, uint8_t *buffer, uint16_t len) { + int rv; + int i; + + rv = i2c_addr(addr, I2C_WRITE); + if (rv) return rv; + + rv = i2c_write(0, reg >> 8); + if (rv) return rv; + + rv = i2c_write(0, reg & 0xff); + if (rv) return rv; + + rv = i2c_addr(addr, I2C_READ); + if (rv) return rv; + + for (i=0; i<len; i++) { + rv = i2c_read(i == (len - 1) ? (I2C_CMD_ACK | I2C_CMD_STOP) : 0); /* Set NACK to end read, and generate STOP condition */ + if (rv < 0) return rv; + + buffer[i] = (uint8_t)rv; + } + + return EOS_OK; +} + +int eos_i2c_write8(uint8_t addr, uint8_t reg, uint8_t *buffer, uint16_t len) { int rv; int i; @@ -101,10 +125,23 @@ int eos_i2c_write(uint8_t addr, uint8_t reg, uint8_t *buffer, uint16_t len) { return EOS_OK; } -int eos_i2c_read8(uint8_t addr, uint8_t reg, uint8_t *data) { - return eos_i2c_read(addr, reg, data, 1); -} +int eos_i2c_write16(uint8_t addr, uint16_t reg, uint8_t *buffer, uint16_t len) { + int rv; + int i; + + rv = i2c_addr(addr, I2C_WRITE); + if (rv) return rv; -int eos_i2c_write8(uint8_t addr, uint8_t reg, uint8_t data) { - return eos_i2c_write(addr, reg, &data, 1); + rv = i2c_write(0, reg >> 8); + if (rv) return rv; + + rv = i2c_write(0, reg & 0xff); + if (rv) return rv; + + for (i=0; i<len; i++) { + rv = i2c_write(i == (len - 1) ? I2C_CMD_STOP : 0, buffer[i]); + if (rv) return rv; + } + + return EOS_OK; } diff --git a/fw/fe310/eos/i2c.h b/fw/fe310/eos/i2c.h index 2da0517..b407f8d 100644 --- a/fw/fe310/eos/i2c.h +++ b/fw/fe310/eos/i2c.h @@ -1,10 +1,12 @@ #include <stdint.h> +#define EOS_I2C_SPEED 100000 + int eos_i2c_init(uint8_t wakeup_cause); -int eos_i2c_start(uint32_t baud_rate); +int eos_i2c_start(void); void eos_i2c_stop(void); -void eos_i2c_set_baud_rate(uint32_t baud_rate); -int eos_i2c_read(uint8_t addr, uint8_t reg, uint8_t *buffer, uint16_t len); -int eos_i2c_write(uint8_t addr, uint8_t reg, uint8_t *buffer, uint16_t len); -int eos_i2c_read8(uint8_t addr, uint8_t reg, uint8_t *data); -int eos_i2c_write8(uint8_t addr, uint8_t reg, uint8_t data); +void eos_i2c_speed(uint32_t baud_rate); +int eos_i2c_read8(uint8_t addr, uint8_t reg, uint8_t *buffer, uint16_t len); +int eos_i2c_read16(uint8_t addr, uint16_t reg, uint8_t *buffer, uint16_t len); +int eos_i2c_write8(uint8_t addr, uint8_t reg, uint8_t *buffer, uint16_t len); +int eos_i2c_write16(uint8_t addr, uint16_t reg, uint8_t *buffer, uint16_t len); diff --git a/fw/fe310/eos/i2c/bq25895.c b/fw/fe310/eos/i2c/bq25895.c index ab2268b..f8923c6 100644 --- a/fw/fe310/eos/i2c/bq25895.c +++ b/fw/fe310/eos/i2c/bq25895.c @@ -7,29 +7,37 @@ #include "i2c.h" #include "i2c/bq25895.h" +static int reg_read(uint8_t reg, uint8_t *data) { + return eos_i2c_read8(BQ25895_ADDR, reg, data, 1); +} + +static int reg_write(uint8_t reg, uint8_t data) { + return eos_i2c_write8(BQ25895_ADDR, reg, &data, 1); +} + int eos_bq25895_init(uint8_t wakeup_cause) { int rst = (wakeup_cause == EOS_PWR_WAKE_RST); int i, rv = EOS_OK; uint8_t data = 0; - rv = eos_i2c_start(100000); + rv = eos_i2c_start(); if (rv) return rv; if (rst) { - rv = eos_i2c_write8(BQ25895_ADDR, 0x14, 0x80); // reset + rv = reg_write(0x14, 0x80); // reset if (rv) printf("I2C ERROR 0x14\n"); - rv = eos_i2c_write8(BQ25895_ADDR, 0x14, 0x00); // disable watchdog + rv = reg_write(0x14, 0x00); // disable watchdog if (rv) printf("I2C ERROR 0x14\n"); - rv = eos_i2c_write8(BQ25895_ADDR, 0x07, 0x8d); // disable watchdog + rv = reg_write(0x07, 0x8d); // disable watchdog if (rv) printf("I2C ERROR 0x07\n"); - rv = eos_i2c_write8(BQ25895_ADDR, 0x00, 0x28); // 2.1A input current + rv = reg_write(0x00, 0x28); // 2.1A input current if (rv) printf("I2C ERROR 0x00\n"); - rv = eos_i2c_write8(BQ25895_ADDR, 0x02, 0x30); // enable ICO, disaable MaxCharge and D+/D- + rv = reg_write(0x02, 0x30); // enable ICO, disaable MaxCharge and D+/D- if (rv) printf("I2C ERROR 0x02\n"); } printf("BQ25895:\n"); for (i=0; i<0x15; i++) { - rv = eos_i2c_read8(BQ25895_ADDR, i, &data); + rv = reg_read(i, &data); if (!rv) printf("REG%02x: %02x\n", i, data); } eos_i2c_stop(); diff --git a/fw/fe310/eos/i2c/ov2640.c b/fw/fe310/eos/i2c/ov2640.c index 1a29463..6e54f10 100644 --- a/fw/fe310/eos/i2c/ov2640.c +++ b/fw/fe310/eos/i2c/ov2640.c @@ -339,11 +339,11 @@ static const uint8_t saturation_regs[NUM_SATURATION_LEVELS + 1][5] = { }; static int reg_read(int8_t reg, uint8_t *data) { - return eos_i2c_read8(OV2640_ADDR, reg, data); + return eos_i2c_read8(OV2640_ADDR, reg, data, 1); } static int reg_write(uint8_t reg, uint8_t data) { - return eos_i2c_write8(OV2640_ADDR, reg, data); + return eos_i2c_write8(OV2640_ADDR, reg, &data, 1); } static int regarr_write(const uint8_t (*regs)[2]) { diff --git a/fw/fe310/test/cam.c b/fw/fe310/test/cam.c index 3162b40..2cef686 100644 --- a/fw/fe310/test/cam.c +++ b/fw/fe310/test/cam.c @@ -140,11 +140,13 @@ void app_cam(EVEWindow *window, EVEViewStack *stack) { eve_gpio_set(EVE_GPIO_CAM, 1); eos_time_sleep(100); - rv = eos_i2c_start(100000); + eos_i2c_speed(100000); + rv = eos_i2c_start(); if (!rv) rv = eos_ov2640_init(); if (!rv) rv = eos_ov2640_set_pixfmt(PIXFORMAT_JPEG); if (!rv) rv = eos_ov2640_set_framesize(FRAMESIZE_VGA); eos_i2c_stop(); + eos_i2c_speed(EOS_I2C_SPEED); if (!rv) { printf("CAM INIT\n"); diff --git a/fw/fe310/test/test.c b/fw/fe310/test/test.c index cda0255..87c1e2b 100644 --- a/fw/fe310/test/test.c +++ b/fw/fe310/test/test.c @@ -24,6 +24,14 @@ #include <stdio.h> +static int reg_read(uint8_t reg, uint8_t *data) { + return eos_i2c_read8(BQ25895_ADDR, reg, data, 1); +} + +static int reg_write(uint8_t reg, uint8_t data) { + return eos_i2c_write8(BQ25895_ADDR, reg, &data, 1); +} + int app_test_uievt(EVEForm *form, uint16_t evt, void *param) { uint8_t data = 0; int rv, ret = 0, i; @@ -32,13 +40,13 @@ int app_test_uievt(EVEForm *form, uint16_t evt, void *param) { case EVE_UIEVT_PAGE_TOUCH: printf("PAGE TOUCH\n"); printf("BQ25895:\n"); - rv = eos_i2c_start(400000); + rv = eos_i2c_start(); if (rv) { printf("I2C BUSY\n"); return 0; } for (i=0; i<0x15; i++) { - rv = eos_i2c_read8(BQ25895_ADDR, i, &data); + rv = reg_read(i, &data); if (!rv) printf("REG%02x: %02x\n", i, data); } eos_i2c_stop(); |