ATMEL: host_mem.h File Reference

- Main Page
- Modules
- Data Structures
- Files
- Related Pages

# host_mem.h File Reference

`#include "conf/conf_access.h"`
`#include "modules/control_access/ctrl_status.h"`
`#include "conf/conf_usb.h"`

Include dependency graph for host_mem.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Functions

void | host_mem_init (void)
| This fonction must be call only once at the program startup to initialize the USB driver MS.
Bool | host_mem_install (void)
| This fonction must be call when a device is connected.
void | host_mem_uninstall (void)
| This fonction must be call when a device is disconnected.
U8 | host_mem_get_lun (void)
| This fonction returns the number of LUN of the devices mass storage connected to the host.
Ctrl_status | host_mem_test_unit_ready (U8 lun)
| This fonction test the state of memory, and start the initialisation of the memory.
Ctrl_status | host_mem_read_capacity (U8 lun, U32 _MEM_TYPE_SLOW_ *u32_nb_sector)
| This fonction returns the address of the last valid sector.
U8 | host_mem_read_sector_size (U8 lun)
Bool | host_mem_wr_protect_cache (U8 lun)
Bool | host_mem_wr_protect (U8 lun)
Bool | host_mem_removal (void)
| This fonction inform about the memory type.
Ctrl_status | host_mem_read_format_capacity (U8 lun)
Ctrl_status | host_mem_inquiry (U8 lun)
Ctrl_status | host_mem_mem_2_ram (U8 lun, U32 addr, U8 *ram)
Ctrl_status | host_mem_mem_2_ram_stop (void)
| This function terminates the tranfer of a physical sector from memory.
Ctrl_status | host_mem_ram_2_mem (U8 lun, U32 addr, U8 *ram)
Ctrl_status | host_mem_mem_2_ram_ext (U8 lun, U32 addr, U8 *ram, U8 u8_nb_sector)
Ctrl_status | host_mem_ram_2_mem_ext (U8 lun, U32 addr, U8 *ram, U8 u8_nb_sector)

## Detailed Description

This file is a template for writing C software programs.

This file contains the C51 RAM called CRAM management routines which are used for Mass storage when doing ISP This file calls routines of the cram_mem.c file

- Compiler: IAR EWAVR and GNU GCC for AVR
- Supported devices: AT90USB1287, AT90USB1286, AT90USB647, AT90USB646

Author:
  Atmel Corporation: http://www.atmel.com
  Support and FAQ: http://support.atmel.no/

Definition in file host_mem.h.

## Function Documentation

void host_mem_init | ( | void |  | ) |

This fonction must be call only once at the program startup to initialize the USB driver MS.

Definition at line 173 of file host_mem.c.

References S_dms_device::device_index, g_ms_nb_connected, i, and USB_MAX_DMS_NUMBER.

    00174 {
    00175    U8 i;
    00176    // Reset Device MS struct
    00177    for(i=0; i<USB_MAX_DMS_NUMBER; i++)
    00178    {
    00179       g_ms_devices[i].device_index=0xFF;
    00180    }
    00181    g_ms_nb_connected=0;
    00182 }

Bool host_mem_install | ( | void |  | ) |

This fonction must be call when a device is connected.

Definition at line 187 of file host_mem.c.

References S_lun_info::b_first_unitready, S_lun_info::b_protected, CONTROL_GOOD, data_stage, S_usb_tree::device, S_dms_device::device_index, S_usb_interface::ep, FALSE, g_ms_nb_connected, g_ms_sel_dms, Get_class, Get_ep_addr, Get_nb_device, Get_nb_supported_interface, host_mem_inquiry(), host_mem_read_capacity(), host_ms_get_max_lun, Host_select_device, i, S_usb_device::interface, Is_ep_addr_in, LOG_STR_CODE, S_dms_device::lun_info, MS_CLASS, S_dms_device::nb_lun, S_dms_device::pipe_in, S_usb_endpoint::pipe_number, S_dms_device::pipe_out, TRUE, USB_MAX_DMS_NUMBER, USB_MAX_LUN_PER_DMS, and usb_tree.

    00188 {
    00189    U32 capacity;
    00190    U8  device_num;
    00191    U8  interface_num;
    00192    U8  lun_offset;
    00193    U8  i;
    00194
    00195    // Check all devices connected
    00196    for( device_num=0; device_num<Get_nb_device(); device_num++)
    00197    {
    00198       Host_select_device(device_num);
    00199
    00200       // Check all interfaces of device
    00201       for( interface_num=0; interface_num < Get_nb_supported_interface(); interface_num++ )
    00202       {
    00203          if( Get_class(interface_num)!= MS_CLASS )
    00204             continue;   // It is not a MS interface
    00205
    00206          LOG_STR_CODE(log_ms_connect);
    00207
    00208          // If it is not the first MS device
    00209          if( g_ms_nb_connected != 0 )
    00210          {
    00211             // Check if already installed
    00212             for( i=0; i<g_ms_nb_connected; i++ )
    00213             {
    00214                if( g_ms_devices[i].device_index == device_num )
    00215                   break;
    00216             }
    00217             if( i!=g_ms_nb_connected )
    00218                break;      // Device already installed
    00219             // Check struct space free
    00220             if( USB_MAX_DMS_NUMBER == g_ms_nb_connected )
    00221                break;      // Too many MS device connected
    00222          }
    00223
    00224          //** Start install
    00225          g_ms_devices[g_ms_nb_connected].device_index = device_num;
    00226
    00227          // Get correct physical pipes associated to IN/OUT Mass Storage Endpoints
    00228          if(Is_ep_addr_in(Get_ep_addr(interface_num,0)))
    00229          {  //Yes associate it to the MassStorage IN pipe
    00230             g_ms_devices[g_ms_nb_connected].pipe_in=usb_tree.device[device_num].interface[interface_num].ep[0].pipe_number;
    00231             g_ms_devices[g_ms_nb_connected].pipe_out=usb_tree.device[device_num].interface[interface_num].ep[1].pipe_number;
    00232          }
    00233          else
    00234          {  //No, invert...
    00235             g_ms_devices[g_ms_nb_connected].pipe_in=usb_tree.device[device_num].interface[interface_num].ep[1].pipe_number;
    00236             g_ms_devices[g_ms_nb_connected].pipe_out=usb_tree.device[device_num].interface[interface_num].ep[0].pipe_number;
    00237          }
    00238
    00239          // Get last LUN number on the device mass storage connected
    00240          if( CONTROL_GOOD != host_ms_get_max_lun() )
    00241            data_stage[0] = 0; // If STALL request then last LUN number = 0
    00242          g_ms_devices[g_ms_nb_connected].nb_lun = data_stage[0]+1;   // Save number of LUN = last LUN number + 1
    00243
    00244          if( USB_MAX_LUN_PER_DMS < g_ms_devices[g_ms_nb_connected].nb_lun )
    00245             g_ms_devices[g_ms_nb_connected].nb_lun = USB_MAX_LUN_PER_DMS;   // Limitation on the number of LUN
    00246
    00247          // Compute LUN offset of this new device
    00248          lun_offset = 0;
    00249          for( i=0; i<g_ms_nb_connected; i++)
    00250          {
    00251             lun_offset +=g_ms_devices[i].nb_lun;
    00252          }
    00253
    00254          // Add the new MS device in MS strut
    00255          g_ms_sel_dms = g_ms_nb_connected;
    00256          g_ms_nb_connected++;
    00257
    00258          // Initialize all LUN from the new MS device
    00259          for( i=0; i<g_ms_devices[g_ms_sel_dms].nb_lun; i++ )
    00260          {
    00261             // Init struct of LUN
    00262             g_ms_devices[g_ms_sel_dms].lun_info[i].b_first_unitready = FALSE;
    00263             g_ms_devices[g_ms_sel_dms].lun_info[i].b_protected       = TRUE;
    00264             // Init MS corresponding at LUN
    00265             host_mem_inquiry( lun_offset+i );
    00266             host_mem_read_capacity( lun_offset+i, &capacity );
    00267          }
    00268          return TRUE;   // Install interface/device done
    00269       }
    00270    }
    00271    return FALSE;   // No MSC install
    00272 }

Here is the call graph for this function:

void host_mem_uninstall | ( | void |  | ) |

This fonction must be call when a device is disconnected.

Definition at line 277 of file host_mem.c.

References S_usb_tree::device, S_usb_device::device_address, S_dms_device::device_index, FALSE, g_ms_nb_connected, i, TRUE, and usb_tree.

    00278 {
    00279    U8  device_num;
    00280    U8  i;
    00281    Bool b_last_ms_conected = TRUE;
    00282
    00283    for( i=g_ms_nb_connected; i!=0; i--)
    00284    {
    00285       device_num = g_ms_devices[i-1].device_index;
    00286       if( 0xFF == device_num )
    00287       {
    00288          // Device already remove but struct no updated
    00289          // it may be possible in case of many MS on USB HUB
    00290          g_ms_nb_connected--;
    00291          continue;
    00292       }
    00293       if( 0 == usb_tree.device[device_num].device_address )
    00294       {
    00295          g_ms_devices[i-1].device_index = 0xFF;
    00296          if( b_last_ms_conected )
    00297             g_ms_nb_connected--;
    00298       }else{
    00299          b_last_ms_conected = FALSE;
    00300       }
    00301    }
    00302 }

U8 host_mem_get_lun | ( | void |  | ) |

This fonction returns the number of LUN of the devices mass storage connected to the host.

Returns:
  The number of LUN

Definition at line 309 of file host_mem.c.

References g_ms_nb_connected, i, Is_host_ms_configured, and S_dms_device::nb_lun.

    00310 {
    00311    U8 i;
    00312    U8 host_ms_max_lun = 0;
    00313
    00314    if(!Is_host_ms_configured())
    00315       return 0;
    00316
    00317    for( i=0; i<g_ms_nb_connected; i++ )
    00318    {
    00319       host_ms_max_lun += g_ms_devices[i].nb_lun;
    00320    }
    00321    return host_ms_max_lun;
    00322 }

Ctrl_status host_mem_test_unit_ready | ( | U8 | lun | ) |

This fonction test the state of memory, and start the initialisation of the memory.

Returns:
  Ctrl_status It is ready -> CTRL_GOOD Memory unplug -> CTRL_NO_PRESENT Not initialize or change -> CTRL_BUSY A error occur -> CTRL_FAIL

Definition at line 337 of file host_mem.c.

References S_lun_info::b_first_unitready, S_lun_info::b_protected, S_cbw::cmd, CTRL_BUSY, CTRL_FAIL, CTRL_GOOD, CTRL_NO_PRESENT, S_cbw::dir, FALSE, g_ms_sel_dms, g_ms_sel_lun, g_readctx_b_run, host_mem_cbw_init(), host_mem_cbw_send(), host_mem_csw_read(), host_mem_mem_2_ram_stop(), host_mem_read_capacity(), host_mem_select_lun(), host_mem_wr_protect(), S_cbw::lgt, S_dms_device::lun_info, SBC_CMD_DIR_OUT, SBC_CMD_TEST_UNIT_READY, and TRUE.

    00338 {
    00339    Ctrl_status status;
    00340    U32   u32_nb_sector;
    00341
    00342    if( !host_mem_select_lun(lun) )
    00343       return CTRL_NO_PRESENT;
    00344    if (g_readctx_b_run)
    00345    {
    00346       host_mem_mem_2_ram_stop();
    00347       host_mem_select_lun(lun);
    00348    }
    00349
    00350    // Send CBW
    00351    host_mem_cbw_init();
    00352    // g_ms_cbw.lgt_lsb0 = 0;
    00353    g_ms_cbw.dir               = SBC_CMD_DIR_OUT;
    00354    g_ms_cbw.lgt               = 0x06;
    00355    g_ms_cbw.cmd               = SBC_CMD_TEST_UNIT_READY;
    00356    if( !host_mem_cbw_send() )
    00357       return CTRL_FAIL;
    00358
    00359    // Read CSW status
    00360    status = host_mem_csw_read();
    00361    if( status == CTRL_GOOD )
    00362    {
    00363       if( FALSE == g_ms_devices[g_ms_sel_dms].lun_info[g_ms_sel_lun].b_first_unitready )
    00364       {
    00365          // Signal the new diskl
    00366          status = CTRL_BUSY;
    00367          // U-Disk valid then update info LUN
    00368          if( CTRL_GOOD == host_mem_read_capacity( lun , &u32_nb_sector ) )
    00369          {
    00370             g_ms_devices[g_ms_sel_dms].lun_info[g_ms_sel_lun].b_protected       = host_mem_wr_protect( lun );
    00371             g_ms_devices[g_ms_sel_dms].lun_info[g_ms_sel_lun].b_first_unitready = TRUE;
    00372          }
    00373       }
    00374    }
    00375    else
    00376    {
    00377       g_ms_devices[g_ms_sel_dms].lun_info[g_ms_sel_lun].b_first_unitready = FALSE;
    00378    }
    00379    return status;
    00380 }

Here is the call graph for this function:

Ctrl_status host_mem_read_capacity | ( | U8 | lun,
|  | U32 _MEM_TYPE_SLOW_ * | u32_nb_sector |
| ) |  |  |

This fonction returns the address of the last valid sector.

Returns:
  *u16_nb_sector number of sector (sector = 512B)

  Ctrl_status It is ready -> CTRL_GOOD Memory unplug -> CTRL_NO_PRESENT Not initialize or change -> CTRL_BUSY A error occur -> CTRL_FAIL

