uart.h
308 lines
| 8.7 KiB
| text/x-c
|
CLexer
r72 | /*------------------------------------------------------------------------------ | |||
-- This file is a part of the libuc, microcontroler library | ||||
-- Copyright (C) 2012, Alexis Jeandet | ||||
-- | ||||
-- This program is free software; you can redistribute it and/or modify | ||||
-- it under the terms of the GNU General Public License as published by | ||||
-- the Free Software Foundation; either version 3 of the License, or | ||||
-- (at your option) any later version. | ||||
-- | ||||
-- This program is distributed in the hope that it will be useful, | ||||
-- but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
-- GNU General Public License for more details. | ||||
-- | ||||
-- You should have received a copy of the GNU General Public License | ||||
-- along with this program; if not, write to the Free Software | ||||
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||||
------------------------------------------------------------------------------- | ||||
-- Author : Alexis Jeandet | ||||
-- Mail : alexis.jeandet@gmail.com | ||||
-------------------------------------------------------------------------------*/ | ||||
/*! \file uart.h | ||||
\brief UART api. | ||||
The uart api gives you a standard way to drive any uart on any processor. | ||||
With this api you will be able to open and configure your UART define the | ||||
associated pins when applicable and read/write on it. Note that depending | ||||
on the target architecture you can have soft or hard UART. | ||||
A simple example to read or writes on uart1. | ||||
\code | ||||
// lets open uart1 | ||||
//config : | ||||
// -parity = No | ||||
// -data bits = 8 | ||||
// -stop bits = 1 | ||||
// -speed = 115200 bauds | ||||
// -TX = PA9 | ||||
// -RX = PA10 | ||||
// -CTS = unused (-1) | ||||
// -RTS = unused (-1) | ||||
uart_t uart = uartopenandconfig(uart1,uartparitynone | uart8bits | uartonestop,115200,PA9,PA10,-1,-1); | ||||
//check that everything is ok | ||||
if(uart==uart1) | ||||
{ | ||||
//to write 0xFF on uart1 | ||||
uartputc(uart,0xFF); | ||||
//you can also send a string on uart1 | ||||
char* buffer="hello"; | ||||
uartputs(uart,buffer); | ||||
//to read a char | ||||
char result = uartgetc(uart); | ||||
} | ||||
\endcode | ||||
*/ | ||||
#ifndef UART_H | ||||
#define UART_H | ||||
#include <streamdevices.h> | ||||
#ifdef __cplusplus | ||||
extern "C" { | ||||
#endif | ||||
r94 | /*! | |||
r72 | * @brief uart handle | |||
* | ||||
* uart_t is the handle type you will use for all uart related functions. | ||||
*/ | ||||
typedef int uart_t; | ||||
#define uart1 0 ///< uart1 | ||||
#define uart2 1 ///< uart2 | ||||
#define uart3 2 ///< uart3 | ||||
#define uart4 3 ///< uart4 | ||||
#define uart5 4 ///< uart5 | ||||
#define uart6 5 ///< uart6 | ||||
#define uart7 6 ///< uart7 | ||||
#define uart8 7 ///< uart8 | ||||
#define uart9 8 ///< uart9 | ||||
#ifndef DOXYGEN_SHOULD_SKIP_THIS | ||||
#define UARTPARITYMASK 0x3 | ||||
#define UARTBITSMASK 0xC | ||||
#define UARTSTOPBITSMASK 0x70 | ||||
#endif | ||||
/** | ||||
* @brief Uart parity enum | ||||
* | ||||
* Use uartparity_t values to configure the parity of your UART port with | ||||
* uartsetparity function or uartsetconfig. Don't try to use numerical values | ||||
* directly! | ||||
*/ | ||||
typedef enum uartparity_t | ||||
{ | ||||
uartparitynone = 0x1, /**< No parity */ | ||||
uartparityeven = 0x2, /**< Even parity */ | ||||
uartparityodd = 0x3 /**< Odd parity */ | ||||
}uartparity_t; | ||||
/** | ||||
* @brief Uart data bits enum | ||||
* | ||||
* Use uartbits_t values to configure the number of data bits of your UART port with | ||||
* uartsetdatabits function or uartsetconfig. Don't try to use numerical values | ||||
* directly! | ||||
*/ | ||||
typedef enum uartbits_t | ||||
{ | ||||
uart7bits = 0x4, /**< 7 data bits communication */ | ||||
uart8bits = 0x8, /**< 8 data bits communication */ | ||||
uart9bits = 0xC /**< 9 data bits communication */ | ||||
}uartbits_t; | ||||
/** | ||||
* @brief Uart stop bits enum | ||||
* | ||||
* Use uartstopbits_t values to configure the number of stop bits of your UART port with | ||||
* uartsetstopbits function or uartsetconfig. Don't try to use numerical values | ||||
* directly! | ||||
*/ | ||||
typedef enum uartstopbits_t | ||||
{ | ||||
uarthalfstop = 0x10, /**< 0.5 stop bits communication */ | ||||
uartonestop = 0x20, /**< 1 stop bits communication */ | ||||
uartonehalfstop = 0x30, /**< 1.5 stop bits communication */ | ||||
uarttwostop = 0x40 /**< 2 stop bits communication */ | ||||
}uartstopbits_t; | ||||
r94 | /*! | |||
This function opens the given uart, it should turn it ON iff needed and enable it. | ||||
@param count The uart identifier uart1 for example | ||||
@return The uart identifier on success or -1 if it fails. | ||||
r72 | */ | |||
extern uart_t uartopen(int count); | ||||
/** | ||||
* @brief Uart open and configure function | ||||
* | ||||
* This function call uartopen and configure the UART with the given parameters. | ||||
* @param count The uart identifier uart1 for example | ||||
* @param cfg The configuration of the UART, build from an or mask. | ||||
* @param speed The target speed of the UART in bauds, the real speed depend on the implementation | ||||
* and the target. | ||||
* @param TXpin The TXpin look gpio api. | ||||
* @param RXpin The RXpin look gpio api. | ||||
* @param RTSpin The RTSpin look gpio api. | ||||
* @param CTSpin The CTSpin look gpio api. | ||||
* @return The uart identifier on success or -1 if it fails. | ||||
* @sa uartclose(uart_t uart), uartopenandconfig(int count ,uint32_t cfg,uint32_t speed,uint32_t TXpin,uint32_t RXpin,uint32_t RTSpin,uint32_t CTSpin), uartsetconfig(uart_t uart,uint32_t cfg,uint32_t speed) | ||||
*/ | ||||
extern uart_t uartopenandconfig(int count ,uint32_t cfg,uint32_t speed,uint32_t TXpin,uint32_t RXpin,uint32_t RTSpin,uint32_t CTSpin); | ||||
/** | ||||
* @brief Uart close function | ||||
* | ||||
* This function should at least reset the UART configuration. | ||||
* @param uart The uart identifier uart1 for example. | ||||
* @return 1 on success or -1 on error. | ||||
*/ | ||||
extern int uartclose(uart_t uart); | ||||
/** | ||||
* @brief uart set pins function | ||||
* | ||||
* This function sets the Uart pins, you have to check for each target that the pins you selected | ||||
* are compatible with the desired function. | ||||
* @param uart The uart identifier uart1 for example. | ||||
* @param TXpin The TXpin look gpio api. | ||||
* @param RXpin The RXpin look gpio api. | ||||
* @param RTSpin The RTSpin look gpio api. | ||||
* @param CTSpin The CTSpin look gpio api. | ||||
* @return 1 on success or -1 on error. | ||||
* @sa uartopen(int count) | ||||
*/ | ||||
extern int uartsetpins(uart_t uart,uint32_t TXpin,uint32_t RXpin,uint32_t RTSpin,uint32_t CTSpin); | ||||
/** | ||||
* @brief Enable uart functionnable | ||||
* | ||||
*This function enables given uart, it makes sens on target where you can enable or disable | ||||
*the uart. | ||||
* @param uart The uart identifier uart1 for example. | ||||
* @return 1 on success or -1 on error. | ||||
* @sa uartenable(uart_t uart) | ||||
*/ | ||||
extern int uartenable(uart_t uart); | ||||
/** | ||||
* @brief Disable uart function | ||||
* | ||||
*This function disables given uart, it makes sens on target where you can enable or disable | ||||
*the uart. | ||||
* @param uart The uart identifier uart1 for example. | ||||
* @return 1 on success or -1 on error. | ||||
*/ | ||||
extern int uartdisable(uart_t uart); | ||||
/** | ||||
* @brief uart set configuration function | ||||
* | ||||
* This function configure the given uart. | ||||
* @param uart The uart identifier uart1 for example. | ||||
* @param cfg | ||||
* @param speed | ||||
* @return | ||||
*/ | ||||
extern int uartsetconfig(uart_t uart,uint32_t cfg,uint32_t speed); | ||||
/** | ||||
* @brief uartsetspeed | ||||
* @param uart The uart identifier uart1 for example. | ||||
* @param speed | ||||
* @return | ||||
*/ | ||||
extern int uartsetspeed(uart_t uart,uint32_t speed); | ||||
/** | ||||
* @brief uartsetparity | ||||
* @param uart The uart identifier uart1 for example. | ||||
* @param parity | ||||
* @return | ||||
*/ | ||||
extern int uartsetparity(uart_t uart,uartparity_t parity); | ||||
/** | ||||
* @brief uartsetdatabits | ||||
* @param uart The uart identifier uart1 for example. | ||||
* @param databits | ||||
* @return | ||||
*/ | ||||
extern int uartsetdatabits(uart_t uart,uartbits_t databits); | ||||
/** | ||||
* @brief uartsetstopbits | ||||
* @param uart The uart identifier uart1 for example. | ||||
* @param stopbits | ||||
* @return | ||||
*/ | ||||
extern int uartsetstopbits(uart_t uart,uartstopbits_t stopbits); | ||||
/** | ||||
* @brief uartputc | ||||
* @param uart The uart identifier uart1 for example. | ||||
* @param c | ||||
* @return | ||||
*/ | ||||
extern int uartputc(uart_t uart,char c); | ||||
/** | ||||
* @brief uartgetc | ||||
* @param uart The uart identifier uart1 for example. | ||||
* @return | ||||
*/ | ||||
extern char uartgetc(uart_t uart); | ||||
/** | ||||
* @brief uartputs | ||||
* @param uart The uart identifier uart1 for example. | ||||
* @param s | ||||
* @return | ||||
*/ | ||||
extern int uartputs(uart_t uart,char* s); | ||||
/** | ||||
* @brief uartgets | ||||
* @param uart The uart identifier uart1 for example. | ||||
* @param s | ||||
* @return | ||||
*/ | ||||
extern int uartgets(uart_t uart,char* s); | ||||
/** | ||||
* @brief uartputnc | ||||
* @param uart The uart identifier uart1 for example. | ||||
* @param c | ||||
* @param n | ||||
* @return | ||||
*/ | ||||
extern int uartputnc(uart_t uart,char* c,int n); | ||||
/** | ||||
* @brief uartgetnc | ||||
* @param uart The uart identifier uart1 for example. | ||||
* @param c | ||||
* @param n | ||||
* @return | ||||
*/ | ||||
extern int uartgetnc(uart_t uart,char* c,int n); | ||||
/** | ||||
* @brief uartavailiabledata | ||||
* @param uart The uart identifier uart1 for example. | ||||
* @return | ||||
*/ | ||||
extern int uartavailiabledata(uart_t uart); | ||||
/** | ||||
* @brief uartmkstreamdev | ||||
* @param uart The uart identifier uart1 for example. | ||||
* @param strdev | ||||
* @return | ||||
*/ | ||||
extern int uartmkstreamdev(uart_t uart,streamdevice* strdev); | ||||
#ifdef __cplusplus | ||||
} | ||||
#endif | ||||
#endif //SPI_H | ||||