Skip to content

Commit

Permalink
[SQUASH ME] ng_slip: make uart device descriptor public
Browse files Browse the repository at this point in the history
  • Loading branch information
miri64 committed Mar 24, 2015
1 parent ba23954 commit e386262
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 67 deletions.
36 changes: 28 additions & 8 deletions sys/include/net/ng_slip.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,44 @@ typedef uint8_t uart_t;
#endif

/**
* Initializes a new @ref ng_net_slip control thread for UART device @p uart.
* @brief Descriptor for the UART interface.
*/
typedef struct {
uart_t uart; /**< the UART interface */
ringbuffer_t *in_buf; /**< input buffer */
ringbuffer_t *out_buf; /**< output buffer */
/**
* @{
* @name Internal parameters
* @brief These will be overwritten on ng_slip_init()
* @internal
*/
uint32_t in_bytes; /**< the number of bytes received of a currently
* incoming packet */
uint16_t in_esc; /**< receiver is in escape mode */
kernel_pid_t slip_pid; /**< the PID of preinitialized */
/**
* @}
*/
} ng_slip_dev_t;

/**
* Initializes a new @ref net_ng_slip control thread for UART device @p uart.
*
* @param[in] priority The priority for the thread housing the SLIP instance.
* @param[in] uart The (uninitialized) UART device.
* @param[in] dev A preinitialized device descriptor for the UART
* @param[in] baudrate Symbole rate for the UART device.
* @param[in] in_buf Ringbuffer to store the incoming data from the UART in.
* Must be able to hold at least one L3 packet.
* @param[in] out_buf Ringbuffer to store the outgoing data for the UART in.
*
* @return PID of SLIP thread on success
* @return -EEXIST, if there is already an instance of SLIP running
* @return -EFAULT, if slip_dev_t::in_buf or slip_dev_t::out_buf of @p dev
* was NULL.
* @return -EINVAL, if @p priority is greater than or equal to
* @ref SCHED_PRIO_LEVELS
* @return -ENODEV, if slip_dev_t::uart of @p dev was no valid UART.
* @return -ENOTSUP, if board does not implement @ref driver_periph_uart
* @return -EOVERFLOW, if there are too many threads running already
*/
kernel_pid_t ng_slip_init(char priority, uart_t uart, uint32_t baudrate,
ringbuffer_t *in_buf, ringbuffer_t *out_buf);
kernel_pid_t ng_slip_init(char priority, ng_slip_dev_t *dev, uint32_t baudrate);

#ifdef __cplusplus
}
Expand Down
107 changes: 48 additions & 59 deletions sys/net/link_layer/ng_slip/ng_slip.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,9 @@
#define _SLIP_NAME "SLIP"
#define _SLIP_MSG_QUEUE_SIZE (8U)

/* descriptor for a slip interface's uart */
typedef struct {
kernel_pid_t slip_pid; /* the PID of the SLIP thread */
uint32_t in_bytes; /* the number of bytes received of a currently
* incoming packet */
uint8_t in_esc; /* receiver is in escape mode */
ringbuffer_t *in_buf; /* input buffer */
ringbuffer_t *out_buf; /* output buffer */
} _slip_uart_t;

#define _SLIP_UART(arg) ((_slip_uart_t *)arg)
#define _SLIP_DEV(arg) ((ng_slip_dev_t *)arg)

static char _slip_stack[_SLIP_STACK_SIZE];
static _slip_uart_t _slip_uarts[UART_NUMOF];

/* UART callbacks */
static void _slip_rx_cb(void *arg, char data)
Expand All @@ -66,27 +55,27 @@ static void _slip_rx_cb(void *arg, char data)
msg_t msg;

msg.type = _SLIP_MSG_TYPE;
msg.content.value = _SLIP_UART(arg)->in_bytes;
msg.content.value = _SLIP_DEV(arg)->in_bytes;

msg_send_int(&msg, _SLIP_UART(arg)->slip_pid);
msg_send_int(&msg, _SLIP_DEV(arg)->slip_pid);

_SLIP_UART(arg)->in_bytes = 0;
_SLIP_DEV(arg)->in_bytes = 0;
}