Definition at line 392 of file host_mem.c.

References S_cbw::cmd, CTRL_FAIL, CTRL_NO_PRESENT, S_cbw::dir, DMS_pipe_in, g_ms_sel_dms, g_ms_sel_lun, g_readctx_b_run, host_get_data(), host_mem_cbw_init(), host_mem_cbw_send(), host_mem_csw_read(), host_mem_mem_2_ram_stop(), host_mem_select_lun(), host_mem_stall_management(), S_cbw::lgt, S_cbw::lgt_lsb0, S_dms_device::lun_info, MSB0, MSB1, MSB2, MSB3, PIPE_GOOD, PIPE_STALL, SBC_CMD_DIR_IN, SBC_CMD_READ_CAPACITY, and S_lun_info::u8_block_size.

    00393 {
    00394    U16   nb;
    00395    U8    datas[8], status;
    00396
    00397    if( !host_mem_select_lun(lun) )
    00398       return CTRL_NO_PRESENT;
    00399    if (g_readctx_b_run)
    00400    {
    00401       host_mem_mem_2_ram_stop();
    00402       host_mem_select_lun(lun);
    00403    }
    00404
    00405    // Send CBW
    00406    host_mem_cbw_init();
    00407    g_ms_cbw.lgt_lsb0          = 0x08;
    00408    g_ms_cbw.dir               = SBC_CMD_DIR_IN;
    00409    g_ms_cbw.lgt               = 0x0A;
    00410    g_ms_cbw.cmd               = SBC_CMD_READ_CAPACITY;
    00411    if( !host_mem_cbw_send() )
    00412       return CTRL_FAIL;
    00413
    00414    // Receiv the capacity data
    00415    nb=8;
    00416    status = host_get_data(DMS_pipe_in(),&nb,datas);
    00417    if( PIPE_GOOD != status )
    00418    {
    00419       if( PIPE_STALL == status )
    00420         host_mem_stall_management();
    00421       host_mem_csw_read();
    00422       return CTRL_FAIL;
    00423    }
    00424
    00425    // Get last sector address
    00426    MSB0(*u32_nb_sector)      = datas[0];
    00427    MSB1(*u32_nb_sector)      = datas[1];
    00428    MSB2(*u32_nb_sector)      = datas[2];
    00429    MSB3(*u32_nb_sector)      = datas[3];
    00430    // Get block size (unit 512B)
    00431    g_ms_devices[ g_ms_sel_dms ].lun_info[ g_ms_sel_lun ].u8_block_size = datas[6]/2; // Block size MSB
    00432
    00433    // Read CSW status
    00434    return host_mem_csw_read();
    00435 }

Here is the call graph for this function:

U8 host_mem_read_sector_size | ( | U8 | lun | ) |

Returns the physical sector of the memory

Parameters:

  | lun,global | lun selected (global!=lun of specific USB device)

Returns:
  size of physical sector of disk (unit 512B)

Definition at line 444 of file host_mem.c.

References g_ms_sel_dms, g_ms_sel_lun, host_mem_select_lun(), host_mem_test_unit_ready(), S_dms_device::lun_info, and S_lun_info::u8_block_size.

    00445 {
    00446    host_mem_test_unit_ready( lun );
    00447    if( !host_mem_select_lun(lun) )
    00448       return 1;
    00449    return g_ms_devices[g_ms_sel_dms].lun_info[g_ms_sel_lun].u8_block_size;
    00450 }

Here is the call graph for this function:

Bool host_mem_wr_protect_cache | ( | U8 | lun | ) |

Return the write protected mode (cache used) This information is in a cache to speed up command

Parameters:

  | lun,global | lun selected (global!=lun of specific USB device)

Returns:
  TRUE, the memory is protected

Definition at line 460 of file host_mem.c.

References S_lun_info::b_protected, g_ms_sel_dms, g_ms_sel_lun, host_mem_select_lun(), S_dms_device::lun_info, and TRUE.

    00461 {
    00462    if( !host_mem_select_lun(lun) )
    00463       return TRUE;   // no present
    00464
    00465    return g_ms_devices[g_ms_sel_dms].lun_info[g_ms_sel_lun].b_protected;
    00466 }

Here is the call graph for this function:

Bool host_mem_wr_protect | ( | U8 | lun | ) |

Return the write protected mode (no cache)

Parameters:

  | lun,global | lun selected (global!=lun of specific USB device)

Returns:
  TRUE, the memory is protected

Definition at line 475 of file host_mem.c.

References S_cbw::cmd, S_cbw::dir, DMS_pipe_in, g_ms_sel_dms, g_ms_sel_lun, g_readctx_b_run, host_get_data(), host_mem_cbw_init(), host_mem_cbw_send(), host_mem_csw_read(), host_mem_mem_2_ram_stop(), host_mem_select_lun(), host_mem_stall_management(), S_cbw::lgt, S_cbw::lgt_lsb0, PIPE_GOOD, PIPE_STALL, SBC_CMD_DIR_IN, SBC_CMD_MODE_SENSE_6, TRUE, S_cbw::u8_data, and USB_SUPPORT_MS_SECTOR_WR_MAX.

    00476 {
    00477    U16 nb;
    00478    U8 write_code[0x0C], status;
    00479
    00480    if( !host_mem_select_lun(lun) )
    00481       return TRUE;
    00482
    00483    if( USB_SUPPORT_MS_SECTOR_WR_MAX < g_ms_devices[ g_ms_sel_dms ].lun_info[ g_ms_sel_lun ].u8_block_size )
    00484       return TRUE;   // No supported this lun in write mode, because sector size != 512B
    00485
    00486    if (g_readctx_b_run)
    00487    {
    00488       host_mem_mem_2_ram_stop();
    00489       host_mem_select_lun(lun);
    00490    }
    00491
    00492    // Send CBW
    00493    host_mem_cbw_init();
    00494    g_ms_cbw.dir               = SBC_CMD_DIR_IN;
    00495    g_ms_cbw.lgt               = 0x06;
    00496    g_ms_cbw.cmd               = SBC_CMD_MODE_SENSE_6;
    00497    g_ms_cbw.lgt_lsb0          = 0x0C;
    00498    //g_ms_cbw.u8_data[16-16]  = 0x00;                 // 16 - CBWCB1 - Option
    00499    g_ms_cbw.u8_data[17-16]    = 0x3F;                 // 17 - CBWCB2 - Page Code 3F = return all mode pages
    00500    //g_ms_cbw.u8_data[18-16]  = 0x00;                 // 18 - CBWCB3 - reserved
    00501    g_ms_cbw.u8_data[19-16]    = 0x0C;                 // 19 - CBWCB4 - Allocation Length
    00502    //g_ms_cbw.u8_data[20-16]  = 0x00;                 // 20 - CBWCB5 - Control
    00503    if( !host_mem_cbw_send() )
    00504       return TRUE;
    00505
    00506    // Receiv
    00507    nb=0x0C;
    00508    status = host_get_data(DMS_pipe_in(),&nb,write_code);
    00509    if( PIPE_GOOD != status )
    00510    {
    00511       if( PIPE_STALL == status )
    00512         host_mem_stall_management();
    00513       write_code[2] = 0x00;   // error then no write protected by defaut
    00514    }
    00515
    00516    // Read CSW status
    00517    host_mem_csw_read();       // ignore error on CSW (in case of specific Udisk)
    00518
    00519    return (write_code[2] == 0x80);
    00520 }

