www.pudn.com > SMSC USB2.0.zip > stream.c


/*============================================================================ 
  ____________________________________________________________________________ 
                                ______________________________________________ 
   SSSS  M   M          CCCC          Standard Microsystems Corporation 
  S      MM MM   SSSS  C                    Austin Design Center 
   SSS   M M M  S      C                 11000 N. Mopac Expressway 
      S  M   M   SSS   C                Stonelake Bldg. 6, Suite 500 
  SSSS   M   M      S   CCCC                Austin, Texas 78759 
                SSSS            ______________________________________________ 
  ____________________________________________________________________________ 
  Copyright(C) 1999, Standard Microsystems Corporation 
  All Rights Reserved. 
  This program code listing is proprietary to SMSC and may not be copied, 
  distributed, or used without a license to do so.  Such license may have 
  Limited or Restricted Rights. Please refer to the license for further 
  clarification. 
  ____________________________________________________________________________ 
  Notice: The program contained in this listing is a proprietary trade 
  secret of SMSC, Hauppauge, New York, and is copyrighted 
  under the United States Copyright Act of 1976 as an unpublished work, 
  pursuant to Section 104 and Section 408 of Title XVII of the United 
  States code. Unauthorized copying, adaption, distribution, use, or 
  display is prohibited by this law. 
  ____________________________________________________________________________ 
  Use, duplication, or disclosure by the Government is subject to 
  restrictions as set forth in subparagraph(c)(1)(ii) of the Rights 
  in Technical Data and Computer Software clause at DFARS 52.227-7013. 
  Contractor/Manufacturer is Standard Microsystems Corporation, 
  80 Arkay Drive, Hauppauge, New York, 1178-8847. 
  ____________________________________________________________________________ 
  ____________________________________________________________________________ 
  stream.c - pseudo-stream implmentation over usb endpoint 
  ____________________________________________________________________________ 
  comments tbd 
  ____________________________________________________________________________ 
  Revision History 
  Date      Who  Comment 
  ________  ___  _____________________________________________________________ 
  06/09/01  cds  initial version 
============================================================================*/ 
#include "project.h" 
 
//------------------------------------------------------------------------------ 
// stream state machine globals 
static xdata   uint16  _stream_last_pnr ; 
static xdata   uint8   _stream_2_dir ; 
static xdata   uint8   _stream_2_pnr ; 
static xdata   uint16  _stream_2_offset ; 
static xdata   uint16  _stream_2_len ; 
 
 
//------------------------------------------------------------------------------ 
// Declaration: 
//  void stream_open(uint8 ndp, uint8 dir) reentrant; 
// Purpose: 
//  opens the given endpoint for streaming bytes in the given direction. 
// Arguments: 
//  ndp - the endpoint to be used for streaming the bytes 
//  dir - the direction to stream.  k_data_in (dev to host) or k_data_out (host to dev) 
// Return: 
//  null 
//------------------------------------------------------------------------------ 
void stream_open(uint8 ndp, uint8 dir) reentrant 
{ 
  uint8 tmp = ndp; 
  _stream_2_dir    = dir ; 
  _stream_2_offset = 0 ; 
  _stream_2_len    = 0 ; 
  if (_stream_2_dir == k_data_out) 
  { 
    trace0(0, stream, 0, "ndp:2 open for data-out") ; 
    // enable the buffer now 
    _buffer_rx_enable(4); 
  } 
  else 
  { 
    trace0(0, stream, 0, "ndp:2 open for data-in") ; 
    _stream_2_pnr    = 4 ; 
    _stream_2_offset = 0 ; 
    _stream_2_len    = ((x_sie_conf & kbm_sie_conf_hspeed)) ? k_hs_maxpktsz : k_fs_maxpktsz ; 
    x_ramrdbc_a2      = _l(0); 
    x_ramrdbc_a1      = _h(0); 
  } 
} 
 
