在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函数中的长度没有这么大。
总结一下就是:动态申请的内存过大则会导致消息传递错误
---------------------
作者:恰_同学少年
来源:CSDN
原文:https://blog.csdn.net/qq_34322603/article/details/78965145