Contiki-NG
uart0.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020, Toshiba BRIL
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 copyright holder nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30/**
31 * \addtogroup nrf52840-dev Device drivers
32 * @{
33 *
34 * \addtogroup nrf52840-uart UART driver
35 * @{
36 *
37 * \file
38 * A header file for Contiki compatible UART driver.
39 */
40#include "contiki.h"
41#include "nrf.h"
42#include "nrf_uart.h"
43#include "nrf_gpio.h"
44#include "dev/uart0.h"
45
46#include <stdlib.h>
47#include <stdint.h>
48/*---------------------------------------------------------------------------*/
49static int (*input_handler)(unsigned char c);
50
51#define UART_INSTANCE NRF_UART0
52/*---------------------------------------------------------------------------*/
53#define TX_PIN NRF_UART0_TX_PIN
54#define RX_PIN NRF_UART0_RX_PIN
55/*---------------------------------------------------------------------------*/
56void
57uart0_set_input(int (*input)(unsigned char c))
58{
59 input_handler = input;
60
61 if(input != NULL) {
62 nrf_uart_int_enable(UART_INSTANCE, NRF_UART_INT_MASK_RXDRDY);
63 NVIC_ClearPendingIRQ(UARTE0_UART0_IRQn);
64 NVIC_EnableIRQ(UARTE0_UART0_IRQn);
65 nrf_uart_task_trigger(UART_INSTANCE, NRF_UART_TASK_STARTRX);
66 } else {
67 nrf_uart_int_disable(UART_INSTANCE, NRF_UART_INT_MASK_RXDRDY);
68 NVIC_ClearPendingIRQ(UARTE0_UART0_IRQn);
69 NVIC_DisableIRQ(UARTE0_UART0_IRQn);
70 nrf_uart_task_trigger(UART_INSTANCE, NRF_UART_TASK_STOPRX);
71 }
72}
73/*---------------------------------------------------------------------------*/
74void
75uart0_writeb(unsigned char c)
76{
77 nrf_uart_txd_set(UART_INSTANCE, c);
78
79 /* Block if previous TX is ongoing */
80 while(nrf_uart_event_check(UART_INSTANCE, NRF_UART_EVENT_TXDRDY) == false);
81 nrf_uart_event_clear(UART_INSTANCE, NRF_UART_EVENT_TXDRDY);
82}
83/*---------------------------------------------------------------------------*/
84void
85uart0_init(unsigned long ubr)
86{
87 nrf_uart_disable(UART_INSTANCE);
88 nrf_gpio_cfg_output(TX_PIN);
89 nrf_gpio_pin_set(TX_PIN);
90 nrf_gpio_cfg_input(RX_PIN, NRF_GPIO_PIN_NOPULL);
91
92 nrf_uart_baudrate_set(UART_INSTANCE, UART0_CONF_BAUD_RATE);
93 nrf_uart_configure(UART_INSTANCE, NRF_UART_PARITY_EXCLUDED,
94 NRF_UART_HWFC_DISABLED);
95 nrf_uart_txrx_pins_set(UART_INSTANCE, TX_PIN, RX_PIN);
96 nrf_uart_event_clear(UART_INSTANCE, NRF_UART_EVENT_TXDRDY);
97 nrf_uart_enable(UART_INSTANCE);
98 nrf_uart_task_trigger(UART_INSTANCE, NRF_UART_TASK_STARTTX);
99}
100/*---------------------------------------------------------------------------*/
101void
102UARTE0_UART0_IRQHandler(void)
103{
104 nrf_uart_event_clear(UART_INSTANCE, NRF_UART_EVENT_RXDRDY);
105 input_handler(nrf_uart_rxd_get(UART_INSTANCE));
106}
107/*---------------------------------------------------------------------------*/
108/**
109 * @}
110 * @}
111 */
#define UART0_CONF_BAUD_RATE
Default UART0 baud rate.
Definition: cc2538-conf.h:110
void uart0_init(unsigned long ubr)
Initalize the RS232 port.
Definition: uart0.c:85
static void input(void)
Process a received 6lowpan packet.
Definition: sicslowpan.c:1833