diff options
Diffstat (limited to 'firmware/zpu/lib')
| -rw-r--r-- | firmware/zpu/lib/net_common.c | 2 | ||||
| -rw-r--r-- | firmware/zpu/lib/udp_uart.c | 115 | ||||
| -rw-r--r-- | firmware/zpu/lib/udp_uart.h | 36 | 
3 files changed, 152 insertions, 1 deletions
diff --git a/firmware/zpu/lib/net_common.c b/firmware/zpu/lib/net_common.c index 698ed97f4..42e365393 100644 --- a/firmware/zpu/lib/net_common.c +++ b/firmware/zpu/lib/net_common.c @@ -201,7 +201,7 @@ send_pkt(      //create a list of all buffers to copy      const void *buffs[] = {&ctrl_word, &ehdr, buf0, buf1, buf2}; -    size_t lens[] = {sizeof(ctrl_word), sizeof(ehdr), len0, len1, len2}; +    size_t lens[] = {sizeof(ctrl_word), sizeof(ehdr), len0, len1, (len2 + 3) & ~3};      //copy each buffer into the out buffer      for (size_t i = 0; i < sizeof(buffs)/sizeof(buffs[0]); i++){ diff --git a/firmware/zpu/lib/udp_uart.c b/firmware/zpu/lib/udp_uart.c new file mode 100644 index 000000000..6f6b9ee91 --- /dev/null +++ b/firmware/zpu/lib/udp_uart.c @@ -0,0 +1,115 @@ +/* + * Copyright 2011 Ettus Research LLC + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program.  If not, see <http://www.gnu.org/licenses/>. + */ + +#include "udp_uart.h" +#include "hal_uart.h" +#include "net_common.h" +#include "compiler.h" +#include <stdbool.h> + +/*********************************************************************** + * Constants + **********************************************************************/ +#define MAX_NUM_UARTS 4 +#ifndef UDP_UART_MASK +    #error missing definition for UDP_UART_MASK enable mask +#endif +static const size_t num_idle_cyc_b4_flush = 11; //small but lucky number + +/*********************************************************************** + * Globals + **********************************************************************/ +static uint16_t _base_port; + +typedef struct{ +    struct socket_address dst; +    _AL4 uint8_t buf[256]; +    size_t len; //length of buffer +    size_t cyc; //idle cycle count +} udp_uart_state_t; + +static udp_uart_state_t _states[MAX_NUM_UARTS]; + +/*********************************************************************** + * UDP handler for UARTs + **********************************************************************/ +static void handle_uart_data_packet( +    struct socket_address src, struct socket_address dst, +    unsigned char *payload, int payload_len +){ +    //handle ICMP destination unreachable +    if (payload == NULL){ +        const size_t which = src.port-_base_port; +        if (which >= MAX_NUM_UARTS) return; +        _states[which].dst.port = 0; +    } + +    //handle a regular blocking UART write +    else{ +        const size_t which = dst.port-_base_port; +        if (which >= MAX_NUM_UARTS) return; +        _states[which].dst = src; +        for (size_t i = 0; i < payload_len; i++){ +            hal_uart_putc((hal_uart_name_t)which, (int)payload[i]); +        } +    } +} + +/*********************************************************************** + * Public init function + **********************************************************************/ +void udp_uart_init(const uint16_t base_port){ +    _base_port = base_port; +    for(size_t i = 0; i < MAX_NUM_UARTS; i++){ +        _states[i].dst.port = 0; //reset to null port +        _states[i].len = 0; +        _states[i].cyc = 0; +        register_udp_listener(_base_port+i, handle_uart_data_packet); +    } +} + +/*********************************************************************** + * Public poll function + **********************************************************************/ +void udp_uart_poll(void){ +    for (size_t i = 0; i < MAX_NUM_UARTS; i++){ +        if (((UDP_UART_MASK) & (1 << i)) == 0) continue; + +        bool newline = false; +        udp_uart_state_t *state = &_states[i]; + +        //read all characters we can without blocking +        for (size_t j = state->len; j < sizeof(_states[0].buf); j++){ +            uint8_t ch = hal_uart_getc_noblock((hal_uart_name_t)i); +            if (ch == 255) break; +            if (ch == '\n' || ch == '\r') newline = true; +            state->buf[j] = ch; +            state->len++; +            state->cyc = 0; //reset idle cycles +        } + +        //nothing in buffer, continue to next uart +        if (state->len == 0) continue; + +        //send out a message if newline or forced flush +        if (newline || state->cyc++ > num_idle_cyc_b4_flush){ +            if (state->dst.port != 0) send_udp_pkt(_base_port+i, state->dst, state->buf, state->len); +            state->len = 0; +            state->cyc = 0; +        } +    } +} diff --git a/firmware/zpu/lib/udp_uart.h b/firmware/zpu/lib/udp_uart.h new file mode 100644 index 000000000..d448e7611 --- /dev/null +++ b/firmware/zpu/lib/udp_uart.h @@ -0,0 +1,36 @@ +/* + * Copyright 2011 Ettus Research LLC + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program.  If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef INCLUDED_UDP_UART_H +#define INCLUDED_UDP_UART_H + +#include <stdint.h> + +/*! + * Initialize the UDP/UART module. + * Registers handler into the network. + * \param base_port the source port for UART0 + */ +void udp_uart_init(const uint16_t base_port); + +/*! + * Polls the UART state machine, + * and sends messages over UDP. + */ +void udp_uart_poll(void); + +#endif /* INCLUDED_UDP_UART_H */  | 
