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

📄 ms_api.c

📁 ATMEL ARM7 虚拟优盘
💻 C
字号:
//  ----------------------------------------------------------------------------
//          ATMEL Microcontroller Software Support  -  ROUSSET  -
//  ----------------------------------------------------------------------------
//  DISCLAIMER:  CONDITIONS AS PER SIGNED LIMITED LICENSE AGREEMENT (AT91
//  SOFTWARE AND USER DOCUMENTATION)
//  ALL SOFTWARE IS PROVIDED AS IS, WITH ALL FAULTS, AND WITH NO WARRANTY
//  WHATSOEVER.  ATMEL EXPRESSLY DISCLAIMS ALL WARRANTIES, EXPRESS, IMPLIED,
//  OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.
//  ----------------------------------------------------------------------------
// File Name           : ms_api.c
// Object              : command sent to the chip
// Creation            : JCB 15/apr/2005
// Modif               :
// ----------------------------------------------------------------------------
#include "po_types.h"
#include "trace.h"
#include "fw_usb.h"
#include "po_kernel.h"
#include "ms_device.h"

/*****************************************************************
*
* ROUTINE : usbms_init
*
*-----------------------------------------------------------------
*
* Purpose : some initializations, TB, FIFO, flags
*
* Input Parameters  : NONE
*
* Output Parameters : USBMS_OK
*                     USBMS_ERROR
*
*****************************************************************/
USBMS_RETURN usbms_init( void )
{
  USBMS_RETURN _status = USBMS_OK;
  UCHAR _error = 0,_i = 0,_j = 0;

  ms_initAutomat();

  USB_RX = 0;

  /*initialize the ms internal buffer*/
  for(_i = 0; _i < MAX_LUN_NUM; _i ++)
  {
    usbms_context.usb_lun[_i].cur_request.data_buff = po_rngCreate(MAX_TRANSFER_SIZE);
    if(usbms_context.usb_lun[_i].cur_request.data_buff.status != 1)
    {
      /*allocation failed*/
      TRACE_ERROR( "buffer failed\n\r");
      return USBMS_ERROR;
    }
  }

  /* create the FIFO  == output fifo*/
  fw_fifoId = po_rngCreate(FW_FIFO_SIZE);

  if(fw_fifoId.status == 1)
  {
    /* allocate the TBs */
    for(_i = 0; _i < FW_TB_NUM; _i++)
    {
      fw_TB[_i] = (structTB *) po_malloc(sizeof(structTB));

      if(!fw_TB[_i])
      {
        TRACE_ERROR( "_i= : %d  ERROR\n\r",_i);
        for(_j = 0;_j < _i ; _j++)
        {
          /* free the TB already allocated */
          po_free(fw_TB[_j]);
        }
        _error++;
        break;
      }
    }
    if(_error == 0)
    {
      /* initialize all TBs */
      fw_writeTB = fw_TB;
      fw_readTB = fw_TB;
      for(_i = 0;_i < FW_TB_NUM;_i++)
      {
        /* buffer is empty */
        fw_TB[_i][0].status |= FW_EBIT;
        /* index of data buffer*/
        fw_TB[_i][0].index = 0;
        /* size = 0 */
        fw_TB[_i][0].length = 0;
      }
    }
    else
    {
      TRACE_ERROR( "Alloc TB faild\n\r");
      TRACE_ERROR( "Num error : %d\n\r",_error);
      /* free the IN FIFO */
      po_rngDelete(&fw_fifoId);
      _status = USBMS_ERROR;
    }
  }
  else
  {
    TRACE_ERROR( "IN FIFO faild\n\r");
    _status = USBMS_ERROR;
  }

  /*initialization of global variables */
  fw_controlData.state = FW_STATE_END;
  fw_controlData.pData = NULL;

  fw_IsrCount = 0;
  /* at the beginning we wait until the connection is
  _* done : the first SOF is received */
  fw_deviceState |= FW_DS_COMHANG;
  fw_lastTxPacket = 0;

  return _status;
}


/*****************************************************************
*
* ROUTINE     ms_retreiveTb
*
*-----------------------------------------------------------------
*
* Purpose :
*   scan the TB pool for valid buffers and pick data
*
* Input parameters  : DataBuffer : buffer of data to be retreived
*                     length : length of data to read
*
* Output parameters : length of data read
*
* Global data : fw_TB      : array of the buffer IDs
*               fw_writeTB : TB currently used by the firmware
*               fw_readTB  :
*
*****************************************************************/
ULONG ms_retreiveTb( char *DataBuffer,ULONG length )
{
  ULONG     _lengthRead = 0, _len = 0;
  structTB *_TB;
  UCHAR    *_indexTB;
  UCHAR     _flag = 0;
  ULONG    *_sizeTB;

  while(_lengthRead != length)
  {
    _TB = fw_readTB[0];

    /* check the validity of the TB */
    if(!(_TB->status & FW_VBIT))
    {
      break;
    }

    _sizeTB = (ULONG*)&_TB->length;
    _indexTB = (UCHAR*)&_TB->data[_TB->index]; /* pointer to the current read position */

    _len = length - _lengthRead;

    /* if the length of data to read is larger than the number of data in the current TB */
    if(*_sizeTB < _len)
    {
      _len = *_sizeTB;
    }

    /* read the data in the current */
    po_memcpy( (DataBuffer + _lengthRead),_indexTB ,_len);

    /* update the size in the TB and the size of data read*/
     *_sizeTB -= _len;
    _lengthRead += _len;

    if(*_sizeTB == 0)
    {
      po_lock();
      /* let the TB be empty*/
      _TB->status |= FW_EBIT;
      USB_RX--;
      /* index of data buffer*/
      _TB->index = 0;
      /* let the TB be unvalid */
      _TB->status &= ~FW_VBIT;
      po_unlock();

      fw_readTB++;
      /* it is the end of the pool */
      if(fw_readTB > (fw_TB + (FW_TB_NUM - 1)))
      {
        fw_readTB = fw_TB;
      }
      _flag = 1;
    }
    else
    {
      /* new read index of data buffer*/
      _TB->index += _len;
    }
  }

  if((fw_deviceState & FW_DS_TB_FULL) && _flag)
  {
    TRACE_ERROR( "TB N FULL\n\r");
    /* the TB pool isn't full anymore */
    fw_deviceState &= ~FW_DS_TB_FULL;
  }

  return _lengthRead;
}


/*****************************************************************
*
* ROUTINE     usb_send
*
*-----------------------------------------------------------------
*
* Purpose :
*   queues data in the IN FIFO and signal the driver if the fifo is
*  full.
*
* Input parameters  : DataBuffer : buffer containing data
*                     Length : length of data
*
* Output parameters : length of data put in the IN FIFO
*
* Global data : fw_fifoId : ID of the IN FIFO
*
*****************************************************************/
USHORT usb_send(char *DataBuffer, USHORT Length)
{
  USHORT _len = 0;

  _len = po_rngBufPut(&fw_fifoId, DataBuffer,Length);

  if( po_rngNBytes(&fw_fifoId) > FW_IN_FIFO_FULL )
  {
    fw_deviceState |= FW_DS_FIFO_FULL;
  }
  return _len;
}







⌨️ 快捷键说明

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