Contiki 2.5
Macros | Enumerations | Functions | Variables
USB Mass Storage Task

Macros

#define ENTRY_SIZE   32
 FAT specification.
 
#define LUN_0_EN   1
 Computed the maximum of static lun (don't add the lun of Mass Storage in mode USB Host)
 
#define ID_STREAM_ERR   0xFF
 -— Interface for streaming interface -------------------------------—
 

Enumerations

enum  Ctrl_status
 Define control status.
 

Functions

void avrf_mem_init (void)
 This function initializes the hw/sw ressources required to drive the AVR Flash. More...
 
Ctrl_status avrf_test_unit_ready (void)
 This function tests the state of the AVR Flash. More...
 
Ctrl_status avrf_read_capacity (U32 *u32_nb_sector)
 This function gives the address of the last valid sector. More...
 
Bool avrf_wr_protect (void)
 This function returns the write protected status of the memory. More...
 
Bool avrf_removal (void)
 This function tells if the memory has been removed or not. More...
 
Ctrl_status avrf_read_10 (U32 addr, U16 nb_sector)
 This function performs a read operation of n sectors from a given address on. More...
 
Ctrl_status avrf_write_10 (U32 addr, U16 nb_sector)
 This fonction initialise the memory for a write operation. More...
 
bit avrf_read_open (Uint32 pos)
 This function opens a DF memory in read mode at a given sector address. More...
 
void avrf_read_close (void)
 This function unselects the current DF memory. More...
 
bit avrf_read_sector (Uint16 nb_sector)
 This function is optimized and writes nb-sector * 512 Bytes from DataFlash memory to USB controller. More...
 
bit avrf_write_open (Uint32 pos)
 This function opens a DF memory in write mode at a given sector address. More...
 
void avrf_write_close (void)
 This function fills the end of the logical sector (512B) and launch page programming. More...
 
bit avrf_write_sector (Uint16 nb_sector)
 This function is optimized and writes nb-sector * 512 Bytes from USB controller to DataFlash memory. More...
 
Ctrl_status avrf_usb_read ()
 This fonction transfer the memory data (programed in scsi_read_10) directly to the usb interface. More...
 
Ctrl_status avrf_usb_write (void)
 This fonction transfer the usb data (programed in scsi_write_10) directly to the memory data. More...
 
U8 get_nb_lun ()
 This fonction return the number of logical unit. More...
 
U8 get_cur_lun ()
 This fonction return the current logical unit. More...
 
Ctrl_status mem_test_unit_ready (U8 lun)
 This fonction test the state of memory, and start the initialisation of the memory. More...
 
Ctrl_status mem_read_capacity (U8 lun, U32 _MEM_TYPE_SLOW_ *u32_nb_sector)
 This fonction return the capacity of the memory. More...
 
Bool mem_wr_protect (U8 lun)
 This fonction return is the write protected mode. More...
 
Bool mem_removal (U8 lun)
 This fonction inform about the memory type. More...
 
Ctrl_status memory_2_usb (U8 lun, U32 addr, U16 nb_sector)
 This fonction returns a pointer to the LUN name. More...
 
Ctrl_status usb_2_memory (U8 lun, U32 addr, U16 nb_sector)
 This fonction trabsfer a data from usb to memory. More...
 
Ctrl_status memory_2_ram (U8 lun, const U32 _MEM_TYPE_SLOW_ *addr, U8 _MEM_TYPE_SLOW_ *ram)
 Interface for RAM. More...
 
Ctrl_status ram_2_memory (U8 lun, const U32 _MEM_TYPE_SLOW_ *addr, U8 _MEM_TYPE_SLOW_ *ram)
 This fonction trabsfer a data from ram to memory. More...
 
U8 stream_mem_to_mem (U8 src_lun, U32 src_addr, U8 dest_lun, U32 dest_addr, U16 nb_sector)
 Interface for streaming interface. More...
 
Ctrl_status stream_state (U8 Id)
 Returns the state on a data transfer. More...
 
U16 stream_stop (U8 Id)
 Stop the data transfer. More...
 
Bool scsi_decode_command (void)
 SCSI decoder function. More...
 
Bool sbc_request_sense (void)
 This function manages the SCSI REQUEST SENSE command (0x03) More...
 
Bool sbc_inquiry (void)
 This function manages the SCSI INQUIRY command (0x12) More...
 
Bool sbc_mode_sense (Bool b_sense_10)
 This function manages the SCSI MODE SENSE command (0x1A for sense 6 and 0x5A for sense 10) More...
 
Bool sbc_prevent_allow_medium_removal (void)
 This function manages the SCSI PREVENT ALLOW MEDIUM REMOVAL command (0x1E) More...
 
void sbc_lun_status_is_good (void)
 This fonction send the UFI status GOOD.
 
void sbc_lun_status_is_not_present (void)
 This fonction send the UFI status "lun not present".
 
void sbc_lun_status_is_busy_or_change (void)
 This fonction send the UFI status busy and change.
 
void sbc_lun_status_is_fail (void)
 This fonction send the UFI status FAIL.
 
void sbc_lun_status_is_protected (void)
 This fonction send the UFI status FAIL because write protection.
 
void usb_mass_storage_cbw (void)
 USB Command Block Wrapper (CBW) management. More...
 
void usb_mass_storage_csw (void)
 USB Command Status Wrapper (CSW) management. More...
 
 PROCESS_THREAD (storage_process, ev, data_proc)
 USB Mass Storage Class Process. More...
 

Variables

U8 usb_configuration_nb
 Public : (U8) usb_configuration_nb Store the number of the USB configuration used by the USB device when its value is different from zero, it means the device mode is enumerated Used with USB_DEVICE_FEATURE == ENABLED only /.
 

Detailed Description

Function Documentation

void avrf_mem_init ( void  )

This function initializes the hw/sw ressources required to drive the AVR Flash.

Warning
Code:?? bytes (function code length)

/

Definition at line 91 of file avr_flash.c.

Ctrl_status avrf_read_10 ( U32  addr,
U16  nb_sector 
)

This function performs a read operation of n sectors from a given address on.

(sector = 512B)

    DATA FLOW is: AVRF => USB
Warning
Code:?? bytes (function code length)
Parameters
addrSector address to start the read from
nb_sectorNumber of sectors to transfer
Returns
Ctrl_status It is ready -> CTRL_GOOD A error occur -> CTRL_FAIL

Definition at line 178 of file avr_flash.c.

References avrf_read_close(), avrf_read_open(), and avrf_read_sector().

Ctrl_status avrf_read_capacity ( U32 *  u32_nb_sector)

This function gives the address of the last valid sector.

Warning
Code:?? bytes (function code length)
Parameters
*u32_nb_sectornumber of sector (sector = 512B). OUT
Returns
Ctrl_status It is ready -> CTRL_GOOD /

Definition at line 122 of file avr_flash.c.

void avrf_read_close ( void  )

This function unselects the current DF memory.

Warning
Code:?? bytes (function code length)

/

Definition at line 239 of file avr_flash.c.

Referenced by avrf_read_10().

bit avrf_read_open ( Uint32  pos)

This function opens a DF memory in read mode at a given sector address.

NOTE: Address may not be synchronized on the beginning of a page (depending on the DF page size).

Warning
Code:?? bytes (function code length)
Parameters
posLogical sector address
Returns
bit The open succeeded -> OK /

Definition at line 224 of file avr_flash.c.

Referenced by avrf_read_10().

bit avrf_read_sector ( Uint16  nb_sector)

This function is optimized and writes nb-sector * 512 Bytes from DataFlash memory to USB controller.

     DATA FLOW is: DF => USB

NOTE:

  • First call must be preceded by a call to the df_read_open() function,
  • The USB EPIN must have been previously selected,
  • USB ping-pong buffers are free,
  • As 512 is always a sub-multiple of page size, there is no need to check page end for each Bytes,
  • Interrupts are disabled during transfer to avoid timer interrupt,
  • nb_sector always >= 1, cannot be zero.
Warning
code:?? bytes (function code length)
Parameters
nb_sectornumber of contiguous sectors to read [IN]
Returns
bit The read succeeded -> OK /

Definition at line 269 of file avr_flash.c.

References FALSE, Is_usb_write_enabled, Usb_send_in, and Usb_write_byte.

Referenced by avrf_read_10().

Bool avrf_removal ( void  )

This function tells if the memory has been removed or not.

Warning
Code:?? bytes (function code length)
Returns
FALSE -> The memory isn't removed /

Definition at line 153 of file avr_flash.c.

References FALSE.

Ctrl_status avrf_test_unit_ready ( void  )

This function tests the state of the AVR Flash.

Warning
Code:?? bytes (function code length)
Returns
Ctrl_status It is ready -> CTRL_GOOD Else -> CTRL_NO_PRESENT /

Definition at line 106 of file avr_flash.c.

Ctrl_status avrf_usb_read ( )

This fonction transfer the memory data (programed in scsi_read_10) directly to the usb interface.

Returns
Ctrl_status It is ready -> CTRL_GOOD

Definition at line 499 of file avr_flash.c.

Ctrl_status avrf_usb_write ( void  )

This fonction transfer the usb data (programed in scsi_write_10) directly to the memory data.

Returns
Ctrl_status It is ready -> CTRL_GOOD

Definition at line 510 of file avr_flash.c.

Bool avrf_wr_protect ( void  )

This function returns the write protected status of the memory.

Only used by memory removal with a HARDWARE SPECIFIC write protected detection !!! The customer must unplug the memory to change this write protected status, which cannot be for a DF.

Warning
Code:?? bytes (function code length)
Returns
FALSE -> the memory is not write-protected (always) /

Definition at line 140 of file avr_flash.c.

References FALSE.

Ctrl_status avrf_write_10 ( U32  addr,
U16  nb_sector 
)

This fonction initialise the memory for a write operation.

    DATA FLOW is: USB => DF

(sector = 512B)

Parameters
addrSector address to start write
nb_sectorNumber of sectors to transfer
Returns
Ctrl_status It is ready -> CTRL_GOOD A error occur -> CTRL_FAIL

Definition at line 200 of file avr_flash.c.

References avrf_write_close(), avrf_write_open(), and avrf_write_sector().

void avrf_write_close ( void  )

This function fills the end of the logical sector (512B) and launch page programming.

Warning
Code:?? bytes (function code length)

/

Definition at line 329 of file avr_flash.c.

Referenced by avrf_write_10().

bit avrf_write_open ( Uint32  pos)

This function opens a DF memory in write mode at a given sector address.

NOTE: If page buffer > 512 bytes, page content is first loaded in buffer to be partially updated by write_byte or write64 functions.

Warning
Code:?? bytes (function code length)
Parameters
posSector address
Returns
bit The open succeeded -> OK /

Definition at line 313 of file avr_flash.c.

Referenced by avrf_write_10().

bit avrf_write_sector ( Uint16  nb_sector)

This function is optimized and writes nb-sector * 512 Bytes from USB controller to DataFlash memory.

Funtions to link USB DEVICE flow with data flash.

     DATA FLOW is: USB => DF

NOTE:

  • First call must be preceded by a call to the df_write_open() function,
  • As 512 is always a sub-multiple of page size, there is no need to check page end for each Bytes,
  • The USB EPOUT must have been previously selected,
  • Interrupts are disabled during transfer to avoid timer interrupt,
  • nb_sector always >= 1, cannot be zero.
Warning
code:?? bytes (function code length)
Parameters
nb_sectornumber of contiguous sectors to write [IN]
Returns
bit The write succeeded -> OK /

Definition at line 400 of file avr_flash.c.

References FALSE, Is_usb_read_enabled, Usb_ack_receive_out, and Usb_read_byte.

Referenced by avrf_write_10().

U8 get_cur_lun ( )

This fonction return the current logical unit.

Returns
U8 number of logical unit in the system

Definition at line 206 of file ctrl_access.c.

Referenced by usb_mass_storage_cbw().

U8 get_nb_lun ( )

This fonction return the number of logical unit.

**** Listing of commun interface ****************************************

Returns
U8 number of logical unit in the system

Definition at line 192 of file ctrl_access.c.

Referenced by usb_user_read_request().

Ctrl_status mem_read_capacity ( U8  lun,
U32 _MEM_TYPE_SLOW_ *  u32_nb_sector 
)

This fonction return the capacity of the memory.

Parameters
lunLogical unit number
u32_nb_sectorThe sector to query
Returns
*u16_nb_sector number of sector (sector = 512B)
Ctrl_status It is ready -> CTRL_GOOD Memory unplug -> CTRL_NO_PRESENT

Definition at line 288 of file ctrl_access.c.

Bool mem_removal ( U8  lun)

This fonction inform about the memory type.

Parameters
lunLogical unit number
Returns
TRUE -> The memory is removal

Definition at line 410 of file ctrl_access.c.

Ctrl_status mem_test_unit_ready ( U8  lun)

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

MORE (see SPC-3 §5.2.4) : The TEST UNIT READY command allows an application client to poll a logical unit until it is ready without the need to allocate space for returned data. The TEST UNIT READY command may be used to check the media status of logical units with removable media.

Parameters
lunLogical unit number
Returns
Ctrl_status It is ready -> CTRL_GOOD Memory unplug -> CTRL_NO_PRESENT Not initialize -> CTRL_BUSY

Definition at line 225 of file ctrl_access.c.

Bool mem_wr_protect ( U8  lun)

This fonction return is the write protected mode.

Parameters
lunLogical unit number

Only used by memory removal with a HARDWARE SPECIFIC write protected detection !!! The customer must be unplug the card for change this write protected mode.

Returns
TRUE -> the memory is protected

Definition at line 350 of file ctrl_access.c.

Ctrl_status memory_2_ram ( U8  lun,
const U32 _MEM_TYPE_SLOW_ *  addr,
U8 _MEM_TYPE_SLOW_ *  ram 
)

Interface for RAM.

This fonction tranfer one sector data from memory to ram

Parameters
lunLogical unit number
addrSector address to start read (sector = 512B)
ramAdresse of ram buffer (only xdata)
Returns
TRUE -> The memory is removal

Definition at line 726 of file ctrl_access.c.

Referenced by stream_mem_to_mem().

Ctrl_status memory_2_usb ( U8  lun,
U32  addr,
U16  nb_sector 
)

This fonction returns a pointer to the LUN name.

**** Listing of READ/WRITE interface ************************************

Parameters
lunLogical unit number
Returns
pointer to code string-----—— Listing of READ/WRITE interface with MODE ACCESS REGISTER -------—— This fonction tranfer a data from memory to usb
Parameters
lunLogical unit number
addrSector address to start read (sector = 512B)
nb_sectorNumber of sectors to transfer
Returns
Ctrl_status It is ready -> CTRL_GOOD A error occur -> CTRL_FAIL Memory unplug -> CTRL_NO_PRESENT

Definition at line 540 of file ctrl_access.c.

PROCESS_THREAD ( storage_process  ,
ev  ,
data_proc   
)

USB Mass Storage Class Process.

This is the link between USB and the "good stuff". In this routine data is received and processed by USB Storage Class

Definition at line 108 of file storage_task.c.

References CLOCK_SECOND, etimer_expired(), etimer_set(), Is_device_enumerated, Is_usb_receive_out, PROCESS_BEGIN, PROCESS_END, PROCESS_WAIT_EVENT_UNTIL, usb_mass_storage_cbw(), usb_mass_storage_csw(), and Usb_select_endpoint.

Ctrl_status ram_2_memory ( U8  lun,
const U32 _MEM_TYPE_SLOW_ *  addr,
U8 _MEM_TYPE_SLOW_ *  ram 
)

This fonction trabsfer a data from ram to memory.

Parameters
lunLogical unit number
addrSector address to start write (sector = 512B)
ramAdresse of ram buffer (only xdata)
Returns
TRUE -> The memory is removal

Definition at line 826 of file ctrl_access.c.

Referenced by stream_mem_to_mem().

Bool sbc_inquiry ( void  )

This function manages the SCSI INQUIRY command (0x12)

The SCSI Inquiry field contains information regarding parameters of the target. For example:

  • vendor identification
  • product identification
  • peripheral qualifier
  • peripheral device type
  • etc
Warning
Code:.. bytes (function code length)
Returns
FALSE: result KO, TRUE: result OK

Definition at line 291 of file scsi_decoder.c.

References FALSE, sbc_lun_status_is_good(), TRUE, and Usb_write_byte.

Referenced by scsi_decode_command().

Bool sbc_mode_sense ( Bool  b_sense_10)

This function manages the SCSI MODE SENSE command (0x1A for sense 6 and 0x5A for sense 10)

The SCSI mode sense function returns parameters to an application client. It is a complementary command to the SCSI MODE SELECT command.

Warning
Code:.. bytes (function code length)
Parameters
b_sense_10( TRUE = sense 10, TRUE = sense 6)
Returns
FALSE: result KO, TRUE: result OK

Definition at line 558 of file scsi_decoder.c.

References FALSE, sbc_lun_status_is_good(), and TRUE.

Referenced by scsi_decode_command().

Bool sbc_prevent_allow_medium_removal ( void  )

This function manages the SCSI PREVENT ALLOW MEDIUM REMOVAL command (0x1E)

The SCSI prevent allow medium removal command requests that the target enable or disable the removal of the medium in the logical unit.

Warning
Code:.. bytes (function code length)
Returns
FALSE: result KO, TRUE: result OK

Definition at line 740 of file scsi_decoder.c.

References sbc_lun_status_is_good(), and TRUE.

Referenced by scsi_decode_command().

Bool sbc_request_sense ( void  )

This function manages the SCSI REQUEST SENSE command (0x03)

The SCSI Sense contains the status of the last command This status is composed of 3 Bytes :

  • sense key (g_scsi_sense.key)
  • additional sense code (g_scsi_sense.asc)
  • additional sense code qualifier (g_scsi_sense.ascq)
Warning
Code:.. bytes (function code length)
Returns
FALSE: result KO, TRUE: result OK

Definition at line 231 of file scsi_decoder.c.

References sbc_lun_status_is_good(), TRUE, and Usb_write_byte.

Referenced by scsi_decode_command().

Bool scsi_decode_command ( void  )

SCSI decoder function.

This function read the SCSI command and launches the appropriate function

Warning
Code:.. bytes (function FLASH length)
Returns
FALSE: result KO, TRUE: result OK

Definition at line 138 of file scsi_decoder.c.

References FALSE, sbc_inquiry(), sbc_lun_status_is_good(), sbc_mode_sense(), sbc_prevent_allow_medium_removal(), sbc_request_sense(), and TRUE.

Referenced by usb_mass_storage_cbw().

U8 stream_mem_to_mem ( U8  src_lun,
U32  src_addr,
U8  dest_lun,
U32  dest_addr,
U16  nb_sector 
)

Interface for streaming interface.

This fonction copy a data from memory to other memory

Parameters
src_lunThe LUN of the source
src_addrThe address of the source
dest_lunThe LUN of the destination
dest_addrThe address of the destination
nb_sectorNumber of sectors to transfer

Definition at line 931 of file ctrl_access.c.

References memory_2_ram(), and ram_2_memory().

Ctrl_status stream_state ( U8  Id)

Returns the state on a data transfer.

Parameters
Idtransfer id
Returns
the state of the transfer CTRL_GOOD It is finish CTRL_BUSY It is running CTRL_FAIL It is fail

Definition at line 957 of file ctrl_access.c.

U16 stream_stop ( U8  Id)

Stop the data transfer.

Parameters
IdTransfer id
Returns
the number of sector remainder

Definition at line 969 of file ctrl_access.c.

Ctrl_status usb_2_memory ( U8  lun,
U32  addr,
U16  nb_sector 
)

This fonction trabsfer a data from usb to memory.

Parameters
lunLogical unit number
addrSector address to start write (sector = 512B)
nb_sectorNumber of sectors to transfer
Returns
Ctrl_status It is ready -> CTRL_GOOD A error occur -> CTRL_FAIL Memory unplug -> CTRL_NO_PRESENT

Definition at line 633 of file ctrl_access.c.

void usb_mass_storage_cbw ( void  )

USB Command Block Wrapper (CBW) management.

This function decodes the CBW command and stores the SCSI command

Warning
Code:?? bytes (function code length)

check if dCBWSignature is correct

'U'

'S'

'B'

'C'

Store CBW Tag to be repeated in CSW

if (bmCBWFlags.bit7 == 1) {direction = IN}

dummy CBWCBLength read

Definition at line 150 of file storage_task.c.

References FALSE, get_cur_lun(), scsi_decode_command(), TRUE, Usb_ack_receive_out, Usb_enable_stall_handshake, Usb_read_byte, and Usb_select_endpoint.

Referenced by PROCESS_THREAD().

void usb_mass_storage_csw ( void  )

USB Command Status Wrapper (CSW) management.

This function sends the status in relation with the last CBW

write CSW Signature

'U'

'S'

'B'

'S' write stored CBW Tag

write data residue value

write command status

0 -> PASS, 1 -> FAIL

Definition at line 235 of file storage_task.c.

References Is_usb_endpoint_stall_requested, Is_usb_receive_setup, Is_usb_write_enabled, usb_process_request(), Usb_select_endpoint, Usb_send_in, and Usb_write_byte.

Referenced by PROCESS_THREAD().