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

📄 virtual_mem.c

📁 应用AT89C5131A单片机
💻 C
📖 第 1 页 / 共 2 页
字号:
//!
//! (sector = 512B)
//! @param addr         Sector address to start read
//! @param nb_sector    Number of sectors to transfer
//!
//! @return                Ctrl_status
//!   It is ready    ->    CTRL_GOOD
//!   Memory unplug            ->    CTRL_NO_PRESENT
//!   Not initialize or change ->    CTRL_BUSY
//!   A error occur  ->    CTRL_FAIL
//!
Ctrl_status virtual_read_10( U32 addr , U16 nb_sector )
{
   if ( !virtual_is_present() )
      return CTRL_NO_PRESENT;
   virtual_check_init();
   if ( virtual_check_change_state() )
      return CTRL_BUSY;

#if (VMEM_NB_SECTOR < 8)
   if ( 8              < (addr + nb_sector))
#else
   if ( VMEM_NB_SECTOR < (addr + nb_sector))
#endif
   {
      return CTRL_FAIL;
   }

   s_u16_addr = addr;
   s_u8_nb_sector = LSB(nb_sector);

   return CTRL_GOOD;
}


//! This fonction initialise the memory for a write operation
//!
//! (sector = 512B)
//! @param addr         Sector address to start write
//! @param nb_sector    Number of sectors to transfer
//!
//! @return                Ctrl_status
//!   It is ready    ->    CTRL_GOOD
//!   Memory unplug            ->    CTRL_NO_PRESENT
//!   Not initialize or change ->    CTRL_BUSY
//!   A error occur  ->    CTRL_FAIL
//!
Ctrl_status virtual_write_10( U32 addr , U16 nb_sector )
{
   if (!virtual_is_present())         return CTRL_NO_PRESENT;

   virtual_check_init();

   if (virtual_check_change_state())  return CTRL_BUSY;

   if ( VMEM_NB_SECTOR < (addr + nb_sector))
   {
      return CTRL_FAIL;
   }
   
   s_u16_addr = addr;
   s_u8_nb_sector = LSB(nb_sector);

   return CTRL_GOOD;
}


//------------ SPECIFIC FONCTION USB TRANSFER -----------------------------------------

//! This fonction transfer the memory data (programed in scsi_read_10) directly to the usb interface
//!
//! @return                Ctrl_status
//!   It is ready    ->    CTRL_GOOD
//!   Memory unplug            ->    CTRL_NO_PRESENT
//!   Not initialize or change ->    CTRL_BUSY
//!   A error occur            ->    CTRL_FAIL
//!
Ctrl_status virtual_usb_read()
{
   U8    code *ptr_cram;
   U8    nb_64;
   U8    nb_read;

   while (s_u8_nb_sector != 0)
   {
      if ( !virtual_is_present() )        return CTRL_NO_PRESENT;

      if (s_u16_addr > (VMEM_NB_SECTOR-1))   // If overflow (possible with size virtual mem < 8 sectors)
         s_u16_addr = VMEM_NB_SECTOR-1;   // then read the last sector

      ptr_cram = &vmem_data[VMEM_SECTOR_SIZE * s_u16_addr];

      nb_64 = (VMEM_SECTOR_SIZE/64);
      while ( nb_64 )
      {
         // read 8x64B = 512B
         for(nb_read=0;nb_read<64;nb_read++)
         {
            Usb_write_byte(*ptr_cram++);
         }
         nb_64--;  
         Usb_send_in();
      }
      s_u16_addr++;         // new page
      s_u8_nb_sector--;
   }

   return CTRL_GOOD;
}


