⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 z85230buf.c

📁 Curtiss-Wright Controls Embedded Computing公司的cw183板bsp源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* z85230.c - Z85230 SCC Serial Communications Controller driver *//* Copyright 2000-2002 Dy 4 Systems, Inc. *//*modification history--------------------*//*DESCRIPTIONUSAGEAll buffer manipulation in this driver is done through calling themethods implemented in this file.  The notable exception is z85230Hw, whichhas carte blanche to do whatever it wants to the internal buffers.*//* includes */#include "h/drv/sio/z85230OS.h"#include "h/drv/sio/z85230Buf.h"/* macros *//* functions *//*************************************************************************** * z85230BufPackedSize - return the number of bytes required to pack *                       data at character size (between 5 and 7) * RETURNS * the number of bytes required to pack the data */int z85230BufPackedSize( int dataSize, int charSize )  {  Z85230_ASSERT( (charSize >= 5) && (charSize <= 7) );  /* round up as data will be zero padded */  return ( (charSize * dataSize) + 7 ) / 8;  }/*************************************************************************** * z85230BufUnpackedSize - return the number of bytes required to unpack *                         data at character size ( between 5 and 7 ) * * RETURNS * the number of bytes required to unpack the data */int z85230BufUnpackedSize( int packedDataSize, int charSize )  {  Z85230_ASSERT( (charSize >= 5) && (charSize <= 7) );  /* we round down as data is zero padded */  return (packedDataSize * 8) / charSize;  }/*************************************************************************** * z85230BufPackBytes - pack a set of characters (bitsize between 5 and 7) *                      into memory - no characters are packed if there is *                      not enough room for all of them * * RETURNS * the number of bytes of memory packed */int z85230BufPackBytes( unsigned char * dst, int dstSize,			unsigned char * src, int srcSize,			int charSize )  {  unsigned short residue = 0;  int residueSize = 0;  int bytesPacked = 0;  int srcIdx = 0;  unsigned char mask=0;  Z85230_ASSERT( (charSize >= 5) && (charSize <= 7) );  if ( srcSize == 0 )    {    /* nothing to unpack */    return 0;    }  if ( z85230BufPackedSize( srcSize, charSize ) > dstSize )    {    /* not enough room to pack it */    return 0;    }  switch ( charSize )    {    case 5:      mask = 0x1f;      break;    case 6:      mask = 0x3f;      break;    case 7:      mask = 0x7f;      break;    }  while ( srcIdx < srcSize )    {    /* update residue, residueSize */    residue = (residue << charSize) | (src[ srcIdx++ ] & mask);    residueSize += charSize;    if ( residueSize >= 8 )      {      /* pack a character by pulling off top 8 bits of residue */      dst[ bytesPacked++ ] = 	(unsigned char)( (residue >> (residueSize - 8)) & 0xff);      residueSize -= 8;      }    }  /* pack in remaining bits, if any */  if ( residueSize > 0 )    {    dst[ bytesPacked++ ] =       (unsigned char)( residue << (8 - residueSize) ) & 0xff;    }  return bytesPacked;  }/*************************************************************************** * z85230BufUnpackBytes - unpack an array of packed characters into the memory *                        specified.  No bytes are unpacked if they won't *                        all fit * RETURNS * the number of characters unpacked */int z85230BufUnpackBytes( unsigned char * dst, int dstSize,			  unsigned char * src, int srcSize,			  int charSize )  {  unsigned short residue = 0;  int residueSize = 0;  int bytesUnpacked = 0;  int srcIdx = 0;  unsigned char mask=0;  Z85230_ASSERT( (charSize >= 5) && (charSize <= 7) );  if ( srcSize == 0 )    {    /* nothing to unpack */    return 0;    }  if ( z85230BufUnpackedSize( srcSize, charSize ) > dstSize )    {    /* not enough room to unpack it */    return 0;    }  switch ( charSize )    {    case 5:      mask = 0x1f;      break;    case 6:      mask = 0x3f;      break;    case 7:      mask = 0x7f;      break;    }  while ( srcIdx < srcSize )    {    /* shift src bits into residue */    residue = (residue << 8) | src[ srcIdx++ ];    residueSize += 8;    /* unpack bits from residue into dst */    while ( residueSize >= charSize )      {      /* pull top charSize bits from residue */      dst[ bytesUnpacked++ ] = 	(unsigned char)((residue >> (residueSize - charSize)) & mask);      residueSize -= charSize;      }    }#if 0 /* We drop remainder */  /* handle remainder */  dst[ bytesUnpacked++ ] = (unsigned char)(residue & mask);#endif  return bytesUnpacked;  }/*************************************************************************** * z85230BufNumFrames *  * RETURNS * the number of frames in the buffer */int z85230BufNumFrames( Z85230BUF_FRAMES * buffer )  {  int numFrames = buffer->write - buffer->read;  if ( numFrames < 0 )    {    numFrames += buffer->size;    }  return numFrames;  }/*************************************************************************** * z85230BufCreateFrames - create a buffer * * RETURNS * Z85230_OK: success * !Z85230_OK: failure */int z85230BufCreateFrames( Z85230BUF_FRAMES * buffer, 			    char ** bufs, int frameSize, int numFrames )  {  int i;  Z85230_ASSERT( frameSize > 0 );  Z85230_ASSERT( numFrames > 0 );  /* allocate Frames */  buffer->frames = (Z85230BUF_FRAME *)    z85230OSMalloc( numFrames * sizeof( Z85230BUF_FRAME ) );  if ( buffer->frames == NULL )    {    return Z85230_ERR_OUT_OF_MEM;    }  /* set up each frame */  if ( bufs != NULL )    {    for ( i = 0; i < numFrames; i++ )      {      buffer->frames[ i ].buf = bufs[ i ];      buffer->frames[ i ].maxSize = frameSize;      buffer->frames[ i ].size = 0;      buffer->frames[ i ].read = 0;      }    }  else    {    int failed = FALSE;    for ( i = 0; i < numFrames; i++ )      {      buffer->frames[ i ].buf = (unsigned char *)	z85230OSMalloc( frameSize );      buffer->frames[ i ].maxSize = frameSize;      buffer->frames[ i ].size = 0;      buffer->frames[ i ].read = 0;      /* check result */      if ( buffer->frames[ i ].buf == NULL )	{	failed = TRUE;	}      }    if ( failed )      {      /* clean up */      for ( i = 0; i < numFrames; i++ )	{	z85230OSFree( buffer->frames[ i ].buf );	}      z85230OSFree( buffer->frames );      /* complain */      return Z85230_ERR_OUT_OF_MEM;      }    }  buffer->size = numFrames;  buffer->read = buffer->write = 0;  /* success */  return Z85230_OK;  }/*************************************************************************** * z85230BufDeleteFrames - delete frames and buffers *  * RETURNS * Z85230_OK */int z85230BufDeleteFrames( Z85230BUF_FRAMES * buffer )  {  int i;  for ( i = 0; i < buffer->size; i++ )    {    z85230OSFree( buffer->frames[ i ].buf );    }  z85230OSFree( buffer->frames );  return Z85230_OK;  }/*************************************************************************** * z85230BufReadFrame - read a frame into the buffer specified * * RETURNS * the size of the frame, or -1 on overflow * * DETAIL * No bytes are copied on overflow */int z85230BufReadFrame( Z85230BUF_FRAMES * buffer, char * bytes, 			int maxFrameSize, int charSize )  {  int frameSize;  Z85230BUF_FRAME * frame = &(buffer->frames[ buffer->read ]);  if ( buffer->read == buffer->write )    {    /* empty buffer */    return 0;    }  /* copy frame */  if ( charSize != 8 )    {    if ( z85230BufUnpackedSize( frame->size - frame->read, charSize )	 > maxFrameSize )      {      /* overflow */      return -1;      }    frameSize = z85230BufUnpackBytes( bytes, maxFrameSize,				      &(frame->buf[ frame->read ]), 				      frame->size - frame->read,				      charSize );    }  else    {    frameSize = frame->size - frame->read;        if ( frameSize > maxFrameSize )      {      /* overflow */      return -1;      }    /* coping a frame size of <0, will cause the device to lock up */    if( frameSize > 0 )      {      memcpy( bytes, &(frame->buf[ frame->read ]), frameSize );      }    else      {      frameSize=0;      }    }  /* increment frame index */  buffer->read = (buffer->read + 1) % buffer->size;  buffer->frames[ buffer->read ].read = 0;  return frameSize;  }/*************************************************************************** * z85230BufWriteFrame - write a frame up to the specified number of bytes * * RETURNS * the number of bytes in the frame written, or -1 on overflow * * DETAIL * No bytes are copied on overflow.  If there are bytes already in the * frame, the contents of frame are appended. * * Mask is applied to all * bytes appended (but not to those already in frame) */int z85230BufWriteFrame( Z85230BUF_FRAMES * buffer, char * bytes, 			 int frameSize, int charSize )  {  Z85230BUF_FRAME * frame = &(buffer->frames[ buffer->write ]);  if ( (buffer->write + 1) % buffer->size == buffer->read )    {    /* buffer full */    return 0;    }  if ( charSize != 8 )    {    if ( frame->maxSize < 	 z85230BufPackedSize( frameSize + frame->size, charSize ) )      {      /* overflow */      return -1;      }        /* pack */    frame->size += z85230BufPackBytes( &(frame->buf[ frame->size ]), 				       frame->maxSize - frame->size,				       bytes, frameSize,				       charSize );    }  else    {    if ( frameSize + frame->size > frame->maxSize )      {      /* overflow */      return -1;      }    /* copy */    z85230OSMemcpy( &(frame->buf[ frame->size ]), bytes, frameSize );    frame->size += frameSize;    }  /* update buffer */  buffer->write = (buffer->write + 1) % buffer->size;  /* initialize new frame */  frame = &(buffer->frames[ buffer->write ]);  frame->size = 0;  frame->read = 0;  return frameSize;  }/*************************************************************************** * z85230BufReadFrameByte - get a byte from the current frame * * RETURNS * the number of bytes read from the frame */int z85230BufReadFrameByte( Z85230BUF_FRAMES * buffer, unsigned char * byte )  {  Z85230BUF_FRAME * frame = &(buffer->frames[ buffer->read ]);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -