summaryrefslogtreecommitdiff
path: root/fw/fe310/eos
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@majstor.org>2022-03-30 13:22:19 +0200
committerUros Majstorovic <majstor@majstor.org>2022-03-30 13:22:19 +0200
commitc6962c5700f99441538dafa346626bb7e6d12488 (patch)
treef463808368735c290312b7bff906f2599293d0ac /fw/fe310/eos
parent0a5f8363fe4e6b3c7d4f17fde61e00ab63e43bcb (diff)
sock api fixed; net reply messages fixed
Diffstat (limited to 'fw/fe310/eos')
-rw-r--r--fw/fe310/eos/Makefile2
-rw-r--r--fw/fe310/eos/app/Makefile15
-rw-r--r--fw/fe310/eos/app/app_root.c88
-rw-r--r--fw/fe310/eos/app/app_root.h12
-rw-r--r--fw/fe310/eos/app/app_status.c17
-rw-r--r--fw/fe310/eos/app/app_status.h4
-rw-r--r--fw/fe310/eos/board.h4
-rw-r--r--fw/fe310/eos/eos.h7
-rw-r--r--fw/fe310/eos/eve/eve_eos.c2
-rw-r--r--fw/fe310/eos/event.c2
-rw-r--r--fw/fe310/eos/i2s.c177
-rw-r--r--fw/fe310/eos/i2s.h4
-rw-r--r--fw/fe310/eos/i2s_def.h3
-rw-r--r--fw/fe310/eos/interrupt.c2
-rw-r--r--fw/fe310/eos/net.c62
-rw-r--r--fw/fe310/eos/net.h13
-rw-r--r--fw/fe310/eos/sock.c101
-rw-r--r--fw/fe310/eos/sock.h10
-rw-r--r--fw/fe310/eos/trap_entry.S192
-rw-r--r--fw/fe310/eos/wifi.c61
-rw-r--r--fw/fe310/eos/wifi.h7
21 files changed, 432 insertions, 353 deletions
diff --git a/fw/fe310/eos/Makefile b/fw/fe310/eos/Makefile
index 01318de..a52b5db 100644
--- a/fw/fe310/eos/Makefile
+++ b/fw/fe310/eos/Makefile
@@ -1,7 +1,7 @@
include ../common.mk
CFLAGS += -I. -I$(bsp_dir)/include -I$(bsp_dir)/drivers -I$(ext_dir)/crypto
-obj = trap_entry.o eos.o msgq.o event.o interrupt.o timer.o pwr.o i2s.o i2c.o uart.o spi.o spi_dev.o lcd.o sdcard.o sdc_crypto.o cam.o net.o wifi.o cell.o sock.o unicode.o
+obj = trap_entry.o eos.o msgq.o event.o interrupt.o timer.o pwr.o i2s.o i2c.o uart.o spi.o spi_dev.o lcd.o sdcard.o sdc_crypto.o cam.o net.o rng.o wifi.o cell.o sock.o unicode.o
%.o: %.c %.h
diff --git a/fw/fe310/eos/app/Makefile b/fw/fe310/eos/app/Makefile
deleted file mode 100644
index fe0ba21..0000000
--- a/fw/fe310/eos/app/Makefile
+++ /dev/null
@@ -1,15 +0,0 @@
-include ../../common.mk
-
-obj = app_root.o app_status.o
-
-
-%.o: %.c %.h
- $(CC) $(CFLAGS) -c $<
-
-%.o: %.S
- $(CC) $(CFLAGS) -c $<
-
-all: $(obj)
-
-clean:
- rm -f *.o
diff --git a/fw/fe310/eos/app/app_root.c b/fw/fe310/eos/app/app_root.c
deleted file mode 100644
index fac9548..0000000
--- a/fw/fe310/eos/app/app_root.c
+++ /dev/null
@@ -1,88 +0,0 @@
-#include <stdlib.h>
-
-#include "net.h"
-
-#include "eve/eve.h"
-#include "eve/eve_kbd.h"
-#include "eve/eve_font.h"
-
-#include "eve/screen/window.h"
-#include "eve/screen/page.h"
-#include "eve/screen/form.h"
-
-#include "eve/widget/widgets.h"
-
-#include "app_status.h"
-#include "app_root.h"
-
-#define KBD_X 0
-#define KBD_Y 575
-#define KBD_W 480
-#define KBD_H 225
-
-static EVEKbd kbd;
-static EVEFont font;
-static EVEWindowRoot win_root;
-static EVEWindowKbd win_kbd;
-static EVEWindow win_status;
-static EVEWindow win_main;
-static EVEView view_status;
-static EVEViewStack view_stack;
-
-EVEWindowRoot *app_root(void) {
- return &win_root;
-}
-
-void app_root_refresh(void) {
- eve_spi_start();
- eve_window_root_draw(app_root());
- eve_spi_stop();
-}
-
-void app_root_init(eve_view_constructor_t home_page, int b) {
- EVERect g;
-
- eve_spi_start();
-
- if (b >= 0) eve_brightness(b);
-
- eve_font_init(&font, APP_FONT_HANDLE);
-
- g.x = 0;
- g.y = 0;
- g.w = APP_SCREEN_W;
- g.h = APP_SCREEN_H;
- eve_window_init_root(&win_root, &g, "root", &font);
-
- g.x = KBD_X;
- g.y = KBD_Y;
- g.w = KBD_W;
- g.h = KBD_H;
- eve_kbd_init(&kbd, &g, win_root.mem_next, &win_root.mem_next);
- eve_window_init_kbd(&win_kbd, &g, &win_root, "kbd", &kbd);
-
- g.x = 0;
- g.y = 0;
- g.w = APP_SCREEN_W;
- g.h = APP_STATUS_H;
- eve_window_init(&win_status, &g, (EVEWindow *)&win_root, "status");
- eve_view_init(&view_status, &win_status, app_status_draw, app_status_touch, NULL, NULL);
-
- g.x = 0;
- g.y = APP_STATUS_H;
- g.w = APP_SCREEN_W;
- g.h = APP_SCREEN_H - APP_STATUS_H;
- eve_window_init(&win_main, &g, (EVEWindow *)&win_root, "main");
-
- eve_view_stack_init(&view_stack);
- eve_view_create(&win_main, &view_stack, home_page);
-
- eve_window_append(&win_status);
- eve_window_append(&win_main);
-
- eve_window_root_draw(&win_root);
-
- eve_spi_stop();
-
- eos_net_acquire_for_evt(EOS_EVT_UI | EVE_ETYPE_INTR, 1);
-}
diff --git a/fw/fe310/eos/app/app_root.h b/fw/fe310/eos/app/app_root.h
deleted file mode 100644
index b085344..0000000
--- a/fw/fe310/eos/app/app_root.h
+++ /dev/null
@@ -1,12 +0,0 @@
-#include <stdint.h>
-
-#define APP_SCREEN_W 480
-#define APP_SCREEN_H 800
-#define APP_STATUS_H 60
-
-#define APP_FONT_HANDLE 31
-
-EVEWindowRoot *app_root(void);
-void app_root_refresh(void);
-
-void app_root_init(eve_view_constructor_t home_page, int b);
diff --git a/fw/fe310/eos/app/app_status.c b/fw/fe310/eos/app/app_status.c
deleted file mode 100644
index 4ab0a97..0000000
--- a/fw/fe310/eos/app/app_status.c
+++ /dev/null
@@ -1,17 +0,0 @@
-#include <stdlib.h>
-
-#include "eve/eve.h"
-#include "eve/eve_kbd.h"
-#include "eve/eve_font.h"
-
-#include "eve/screen/window.h"
-
-#include "app_status.h"
-
-uint8_t app_status_draw(EVEView *view, uint8_t tag0) {
- return eve_view_clear(view, tag0, 0);
-}
-
-int app_status_touch(EVEView *view, EVETouch *touch, uint16_t evt, uint8_t tag0) {
- return 0;
-}
diff --git a/fw/fe310/eos/app/app_status.h b/fw/fe310/eos/app/app_status.h
deleted file mode 100644
index 9b2ac66..0000000
--- a/fw/fe310/eos/app/app_status.h
+++ /dev/null
@@ -1,4 +0,0 @@
-#include <stdint.h>
-
-int app_status_touch(EVEView *view, EVETouch *touch, uint16_t evt, uint8_t tag0);
-uint8_t app_status_draw(EVEView *view, uint8_t tag0);
diff --git a/fw/fe310/eos/board.h b/fw/fe310/eos/board.h
index a18b58f..99832a7 100644
--- a/fw/fe310/eos/board.h
+++ b/fw/fe310/eos/board.h
@@ -30,8 +30,8 @@
#define I2S_PIN_SD_IN 13
#define I2S_PIN_SD_OUT 12
-#define I2S_IRQ_WS_ID (INT_PWM1_BASE + 0)
-#define I2S_IRQ_SD_ID (INT_PWM1_BASE + 3)
+#define I2S_IRQ_WS_ID (INT_PWM2_BASE + 0)
+#define I2S_IRQ_SD_ID (INT_PWM2_BASE + 3)
#define I2S_CTRL_ADDR_CK PWM0_CTRL_ADDR
#define I2S_CTRL_ADDR_WS_MIC PWM1_CTRL_ADDR
diff --git a/fw/fe310/eos/eos.h b/fw/fe310/eos/eos.h
index 6c061d5..87a7e27 100644
--- a/fw/fe310/eos/eos.h
+++ b/fw/fe310/eos/eos.h
@@ -5,9 +5,10 @@
#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_SIZE -10
+#define EOS_ERR_FULL -11
+#define EOS_ERR_EMPTY -12
+#define EOS_ERR_NOTFOUND -13
#define EOS_ERR_NET -20
diff --git a/fw/fe310/eos/eve/eve_eos.c b/fw/fe310/eos/eve/eve_eos.c
index 283aa6a..2e13cd2 100644
--- a/fw/fe310/eos/eve/eve_eos.c
+++ b/fw/fe310/eos/eve/eve_eos.c
@@ -101,4 +101,4 @@ void eos_eve_stop(void) {
eve_spi_start();
_stop();
eve_spi_stop();
-} \ No newline at end of file
+}
diff --git a/fw/fe310/eos/event.c b/fw/fe310/eos/event.c
index 4a6493f..753cdb4 100644
--- a/fw/fe310/eos/event.c
+++ b/fw/fe310/eos/event.c
@@ -140,7 +140,7 @@ 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);
+ printf("EVT BAD HANDLER:0x%x\n", type);
}
void eos_evtq_set_handler(unsigned char type, eos_evt_handler_t handler) {
diff --git a/fw/fe310/eos/i2s.c b/fw/fe310/eos/i2s.c
index 9cc32e0..9cc9d9c 100644
--- a/fw/fe310/eos/i2s.c
+++ b/fw/fe310/eos/i2s.c
@@ -25,22 +25,41 @@
#define EOS_ABUF_IDX_MASK(IDX, SIZE) ((IDX) & ((SIZE) - 1))
-EOSABuf _eos_i2s_mic_buf;
-EOSABuf _eos_i2s_spk_buf;
-uint32_t _eos_i2s_fmt = 0;
-uint32_t _eos_i2s_mode = 0;
-uint32_t _eos_i2s_sample = 0;
-uint32_t _eos_i2s_mic_wm = 0;
-uint32_t _eos_i2s_spk_wm = 0;
-uint32_t _eos_i2s_mic_evt_enable = 0;
-uint32_t _eos_i2s_spk_evt_enable = 0;
+EOSABuf i2s_mic_buf;
+EOSABuf i2s_spk_buf;
-static eos_i2s_handler_t i2s_spk_handler = NULL;
static eos_i2s_handler_t i2s_mic_handler = NULL;
+static eos_i2s_handler_t i2s_spk_handler = NULL;
static uint32_t i2s_clk_period;
static uint8_t i2s_mic_volume = 0; /* 0 - 8 */
static uint8_t i2s_spk_volume = 16; /* 0 - 16 */
+uint32_t _eos_i2s_drvr[] = {
+ 0, /* I2S_MIC_BUF */
+ 0, /* I2S_SPK_BUF */
+ EOS_I2S_FMT_PCM16, /* I2S_FMT */
+ EOS_I2S_MODE_STEREO, /* I2S_MODE */
+ 0, /* I2S_MIC_WM */
+ 0, /* I2S_SPK_WM */
+ 0, /* I2S_MIC_EVT */
+ 0, /* I2S_SPK_EVT */
+ 0, /* I2S_MIC_CMP2 */
+ 0, /* I2S_MIC_CMP3 */
+ 0, /* I2S_SAMPLE */
+};
+
+#define I2S_MIC_BUF 0
+#define I2S_SPK_BUF 1
+#define I2S_FMT 2
+#define I2S_MODE 3
+#define I2S_MIC_WM 4
+#define I2S_SPK_WM 5
+#define I2S_MIC_EVT 6
+#define I2S_SPK_EVT 7
+#define I2S_MIC_CMP2 8
+#define I2S_MIC_CMP3 9
+#define I2S_SAMPLE 10
+
static void _abuf_init(EOSABuf *buf, uint8_t *array, uint16_t size) {
buf->idx_r = 0;
buf->idx_w = 0;
@@ -86,7 +105,6 @@ static int _abuf_pop16(EOSABuf *buf, uint16_t *sample) {
}
}
-
static void _abuf_flush(EOSABuf *buf) {
buf->idx_r = 0;
buf->idx_w = 0;
@@ -99,17 +117,21 @@ static uint16_t _abuf_len(EOSABuf *buf) {
static void i2s_handle_evt(unsigned char type, unsigned char *buffer, uint16_t len) {
switch(type & ~EOS_EVT_MASK) {
case EOS_I2S_ETYPE_MIC:
- if (i2s_mic_handler) i2s_mic_handler(type);
- clear_csr(mstatus, MSTATUS_MIE);
- _eos_i2s_mic_evt_enable = 1;
- set_csr(mstatus, MSTATUS_MIE);
+ if (i2s_mic_handler) {
+ i2s_mic_handler(type);
+ clear_csr(mstatus, MSTATUS_MIE);
+ _eos_i2s_drvr[I2S_MIC_EVT] = 1;
+ set_csr(mstatus, MSTATUS_MIE);
+ }
break;
case EOS_I2S_ETYPE_SPK:
- if (i2s_spk_handler) i2s_spk_handler(type);
- clear_csr(mstatus, MSTATUS_MIE);
- _eos_i2s_spk_evt_enable = 1;
- set_csr(mstatus, MSTATUS_MIE);
+ if (i2s_spk_handler) {
+ i2s_spk_handler(type);
+ clear_csr(mstatus, MSTATUS_MIE);
+ _eos_i2s_drvr[I2S_SPK_EVT] = 1;
+ set_csr(mstatus, MSTATUS_MIE);
+ }
break;
default:
@@ -118,23 +140,40 @@ static void i2s_handle_evt(unsigned char type, unsigned char *buffer, uint16_t l
}
}
-static void _mic_vol_set(uint8_t vol) {
- I2S_REG_WS_MIC(PWM_CMP2) = i2s_clk_period * (vol + 1);
- I2S_REG_WS_MIC(PWM_CMP3) = I2S_REG_WS_MIC(PWM_CMP2) + i2s_clk_period * 16;
-}
+#define PLIC_PRIORITY 0x0C000000
+
+static void i2s_cmp_set(void) {
+ int c = 7; /* interrupt will trigger c i2s clocks before spk ws */
+ int spk_ws_offset = i2s_spk_volume - 16 + i2s_mic_volume;
+ volatile uint32_t *p = (uint32_t *)PLIC_PRIORITY+I2S_IRQ_SD_ID;
+
+ /* interrupt trigger - will start with left channel */
+ if (spk_ws_offset - c < 0) {
+ I2S_REG_WS_SPK(PWM_CMP3) = i2s_clk_period * (64 + spk_ws_offset - c);
+ } else {
+ I2S_REG_WS_SPK(PWM_CMP3) = i2s_clk_period * (spk_ws_offset - c);
+ }
-static void _spk_vol_set(uint8_t vol) {
- int spk_cmp = vol + i2s_mic_volume - 16;
+ /* disable interrupt for this cycle */
+ *p = 0;
- if (spk_cmp <= 0) {
- I2S_REG_WS_SPK(PWM_CMP1) = i2s_clk_period * (32 + spk_cmp);
- I2S_REG_WS_SPK(PWM_CMP2) = i2s_clk_period * (64 + spk_cmp);
+ /* empty buffers */
+ // i2s_mic_buf.idx_r = i2s_mic_buf.idx_w;
+ // i2s_spk_buf.idx_w = i2s_spk_buf.idx_r;
+
+ /* adjust spk ws relative to mic ws */
+ if (spk_ws_offset <= 0) {
+ spk_ws_offset += 32;
GPIO_REG(GPIO_OUTPUT_XOR) &= ~(1 << I2S_PIN_WS_SPK);
} else {
- I2S_REG_WS_SPK(PWM_CMP1) = i2s_clk_period * spk_cmp;
- I2S_REG_WS_SPK(PWM_CMP2) = i2s_clk_period * (32 + spk_cmp);
- GPIO_REG(GPIO_OUTPUT_XOR) |= (1 << I2S_PIN_WS_SPK);
+ GPIO_REG(GPIO_OUTPUT_XOR) |= (1 << I2S_PIN_WS_SPK);
}
+ I2S_REG_WS_SPK(PWM_CMP1) = i2s_clk_period * spk_ws_offset;
+ I2S_REG_WS_SPK(PWM_CMP2) = i2s_clk_period * (32 + spk_ws_offset);
+
+ /* mic cmp2 relative to interrupt trigger */
+ _eos_i2s_drvr[I2S_MIC_CMP2] = (17 + c - i2s_spk_volume) * i2s_clk_period; /* (17 + c - i2s_spk_volume) == (1 + i2s_mic_volume) - (spk_ws_offset - c) */
+ _eos_i2s_drvr[I2S_MIC_CMP3] = 16 * i2s_clk_period;
}
extern void _eos_i2s_start_pwm(void);
@@ -179,7 +218,7 @@ void eos_i2s_init_mux(void) {
GPIO_REG(GPIO_OUTPUT_EN) &= ~(1 << I2S_PIN_SD_OUT);
}
-void eos_i2s_start(uint32_t sample_rate, unsigned char fmt) {
+void eos_i2s_start(uint32_t sample_rate) {
i2s_clk_period = ((PRCI_get_cpu_freq() / (sample_rate * 64)) & ~I2S_PWM_SCALE_CK_MASK) + 1;
GPIO_REG(GPIO_INPUT_EN) |= (1 << I2S_PIN_SD_IN);
@@ -195,18 +234,28 @@ void eos_i2s_start(uint32_t sample_rate, unsigned char fmt) {
I2S_REG_WS_MIC(PWM_CMP0) = i2s_clk_period * 64 - 1;
I2S_REG_WS_MIC(PWM_CMP1) = i2s_clk_period * 32;
- _mic_vol_set(i2s_mic_volume);
I2S_REG_WS_SPK(PWM_CMP0) = i2s_clk_period * 64 - 1;
- _spk_vol_set(i2s_spk_volume);
+ i2s_cmp_set();
I2S_REG_CK(PWM_COUNT) = 0;
I2S_REG_WS_MIC(PWM_COUNT) = 0;
I2S_REG_WS_SPK(PWM_COUNT) = i2s_clk_period / 2;
- _eos_i2s_fmt = fmt;
- _eos_i2s_mic_evt_enable = 1;
- _eos_i2s_spk_evt_enable = 1;
+ if (i2s_mic_buf.array && i2s_mic_buf.size) {
+ _eos_i2s_drvr[I2S_MIC_BUF] = (uint32_t)&i2s_mic_buf;
+ if (_eos_i2s_drvr[I2S_MIC_WM] == 0) {
+ _eos_i2s_drvr[I2S_MIC_WM] = i2s_mic_buf.size / 2;
+ }
+ }
+ if (i2s_spk_buf.array && i2s_spk_buf.size) {
+ _eos_i2s_drvr[I2S_SPK_BUF] = (uint32_t)&i2s_spk_buf;
+ if (_eos_i2s_drvr[I2S_SPK_WM] == 0) {
+ _eos_i2s_drvr[I2S_SPK_WM] = i2s_spk_buf.size / 2;
+ }
+ }
+ if (i2s_mic_handler) _eos_i2s_drvr[I2S_MIC_EVT] = 1;
+ if (i2s_spk_handler) _eos_i2s_drvr[I2S_SPK_EVT] = 1;
eos_intr_set_priority(I2S_IRQ_SD_ID, IRQ_PRIORITY_I2S_SD);
eos_intr_set_priority(I2S_IRQ_WS_ID, IRQ_PRIORITY_I2S_WS);
@@ -237,8 +286,13 @@ void eos_i2s_stop(void) {
I2S_REG_WS_MIC(PWM_COUNT) = 0;
I2S_REG_WS_SPK(PWM_COUNT) = 0;
- _eos_i2s_mic_evt_enable = 0;
- _eos_i2s_spk_evt_enable = 0;
+ _eos_i2s_drvr[I2S_MIC_BUF] = 0;
+ _eos_i2s_drvr[I2S_MIC_EVT] = 0;
+ _eos_i2s_drvr[I2S_MIC_WM] = 0;
+
+ _eos_i2s_drvr[I2S_SPK_BUF] = 0;
+ _eos_i2s_drvr[I2S_SPK_EVT] = 0;
+ _eos_i2s_drvr[I2S_SPK_WM] = 0;
eos_intr_disable(I2S_IRQ_WS_ID);
eos_intr_disable(I2S_IRQ_SD_ID);
@@ -257,9 +311,17 @@ int eos_i2s_running(void) {
return !!(GPIO_REG(GPIO_IOF_EN) & (1 << I2S_PIN_CK));
}
+void eos_i2s_set_fmt(unsigned char fmt) {
+ _eos_i2s_drvr[I2S_FMT] = fmt;
+}
+
+void eos_i2s_set_mode(unsigned char mode) {
+ _eos_i2s_drvr[I2S_MODE] = mode;
+}
+
void eos_i2s_mic_init(uint8_t *mic_arr, uint16_t mic_arr_size) {
clear_csr(mstatus, MSTATUS_MIE);
- _abuf_init(&_eos_i2s_mic_buf, mic_arr, mic_arr_size);
+ _abuf_init(&i2s_mic_buf, mic_arr, mic_arr_size);
set_csr(mstatus, MSTATUS_MIE);
}
@@ -271,14 +333,14 @@ void eos_i2s_mic_set_handler(eos_i2s_handler_t wm_handler) {
void eos_i2s_mic_set_wm(uint16_t wm) {
clear_csr(mstatus, MSTATUS_MIE);
- _eos_i2s_mic_wm = wm;
+ _eos_i2s_drvr[I2S_MIC_WM] = wm;
set_csr(mstatus, MSTATUS_MIE);
}
uint16_t eos_i2s_mic_len(void) {
clear_csr(mstatus, MSTATUS_MIE);
- uint16_t ret = _abuf_len(&_eos_i2s_mic_buf);
+ uint16_t ret = _abuf_len(&i2s_mic_buf);
set_csr(mstatus, MSTATUS_MIE);
return ret;
}
@@ -288,15 +350,15 @@ uint16_t eos_i2s_mic_read(uint8_t *sample, uint16_t ssize) {
uint16_t _ssize = 0;
clear_csr(mstatus, MSTATUS_MIE);
- _ssize = MIN(ssize, _abuf_len(&_eos_i2s_mic_buf));
+ _ssize = MIN(ssize, _abuf_len(&i2s_mic_buf));
set_csr(mstatus, MSTATUS_MIE);
for (i=0; i<_ssize; i++) {
- sample[i] = _eos_i2s_mic_buf.array[EOS_ABUF_IDX_MASK(_eos_i2s_mic_buf.idx_r + i, _eos_i2s_mic_buf.size)];
+ sample[i] = i2s_mic_buf.array[EOS_ABUF_IDX_MASK(i2s_mic_buf.idx_r + i, i2s_mic_buf.size)];
}
clear_csr(mstatus, MSTATUS_MIE);
- _eos_i2s_mic_buf.idx_r += _ssize;
+ i2s_mic_buf.idx_r += _ssize;
set_csr(mstatus, MSTATUS_MIE);
return _ssize;
@@ -306,7 +368,7 @@ int eos_i2s_mic_pop8(uint8_t *sample) {
int ret;
clear_csr(mstatus, MSTATUS_MIE);
- ret = _abuf_pop8(&_eos_i2s_mic_buf, sample);
+ ret = _abuf_pop8(&i2s_mic_buf, sample);
set_csr(mstatus, MSTATUS_MIE);
return ret;
@@ -316,7 +378,7 @@ int eos_i2s_mic_pop16(uint16_t *sample) {
int ret;
clear_csr(mstatus, MSTATUS_MIE);
- ret = _abuf_pop16(&_eos_i2s_mic_buf, sample);
+ ret = _abuf_pop16(&i2s_mic_buf, sample);
set_csr(mstatus, MSTATUS_MIE);
return ret;
@@ -332,14 +394,13 @@ void eos_i2s_mic_vol_set(int vol) {
i2s_mic_volume = vol;
clear_csr(mstatus, MSTATUS_MIE);
- _mic_vol_set(vol);
- _spk_vol_set(i2s_spk_volume);
+ i2s_cmp_set();
set_csr(mstatus, MSTATUS_MIE);
}
void eos_i2s_spk_init(uint8_t *spk_arr, uint16_t spk_arr_size) {
clear_csr(mstatus, MSTATUS_MIE);
- _abuf_init(&_eos_i2s_spk_buf, spk_arr, spk_arr_size);
+ _abuf_init(&i2s_spk_buf, spk_arr, spk_arr_size);
set_csr(mstatus, MSTATUS_MIE);
}
@@ -351,13 +412,13 @@ void eos_i2s_spk_set_handler(eos_i2s_handler_t wm_handler) {
void eos_i2s_spk_set_wm(uint16_t wm) {
clear_csr(mstatus, MSTATUS_MIE);
- _eos_i2s_spk_wm = wm;
+ _eos_i2s_drvr[I2S_SPK_WM] = wm;
set_csr(mstatus, MSTATUS_MIE);
}
uint16_t eos_i2s_spk_len(void) {
clear_csr(mstatus, MSTATUS_MIE);
- uint16_t ret = _abuf_len(&_eos_i2s_spk_buf);
+ uint16_t ret = _abuf_len(&i2s_spk_buf);
set_csr(mstatus, MSTATUS_MIE);
return ret;
}
@@ -367,15 +428,15 @@ uint16_t eos_i2s_spk_write(uint8_t *sample, uint16_t ssize) {
uint16_t _ssize = 0;
clear_csr(mstatus, MSTATUS_MIE);
- _ssize = MIN(ssize, _eos_i2s_spk_buf.size - _abuf_len(&_eos_i2s_spk_buf));
+ _ssize = MIN(ssize, i2s_spk_buf.size - _abuf_len(&i2s_spk_buf));
set_csr(mstatus, MSTATUS_MIE);
for (i=0; i<_ssize; i++) {
- _eos_i2s_spk_buf.array[EOS_ABUF_IDX_MASK(_eos_i2s_spk_buf.idx_w + i, _eos_i2s_spk_buf.size)] = sample[i];
+ i2s_spk_buf.array[EOS_ABUF_IDX_MASK(i2s_spk_buf.idx_w + i, i2s_spk_buf.size)] = sample[i];
}
clear_csr(mstatus, MSTATUS_MIE);
- _eos_i2s_spk_buf.idx_w += _ssize;
+ i2s_spk_buf.idx_w += _ssize;
set_csr(mstatus, MSTATUS_MIE);
return _ssize;
@@ -385,7 +446,7 @@ int eos_i2s_spk_push8(uint8_t sample) {
int ret;
clear_csr(mstatus, MSTATUS_MIE);
- ret = _abuf_push8(&_eos_i2s_spk_buf, sample);
+ ret = _abuf_push8(&i2s_spk_buf, sample);
set_csr(mstatus, MSTATUS_MIE);
return ret;
@@ -395,7 +456,7 @@ int eos_i2s_spk_push16(uint16_t sample) {
int ret;
clear_csr(mstatus, MSTATUS_MIE);
- ret = _abuf_push16(&_eos_i2s_spk_buf, sample);
+ ret = _abuf_push16(&i2s_spk_buf, sample);
set_csr(mstatus, MSTATUS_MIE);
return ret;
@@ -411,6 +472,6 @@ void eos_i2s_spk_vol_set(int vol) {
i2s_spk_volume = vol;
clear_csr(mstatus, MSTATUS_MIE);
- _spk_vol_set(vol);
+ i2s_cmp_set();
set_csr(mstatus, MSTATUS_MIE);
}
diff --git a/fw/fe310/eos/i2s.h b/fw/fe310/eos/i2s.h
index f53e183..81b4ade 100644
--- a/fw/fe310/eos/i2s.h
+++ b/fw/fe310/eos/i2s.h
@@ -13,9 +13,11 @@ typedef void (*eos_i2s_handler_t) (unsigned char);
int eos_i2s_init(uint8_t wakeup_cause);
void eos_i2s_init_mux(void);
-void eos_i2s_start(uint32_t sample_rate, unsigned char fmt);
+void eos_i2s_start(uint32_t sample_rate);
void eos_i2s_stop(void);
int eos_i2s_running(void);
+void eos_i2s_set_fmt(unsigned char fmt);
+void eos_i2s_set_mode(unsigned char mode);
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 a338828..3529be1 100644
--- a/fw/fe310/eos/i2s_def.h
+++ b/fw/fe310/eos/i2s_def.h
@@ -1,5 +1,8 @@
#define EOS_I2S_FMT_PCM16 0
#define EOS_I2S_FMT_ALAW 1
+#define EOS_I2S_MODE_STEREO 0
+#define EOS_I2S_MODE_MONO 1
+
#define EOS_I2S_ETYPE_MIC 1
#define EOS_I2S_ETYPE_SPK 2
diff --git a/fw/fe310/eos/interrupt.c b/fw/fe310/eos/interrupt.c
index 8af492b..dab6fab 100644
--- a/fw/fe310/eos/interrupt.c
+++ b/fw/fe310/eos/interrupt.c
@@ -20,7 +20,7 @@ uintptr_t eos_intr_handle(uintptr_t int_num) {
if ((int_num >=1) && (int_num <= PLIC_NUM_INTERRUPTS) && (ext_interrupt_handler[int_num-1])) {
ext_interrupt_handler[int_num-1]();
} else {
- printf("error:%d\n", int_num);
+ printf("INTR ERROR:%d\n", int_num);
exit(int_num);
}
return int_num;
diff --git a/fw/fe310/eos/net.c b/fw/fe310/eos/net.c
index e65bcd2..e10414c 100644
--- a/fw/fe310/eos/net.c
+++ b/fw/fe310/eos/net.c
@@ -24,7 +24,7 @@
#define NET_STATE_FLAG_INIT 0x02
#define NET_STATE_FLAG_XCHG 0x04
#define NET_STATE_FLAG_ONEW 0x10
-#define NET_STATE_FLAG_REPW 0x20
+#define NET_STATE_FLAG_SYNC 0x20
#define NET_STATE_FLAG_RTS 0x40
#define NET_STATE_FLAG_CTS 0x80
@@ -108,7 +108,6 @@ static void net_xchg_start(unsigned char type, unsigned char *buffer, uint16_t l
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;
- if (type & EOS_NET_MTYPE_FLAG_REPW) net_state_flags |= NET_STATE_FLAG_REPW;
net_state_type = type;
net_state_len_tx = len;
@@ -156,7 +155,7 @@ static void net_handle_xchg(void) {
r2 = SPI1_REG(SPI_REG_RXFIFO);
r3 = SPI1_REG(SPI_REG_RXFIFO);
- if (net_state_flags & (NET_STATE_FLAG_ONEW | NET_STATE_FLAG_REPW)) {
+ if (net_state_flags & NET_STATE_FLAG_ONEW) {
r1 = 0;
r2 = 0;
r3 = 0;
@@ -189,20 +188,17 @@ static void net_handle_xchg(void) {
eos_spi_handle_xchg();
if (SPI1_REG(SPI_REG_CSMODE) == SPI_CSMODE_AUTO) { // exchange done
- if (net_state_flags & NET_STATE_FLAG_REPW) {
- net_state_flags &= ~NET_STATE_FLAG_REPW;
- } else {
+ if (!(net_state_flags & NET_STATE_FLAG_SYNC)) {
if (net_state_type) {
int r = eos_evtq_push_isr(EOS_EVT_NET | net_state_type, net_state_buf, net_state_len_rx);
if (r) eos_bufq_push(&net_buf_q, net_state_buf);
} else if (((net_state_flags & NET_STATE_FLAG_ONEW) || net_state_next_cnt) && (net_state_next_buf == NULL)) {
net_state_next_buf = net_state_buf;
- net_state_flags &= ~NET_STATE_FLAG_ONEW;
} else {
eos_bufq_push(&net_buf_q, net_state_buf);
}
}
- net_state_flags &= ~NET_STATE_FLAG_XCHG;
+ net_state_flags &= ~(NET_STATE_FLAG_ONEW | NET_STATE_FLAG_XCHG);
}
}
@@ -515,17 +511,21 @@ void eos_net_free(unsigned char *buffer, unsigned char more) {
set_csr(mstatus, MSTATUS_MIE);
}
-static int net_xchg(unsigned char *type, unsigned char *buffer, uint16_t *len) {
+static int net_xchg(unsigned char *type, unsigned char *buffer, uint16_t *len, unsigned char flags) {
int rv = EOS_OK;
int _sync = 0;
unsigned char _type = *type;
uint16_t _len = *len;
uint8_t spi_dev = EOS_SPI_DEV_NET;
+ if (flags & EOS_NET_FLAG_ONEW) _type |= EOS_NET_MTYPE_FLAG_ONEW;
+ if (flags & EOS_NET_FLAG_REPL) _type |= EOS_NET_MTYPE_FLAG_REPL;
+ if (flags & EOS_NET_FLAG_SYNC) _sync = 1;
+
clear_csr(mstatus, MSTATUS_MIE);
- if ((_type & EOS_NET_MTYPE_FLAG_REPW) || ((_type & EOS_NET_MTYPE_FLAG_ONEW) && !(net_state_flags & NET_STATE_FLAG_RUN))) _sync = 1;
+ if ((flags & EOS_NET_FLAG_ONEW) && !(net_state_flags & NET_STATE_FLAG_RUN)) _sync = 1;
- if (!(net_state_flags & NET_STATE_FLAG_RUN) && _sync) {
+ if (_sync && !(net_state_flags & NET_STATE_FLAG_RUN)) {
int _rv;
set_csr(mstatus, MSTATUS_MIE);
@@ -542,19 +542,25 @@ static int net_xchg(unsigned char *type, unsigned char *buffer, uint16_t *len) {
set_csr(mstatus, MSTATUS_MIE);
clear_csr(mstatus, MSTATUS_MIE);
}
+ if (flags & EOS_NET_FLAG_SYNC) {
+ net_state_flags |= NET_STATE_FLAG_SYNC;
+ }
net_xchg_start(_type, buffer, _len);
- if (_type & EOS_NET_MTYPE_FLAG_REPW) {
- while (!(net_state_flags & NET_STATE_FLAG_CTS)) {
- asm volatile ("wfi");
- set_csr(mstatus, MSTATUS_MIE);
- clear_csr(mstatus, MSTATUS_MIE);
+ if (flags & EOS_NET_FLAG_SYNC) {
+ if (flags & EOS_NET_FLAG_REPL) {
+ while (!(net_state_flags & NET_STATE_FLAG_CTS)) {
+ asm volatile ("wfi");
+ set_csr(mstatus, MSTATUS_MIE);
+ clear_csr(mstatus, MSTATUS_MIE);
+ }
+ net_xchg_start(0, buffer, 0);
}
- net_xchg_start(0, buffer, 0);
while (net_state_flags & NET_STATE_FLAG_XCHG) {
asm volatile ("wfi");
set_csr(mstatus, MSTATUS_MIE);
clear_csr(mstatus, MSTATUS_MIE);
}
+ net_state_flags &= ~NET_STATE_FLAG_SYNC;
*type = net_state_type;
*len = net_state_len_rx;
}
@@ -574,12 +580,22 @@ static int net_xchg(unsigned char *type, unsigned char *buffer, uint16_t *len) {
return rv;
}
-int eos_net_send(unsigned char type, unsigned char *buffer, uint16_t len, unsigned char more) {
- if (more) type |= EOS_NET_MTYPE_FLAG_ONEW;
- return net_xchg(&type, buffer, &len);
+int eos_net_xchg(unsigned char *type, unsigned char *buffer, uint16_t *len) {
+ return net_xchg(type, buffer, len, (EOS_NET_FLAG_ONEW | EOS_NET_FLAG_SYNC | EOS_NET_FLAG_REPL));
+}
+
+int eos_net_send(unsigned char type, unsigned char *buffer, uint16_t len) {
+ return net_xchg(&type, buffer, &len, (EOS_NET_FLAG_ONEW | EOS_NET_FLAG_SYNC));
}
-int eos_net_xchg(unsigned char *type, unsigned char *buffer, uint16_t *len) {
- *type |= EOS_NET_MTYPE_FLAG_REPW;
- return net_xchg(type, buffer, len);
+int eos_net_send_async(unsigned char type, unsigned char *buffer, uint16_t len, unsigned char more) {
+ return net_xchg(&type, buffer, &len, more ? EOS_NET_FLAG_ONEW : 0);
+}
+
+int _eos_net_send(unsigned char type, unsigned char *buffer, uint16_t len, unsigned char async, unsigned char more) {
+ if (async) {
+ eos_net_send_async(type, buffer, len, more);
+ } else {
+ eos_net_send(type, buffer, len);
+ }
}
diff --git a/fw/fe310/eos/net.h b/fw/fe310/eos/net.h
index 86496bb..79caf4b 100644
--- a/fw/fe310/eos/net.h
+++ b/fw/fe310/eos/net.h
@@ -3,9 +3,10 @@
/* common */
#define EOS_NET_MTU 1500
-#define EOS_NET_SIZE_BUF (EOS_NET_MTU + 4)
+#define EOS_NET_SIZE_BUF EOS_NET_MTU
#define EOS_NET_MTYPE_SOCK 1
+#define EOS_NET_MTYPE_RNG 3
#define EOS_NET_MTYPE_POWER 4
#define EOS_NET_MTYPE_WIFI 5
@@ -16,12 +17,16 @@
#define EOS_NET_MAX_MTYPE 8
#define EOS_NET_MTYPE_FLAG_ONEW 0x40
-#define EOS_NET_MTYPE_FLAG_REPW 0x80
+#define EOS_NET_MTYPE_FLAG_REPL 0x80
#define EOS_NET_MTYPE_FLAG_MASK 0xc0
/* fe310 specific */
#define EOS_NET_SIZE_BUFQ 2
+#define EOS_NET_FLAG_ONEW 0x1
+#define EOS_NET_FLAG_SYNC 0x2
+#define EOS_NET_FLAG_REPL 0x4
+
int eos_net_init(uint8_t wakeup_cause);
int eos_net_run(uint8_t wakeup_cause);
void eos_net_start(void);
@@ -36,5 +41,7 @@ void eos_net_acquire(void);
void eos_net_release(void);
unsigned char *eos_net_alloc(void);
void eos_net_free(unsigned char *buffer, unsigned char more);
-int eos_net_send(unsigned char type, unsigned char *buffer, uint16_t len, unsigned char more);
int eos_net_xchg(unsigned char *type, unsigned char *buffer, uint16_t *len);
+int eos_net_send(unsigned char type, unsigned char *buffer, uint16_t len);
+int eos_net_send_async(unsigned char type, unsigned char *buffer, uint16_t len, unsigned char more);
+int _eos_net_send(unsigned char type, unsigned char *buffer, uint16_t len, unsigned char async, unsigned char more);
diff --git a/fw/fe310/eos/sock.c b/fw/fe310/eos/sock.c
index ef043d1..7365c97 100644
--- a/fw/fe310/eos/sock.c
+++ b/fw/fe310/eos/sock.c
@@ -52,52 +52,103 @@ eos_evt_handler_t eos_sock_get_handler(unsigned char sock) {
return NULL;
}
-int eos_sock_open_udp(eos_evt_handler_t handler) {
- unsigned char type = EOS_SOCK_MTYPE_OPEN_DGRAM;
- unsigned char *buffer = eos_net_alloc();
- uint16_t buf_size;
+int eos_sock_open_udp(eos_evt_handler_t handler, unsigned char *buffer) {
+ unsigned char type;
+ uint16_t len;
+ int do_release;
int rv, sock;
- buffer[0] = type;
- rv = eos_net_send(EOS_NET_MTYPE_SOCK, buffer, 1, 0);
+ do_release = 0;
+ if (buffer == NULL) {
+ buffer = eos_net_alloc();
+ do_release = 1;
+ }
+
+ type = EOS_NET_MTYPE_SOCK;
+ len = 1;
+ buffer[0] = EOS_SOCK_MTYPE_OPEN_DGRAM;
+
+ rv = eos_net_xchg(&type, buffer, &len);
if (rv) return rv;
- 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;
- }
+ if (type != EOS_NET_MTYPE_SOCK) return EOS_ERR_NET;
+ if (len < 2) return EOS_ERR_SIZE;
sock = buffer[1];
- eos_net_free(buffer, 1);
-
if (sock == 0) return EOS_ERR_NET;
+ if (do_release) {
+ eos_net_free(buffer, 1);
+ }
eos_sock_set_handler(sock, handler);
+
return sock;
}
-void eos_sock_close(unsigned char sock) {
- unsigned char *buffer = eos_net_alloc();
+void eos_sock_close(unsigned char sock, unsigned char *buffer) {
+ int async;
+
+ async = 0;
+ if (buffer == NULL) {
+ buffer = eos_net_alloc();
+ async = 1;
+ }
buffer[0] = EOS_SOCK_MTYPE_CLOSE;
buffer[1] = sock;
- eos_net_send(EOS_NET_MTYPE_SOCK, buffer, 2, 1);
+ _eos_net_send(EOS_NET_MTYPE_SOCK, buffer, 2, async, 1);
eos_sock_set_handler(sock, NULL);
}
-int eos_sock_sendto(unsigned char sock, unsigned char *buffer, uint16_t size, unsigned char more, EOSNetAddr *addr) {
- unsigned char type = EOS_NET_MTYPE_SOCK;
-
+static int sock_send(unsigned char sock, unsigned char *msg, uint16_t msg_len, EOSNetAddr *addr, unsigned char *buffer) {
buffer[0] = EOS_SOCK_MTYPE_PKT;
buffer[1] = sock;
- memcpy(buffer+2, addr->host, sizeof(addr->host));
- memcpy(buffer+2+sizeof(addr->host), &addr->port, sizeof(addr->port));
- return eos_net_send(type, buffer, size, more);
+ buffer += 2;
+ memcpy(buffer, addr->host, sizeof(addr->host));
+ buffer += sizeof(addr->host);
+ buffer[0] = addr->port >> 8;
+ buffer[1] = addr->port;
+ buffer += sizeof(addr->port);
+ if (msg) {
+ if (msg_len + EOS_SOCK_SIZE_UDP_HDR > EOS_NET_MTU) return EOS_ERR_SIZE;
+ memcpy(buffer, msg, msg_len);
+ }
+
+ return EOS_OK;
}
-void eos_sock_getfrom(unsigned char *buffer, EOSNetAddr *addr) {
- memcpy(addr->host, buffer+2, sizeof(addr->host));
- memcpy(&addr->port, buffer+2+sizeof(addr->host), sizeof(addr->port));
+int eos_sock_sendto(unsigned char sock, unsigned char *msg, size_t msg_len, EOSNetAddr *addr, unsigned char *buffer) {
+ int rv;
+
+ rv = sock_send(sock, msg, msg_len, addr, buffer);
+ if (rv) return rv;
+
+ return eos_net_send(EOS_NET_MTYPE_SOCK, buffer, msg_len + EOS_SOCK_SIZE_UDP_HDR);
+}
+
+int eos_sock_sendto_async(unsigned char sock, unsigned char *msg, size_t msg_len, EOSNetAddr *addr, unsigned char *buffer, unsigned char more) {
+ int rv;
+
+ rv = sock_send(sock, msg, msg_len, addr, buffer);
+ if (rv) return rv;
+
+ return eos_net_send_async(EOS_NET_MTYPE_SOCK, buffer, msg_len + EOS_SOCK_SIZE_UDP_HDR, more);
}
+int eos_sock_recvfrom(unsigned char *buffer, uint16_t len, unsigned char *msg, size_t msg_size, EOSNetAddr *addr) {
+ if (len < EOS_SOCK_SIZE_UDP_HDR) return EOS_ERR_SIZE;
+
+ if (buffer[0] != EOS_SOCK_MTYPE_PKT) return EOS_ERR_NET;
+ buffer += 2;
+ memcpy(addr->host, buffer, sizeof(addr->host));
+ buffer += sizeof(addr->host);
+ addr->port = (uint16_t)buffer[0] << 8;
+ addr->port |= (uint16_t)buffer[1];
+ buffer += sizeof(addr->port);
+ if (msg) {
+ if (msg_size < len - EOS_SOCK_SIZE_UDP_HDR) return EOS_ERR_SIZE;
+ memcpy(msg, buffer, len - EOS_SOCK_SIZE_UDP_HDR);
+ }
+
+ return EOS_OK;
+}
diff --git a/fw/fe310/eos/sock.h b/fw/fe310/eos/sock.h
index 66fa2e1..5ef42e3 100644
--- a/fw/fe310/eos/sock.h
+++ b/fw/fe310/eos/sock.h
@@ -20,7 +20,9 @@ void eos_sock_netinit(void);
void eos_sock_set_handler(unsigned char sock, eos_evt_handler_t handler);
eos_evt_handler_t eos_sock_get_handler(unsigned char sock);
-int eos_sock_open_udp(eos_evt_handler_t handler);
-void eos_sock_close(unsigned char sock);
-int eos_sock_sendto(unsigned char sock, unsigned char *buffer, uint16_t size, unsigned char more, EOSNetAddr *addr);
-void eos_sock_getfrom(unsigned char *buffer, EOSNetAddr *addr);
+int eos_sock_open_udp(eos_evt_handler_t handler, unsigned char *buffer);
+void eos_sock_close(unsigned char sock, unsigned char *buffer);
+
+int eos_sock_sendto(unsigned char sock, unsigned char *msg, size_t msg_len, EOSNetAddr *addr, unsigned char *buffer);
+int eos_sock_sendto_async(unsigned char sock, unsigned char *msg, size_t msg_len, EOSNetAddr *addr, unsigned char *buffer, unsigned char more);
+int eos_sock_recvfrom(unsigned char *buffer, uint16_t len, unsigned char *msg, size_t msg_size, EOSNetAddr *addr);
diff --git a/fw/fe310/eos/trap_entry.S b/fw/fe310/eos/trap_entry.S
index 506683b..fb2b121 100644
--- a/fw/fe310/eos/trap_entry.S
+++ b/fw/fe310/eos/trap_entry.S
@@ -25,6 +25,18 @@
#define INT_PWM1_BASE 44
#define INT_PWM2_BASE 48
+#define I2S_MIC_BUF (0*4)
+#define I2S_SPK_BUF (1*4)
+#define I2S_FMT (2*4)
+#define I2S_MODE (3*4)
+#define I2S_MIC_WM (4*4)
+#define I2S_SPK_WM (5*4)
+#define I2S_MIC_EVT (6*4)
+#define I2S_SPK_EVT (7*4)
+#define I2S_MIC_CMP2 (8*4)
+#define I2S_MIC_CMP3 (9*4)
+#define I2S_SAMPLE (10*4)
+
#include "board.h"
#include "irq_def.h"
#include "evt_def.h"
@@ -37,7 +49,7 @@
.global eos_trap_entry
eos_trap_entry:
- addi sp, sp, -10*REGBYTES
+ addi sp, sp, -12*REGBYTES
STORE x8, 0*REGBYTES(sp)
STORE x9, 1*REGBYTES(sp)
STORE x18, 2*REGBYTES(sp)
@@ -46,8 +58,10 @@ eos_trap_entry:
STORE x21, 5*REGBYTES(sp)
STORE x22, 6*REGBYTES(sp)
STORE x23, 7*REGBYTES(sp)
- STORE x24, 8*REGBYTES(sp)
- STORE x25, 9*REGBYTES(sp)
+ STORE x24, 8*REGBYTES(sp) # format: 0 - PCM16; 1 - ALAW
+ STORE x25, 9*REGBYTES(sp) # mode: 0 - stereo; 1 - mono
+ STORE x26, 10*REGBYTES(sp) # channel: 0 - left; 1 - right
+ STORE x27, 11*REGBYTES(sp) # _eos_event_q addr
csrr x8, mcause
li x18, MCAUSE_EXT
@@ -86,51 +100,57 @@ evtq_push:
jalr x0, x21
i2s_handle_sd:
- li x9, I2S_CTRL_ADDR_WS_MIC
- lw x18, PWM_COUNT(x9)
- lw x19, PWM_CMP1(x9)
- lw x20, PWM_CMP2(x9)
- lw x21, PWM_CMP3(x9)
+ li x8, I2S_CTRL_ADDR_WS_SPK
+ lw x18, PWM_COUNT(x8)
+ lw x19, PWM_CMP3(x8)
# exit if too early
- bltu x18, x21, i2s_sd_exit
-
- # move CMPs for next channel and store parity to x25
- li x25, 0
- bltu x20, x19, 0f
- li x25, 1
- neg x19, x19
- li x22, PLIC_PRIORITY
- sw x0, 4*I2S_IRQ_SD_ID(x22)
+ bltu x18, x19, i2s_sd_exit
+
+ la x27, _eos_i2s_drvr
+
+ # move CMPs for next channel and store channel bit to x26
+ lw x20, I2S_MIC_CMP2(x27)
+ lw x21, I2S_MIC_CMP3(x27) # 16-bit period
+
+ add x23, x19, x20
+ add x24, x23, x21
+ slli x20, x21, 1 # 32-bit period
+ slli x21, x20, 1 # 64-bit period
+ bltu x24, x21, 0f
+ neg x21, x21
+ add x23, x23, x21
+ add x24, x24, x21
0:
- add x20, x20, x19
- add x21, x21, x19
- sw x20, PWM_CMP2(x9)
- sw x21, PWM_CMP3(x9)
+ li x26, 0
+ bltu x23, x20, 0f
+ li x26, 1
+0:
+ bltu x19, x20, 0f
+ neg x20, x20
+ li x18, PLIC_PRIORITY
+ sw x0, 4*I2S_IRQ_SD_ID(x18)
+0:
+ add x19, x19, x20
+
+ li x9, I2S_CTRL_ADDR_WS_MIC
+ sw x19, PWM_CMP3(x8)
+ sw x23, PWM_CMP2(x9)
+ sw x24, PWM_CMP3(x9)
- la x9, _eos_i2s_fmt
- lw x24, 0(x9)
+ lw x24, I2S_FMT(x27)
+ lw x25, I2S_MODE(x27)
i2s_abuf_pop:
- la x9, _eos_i2s_mode
- lw x22, 0(x9)
- and x22, x22, x25
- beqz x22, 0f
+ and x8, x25, x26
+ beqz x8, 0f
- la x9, _eos_i2s_sample
- lw x8, 0(x9)
+ lw x8, I2S_SAMPLE(x27)
j i2s_sd_xchg
-
0:
- mv x8, x0
-
- # check for pop from spk buf
- la x9, _eos_i2s_spk_wm
- lw x22, 0(x9)
- beqz x22, i2s_sd_xchg
-
# pop from spk buf -> x8
- la x9, _eos_i2s_spk_buf
+ lw x9, I2S_SPK_BUF(x27)
+ beqz x9, i2s_sd_xchg
lhu x18, I2S_ABUF_OFF_IDXR(x9)
lhu x19, I2S_ABUF_OFF_IDXW(x9)
lhu x20, I2S_ABUF_OFF_SIZE(x9)
@@ -160,12 +180,12 @@ i2s_abuf_pop:
and x18, x18, x21
# check for push to event queue
- bgtu x18, x22, i2s_decode
+ lw x9, I2S_SPK_WM(x27)
+ bgtu x18, x9, i2s_decode
- la x9, _eos_i2s_spk_evt_enable
- lw x18, 0(x9)
- beqz x18, i2s_decode
- sw x0, 0(x9)
+ lw x9, I2S_SPK_EVT(x27)
+ beqz x9, i2s_decode
+ sw x0, I2S_SPK_EVT(x27)
# push to event queue
jal x22, evtq_push
@@ -212,8 +232,8 @@ i2s_decode:
beqz x9, 3f
mul x8, x8, x9
3:
- la x9, _eos_i2s_sample
- sw x8, 0(x9)
+ beqz x25, i2s_sd_xchg
+ sw x8, I2S_SAMPLE(x27)
i2s_sd_xchg:
# read/write shift reg: x8 -> sr -> x8
@@ -318,16 +338,12 @@ i2s_encode:
andi x8, x8, 0xff
i2s_abuf_push:
- # check parity
- # bnez x25, i2s_sd_exit
-
- # check for push to mic buf
- la x9, _eos_i2s_mic_wm
- lw x22, 0(x9)
- beqz x22, i2s_sd_exit
+ # check channel
+ # bnez x26, i2s_sd_exit
# push to mic buf
- la x9, _eos_i2s_mic_buf
+ lw x9, I2S_MIC_BUF(x27)
+ beqz x9, i2s_sd_exit
lhu x18, I2S_ABUF_OFF_IDXR(x9)
lhu x19, I2S_ABUF_OFF_IDXW(x9)
lhu x20, I2S_ABUF_OFF_SIZE(x9)
@@ -357,12 +373,12 @@ i2s_abuf_push:
2:
# check for push to event queue
- bltu x18, x22, i2s_sd_exit
+ lw x9, I2S_MIC_WM(x27)
+ bltu x18, x9, i2s_sd_exit
- la x9, _eos_i2s_mic_evt_enable
- lw x18, 0(x9)
- beqz x18, i2s_sd_exit
- sw x0, 0(x9)
+ lw x9, I2S_MIC_EVT(x27)
+ beqz x9, i2s_sd_exit
+ sw x0, I2S_MIC_EVT(x27)
# push to event queue
jal x22, evtq_push
@@ -427,16 +443,6 @@ _eos_i2s_start_pwm:
ret
-.global _eos_flash_set
-_eos_flash_set:
- li a3, SPI0_CTRL_ADDR
- sw x0, SPI_REG_FCTRL(a3)
- sw a0, SPI_REG_SCKDIV(a3)
- sw a1, SPI_REG_FFMT(a3)
- li a0, 1
- sw a0, SPI_REG_FCTRL(a3)
- ret
-
trap_exit_data:
# Remain in M-mode after mret
li x18, MSTATUS_MPP
@@ -452,7 +458,9 @@ trap_exit_data:
LOAD x23, 7*REGBYTES(sp)
LOAD x24, 8*REGBYTES(sp)
LOAD x25, 9*REGBYTES(sp)
- addi sp, sp, 10*REGBYTES
+ LOAD x26, 10*REGBYTES(sp)
+ LOAD x27, 11*REGBYTES(sp)
+ addi sp, sp, 12*REGBYTES
mret
@@ -465,7 +473,7 @@ handle_intr:
.align 4
trap_entry_text:
- addi sp, sp, -22*REGBYTES
+ addi sp, sp, -20*REGBYTES
STORE x1, 0*REGBYTES(sp)
STORE x2, 1*REGBYTES(sp)
@@ -482,12 +490,10 @@ trap_entry_text:
STORE x15, 12*REGBYTES(sp)
STORE x16, 13*REGBYTES(sp)
STORE x17, 14*REGBYTES(sp)
- STORE x26, 15*REGBYTES(sp)
- STORE x27, 16*REGBYTES(sp)
- STORE x28, 17*REGBYTES(sp)
- STORE x29, 18*REGBYTES(sp)
- STORE x30, 19*REGBYTES(sp)
- STORE x31, 20*REGBYTES(sp)
+ STORE x28, 15*REGBYTES(sp)
+ STORE x29, 16*REGBYTES(sp)
+ STORE x30, 17*REGBYTES(sp)
+ STORE x31, 18*REGBYTES(sp)
li x18, MCAUSE_TIMER
beq x8, x18, handle_timer
@@ -526,23 +532,23 @@ trap_exit_text:
LOAD x15, 12*REGBYTES(sp)
LOAD x16, 13*REGBYTES(sp)
LOAD x17, 14*REGBYTES(sp)
- LOAD x26, 15*REGBYTES(sp)
- LOAD x27, 16*REGBYTES(sp)
- LOAD x28, 17*REGBYTES(sp)
- LOAD x29, 18*REGBYTES(sp)
- LOAD x30, 19*REGBYTES(sp)
- LOAD x31, 20*REGBYTES(sp)
-
- LOAD x8, 22*REGBYTES(sp)
- LOAD x9, 23*REGBYTES(sp)
- LOAD x18, 24*REGBYTES(sp)
- LOAD x19, 25*REGBYTES(sp)
- LOAD x20, 26*REGBYTES(sp)
- LOAD x21, 27*REGBYTES(sp)
- LOAD x22, 28*REGBYTES(sp)
- LOAD x23, 29*REGBYTES(sp)
- LOAD x24, 30*REGBYTES(sp)
- LOAD x25, 31*REGBYTES(sp)
+ LOAD x28, 15*REGBYTES(sp)
+ LOAD x29, 16*REGBYTES(sp)
+ LOAD x30, 17*REGBYTES(sp)
+ LOAD x31, 18*REGBYTES(sp)
+
+ LOAD x8, 20*REGBYTES(sp)
+ LOAD x9, 21*REGBYTES(sp)
+ LOAD x18, 22*REGBYTES(sp)
+ LOAD x19, 23*REGBYTES(sp)
+ LOAD x20, 24*REGBYTES(sp)
+ LOAD x21, 25*REGBYTES(sp)
+ LOAD x22, 26*REGBYTES(sp)
+ LOAD x23, 27*REGBYTES(sp)
+ LOAD x24, 28*REGBYTES(sp)
+ LOAD x25, 29*REGBYTES(sp)
+ LOAD x26, 30*REGBYTES(sp)
+ LOAD x27, 31*REGBYTES(sp)
addi sp, sp, 32*REGBYTES
mret
diff --git a/fw/fe310/eos/wifi.c b/fw/fe310/eos/wifi.c
index d7c1824..0663582 100644
--- a/fw/fe310/eos/wifi.c
+++ b/fw/fe310/eos/wifi.c
@@ -43,3 +43,64 @@ eos_evt_handler_t eos_wifi_get_handler(unsigned char mtype) {
if (mtype < EOS_WIFI_MAX_MTYPE) return evt_handler[mtype];
return NULL;
}
+
+int eos_wifi_scan(unsigned char *buffer) {
+ int async;
+
+ async = 0;
+ if (buffer == NULL) {
+ buffer = eos_net_alloc();
+ async = 1;
+ }
+ buffer[0] = EOS_WIFI_MTYPE_SCAN;
+ return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, async, 1);
+}
+
+int eos_wifi_auth(const char *ssid, const char *pass, unsigned char *buffer) {
+ unsigned char *buf;
+ size_t ssid_len, pass_len;
+ int async;
+
+ async = 0;
+ if (buffer == NULL) {
+ buffer = eos_net_alloc();
+ async = 1;
+ }
+ ssid_len = strlen(ssid) + 1;
+ pass_len = strlen(pass) + 1;
+ if ((1 + ssid_len + pass_len) > EOS_NET_MTU) return EOS_ERR_SIZE;
+
+ buf = buffer;
+ buf[0] = EOS_WIFI_MTYPE_AUTH;
+ buf++;
+ strcpy(buf, ssid);
+ buf += ssid_len;
+ strcpy(buf, pass);
+ buf += pass_len;
+
+ return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1 + ssid_len + pass_len, async, 1);
+}
+
+int eos_wifi_connect(unsigned char *buffer) {
+ int async;
+
+ async = 0;
+ if (buffer == NULL) {
+ buffer = eos_net_alloc();
+ async = 1;
+ }
+ buffer[0] = EOS_WIFI_MTYPE_CONNECT;
+ return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, async, 1);
+}
+
+int eos_wifi_disconnect(unsigned char *buffer) {
+ int async;
+
+ async = 0;
+ if (buffer == NULL) {
+ buffer = eos_net_alloc();
+ async = 1;
+ }
+ buffer[0] = EOS_WIFI_MTYPE_DISCONNECT;
+ return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, async, 1);
+}
diff --git a/fw/fe310/eos/wifi.h b/fw/fe310/eos/wifi.h
index 87f418f..4a49518 100644
--- a/fw/fe310/eos/wifi.h
+++ b/fw/fe310/eos/wifi.h
@@ -2,7 +2,7 @@
#include "event.h"
#define EOS_WIFI_MTYPE_SCAN 1
-#define EOS_WIFI_MTYPE_CONFIG 2
+#define EOS_WIFI_MTYPE_AUTH 2
#define EOS_WIFI_MTYPE_CONNECT 3
#define EOS_WIFI_MTYPE_DISCONNECT 4
@@ -11,3 +11,8 @@
void eos_wifi_netinit(void);
void eos_wifi_set_handler(unsigned char mtype, eos_evt_handler_t handler);
eos_evt_handler_t eos_wifi_get_handler(unsigned char mtype);
+
+int eos_wifi_scan(unsigned char *buffer);
+int eos_wifi_auth(const char *ssid, const char *pass, unsigned char *buffer);
+int eos_wifi_connect(unsigned char *buffer);
+int eos_wifi_disconnect(unsigned char *buffer);