diff options
author | Uros Majstorovic <majstor@majstor.org> | 2020-05-17 04:03:23 +0200 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2020-05-17 04:03:23 +0200 |
commit | 75e57273a7ffded0ddcd3698ba68603c4be8a4cd (patch) | |
tree | 8b882cb41073ceadcdc5c27534a2dcc222adfb93 /code | |
parent | 5d157b4f7ecea4793c9da5c33a890d4ea4afc545 (diff) |
power management implemented; net protocol change
Diffstat (limited to 'code')
-rw-r--r-- | code/fe310/eos/Makefile | 2 | ||||
-rw-r--r-- | code/fe310/eos/cell.c | 19 | ||||
-rw-r--r-- | code/fe310/eos/cell.h | 15 | ||||
-rw-r--r-- | code/fe310/eos/eos.c | 15 | ||||
-rw-r--r-- | code/fe310/eos/eos.h | 5 | ||||
-rw-r--r-- | code/fe310/eos/eve/eve.c | 209 | ||||
-rw-r--r-- | code/fe310/eos/eve/eve.h | 13 | ||||
-rwxr-xr-x | code/fe310/eos/eve/eve_config.h | 78 | ||||
-rw-r--r-- | code/fe310/eos/eve/eve_platform.c | 2 | ||||
-rw-r--r-- | code/fe310/eos/eve/eve_platform.h | 2 | ||||
-rw-r--r-- | code/fe310/eos/event.c | 55 | ||||
-rw-r--r-- | code/fe310/eos/event.h | 8 | ||||
-rw-r--r-- | code/fe310/eos/msgq.c | 7 | ||||
-rw-r--r-- | code/fe310/eos/msgq.h | 2 | ||||
-rw-r--r-- | code/fe310/eos/net.c | 183 | ||||
-rw-r--r-- | code/fe310/eos/net.h | 27 | ||||
-rw-r--r-- | code/fe310/eos/net_def.h | 2 | ||||
-rw-r--r-- | code/fe310/eos/sock.c | 18 | ||||
-rw-r--r-- | code/fe310/eos/spi.c | 6 | ||||
-rw-r--r-- | code/fe310/eos/wifi.c | 13 |
20 files changed, 455 insertions, 226 deletions
diff --git a/code/fe310/eos/Makefile b/code/fe310/eos/Makefile index a49f942..48b5319 100644 --- a/code/fe310/eos/Makefile +++ b/code/fe310/eos/Makefile @@ -2,7 +2,7 @@ include ../common.mk CFLAGS += -I../include -I../drivers -obj = trap_entry.o eos.o msgq.o event.o interrupt.o timer.o i2s.o uart.o spi.o spi_dev.o net.o wifi.o cell.o sock.o +obj = trap_entry.o eos.o msgq.o event.o interrupt.o timer.o power.o i2s.o uart.o spi.o spi_dev.o net.o wifi.o cell.o sock.o %.o: %.c %.h diff --git a/code/fe310/eos/cell.c b/code/fe310/eos/cell.c index d75e1b9..3ce81f9 100644 --- a/code/fe310/eos/cell.c +++ b/code/fe310/eos/cell.c @@ -9,13 +9,10 @@ #include "cell.h" static eos_evt_handler_t evt_handler[EOS_CELL_MAX_MTYPE]; -static uint16_t evt_handler_flags_buf_free = 0; -static uint16_t evt_handler_flags_buf_acq = 0; static void cell_handle_evt(unsigned char type, unsigned char *buffer, uint16_t len) { if ((buffer == NULL) || (len < 1)) { - eos_evtq_bad_handler(type, buffer, len); - eos_net_free(buffer, 0); + eos_net_bad_handler(type, buffer, len); return; } @@ -23,22 +20,26 @@ static void cell_handle_evt(unsigned char type, unsigned char *buffer, uint16_t if (mtype < EOS_CELL_MAX_MTYPE) { evt_handler[mtype](type, buffer, len); } else { - eos_evtq_bad_handler(type, buffer, len); - eos_net_free(buffer, 0); - return; + eos_net_bad_handler(type, buffer, len); } } +static void cell_handle_rdy(unsigned char type, unsigned char *buffer, uint16_t len) { + // Do nothing + eos_net_free(buffer, 0); +} + void eos_cell_init(void) { int i; for (i=0; i<EOS_CELL_MAX_MTYPE; i++) { - evt_handler[i] = eos_evtq_bad_handler; + evt_handler[i] = eos_net_bad_handler; } eos_net_set_handler(EOS_NET_MTYPE_CELL, cell_handle_evt); + eos_cell_set_handler(EOS_CELL_MTYPE_READY, cell_handle_rdy); } void eos_cell_set_handler(unsigned char mtype, eos_evt_handler_t handler) { - if (handler == NULL) handler = eos_evtq_bad_handler; + if (handler == NULL) handler = eos_net_bad_handler; if (mtype < EOS_CELL_MAX_MTYPE) evt_handler[mtype] = handler; } diff --git a/code/fe310/eos/cell.h b/code/fe310/eos/cell.h index 2103c72..7691ff6 100644 --- a/code/fe310/eos/cell.h +++ b/code/fe310/eos/cell.h @@ -1,16 +1,17 @@ #include <stdint.h> #include "event.h" -#define EOS_CELL_MTYPE_DATA 0 -#define EOS_CELL_MTYPE_AUDIO 1 +#define EOS_CELL_MTYPE_READY 0 +#define EOS_CELL_MTYPE_DATA 1 +#define EOS_CELL_MTYPE_AUDIO 2 -#define EOS_CELL_MTYPE_DATA_START 2 -#define EOS_CELL_MTYPE_DATA_STOP 3 +#define EOS_CELL_MTYPE_DATA_START 4 +#define EOS_CELL_MTYPE_DATA_STOP 5 -#define EOS_CELL_MTYPE_AUDIO_START 4 -#define EOS_CELL_MTYPE_AUDIO_STOP 5 +#define EOS_CELL_MTYPE_AUDIO_START 6 +#define EOS_CELL_MTYPE_AUDIO_STOP 7 -#define EOS_CELL_MAX_MTYPE 2 +#define EOS_CELL_MAX_MTYPE 4 void eos_cell_init(void); void eos_cell_set_handler(unsigned char mtype, eos_evt_handler_t handler);
\ No newline at end of file diff --git a/code/fe310/eos/eos.c b/code/fe310/eos/eos.c index 40b680c..292e3c6 100644 --- a/code/fe310/eos/eos.c +++ b/code/fe310/eos/eos.c @@ -1,6 +1,7 @@ #include "event.h" #include "interrupt.h" #include "timer.h" +#include "power.h" #include "i2s.h" #include "uart.h" #include "spi.h" @@ -12,9 +13,14 @@ #include "eos.h" -uint32_t touch_transform[6] = {0xfa46,0xfffffcf6,0x422fe,0xffffff38,0x10002,0xf3cb0}; +uint32_t eve_touch[6] = {0xfa46,0xfffffcf6,0x422fe,0xffffff38,0x10002,0xf3cb0}; + +#include <stdio.h> void eos_init(void) { + uint8_t wake_src = eos_power_cause_wake(); + printf("WAKE SRC:%d\n", wake_src); + eos_evtq_init(); eos_intr_init(); eos_timer_init(); @@ -22,12 +28,17 @@ void eos_init(void) { eos_uart_init(); eos_spi_init(); eos_net_init(); + eos_power_init(); eos_wifi_init(); eos_cell_init(); eos_sock_init(); eos_spi_dev_init(); + int rv = eos_net_wake(wake_src); + printf("NET WAKE:%d\n", wake_src); + + eve_set_touch_calibration(eve_touch); eos_spi_dev_start(EOS_DEV_DISP); - eve_init(touch_transform); + eve_init(wake_src == EOS_PWR_WAKE_RESET); eos_spi_dev_stop(); } diff --git a/code/fe310/eos/eos.h b/code/fe310/eos/eos.h index 08bd10b..2a295f1 100644 --- a/code/fe310/eos/eos.h +++ b/code/fe310/eos/eos.h @@ -1,7 +1,12 @@ #define EOS_OK 0 #define EOS_ERR -1 +#define EOS_ERR_TIMEOUT -2 +#define EOS_ERR_BUSY -3 + #define EOS_ERR_FULL -10 #define EOS_ERR_EMPTY -11 +#define EOS_ERR_NOTFOUND -12 + #define EOS_ERR_NET -20 void eos_init(void); diff --git a/code/fe310/eos/eve/eve.c b/code/fe310/eos/eve/eve.c index 1a51891..47b07c9 100644 --- a/code/fe310/eos/eve/eve.c +++ b/code/fe310/eos/eve/eve.c @@ -9,6 +9,8 @@ static char _cmd_burst; static uint16_t _cmd_offset; static uint32_t _dl_addr; +static uint32_t *_touch_calib; +static uint8_t _brigtness; void eve_command(uint8_t command, uint8_t parameter) { eve_spi_cs_set(); @@ -67,16 +69,8 @@ void eve_write32(uint32_t addr, uint32_t data) { eve_spi_cs_clear(); } -void eve_active(void) { - eve_command(EVE_ACTIVE, 0); -} - -void eve_brightness(uint8_t b) { - eve_write8(REG_PWM_DUTY, b); -} - static void _dl_inc(uint32_t i) { - _dl_addr += i; + _dl_addr += i; } void eve_dl_start(uint32_t addr) { @@ -97,8 +91,8 @@ uint32_t eve_dl_get_addr(void) { } static void _cmd_inc(uint16_t i) { - _cmd_offset += i; - _cmd_offset &= 0x0fff; + _cmd_offset += i; + _cmd_offset &= 0x0fff; } static void _cmd_begin(uint32_t command) { @@ -107,7 +101,7 @@ static void _cmd_begin(uint32_t command) { if (_cmd_burst) { flags = EVE_SPI_FLAG_TX; } else { - uint32_t addr = EVE_RAM_CMD + _cmd_offset; + uint32_t addr = EVE_RAM_CMD + _cmd_offset; eve_spi_cs_set(); eve_spi_xchg24(addr | EVE_MEM_WRITE, 0); } @@ -177,17 +171,17 @@ void eve_cmd(uint32_t cmd, const char *fmt, ...) { } i++; } - va_end(argv); + va_end(argv); /* padding */ - i = _cmd_offset & 3; /* equivalent to _cmd_offset % 4 */ + i = _cmd_offset & 3; /* equivalent to _cmd_offset % 4 */ if (i) { - i = 4 - i; /* 3, 2 or 1 */ + i = 4 - i; /* 3, 2 or 1 */ _cmd_inc(i); - while (i > 0) { + while (i > 0) { eve_spi_xchg8(0, flags); - i--; - } + i--; + } } _cmd_end(); } @@ -202,14 +196,14 @@ void eve_cmd_dl(uint32_t dl) { } int eve_cmd_done(void) { - uint16_t r = eve_read16(REG_CMD_READ); + uint16_t r = eve_read16(REG_CMD_READ); if (r == 0xfff) { - _cmd_offset = 0; - eve_write8(REG_CPURESET, 1); - eve_write16(REG_CMD_READ, 0); - eve_write16(REG_CMD_WRITE, 0); - eve_write16(REG_CMD_DL, 0); - eve_write8(REG_CPURESET, 0); + _cmd_offset = 0; + eve_write8(REG_CPURESET, 1); + eve_write16(REG_CMD_READ, 0); + eve_write16(REG_CMD_WRITE, 0); + eve_write16(REG_CMD_DL, 0); + eve_write8(REG_CPURESET, 0); return -1; } return (r == _cmd_offset); @@ -228,7 +222,7 @@ int eve_cmd_exec(int w) { } void eve_cmd_burst_start(void) { - uint32_t addr = EVE_RAM_CMD + _cmd_offset; + uint32_t addr = EVE_RAM_CMD + _cmd_offset; eve_spi_cs_set(); eve_spi_xchg24(addr | EVE_MEM_WRITE, EVE_SPI_FLAG_TX); _cmd_burst = 1; @@ -240,69 +234,108 @@ void eve_cmd_burst_end(void) { _cmd_burst = 0; } -int eve_init(uint32_t *touch_transform) { - uint8_t chipid = 0; - uint16_t timeout = 0; +void eve_active(void){ + eve_command(EVE_ACTIVE, 0); +} + +void eve_standby(void) { + eve_command(EVE_STANDBY, 0); +} + +void eve_sleep(void) { + _brigtness = eve_read8(REG_PWM_DUTY); + eve_write8(REG_PWM_DUTY, 0x0); + + eve_write8(REG_CTOUCH_EXTENDED, 0x01); + eve_write8(REG_TOUCH_MODE, EVE_TMODE_OFF); + + eve_time_sleep(500); + eve_command(EVE_SLEEP, 0); +} + +void eve_wake(void) { + eve_command(EVE_ACTIVE, 0); /* start EVE */ + eve_time_sleep(40); + + eve_write8(REG_TOUCH_MODE, EVE_TMODE_CONTINUOUS); + eve_write8(REG_CTOUCH_EXTENDED, 0x00); + + eve_write8(REG_PWM_DUTY, _brigtness); +} + +void eve_brightness(uint8_t b) { + eve_write8(REG_PWM_DUTY, b); +} + +void eve_set_touch_calibration(uint32_t *matrix) { + _touch_calib = matrix; +} + +static int _init(void) { + uint8_t chipid = 0; + uint16_t timeout = 0; eve_command(EVE_RST_PULSE, 0); eve_command(EVE_CLKEXT, 0); eve_command(EVE_ACTIVE, 0); /* start EVE */ while (chipid != 0x7C) { /* if chipid is not 0x7c, continue to read it until it is, EVE needs a moment for it's power on self-test and configuration */ - eve_sleep(1); - chipid = eve_read8(REG_ID); - timeout++; - if (timeout > 400) return EVE_ERR; - } + eve_time_sleep(1); + chipid = eve_read8(REG_ID); + timeout++; + if (timeout > 400) return EVE_ERR; + } eve_write8(REG_PWM_DUTY, 0); + eve_write16(REG_GPIOX, 0); /* Initialize Display */ - eve_write16(REG_HCYCLE, EVE_HCYCLE); /* total number of clocks per line, incl front/back porch */ - eve_write16(REG_HOFFSET, EVE_HOFFSET); /* start of active line */ - eve_write16(REG_HSYNC0, EVE_HSYNC0); /* start of horizontal sync pulse */ - eve_write16(REG_HSYNC1, EVE_HSYNC1); /* end of horizontal sync pulse */ - eve_write16(REG_VCYCLE, EVE_VCYCLE); /* total number of lines per screen, including pre/post */ - eve_write16(REG_VOFFSET, EVE_VOFFSET); /* start of active screen */ - eve_write16(REG_VSYNC0, EVE_VSYNC0); /* start of vertical sync pulse */ - eve_write16(REG_VSYNC1, EVE_VSYNC1); /* end of vertical sync pulse */ - eve_write8(REG_SWIZZLE, EVE_SWIZZLE); /* FT8xx output to LCD - pin order */ - eve_write8(REG_PCLK_POL, EVE_PCLKPOL); /* LCD data is clocked in on this PCLK edge */ - eve_write8(REG_CSPREAD, EVE_CSPREAD); /* helps with noise, when set to 1 fewer signals are changed simultaneously, reset-default: 1 */ - eve_write16(REG_HSIZE, EVE_HSIZE); /* active display width */ - eve_write16(REG_VSIZE, EVE_VSIZE); /* active display height */ - - /* do not set PCLK yet - wait for just after the first display list */ - - /* configure Touch */ - eve_write8(REG_TOUCH_MODE, EVE_TMODE_CONTINUOUS); /* enable touch */ - eve_write16(REG_TOUCH_RZTHRESH, EVE_TOUCH_RZTHRESH); /* eliminate any false touches */ - - /* disable Audio for now */ - eve_write8(REG_VOL_PB, 0x00); /* turn recorded audio volume down */ - eve_write8(REG_VOL_SOUND, 0x00); /* turn synthesizer volume off */ - eve_write16(REG_SOUND, 0x6000); /* set synthesizer to mute */ - - /* write a basic display-list to get things started */ + eve_write16(REG_HCYCLE, EVE_HCYCLE); /* total number of clocks per line, incl front/back porch */ + eve_write16(REG_HOFFSET, EVE_HOFFSET); /* start of active line */ + eve_write16(REG_HSYNC0, EVE_HSYNC0); /* start of horizontal sync pulse */ + eve_write16(REG_HSYNC1, EVE_HSYNC1); /* end of horizontal sync pulse */ + eve_write16(REG_VCYCLE, EVE_VCYCLE); /* total number of lines per screen, including pre/post */ + eve_write16(REG_VOFFSET, EVE_VOFFSET); /* start of active screen */ + eve_write16(REG_VSYNC0, EVE_VSYNC0); /* start of vertical sync pulse */ + eve_write16(REG_VSYNC1, EVE_VSYNC1); /* end of vertical sync pulse */ + eve_write8(REG_SWIZZLE, EVE_SWIZZLE); /* FT8xx output to LCD - pin order */ + eve_write8(REG_PCLK_POL, EVE_PCLKPOL); /* LCD data is clocked in on this PCLK edge */ + eve_write8(REG_CSPREAD, EVE_CSPREAD); /* helps with noise, when set to 1 fewer signals are changed simultaneously, reset-default: 1 */ + eve_write16(REG_HSIZE, EVE_HSIZE); /* active display width */ + eve_write16(REG_VSIZE, EVE_VSIZE); /* active display height */ + + /* do not set PCLK yet - wait for just after the first display list */ + + /* disable Audio */ + eve_write8(REG_VOL_PB, 0x00); /* turn recorded audio volume down */ + eve_write8(REG_VOL_SOUND, 0x00); /* turn synthesizer volume off */ + eve_write16(REG_SOUND, 0x6000); /* set synthesizer to mute */ + + /* write a basic display-list to get things started */ eve_dl_start(EVE_RAM_DL); eve_dl_write(CLEAR_COLOR_RGB(0,0,0)); - eve_dl_write(CLEAR(1,1,1)); + eve_dl_write(CLEAR(1,1,1)); eve_dl_write(DISPLAY()); eve_dl_swap(); - /* nothing is being displayed yet... the pixel clock is still 0x00 */ - eve_write8(REG_GPIO, 0x80); /* enable the DISP signal to the LCD panel, it is set to output in REG_GPIO_DIR by default */ - eve_write8(REG_PCLK, EVE_PCLK); /* now start clocking data to the LCD panel */ - - if (touch_transform) { - eve_write32(REG_TOUCH_TRANSFORM_A, touch_transform[0]); - eve_write32(REG_TOUCH_TRANSFORM_B, touch_transform[1]); - eve_write32(REG_TOUCH_TRANSFORM_C, touch_transform[2]); - eve_write32(REG_TOUCH_TRANSFORM_D, touch_transform[3]); - eve_write32(REG_TOUCH_TRANSFORM_E, touch_transform[4]); - eve_write32(REG_TOUCH_TRANSFORM_F, touch_transform[5]); + /* nothing is being displayed yet... the pixel clock is still 0x00 */ + eve_write16(REG_GPIOX, 0x8000); /* 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 */ + + /* configure Touch */ + eve_write16(REG_TOUCH_CONFIG, 0xb81); /* enable touch low power mode: 0xb81 - default: 0x381 */ + eve_write8(REG_TOUCH_MODE, EVE_TMODE_CONTINUOUS); /* enable touch */ + eve_write16(REG_TOUCH_RZTHRESH, EVE_TOUCH_RZTHRESH); /* eliminate any false touches */ + + if (_touch_calib) { + eve_write32(REG_TOUCH_TRANSFORM_A, _touch_calib[0]); + eve_write32(REG_TOUCH_TRANSFORM_B, _touch_calib[1]); + eve_write32(REG_TOUCH_TRANSFORM_C, _touch_calib[2]); + eve_write32(REG_TOUCH_TRANSFORM_D, _touch_calib[3]); + eve_write32(REG_TOUCH_TRANSFORM_E, _touch_calib[4]); + eve_write32(REG_TOUCH_TRANSFORM_F, _touch_calib[5]); } else { - uint32_t touch_transform[6]; + uint32_t touch_calib[6]; eve_cmd_dl(CMD_DLSTART); eve_cmd_dl(CLEAR_COLOR_RGB(0,0,0)); eve_cmd_dl(CLEAR(1,1,1)); @@ -312,25 +345,37 @@ int eve_init(uint32_t *touch_transform) { eve_cmd_dl(CMD_SWAP); eve_cmd_exec(1); - touch_transform[0] = eve_read32(REG_TOUCH_TRANSFORM_A); - touch_transform[1] = eve_read32(REG_TOUCH_TRANSFORM_B); - touch_transform[2] = eve_read32(REG_TOUCH_TRANSFORM_C); - touch_transform[3] = eve_read32(REG_TOUCH_TRANSFORM_D); - touch_transform[4] = eve_read32(REG_TOUCH_TRANSFORM_E); - touch_transform[5] = eve_read32(REG_TOUCH_TRANSFORM_F); + touch_calib[0] = eve_read32(REG_TOUCH_TRANSFORM_A); + touch_calib[1] = eve_read32(REG_TOUCH_TRANSFORM_B); + touch_calib[2] = eve_read32(REG_TOUCH_TRANSFORM_C); + touch_calib[3] = eve_read32(REG_TOUCH_TRANSFORM_D); + touch_calib[4] = eve_read32(REG_TOUCH_TRANSFORM_E); + touch_calib[5] = eve_read32(REG_TOUCH_TRANSFORM_F); - printf("TOUCH TRANSFORM:{0x%x,0x%x,0x%x,0x%x,0x%x,0x%x}\n", touch_transform[0], touch_transform[1], touch_transform[2], touch_transform[3], touch_transform[4], touch_transform[5]); + printf("TOUCH TRANSFORM:{0x%x,0x%x,0x%x,0x%x,0x%x,0x%x}\n", touch_calib[0], touch_calib[1], touch_calib[2], touch_calib[3], touch_calib[4], touch_calib[5]); } - eve_write32(REG_CTOUCH_EXTENDED, 0x00); + eve_write8(REG_CTOUCH_EXTENDED, 0x00); + eve_cmd(CMD_SETROTATE, "w", 2); eve_cmd_exec(1); + return EVE_OK; +} + +int eve_init(int pwr_on) { + if (pwr_on) { + int rv = _init(); + if (rv) return rv; + } else { + eve_wake(); + } + eve_init_touch(); eve_init_track(); eve_init_platform(); - eve_sleep(500); + eve_time_sleep(500); eve_command(EVE_STANDBY, 0); return EVE_OK; diff --git a/code/fe310/eos/eve/eve.h b/code/fe310/eos/eve/eve.h index 38310f6..2fb2fb5 100644 --- a/code/fe310/eos/eve/eve.h +++ b/code/fe310/eos/eve/eve.h @@ -18,9 +18,6 @@ void eve_write8(uint32_t addr, uint8_t data); void eve_write16(uint32_t addr, uint16_t data); void eve_write32(uint32_t addr, uint32_t data); -void eve_active(void); -void eve_brightness(uint8_t b); - void eve_dl_start(uint32_t addr); void eve_dl_write(uint32_t dl); void eve_dl_swap(void); @@ -33,4 +30,12 @@ int eve_cmd_done(void); int eve_cmd_exec(int w); void eve_cmd_burst_start(void); void eve_cmd_burst_end(void); -int eve_init(uint32_t *touch_transform); + +void eve_active(void); +void eve_standby(void); +void eve_sleep(void); +void eve_wake(void); +void eve_brightness(uint8_t b); +void eve_set_touch_calibration(uint32_t *matrix); + +int eve_init(int pwr_on); diff --git a/code/fe310/eos/eve/eve_config.h b/code/fe310/eos/eve/eve_config.h index a7d3c42..882ea89 100755 --- a/code/fe310/eos/eve/eve_config.h +++ b/code/fe310/eos/eve/eve_config.h @@ -1,39 +1,39 @@ -#ifndef EVE_CONFIG_H_
-#define EVE_CONFIG_H_
-
-/* my display */
-
-#define EVE_TH 1200L
-#define EVE_THD 800L
-#define EVE_THF 210L
-#define EVE_THP 20L
-#define EVE_THB 46L
-
-#define EVE_TV 650L
-#define EVE_TVD 480L
-#define EVE_TVF 22L
-#define EVE_TVP 12L
-#define EVE_TVB 23L
-
-
-#define EVE_HSIZE (EVE_THD) /* Thd Length of visible part of line (in PCLKs) - display width */
-#define EVE_HSYNC0 (EVE_THF) /* Thf Horizontal Front Porch */
-#define EVE_HSYNC1 (EVE_THF + EVE_THP) /* Thf + Thp Horizontal Front Porch plus Hsync Pulse width */
-#define EVE_HOFFSET (EVE_THF + EVE_THP + EVE_THB) /* Thf + Thp + Thb Length of non-visible part of line (in PCLK cycles) */
-#define EVE_HCYCLE (EVE_TH) /* Th Total length of line (visible and non-visible) (in PCLKs) */
-
-#define EVE_VSIZE (EVE_TVD) /* Tvd Number of visible lines (in lines) - display height */
-#define EVE_VSYNC0 (EVE_TVF) /* Tvf Vertical Front Porch */
-#define EVE_VSYNC1 (EVE_TVF + EVE_TVP) /* Tvf + Tvp Vertical Front Porch plus Vsync Pulse width */
-#define EVE_VOFFSET (EVE_TVF + EVE_TVP + EVE_TVB) /* Tvf + Tvp + Tvb Number of non-visible lines (in lines) */
-#define EVE_VCYCLE (EVE_TV) /* Tv Total number of lines (visible and non-visible) (in lines) */
-
-#define EVE_PCLKPOL (1L) /* PCLK polarity (0 = rising edge, 1 = falling edge) */
-#define EVE_SWIZZLE (0L) /* Defines the arrangement of the RGB pins of the FT800 */
-#define EVE_PCLK (2L) /* 60MHz / REG_PCLK = PCLK frequency - 30 MHz */
-#define EVE_CSPREAD (1L) /* helps with noise, when set to 1 fewer signals are changed simultaneously, reset-default: 1 */
-#define EVE_TOUCH_RZTHRESH (1200L) /* touch-sensitivity */
-#define EVE_HAS_CRYSTAL
-#define FT81X_ENABLE
-
-#endif /* EVE_CONFIG_H */
+#ifndef EVE_CONFIG_H_ +#define EVE_CONFIG_H_ + +/* my display */ + +#define EVE_TH 1200L +#define EVE_THD 800L +#define EVE_THF 210L +#define EVE_THP 20L +#define EVE_THB 46L + +#define EVE_TV 650L +#define EVE_TVD 480L +#define EVE_TVF 22L +#define EVE_TVP 12L +#define EVE_TVB 23L + + +#define EVE_HSIZE (EVE_THD) /* Thd Length of visible part of line (in PCLKs) - display width */ +#define EVE_HSYNC0 (EVE_THF) /* Thf Horizontal Front Porch */ +#define EVE_HSYNC1 (EVE_THF + EVE_THP) /* Thf + Thp Horizontal Front Porch plus Hsync Pulse width */ +#define EVE_HOFFSET (EVE_THF + EVE_THP + EVE_THB) /* Thf + Thp + Thb Length of non-visible part of line (in PCLK cycles) */ +#define EVE_HCYCLE (EVE_TH) /* Th Total length of line (visible and non-visible) (in PCLKs) */ + +#define EVE_VSIZE (EVE_TVD) /* Tvd Number of visible lines (in lines) - display height */ +#define EVE_VSYNC0 (EVE_TVF) /* Tvf Vertical Front Porch */ +#define EVE_VSYNC1 (EVE_TVF + EVE_TVP) /* Tvf + Tvp Vertical Front Porch plus Vsync Pulse width */ +#define EVE_VOFFSET (EVE_TVF + EVE_TVP + EVE_TVB) /* Tvf + Tvp + Tvb Number of non-visible lines (in lines) */ +#define EVE_VCYCLE (EVE_TV) /* Tv Total number of lines (visible and non-visible) (in lines) */ + +#define EVE_PCLKPOL (1L) /* PCLK polarity (0 = rising edge, 1 = falling edge) */ +#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_HAS_CRYSTAL +#define FT81X_ENABLE + +#endif /* EVE_CONFIG_H */ diff --git a/code/fe310/eos/eve/eve_platform.c b/code/fe310/eos/eve/eve_platform.c index ab9157e..2c6c319 100644 --- a/code/fe310/eos/eve/eve_platform.c +++ b/code/fe310/eos/eve/eve_platform.c @@ -31,7 +31,7 @@ static void handle_intr(void) { return; } -void eve_sleep(uint32_t ms) { +void eve_time_sleep(uint32_t ms) { eos_time_sleep(ms); } diff --git a/code/fe310/eos/eve/eve_platform.h b/code/fe310/eos/eve/eve_platform.h index baa48cd..5d67020 100644 --- a/code/fe310/eos/eve/eve_platform.h +++ b/code/fe310/eos/eve/eve_platform.h @@ -19,7 +19,7 @@ #define eve_spi_xchg24 eos_spi_xchg24 #define eve_spi_xchg32 eos_spi_xchg32 -void eve_sleep(uint32_t ms); +void eve_time_sleep(uint32_t ms); void eve_timer_set(uint32_t ms); void eve_timer_clear(void); uint64_t eve_time_get_tick(void); diff --git a/code/fe310/eos/event.c b/code/fe310/eos/event.c index 8469101..6953dca 100644 --- a/code/fe310/eos/event.c +++ b/code/fe310/eos/event.c @@ -51,24 +51,11 @@ void eos_evtq_pop(unsigned char *type, unsigned char **buffer, uint16_t *len) { set_csr(mstatus, MSTATUS_MIE); } -void eos_evtq_bad_handler(unsigned char type, unsigned char *buffer, uint16_t len) { - printf("evt bad handler:%d\n", type); -} - -void eos_evtq_set_handler(unsigned char type, eos_evt_handler_t handler) { - unsigned char idx = (type & EOS_EVT_MASK) >> 4; - - if (idx <= EOS_EVT_MAX_EVT) evt_handler[idx] = handler; -} - -eos_evt_handler_t eos_evtq_get_handler(unsigned char type) { - unsigned char idx = (type & EOS_EVT_MASK) >> 4; - - if (idx <= EOS_EVT_MAX_EVT) return evt_handler[idx]; - return NULL; +void eos_evtq_pop_isr(unsigned char *type, unsigned char **buffer, uint16_t *len) { + eos_msgq_pop(&_eos_event_q, type, buffer, len); } -void eos_evtq_get(unsigned char type, unsigned char *selector, uint16_t sel_len, unsigned char **buffer, uint16_t *len) { +void eos_evtq_wait(unsigned char type, unsigned char *selector, uint16_t sel_len, unsigned char **buffer, uint16_t *len) { int rv = 0; while(!rv) { @@ -93,6 +80,27 @@ void eos_evtq_get(unsigned char type, unsigned char *selector, uint16_t sel_len, } } +void eos_evtq_flush(void) { + clear_csr(mstatus, MSTATUS_MIE); + eos_evtq_flush_isr(); + set_csr(mstatus, MSTATUS_MIE); +} + +void eos_evtq_flush_isr(void) { + unsigned char type; + unsigned char *buffer; + uint16_t len; + + do { + eos_msgq_pop(&_eos_event_q, &type, &buffer, &len); + if (type) { + set_csr(mstatus, MSTATUS_MIE); + evt_handler[0](type, buffer, len); + clear_csr(mstatus, MSTATUS_MIE); + } + } while (type); +} + void eos_evtq_loop(void) { unsigned char type; unsigned char *buffer; @@ -112,4 +120,19 @@ void eos_evtq_loop(void) { } } +void eos_evtq_bad_handler(unsigned char type, unsigned char *buffer, uint16_t len) { + printf("evt bad handler:0x%x\n", type); +} + +void eos_evtq_set_handler(unsigned char type, eos_evt_handler_t handler) { + unsigned char idx = (type & EOS_EVT_MASK) >> 4; + + if (idx <= EOS_EVT_MAX_EVT) evt_handler[idx] = handler; +} +eos_evt_handler_t eos_evtq_get_handler(unsigned char type) { + unsigned char idx = (type & EOS_EVT_MASK) >> 4; + + if (idx <= EOS_EVT_MAX_EVT) return evt_handler[idx]; + return NULL; +} diff --git a/code/fe310/eos/event.h b/code/fe310/eos/event.h index 1705389..ba906bb 100644 --- a/code/fe310/eos/event.h +++ b/code/fe310/eos/event.h @@ -8,9 +8,13 @@ void eos_evtq_init(void); int eos_evtq_push(unsigned char type, unsigned char *buffer, uint16_t len); int eos_evtq_push_isr(unsigned char type, unsigned char *buffer, uint16_t len); void eos_evtq_pop(unsigned char *type, unsigned char **buffer, uint16_t *len); +void eos_evtq_pop_isr(unsigned char *type, unsigned char **buffer, uint16_t *len); +void eos_evtq_wait(unsigned char type, unsigned char *selector, uint16_t sel_len, unsigned char **buffer, uint16_t *len); +void eos_evtq_flush(void); +void eos_evtq_flush_isr(void); +void eos_evtq_loop(void); + void eos_evtq_bad_handler(unsigned char type, unsigned char *buffer, uint16_t len); void eos_evtq_set_handler(unsigned char type, eos_evt_handler_t handler); eos_evt_handler_t eos_evtq_get_handler(unsigned char type); -void eos_evtq_get(unsigned char type, unsigned char *selector, uint16_t sel_len, unsigned char **buffer, uint16_t *len); -void eos_evtq_loop(void); diff --git a/code/fe310/eos/msgq.c b/code/fe310/eos/msgq.c index 07ea70f..e76090c 100644 --- a/code/fe310/eos/msgq.c +++ b/code/fe310/eos/msgq.c @@ -79,6 +79,10 @@ int eos_msgq_get(EOSMsgQ *msgq, unsigned char type, unsigned char *selector, uin return 0; } +uint8_t eos_msgq_len(EOSMsgQ *msgq) { + return (uint8_t)(msgq->idx_w - msgq->idx_r); +} + void eos_bufq_init(EOSBufQ *bufq, unsigned char **array, uint8_t size) { bufq->idx_r = 0; bufq->idx_w = 0; @@ -99,3 +103,6 @@ unsigned char *eos_bufq_pop(EOSBufQ *bufq) { return bufq->array[IDX_MASK(bufq->idx_r++, bufq->size)]; } +uint8_t eos_bufq_len(EOSBufQ *bufq) { + return (uint8_t)(bufq->idx_w - bufq->idx_r); +} diff --git a/code/fe310/eos/msgq.h b/code/fe310/eos/msgq.h index f8c05c4..9f2ca81 100644 --- a/code/fe310/eos/msgq.h +++ b/code/fe310/eos/msgq.h @@ -17,6 +17,7 @@ void eos_msgq_init(EOSMsgQ *msgq, EOSMsgItem *array, uint8_t size); int eos_msgq_push(EOSMsgQ *msgq, unsigned char type, unsigned char *buffer, uint16_t len); void eos_msgq_pop(EOSMsgQ *msgq, unsigned char *type, unsigned char **buffer, uint16_t *len); int eos_msgq_get(EOSMsgQ *msgq, unsigned char type, unsigned char *selector, uint16_t sel_len, unsigned char **buffer, uint16_t *len); +uint8_t eos_msgq_len(EOSMsgQ *msgq); typedef struct EOSBufQ { uint8_t idx_r; @@ -28,3 +29,4 @@ typedef struct EOSBufQ { void eos_bufq_init(EOSBufQ *bufq, unsigned char **array, uint8_t size); int eos_bufq_push(EOSBufQ *bufq, unsigned char *buffer); unsigned char *eos_bufq_pop(EOSBufQ *bufq); +uint8_t eos_bufq_len(EOSBufQ *bufq); diff --git a/code/fe310/eos/net.c b/code/fe310/eos/net.c index 47f49b6..297e8f0 100644 --- a/code/fe310/eos/net.c +++ b/code/fe310/eos/net.c @@ -6,8 +6,10 @@ #include "eos.h" #include "msgq.h" -#include "interrupt.h" #include "event.h" +#include "interrupt.h" +#include "timer.h" +#include "power.h" #include "spi.h" #include "spi_def.h" @@ -26,7 +28,7 @@ static unsigned char net_bufq_buffer[EOS_NET_SIZE_BUFQ][EOS_NET_SIZE_BUF]; static EOSMsgQ net_send_q; static EOSMsgItem net_sndq_array[EOS_NET_SIZE_BUFQ]; -static uint8_t net_state_flags = 0; +static volatile uint8_t net_state_flags = 0; static unsigned char net_state_type = 0; static uint32_t net_state_len_tx = 0; static uint32_t net_state_len_rx = 0; @@ -39,14 +41,51 @@ static eos_evt_handler_t net_handler[EOS_NET_MAX_MTYPE]; static uint16_t net_wrapper_acq[EOS_EVT_MAX_EVT]; static uint16_t net_flags_acq[EOS_EVT_MAX_EVT]; +static int net_xchg_sleep(void) { + int i; + int rv = EOS_OK; + volatile uint32_t x = 0; + + net_state_flags &= ~NET_STATE_FLAG_CTS; + + SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_HOLD; + + SPI1_REG(SPI_REG_TXFIFO) = 0xff; + while ((x = SPI1_REG(SPI_REG_RXFIFO)) & SPI_RXFIFO_EMPTY); + if (x & 0xFF) rv = EOS_ERR_BUSY; + + for (i=0; i<7; i++) { + while (SPI1_REG(SPI_REG_TXFIFO) & SPI_TXFIFO_FULL); + SPI1_REG(SPI_REG_TXFIFO) = 0; + while ((x = SPI1_REG(SPI_REG_RXFIFO)) & SPI_RXFIFO_EMPTY); + } + + SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_AUTO; + + return rv; +} + +static void net_xchg_wake(void) { + int i; + volatile uint32_t x = 0; + + SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_HOLD; + + for (i=0; i<8; i++) { + while (SPI1_REG(SPI_REG_TXFIFO) & SPI_TXFIFO_FULL); + SPI1_REG(SPI_REG_TXFIFO) = 0; + while ((x = SPI1_REG(SPI_REG_RXFIFO)) & SPI_RXFIFO_EMPTY); + } + + SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_AUTO; +} + static void net_xchg_reset(void) { net_state_flags &= ~NET_STATE_FLAG_CTS; net_state_flags |= (NET_STATE_FLAG_RST | NET_STATE_FLAG_XCHG); SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_HOLD; - SPI1_REG(SPI_REG_TXFIFO) = 0; - SPI1_REG(SPI_REG_RXCTRL) = SPI_RXWM(0); SPI1_REG(SPI_REG_IE) = SPI_IP_RXWM; } @@ -55,8 +94,8 @@ static void net_xchg_start(unsigned char type, unsigned char *buffer, uint16_t l net_state_flags &= ~NET_STATE_FLAG_CTS; net_state_flags |= (NET_STATE_FLAG_INIT | NET_STATE_FLAG_XCHG); - if (net_state_next_cnt && (net_state_next_buf == NULL)) type |= NET_MTYPE_FLAG_ONEW; - if (type & NET_MTYPE_FLAG_ONEW) net_state_flags |= NET_STATE_FLAG_ONEW; + if (net_state_next_cnt && (net_state_next_buf == NULL)) type |= EOS_NET_MTYPE_FLAG_ONEW; + if (type & EOS_NET_MTYPE_FLAG_ONEW) net_state_flags |= NET_STATE_FLAG_ONEW; net_state_type = type; net_state_len_tx = len; @@ -64,11 +103,10 @@ static void net_xchg_start(unsigned char type, unsigned char *buffer, uint16_t l net_state_buf = buffer; SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_HOLD; - - SPI1_REG(SPI_REG_TXFIFO) = ((type << 3) | (len >> 8)) & 0xFF; - SPI1_REG(SPI_REG_TXFIFO) = len & 0xFF; - - SPI1_REG(SPI_REG_RXCTRL) = SPI_RXWM(1); + SPI1_REG(SPI_REG_TXFIFO) = type; + SPI1_REG(SPI_REG_TXFIFO) = (len >> 8) & 0xFF; + SPI1_REG(SPI_REG_TXFIFO) = (len & 0xFF); + SPI1_REG(SPI_REG_RXCTRL) = SPI_RXWM(2); SPI1_REG(SPI_REG_IE) = SPI_IP_RXWM; } @@ -91,7 +129,7 @@ static int net_xchg_next(unsigned char *_buffer) { } static void net_handle_xchg(void) { - volatile uint32_t r1, r2; + volatile uint32_t r1, r2, r3; uint32_t len; if (net_state_flags & NET_STATE_FLAG_RST) { @@ -107,22 +145,24 @@ static void net_handle_xchg(void) { r1 = SPI1_REG(SPI_REG_RXFIFO); r2 = SPI1_REG(SPI_REG_RXFIFO); + r3 = SPI1_REG(SPI_REG_RXFIFO); if (net_state_flags & NET_STATE_FLAG_ONEW) { r1 = 0; r2 = 0; + r3 = 0; } - net_state_type = ((r1 & 0xFF) >> 3); - net_state_len_rx = ((r1 & 0x07) << 8); - net_state_len_rx |= (r2 & 0xFF); + net_state_type = (r1 & 0xFF); + net_state_len_rx = (r2 & 0xFF) << 8; + net_state_len_rx |= (r3 & 0xFF); len = MAX(net_state_len_tx, net_state_len_rx); // esp32 bug workaraund - if (len < 6) { - len = 6; - } else if ((len + 2) % 4 != 0) { - len = ((len + 2)/4 + 1) * 4 - 2; + if (len < 5) { + len = 5; + } else if ((len + 3) % 4 != 0) { + len = ((len + 3)/4 + 1) * 4 - 3; } if (len > EOS_NET_SIZE_BUF) { @@ -180,8 +220,7 @@ static void net_handle_evt(unsigned char type, unsigned char *buffer, uint16_t l if (idx < EOS_NET_MAX_MTYPE) { net_handler[idx](type, buffer, len); } else { - eos_evtq_bad_handler(type, buffer, len); - eos_net_free(buffer, 0); + eos_net_bad_handler(type, buffer, len); } } @@ -215,6 +254,7 @@ static void evt_handler_wrapper(unsigned char type, unsigned char *buffer, uint1 ok = net_acquire(net_wrapper_acq[idx] & flag); if (ok) { eos_evtq_get_handler(type)(type, buffer, len); + eos_net_release(); net_wrapper_acq[idx] &= ~flag; } else { net_wrapper_acq[idx] |= flag; @@ -247,7 +287,7 @@ void eos_net_init(void) { } for (i=0; i<EOS_NET_MAX_MTYPE; i++) { - net_handler[i] = eos_evtq_bad_handler; + net_handler[i] = eos_net_bad_handler; } eos_evtq_set_handler(0, evt_handler); eos_evtq_set_handler(EOS_EVT_NET, net_handle_evt); @@ -275,30 +315,117 @@ void eos_net_start(void) { SPI1_REG(SPI_REG_SCKDIV) = NET_SPI_DIV; SPI1_REG(SPI_REG_CSID) = NET_SPI_CSID; + clear_csr(mstatus, MSTATUS_MIE); net_state_flags |= NET_STATE_FLAG_RUN; if (net_state_flags & NET_STATE_FLAG_CTS) { - clear_csr(mstatus, MSTATUS_MIE); net_xchg_next(NULL); - set_csr(mstatus, MSTATUS_MIE); } + set_csr(mstatus, MSTATUS_MIE); } void eos_net_stop(void) { uint8_t done = 0; - if (!(net_state_flags & NET_STATE_FLAG_RUN)) return; + clear_csr(mstatus, MSTATUS_MIE); + if (net_state_flags & NET_STATE_FLAG_RUN) { + net_state_flags &= ~NET_STATE_FLAG_RUN; + done = !(net_state_flags & NET_STATE_FLAG_XCHG); + } else { + done = 1; + } + set_csr(mstatus, MSTATUS_MIE); while (!done) { clear_csr(mstatus, MSTATUS_MIE); - net_state_flags &= ~NET_STATE_FLAG_RUN; done = !(net_state_flags & NET_STATE_FLAG_XCHG); if (!done) asm volatile ("wfi"); set_csr(mstatus, MSTATUS_MIE); } } +int eos_net_sleep(uint32_t timeout) { + volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME); + uint64_t then_ms = timeout + *mtime * 1000 / EOS_TIMER_RTC_FREQ; + uint8_t done = 0; + int rv = EOS_OK; + + clear_csr(mstatus, MSTATUS_MIE); + if (!(net_state_flags & NET_STATE_FLAG_RUN)) rv = EOS_ERR; + set_csr(mstatus, MSTATUS_MIE); + + if (rv) return rv; + + do { + if (*mtime * 1000 / EOS_TIMER_RTC_FREQ > then_ms) return EOS_ERR_TIMEOUT; + clear_csr(mstatus, MSTATUS_MIE); + eos_evtq_flush_isr(); + done = (eos_msgq_len(&net_send_q) == 0); + done = done && (!(net_state_flags & NET_STATE_FLAG_RTS) && (net_state_flags & NET_STATE_FLAG_CTS)); + if (done) done = (net_xchg_sleep() == EOS_OK); + if (!done) { + asm volatile ("wfi"); + set_csr(mstatus, MSTATUS_MIE); + } + } while (!done); + + while (!(GPIO_REG(GPIO_RISE_IP) & (1 << NET_PIN_CTS))) { + if (*mtime * 1000 / EOS_TIMER_RTC_FREQ > then_ms) { + rv = EOS_ERR_TIMEOUT; + break; + } + asm volatile ("wfi"); + } + + if (!rv) { + GPIO_REG(GPIO_RISE_IP) = (1 << NET_PIN_CTS); + net_state_flags &= ~NET_STATE_FLAG_RUN; + } + + set_csr(mstatus, MSTATUS_MIE); + + return rv; +} + +int eos_net_wake(uint8_t source) { + int rv = EOS_OK; + + clear_csr(mstatus, MSTATUS_MIE); + if (net_state_flags & NET_STATE_FLAG_RUN) rv = EOS_ERR; + set_csr(mstatus, MSTATUS_MIE); + + if (rv) return rv; + + eos_intr_set_handler(INT_SPI1_BASE, net_handle_xchg); + SPI1_REG(SPI_REG_SCKDIV) = NET_SPI_DIV; + SPI1_REG(SPI_REG_CSID) = NET_SPI_CSID; + + clear_csr(mstatus, MSTATUS_MIE); + net_state_flags |= NET_STATE_FLAG_RUN; + if (source) { + if (source != EOS_PWR_WAKE_BTN) { + net_xchg_wake(); + } + if (!(net_state_flags & NET_STATE_FLAG_CTS)) { + while (!(GPIO_REG(GPIO_RISE_IP) & (1 << NET_PIN_CTS))) { + asm volatile ("wfi"); + } + GPIO_REG(GPIO_RISE_IP) = (1 << NET_PIN_CTS); + } + net_xchg_reset(); + if (GPIO_REG(GPIO_INPUT_VAL) & (1 << NET_PIN_RTS)) net_state_flags |= NET_STATE_FLAG_RTS; + } + set_csr(mstatus, MSTATUS_MIE); + + return rv; +} + +void eos_net_bad_handler(unsigned char type, unsigned char *buffer, uint16_t len) { + eos_evtq_bad_handler(type, buffer, len); + if (buffer) eos_net_free(buffer, 0); +} + void eos_net_set_handler(unsigned char mtype, eos_evt_handler_t handler) { - if (handler == NULL) handler = eos_evtq_bad_handler; + if (handler == NULL) handler = eos_net_bad_handler; if (mtype && (mtype <= EOS_NET_MAX_MTYPE)) net_handler[mtype - 1] = handler; } @@ -354,7 +481,7 @@ int eos_net_send(unsigned char type, unsigned char *buffer, uint16_t len, unsign int rv = EOS_OK; if (more) { - type |= NET_MTYPE_FLAG_ONEW; + type |= EOS_NET_MTYPE_FLAG_ONEW; } clear_csr(mstatus, MSTATUS_MIE); if ((net_state_flags & NET_STATE_FLAG_RUN) && (net_state_flags & NET_STATE_FLAG_CTS)) { diff --git a/code/fe310/eos/net.h b/code/fe310/eos/net.h index 5cb8da6..1aaea1f 100644 --- a/code/fe310/eos/net.h +++ b/code/fe310/eos/net.h @@ -1,22 +1,31 @@ #include <stdint.h> #include "event.h" -#define EOS_NET_SIZE_BUF 1500 -#define EOS_NET_SIZE_BUFQ 2 +/* common */ +#define EOS_NET_SIZE_BUF 1500 -#define EOS_NET_MTYPE_SOCK 1 -#define EOS_NET_MTYPE_AUDIO 2 +#define EOS_NET_MTYPE_SOCK 1 +#define EOS_NET_MTYPE_POWER 4 -#define EOS_NET_MTYPE_WIFI 5 -#define EOS_NET_MTYPE_CELL 6 -#define EOS_NET_MTYPE_SIP 7 -#define EOS_NET_MTYPE_APP 8 +#define EOS_NET_MTYPE_WIFI 5 +#define EOS_NET_MTYPE_CELL 6 +#define EOS_NET_MTYPE_SIP 7 +#define EOS_NET_MTYPE_APP 8 -#define EOS_NET_MAX_MTYPE 8 +#define EOS_NET_MAX_MTYPE 8 + +#define EOS_NET_MTYPE_FLAG_ONEW 0x80 + +/* fe310 specific */ +#define EOS_NET_SIZE_BUFQ 2 void eos_net_init(void); void eos_net_start(void); void eos_net_stop(void); +int eos_net_sleep(uint32_t timeout); +int eos_net_wake(uint8_t source); + +void eos_net_bad_handler(unsigned char type, unsigned char *buffer, uint16_t len); void eos_net_set_handler(unsigned char type, eos_evt_handler_t handler); void eos_net_acquire_for_evt(unsigned char type, char acq); diff --git a/code/fe310/eos/net_def.h b/code/fe310/eos/net_def.h index 40c55d1..9aa25dd 100644 --- a/code/fe310/eos/net_def.h +++ b/code/fe310/eos/net_def.h @@ -9,7 +9,5 @@ #define NET_STATE_FLAG_ONEW 0x20 #define NET_STATE_FLAG_XCHG 0x40 -#define NET_MTYPE_FLAG_ONEW 0x10 - #define NET_SPI_DIV 16 #define NET_SPI_CSID 3 diff --git a/code/fe310/eos/sock.c b/code/fe310/eos/sock.c index 5e3dc67..720e620 100644 --- a/code/fe310/eos/sock.c +++ b/code/fe310/eos/sock.c @@ -9,21 +9,18 @@ #include "sock.h" static eos_evt_handler_t evt_handler[EOS_SOCK_MAX_SOCK]; -static uint16_t evt_handler_flags_buf_free = 0; -static uint16_t evt_handler_flags_buf_acq = 0; static void sock_handle_evt(unsigned char type, unsigned char *buffer, uint16_t len) { unsigned char sock; if ((buffer == NULL) || (len < 2)) { - eos_evtq_bad_handler(type, buffer, len); - eos_net_free(buffer, 0); + eos_net_bad_handler(type, buffer, len); return; } sock = buffer[1]; if ((sock == 0) || (sock > EOS_SOCK_MAX_SOCK)) { - eos_evtq_bad_handler(type, buffer, len); - eos_net_free(buffer, 0); + eos_net_bad_handler(type, buffer, len); + return; } sock--; @@ -32,8 +29,7 @@ static void sock_handle_evt(unsigned char type, unsigned char *buffer, uint16_t evt_handler[sock](type, buffer, len); break; default: - eos_evtq_bad_handler(type, buffer, len); - eos_net_free(buffer, 0); + eos_net_bad_handler(type, buffer, len); break; } } @@ -42,13 +38,13 @@ void eos_sock_init(void) { int i; for (i=0; i<EOS_SOCK_MAX_SOCK; i++) { - evt_handler[i] = eos_evtq_bad_handler; + evt_handler[i] = eos_net_bad_handler; } eos_net_set_handler(EOS_NET_MTYPE_SOCK, sock_handle_evt); } void eos_sock_set_handler(unsigned char sock, eos_evt_handler_t handler) { - if (handler == NULL) handler = eos_evtq_bad_handler; + if (handler == NULL) handler = eos_net_bad_handler; if (sock && (sock <= EOS_SOCK_MAX_SOCK)) evt_handler[sock - 1] = handler; } @@ -62,7 +58,7 @@ int eos_sock_open_udp(void) { rv = eos_net_send(EOS_NET_MTYPE_SOCK, buffer, 1, 0); if (rv) return rv; - eos_evtq_get(EOS_NET_MTYPE_SOCK, &type, 1, &buffer, &buf_size); + eos_evtq_wait(EOS_NET_MTYPE_SOCK, &type, 1, &buffer, &buf_size); if (buf_size < 2) { eos_net_free(buffer, 0); return EOS_ERR_NET; diff --git a/code/fe310/eos/spi.c b/code/fe310/eos/spi.c index adc66a6..fb47313 100644 --- a/code/fe310/eos/spi.c +++ b/code/fe310/eos/spi.c @@ -20,7 +20,7 @@ static uint8_t spi_dev; static uint8_t spi_dev_cs_pin; -static uint8_t spi_state_flags; +static volatile uint8_t spi_state_flags; static unsigned char spi_in_xchg; static uint32_t spi_state_len = 0; @@ -82,7 +82,7 @@ void eos_spi_stop(void) { void eos_spi_set_handler(unsigned char dev, eos_evt_handler_t handler) { if (handler == NULL) handler = eos_evtq_bad_handler; - if (dev && (dev <= EOS_NET_MAX_MTYPE)) evt_handler[dev - 1] = handler; + if (dev && (dev <= EOS_SPI_MAX_DEV)) evt_handler[dev - 1] = handler; } void _eos_spi_xchg_init(unsigned char *buffer, uint16_t len, uint8_t flags) { @@ -95,7 +95,7 @@ void _eos_spi_xchg_init(unsigned char *buffer, uint16_t len, uint8_t flags) { } static void spi_xchg_finish(void) { - volatile uint8_t done = 0; + uint8_t done = 0; while (!done) { clear_csr(mstatus, MSTATUS_MIE); diff --git a/code/fe310/eos/wifi.c b/code/fe310/eos/wifi.c index c6b1d73..d58c0eb 100644 --- a/code/fe310/eos/wifi.c +++ b/code/fe310/eos/wifi.c @@ -9,13 +9,10 @@ #include "wifi.h" static eos_evt_handler_t evt_handler[EOS_WIFI_MAX_MTYPE]; -static uint16_t evt_handler_flags_buf_free = 0; -static uint16_t evt_handler_flags_buf_acq = 0; static void wifi_handle_evt(unsigned char type, unsigned char *buffer, uint16_t len) { if ((buffer == NULL) || (len < 1)) { - eos_evtq_bad_handler(type, buffer, len); - eos_net_free(buffer, 0); + eos_net_bad_handler(type, buffer, len); return; } @@ -23,9 +20,7 @@ static void wifi_handle_evt(unsigned char type, unsigned char *buffer, uint16_t if (mtype < EOS_WIFI_MAX_MTYPE) { evt_handler[mtype](type, buffer, len); } else { - eos_evtq_bad_handler(type, buffer, len); - eos_net_free(buffer, 0); - return; + eos_net_bad_handler(type, buffer, len); } } @@ -33,13 +28,13 @@ void eos_wifi_init(void) { int i; for (i=0; i<EOS_WIFI_MAX_MTYPE; i++) { - evt_handler[i] = eos_evtq_bad_handler; + evt_handler[i] = eos_net_bad_handler; } eos_net_set_handler(EOS_NET_MTYPE_WIFI, wifi_handle_evt); } void eos_wifi_set_handler(unsigned char mtype, eos_evt_handler_t handler) { - if (handler == NULL) handler = eos_evtq_bad_handler; + if (handler == NULL) handler = eos_net_bad_handler; if (mtype < EOS_WIFI_MAX_MTYPE) evt_handler[mtype] = handler; } |