2014-02-03 19:36:12 +01:00
|
|
|
/*
|
2015-10-19 14:51:20 +02:00
|
|
|
* Copyright (C) 2014-2015 Freie Universität Berlin
|
2014-02-03 19:36:12 +01:00
|
|
|
*
|
2014-08-27 18:47:31 +02:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
2014-02-03 19:36:12 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-10-25 15:37:04 +02:00
|
|
|
* @defgroup driver_periph_uart UART
|
2014-02-03 19:36:12 +01:00
|
|
|
* @ingroup driver_periph
|
|
|
|
* @brief Low-level UART peripheral driver
|
|
|
|
* @{
|
|
|
|
*
|
2014-10-25 15:37:04 +02:00
|
|
|
* @file
|
2015-10-19 14:51:20 +02:00
|
|
|
* @brief Low-level UART peripheral driver interface definition
|
2014-02-03 19:36:12 +01:00
|
|
|
*
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
|
|
|
*/
|
|
|
|
|
2015-03-23 21:07:50 +01:00
|
|
|
#ifndef PERIPH_UART_H
|
|
|
|
#define PERIPH_UART_H
|
2014-02-03 19:36:12 +01:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2015-10-19 14:51:20 +02:00
|
|
|
#include "periph_cpu.h"
|
2014-02-03 19:36:12 +01:00
|
|
|
#include "periph_conf.h"
|
2015-10-19 14:51:20 +02:00
|
|
|
/* TODO: remove once all platforms are ported to this interface */
|
|
|
|
#include "periph/dev_enums.h"
|
2014-02-03 19:36:12 +01:00
|
|
|
|
2014-10-13 15:49:17 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2014-02-03 19:36:12 +01:00
|
|
|
/**
|
2015-10-19 14:51:20 +02:00
|
|
|
* @brief Make sure the number of available UART devices is defined
|
|
|
|
* @{
|
2014-02-03 19:36:12 +01:00
|
|
|
*/
|
2015-10-19 14:51:20 +02:00
|
|
|
#ifndef UART_NUMOF
|
|
|
|
#error "UART_NUMOF undefined for the target platform"
|
2014-02-03 19:36:12 +01:00
|
|
|
#endif
|
2015-10-19 14:51:20 +02:00
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Define default UART type identifier
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
#ifndef HAVE_UART_T
|
|
|
|
typedef unsigned int uart_t;
|
2014-02-03 19:36:12 +01:00
|
|
|
#endif
|
2015-10-19 14:51:20 +02:00
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Default UART undefined value
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
#ifndef UART_UNDEF
|
|
|
|
#define UART_UNDEF (-1)
|
2014-02-03 19:36:12 +01:00
|
|
|
#endif
|
2015-10-19 14:51:20 +02:00
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Default UART device access macro
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
#ifndef UART_DEV
|
|
|
|
#define UART_DEV(x) (x)
|
2014-02-03 19:36:12 +01:00
|
|
|
#endif
|
2015-10-19 14:51:20 +02:00
|
|
|
/** @} */
|
2014-02-03 19:36:12 +01:00
|
|
|
|
2014-07-28 14:45:51 +02:00
|
|
|
/**
|
2015-10-19 14:51:20 +02:00
|
|
|
* @brief Signature for receive interrupt callback
|
2014-07-28 14:45:51 +02:00
|
|
|
*
|
2015-10-19 14:51:20 +02:00
|
|
|
* @param[in] arg context to the callback (optional)
|
2014-07-28 14:45:51 +02:00
|
|
|
* @param[in] data the byte that was received
|
|
|
|
*/
|
|
|
|
typedef void(*uart_rx_cb_t)(void *arg, char data);
|
|
|
|
|
|
|
|
/**
|
2015-10-19 14:51:20 +02:00
|
|
|
* @brief Interrupt context for a UART device
|
|
|
|
* @{
|
2014-07-28 14:45:51 +02:00
|
|
|
*/
|
2015-10-19 14:51:20 +02:00
|
|
|
#ifndef HAVE_UART_ISR_CTX_T
|
|
|
|
typedef struct {
|
|
|
|
uart_rx_cb_t rx_cb; /**< data received interrupt callback */
|
|
|
|
void *arg; /**< argument to both callback routines */
|
|
|
|
} uart_isr_ctx_t;
|
|
|
|
#endif
|
|
|
|
/** @} */
|
2014-02-03 19:36:12 +01:00
|
|
|
|
|
|
|
/**
|
2015-10-19 14:51:20 +02:00
|
|
|
* @brief Initialize a given UART device
|
2014-05-14 10:46:15 +02:00
|
|
|
*
|
2014-02-03 19:36:12 +01:00
|
|
|
* The UART device will be initialized with the following configuration:
|
|
|
|
* - 8 data bits
|
|
|
|
* - no parity
|
|
|
|
* - 1 stop bit
|
2015-10-19 14:51:20 +02:00
|
|
|
* - baudrate as given
|
2014-05-14 10:46:15 +02:00
|
|
|
*
|
2015-10-19 14:51:20 +02:00
|
|
|
* @param[in] uart UART device to initialize
|
|
|
|
* @param[in] baudrate desired baudrate in baud/s
|
|
|
|
* @param[in] rx_cb receive callback, executed in interrupt context once
|
|
|
|
* for every byte that is received (RX buffer filled)
|
|
|
|
* @param[in] arg optional context passed to the callback functions
|
2014-05-14 10:46:15 +02:00
|
|
|
*
|
2014-07-28 14:45:51 +02:00
|
|
|
* @return 0 on success
|
2015-10-19 14:51:20 +02:00
|
|
|
* @return -1 on invalid UART device
|
|
|
|
* @return -2 on inapplicable baudrate
|
|
|
|
* @return -3 on other errors
|
2014-02-03 19:36:12 +01:00
|
|
|
*/
|
2015-10-19 14:51:20 +02:00
|
|
|
int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg);
|
2014-02-03 19:36:12 +01:00
|
|
|
|
|
|
|
/**
|
2015-10-19 14:51:20 +02:00
|
|
|
* @brief Write data from the given buffer to the specified UART device
|
2014-05-14 10:46:15 +02:00
|
|
|
*
|
2015-10-19 14:51:20 +02:00
|
|
|
* This function is blocking, as it will only return after @p len bytes from the
|
|
|
|
* given buffer have been send. The way this data is send is up to the
|
|
|
|
* implementation: active waiting, interrupt driven, DMA, etc.
|
2014-05-14 10:46:15 +02:00
|
|
|
*
|
2015-10-19 14:51:20 +02:00
|
|
|
* @param[in] uart UART device to use for transmission
|
|
|
|
* @param[in] data data buffer to send
|
|
|
|
* @param[in] len number of bytes to send
|
2014-05-14 10:46:15 +02:00
|
|
|
*
|
2014-02-03 19:36:12 +01:00
|
|
|
*/
|
2015-10-19 14:51:20 +02:00
|
|
|
void uart_write(uart_t uart, const uint8_t *data, size_t len);
|
2014-02-03 19:36:12 +01:00
|
|
|
|
2014-07-28 14:45:51 +02:00
|
|
|
/**
|
2015-10-19 14:51:20 +02:00
|
|
|
* @brief Power on the given UART device
|
2014-07-28 14:45:51 +02:00
|
|
|
*
|
|
|
|
* @param[in] uart the UART device to power on
|
|
|
|
*/
|
|
|
|
void uart_poweron(uart_t uart);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Power off the given UART device
|
|
|
|
*
|
|
|
|
* @param[in] uart the UART device to power off
|
|
|
|
*/
|
|
|
|
void uart_poweroff(uart_t uart);
|
|
|
|
|
2014-10-13 15:49:17 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-03-23 21:07:50 +01:00
|
|
|
#endif /* PERIPH_UART_H */
|
2014-02-03 19:36:12 +01:00
|
|
|
/** @} */
|