From 5506d89d97db87f78ab2ba4715c4248a3d49f98f Mon Sep 17 00:00:00 2001
From: Uros Majstorovic <majstor@majstor.org>
Date: Tue, 30 Nov 2021 22:05:17 +0100
Subject: startup code uses init/run

---
 fw/fe310/eos/eos.c              | 16 ++++++++------
 fw/fe310/eos/eos.h              |  2 +-
 fw/fe310/eos/eve/eve.c          | 46 +++++++++++++++++++++++++++++------------
 fw/fe310/eos/eve/eve.h          |  3 ++-
 fw/fe310/eos/eve/eve_config.h   | 16 +++++++-------
 fw/fe310/eos/eve/eve_platform.h |  6 +++---
 fw/fe310/eos/eve/eve_touch.c    | 26 ++++++++++++++++++-----
 fw/fe310/eos/eve/eve_touch.h    |  5 +++--
 fw/fe310/eos/i2c/bq25895.c      |  2 +-
 fw/fe310/eos/lcd.c              |  4 ++--
 fw/fe310/eos/net.c              | 26 +++++++++++++++++------
 fw/fe310/eos/net.h              |  3 ++-
 fw/fe310/eos/power.h            |  5 -----
 fw/fe310/eos/spi_dev.c          |  2 +-
 fw/fe310/test/main.c            |  2 +-
 15 files changed, 109 insertions(+), 55 deletions(-)

diff --git a/fw/fe310/eos/eos.c b/fw/fe310/eos/eos.c
index 0b6228b..2f1f4d3 100644
--- a/fw/fe310/eos/eos.c
+++ b/fw/fe310/eos/eos.c
@@ -23,12 +23,12 @@
 #include "eos.h"
 
 void eos_init(void) {
-    uint8_t wakeup_cause = eos_power_wakeup_cause() | EOS_PWR_INIT;
+    uint8_t wakeup_cause = eos_power_wakeup_cause();
     uint32_t touch_matrix[6] = {0xfa46,0xfffffcf6,0x422fe,0xffffff38,0x10002,0xf3cb0};
     int touch_calibrate = 0;
     int rv;
 
-    printf("INIT:%d\n", wakeup_cause & ~EOS_PWR_INIT);
+    printf("INIT:%d\n", wakeup_cause);
 
     rv = eos_evtq_init(wakeup_cause);
     if (rv) printf("EVTQ INIT ERR:%d\n", rv);
@@ -67,16 +67,20 @@ void eos_init(void) {
         printf("TOUCH MATRIX:\n");
         printf("uint32_t touch_matrix[6] = {0x%x,0x%x,0x%x,0x%x,0x%x,0x%x}\n", touch_matrix[0], touch_matrix[1], touch_matrix[2], touch_matrix[3], touch_matrix[4], touch_matrix[5]);
     }
-    eos_start(wakeup_cause);
+    eos_run(wakeup_cause);
 }
 
