kendryte-standalone-sdk/lib/drivers/include/uart.h

210 lines
4.5 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/* Copyright 2018 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file
* @brief Universal Asynchronous Receiver/Transmitter (UART)
*
* The UART peripheral supports the following features:
*
* - 8-N-1 and 8-N-2 formats: 8 data bits, no parity bit, 1 start
* bit, 1 or 2 stop bits
*
* - 8-entry transmit and receive FIFO buffers with programmable
* watermark interrupts
*
* - 16× Rx oversampling with 2/3 majority voting per bit
*
* The UART peripheral does not support hardware flow control or
* other modem control signals, or synchronous serial data
* tranfesrs.
*
*
*/
#ifndef _DRIVER_APBUART_H
#define _DRIVER_APBUART_H
#include <stdint.h>
#include "platform.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum
{
UART_DEV1 = 0,
UART_DEV2,
UART_DEV3,
} uart_dev_t;
typedef struct
{
union
{
volatile uint32_t RBR;
volatile uint32_t DLL;
volatile uint32_t THR;
};
union
{
volatile uint32_t DLH;
volatile uint32_t IER;
};
union
{
volatile uint32_t FCR;
volatile uint32_t IIR;
};
volatile uint32_t LCR;
volatile uint32_t MCR;
volatile uint32_t LSR;
volatile uint32_t MSR;
volatile uint32_t SCR;
volatile uint32_t LPDLL;
volatile uint32_t LPDLH;
volatile uint32_t reserve[18];
volatile uint32_t FAR;
volatile uint32_t TFR;
volatile uint32_t RFW;
volatile uint32_t USR;
volatile uint32_t TFL;
volatile uint32_t RFL;
volatile uint32_t SRR;
volatile uint32_t SRTS;
volatile uint32_t SBCR;
volatile uint32_t SDMAM;
volatile uint32_t SFE;
volatile uint32_t SRT;
volatile uint32_t STET;
volatile uint32_t HTX;
volatile uint32_t DMASA;
volatile uint32_t TCR;
volatile uint32_t DE_EN;
volatile uint32_t RE_EN;
volatile uint32_t DET;
volatile uint32_t TAT;
volatile uint32_t DLF;
volatile uint32_t RAR;
volatile uint32_t TAR;
volatile uint32_t LCR_EXT;
volatile uint32_t R[5];
volatile uint32_t CPR;
volatile uint32_t UCV;
volatile uint32_t CTR;
} uart_t;
typedef enum
{
UART_BITWIDTH_5BIT = 0,
UART_BITWIDTH_6BIT,
UART_BITWIDTH_7BIT,
UART_BITWIDTH_8BIT,
} uart_bitwidth_t;
typedef enum
{
UART_STOPBIT_1 = 0,
UART_STOPBIT_1P5OR2,
} uart_stopbit_t;
typedef enum
{
UART_PORITY_DISABLE = 0,
UART_PORITY_ODD = 1,
UART_PORITY_EVEN = 3
} uart_pority_t;
typedef struct
{
uint32_t baudrate;
uart_bitwidth_t bitwidth;
uart_stopbit_t stopbit;
uart_pority_t pority;
uint32_t is_hw_flow_en;
} uart_info_t;
typedef enum
{
DISABLE = 0,
ENABLE,
} uart_rede_sel;
typedef enum
{
UART_STOP_1,
UART_STOP_1_5,
UART_STOP_2
} uart_stopbit;
typedef enum
{
UART_PARITY_None,
UART_PARITY_Odd,
UART_PARITY_Even
} uart_parity;
/**
* @brief Send data from uart
*
* @param[in] channel Uart index
* @param[in] buffer The data be transfer
* @param[in] len The data length
*
* @return Transfer length
*/
int uart_write(uint8_t channel, const char* buffer, size_t len);
/**
* @brief Read data from uart
*
* @param[in] channel Uart index
* @param[in] buffer The Data received
* @param[in] len Receive length
*
* @return Receive length
*/
int uart_read(uint8_t channel, char* buffer, size_t len);
/**
* @brief Init uart
*
* @param[in] channel Uart index
*
*/
void uartapb_init(uint8_t channel);
/**
* @brief Set uart param
*
* @param[in] channel Uart index
* @param[in] baud_rate Baudrate
* @param[in] data_width Data width
* @param[in] stopbit Stop bit
* @param[in] parity Odd Even parity
*
*/
void uart_config(uint8_t channel, size_t baud_rate, size_t data_width, uart_stopbit stopbit, uart_parity parity);
#ifdef __cplusplus
}
#endif
#endif /* _DRIVER_APBUART_H */