//! This fonction transfer the usb data (programed in scsi_write_10) directly to the memory data
//!
//! @return                Ctrl_status
//!   It is ready    ->    CTRL_GOOD
//!   Memory unplug            ->    CTRL_NO_PRESENT
//!   Not initialize or change ->    CTRL_BUSY
//!   A error occur            ->    CTRL_FAIL
//!
Ctrl_status virtual_usb_write( void )
{
   U8    xdata *ptr_cram;
   U8    nb_64;
   U8    nb_read;

   while (s_u8_nb_sector != 0)
   {
      if ( !virtual_is_present() )
         return CTRL_NO_PRESENT;
      if ( virtual_check_change_state() )
         return CTRL_BUSY;

      ptr_cram = &vmem_data[VMEM_SECTOR_SIZE * s_u16_addr];

      nb_64 = (VMEM_SECTOR_SIZE/64);

      while ( nb_64 )
      {
         while(!Is_usb_receive_out());
         for(nb_read=0;nb_read<64;nb_read++)
         {
            page_mask[nb_read] = Usb_read_byte();
         }
         Usb_ack_receive_out_ms();
         while(!Is_usb_receive_out());
         for(nb_read=64;nb_read<128;nb_read++)
         {
            page_mask[nb_read] = Usb_read_byte();
         }
         nb_64 -= 2;
         Usb_ack_receive_out_ms();
         __api_wr_code_page_fix (ptr_cram, page_mask, 128);
         ptr_cram += 128;
      }
      s_u16_addr++;         // new page
      s_u8_nb_sector--;
   }

   return CTRL_GOOD;
}


/*F**************************************************************************
* NAME: __api_wr_code_byte 
*----------------------------------------------------------------------------
* PARAMS:
* Uint16 address : address to program
* Uchar value : data to write   
* return: 
* Uchar return : 
*       return  = 0x00 -> pass                            
*       return != 0x00 -> fail
*----------------------------------------------------------------------------
* PURPOSE: 
* This function allows to program data byte in Flash memory.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* To use this function the constante __API_WR_CODE_BYTE must be define in 
* C header file flash_api.h.
*----------------------------------------------------------------------------
* REQUIREMENTS: 
*****************************************************************************/
Uchar __api_wr_code_byte (Uchar xdata * pt_address, Uchar value)
{
  bit ea_save;

  ea_save = EA;
  EA = 0;
  api_command = _COMMAND_WR_CODE_BYTE;
  FCON = 0x08;

  *pt_address = value;

  MAP_BOOT;
  __API_FLASH_ENTRY_POINT_PE();
  UNMAP_BOOT;
  EA = ea_save;        // restore interrupt state

  return(api_value);
}



/*F**************************************************************************
* NAME: __api_wr_code_page_fix 
*----------------------------------------------------------------------------
* PARAMS:
* Uint16 add_flash : address of the first byte to program in the Flash
* Uint16 add_xram  : address in XRAM of the first data to program
* Uchar nb_data : number of bytes to program
* return: 
* Uchar return : 
*       return = 0x00 -> pass                            
*       return != 0x00 -> fail
*----------------------------------------------------------------------------
* PURPOSE: 
* This function allows to program until 128 Datas in Flash memory.
* Number of bytes to program is limited such as the Flash write remains in a
* single 128 bytes page. 
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* To use this function the constante __API_WR_CODE_PAGE must be define in 
* C header file flash_api.h.
* This function used Dual Data Pointer DPTR0&1. At the end of this function 
* DPTR = DPTR0.
*----------------------------------------------------------------------------
* REQUIREMENTS: 
*****************************************************************************/
Uchar __api_wr_code_page_fix (Uchar xdata * pt_code, Uchar xdata * pt_xram, Uchar nb_data)
{
  bit ea_save;
  Uint16 add_xram; 
  data Uint16 add_code;


  add_xram = DPTR; // save the DPTR
  add_code = pt_code;
  ea_save = EA;
  EA = 0;
  add_xram = pt_xram;
  api_command =0x01;
  api_value   = nb_data;
  api_dpl = LOW(add_xram);
  api_dph = ((Uchar)((add_xram)>>8));
  AUXR1++;
//  add_xram = DPTR; // save the DPTR
//  DPTR = pt_code;
  DPTR = add_code;
  AUXR1++;
  MAP_BOOT;
  __API_FLASH_ENTRY_POINT();
  UNMAP_BOOT;

  AUXR1++;
  DPTR = add_xram; // restore the DPTR
  AUXR1++;
  EA = ea_save;    	// restore interrupt state
 
  return(api_value);
}

⌨️ 快捷键说明

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