-void eos_start(uint8_t wakeup_cause) {
+void eos_run(uint8_t wakeup_cause) {
+    int rv;
+
     eos_spi_select(EOS_SPI_DEV_EVE);
-    eve_start(wakeup_cause);
+    rv = eve_run(wakeup_cause);
     eos_spi_deselect();
+    if (rv) printf("EVE RUN ERR:%d\n", rv);
 
     eos_wifi_init();
     eos_cell_init();
     eos_sock_init();
-    eos_net_start(wakeup_cause);
+    rv = eos_net_run(wakeup_cause);
+    if (rv) printf("NET RUN ERR:%d\n", rv);
 }
diff --git a/fw/fe310/eos/eos.h b/fw/fe310/eos/eos.h
index 44570dc..6c061d5 100644
--- a/fw/fe310/eos/eos.h
+++ b/fw/fe310/eos/eos.h
@@ -12,4 +12,4 @@
 #define EOS_ERR_NET             -20
 
 void eos_init(void);
-void eos_start(uint8_t wakeup_cause);
\ No newline at end of file
+void eos_run(uint8_t wakeup_cause);
\ No newline at end of file
diff --git a/fw/fe310/eos/eve/eve.c b/fw/fe310/eos/eve/eve.c
index 8cca34b..ddfd4c0 100644
--- a/fw/fe310/eos/eve/eve.c
+++ b/fw/fe310/eos/eve/eve.c
@@ -375,8 +375,25 @@ static int _init(uint8_t gpio_dir) {
     return EVE_OK;
 }
 
+static void _start(void) {
+    uint16_t gpiox;
+
+    gpiox = eve_read16(REG_GPIOX) | 0x8000;
+    eve_write16(REG_GPIOX, gpiox);          /* enable the DISP signal to the LCD panel, it is set to output in REG_GPIOX_DIR by default */
+    eve_write8(REG_PCLK, EVE_PCLK);         /* now start clocking data to the LCD panel */
+}
+
+static void _stop(void) {
+    uint16_t gpiox;
+
+    gpiox = eve_read16(REG_GPIOX) & ~0x8000;
+    eve_write16(REG_GPIOX, gpiox);
+    eve_write8(REG_PCLK, 0);
+}
+
 int eve_init(uint8_t wakeup_cause, int touch_calibrate, uint32_t *touch_matrix, uint8_t gpio_dir) {
-    int rst = (wakeup_cause == EVE_INIT_RST);
+    int rv;
+    int rst = (wakeup_cause == EVE_WAKE_RST);
 
     if (rst) {
         int rv = _init(gpio_dir);
@@ -386,30 +403,33 @@ int eve_init(uint8_t wakeup_cause, int touch_calibrate, uint32_t *touch_matrix,
         eve_time_sleep(40);
     }
 
-    eve_touch_init(wakeup_cause, touch_calibrate, touch_matrix);
+    rv = eve_touch_init(wakeup_cause, touch_calibrate, touch_matrix);
+    if (rv) return rv;
+
     eve_platform_init();
 
     return EVE_OK;
 }
 
-void eve_start(uint8_t wakeup_cause) {
-    uint16_t gpiox;
+int eve_run(uint8_t wakeup_cause) {
+    int rv;
 
-    gpiox = eve_read16(REG_GPIOX) | 0x8000;
-    eve_write16(REG_GPIOX, gpiox);          /* enable the DISP signal to the LCD panel, it is set to output in REG_GPIOX_DIR by default */
-    eve_write8(REG_PCLK, EVE_PCLK);         /* now start clocking data to the LCD panel */
+    _start();
+    rv = eve_touch_run(wakeup_cause);
 
-    eve_touch_start(wakeup_cause);
+    return rv;
+}
+
+void eve_start(void) {
+    _start();
+    eve_touch_start();
 }
 
 void eve_stop(void) {
     uint16_t gpiox;
 
     eve_touch_stop();
-
-    gpiox = eve_read16(REG_GPIOX) & ~0x8000;
-    eve_write16(REG_GPIOX, gpiox);
-    eve_write8(REG_PCLK, 0);
+    _stop();
 }
 
 int eve_gpio_get(int gpio) {
@@ -469,7 +489,7 @@ void eve_active(void) {
 
     if (power_state == EVE_PSTATE_SLEEP) {
         eve_time_sleep(40);
-        eve_start(0);
+        eve_start();
     }
 
     power_state = EVE_PSTATE_ACTIVE;
diff --git a/fw/fe310/eos/eve/eve.h b/fw/fe310/eos/eve/eve.h
index 363905c..ae0a672 100644
--- a/fw/fe310/eos/eve/eve.h
+++ b/fw/fe310/eos/eve/eve.h
@@ -57,7 +57,8 @@ void eve_cmd_burst_start(void);
 void eve_cmd_burst_end(void);
 
 int eve_init(uint8_t wakeup_cause, int touch_calibrate, uint32_t *touch_matrix, uint8_t gpio_dir);
-void eve_start(uint8_t wakeup_cause);
+int eve_run(uint8_t wakeup_cause);
+void eve_start(void);
 void eve_stop(void);
 
 int eve_gpio_get(int gpio);
diff --git a/fw/fe310/eos/eve/eve_config.h b/fw/fe310/eos/eve/eve_config.h
index b151c63..1126277 100755
--- a/fw/fe310/eos/eve/eve_config.h
+++ b/fw/fe310/eos/eve/eve_config.h
@@ -3,19 +3,20 @@
 
 /* FocusLCDs E50RG84885LWAM520-CA */
 
-#define EVE_HLPW    20      /* horizontal low pulse width */
-#define EVE_HBP     60      /* horizontal back porch */
-#define EVE_HFP     40      /* horizontal front porch */
+#define EVE_HLPW    6       /* horizontal low pulse width */
+#define EVE_HBP     6       /* horizontal back porch */
+#define EVE_HFP     6       /* horizontal front porch */
 #define EVE_HACT    480     /* horizontal active pixels */
 #define EVE_HTOT    (EVE_HLPW + EVE_HBP + EVE_HFP + EVE_HACT + 10)
 
 
-#define EVE_VLPW    26      /* vertical low pulse width */
-#define EVE_VBP     50      /* vertical back porch */
-#define EVE_VFP     30      /* vertical front porch */
+#define EVE_VLPW    6       /* vertical low pulse width */
+#define EVE_VBP     6       /* vertical back porch */
+#define EVE_VFP     6       /* vertical front porch */
 #define EVE_VACT    854     /* vertical active pixels */
 #define EVE_VTOT    (EVE_VLPW + EVE_VBP + EVE_VFP + EVE_VACT + 10)
 
+
 #define EVE_HCYCLE          (EVE_HTOT)                      /* Th Total length of line (visible and non-visible) (in PCLKs) */
 #define EVE_HSIZE           (EVE_HACT)                      /* Length of visible part of line (in PCLKs) - display width */
 #define EVE_HOFFSET         (EVE_HFP + EVE_HLPW + EVE_HBP)  /* Length of non-visible part of line (in PCLK cycles) */
@@ -32,7 +33,8 @@
 #define EVE_SWIZZLE         0                               /* Defines the arrangement of the RGB pins */
 #define EVE_CSPREAD         0                               /* helps with noise, when set to 1 fewer signals are changed simultaneously, reset-default: 1 */
 
-#define EVE_PCLK            2                               /* 36 MHz */
+#define EVE_PCLK            1                               /* Clock from EVE_PCLK_FREQ */
+#define EVE_PCLK_FREQ       27000000
 
 #define EVE_GEN             4
 
diff --git a/fw/fe310/eos/eve/eve_platform.h b/fw/fe310/eos/eve/eve_platform.h
index 587e1cf..b33bd48 100644
--- a/fw/fe310/eos/eve/eve_platform.h
+++ b/fw/fe310/eos/eve/eve_platform.h
@@ -12,9 +12,9 @@
 #define EVE_SPI_FLAG_BSWAP  EOS_SPI_FLAG_BSWAP
 #define EVE_SPI_FLAG_TX     EOS_SPI_FLAG_TX
 
-#define EVE_INIT_RST        EOS_INIT_RST
-#define EVE_INIT_RTC        EOS_INIT_RTC
-#define EVE_INIT_BTN        EOS_INIT_BTN
+#define EVE_WAKE_RST        EOS_PWR_WAKE_RST
+#define EVE_WAKE_RTC        EOS_PWR_WAKE_RTC
+#define EVE_WAKE_BTN        EOS_PWR_WAKE_BTN
 
 void *eve_malloc(size_t);
 void eve_free(void *);
diff --git a/fw/fe310/eos/eve/eve_touch.c b/fw/fe310/eos/eve/eve_touch.c
index fc9d040..ff155d5 100644
--- a/fw/fe310/eos/eve/eve_touch.c
+++ b/fw/fe310/eos/eve/eve_touch.c
@@ -336,8 +336,16 @@ static void _init(int touch_calibrate, uint32_t *touch_matrix) {
     while(eve_read8(REG_INT_FLAGS));
 }
 
-void eve_touch_init(uint8_t wakeup_cause, int touch_calibrate, uint32_t *touch_matrix) {
-    int rst = (wakeup_cause == EVE_INIT_RST);
+static void _start(void) {
+    eve_write8(REG_TOUCH_MODE, EVE_TMODE_CONTINUOUS);
+}
+
+static void _stop(void) {
+    eve_write8(REG_TOUCH_MODE, EVE_TMODE_OFF);
+}
+
+int eve_touch_init(uint8_t wakeup_cause, int touch_calibrate, uint32_t *touch_matrix) {
+    int rst = (wakeup_cause == EVE_WAKE_RST);
     int i;
 
     eve_vtrack_init();
@@ -348,14 +356,22 @@ void eve_touch_init(uint8_t wakeup_cause, int touch_calibrate, uint32_t *touch_m
     }
 
     if (rst) _init(touch_calibrate, touch_matrix);
+
+    return EVE_OK;
 }
 
-void eve_touch_start(uint8_t wakeup_cause) {
-    eve_write8(REG_TOUCH_MODE, EVE_TMODE_CONTINUOUS);
+int eve_touch_run(uint8_t wakeup_cause) {
+    _start();
+
+    return EVE_OK;
+}
+
+void eve_touch_start(void) {
+    _start();
 }
 
 void eve_touch_stop(void) {
-    eve_write8(REG_TOUCH_MODE, EVE_TMODE_OFF);
+    _stop();
 }
 
 void eve_touch_set_handler(eve_touch_handler_t handler, void *param) {
diff --git a/fw/fe310/eos/eve/eve_touch.h b/fw/fe310/eos/eve/eve_touch.h
index b6ca6d9..37c4013 100644
--- a/fw/fe310/eos/eve/eve_touch.h
+++ b/fw/fe310/eos/eve/eve_touch.h
@@ -101,8 +101,9 @@ typedef void (*eve_touch_handler_t) (EVETouch *, uint16_t, uint8_t, void *);
 void eve_handle_touch(void);
 void eve_handle_time(void);
 
-void eve_touch_init(uint8_t wakeup_cause, int touch_calibrate, uint32_t *touch_matrix);
-void eve_touch_start(uint8_t wakeup_cause);
+int eve_touch_init(uint8_t wakeup_cause, int touch_calibrate, uint32_t *touch_matrix);
+int eve_touch_run(uint8_t wakeup_cause);
+void eve_touch_start(void);
 void eve_touch_stop(void);
 
 void eve_touch_set_handler(eve_touch_handler_t handler, void *handler_param);
diff --git a/fw/fe310/eos/i2c/bq25895.c b/fw/fe310/eos/i2c/bq25895.c
index b619fe3..ab2268b 100644
--- a/fw/fe310/eos/i2c/bq25895.c
+++ b/fw/fe310/eos/i2c/bq25895.c
@@ -8,7 +8,7 @@
 #include "i2c/bq25895.h"
 
 int eos_bq25895_init(uint8_t wakeup_cause) {
-    int rst = (wakeup_cause == EOS_INIT_RST);
+    int rst = (wakeup_cause == EOS_PWR_WAKE_RST);
     int i, rv = EOS_OK;
     uint8_t data = 0;
 
diff --git a/fw/fe310/eos/lcd.c b/fw/fe310/eos/lcd.c
index 8a77651..86dd1e9 100644
--- a/fw/fe310/eos/lcd.c
+++ b/fw/fe310/eos/lcd.c
@@ -41,7 +41,7 @@ int eos_lcd_select(void) {
 void eos_lcd_deselect(void) {
     GPIO_REG(GPIO_OUTPUT_VAL) |= (1 << IOF_SPI1_MOSI);
     GPIO_REG(GPIO_IOF_EN) |= SPI_IOF_MASK;
-    eos_net_start(0);
+    eos_net_start();
 }
 
 void eos_lcd_cs_set(void) {
@@ -100,7 +100,7 @@ static int _init(void) {
     eos_lcd_write(1, 0x04);
     eos_lcd_write(1, 0x01);
 
-    // eos_lcd_write(0, 0x08); //Output SDA
+    // eos_lcd_write(0, 0x08); // Output SDA
     // eos_lcd_write(1, 0x10);
 
     eos_lcd_write(0, 0x20);    // set DE/VSYNC mode
diff --git a/fw/fe310/eos/net.c b/fw/fe310/eos/net.c
index dffbc31..167a879 100644
--- a/fw/fe310/eos/net.c
+++ b/fw/fe310/eos/net.c
@@ -305,6 +305,12 @@ static void net_resume(void) {
     }
 }
 
+static void net_start(void) {
+    eos_intr_set_handler(INT_SPI1_BASE, net_handle_xchg);
+    SPI1_REG(SPI_REG_SCKDIV) = eos_spi_div(EOS_SPI_DEV_NET);
+    SPI1_REG(SPI_REG_CSID) = eos_spi_csid(EOS_SPI_DEV_NET);
+}
+
 int eos_net_init(uint8_t wakeup_cause) {
     int i;
 
@@ -346,14 +352,12 @@ int eos_net_init(uint8_t wakeup_cause) {
     return EOS_OK;
 }
 
-void eos_net_start(uint8_t wakeup_cause) {
-    eos_intr_set_handler(INT_SPI1_BASE, net_handle_xchg);
-    SPI1_REG(SPI_REG_SCKDIV) = eos_spi_div(EOS_SPI_DEV_NET);
-    SPI1_REG(SPI_REG_CSID) = eos_spi_csid(EOS_SPI_DEV_NET);
+int eos_net_run(uint8_t wakeup_cause) {
+    net_start();
 
     clear_csr(mstatus, MSTATUS_MIE);
-    if ((wakeup_cause & EOS_PWR_INIT) && (wakeup_cause != EOS_INIT_RST)) {
-        if (wakeup_cause != EOS_INIT_BTN) {
+    if (wakeup_cause != EOS_PWR_WAKE_RST) {
+        if (wakeup_cause != EOS_PWR_WAKE_BTN) {
             net_xchg_wake();
         }
         if (!(net_state_flags & NET_STATE_FLAG_CTS)) {
@@ -366,6 +370,16 @@ void eos_net_start(uint8_t wakeup_cause) {
     }
     net_resume();
     set_csr(mstatus, MSTATUS_MIE);
+
+    return EOS_OK;
+}
+
+void eos_net_start(void) {
+    net_start();
+
+    clear_csr(mstatus, MSTATUS_MIE);
+    net_resume();
+    set_csr(mstatus, MSTATUS_MIE);
 }
 
 void eos_net_stop(void) {
diff --git a/fw/fe310/eos/net.h b/fw/fe310/eos/net.h
index 6cb4cf0..86496bb 100644
--- a/fw/fe310/eos/net.h
+++ b/fw/fe310/eos/net.h
@@ -23,7 +23,8 @@
 #define EOS_NET_SIZE_BUFQ           2
 
 int eos_net_init(uint8_t wakeup_cause);
-void eos_net_start(uint8_t wakeup_cause);
+int eos_net_run(uint8_t wakeup_cause);
+void eos_net_start(void);
 void eos_net_stop(void);
 int eos_net_sleep(uint32_t timeout);
 
diff --git a/fw/fe310/eos/power.h b/fw/fe310/eos/power.h
index 241ee07..76b57a8 100644
--- a/fw/fe310/eos/power.h
+++ b/fw/fe310/eos/power.h
@@ -8,16 +8,11 @@
 #define EOS_PWR_WAKE_RST        0
 #define EOS_PWR_WAKE_RTC        1
 #define EOS_PWR_WAKE_BTN        2
-#define EOS_PWR_INIT            0x80
 
 #define EOS_PWR_RST_PWRON       0
 #define EOS_PWR_RST_EXT         1
 #define EOS_PWR_RST_WDOG        2
 
-#define EOS_INIT_RST            (EOS_PWR_WAKE_RST | EOS_PWR_INIT)
-#define EOS_INIT_RTC            (EOS_PWR_WAKE_RTC | EOS_PWR_INIT)
-#define EOS_INIT_BTN            (EOS_PWR_WAKE_BTN | EOS_PWR_INIT)
-
 int eos_power_init(uint8_t wakeup_cause);
 uint8_t eos_power_wakeup_cause(void);
 uint8_t eos_power_reset_cause(void);
diff --git a/fw/fe310/eos/spi_dev.c b/fw/fe310/eos/spi_dev.c
index b060bad..54b337a 100644
--- a/fw/fe310/eos/spi_dev.c
+++ b/fw/fe310/eos/spi_dev.c
@@ -62,7 +62,7 @@ int eos_spi_deselect(void) {
     eos_spi_stop();
 
     spi_dev = EOS_SPI_DEV_NET;
-    eos_net_start(0);
+    eos_net_start();
 
     return EOS_OK;
 }
diff --git a/fw/fe310/test/main.c b/fw/fe310/test/main.c
index 9cab6f7..f7c85e5 100644
--- a/fw/fe310/test/main.c
+++ b/fw/fe310/test/main.c
@@ -82,7 +82,7 @@ void app_home_page(EVEWindow *window, EVEViewStack *stack) {
 
 int main() {
     uint8_t wakeup_cause = eos_power_wakeup_cause();
-    int rst = (wakeup_cause == EVE_INIT_RST);
+    int rst = (wakeup_cause == EOS_PWR_WAKE_RST);
 
     printf("\nREADY.\n");
     printf("FREQ:%lu\n", PRCI_get_cpu_freq());
-- 
cgit v1.2.3