ms.c

来自「U盘控制器USB97C223的固件代码,对2kPAGE NAND FLASH 有」· C语言 代码 · 共 1,709 行 · 第 1/5 页

C
1,709 行
字号
  return k_success;
}
//+-----------------------------------------------------------------------------
// Name:
//   ms_get_int
//
// Declaration:
//   t_result ms_get_int(uint8 ticks);
//
// Purpose:
//   Issue a getint command to solicit the result of the previous command.
//
// Arguments:
//   ticks - a uint8 representing the timeout period in milliseconds.
//
// Return:
//   A t_result indicating:
//     k_success - the module global variable _factor contains the results.
//     k_error   - either cannot issue the command or read back the results.
//                 contents of _factor undefined.
//
// Notes:
//   This is a FUNCTION, not a DFA.
//   The module global variable _factor is written with the results as a side effect.
//
// Since:
//   fmc-1.0
//------------------------------------------------------------------------------
t_result ms_get_int(uint16 ticks) reentrant;
t_result ms_get_int(uint16 ticks) reentrant
{
  uint8 status;
  uint8 val;
  trace0(0, ms, 0, "ms_get_int()");
  #if defined(k_mcu_97210)
  status= _ms_register_rd(ms_alt_stat);
  val=kbm_msc_stat_sif;
  #elif defined(k_mcu_97223) 
  status= _ms_register_rd(msc_alt_stat_h);
  // TRACE2(18, reg, 0, "Reading Addr %04x Data-%02x",(uint16)&msc_alt_stat_h,_ms_register_rd( msc_alt_stat_h));
  val = kbm_ms_alt_stat_int;
  #endif
  if ((status & val) == val)
  {
    _mcu_register_wr(x_msc_stat, kbm_msc_stat_sif);
  }
  else
    //end of new
    if (k_success != ms_wait_msc_stat_sif_with_timeout(ticks))
    return k_error;
  // If this is a MSPRO device in Parllel mode then we have the Status Information avialable on the
  // parllel bus , hence no need to read FIFO .
 #if defined(k_mcu_97223)
  if (_msparallel)
  {
    trace0(0, ms, 0, "Mode id Parallel");
      #if defined(k_mcu_97223) 
    g_tmp =   _mcu_register_rd(msc_alt_stat_l);
      #endif 
    _factor =  g_tmp <<4;
    g_tmp &= 0x01;
    _factor |= g_tmp;
    trace1(0, ms, 0, "Factor = %02x",_factor);
    return k_success;
  }
  #endif
  ms_set_tpc(k_ms_tpc_get_int, 1);
  // wait rx fifo not empty
  if (_ms_rx_data_fifo_is_empty())
  {
    #if defined(k_mcu_97210)
    if (k_success != ms_wait_fifo_with_timeout(ms_fifo_stat_r_buf_e, 0, 3))
      return k_error;
     #elif defined(k_mcu_97223) 
    if (k_success != ms_wait_fifo_with_timeout(kbm_ms_alt_stat_fifo_e, 0, 3))
      return k_error;
     #endif
  }
  // read the interrupt data
  _factor = _ms_register_rd(ms_rx_db_lsb);
  g_tmp = _ms_register_rd(ms_rx_db_msb);       // throw away
  trace1(0, ms, 0, "_factor:%02X", _factor);
  //_profile_off(6);
  return k_success;
}
// so far i haven't got the acd code to work correctly...
// but since it doesn't improve performance anyhow, there's not much point...
//#define k_use_acd
#ifdef k_use_acd
//+-----------------------------------------------------------------------------
// Name:
//   ms_get_int_acd
//
// Declaration:
//   t_result ms_get_int_acd(uint8 ticks);
//
// Purpose:
//   Issue a getint command to solicit the result of the previous command.
//   Only to be used with ACD mode.
//   Allows skipping the wait for SIF, and the set-tpc for getint.
//   The goal is to use it to slightly improve the read performance.
//
// Arguments:
//   None.
//
// Return:
//   A t_result indicating:
//     k_success - the module global variable _factor contains the results.
//     k_error   - either cannot issue the command or read back the results.
//                 contents of _factor undefined.
//
// Notes:
//   This is a FUNCTION, not a DFA.
//   The module global variable _factor is written with the results as a side effect.
//
// Since:
//   fmc-1.0
//------------------------------------------------------------------------------
t_result ms_get_int_acd(void) reentrant;
t_result ms_get_int_acd(void) reentrant
{
  trace0(0, ms, 0, "ms_get_int_acd()");
  // wait rx fifo not empty
  if (_ms_rx_data_fifo_is_empty())
    if (k_success != ms_wait_fifo_with_timeout(ms_fifo_stat_r_buf_e, 0, 3))
      return k_error;
    // read the interrupt data
  _factor = _ms_register_rd(ms_rx_db_lsb);
  g_tmp = _ms_register_rd(ms_rx_db_msb);        // throw away
  trace1(0, ms, 0, "_factor:%02X", _factor);
  return k_success;
}
#endif
//+-----------------------------------------------------------------------------
// Name:
//   ms_set_cmd
//
// Declaration:
//   t_result ms_set_cmd(uint8 cmd);
//
// Purpose:
//   Send a flash command to the MS card.
//
// Arguments:
//   cmd - a uint8 representing the command id.
//
// Return:
//   A t_result indicating:
//     k_success - command issued.
//     k_error   - failed to issue command.
//
// Notes:
//   This is a FUNCTION, not a DFA.
//
// Since:
//   fmc-1.0
//------------------------------------------------------------------------------
t_result ms_set_cmd(uint8 cmd) reentrant;
t_result ms_set_cmd(uint8 cmd) reentrant
{
  trace1(0, ms, 0, "ms_set_cmd() cmd:%02X", cmd);
  // write the tpc
  if (k_success != ms_set_tpc(k_ms_tpc_set_cmd, 1))
    return(k_error);
  // write the command
  _ms_register_wr(ms_tx_db_msb, 0);
  _ms_register_wr(ms_tx_db_lsb, cmd);
  if (!_ms_tx_data_fifo_is_empty())
  {
    #if defined(k_mcu_97210)
    if (k_success != ms_wait_fifo_with_timeout(ms_fifo_stat_t_buf_e, ms_fifo_stat_t_buf_e, 3))
      return k_error;
    #elif defined(k_mcu_97223) 
    if (k_success != ms_wait_fifo_with_timeout( kbm_ms_alt_stat_fifo_e , kbm_ms_alt_stat_fifo_e , 3))
      return(k_error);
    #endif
  }
  return k_success;
}
//+-----------------------------------------------------------------------------
// Name:
//   ms_set_read_reg_addrs
//
// Declaration:
//   t_result ms_set_read_reg_addrs(uint8 start, uint8 count);
//
// Purpose:
//   Tell the MS card that its registers are to be read by specifying the starting
//   register offset and the number of registers to be read.
//
// Arguments:
//   start - the offset of the first register to read.
//   count - the number of consecutive registers to read.
//
// Return:
//   A t_result indicating:
//     k_success - read register start/count are delivered to the card.
//     k_error   - failed to configure the card for a register read.
//
// Notes:
//   This is a FUNCTION, not a DFA.
//
// Since:
//   fmc-1.0
//------------------------------------------------------------------------------
t_result ms_set_read_reg_addrs(uint8 start, uint8 count) reentrant;
t_result ms_set_read_reg_addrs(uint8 start, uint8 count) reentrant
{
  trace2(0, ms, 0, "ms_set_read_reg_addrs() start:0x%02X count:0x%02X", start, count);
  // write the tpc
  if (k_success != ms_set_tpc(k_ms_tpc_set_rw_reg, 4))
    return k_error;
  // write the command
  _ms_register_wr(ms_tx_db_msb, count);
  _ms_register_wr(ms_tx_db_lsb, start);
    #if defined(k_mcu_97210)
  if (k_success != ms_wait_fifo_with_timeout(ms_fifo_stat_t_buf_e, ms_fifo_stat_t_buf_e, 3))
    return k_error;
    #elif defined(k_mcu_97223) 
  if (k_success != ms_wait_fifo_with_timeout( kbm_ms_alt_stat_fifo_e , kbm_ms_alt_stat_fifo_e , 3))
    return k_error;
    #endif
  _ms_register_wr(ms_tx_db_msb, 0);
  _ms_register_wr(ms_tx_db_lsb, k_ms_parm_system);

  if (!_ms_tx_data_fifo_is_empty())
    #if defined(k_mcu_97210)
    if (k_success != ms_wait_fifo_with_timeout(ms_fifo_stat_t_buf_e, ms_fifo_stat_t_buf_e, 3))
      return k_error;
    #elif defined(k_mcu_97223) 
    if (k_success != ms_wait_fifo_with_timeout(kbm_ms_alt_stat_fifo_e, kbm_ms_alt_stat_fifo_e, 3))
      return k_error;
   #endif
  return k_success;
}