Here is the call graph for this function:

Bool host_mem_removal | ( | void |  | ) |

This fonction inform about the memory type.

Returns:
  FASLE -> The memory isn't removal

Definition at line 527 of file host_mem.c.

References TRUE.

    00528 {
    00529    return TRUE;
    00530 }

Ctrl_status host_mem_read_format_capacity | ( | U8 | lun | ) |

Ctrl_status host_mem_inquiry | ( | U8 | lun | ) |

Read inquiry datas

Parameters:

  | lun,global | lun selected (global!=lun of specific USB device)

Returns:
  Ctrl_status It is ready -> CTRL_GOOD Memory unplug -> CTRL_NO_PRESENT Not initialize or change -> CTRL_BUSY A error occur -> CTRL_FAIL

Definition at line 600 of file host_mem.c.

References S_cbw::cmd, CTRL_FAIL, CTRL_NO_PRESENT, S_cbw::dir, DMS_pipe_in, g_readctx_b_run, host_get_data(), host_mem_cbw_init(), host_mem_cbw_send(), host_mem_csw_read(), host_mem_mem_2_ram_stop(), host_mem_select_lun(), host_mem_stall_management(), S_cbw::lgt, S_cbw::lgt_lsb0, PIPE_STALL, SBC_CMD_DIR_IN, SBC_CMD_INQUIRY, and S_cbw::u8_data.

    00601 {
    00602    U16 nb;
    00603    U8 datas[31], status;
    00604
    00605    if( !host_mem_select_lun(lun) )
    00606       return CTRL_NO_PRESENT;
    00607    if (g_readctx_b_run)
    00608    {
    00609       host_mem_mem_2_ram_stop();
    00610       host_mem_select_lun(lun);
    00611    }
    00612
    00613    host_mem_cbw_init();
    00614    g_ms_cbw.lgt_lsb0          = 0x24;
    00615    g_ms_cbw.dir               = SBC_CMD_DIR_IN;
    00616    g_ms_cbw.lgt               = 0x06;
    00617    g_ms_cbw.cmd               = SBC_CMD_INQUIRY;
    00618    //g_ms_cbw.u8_data[16-16]  = 0x00;              // 16 - CBWCB1 - Option
    00619    //g_ms_cbw.u8_data[17-16]  = 0x00;              // 17 - CBWCB2 - Page or operation code
    00620    //g_ms_cbw.u8_data[18-16]  = 0x00;              // 18 - CBWCB3 - reserved
    00621    g_ms_cbw.u8_data[19-16]    = 0x24;              // 19 - CBWCB4 - Allocation Length
    00622    //g_ms_cbw.u8_data[20-16]  = 0x00;              // 20 - CBWCB5 - Control
    00623    if( !host_mem_cbw_send() )
    00624       return CTRL_FAIL;
    00625
    00626    // Receiv Inquiry data
    00627    // Transfer data ...
    00628    nb=31;
    00629    status = host_get_data(DMS_pipe_in(),&nb,datas);
    00630    // Ignore inquiry data
    00631    if( PIPE_STALL == status )
    00632      host_mem_stall_management();
    00633
    00634    // Read CSW status
    00635    return host_mem_csw_read();
    00636 }

Here is the call graph for this function:

Ctrl_status host_mem_mem_2_ram | ( | U8 | lun,
|  | U32 | addr,
|  | U8 * | ram |
| ) |  |  |

Read sectors on lun

Parameters:

  | lun,global | lun selected (global!=lun of specific USB device)
  | addr | Sector address to start read (unit 512B)
  | *ram | buffer 512B to store the sector readed

Returns:
  Ctrl_status It is ready -> CTRL_GOOD Memory unplug -> CTRL_NO_PRESENT Not initialize or change -> CTRL_BUSY A error occur -> CTRL_FAIL

Definition at line 750 of file host_mem.c.

