Contiki-NG
Loading...
Searching...
No Matches
uart0-arch.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/
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 cc13xx-cc26xx-uart
32 * @{
33 *
34 * \file
35 * Implementation of UART driver for CC13xx/CC26xx.
36 * \author
37 * Edvard Pettersen <e.pettersen@ti.com>
38 */
39/*---------------------------------------------------------------------------*/
40#include "contiki.h"
41/*---------------------------------------------------------------------------*/
42#include "uart0-arch.h"
43/*---------------------------------------------------------------------------*/
44#include <Board.h>
45
46#include <ti/drivers/UART.h>
47/*---------------------------------------------------------------------------*/
48#include <stdint.h>
49#include <stdbool.h>
50/*---------------------------------------------------------------------------*/
51static UART_Handle uart_handle;
52
53static volatile uart0_input_fxn_t curr_input_cb;
54static unsigned char char_buf;
55
56static bool initialized;
57/*---------------------------------------------------------------------------*/
58static void
59uart0_cb(UART_Handle handle, void *buf, size_t count)
60{
61 /* Simply return if the current callback is NULL. */
62 if(!curr_input_cb) {
63 return;
64 }
65
66 /*
67 * Save the current callback function locally, as it might be overwritten
68 * after calling the callback.
69 */
70 const uart0_input_fxn_t curr_cb = curr_input_cb;
71 curr_cb(char_buf);
72 /*
73 * If curr_input_cb didn't change after the call, do another read.
74 * Else, the uart0_set_callback was called with a different callback pointer
75 * and triggered an another read.
76 */
77 if(curr_cb == curr_input_cb) {
78 UART_read(uart_handle, &char_buf, 1);
79 }
80}
81/*---------------------------------------------------------------------------*/
82void
84{
85 if(initialized) {
86 return;
87 }
88
89 UART_Params uart_params;
90 UART_Params_init(&uart_params);
91
92 uart_params.baudRate = TI_UART_CONF_BAUD_RATE;
93 uart_params.readMode = UART_MODE_CALLBACK;
94 uart_params.writeMode = UART_MODE_BLOCKING;
95 uart_params.readCallback = uart0_cb;
96 uart_params.readDataMode = UART_DATA_TEXT;
97 uart_params.readReturnMode = UART_RETURN_NEWLINE;
98
99 /* No error handling. */
100 uart_handle = UART_open(Board_UART0, &uart_params);
101
102 initialized = true;
103}
104/*---------------------------------------------------------------------------*/
105int_fast32_t
106uart0_write(const void *buf, size_t buf_size)
107{
108 if(!initialized) {
109 return UART_STATUS_ERROR;
110 }
111 return UART_write(uart_handle, buf, buf_size);
112}
113/*---------------------------------------------------------------------------*/
114int_fast32_t
115uart0_write_byte(uint8_t byte)
116{
117 if(!initialized) {
118 return UART_STATUS_ERROR;
119 }
120 return UART_write(uart_handle, &byte, 1);
121}
122/*---------------------------------------------------------------------------*/
123int_fast32_t
124uart0_set_callback(uart0_input_fxn_t input_cb)
125{
126 if(!initialized) {
127 return UART_STATUS_ERROR;
128 }
129
130 if(curr_input_cb == input_cb) {
131 return UART_STATUS_SUCCESS;
132 }
133
134 curr_input_cb = input_cb;
135 if(input_cb) {
136 return UART_read(uart_handle, &char_buf, 1);
137 } else {
138 UART_readCancel(uart_handle);
139 return UART_STATUS_SUCCESS;
140 }
141}
142/*---------------------------------------------------------------------------*/
143/** @} */
int_fast32_t uart0_write_byte(uint8_t byte)
Writes a single byte to the UART interface.
Definition uart0-arch.c:115
int_fast32_t uart0_write(const void *buf, size_t buf_size)
Writes data from a memory buffer to the UART interface.
Definition uart0-arch.c:106
void uart0_init(void)
Initializes the UART driver.
Definition uart0-arch.c:83
int_fast32_t uart0_set_callback(uart0_input_fxn_t input_cb)
Set the callback function for when bytes are received on UART0.
Definition uart0-arch.c:124
static volatile uint64_t count
Num.
Definition clock.c:50
#define TI_UART_CONF_BAUD_RATE
Default UART0 baud rate.
Header file of UART driver for CC13xx/CC26xx.