Skip to content

Commit

Permalink
Add buffer reusage for rpi_pico serial
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Dec 10, 2024
1 parent 8b9a9fb commit fd7de0b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
3 changes: 3 additions & 0 deletions examples/rpi_pico/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ void print_ip_address() {

void main_task(void *params) {
(void)params;
#ifndef NDEBUG
vTaskDelay(pdMS_TO_TICKS(3000));
#endif

#if WIFI_SUPPORT_ENABLED
if (cyw43_arch_init()) {
printf("Failed to initialise\n");
Expand Down
6 changes: 5 additions & 1 deletion include/zenoh-pico/system/platform/rpi_pico.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ typedef struct {
int _fd;
#endif
#if Z_FEATURE_LINK_SERIAL == 1
uart_inst_t *_serial;
struct {
uart_inst_t *_serial;
uint8_t *tmp_buf;
uint8_t *raw_buf;
};
#endif
};
} _z_sys_net_socket_t;
Expand Down
37 changes: 17 additions & 20 deletions src/system/rpi_pico/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,13 +530,18 @@ z_result_t _z_open_serial_from_pins(_z_sys_net_socket_t *sock, uint32_t txpin, u

_z_open_serial_impl(sock->_serial, txpin, rxpin, baudrate);

sock->tmp_buf = (uint8_t *)z_malloc(_Z_SERIAL_MFS_SIZE);
sock->raw_buf = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE);

return _z_connect_serial(*sock);
}

z_result_t _z_open_serial_from_dev(_z_sys_net_socket_t *sock, char *dev, uint32_t baudrate) {
z_result_t ret = _Z_RES_OK;

sock->_serial = NULL;
sock->tmp_buf = (uint8_t *)z_malloc(_Z_SERIAL_MFS_SIZE);
sock->raw_buf = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE);

#if Z_FEATURE_LINK_SERIAL_USB == 1
if (strcmp("usb", dev) == 0) {
Expand All @@ -559,6 +564,8 @@ z_result_t _z_open_serial_from_dev(_z_sys_net_socket_t *sock, char *dev, uint32_

if (sock->_serial == NULL) {
_Z_ERROR("invalid device name");
z_free(sock->tmp_buf);
z_free(sock->raw_buf);
return _Z_ERR_INVALID;
}

Expand Down Expand Up @@ -600,52 +607,42 @@ void _z_close_serial(_z_sys_net_socket_t *sock) {
_z_usb_uart_deinit();
#endif
}
z_free(sock->tmp_buf);
z_free(sock->raw_buf);
}

size_t _z_read_serial_internal(const _z_sys_net_socket_t sock, uint8_t *header, uint8_t *ptr, size_t len) {
uint8_t *raw_buf = z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE);
size_t rb = 0;
for (size_t i = 0; i < _Z_SERIAL_MAX_COBS_BUF_SIZE; i++) {
#if Z_FEATURE_LINK_SERIAL_USB == 1
raw_buf[i] = (sock._serial == NULL) ? _z_usb_uart_getc() : uart_getc(sock._serial);
sock.raw_buf[i] = (sock._serial == NULL) ? _z_usb_uart_getc() : uart_getc(sock._serial);
#else
raw_buf[i] = uart_getc(sock._serial);
sock.raw_buf[i] = uart_getc(sock._serial);
#endif
rb++;
if (raw_buf[i] == 0x00) {
if (sock.raw_buf[i] == 0x00) {
break;
}
}

uint8_t *tmp_buf = z_malloc(_Z_SERIAL_MFS_SIZE);
size_t ret = _z_serial_msg_deserialize(raw_buf, rb, ptr, len, header, tmp_buf, _Z_SERIAL_MFS_SIZE);

z_free(raw_buf);
z_free(tmp_buf);

return ret;
return _z_serial_msg_deserialize(sock.raw_buf, rb, ptr, len, header, sock.tmp_buf, _Z_SERIAL_MFS_SIZE);
}

size_t _z_send_serial_internal(const _z_sys_net_socket_t sock, uint8_t header, const uint8_t *ptr, size_t len) {
uint8_t *tmp_buf = (uint8_t *)z_malloc(_Z_SERIAL_MFS_SIZE);
uint8_t *raw_buf = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE);
size_t ret =
_z_serial_msg_serialize(raw_buf, _Z_SERIAL_MAX_COBS_BUF_SIZE, ptr, len, header, tmp_buf, _Z_SERIAL_MFS_SIZE);
size_t ret = _z_serial_msg_serialize(sock.raw_buf, _Z_SERIAL_MAX_COBS_BUF_SIZE, ptr, len, header, sock.tmp_buf,
_Z_SERIAL_MFS_SIZE);

if (ret == SIZE_MAX) {
return ret;
}
if (sock._serial == NULL) {
#if Z_FEATURE_LINK_SERIAL_USB == 1
_z_usb_uart_write(raw_buf, ret);
_z_usb_uart_write(sock.raw_buf, ret);
#endif
} else {
uart_write_blocking(sock._serial, raw_buf, ret);
uart_write_blocking(sock._serial, sock.raw_buf, ret);
}

z_free(raw_buf);
z_free(tmp_buf);

return len;
}
#endif
Expand Down

0 comments on commit fd7de0b

Please sign in to comment.