Contiki-NG
uart0.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, Nordic Semiconductor
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30/**
31 * \addtogroup nrf52832-dev Device drivers
32 * @{
33 *
34 * \addtogroup nrf52832-uart UART driver
35 * @{
36 *
37 * \file
38 * Contiki compatible UART driver.
39 * \author
40 * Wojciech Bober <wojciech.bober@nordicsemi.no>
41 */
42#include <stdlib.h>
43#include "nrf.h"
44#include "nrf_drv_config.h"
45#include "nrf_drv_uart.h"
46#include "app_util_platform.h"
47#include "app_error.h"
48
49#include "contiki.h"
50#include "dev/uart0.h"
51#include "dev/watchdog.h"
52#include "lib/ringbuf.h"
53
54#define TXBUFSIZE 128
55static uint8_t rx_buffer[1];
56
57static int (*uart0_input_handler)(unsigned char c);
58
59static struct ringbuf txbuf;
60static uint8_t txbuf_data[TXBUFSIZE];
61
62/*---------------------------------------------------------------------------*/
63static void
64uart_event_handler(nrf_drv_uart_event_t * p_event, void * p_context)
65{
66 if (p_event->type == NRF_DRV_UART_EVT_RX_DONE) {
67 if (uart0_input_handler != NULL) {
68 uart0_input_handler(p_event->data.rxtx.p_data[0]);
69 }
70 (void)nrf_drv_uart_rx(rx_buffer, 1);
71 } else if (p_event->type == NRF_DRV_UART_EVT_TX_DONE) {
72 if (ringbuf_elements(&txbuf) > 0) {
73 uint8_t c = ringbuf_get(&txbuf);
74 nrf_drv_uart_tx(&c, 1);
75 }
76 }
77}
78/*---------------------------------------------------------------------------*/
79void
80uart0_set_input(int (*input)(unsigned char c))
81{
82 uart0_input_handler = input;
83}
84/*---------------------------------------------------------------------------*/
85void
86uart0_writeb(unsigned char c)
87{
88 if (nrf_drv_uart_tx(&c, 1) == NRF_ERROR_BUSY) {
89 while (ringbuf_put(&txbuf, c) == 0) {
90 __WFE();
91 }
92 }
93}
94/*---------------------------------------------------------------------------*/
95/**
96 * Initialize the RS232 port.
97 *
98 */
99void
100uart0_init(unsigned long ubr)
101{
102 nrf_drv_uart_config_t config = NRF_DRV_UART_DEFAULT_CONFIG;
103 ret_code_t retcode = nrf_drv_uart_init(&config, uart_event_handler);
104 APP_ERROR_CHECK(retcode);
105
106 ringbuf_init(&txbuf, txbuf_data, sizeof(txbuf_data));
107
108 nrf_drv_uart_rx_enable();
109 nrf_drv_uart_rx(rx_buffer, 1);
110}
111/**
112 * @}
113 * @}
114 */
void uart0_init(unsigned long ubr)
Initialize the RS232 port.
Definition: uart0.c:100
static int config(int type, int c, nrf_drv_gpiote_pin_t pin)
Configuration function for the button sensor for all buttons.
int ringbuf_get(struct ringbuf *r)
Get a byte from the ring buffer.
Definition: ringbuf.c:80
int ringbuf_put(struct ringbuf *r, uint8_t c)
Insert a byte into the ring buffer.
Definition: ringbuf.c:53
void ringbuf_init(struct ringbuf *r, uint8_t *dataptr, uint8_t size)
Initialize a ring buffer.
Definition: ringbuf.c:44
int ringbuf_elements(struct ringbuf *r)
Get the number of elements currently in the ring buffer.
Definition: ringbuf.c:119
static void input(void)
Process a received 6lowpan packet.
Definition: sicslowpan.c:1858
Header file for the ring buffer library.
Structure that holds the state of a ring buffer.
Definition: ringbuf.h:68