Contiki 3.x
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
55 static uint8_t rx_buffer[1];
56 
57 static int (*uart0_input_handler)(unsigned char c);
58 
59 static struct ringbuf txbuf;
60 static uint8_t txbuf_data[TXBUFSIZE];
61 
62 /*---------------------------------------------------------------------------*/
63 static void
64 uart_event_handler(nrf_drv_uart_event_t * p_event, void * p_context)
65 {
66  ENERGEST_ON(ENERGEST_TYPE_IRQ);
67 
68  if (p_event->type == NRF_DRV_UART_EVT_RX_DONE) {
69  if (uart0_input_handler != NULL) {
70  uart0_input_handler(p_event->data.rxtx.p_data[0]);
71  }
72  (void)nrf_drv_uart_rx(rx_buffer, 1);
73  } else if (p_event->type == NRF_DRV_UART_EVT_TX_DONE) {
74  if (ringbuf_elements(&txbuf) > 0) {
75  uint8_t c = ringbuf_get(&txbuf);
76  nrf_drv_uart_tx(&c, 1);
77  }
78  }
79 
80  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
81 }
82 /*---------------------------------------------------------------------------*/
83 void
84 uart0_set_input(int (*input)(unsigned char c))
85 {
86  uart0_input_handler = input;
87 }
88 /*---------------------------------------------------------------------------*/
89 void
90 uart0_writeb(unsigned char c)
91 {
92  if (nrf_drv_uart_tx(&c, 1) == NRF_ERROR_BUSY) {
93  while (ringbuf_put(&txbuf, c) == 0) {
94  __WFE();
95  }
96  }
97 }
98 /*---------------------------------------------------------------------------*/
99 /**
100  * Initialize the RS232 port.
101  *
102  */
103 void
104 uart0_init(unsigned long ubr)
105 {
106  nrf_drv_uart_config_t config = NRF_DRV_UART_DEFAULT_CONFIG;
107  ret_code_t retcode = nrf_drv_uart_init(&config, uart_event_handler);
108  APP_ERROR_CHECK(retcode);
109 
110  ringbuf_init(&txbuf, txbuf_data, sizeof(txbuf_data));
111 
112  nrf_drv_uart_rx_enable();
113  nrf_drv_uart_rx(rx_buffer, 1);
114 }
115 /**
116  * @}
117  * @}
118  */
int ringbuf_get(struct ringbuf *r)
Get a byte from the ring buffer.
Definition: ringbuf.c:80
void uart0_init(unsigned long ubr)
Initialize the RS232 port.
Definition: uart0.c:104
int ringbuf_put(struct ringbuf *r, uint8_t c)
Insert a byte into the ring buffer.
Definition: ringbuf.c:53
static void input(void)
Process a received 6lowpan packet.
Definition: sicslowpan.c:1503
int ringbuf_elements(struct ringbuf *r)
Get the number of elements currently in the ring buffer.
Definition: ringbuf.c:119
#define NULL
The null pointer.
void ringbuf_init(struct ringbuf *r, uint8_t *dataptr, uint8_t size)
Initialize a ring buffer.
Definition: ringbuf.c:44
Structure that holds the state of a ring buffer.
Definition: ringbuf.h:68
void uart0_set_input(int(*input)(unsigned char c))
Definition: uart0.c:84
Header file for the ring buffer library