//------------------------------------------------------------------------------ 
// Declaration: 
//  uint8 stream_get(uint8* bytep) reentrant; 
// Purpose: 
//  Gets the next byte from an k_data_out stream. 
// Arguments: 
//  ndp - the stream ndp 
//  bytep - a pointer to where the byte will be placed 
// Return: 
//  0 - no bytes available. 
//  1 - one byte placed into bytep location 
//------------------------------------------------------------------------------ 
uint8 stream_put(uint8 ndp, uint8 byte) reentrant 
{ 
  uint16 pkt_offset ; 
  ndp=ndp ; 
  if (_stream_2_dir != k_data_in) 
  { 
    // stream opened for data-out.  cannot put byte into stream 
    return 0; 
  } 
  if (!(_stream_2_len)) 
  { 
    // no current packet - check if the next one is available... 
    uint8 tmppnr = ((_stream_2_pnr==4)?5:4) ; 
    trace1(0, stream, 0, "ndp 2: try to allocate buffer %c", (tmppnr-4+'a')); 
    if (!(packet_is_available(tmppnr))) 
    { 
      // packet not available.  no room for byte at this time 
      return 0 ; 
    } 
    // buffer is available 
    _stream_2_pnr    = tmppnr ; 
    _stream_2_offset = 0 ; 
    _stream_2_len    = ((x_sie_conf & kbm_sie_conf_hspeed)) ? k_hs_maxpktsz : k_fs_maxpktsz ; 
  } 
  // we've got room for at least one byte.  put it into the packet buffer 
  mcu_begin_critical_section() ; 
  // the last put to sram was to a different pnr (buffer) location.  reset the offset 
  pkt_offset = _uint16(k_pkt_addrhi[_stream_2_pnr], k_pkt_addrlo[_stream_2_pnr]) + _stream_2_offset ; 
  x_sram_addr_lo = _l(pkt_offset) ; 
  x_sram_addr_hi = _h(pkt_offset) ; 
  // sram addr is now correct 
  x_sram_data = byte ; 
  // update bytecount 
  _stream_2_offset++ ; 
  mcu_end_critical_section(); 
  // check if the packet buffer is now full 
  if (_stream_2_offset == _stream_2_len) 
  { 
    // transmit it 
    if (_stream_2_pnr == 4)  // buffer A 
    { 
      trace1(0, stream, 0, "ndp:2, buf:a - transmitting %d bytes",_stream_2_len); 
      x_ramrdbc_a2 = _l(_stream_2_len); 
      x_ramrdbc_a1 = _h(_stream_2_len); 
    } 
    // not using an else right now to help debug 
    if (_stream_2_pnr == 5)  // buffer B 
    { 
      trace1(0, stream, 0, "ndp:2, buf:b - transmitting %d bytes",_stream_2_len); 
      x_ramrdbc_b2 = _l(_stream_2_len); 
      x_ramrdbc_b1 = _h(_stream_2_len); 
    } 
    // clear offset and length so that next pass, we try to grab the next buffer 
    _stream_2_offset = _stream_2_len = 0 ; 
    txfifo_wr( 2, _stream_2_pnr ) ; 
  } 
  // report our byte 
  return 1; 
} 
 
//------------------------------------------------------------------------------ 
// Declaration: 
//  uint8 stream_peek(uint8 ndp, uint8* bytep) reentrant ; 
// Purpose: 
//  Peeks at the stream and returns a byte if one is available, 
//  but does not remove the byte from the stream.  This function 
//  can be used to ensure that there are bytes available to be 
//  "gotten" before moving on. 
// Arguments: 
//  ndp - the stream ndp 
//  bytep - ptr to where byte will be stored 
// Return: 
//  0 - no bytes available. 
//  1 - one byte placed into bytep location 
//------------------------------------------------------------------------------ 
uint8 stream_peek(uint8 ndp, uint8* bytep) reentrant 
{ 
  uint16 pkt_offset ; 
  ndp=ndp ; 
  if (_stream_2_dir != k_data_out) 
  { 
    return 0 ; 
  } 
  if (!_stream_2_len) 
  { 
    // no current packet - check to see if one has come in 
    uint8 tmppnr = endpoint_rd_rx_pnr(k_rx_pipe); 
    uint8 ch ; 
    if (!tmppnr) 
    { 
      // no packets have arrived 
      trace0(0, stream, 0, "ndp:2 get failed - no packets") ; 
      return 0 ; 
    } 
    // buffer is available 
    _stream_2_pnr    = tmppnr ; 
    _stream_2_offset = 0 ; 
    _stream_2_len    = mmu_rd_len(_stream_2_pnr) ; 
    ch=(_stream_2_pnr-4+'a') ; 
    trace2(0, stream, 0, "ndp:2 buf:%c - received %d bytes", ch, _stream_2_len) ; 
  } 
  // we've got room for at least one byte.  put it into the packet buffer 
  mcu_begin_critical_section() ; 
  // the last put to sram was to a different pnr (buffer) location.  reset the offset 
  pkt_offset = _uint16(k_pkt_addrhi[_stream_2_pnr], k_pkt_addrlo[_stream_2_pnr]) + _stream_2_offset ; 
  x_sram_addr_lo = _l(pkt_offset) ; 
  x_sram_addr_hi = _h(pkt_offset) ; 
  // read the data 
  *bytep=x_sram_data ; 
  mcu_end_critical_section() ; 
  return 1 ; 
} 
 