References S_cbw::cmd, CTRL_FAIL, CTRL_GOOD, S_dms_device::device_index, S_cbw::dir, DMS_pipe_in, FALSE, g_ms_sel_dms, g_ms_sel_lun, g_readctx_b_run, g_readctx_u16_sector, g_readctx_u32_addr, g_readctx_u8_device, g_readctx_u8_lun, host_get_data(), host_mem_cbw_init(), host_mem_cbw_send(), host_mem_csw_read(), host_mem_mem_2_ram_stop(), host_mem_select_lun(), host_mem_stall_management(), HOST_MS_CBW_DATA_POS, HOST_SECTOR_SIZE, S_cbw::lgt, S_cbw::lgt_lsb0, S_cbw::lgt_lsb1, S_cbw::lgt_lsb2, S_cbw::lgt_lsb3, S_dms_device::lun_info, MSB0, MSB1, MSB2, MSB3, NULL, PIPE_GOOD, PIPE_STALL, SBC_CMD_DIR_IN, SBC_CMD_READ_10, TRUE, S_lun_info::u8_block_size, and S_cbw::u8_data.

    00751 {
    00752    U8    status;
    00753    U16   nb;
    00754    U8    u8_nb_sec_ignore_at_beg, u8_sector_size;
    00755    U32   u32_address;
    00756
    00757    if( !host_mem_select_lun(lun) )
    00758       return CTRL_FAIL; // no present
    00759
    00760    u8_nb_sec_ignore_at_beg    = 0;
    00761
    00762    if( g_readctx_b_run )
    00763    {
    00764       // Check if the sector requested is the following of previous read command
    00765       if( ( g_ms_devices[g_ms_sel_dms].device_index != g_readctx_u8_device)
    00766       ||  ( g_readctx_u8_lun != g_ms_sel_lun)
    00767       ||  ( g_readctx_u32_addr > addr)
    00768       ||  ( (g_readctx_u32_addr+g_readctx_u16_sector) <= addr ) )
    00769       {
    00770          host_mem_mem_2_ram_stop();
    00771          host_mem_select_lun(lun);
    00772       }else{
    00773          u8_nb_sec_ignore_at_beg = addr-g_readctx_u32_addr;
    00774       }
    00775    }
    00776
    00777    if( !g_readctx_b_run )
    00778    {
    00779       u8_sector_size = g_ms_devices[ g_ms_sel_dms ].lun_info[ g_ms_sel_lun ].u8_block_size;
    00780
    00781       // Compute the address in physical sector units
    00782       u32_address                = addr / u8_sector_size;
    00783       // Compute the number of sector to ignore from the beginning of the physical sector
    00784       u8_nb_sec_ignore_at_beg    = addr % u8_sector_size;
    00785       // Update context
    00786       g_readctx_u16_sector       = u8_sector_size;
    00787       g_readctx_u32_addr         = addr - u8_nb_sec_ignore_at_beg;
    00788
    00789       // Send CBW
    00790       host_mem_cbw_init();
    00791       g_ms_cbw.lgt_lsb0          = 0x00;
    00792       g_ms_cbw.lgt_lsb1          = u8_sector_size * (512/256);
    00793       g_ms_cbw.lgt_lsb2          = 0x00;
    00794       g_ms_cbw.lgt_lsb3          = 0x00;
    00795       g_ms_cbw.dir               = SBC_CMD_DIR_IN;
    00796       g_ms_cbw.lgt               = 0x0A;
    00797       g_ms_cbw.cmd               = SBC_CMD_READ_10;
    00798       //g_ms_cbw.u8_data[16-HOST_MS_CBW_DATA_POS]  = 0x00;              // 16 - CBWCB1 - Option
    00799       g_ms_cbw.u8_data[17-HOST_MS_CBW_DATA_POS]    = MSB0(u32_address); // 17 - CBWCB2 - MSB3(Logical Block Address)
    00800       g_ms_cbw.u8_data[18-HOST_MS_CBW_DATA_POS]    = MSB1(u32_address); // 18 - CBWCB3 - MSB2(Logical Block Address)
    00801       g_ms_cbw.u8_data[19-HOST_MS_CBW_DATA_POS]    = MSB2(u32_address); // 19 - CBWCB4 - MSB1(Logical Block Address)
    00802       g_ms_cbw.u8_data[20-HOST_MS_CBW_DATA_POS]    = MSB3(u32_address); // 20 - CBWCB5 - MSB0(Logical Block Address)
    00803       //g_ms_cbw.u8_data[21-HOST_MS_CBW_DATA_POS]  = 0x00;              // 21 - CBWCB6 - reserved
    00804       //g_ms_cbw.u8_data[22-HOST_MS_CBW_DATA_POS]  = 0x00;              // 22 - CBWCB7 - MSB1(Transfer Length)
    00805       g_ms_cbw.u8_data[23-HOST_MS_CBW_DATA_POS]    = 0x01;              // 23 - CBWCB8 - MSB0(Transfer Length)
    00806       //g_ms_cbw.u8_data[24-HOST_MS_CBW_DATA_POS]  = 0x00;              // 24 - CBWCB9 - Control
    00807       if( !host_mem_cbw_send() )
    00808          return CTRL_FAIL;
    00809    }
    00810
    00811    if( 0 != u8_nb_sec_ignore_at_beg )
    00812    {
    00813       // Ignore eventualy first sector of command (in case of device with sector >512B)
    00814       // Update sector remaining
    00815       g_readctx_u16_sector -= u8_nb_sec_ignore_at_beg;
    00816       g_readctx_u32_addr   += u8_nb_sec_ignore_at_beg;
    00817       while( 0 != u8_nb_sec_ignore_at_beg )
    00818       {
    00819          // Read one sector
    00820          nb=HOST_SECTOR_SIZE;
    00821          status = host_get_data(DMS_pipe_in(),&nb,NULL);
    00822          if(PIPE_GOOD != status)
    00823          {
    00824             if(PIPE_STALL==status)
    00825                host_mem_stall_management();
    00826             host_mem_csw_read();
    00827             return CTRL_FAIL;
    00828          }
    00829          u8_nb_sec_ignore_at_beg--;
    00830       }
    00831    }
    00832
    00833    // Transfer data ...
    00834    g_readctx_u16_sector--;
    00835    g_readctx_u32_addr++;
    00836    nb=HOST_SECTOR_SIZE;
    00837    status = host_get_data(DMS_pipe_in(),&nb,ram);
    00838    if(PIPE_GOOD != status)
    00839    {
    00840       if(PIPE_STALL==status)
    00841          host_mem_stall_management();
    00842       host_mem_csw_read();
    00843       return CTRL_FAIL;
    00844    }
    00845
    00846    if( 0 != g_readctx_u16_sector )
    00847    {
    00848       //  Update context
    00849       g_readctx_u8_device = g_ms_devices[g_ms_sel_dms].device_index;
    00850       g_readctx_u8_lun = g_ms_sel_lun;
    00851       g_readctx_b_run = TRUE;
    00852       return CTRL_GOOD;
    00853    }
    00854
    00855    // Read CSW status
    00856    g_readctx_b_run = FALSE;
    00857    return host_mem_csw_read();
    00858 }

Here is the call graph for this function:

Ctrl_status host_mem_mem_2_ram_stop | ( | void |  | ) |

This function terminates the tranfer of a physical sector from memory.

Returns:
  Ctrl_status It is ready -> CTRL_GOOD Memory unplug -> CTRL_NO_PRESENT Not initialized or changed -> CTRL_BUSY An error occurred -> CTRL_FAIL

Definition at line 869 of file host_mem.c.

References CTRL_FAIL, DMS_pipe_in, FALSE, g_ms_sel_dms, g_ms_sel_lun, g_readctx_b_run, g_readctx_u16_sector, g_readctx_u8_device, g_readctx_u8_lun, host_get_data(), host_mem_csw_read(), host_mem_stall_management(), HOST_SECTOR_SIZE, Host_select_device, NULL, PIPE_GOOD, and PIPE_STALL.

    00870 {
    00871    U16 nb;
    00872    U8  status;
    00873
    00874    g_readctx_b_run = FALSE;
    00875
    00876    g_ms_sel_lun = g_readctx_u8_lun;
    00877    g_ms_sel_dms = g_readctx_u8_device;
    00878    Host_select_device( g_ms_devices[g_ms_sel_dms].device_index );
    00879
    00880    while( 0 != g_readctx_u16_sector )
    00881    {
    00882       // Read one sector
    00883       nb=HOST_SECTOR_SIZE;
    00884       status = host_get_data(DMS_pipe_in(),&nb,NULL);
    00885       if(PIPE_GOOD != status)
    00886       {
    00887          if(PIPE_STALL==status)
    00888             host_mem_stall_management();
    00889          host_mem_csw_read();
    00890          return CTRL_FAIL;
    00891       }
    00892       g_readctx_u16_sector--;
    00893    }
    00894
    00895    // Read CSW status
    00896    return host_mem_csw_read();
    00897 }

Here is the call graph for this function:

Ctrl_status host_mem_ram_2_mem | ( | U8 | lun,
|  | U32 | addr,
|  | U8 * | ram |
| ) |  |  |

Write a sectors

Parameters:

  | lun,global | lun selected (global!=lun of specific USB device)
  | addr | Sector address to start write (unit 512B)
  | *ram | buffer 512B with the data to store in the sector writed

Returns:
  Ctrl_status It is ready -> CTRL_GOOD Memory unplug -> CTRL_NO_PRESENT Not initialize or change -> CTRL_BUSY A error occur -> CTRL_FAIL

Definition at line 912 of file host_mem.c.

References S_cbw::cmd, CTRL_FAIL, CTRL_GOOD, S_cbw::dir, DMS_pipe_out, g_ms_sel_dms, g_ms_sel_lun, g_readctx_b_run, host_mem_cbw_init(), host_mem_cbw_send(), host_mem_csw_read(), host_mem_mem_2_ram(), host_mem_mem_2_ram_stop(), host_mem_select_lun(), host_mem_stall_management(), HOST_MS_CBW_DATA_POS, HOST_SECTOR_SIZE, host_send_data(), S_cbw::lgt, S_cbw::lgt_lsb1, S_dms_device::lun_info, MSB0, MSB1, MSB2, MSB3, PIPE_STALL, SBC_CMD_DIR_OUT, SBC_CMD_WRITE_10, S_lun_info::u8_block_size, S_cbw::u8_data, and USB_SUPPORT_MS_SECTOR_WR_MAX.

    00913 {
    00914    U8    status;
    00915    U16   nb, u16_sector;
    00916    U8    u8_nb_sec_ignore_at_beg, u8_sector_size;
    00917    U32   u32_address;
    00918 #if (USB_SUPPORT_MS_SECTOR_WR_MAX != 1)
    00919    Ctrl_status read_status;
    00920    U8    u8_i,u8_j;
    00921    U32   u32_addr;
    00922 #endif
    00923
    00924    if( !host_mem_select_lun(lun) )
    00925       return CTRL_FAIL; // no present
    00926
    00927    u8_sector_size = g_ms_devices[ g_ms_sel_dms ].lun_info[ g_ms_sel_lun ].u8_block_size;
    00928
    00929    if( USB_SUPPORT_MS_SECTOR_WR_MAX < u8_sector_size )
    00930       return CTRL_FAIL; // No supported this lun in write mode, because sector size != 512B
    00931
    00932
    00933    // Compute the address in physical sector units
    00934    u32_address                = addr / u8_sector_size;
    00935    // Compute the number of sector to ignore from the beginning of the physical sector
    00936    u8_nb_sec_ignore_at_beg    = addr % u8_sector_size;
    00937    // Update context
    00938    u16_sector                 = u8_sector_size;
    00939
    00940 #if (USB_SUPPORT_MS_SECTOR_WR_MAX != 1)
    00941    // If the sector size > 512B then read sector and store it in a buffer
    00942    if( 1 != u8_sector_size )
    00943    {
    00944       u32_addr = addr - u8_nb_sec_ignore_at_beg;
    00945       for( u8_i=0,u8_j=0 ; u8_i<u8_sector_size; u8_i++,u32_addr++ )
    00946       {
    00947          if( u8_i == u8_nb_sec_ignore_at_beg )
    00948             continue;
    00949          read_status = host_mem_mem_2_ram( lun, u32_addr, &g_ms_cache[u8_j*512] );
    00950          if( CTRL_GOOD != read_status )
    00951             return CTRL_GOOD;
    00952          u8_j++;
    00953       }
    00954    }
    00955 #endif
    00956
    00957    if (g_readctx_b_run)
    00958    {
    00959       host_mem_mem_2_ram_stop();
    00960       host_mem_select_lun(lun);
    00961    }
    00962
    00963    // Send CBW
    00964    host_mem_cbw_init();
    00965    //g_ms_cbw.lgt_lsb0        = 0x00;
    00966    g_ms_cbw.lgt_lsb1          = u8_sector_size * (512/256);
    00967    //g_ms_cbw.lgt_lsb2        = 0x00;
    00968    //g_ms_cbw.lgt_lsb3        = 0x00;
    00969    g_ms_cbw.dir               = SBC_CMD_DIR_OUT;
    00970    g_ms_cbw.lgt               = 0x0A;
    00971    g_ms_cbw.cmd               = SBC_CMD_WRITE_10;
    00972    //g_ms_cbw.u8_data[16-HOST_MS_CBW_DATA_POS]  = 0x00;        // 16 - CBWCB1 - Option
    00973    g_ms_cbw.u8_data[17-HOST_MS_CBW_DATA_POS]    = MSB0(u32_address);  // 17 - CBWCB2 - MSB3(Logical Block Address)
    00974    g_ms_cbw.u8_data[18-HOST_MS_CBW_DATA_POS]    = MSB1(u32_address);  // 18 - CBWCB3 - MSB2(Logical Block Address)
    00975    g_ms_cbw.u8_data[19-HOST_MS_CBW_DATA_POS]    = MSB2(u32_address);  // 19 - CBWCB4 - MSB1(Logical Block Address)
    00976    g_ms_cbw.u8_data[20-HOST_MS_CBW_DATA_POS]    = MSB3(u32_address);  // 20 - CBWCB5 - MSB0(Logical Block Address)
    00977    //g_ms_cbw.u8_data[21-HOST_MS_CBW_DATA_POS]  = 0x00;        // 21 - CBWCB6 - reserved
    00978    //g_ms_cbw.u8_data[22-HOST_MS_CBW_DATA_POS]  = 0x00;        // 22 - CBWCB7 - MSB1(Transfer Length)
    00979    g_ms_cbw.u8_data[23-HOST_MS_CBW_DATA_POS]    = 0x01;        // 23 - CBWCB8 - MSB0(Transfer Length)
    00980    //g_ms_cbw.u8_data[24-HOST_MS_CBW_DATA_POS]  = 0x00;        // 24 - CBWCB9 - Control
    00981    if( !host_mem_cbw_send() )
    00982       return CTRL_FAIL;
    00983
    00984 #if (USB_SUPPORT_MS_SECTOR_WR_MAX != 1)
    00985    if( 0 != u8_nb_sec_ignore_at_beg )
    00986    {
    00987       // Ignore eventualy first sector of command (in case of device with sector >512B)
    00988       // Update sector remaining
    00989       u16_sector -= u8_nb_sec_ignore_at_beg;
    00990       for( u8_i=0; u8_i < u8_nb_sec_ignore_at_beg; u8_i++ )
    00991       {
    00992          // Transfer data ...
    00993          nb=HOST_SECTOR_SIZE;
    00994          status = host_send_data(DMS_pipe_out(),nb,&g_ms_cache[u8_i*512]);
    00995          if(status==PIPE_STALL)
    00996          {
    00997             host_mem_stall_management();
    00998             host_mem_csw_read();
    00999             return CTRL_FAIL;
    01000          }
    01001       }
    01002    }
    01003 #endif
    01004
    01005    // Transfer data ...
    01006    u16_sector--;
    01007    nb=HOST_SECTOR_SIZE;
    01008    status = host_send_data(DMS_pipe_out(),nb,ram);
    01009    if(status==PIPE_STALL)
    01010    {
    01011       host_mem_stall_management();
    01012       host_mem_csw_read();
    01013       return CTRL_FAIL;
    01014    }
    01015
    01016 #if (USB_SUPPORT_MS_SECTOR_WR_MAX != 1)
    01017    while( 0 != u16_sector )
    01018    {
    01019       // Transfer data ...
    01020       nb=HOST_SECTOR_SIZE;
    01021       status = host_send_data(DMS_pipe_out(),nb,&g_ms_cache[u8_nb_sec_ignore_at_beg*512]);
    01022       if(status==PIPE_STALL)
    01023       {
    01024          host_mem_stall_management();
    01025          host_mem_csw_read();
    01026          return CTRL_FAIL;
    01027       }
    01028       u8_nb_sec_ignore_at_beg++;
    01029       u16_sector--;
    01030    }
    01031 #endif
    01032
    01033    // Read CSW status
    01034    return host_mem_csw_read();
    01035 }