//+-----------------------------------------------------------------------------
// Name:
//   ms_set_write_reg_addrs
//
// Declaration:
//   t_result ms_set_write_reg_addrs(uint8 start, uint8 count);
//
// Purpose:
//   Tell the MS card that its registers are to be written by specifying the starting
//   register offset and the number of registers to be written.
//
// Arguments:
//   start - the offset of the first register to write.
//   count - the number of consecutive registers to write.
//
// Return:
//   A t_result indicating:
//     k_success - write register start/count are delivered to the card.
//     k_error   - failed to configure the card for a register write.
//
// Notes:
//   This is a FUNCTION, not a DFA.
//
// Since:
//   fmc-1.0
//------------------------------------------------------------------------------
t_result ms_set_write_reg_addrs(uint8 start, uint8 count) reentrant;
t_result ms_set_write_reg_addrs(uint8 start, uint8 count) reentrant
{
  trace2(0, ms, 0, "ms_set_write_reg_addrs() start:%02X count:0x%02X", start, count);
  // write the tpc
  if (k_success != ms_set_tpc(k_ms_tpc_set_rw_reg, 4))
    return k_error;
  // write the command
  _ms_register_wr(ms_tx_db_msb, k_ms_ext_ovwr_flg);
  _ms_register_wr(ms_tx_db_lsb, 0);
  if (!_ms_tx_data_fifo_is_empty())
    #if defined(k_mcu_97210)
    if (k_success != ms_wait_fifo_with_timeout(ms_fifo_stat_t_buf_e, ms_fifo_stat_t_buf_e, 3))
      return k_error;
    #elif defined(k_mcu_97223) 
    if (k_success != ms_wait_fifo_with_timeout( kbm_ms_alt_stat_fifo_e , kbm_ms_alt_stat_fifo_e , 3))
      return k_error;
    #endif
  _ms_register_wr(ms_tx_db_msb, count);
  _ms_register_wr(ms_tx_db_lsb, start);
  if (!_ms_tx_data_fifo_is_empty())
     #if defined(k_mcu_97210)
    if (k_success != ms_wait_fifo_with_timeout(ms_fifo_stat_t_buf_e, ms_fifo_stat_t_buf_e, 3))
      return k_error;
     #elif defined(k_mcu_97223) 
    if (k_success != ms_wait_fifo_with_timeout( kbm_ms_alt_stat_fifo_e , kbm_ms_alt_stat_fifo_e , 3))
      return k_error;
      #endif
    //_profile_off(6);
  return k_success;
}
//-----------------------------------------------------------------------------------------

//+-----------------------------------------------------------------------------
// Name:
//   ms_read_status
//
// Declaration:
//   t_result ms_read_status(void);
//
// Purpose:
//   After an error read the status registers to determine the cause.
//
// Arguments:
//   None.
//
// Return:
//   A t_result indicating:
//     k_success - the module global variables _status0 and _status1 contain the status.
//     k_error   - failed to obtain the status.
//                 contents of _status0 and _status1 undefined.
//
// Notes:
//   This is a FUNCTION, not a DFA.
//   The module global variables _status0 and _status1are written with the results
//   as a side effect.
//
// Since:
//   fmc-1.0
//------------------------------------------------------------------------------
t_result ms_read_status() reentrant
{
  trace0(0, ms, 0, "ms_read_status()");

  // set the address
  if (k_success != ms_set_read_reg_addrs(k_ms_reg_status0, 2))
    return k_error;
  // write the tpc
  if (k_success != ms_set_tpc(k_ms_tpc_rd_reg, 2))
    return k_error;
  // read the value
  if (_ms_rx_data_fifo_is_empty())
  {
    #if defined(k_mcu_97210)
    if (k_success != ms_wait_fifo_with_timeout(ms_fifo_stat_r_buf_e, 0, 3))
      return k_error;
    #elif defined(k_mcu_97223) 
    if (k_success != ms_wait_fifo_with_timeout(kbm_ms_alt_stat_fifo_e, 0, 3))
      return k_error;
    #endif

⌨️ 快捷键说明

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