Skip to content

Commit

Permalink
Merge pull request #41 from Seeed-Studio/feat/sdk_v1.1
Browse files Browse the repository at this point in the history
Feat: adapt SDK V1.1
  • Loading branch information
LynnL4 authored Jan 2, 2024
2 parents fda90bc + dbf16f4 commit 7588122
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 38 deletions.
2 changes: 1 addition & 1 deletion porting/himax/we2/drivers/drv_ov5647.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ el_err_code_t drv_ov5647_init(uint16_t width, uint16_t height) {

//OV5647 Enable
hx_drv_gpio_set_output(AON_GPIO1, GPIO_OUT_HIGH);
hx_drv_scu_set_PA1_pinmux(SCU_PA1_PINMUX_AON_GPIO1);
hx_drv_scu_set_PA1_pinmux(SCU_PA1_PINMUX_AON_GPIO1, 0);
hx_drv_gpio_set_out_value(AON_GPIO1, GPIO_OUT_HIGH);
EL_LOGD("Set PA1(AON_GPIO1) to High");

Expand Down
2 changes: 1 addition & 1 deletion porting/himax/we2/el_misc_we2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ EL_ATTR_WEAK void el_free(void* ptr) {
#endif
}

EL_ATTR_WEAK void el_reset(void) { exit(0); }
EL_ATTR_WEAK void el_reset(void) { __NVIC_SystemReset(); }

EL_ATTR_WEAK void el_status_led(bool on) {
// maybe unsafe when build with -no-threadsafe-statics
Expand Down
4 changes: 2 additions & 2 deletions porting/himax/we2/el_network_we2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ void NetworkWE2::init(status_cb_t cb) {
}

if (at.port == NULL) {
hx_drv_scu_set_PB6_pinmux(SCU_PB6_PINMUX_UART1_RX);
hx_drv_scu_set_PB7_pinmux(SCU_PB7_PINMUX_UART1_TX);
hx_drv_scu_set_PB6_pinmux(SCU_PB6_PINMUX_UART1_RX, 0);
hx_drv_scu_set_PB7_pinmux(SCU_PB7_PINMUX_UART1_TX, 0);
hx_drv_uart_init(USE_DW_UART_1, HX_UART1_BASE);
at.port = hx_drv_uart_get_dev(USE_DW_UART_1);
if (at.port == NULL) {
Expand Down
101 changes: 72 additions & 29 deletions porting/himax/we2/el_serial_we2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,32 @@ extern "C" {
#include "core/el_types.h"
#include "core/utils/el_ringbuffer.hpp"
#include "el_config_porting.h"

namespace edgelab {

namespace porting {

static lwRingBuffer* _serial_ring_buffer = nullptr;
static char _serial_char_buffer[32]{};
volatile static DEV_UART* _serial_port_handler = nullptr;

void _serial_uart_dma_recv(void*) {
_serial_ring_buffer->put(_serial_char_buffer[0]);
_serial_port_handler->uart_read_udma(_serial_char_buffer, 1, (void*)_serial_uart_dma_recv);
static lwRingBuffer* _rb_rx = nullptr;
static lwRingBuffer* _rb_tx = nullptr;
static char _buf_rx[32]{};
static char _buf_tx[2048]{};
volatile static bool _tx_busy = false;
volatile static SemaphoreHandle_t _mutex_tx = nullptr;
volatile static DEV_UART* _uart = nullptr;

void _uart_dma_recv(void*) {
_rb_rx->put(_buf_rx[0]);
_uart->uart_read_udma(_buf_rx, 1, (void*)_uart_dma_recv);
}

void _uart_dma_send(void*) {
size_t remaind = _rb_tx->size() < 2048 ? _rb_tx->size() : 2048;
_tx_busy = remaind != 0;
if (remaind != 0) {
_rb_tx->get(_buf_tx, remaind);
SCB_CleanDCache_by_Addr((volatile void*)_buf_tx, remaind);
_uart->uart_write_udma(_buf_tx, remaind, (void*)_uart_dma_send);
}
}
} // namespace porting

using namespace porting;
Expand All @@ -63,18 +75,26 @@ el_err_code_t SerialWE2::init() {
if (this->_is_present) [[unlikely]]
return EL_EPERM;

_console_uart = hx_drv_uart_get_dev((USE_DW_UART_E)CONSOLE_UART_ID);
_console_uart = hx_drv_uart_get_dev((USE_DW_UART_E)0);
_console_uart->uart_open(UART_BAUDRATE_921600);

if (!_serial_ring_buffer) [[likely]]
_serial_ring_buffer = new lwRingBuffer{8192};
if (!_rb_rx) [[likely]]
_rb_rx = new lwRingBuffer{8192};

if (!_rb_tx) [[likely]]
_rb_tx = new lwRingBuffer{16384};

_mutex_tx = xSemaphoreCreateMutex();

EL_ASSERT(_serial_ring_buffer);
EL_ASSERT(_rb_rx);
EL_ASSERT(_rb_tx);
EL_ASSERT(_mutex_tx);

_serial_port_handler = _console_uart;
_serial_port_handler->uart_read_udma(_serial_char_buffer, 1, (void*)_serial_uart_dma_recv);
_uart = _console_uart;
_uart->uart_read_udma(_buf_rx, 1, (void*)_uart_dma_recv);

this->_is_present = _console_uart != nullptr;
_tx_busy = false;

return this->_is_present ? EL_OK : EL_EIO;
}
Expand All @@ -83,7 +103,15 @@ el_err_code_t SerialWE2::deinit() {
if (!this->_is_present) [[unlikely]]
return EL_EPERM;

this->_is_present = !hx_drv_uart_deinit((USE_DW_UART_E)CONSOLE_UART_ID) ? false : true;
this->_is_present = !hx_drv_uart_deinit((USE_DW_UART_E)0) ? false : true;

delete _rb_rx;
delete _rb_tx;
vSemaphoreDelete(_mutex_tx);

_rb_rx = nullptr;
_rb_tx = nullptr;
_mutex_tx = nullptr;

return !this->_is_present ? EL_OK : EL_EIO;
}
Expand All @@ -104,29 +132,33 @@ char SerialWE2::get_char() {
if (!this->_is_present) [[unlikely]]
return '\0';

return porting::_serial_ring_buffer->get();
return porting::_rb_rx->get();
}

std::size_t SerialWE2::get_line(char* buffer, size_t size, const char delim) {
if (!this->_is_present) [[unlikely]]
return 0;

return porting::_serial_ring_buffer->extract(delim, buffer, size);
return porting::_rb_rx->extract(delim, buffer, size);
}

std::size_t SerialWE2::read_bytes(char* buffer, size_t size) {
if (!this->_is_present) [[unlikely]]
return 0;

size_t time_start = el_get_time_ms();
size_t read{0};
size_t pos_of_bytes{0};

while (size) {
size_t bytes_to_read{size < 8 ? size : 8};

read += _console_uart->uart_read(buffer + pos_of_bytes, bytes_to_read);
pos_of_bytes += bytes_to_read;
size -= bytes_to_read;
pos_of_bytes = _rb_rx->size();
if (pos_of_bytes) {
read += _rb_rx->get(buffer + read, size);
size -= read;
}
if (el_get_time_ms() - time_start > 3000) {
break;
}
}

return read;
Expand All @@ -136,18 +168,29 @@ std::size_t SerialWE2::send_bytes(const char* buffer, size_t size) {
if (!this->_is_present) [[unlikely]]
return 0;

size_t sent{0};
size_t pos_of_bytes{0};
size_t time_start = el_get_time_ms();
size_t bytes_to_send{0};
size_t sent = 0;
xSemaphoreTake(_mutex_tx, portMAX_DELAY);

while (size) {
size_t bytes_to_send{size < 8 ? size : 8};

sent += _console_uart->uart_write(buffer + pos_of_bytes, bytes_to_send);
pos_of_bytes += bytes_to_send;
bytes_to_send = _rb_tx->put(buffer + sent, size);
size -= bytes_to_send;
sent += bytes_to_send;
if (!_tx_busy) {
_tx_busy = true;
bytes_to_send = _rb_tx->size() < 2048 ? _rb_tx->size() : 2048;
_rb_tx->get(_buf_tx, bytes_to_send);
SCB_CleanDCache_by_Addr((volatile void*)_buf_tx, bytes_to_send);
_uart->uart_write_udma(_buf_tx, bytes_to_send, (void*)_uart_dma_send);
}
if (el_get_time_ms() - time_start > 3000) {
break;
}
}

xSemaphoreGive(_mutex_tx);
return sent;
}

} // namespace edgelab
} // namespace edgelab
24 changes: 19 additions & 5 deletions porting/himax/we2/el_wire_we2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,29 @@ namespace edgelab {

namespace porting {

void i2cs_tx_timeout_cb(void* data) { el_reset(); }

static void i2cs_tx_timer(uint32_t timeout_ms) {
TIMER_CFG_T timer_cfg;
timer_cfg.period = timeout_ms;
timer_cfg.mode = TIMER_MODE_ONESHOT;
timer_cfg.ctrl = TIMER_CTRL_CPU;
timer_cfg.state = TIMER_STATE_DC;

hx_drv_timer_cm55s_stop();
hx_drv_timer_cm55s_start(&timer_cfg, (Timer_ISREvent_t)i2cs_tx_timeout_cb);
}

static void i2c_s_callback_fun_tx(void* param) {
// EL_LOGD("%s \n", __FUNCTION__);

HX_DRV_DEV_IIC* iic_obj = (HX_DRV_DEV_IIC*)param;
HX_DRV_DEV_IIC_INFO* iic_info_ptr = &(iic_obj->iic_info);
WireWE2* wire = (WireWE2*)iic_info_ptr->extra;

memset(wire->tx_buffer, 0, sizeof(wire->tx_buffer));
wire->wire_read_enable(sizeof(wire->rx_buffer));
hx_drv_timer_cm55s_stop();
}

static void i2c_s_callback_fun_rx(void* param) {
Expand Down Expand Up @@ -137,8 +152,8 @@ el_err_code_t WireWE2::init() {
EL_ASSERT(!this->_is_present);

this->index = USE_DW_IIC_SLV_0;
hx_drv_scu_set_PA2_pinmux(SCU_PA2_PINMUX_SB_I2C_S_SCL_0);
hx_drv_scu_set_PA3_pinmux(SCU_PA3_PINMUX_SB_I2C_S_SDA_0);
hx_drv_scu_set_PA2_pinmux(SCU_PA2_PINMUX_SB_I2C_S_SCL_0, 1);
hx_drv_scu_set_PA3_pinmux(SCU_PA3_PINMUX_SB_I2C_S_SDA_0, 1);

if (IIC_ERR_OK != hx_drv_i2cs_init((USE_DW_IIC_SLV_E)this->index, HX_I2C_HOST_SLV_0_BASE)) {
return EL_EIO;
Expand All @@ -149,7 +164,6 @@ el_err_code_t WireWE2::init() {
this->i2c->iic_control(DW_IIC_CMD_SET_TXCB, (void*)i2c_s_callback_fun_tx);
this->i2c->iic_control(DW_IIC_CMD_SET_RXCB, (void*)i2c_s_callback_fun_rx);
this->i2c->iic_control(DW_IIC_CMD_SET_ERRCB, (void*)i2c_s_callback_fun_err);
//this->i2c->iic_control(DW_IIC_CMD_SET_STACB, (void*)i2c_s_callback_fun_sta);
this->i2c->iic_control(DW_IIC_CMD_SLV_SET_SLV_ADDR, (void*)this->addr);

HX_DRV_DEV_IIC_INFO* iic_info_ptr = &(this->i2c->iic_info);
Expand All @@ -173,11 +187,10 @@ el_err_code_t WireWE2::deinit() {
this->i2c->iic_control(DW_IIC_CMD_SET_TXCB, (void*)NULL);
this->i2c->iic_control(DW_IIC_CMD_SET_RXCB, (void*)NULL);
this->i2c->iic_control(DW_IIC_CMD_SET_ERRCB, (void*)NULL);
this->i2c->iic_control(DW_IIC_CMD_SET_STACB, (void*)NULL);

this->i2c = nullptr;

hx_drv_i2cs_deinit((USE_DW_IIC_SLV_E)USE_DW_IIC_SLV_0);
hx_drv_i2cs_deinit((USE_DW_IIC_SLV_E)this->index);

this->_is_present = false;
delete this->rx_ring_buffer;
Expand Down Expand Up @@ -239,6 +252,7 @@ void WireWE2::wire_write_enable(size_t size) {
hx_CleanDCache_by_Addr((void*)this->tx_buffer, size);
IIC_ERR_CODE_E ret =
hx_drv_i2cs_interrupt_write(this->index, this->addr, this->tx_buffer, size, (void*)i2c_s_callback_fun_tx);
i2cs_tx_timer(I2CS_TX_TIME_OUT_MS);
}

} // namespace edgelab
3 changes: 3 additions & 0 deletions porting/himax/we2/el_wire_we2.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ extern "C" {
#define FEATURE_TRANSPORT_CMD_STOP 0x05
#define FEATURE_TRANSPORT_CMD_RESET 0x06

#define I2CS_TX_TIME_OUT_MS 1000

namespace edgelab {

class WireWE2 final : public Wire {
Expand All @@ -83,6 +85,7 @@ class WireWE2 final : public Wire {
void wire_read_enable(size_t size);
void wire_write_enable(size_t size);


public:
lwRingBuffer* rx_ring_buffer;
lwRingBuffer* tx_ring_buffer;
Expand Down

0 comments on commit 7588122

Please sign in to comment.