在ZedBoard上開發基於QNX操作系統PL部分UART設備驅動的過程中遇到了一點問題,問題原因不明但總結下來給大家提供參考,也以便以後對QNX的進一步瞭解後回顧這些問題。
硬件平臺:MIZ702完全兼容ZedBoard
HOST:Windows QNX Momentics IDE
功能:ZYNQ芯片的PL部分實現一路UART,並編寫QNX下設備驅動。
在讀串口時,申請了動態內存保存從串口讀到的數據,然後再填充IOV,返回給Client。代碼如下:
int io_read(resmgr_context_t *ctp,io_read_t *msg,RESMGR_OCB_T *ocb)
{
int nleft;
int nbytes;
int nparts;
int status;
if((status = iofunc_read_verify(ctp,msg,ocb,NULL))!=EOK)
return(status);
if((msg->i.xtype &_IO_XTYPE_MASK)!= _IO_XTYPE_NONE)
return (ENOSYS);
char* buffer = (char *) malloc(msg->i.nbytes);
memset(buffer,0,msg->i.nbytes);
ocb->attr->nbytes = XUartLite_Recv(UartLite,buffer,msg->i.nbytes);
nleft = ocb->attr->nbytes - ocb->offset;
nbytes = min(msg->i.nbytes,nleft);
printf ("receive %d bytes = '%s' expected read %d \n", nbytes, buffer,msg->i.nbytes);
if(nbytes>0)
{
//set up the return data IOV
SETIOV(ctp->iov,buffer,nbytes);
//set up the number of bytes (returned by client's read())
_IO_SET_READ_NBYTES(ctp,nbytes);
//advanced the offset by the number of bytes
//returend to the client
//ocb->offset +=nbytes;
nparts = 1;
}else
{
//they've asked for zero bytes or they've already previously read everythin
_IO_SET_READ_NBYTES(ctp,0);
nparts = 0;
}
//mark the access time as invalid (we just accessed it)
if(msg->i.nbytes > 0)
ocb->attr->flags |= IOFUNC_ATTR_ATIME;
free(buffer);
return(_RESMGR_NPARTS(nparts));
}
動態內存的大小是以read消息中期望讀到的數據長度來決定的,若用cat 命令讀取設備驅動信息 即
cat /dev/uartlite
系統會報錯:Server fault on msg pass
若在Client中用read讀 /dev/uartlite 則沒有問題,這個問題的原因是cat 中 read命令中的長度參數是32768,read函數中的長度沒有這麼大。
總結一下就是:動態申請的內存過大則會導致消息傳遞錯誤