Initial Release
This commit is contained in:
1
src/host/hardware_uart/CMakeLists.txt
Normal file
1
src/host/hardware_uart/CMakeLists.txt
Normal file
@ -0,0 +1 @@
|
||||
pico_simple_hardware_target(uart)
|
91
src/host/hardware_uart/include/hardware/uart.h
Normal file
91
src/host/hardware_uart/include/hardware/uart.h
Normal file
@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef _HARDWARE_UART_H
|
||||
#define _HARDWARE_UART_H
|
||||
|
||||
#include "pico.h"
|
||||
|
||||
#ifndef PARAM_ASSERTIONS_ENABLED_UART
|
||||
#define PARAM_ASSERTIONS_ENABLED_UART 0
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct uart_inst uart_inst_t;
|
||||
|
||||
extern uart_inst_t * const uart0;
|
||||
extern uart_inst_t * const uart1;
|
||||
#define uart_default uart0
|
||||
|
||||
typedef enum {
|
||||
UART_PARITY_NONE,
|
||||
UART_PARITY_EVEN,
|
||||
UART_PARITY_ODD
|
||||
} uart_parity_t;
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Setup
|
||||
|
||||
// Put the UART into a known state, and enable it. Must be called before other
|
||||
// functions.
|
||||
uint uart_init(uart_inst_t *uart, uint baudrate);
|
||||
|
||||
// Disable the UART if it is no longer used. Must be reinitialised before
|
||||
// being used again.
|
||||
void uart_deinit(uart_inst_t *uart);
|
||||
|
||||
// Set baud rate as close as possible to requested, and return actual rate.
|
||||
uint uart_set_baudrate(uart_inst_t *uart, uint baudrate);
|
||||
|
||||
// cts: enable flow control of TX by clear-to-send input
|
||||
// rts: enable assertion of request-to-send output by RX flow control
|
||||
void uart_set_hw_flow(uart_inst_t *uart, bool cts, bool rts);
|
||||
|
||||
// Configure how the UART serialises and deserialises data on the wire
|
||||
void uart_set_format(uart_inst_t *uart, uint data_bits, uint stop_bits, uart_parity_t parity);
|
||||
|
||||
// Enable the UART's interrupt output. Need to install an interrupt handler first.
|
||||
void uart_set_irq_enables(uart_inst_t *uart, bool rx_has_data, bool tx_needs_data);
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Generic input/output
|
||||
|
||||
// If returns 0, no space is available in the UART to write more data.
|
||||
// If returns nonzero, at least that many bytes can be written without blocking.
|
||||
size_t uart_is_writable(uart_inst_t *uart);
|
||||
|
||||
// If returns 0, no data is available to be read from UART.
|
||||
// If returns nonzero, at least that many bytes can be written without blocking.
|
||||
size_t uart_is_readable(uart_inst_t *uart);
|
||||
|
||||
// Write len bytes directly from src to the UART
|
||||
void uart_write_blocking(uart_inst_t *uart, const uint8_t *src, size_t len);
|
||||
|
||||
// Read len bytes directly from the UART to dst
|
||||
void uart_read_blocking(uart_inst_t *uart, uint8_t *dst, size_t len);
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// UART-specific operations and aliases
|
||||
|
||||
void uart_putc(uart_inst_t *uart, char c);
|
||||
|
||||
void uart_puts(uart_inst_t *uart, const char *s);
|
||||
|
||||
char uart_getc(uart_inst_t *uart);
|
||||
|
||||
// en: assert break condition (TX held low) if true. Clear break condition if false.
|
||||
void uart_set_break(uart_inst_t *uart, bool en);
|
||||
|
||||
void uart_default_tx_wait_blocking();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
118
src/host/hardware_uart/uart.c
Normal file
118
src/host/hardware_uart/uart.c
Normal file
@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "hardware/uart.h"
|
||||
|
||||
#if defined(__unix) || defined(__APPLE__)
|
||||
#define _XOPEN_SOURCE 600 /* for ONLCR */
|
||||
#define __BSD_VISIBLE 1 /* for ONLCR in *BSD */
|
||||
|
||||
#include <unistd.h>
|
||||
#include <termios.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifndef FNONBLOCK
|
||||
#define FNONBLOCK O_NONBLOCK
|
||||
#endif
|
||||
|
||||
struct termios _tty;
|
||||
static tcflag_t _res_oflg = 0;
|
||||
static tcflag_t _res_lflg = 0;
|
||||
|
||||
void _resetty(void) {
|
||||
if (!isatty(STDIN_FILENO))
|
||||
return;
|
||||
|
||||
/* reset tty: */
|
||||
_tty.c_oflag = _res_oflg;
|
||||
_tty.c_lflag = _res_lflg;
|
||||
tcsetattr(STDIN_FILENO, TCSADRAIN, &_tty);
|
||||
}
|
||||
|
||||
void _inittty(void) {
|
||||
if (!isatty(STDIN_FILENO))
|
||||
return;
|
||||
|
||||
/* save tty: */
|
||||
tcgetattr(STDIN_FILENO, &_tty);
|
||||
_res_oflg = _tty.c_oflag;
|
||||
_res_lflg = _tty.c_lflag;
|
||||
|
||||
/* set raw: */
|
||||
_tty.c_lflag &= ~(ICANON | ICRNL);// | ISIG);
|
||||
//_tty.c_oflag &= ~ONLCR;
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &_tty);
|
||||
|
||||
fcntl(STDIN_FILENO, F_SETFL, FNONBLOCK);
|
||||
atexit(_resetty);
|
||||
}
|
||||
|
||||
#else
|
||||
void _inittty() {}
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
bool dummy;
|
||||
} uart_hw_t;
|
||||
|
||||
uart_inst_t *const uart0;
|
||||
uart_inst_t *const uart1;
|
||||
|
||||
static int _nextchar = EOF;
|
||||
|
||||
static bool _peekchar() {
|
||||
if (_nextchar == EOF) {
|
||||
_nextchar = getchar();
|
||||
}
|
||||
return _nextchar != EOF;
|
||||
}
|
||||
|
||||
uint uart_init(uart_inst_t *uart, uint baud_rate) {
|
||||
_inittty();
|
||||
return baud_rate;
|
||||
}
|
||||
|
||||
size_t uart_is_writable(uart_inst_t *uart) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// If returns 0, no data is available to be read from UART.
|
||||
// If returns nonzero, at least that many bytes can be written without blocking.
|
||||
size_t uart_is_readable(uart_inst_t *uart) {
|
||||
return _peekchar() ? 1 : 0;
|
||||
}
|
||||
|
||||
// Write len bytes directly from src to the UART
|
||||
//void uart_write_blocking(uart_inst_t uart, const uint8_t *src, size_t len);
|
||||
|
||||
// Read len bytes directly from the UART to dst
|
||||
//void uart_read_blocking(uart_inst_t uart, uint8_t *dst, size_t len);
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// UART-specific operations and aliases
|
||||
|
||||
void uart_putc(uart_inst_t *uart, char c) {
|
||||
putchar(c);
|
||||
}
|
||||
|
||||
void uart_puts(uart_inst_t *uart, const char *s) {
|
||||
puts(s);
|
||||
}
|
||||
|
||||
char uart_getc(uart_inst_t *uart) {
|
||||
while (!_peekchar()) {
|
||||
tight_loop_contents();
|
||||
}
|
||||
char rc = (char) _nextchar;
|
||||
_nextchar = EOF;
|
||||
return rc;
|
||||
}
|
||||
|
||||
void uart_default_tx_wait_blocking() {
|
||||
|
||||
}
|
Reference in New Issue
Block a user