Here is the call graph for this function:

Ctrl_status host_mem_mem_2_ram_ext | ( | U8 | lun,
|  | U32 | addr,
|  | U8 * | ram,
|  | U8 | u8_nb_sector |
| ) |  |  |

Read sectors on lun

Parameters:

  | lun,global | lun selected (global!=lun of specific USB device)
  | addr | Sector address to start read (unit 512B)
  | *ram | buffer 512B to store the sector readed

Returns:
  Ctrl_status It is ready -> CTRL_GOOD Memory unplug -> CTRL_NO_PRESENT Not initialize or change -> CTRL_BUSY A error occur -> CTRL_FAIL

Definition at line 1051 of file host_mem.c.

References S_cbw::cmd, CTRL_FAIL, CTRL_GOOD, S_cbw::dir, DMS_pipe_in, FALSE, g_ms_sel_dms, g_ms_sel_lun, g_readctx_b_run, host_get_data(), host_mem_cbw_init(), host_mem_cbw_send(), host_mem_csw_read(), host_mem_mem_2_ram(), host_mem_mem_2_ram_stop(), host_mem_select_lun(), host_mem_stall_management(), HOST_MS_CBW_DATA_POS, HOST_SECTOR_SIZE, S_cbw::lgt, S_cbw::lgt_lsb0, S_cbw::lgt_lsb1, S_cbw::lgt_lsb2, S_cbw::lgt_lsb3, LSB0, LSB1, LSB2, LSB3, S_dms_device::lun_info, MSB0, MSB1, MSB2, MSB3, PIPE_GOOD, PIPE_STALL, SBC_CMD_DIR_IN, SBC_CMD_READ_10, S_lun_info::u8_block_size, and S_cbw::u8_data.

    01052 {
    01053    Ctrl_status status;
    01054    U8    u8_status;
    01055    U8    u8_sector_size;
    01056    U16   nb;
    01057    U32   dCBWDataTransferLength;
    01058
    01059    if( !host_mem_select_lun(lun) )
    01060       return CTRL_FAIL; // no present
    01061
    01062    u8_sector_size = g_ms_devices[ g_ms_sel_dms ].lun_info[ g_ms_sel_lun ].u8_block_size;
    01063
    01064    if( 1 < u8_sector_size )
    01065    {
    01066       // For U-Disk with large block then use standard routine
    01067       while( u8_nb_sector != 0 ) {
    01068          status = host_mem_mem_2_ram(lun, addr, ram);
    01069          if (status != CTRL_GOOD) return status;
    01070          u8_nb_sector--;
    01071          addr++;
    01072          ram = (U8*)ram + HOST_SECTOR_SIZE;
    01073       }
    01074       return CTRL_GOOD;
    01075    }
    01076
    01077    if (g_readctx_b_run)
    01078    {
    01079       host_mem_mem_2_ram_stop();
    01080       host_mem_select_lun(lun);
    01081    }
    01082
    01083    dCBWDataTransferLength = (U32)u8_nb_sector * 512;
    01084
    01085    // Send CBW
    01086    host_mem_cbw_init();
    01087    g_ms_cbw.lgt_lsb0          = LSB0(dCBWDataTransferLength);     //  8 - LSB0W(dCBWDataTransferLength)
    01088    g_ms_cbw.lgt_lsb1          = LSB1(dCBWDataTransferLength);     //  9 - LSB1W(dCBWDataTransferLength)
    01089    g_ms_cbw.lgt_lsb2          = LSB2(dCBWDataTransferLength);     // 10 - LSB2W(dCBWDataTransferLength)
    01090    g_ms_cbw.lgt_lsb3          = LSB3(dCBWDataTransferLength);     // 11 - LSB3W(dCBWDataTransferLength)
    01091    g_ms_cbw.dir               = SBC_CMD_DIR_IN;
    01092    g_ms_cbw.lgt               = 0x0A;
    01093    g_ms_cbw.cmd               = SBC_CMD_READ_10;
    01094    //g_ms_cbw.u8_data[16-HOST_MS_CBW_DATA_POS]  = 0x00;           // 16 - CBWCB1 - Option
    01095    g_ms_cbw.u8_data[17-HOST_MS_CBW_DATA_POS]    = MSB0(addr);     // 17 - CBWCB2 - MSB3(Logical Block Address)
    01096    g_ms_cbw.u8_data[18-HOST_MS_CBW_DATA_POS]    = MSB1(addr);     // 18 - CBWCB3 - MSB2(Logical Block Address)
    01097    g_ms_cbw.u8_data[19-HOST_MS_CBW_DATA_POS]    = MSB2(addr);     // 19 - CBWCB4 - MSB1(Logical Block Address)
    01098    g_ms_cbw.u8_data[20-HOST_MS_CBW_DATA_POS]    = MSB3(addr);     // 20 - CBWCB5 - MSB0(Logical Block Address)
    01099    //g_ms_cbw.u8_data[21-HOST_MS_CBW_DATA_POS]  = 0x00;           // 21 - CBWCB6 - reserved
    01100    //g_ms_cbw.u8_data[22-HOST_MS_CBW_DATA_POS]  = 0x00;           // 22 - CBWCB7 - MSB1(Transfer Length)
    01101    g_ms_cbw.u8_data[23-HOST_MS_CBW_DATA_POS]    = u8_nb_sector;   // 23 - CBWCB8 - MSB0(Transfer Length)
    01102    //g_ms_cbw.u8_data[24-HOST_MS_CBW_DATA_POS]  = 0x00;           // 24 - CBWCB9 - Control
    01103    if( !host_mem_cbw_send() )
    01104       return CTRL_FAIL;
    01105
    01106    // Transfer data ...
    01107    nb=HOST_SECTOR_SIZE;
    01108    while( u8_nb_sector!= 0 )
    01109    {
    01110       u8_status = host_get_data(DMS_pipe_in(),&nb,ram);
    01111       if(PIPE_GOOD != u8_status)
    01112       {
    01113          if(PIPE_STALL==u8_status)
    01114             host_mem_stall_management();
    01115          host_mem_csw_read();
    01116          return CTRL_FAIL;
    01117       }
    01118       u8_nb_sector--;
    01119       ram = (U8*)ram + HOST_SECTOR_SIZE;
    01120    }
    01121
    01122    // Read CSW status
    01123    g_readctx_b_run = FALSE;
    01124    return host_mem_csw_read();
    01125 }

