USB Mass Storage Task
[RZRAVEN USB Stick (Jackdaw)]

Defines

#define ENTRY_SIZE   32
 FAT specification.
#define LUN_0_EN   0
 Computed the maximum of static lun (don't add the lun of Mass Storage in mode USB Host).

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.
Ctrl_status avrf_test_unit_ready (void)
 This function tests the state of the AVR Flash.
Ctrl_status avrf_read_capacity (U32 *u32_nb_sector)
 This function gives the address of the last valid sector.
Bool avrf_wr_protect (void)
 This function returns the write protected status of the memory.
Bool avrf_removal (void)
 This function tells if the memory has been removed or not.
Ctrl_status avrf_read_10 (U32 addr, U16 nb_sector)
 This function performs a read operation of n sectors from a given address on.
Ctrl_status avrf_write_10 (U32 addr, U16 nb_sector)
 This fonction initialise the memory for a write operation.
bit avrf_read_open (Uint32 pos)
 This function opens a DF memory in read mode at a given sector address.
void avrf_read_close (void)
 This function unselects the current DF memory.
bit avrf_read_sector (Uint16 nb_sector)
 This function is optimized and writes nb-sector * 512 Bytes from DataFlash memory to USB controller.
bit avrf_write_open (Uint32 pos)
 This function opens a DF memory in write mode at a given sector address.
void avrf_write_close (void)
 This function fills the end of the logical sector (512B) and launch page programming.
bit avrf_write_sector (Uint16 nb_sector)
 This function is optimized and writes nb-sector * 512 Bytes from USB controller to DataFlash memory.
Ctrl_status avrf_usb_read ()
 This fonction transfer the memory data (programed in scsi_read_10) directly to the usb interface.
Ctrl_status avrf_usb_write (void)
 This fonction transfer the usb data (programed in scsi_write_10) directly to the memory data.
U8 get_nb_lun ()
 This fonction return the number of logical unit.
U8 get_cur_lun ()
 This fonction return the current logical unit.
Ctrl_status mem_test_unit_ready (U8 lun)
 This fonction test the state of memory, and start the initialisation of the memory.
Ctrl_status mem_read_capacity (U8 lun, U32 _MEM_TYPE_SLOW_ *u32_nb_sector)
 This fonction return the capacity of the memory.
Bool mem_wr_protect (U8 lun)
 This fonction return is the write protected mode.
Bool mem_removal (U8 lun)
 This fonction inform about the memory type.
Ctrl_status memory_2_usb (U8 lun, U32 addr, U16 nb_sector)
 This fonction returns a pointer to the LUN name.
Ctrl_status usb_2_memory (U8 lun, U32 addr, U16 nb_sector)
 This fonction trabsfer a data from usb to memory.
Bool scsi_decode_command (void)
 SCSI decoder function.
Bool sbc_request_sense (void)
 This function manages the SCSI REQUEST SENSE command (0x03).
Bool sbc_inquiry (void)
 This function manages the SCSI INQUIRY command (0x12).
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).
Bool sbc_prevent_allow_medium_removal (void)
 This function manages the SCSI PREVENT ALLOW MEDIUM REMOVAL command (0x1E).
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.
void usb_mass_storage_csw (void)
 USB Command Status Wrapper (CSW) management.
 PROCESS_THREAD (storage_process, ev, data_proc)
 USB Mass Storage Class Process.

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 /.

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)

/

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:
addr Sector address to start the read from
nb_sector Number of sectors to transfer
Returns:
Ctrl_status It is ready -> CTRL_GOOD A error occur -> CTRL_FAIL
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_sector number 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)

/

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:
pos Logical sector address
Returns:
bit The open succeeded -> OK /
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_sector number of contiguous sectors to read [IN]
Returns:
bit The read succeeded -> OK /
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 /
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 /
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
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
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) /
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:
addr Sector address to start write
nb_sector Number of sectors to transfer
Returns:
Ctrl_status It is ready -> CTRL_GOOD A error occur -> CTRL_FAIL
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)

/

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:
pos Sector address
Returns:
bit The open succeeded -> OK /
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_sector number of contiguous sectors to write [IN]
Returns:
bit The write succeeded -> OK /
U8 get_cur_lun (  ) 

This fonction return the current logical unit.

Returns:
U8 number of logical unit in the system

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

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:
lun Logical unit number
u32_nb_sector The sector to query
Returns:
*u16_nb_sector number of sector (sector = 512B)
Ctrl_status It is ready -> CTRL_GOOD Memory unplug -> CTRL_NO_PRESENT
Bool mem_removal ( U8  lun  ) 

This fonction inform about the memory type.

Parameters:
lun Logical unit number
Returns:
TRUE -> The memory is removal
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:
lun Logical unit number
Returns:
Ctrl_status It is ready -> CTRL_GOOD Memory unplug -> CTRL_NO_PRESENT Not initialize -> CTRL_BUSY
Bool mem_wr_protect ( U8  lun  ) 

This fonction return is the write protected mode.

Parameters:
lun Logical 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
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:
lun Logical 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:
lun Logical unit number
addr Sector address to start read (sector = 512B)
nb_sector Number of sectors to transfer
Returns:
Ctrl_status It is ready -> CTRL_GOOD A error occur -> CTRL_FAIL Memory unplug -> CTRL_NO_PRESENT
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.

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
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
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
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
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

Referenced by usb_mass_storage_cbw().

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

This fonction trabsfer a data from usb to memory.

Parameters:
lun Logical unit number
addr Sector address to start write (sector = 512B)
nb_sector Number of sectors to transfer
Returns:
Ctrl_status It is ready -> CTRL_GOOD A error occur -> CTRL_FAIL Memory unplug -> CTRL_NO_PRESENT
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().


Generated on Mon Apr 11 14:23:54 2011 for Contiki 2.5 by  doxygen 1.6.1