if (_SLIP_UART(arg)->in_esc) {
_SLIP_UART(arg)->in_esc = 0;
if (_SLIP_DEV(arg)->in_esc) {
_SLIP_DEV(arg)->in_esc = 0;

switch (data) {
case (_SLIP_END_ESC):
if (ringbuffer_add_one(_SLIP_UART(arg)->in_buf, _SLIP_END) < 0) {
_SLIP_UART(arg)->in_bytes++;
if (ringbuffer_add_one(_SLIP_DEV(arg)->in_buf, _SLIP_END) < 0) {
_SLIP_DEV(arg)->in_bytes++;
}

break;

case (_SLIP_ESC_ESC):
if (ringbuffer_add_one(_SLIP_UART(arg)->in_buf, _SLIP_ESC) < 0) {
_SLIP_UART(arg)->in_bytes++;
if (ringbuffer_add_one(_SLIP_DEV(arg)->in_buf, _SLIP_ESC) < 0) {
_SLIP_DEV(arg)->in_bytes++;
}

break;
Expand All @@ -96,28 +85,28 @@ static void _slip_rx_cb(void *arg, char data)
}
}
else if (data == _SLIP_ESC) {
_SLIP_UART(arg)->in_esc = 1;
_SLIP_DEV(arg)->in_esc = 1;
}
else {
if (ringbuffer_add_one(_SLIP_UART(arg)->in_buf, data) < 0) {
_SLIP_UART(arg)->in_bytes++;
if (ringbuffer_add_one(_SLIP_DEV(arg)->in_buf, data) < 0) {
_SLIP_DEV(arg)->in_bytes++;
}
}
}

int _slip_tx_cb(void *arg)
{
if (_SLIP_UART(arg)->out_buf->avail > 0) {
char c = (char)ringbuffer_get_one(_SLIP_UART(arg)->out_buf);
uart_write((uart_t)(_SLIP_UART(arg) - _slip_uarts), c);
if (_SLIP_DEV(arg)->out_buf->avail > 0) {
char c = (char)ringbuffer_get_one(_SLIP_DEV(arg)->out_buf);
uart_write((uart_t)(_SLIP_DEV(arg)->uart), c);
return 1;
}

return 0;
}

/* SLIP receive handler */
static void _slip_receive(uart_t uart, size_t bytes)
static void _slip_receive(ng_slip_dev_t *dev, size_t bytes)
{
ng_netif_hdr_t *hdr;
ng_netreg_entry_t *sendto;
Expand All @@ -143,7 +132,7 @@ static void _slip_receive(uart_t uart, size_t bytes)
ng_netif_hdr_init(hdr, 0, 0);
hdr->if_pid = thread_getpid();

if (ringbuffer_get(_slip_uarts[uart].in_buf, pkt->data, bytes) != bytes) {
if (ringbuffer_get(dev->in_buf, pkt->data, bytes) != bytes) {
DEBUG("slip: could not read %zu bytes from ringbuffer\n", bytes);
ng_pktbuf_release(pkt);
return;
Expand Down Expand Up @@ -173,14 +162,14 @@ static void _slip_receive(uart_t uart, size_t bytes)
}
}

static inline void _slip_send_char(uart_t uart, char c)
static inline void _slip_send_char(ng_slip_dev_t *dev, char c)
{
ringbuffer_add_one(_slip_uarts[uart].out_buf, c);
uart_tx_begin(uart);
ringbuffer_add_one(dev->out_buf, c);
uart_tx_begin(dev->uart);
}

/* SLIP send handler */
static void _slip_send(uart_t uart, ng_pktsnip_t *pkt)
static void _slip_send(ng_slip_dev_t *dev, ng_pktsnip_t *pkt)
{
ng_pktsnip_t *ptr;

Expand All @@ -194,18 +183,18 @@ static void _slip_send(uart_t uart, ng_pktsnip_t *pkt)
switch (data[i]) {
case _SLIP_END:
DEBUG("slip: encountered END byte on send: stuff with ESC\n");
_slip_send_char(uart, _SLIP_ESC);
_slip_send_char(uart, _SLIP_END_ESC);
_slip_send_char(dev, _SLIP_ESC);
_slip_send_char(dev, _SLIP_END_ESC);
break;

case _SLIP_ESC:
DEBUG("slip: encountered ESC byte on send: stuff with ESC\n");
_slip_send_char(uart, _SLIP_ESC);
_slip_send_char(uart, _SLIP_ESC_ESC);
_slip_send_char(dev, _SLIP_ESC);
_slip_send_char(dev, _SLIP_ESC_ESC);
break;

default:
_slip_send_char(uart, data[i]);
_slip_send_char(dev, data[i]);

break;
}
Expand All @@ -214,14 +203,14 @@ static void _slip_send(uart_t uart, ng_pktsnip_t *pkt)
ptr = ptr->next;
}

_slip_send_char(uart, _SLIP_END);
_slip_send_char(dev, _SLIP_END);

ng_pktbuf_release(pkt);
}

void *_slip(void *args)
{
uart_t uart = *((uart_t *)args);
ng_slip_dev_t *dev = _SLIP_DEV(args);
msg_t msg, reply, msg_q[_SLIP_STACK_SIZE];

msg_init_queue(msg_q, _SLIP_STACK_SIZE);
Expand All @@ -236,12 +225,12 @@ void *_slip(void *args)
switch (msg.type) {
case _SLIP_MSG_TYPE:
DEBUG("slip: incoming message from UART in buffer\n");
_slip_receive(uart, (size_t)msg.content.value);
_slip_receive(dev, (size_t)msg.content.value);
break;

case NG_NETAPI_MSG_TYPE_SND:
DEBUG("slip: NG_NETAPI_MSG_TYPE_SND received\n");
_slip_send(uart, (ng_pktsnip_t *)msg.content.ptr);
_slip_send(dev, (ng_pktsnip_t *)msg.content.ptr);
break;

case NG_NETAPI_MSG_TYPE_GET:
Expand All @@ -256,43 +245,43 @@ void *_slip(void *args)
}
}

kernel_pid_t ng_slip_init(char priority, uart_t uart, uint32_t baudrate,
ringbuffer_t *in_buf, ringbuffer_t *out_buf)
kernel_pid_t ng_slip_init(char priority, ng_slip_dev_t *dev, uint32_t baudrate)
{
kernel_pid_t res;

if (_slip_uarts[uart].slip_pid != KERNEL_PID_UNDEF) {
DEBUG("slip: slip thread for UART_%d exists already\n", uart);
return -EEXIST;
if ((dev->uart < UART_0) || (dev->uart >= UART_NUMOF)) {
DEBUG("slip: dev->uart == %i is no valid UART\n", uart);
return -ENODEV;
}

_slip_uarts[uart].in_bytes = 0;
_slip_uarts[uart].in_esc = 0;
_slip_uarts[uart].in_buf = in_buf;
_slip_uarts[uart].out_buf = out_buf;
if ((dev->in_buf == NULL) || (dev->out_buf == NULL)) {
DEBUG("slip: dev->in_buf or dev->out_buf was NULL\n");
return -EFAULT;
}

DEBUG("slip: initialize UART_%d\n", uart);
uart_init(uart, baudrate, _slip_rx_cb, _slip_tx_cb, _slip_uarts + uart);
dev->in_bytes = 0;
dev->in_esc = 0;

res = thread_create(_slip_stack, _SLIP_STACK_SIZE, priority, CREATE_STACKTEST,
_slip, &uart, _SLIP_NAME);
DEBUG("slip: start SLIP thread: %d\n", res);
_slip, dev, _SLIP_NAME);
DEBUG("slip: started SLIP thread: %d\n", res);

DEBUG("slip: initialize UART_%d\n", uart);
uart_init(dev->uart, baudrate, _slip_rx_cb, _slip_tx_cb, dev);

if (res > 0) {
_slip_uarts[uart].slip_pid = res;
dev->slip_pid = res;
}

return res;
}

#else /* UART_NUMOF */
kernel_pid_t ng_slip_init(char priority, uart_t uart, uint32_t baudrate,
ringbuffer_t *in_buf)
kernel_pid_t ng_slip_init(char priority, ng_slip_dev_t *dev, uint32_t baudrate)
{
(void)priority;
(void)uart;
(void)baudrate;
(void)in_buf;
return -ENOTSUP;
}
#endif /* UART_NUMOF */
Expand Down

0 comments on commit e386262

Please sign in to comment.