From 6a31936e80ae2743376dcbf9d39cd92461c4deca Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Thu, 25 Nov 2021 01:59:07 +0100 Subject: i2s init/start/running fixed; i2c start returns error if i2s is running --- fw/fe310/eos/board.h | 8 +++---- fw/fe310/eos/eos.c | 2 ++ fw/fe310/eos/eve/eve_config.h | 1 - fw/fe310/eos/eve/eve_platform.c | 8 ------- fw/fe310/eos/i2c.c | 13 +++++++++-- fw/fe310/eos/i2c.h | 3 ++- fw/fe310/eos/i2c/bq25895.c | 32 +++++++++++++++----------- fw/fe310/eos/i2s.c | 51 ++++++++++++++--------------------------- fw/fe310/eos/i2s.h | 1 + fw/fe310/eos/i2s_def.h | 4 ++-- fw/fe310/eos/spi_cfg.h | 4 +++- fw/fe310/eos/spi_dev.h | 3 +-- fw/fe310/eos/trap_entry.S | 8 +++---- fw/fe310/test/cam.c | 4 ++-- fw/fe310/test/test.c | 12 ++++++---- 15 files changed, 75 insertions(+), 79 deletions(-) (limited to 'fw/fe310') diff --git a/fw/fe310/eos/board.h b/fw/fe310/eos/board.h index 611751b..b92d500 100644 --- a/fw/fe310/eos/board.h +++ b/fw/fe310/eos/board.h @@ -1,6 +1,5 @@ #define SPI_DIV_NET 16 #define SPI_DIV_EVE 4 -#define SPI_DIV_EVE_SLOW 16 #define SPI_DIV_SDC 1024 #define SPI_DIV_CAM 24 @@ -14,7 +13,7 @@ #define SPI_CSPIN_SDC 0 #define SPI_CSPIN_CAM SPI_CSPIN_NONE -#define SPI_IOF_MASK_CS (((uint32_t)1 << IOF_SPI1_SS0) | ((uint32_t)1 << IOF_SPI1_SS3)) +#define SPI_IOF_MASK_CS (((uint32_t)1 << IOF_SPI1_SS0) | ((uint32_t)1 << IOF_SPI1_SS2) | ((uint32_t)1 << IOF_SPI1_SS3)) #define NET_PIN_RTS 20 #define NET_PIN_CTS 22 @@ -41,5 +40,6 @@ #define EVE_GPIO_DIR 0xf #define EVE_GPIO_CAM 0 -#define EVE_GPIO_EXT 1 -#define EVE_GPIO_MOTOR 3 +#define EVE_GPIO_DISP_EN 1 +#define EVE_GPIO_GAIN_SEL 2 +#define EVE_GPIO_HAPT 3 diff --git a/fw/fe310/eos/eos.c b/fw/fe310/eos/eos.c index e394862..96fc298 100644 --- a/fw/fe310/eos/eos.c +++ b/fw/fe310/eos/eos.c @@ -5,6 +5,7 @@ #include "timer.h" #include "power.h" #include "i2s.h" +#include "i2c.h" #include "uart.h" #include "spi.h" #include "spi_dev.h" @@ -32,6 +33,7 @@ void eos_init(void) { eos_intr_init(wakeup_cause); eos_timer_init(wakeup_cause); eos_i2s_init(wakeup_cause); + eos_i2c_init(wakeup_cause); eos_uart_init(wakeup_cause); eos_spi_init(wakeup_cause); eos_spi_dev_init(wakeup_cause); diff --git a/fw/fe310/eos/eve/eve_config.h b/fw/fe310/eos/eve/eve_config.h index 6b9b0a6..b0dd1a4 100755 --- a/fw/fe310/eos/eve/eve_config.h +++ b/fw/fe310/eos/eve/eve_config.h @@ -32,7 +32,6 @@ #define EVE_SWIZZLE (0L) /* Defines the arrangement of the RGB pins of the FT800 */ #define EVE_PCLK (1L) /* 60MHz / REG_PCLK = PCLK frequency - 30 MHz */ #define EVE_CSPREAD (0L) /* helps with noise, when set to 1 fewer signals are changed simultaneously, reset-default: 1 */ -#define EVE_TOUCH_RZTHRESH (1200L) /* touch-sensitivity */ #define EVE_GEN 4 diff --git a/fw/fe310/eos/eve/eve_platform.c b/fw/fe310/eos/eve/eve_platform.c index 003f396..2a43f1c 100644 --- a/fw/fe310/eos/eve/eve_platform.c +++ b/fw/fe310/eos/eve/eve_platform.c @@ -66,14 +66,6 @@ void eve_spi_stop(void) { eos_spi_deselect(); } -void eve_spi_set_fast(void) { - eos_spi_set_div(EOS_SPI_DEV_EVE, SPI_DIV_EVE); -} - -void eve_spi_set_slow(void) { - eos_spi_set_div(EOS_SPI_DEV_EVE, SPI_DIV_EVE_SLOW); -} - #include void *eve_malloc(size_t size) { diff --git a/fw/fe310/eos/i2c.c b/fw/fe310/eos/i2c.c index 122a1b2..f296bc7 100644 --- a/fw/fe310/eos/i2c.c +++ b/fw/fe310/eos/i2c.c @@ -6,15 +6,24 @@ #include "prci_driver.h" #include "eos.h" +#include "i2s.h" #include "i2c.h" -void eos_i2c_start(uint32_t baud_rate) { - eos_i2c_set_baud_rate(baud_rate); +void eos_i2c_init(uint8_t wakeup_cause) { GPIO_REG(GPIO_IOF_SEL) &= ~IOF0_I2C0_MASK; +} + +int eos_i2c_start(uint32_t baud_rate) { + if (eos_i2s_running()) return EOS_ERR_BUSY; + + eos_i2c_set_baud_rate(baud_rate); GPIO_REG(GPIO_IOF_EN) |= IOF0_I2C0_MASK; + + return EOS_OK; } void eos_i2c_stop(void) { + if (eos_i2s_running()) return; GPIO_REG(GPIO_IOF_EN) &= ~IOF0_I2C0_MASK; } diff --git a/fw/fe310/eos/i2c.h b/fw/fe310/eos/i2c.h index 9692ea0..260e680 100644 --- a/fw/fe310/eos/i2c.h +++ b/fw/fe310/eos/i2c.h @@ -1,6 +1,7 @@ #include -void eos_i2c_start(uint32_t baud_rate); +void eos_i2c_init(uint8_t wakeup_cause); +int eos_i2c_start(uint32_t baud_rate); 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); diff --git a/fw/fe310/eos/i2c/bq25895.c b/fw/fe310/eos/i2c/bq25895.c index 85b162b..db4a6ab 100644 --- a/fw/fe310/eos/i2c/bq25895.c +++ b/fw/fe310/eos/i2c/bq25895.c @@ -9,27 +9,31 @@ void eos_bq25895_init(uint8_t wakeup_cause) { int rst = (wakeup_cause == EOS_PWR_WAKE_RST); - int i, ret = EOS_OK; + int i, rv = EOS_OK; uint8_t data = 0; - eos_i2c_start(100000); + rv = eos_i2c_start(100000); + if (rv) { + printf("I2C BUSY\n"); + return; + } if (rst) { - ret = eos_i2c_write8(BQ25895_ADDR, 0x14, 0x80); // reset - if (ret) printf("I2C ERROR 0x14\n"); - ret = eos_i2c_write8(BQ25895_ADDR, 0x14, 0x00); // disable watchdog - if (ret) printf("I2C ERROR 0x14\n"); - ret = eos_i2c_write8(BQ25895_ADDR, 0x07, 0x8d); // disable watchdog - if (ret) printf("I2C ERROR 0x07\n"); - ret = eos_i2c_write8(BQ25895_ADDR, 0x00, 0x28); // 2.1A input current - if (ret) printf("I2C ERROR 0x00\n"); - ret = eos_i2c_write8(BQ25895_ADDR, 0x02, 0x30); // enable ICO, disaable MaxCharge and D+/D- - if (ret) printf("I2C ERROR 0x02\n"); + rv = eos_i2c_write8(BQ25895_ADDR, 0x14, 0x80); // reset + if (rv) printf("I2C ERROR 0x14\n"); + rv = eos_i2c_write8(BQ25895_ADDR, 0x14, 0x00); // disable watchdog + if (rv) printf("I2C ERROR 0x14\n"); + rv = eos_i2c_write8(BQ25895_ADDR, 0x07, 0x8d); // disable watchdog + if (rv) printf("I2C ERROR 0x07\n"); + rv = eos_i2c_write8(BQ25895_ADDR, 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- + if (rv) printf("I2C ERROR 0x02\n"); } printf("BQ25895:\n"); for (i=0; i<0x15; i++) { - ret = eos_i2c_read8(BQ25895_ADDR, i, &data); - if (!ret) printf("REG%02x: %02x\n", i, data); + rv = eos_i2c_read8(BQ25895_ADDR, i, &data); + if (!rv) printf("REG%02x: %02x\n", i, data); } eos_i2c_stop(); } diff --git a/fw/fe310/eos/i2s.c b/fw/fe310/eos/i2s.c index f4d6b99..695967a 100644 --- a/fw/fe310/eos/i2s.c +++ b/fw/fe310/eos/i2s.c @@ -18,6 +18,8 @@ #define I2S_REG_WS_MIC(o) _REG32(I2S_CTRL_ADDR_WS_MIC, o) #define I2S_REG_WS_SPK(o) _REG32(I2S_CTRL_ADDR_WS_SPK, o) +#define I2S_PIN_PWM ((1 << I2S_PIN_CK) | (1 << I2S_PIN_CK_SW) | (1 << I2S_PIN_WS_MIC) | (1 << I2S_PIN_WS_SPK)) + #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) #define MAX(X, Y) (((X) > (Y)) ? (X) : (Y)) @@ -148,6 +150,11 @@ void eos_i2s_init(uint8_t wakeup_cause) { GPIO_REG(GPIO_PULLUP_EN) &= ~(1 << I2S_PIN_CK_SW); GPIO_REG(GPIO_OUTPUT_XOR) &= ~(1 << I2S_PIN_CK_SW); + GPIO_REG(GPIO_INPUT_EN) &= ~(1 << I2S_PIN_CK_SR); + GPIO_REG(GPIO_OUTPUT_EN) |= (1 << I2S_PIN_CK_SR); + GPIO_REG(GPIO_PULLUP_EN) &= ~(1 << I2S_PIN_CK_SR); + GPIO_REG(GPIO_OUTPUT_XOR) &= ~(1 << I2S_PIN_CK_SR); + GPIO_REG(GPIO_INPUT_EN) &= ~(1 << I2S_PIN_WS_MIC); GPIO_REG(GPIO_OUTPUT_EN) |= (1 << I2S_PIN_WS_MIC); GPIO_REG(GPIO_PULLUP_EN) &= ~(1 << I2S_PIN_WS_MIC); @@ -158,12 +165,7 @@ void eos_i2s_init(uint8_t wakeup_cause) { GPIO_REG(GPIO_PULLUP_EN) &= ~(1 << I2S_PIN_WS_SPK); GPIO_REG(GPIO_OUTPUT_XOR) &= ~(1 << I2S_PIN_WS_SPK); - GPIO_REG(GPIO_INPUT_EN) &= ~(1 << I2S_PIN_CK_SR); - GPIO_REG(GPIO_OUTPUT_EN) |= (1 << I2S_PIN_CK_SR); - GPIO_REG(GPIO_PULLUP_EN) &= ~(1 << I2S_PIN_CK_SR); - GPIO_REG(GPIO_OUTPUT_XOR) &= ~(1 << I2S_PIN_CK_SR); - - GPIO_REG(GPIO_INPUT_EN) &= ~(1 << I2S_PIN_SD_IN); + GPIO_REG(GPIO_INPUT_EN) |= (1 << I2S_PIN_SD_IN); GPIO_REG(GPIO_OUTPUT_EN) &= ~(1 << I2S_PIN_SD_IN); GPIO_REG(GPIO_PULLUP_EN) &= ~(1 << I2S_PIN_SD_IN); GPIO_REG(GPIO_OUTPUT_XOR) &= ~(1 << I2S_PIN_SD_IN); @@ -173,6 +175,9 @@ void eos_i2s_init(uint8_t wakeup_cause) { GPIO_REG(GPIO_PULLUP_EN) &= ~(1 << I2S_PIN_SD_OUT); GPIO_REG(GPIO_OUTPUT_XOR) &= ~(1 << I2S_PIN_SD_OUT); + GPIO_REG(GPIO_IOF_EN) &= ~I2S_PIN_PWM; + GPIO_REG(GPIO_IOF_SEL) |= I2S_PIN_PWM; + GPIO_REG(GPIO_OUTPUT_VAL) |= (1 << I2S_PIN_CK_SW); GPIO_REG(GPIO_OUTPUT_VAL) &= ~((1 << I2S_PIN_CK) | (1 << I2S_PIN_CK_SR) | (1 << I2S_PIN_WS_MIC) | (1 << I2S_PIN_WS_SPK)); } @@ -212,20 +217,8 @@ void eos_i2s_start(uint32_t sample_rate, unsigned char fmt) { I2S_REG_WS_SPK(PWM_CFG) = PWM_CFG_ENALWAYS | PWM_CFG_ZEROCMP | PWM_CFG_CMP1GANG; */ - GPIO_REG(GPIO_INPUT_EN) |= (1 << I2S_PIN_SD_IN); GPIO_REG(GPIO_OUTPUT_VAL) |= (1 << I2S_PIN_CK_SR); - - GPIO_REG(GPIO_IOF_SEL) |= (1 << I2S_PIN_CK); - GPIO_REG(GPIO_IOF_EN) |= (1 << I2S_PIN_CK); - - GPIO_REG(GPIO_IOF_SEL) |= (1 << I2S_PIN_CK_SW); - GPIO_REG(GPIO_IOF_EN) |= (1 << I2S_PIN_CK_SW); - - GPIO_REG(GPIO_IOF_SEL) |= (1 << I2S_PIN_WS_MIC); - GPIO_REG(GPIO_IOF_EN) |= (1 << I2S_PIN_WS_MIC); - - GPIO_REG(GPIO_IOF_SEL) |= (1 << I2S_PIN_WS_SPK); - GPIO_REG(GPIO_IOF_EN) |= (1 << I2S_PIN_WS_SPK); + GPIO_REG(GPIO_IOF_EN) |= I2S_PIN_PWM; } void eos_i2s_stop(void) { @@ -243,22 +236,12 @@ void eos_i2s_stop(void) { eos_intr_disable(I2S_IRQ_WS_ID); eos_intr_disable(I2S_IRQ_SD_ID); - GPIO_REG(GPIO_IOF_EN) &= ~(1 << I2S_PIN_CK); - GPIO_REG(GPIO_IOF_SEL) &= ~(1 << I2S_PIN_CK); - - GPIO_REG(GPIO_IOF_EN) &= ~(1 << I2S_PIN_CK_SW); - GPIO_REG(GPIO_IOF_SEL) &= ~(1 << I2S_PIN_CK_SW); - - GPIO_REG(GPIO_IOF_EN) &= ~(1 << I2S_PIN_WS_MIC); - GPIO_REG(GPIO_IOF_SEL) &= ~(1 << I2S_PIN_WS_MIC); - - GPIO_REG(GPIO_IOF_EN) &= ~(1 << I2S_PIN_WS_SPK); - GPIO_REG(GPIO_IOF_SEL) &= ~(1 << I2S_PIN_WS_SPK); - - GPIO_REG(GPIO_INPUT_EN) &= ~(1 << I2S_PIN_SD_IN); + GPIO_REG(GPIO_OUTPUT_VAL) &= ~(1 << I2S_PIN_CK_SR); + GPIO_REG(GPIO_IOF_EN) &= ~I2S_PIN_PWM; +} - GPIO_REG(GPIO_OUTPUT_VAL) |= (1 << I2S_PIN_CK_SW); - GPIO_REG(GPIO_OUTPUT_VAL) &= ~((1 << I2S_PIN_CK) | (1 << I2S_PIN_CK_SR) | (1 << I2S_PIN_WS_MIC) | (1 << I2S_PIN_WS_SPK)); +int eos_i2s_running(void) { + return !!(GPIO_REG(GPIO_IOF_EN) & I2S_PIN_PWM); } void eos_i2s_mic_init(uint8_t *mic_arr, uint16_t mic_arr_size) { diff --git a/fw/fe310/eos/i2s.h b/fw/fe310/eos/i2s.h index c5a6c1a..1339d0f 100644 --- a/fw/fe310/eos/i2s.h +++ b/fw/fe310/eos/i2s.h @@ -14,6 +14,7 @@ typedef void (*eos_i2s_handler_t) (unsigned char); void eos_i2s_init(uint8_t wakeup_cause); void eos_i2s_start(uint32_t sample_rate, unsigned char fmt); void eos_i2s_stop(void); +int eos_i2s_running(void); void eos_i2s_mic_init(uint8_t *mic_arr, uint16_t mic_arr_size); void eos_i2s_mic_set_handler(eos_i2s_handler_t wm_handler); void eos_i2s_mic_set_wm(uint16_t wm); diff --git a/fw/fe310/eos/i2s_def.h b/fw/fe310/eos/i2s_def.h index 1af70bb..a338828 100644 --- a/fw/fe310/eos/i2s_def.h +++ b/fw/fe310/eos/i2s_def.h @@ -1,5 +1,5 @@ -#define EOS_I2S_FMT_ALAW 0 -#define EOS_I2S_FMT_PCM16 1 +#define EOS_I2S_FMT_PCM16 0 +#define EOS_I2S_FMT_ALAW 1 #define EOS_I2S_ETYPE_MIC 1 #define EOS_I2S_ETYPE_SPK 2 diff --git a/fw/fe310/eos/spi_cfg.h b/fw/fe310/eos/spi_cfg.h index 43b0763..84ab8bb 100644 --- a/fw/fe310/eos/spi_cfg.h +++ b/fw/fe310/eos/spi_cfg.h @@ -1,5 +1,7 @@ #include +#define EOS_SPI_MAX_DEV 4 + typedef struct { uint16_t div; uint8_t csid; @@ -7,7 +9,7 @@ typedef struct { unsigned char evt; } SPIConfig; -static const SPIConfig spi_cfg[] = { +static const SPIConfig spi_cfg[EOS_SPI_MAX_DEV] = { { // DEV_NET .div = SPI_DIV_NET, .csid = SPI_CSID_NET, diff --git a/fw/fe310/eos/spi_dev.h b/fw/fe310/eos/spi_dev.h index 2f56149..d5ddeb3 100644 --- a/fw/fe310/eos/spi_dev.h +++ b/fw/fe310/eos/spi_dev.h @@ -4,8 +4,7 @@ #define EOS_SPI_DEV_EVE 1 #define EOS_SPI_DEV_SDC 2 #define EOS_SPI_DEV_CAM 3 - -#define EOS_SPI_MAX_DEV 4 +#define EOS_SPI_DEV_DISP 4 void eos_spi_dev_init(uint8_t wakeup_cause); int eos_spi_select(unsigned char dev); diff --git a/fw/fe310/eos/trap_entry.S b/fw/fe310/eos/trap_entry.S index d54243f..055ae67 100644 --- a/fw/fe310/eos/trap_entry.S +++ b/fw/fe310/eos/trap_entry.S @@ -111,7 +111,7 @@ i2s_abuf_pop: and x20, x20, x18 lw x21, I2S_ABUF_OFF_ARRAY(x9) add x21, x21, x20 - bnez x23, 0f + beqz x23, 0f lbu x8, 0(x21) addi x18, x18, 1 j 1f @@ -146,7 +146,7 @@ i2s_abuf_pop: sb x18, MSGQ_ITEM_OFF_TYPE(x21) i2s_decode: - bnez x23, i2s_handle_sd_xchg + beqz x23, i2s_handle_sd_xchg # aLaw decode -> x8 xori x8, x8, 0x55 andi x9, x8, 0x80 @@ -260,7 +260,7 @@ i2s_handle_sd_xchg: lw x23, 0(x9) i2s_encode: - bnez x23, i2s_abuf_push + beqz x23, i2s_abuf_push # aLaw encode -> x8 li x18, 0x800 li x19, 7 @@ -307,7 +307,7 @@ i2s_abuf_push: and x20, x20, x19 lw x21, I2S_ABUF_OFF_ARRAY(x9) add x21, x21, x20 - bnez x23, 0f + beqz x23, 0f sb x8, 0(x21) addi x19, x19, 1 addi x18, x18, 1 diff --git a/fw/fe310/test/cam.c b/fw/fe310/test/cam.c index caab2cc..3162b40 100644 --- a/fw/fe310/test/cam.c +++ b/fw/fe310/test/cam.c @@ -139,9 +139,9 @@ void app_cam(EVEWindow *window, EVEViewStack *stack) { eve_gpio_set(EVE_GPIO_CAM, 1); eos_time_sleep(100); - eos_i2c_start(100000); - rv = eos_ov2640_init(); + rv = eos_i2c_start(100000); + 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(); diff --git a/fw/fe310/test/test.c b/fw/fe310/test/test.c index 9e5c790..cda0255 100644 --- a/fw/fe310/test/test.c +++ b/fw/fe310/test/test.c @@ -26,16 +26,20 @@ int app_test_uievt(EVEForm *form, uint16_t evt, void *param) { uint8_t data = 0; - int ret = 0, i; + int rv, ret = 0, i; switch (evt) { case EVE_UIEVT_PAGE_TOUCH: printf("PAGE TOUCH\n"); printf("BQ25895:\n"); - eos_i2c_start(400000); + rv = eos_i2c_start(400000); + if (rv) { + printf("I2C BUSY\n"); + return 0; + } for (i=0; i<0x15; i++) { - ret = eos_i2c_read8(BQ25895_ADDR, i, &data); - if (!ret) printf("REG%02x: %02x\n", i, data); + rv = eos_i2c_read8(BQ25895_ADDR, i, &data); + if (!rv) printf("REG%02x: %02x\n", i, data); } eos_i2c_stop(); break; -- cgit v1.2.3