Here is the call graph for this function:

Ctrl_status host_mem_ram_2_mem_ext | ( | U8 | lun,
|  | U32 | addr,
|  | U8 * | ram,
|  | U8 | u8_nb_sector |
| ) |  |  |

Write a sectors

Parameters:

  | lun,global | lun selected (global!=lun of specific USB device)
  | addr | Sector address to start write (unit 512B)
  | u8_nb_sector | Number of sector to write
  | *ram | buffer with the data to store in the sector writed

Returns:
  Ctrl_status It is ready -> CTRL_GOOD Memory unplug -> CTRL_NO_PRESENT Not initialize or change -> CTRL_BUSY A error occur -> CTRL_FAIL

Definition at line 1142 of file host_mem.c.

References S_cbw::cmd, CTRL_FAIL, S_cbw::dir, DMS_pipe_out, g_ms_sel_dms, g_ms_sel_lun, g_readctx_b_run, host_mem_cbw_init(), host_mem_cbw_send(), host_mem_csw_read(), host_mem_mem_2_ram_stop(), host_mem_select_lun(), host_mem_stall_management(), HOST_MS_CBW_DATA_POS, HOST_SECTOR_SIZE, host_send_data(), S_cbw::lgt, S_cbw::lgt_lsb0, S_cbw::lgt_lsb1, S_cbw::lgt_lsb2, S_cbw::lgt_lsb3, LSB0, LSB1, LSB2, LSB3, S_dms_device::lun_info, MSB0, MSB1, MSB2, MSB3, PIPE_STALL, SBC_CMD_DIR_OUT, SBC_CMD_WRITE_10, S_lun_info::u8_block_size, and S_cbw::u8_data.

    01143 {
    01144    U8    status;
    01145    U16   nb;
    01146    U8    u8_sector_size;
    01147    U32   dCBWDataTransferLength;
    01148
    01149    if( !host_mem_select_lun(lun) )
    01150       return CTRL_FAIL; // no present
    01151
    01152    u8_sector_size = g_ms_devices[ g_ms_sel_dms ].lun_info[ g_ms_sel_lun ].u8_block_size;
    01153
    01154    if( 1 < u8_sector_size )
    01155       return CTRL_FAIL; // No supported this lun in write mode, because sector size != 512B
    01156
    01157
    01158    if (g_readctx_b_run)
    01159    {
    01160       host_mem_mem_2_ram_stop();
    01161       host_mem_select_lun(lun);
    01162    }
    01163
    01164    dCBWDataTransferLength = (U32)u8_nb_sector * 512;
    01165
    01166    // Send CBW
    01167    host_mem_cbw_init();
    01168    g_ms_cbw.lgt_lsb0          = LSB0(dCBWDataTransferLength);  //  8 - LSB0W(dCBWDataTransferLength)
    01169    g_ms_cbw.lgt_lsb1          = LSB1(dCBWDataTransferLength);  //  9 - LSB1W(dCBWDataTransferLength)
    01170    g_ms_cbw.lgt_lsb2          = LSB2(dCBWDataTransferLength);  // 10 - LSB2W(dCBWDataTransferLength)
    01171    g_ms_cbw.lgt_lsb3          = LSB3(dCBWDataTransferLength);  // 11 - LSB3W(dCBWDataTransferLength)
    01172    g_ms_cbw.dir               = SBC_CMD_DIR_OUT;
    01173    g_ms_cbw.lgt               = 0x0A;
    01174    g_ms_cbw.cmd               = SBC_CMD_WRITE_10;
    01175    //g_ms_cbw.u8_data[16-HOST_MS_CBW_DATA_POS]  = 0x00;        // 16 - CBWCB1 - Option
    01176    g_ms_cbw.u8_data[17-HOST_MS_CBW_DATA_POS]    = MSB0(addr);  // 17 - CBWCB2 - MSB3(Logical Block Address)
    01177    g_ms_cbw.u8_data[18-HOST_MS_CBW_DATA_POS]    = MSB1(addr);  // 18 - CBWCB3 - MSB2(Logical Block Address)
    01178    g_ms_cbw.u8_data[19-HOST_MS_CBW_DATA_POS]    = MSB2(addr);  // 19 - CBWCB4 - MSB1(Logical Block Address)
    01179    g_ms_cbw.u8_data[20-HOST_MS_CBW_DATA_POS]    = MSB3(addr);  // 20 - CBWCB5 - MSB0(Logical Block Address)
    01180    //g_ms_cbw.u8_data[21-HOST_MS_CBW_DATA_POS]  = 0x00;        // 21 - CBWCB6 - reserved
    01181    //g_ms_cbw.u8_data[22-HOST_MS_CBW_DATA_POS]  = 0x00;        // 22 - CBWCB7 - MSB1(Transfer Length)
    01182    g_ms_cbw.u8_data[23-HOST_MS_CBW_DATA_POS]    = u8_nb_sector;// 23 - CBWCB8 - MSB0(Transfer Length)
    01183    //g_ms_cbw.u8_data[24-HOST_MS_CBW_DATA_POS]  = 0x00;        // 24 - CBWCB9 - Control
    01184    if( !host_mem_cbw_send() )
    01185       return CTRL_FAIL;
    01186
    01187    // Transfer data ...
    01188    nb=HOST_SECTOR_SIZE;
    01189    while( u8_nb_sector!= 0 )
    01190    {
    01191       status = host_send_data(DMS_pipe_out(),nb,ram);
    01192       if(status==PIPE_STALL)
    01193       {
    01194          host_mem_stall_management();
    01195          host_mem_csw_read();
    01196          return CTRL_FAIL;
    01197       }
    01198       u8_nb_sector--;
    01199       ram = (U8*)ram + HOST_SECTOR_SIZE;
    01200    }
    01201
    01202    // Read CSW status
    01203    return host_mem_csw_read();
    01204 }

Here is the call graph for this function: