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

📄 sd.c

📁 u盘MCU端源代码,开发U盘的可以参考.
💻 C
📖 第 1 页 / 共 5 页
字号:
    }
    if(sync & (kbm_sync_usbrst |kbm_sync_abort))
      longjmp(_sd_context, 1);    //Go to the caller dfa and return failed
  }
  trace0(0, sd, 1, "sd_wait_irq_with_timeout() - got the irq");
  return k_success;
}
#endif

//+-----------------------------------------------------------------------------
// Name:
//   sd_pre_cmd
//
// Declaration:
//   void sd_pre_cmd() reentrant
//
// Purpose:
//   Prepare the device before issuing a command
//
// Arguments:
//   None
//
// Return:
//   None
//
// Notes:
//   This is a FUNCTION, not a DFA.
//
// Since:
//   fmc-1.0
//------------------------------------------------------------------------------
void sd_pre_cmd() reentrant
{
  trace0(0, sd, 1, "+sd_pre_cmd");
  _mcu_register_wr(x_sdc_stat, kbm_sdc_stat_rsp_rdy);
  _mcu_register_wr(x_sdc_stat, kbm_sdc_stat_crc_err);
  sdc_ctl &= ~kbm_sdc_id_data;
  sdc_wait_status_with_timeout(kbm_sdc_stat_crd_rdy,300);
  sdc_wait_status_with_timeout(kbm_sdc_stat_cmd_rdy,300);
  trace0(0, sd, 1, "-sd_pre_cmd");
}

//+-----------------------------------------------------------------------------
// Name:
//   sd_display_rsp_buf
//
// Declaration:
//   void sd_display_rsp_buf() reentrant
//
// Purpose:
//  Dumps out the contents of the cmd_rsp buffer
//
// Arguments:
//   None
//
// Return:
//   None
//
// Notes:
//   This is a FUNCTION, not a DFA.
//
// Since:
//   fmc-1.0
//------------------------------------------------------------------------------
void sd_display_rsp_buf() reentrant
{
  trace1(0, sd, 2, "Response Buffer[0] = %02x",sdc_cmd_rsp_buf[0]);
  trace1(0, sd, 2, "Response Buffer[1] = %02x",sdc_cmd_rsp_buf[1]);
  trace1(0, sd, 2, "Response Buffer[2] = %02x",sdc_cmd_rsp_buf[2]);
  trace1(0, sd, 2, "Response Buffer[3] = %02x",sdc_cmd_rsp_buf[3]);
  trace1(0, sd, 2, "Response Buffer[4] = %02x",sdc_cmd_rsp_buf[4]);
  trace1(0, sd, 2, "Response Buffer[5] = %02x",sdc_cmd_rsp_buf[5]);
  trace1(0, sd, 2, "Response Buffer[6] = %02x",sdc_cmd_rsp_buf[6]);
  trace1(0, sd, 2, "Response Buffer[7] = %02x",sdc_cmd_rsp_buf[7]);
  trace1(0, sd, 2, "Response Buffer[8] = %02x",sdc_cmd_rsp_buf[8]);
  trace1(0, sd, 2, "Response Buffer[9] = %02x",sdc_cmd_rsp_buf[9]);
  trace1(0, sd, 2, "Response Buffer[10] = %02x",sdc_cmd_rsp_buf[10]);
  trace1(0, sd, 2, "Response Buffer[11] = %02x",sdc_cmd_rsp_buf[11]);
  trace1(0, sd, 2, "Response Buffer[12] = %02x",sdc_cmd_rsp_buf[12]);
  trace1(0, sd, 2, "Response Buffer[13] = %02x",sdc_cmd_rsp_buf[13]);
  trace1(0, sd, 2, "Response Buffer[14] = %02x",sdc_cmd_rsp_buf[14]);
  trace1(0, sd, 2, "Response Buffer[15] = %02x",sdc_cmd_rsp_buf[15]);
  trace1(0, sd, 2, "Response Buffer[16] = %02x",sdc_cmd_rsp_buf[16]);
}

//+-----------------------------------------------------------------------------
// Name:
//   sd_app_cmd
//
// Declaration:
//   void sd_app_cmd() reentrant
//
// Purpose:
//   Prepare the device before issuing an app specific command
//
// Arguments:
//   None
//
// Return:
//   None
//
// Notes:
//   This is a FUNCTION, not a DFA.
//
// Since:
//   fmc-1.0
//------------------------------------------------------------------------------
void sd_app_cmd() reentrant
{
  trace0(0, sd, 1, "+sd_app_cmd");
  sd_pre_cmd();
  //fill the command buffer
  sdc_cmd_rsp_buf[0] = 0x77;
  sdc_cmd_rsp_buf[1] = g_sdc_rca_h;
  sdc_cmd_rsp_buf[2] = g_sdc_rca_l;
  sdc_cmd_rsp_buf[3] = 0x00;
  sdc_cmd_rsp_buf[4] = 0x00;
  _sd_wait_48b_resp();
  sd_display_rsp_buf();
  sdc_read_rsp(5);
  trace0(0, sd, 1, "-sd_app_cmd");
}

//+-----------------------------------------------------------------------------
// Name:
//   sd_get_status
//
// Declaration:
//   void sd_get_status() reentrant
//
// Purpose:
//  Get the current State of the SD Device
//
// Arguments:
//   None
//
// Return:
//
//
// Notes:
//   This is a FUNCTION, not a DFA.
//
// Since:
//   fmc-1.0
//------------------------------------------------------------------------------
void sd_get_status() reentrant
{
  trace0(0, sd, 1, "+ sd_get_status()");
  _stack_check();
  sd_pre_cmd();
  _stack_check();
  //fill the command buffer
  sdc_cmd_rsp_buf[0] = 0x4d;
  sdc_cmd_rsp_buf[1] = g_sdc_rca_h;
  sdc_cmd_rsp_buf[2] = g_sdc_rca_l;
  sdc_cmd_rsp_buf[3] = 0x00;
  sdc_cmd_rsp_buf[4] = 0x00;
  _sd_wait_48b_resp();
  //sd_display_rsp_buf();
  _sd_state = sdc_cmd_rsp_buf[3] & 0x1e;
  _sd_state = _sd_state >> 1;
#ifdef Debug_On
  switch(_sd_state)
  {
  case k_sdc_state_stby:
    trace0(0, sd, 1, "Standby state");
    break;
  case k_sdc_state_tran:
    trace0(0, sd, 1, "Transfer state");
    break;
  case k_sdc_state_data:
    trace0(0, sd, 1, "Data state");
    break;
  case k_sdc_state_rcv:
    trace0(0, sd, 1, "Receive state");
    break;
  case k_sdc_state_prg:
    trace0(0, sd, 1, "Program state");
    break;
  case k_sdc_state_dis:
    trace0(0, sd, 1, "Disable state");
    break;
  default:
    trace1(0, sd, 1, "Unknown state %d",_sd_state);
    break;
  }
  if(sdc_cmd_rsp_buf[3] & 0x01)
  {
    trace0(0, sd, 0, "Ready for data!!");
  } else
  {
    trace0(0, sd, 0, "Not Ready for data!!");
  }
#endif
  sdc_read_rsp(5);
  trace0(0, sd, 1, "- sd_get_status()");
  return;
}

//+-----------------------------------------------------------------------------
// Name:
//   sd_set_bus_width
//
// Declaration:
//   void sd_set_bus_width(uint8 BusWidth) reentrant
//
// Purpose:
//  Set the data bus width to either single line or four data lines.
//
// Arguments:
//   None
//
// Return:
//
//
// Notes:
//   This is a FUNCTION, not a DFA.
//
// Since:
//   fmc-1.0
//------------------------------------------------------------------------------
void sd_set_bus_width(uint8 BusWidth) reentrant
{
  trace1(0, sd, 1, "Set Bus width to %d ", BusWidth);
  sd_app_cmd();
  sd_pre_cmd();
  sdc_cmd_rsp_buf[0] = 0x46;
  sdc_cmd_rsp_buf[1] = 0x00;
  sdc_cmd_rsp_buf[2] = 0x00;
  sdc_cmd_rsp_buf[3] = 0x00;
  sdc_cmd_rsp_buf[4] = BusWidth;
  _sd_wait_48b_resp();
  sd_display_rsp_buf();
  sdc_read_rsp(5);
}

//+-----------------------------------------------------------------------------
// Name:
//   sd_stop_transfer
//
// Declaration:
//   void sd_stop_transfer() reentrant
//
// Purpose:
//  Stop the data xfer
//
// Arguments:
//   None
//
// Return:
//
//
// Notes:
//   This is a FUNCTION, not a DFA.
//
// Since:
//   fmc-1.0
//------------------------------------------------------------------------------
void sd_stop_transfer() reentrant
{
  trace0(0, sd, 1, "Stop the transfer");
  sd_pre_cmd();
  sdc_cmd_rsp_buf[0] = 0x4c;
  sdc_cmd_rsp_buf[1] = 0x00;
  sdc_cmd_rsp_buf[2] = 0x00;
  sdc_cmd_rsp_buf[3] = 0x00;
  sdc_cmd_rsp_buf[4] = 0x00;
  _sd_wait_48b_resp();
  sd_display_rsp_buf();
  sd_get_status();
  sdc_read_rsp(5);
}

//+-----------------------------------------------------------------------------
// Name:
//   sd_init_controller
//
// Declaration:
//   void sd_init_controller(void) reentrant
//
// Purpose:
//  tbd
//
// Arguments:
//   None
//
// Return:
//
//
// Notes:
//   tbd
//
// Since:
//   fmc-1.0
//------------------------------------------------------------------------------
void sd_init_controller(void) reentrant
{
  trace0(0, cf, 0, "cf_init_controller()");
  // copy string name into the controller device ID
  memcpy(_lun_data_rd(k_lun_sd, device_id), "SD/MMC", k_lun_max_devid_sz);
  memcpy(_lun_data_rd(k_lun_mmc, device_id), "SD/MMC", k_lun_max_devid_sz);
  _lun_data_wr(k_lun_sd, device_type, k_device_type_sd);
  _lun_data_wr(k_lun_mmc, device_type, k_device_type_mmc);
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
#include "dev.h"
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// should be in sd.h with all other sd register bit definitions
#define kbm_sdc_ctl_sdc_rst 0x20

⌨️ 快捷键说明

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