資源簡(jiǎn)介
ZYNQ7021在Linux下, 串口UART0的實(shí)現(xiàn),可以在PS端直接使用,也可以將UART0引薦通過引腳分配帶EMIO上使用,測(cè)試效果可以查看我的博客。
代碼片段和文件信息
//串口相關(guān)的頭文件??
#include??????/*標(biāo)準(zhǔn)輸入輸出定義*/??
#include?????/*標(biāo)準(zhǔn)函數(shù)庫(kù)定義*/??
#include?????/*Unix?標(biāo)準(zhǔn)函數(shù)定義*/??
#include???
#include?????
#include??????/*文件控制定義*/??
#include????/*PPSIX?終端控制定義*/??
#include??????/*錯(cuò)誤號(hào)定義*/??
#include??
#include?
#include?
//宏定義??
#define?FALSE??-1??
#define?TRUE???0??
/*******************************************************************?
*?名稱:????????????????UART0_Open
*?功能:????????????????打開串口并返回串口設(shè)備文件描述?
*?入口參數(shù):?fd:文件描述符????port?:串口號(hào)(ttyPS0ttyPS1)
*?出口參數(shù):?????????????正確返回為1,錯(cuò)誤返回為0
*******************************************************************/??
int?UART0_Open(int?fdchar*?port)??
{??
????fd?=?open(?port?O_RDWR|O_NOCTTY|O_NDELAY);??//fd?=?-1
????if?(FALSE?==?fd)
????{
????????perror(“Can‘t?Open?Serial?Port“);
????????return(FALSE);
????}
????//恢復(fù)串口為阻塞狀態(tài)
????if(fcntl(fd?F_SETFL?0)?0)
????{
????????printf(“fcntl?failed!\n“);
????????return(FALSE);
????}
????else
????{
????????printf(“fcntl=%d\n“fcntl(fd?F_SETFL0));
????}
????//測(cè)試是否為終端設(shè)備
????if(0?==?isatty(STDIN_FILENO))
????{
????????printf(“standard?input?is?not?a?terminal?device\n“);
????????return(FALSE);
????}
????else
????{
????????printf(“isatty?success!\n“);
????}
????printf(“fd->open=%d\n“fd);
????return?fd;
}??
/*******************************************************************?
*?名稱:????????????????UART0_Close?
*?功能:????????????????關(guān)閉串口并返回串口設(shè)備文件描述?
*?入口參數(shù):?fd:文件描述符????port?:串口號(hào)(ttyPS0ttyPS1)
*?出口參數(shù):?????????????void
*******************************************************************/??
void?UART0_Close(int?fd)??
{??
????close(fd);
}??
/*******************************************************************?
*?名稱:????????????????UART0_Set?
*?功能:????????????????設(shè)置串口數(shù)據(jù)位,停止位和效驗(yàn)位?
*?入口參數(shù):?fd??????????串口文件描述符
*??????????speed???????串口速度
*??????????flow_ctrl???數(shù)據(jù)流控制
*??????????databits????數(shù)據(jù)位???取值為?7?或者8
*??????????stopbits????停止位???取值為?1?或者2
*??????????parity??????效驗(yàn)類型?取值為NEOS
*出口參數(shù):??????????????正確返回為1,錯(cuò)誤返回為0
*******************************************************************/??
int?UART0_Set(int?fdint?speedint?flow_ctrlint?databitsint?stopbitsint?parity)??
{??
????int???i;
????int???status;
????int???speed_arr[]?=?{?B115200?B19200?B9600?B4800?B2400?B1200?B300};
????int???name_arr[]?=?{115200??19200??9600??4800??2400??1200??300};
????struct?termios?options;
????/*tcgetattr(fd&options)得到與fd指向?qū)ο蟮南嚓P(guān)參數(shù),并將它們保存于options該函數(shù)還可以測(cè)試配置是否正確,
?????*該串口是否可用等。若調(diào)用成功,函數(shù)返回值為0,若調(diào)用失敗,函數(shù)返回值為1.?*/
????if??(?tcgetattr(?fd&options)??!=??0)??//erro:?tcgetattr(?fd&options)=1
????{
????????perror(“SetupSerial?1“);
????????return(FALSE);
????}
????//設(shè)置串口輸入波特率和輸出波特率
????for?(?i=?0;??i?????{
????????if??(speed?==?name_arr[i])
????????{
????????????cfsetispeed(&options?speed_arr[i]);
????????????cfsetospeed(&options?speed_arr[i]);
????????}
????}
????//修改控制模式,保證程序不會(huì)占用串口
????options.c_cflag?|=?CLOCAL;
????
評(píng)論
共有 條評(píng)論