//------------------------------------------------------------------------------ 
// Declaration: 
//  void stream_put(uint8 ndp, uint8 byte); 
// Purpose: 
//  Put one byte on the out-bound (k_data_in) stream and send 
//  off the packet if the packet is full. 
// Arguments: 
//  ndp - the stream ndp 
//  byte - data to put 
// Return: 
//  0 - no space for byte.  try again later. 
//  1 - one byte put into the stream. 
//------------------------------------------------------------------------------ 
uint8 stream_get(uint8 ndp, uint8* bytep) reentrant 
{ 
  uint16 pkt_offset ; 
  ndp=ndp ; 
  if (_stream_2_dir != k_data_out) 
  { 
    return 0 ; 
  } 
  if (!_stream_2_len) 
  { 
    // no current packet - check to see if one has come in 
    uint8 tmppnr = endpoint_rd_rx_pnr(k_rx_pipe); 
    uint8 ch ; 
    if (!tmppnr) 
    { 
      // no packets have arrived 
      trace0(0, stream, 0, "ndp:2 get failed - no packets") ; 
      return 0 ; 
    } 
    // buffer is available 
    _stream_2_pnr    = tmppnr ; 
    _stream_2_offset = 0 ; 
    _stream_2_len    = mmu_rd_len(_stream_2_pnr) ; 
    ch=(_stream_2_pnr-4+'a') ; 
    trace2(0, stream, 0, "ndp:2 buf:%c - received %d bytes", ch, _stream_2_len) ; 
  } 
  // we've got room for at least one byte.  put it into the packet buffer 
  mcu_begin_critical_section() ; 
  // the last put to sram was to a different pnr (buffer) location.  reset the offset 
  pkt_offset = _uint16(k_pkt_addrhi[_stream_2_pnr], k_pkt_addrlo[_stream_2_pnr]) + _stream_2_offset ; 
  x_sram_addr_lo = _l(pkt_offset) ; 
  x_sram_addr_hi = _h(pkt_offset) ; 
  // read the data 
  *bytep=x_sram_data ; 
  _stream_2_offset++ ; 
  mcu_end_critical_section() ; 
  // free packet if complete 
  if (_stream_2_offset == _stream_2_len) 
  { 
    mmu_deallocate(_stream_2_pnr); 
    _buffer_rx_enable(_stream_2_pnr); 
    _stream_2_len=0 ; 
  } 
  return 1 ; 
} 
 
//------------------------------------------------------------------------------ 
// Declaration: 
//  void stream_flush(uint8 ndp) reentrant 
// Purpose: 
//  flushes whatever data is in the stream out to the sie.  if 
//  there are 0 bytes in the current pkt, a ZLP is sent over 
//  the usb. 
// Arguments: 
//  ndp - the stream ndp to flush 
// Return: 
//  none 
//------------------------------------------------------------------------------ 
void stream_flush(uint8 ndp) reentrant 
{ 
  ndp = ndp ; 
  trace1(0, stream, 1, "stream_flush(%d)", ndp) ; 
  if (_stream_2_dir == k_data_in) 
  { 
    mcu_begin_critical_section() ; 
    // transmit anything (incl zlp) in the buffer 
    if (_stream_2_pnr == 4)  // buffer A 
    { 
      trace1(0, stream, 0, "ndp:2, buf:a - flushing %d bytes",_stream_2_offset); 
      x_ramrdbc_a2 = _l(_stream_2_offset); 
      x_ramrdbc_a1 = _h(_stream_2_offset); 
    } 
    // not using an else right now to help debug 
    if (_stream_2_pnr == 5)  // buffer B 
    { 
      trace1(0, stream, 0, "ndp:2, buf:b - flushing %d bytes",_stream_2_offset); 
      x_ramrdbc_b2 = _l(_stream_2_offset); 
      x_ramrdbc_b1 = _h(_stream_2_offset); 
    } 
    // clear offset and length so that next pass, we try to grab the next buffer 
    _stream_2_offset = _stream_2_len = 0 ; 
    mcu_end_critical_section(); 
    txfifo_wr( 2, _stream_2_pnr ) ; 
  } 
  else 
  { 
    // data out - simply free the pnr 
    // free packet if complete 
    if (_stream_2_len) 
    { 
      trace1(0, stream, 0, "ndp:2 pnr:%d - flushing %d bytes", _stream_2_pnr) ; 
      mmu_deallocate(_stream_2_len); 
      _stream_2_len=0 ; 
    } 
  } 
} 
 
//------------------------------------------------------------------------------ 
// Declaration: 
//  void stream_close(uint8 ndp) reentrant ; 
// Purpose: 
//  closes an ndp for streaming io.  if there is data remaining 
//  on a data_in stream, it is tx'd before this fn returns. 
//  unlike 'flush', if there is 0 data, a zlp is _not_ sent. 
// Arguments: 
//  ndp - endpoint to close 
// Return: 
//  none 
//------------------------------------------------------------------------------ 
void stream_close(uint8 ndp) reentrant 
{ 
  trace1(0, stream, 0, "close(%d)", ndp) ; 
  // if there is any data... send it out... if there is NOT, don't send a zlp 
  // if this is a data-out, always flush (to free the packets) 
  if (_stream_2_offset || (_stream_2_dir==k_data_out)) stream_flush(ndp) ; 
  // clear the stream info just for s&g 
  _stream_2_dir    = 0 ; 
  _stream_2_offset = 0 ; 
  _stream_2_len    = 0 ; 
  _stream_2_pnr    = 0 ; 
} 
 
//---eof------------------------------------------------------------------------