This file manages the USB device mass-storage task.
Definition in file device_mass_storage_task.c.
#include "conf_usb.h"
#include "board.h"
#include "FreeRTOS.h"
#include "task.h"
#include "usb_drv.h"
#include "usb_descriptors.h"
#include "usb_standard_request.h"
#include "ctrl_access.h"
#include "scsi_decoder.h"
#include "device_mass_storage_task.h"
Go to the source code of this file.
Functions | |
void | device_mass_storage_task (void *pvParameters) |
Entry point of the device mass-storage task management. | |
void | device_mass_storage_task_init (void) |
This function initializes the hardware/software resources required for device mass-storage task. | |
static void | usb_mass_storage_cbw (void) |
USB Command Block Wrapper (CBW) management. | |
static void | usb_mass_storage_csw (void) |
USB Command Status Wrapper (CSW) management. | |
Variables | |
static U32 | dCBWTag |
volatile Bool | ms_multiple_drive |
U8 | usb_LUN |
void device_mass_storage_task | ( | void * | pvParameters | ) |
Entry point of the device mass-storage task management.
This function links the device mass-storage SCSI commands to the USB bus.
Definition at line 122 of file device_mass_storage_task.c.
References configTSK_USB_DMS_PERIOD, EP_MS_OUT, usb_mass_storage_cbw(), and usb_mass_storage_csw().
Referenced by device_mass_storage_task_init().
00126 { 00127 #ifdef FREERTOS_USED 00128 portTickType xLastWakeTime; 00129 00130 xLastWakeTime = xTaskGetTickCount(); 00131 while (TRUE) 00132 { 00133 vTaskDelayUntil(&xLastWakeTime, configTSK_USB_DMS_PERIOD); 00134 00135 // First, check the device enumeration state 00136 if (!Is_device_enumerated()) continue; 00137 #else 00138 // First, check the device enumeration state 00139 if (!Is_device_enumerated()) return; 00140 #endif // FREERTOS_USED 00141 00142 // Display Start-of-Frame counter on LEDs 00143 /*LED_Display_Field(LED_MONO0_GREEN | 00144 LED_MONO1_GREEN | 00145 LED_MONO2_GREEN | 00146 LED_MONO3_GREEN, 00147 sof_cnt >> 5);*/ 00148 00149 // If we receive something in the OUT endpoint, parse it 00150 if (Is_usb_out_received(EP_MS_OUT)) 00151 { 00152 usb_mass_storage_cbw(); 00153 usb_mass_storage_csw(); 00154 } 00155 #ifdef FREERTOS_USED 00156 } 00157 #endif 00158 }
void device_mass_storage_task_init | ( | void | ) |
This function initializes the hardware/software resources required for device mass-storage task.
Definition at line 91 of file device_mass_storage_task.c.
References configTSK_USB_DMS_NAME, configTSK_USB_DMS_PRIORITY, configTSK_USB_DMS_STACK_SIZE, device_mass_storage_task(), EP_MS_IN, and EP_MS_OUT.
Referenced by main().
00092 { 00093 g_scsi_ep_ms_in = EP_MS_IN; 00094 g_scsi_ep_ms_out = EP_MS_OUT; 00095 #ifndef FREERTOS_USED 00096 #if USB_HOST_FEATURE == ENABLED 00097 // If both device and host features are enabled, check if device mode is engaged 00098 // (accessing the USB registers of a non-engaged mode, even with load operations, 00099 // may corrupt USB FIFO data). 00100 if (Is_usb_device()) 00101 #endif // USB_HOST_FEATURE == ENABLED 00102 Usb_enable_sof_interrupt(); 00103 #endif // FREERTOS_USED 00104 00105 #ifdef FREERTOS_USED 00106 xTaskCreate(device_mass_storage_task, 00107 configTSK_USB_DMS_NAME, 00108 configTSK_USB_DMS_STACK_SIZE, 00109 NULL, 00110 configTSK_USB_DMS_PRIORITY, 00111 NULL); 00112 #endif // FREERTOS_USED 00113 }
static void usb_mass_storage_cbw | ( | void | ) | [static] |
USB Command Block Wrapper (CBW) management.
This function decodes the CBW command and stores the SCSI command.
Check if dCBWSignature is correct
Store CBW Tag to be repeated in CSW
if (bmCBWFlags.bit7 == 1) {direction = IN;}
Dummy CBWCBLength read
Store scsi_command
Definition at line 166 of file device_mass_storage_task.c.
References dCBWTag, EP_MS_IN, EP_MS_OUT, ms_multiple_drive, and usb_LUN.
Referenced by device_mass_storage_task().
00167 { 00168 Bool cbw_error; 00169 U8 ms_endpoint; 00170 00171 Usb_reset_endpoint_fifo_access(EP_MS_OUT); 00172 00174 cbw_error = (Usb_read_endpoint_data(EP_MS_OUT, 32) != *(U32 *)&"USBC"); 00175 00177 dCBWTag = Usb_read_endpoint_data(EP_MS_OUT, 32); 00178 00179 g_scsi_data_remaining = Usb_read_endpoint_data(EP_MS_OUT, 32); 00180 g_scsi_data_remaining = usb_format_usb_to_mcu_data(32, g_scsi_data_remaining); 00181 00183 if (Usb_read_endpoint_data(EP_MS_OUT, 8)) 00184 { 00185 ms_endpoint = EP_MS_IN; 00186 if (cbw_error) 00187 { 00188 Usb_ack_out_received_free(EP_MS_OUT); 00189 Usb_enable_stall_handshake(EP_MS_IN); 00190 return; 00191 } 00192 } 00193 else 00194 { 00195 ms_endpoint = EP_MS_OUT; 00196 if (cbw_error) 00197 { 00198 Usb_enable_stall_handshake(EP_MS_OUT); 00199 Usb_ack_out_received_free(EP_MS_OUT); 00200 return; 00201 } 00202 } 00203 00204 usb_LUN = Usb_read_endpoint_data(EP_MS_OUT, 8); 00205 00206 if (!ms_multiple_drive) 00207 { 00208 usb_LUN = get_cur_lun(); 00209 } 00210 00212 Usb_read_endpoint_data(EP_MS_OUT, 8); 00213 00215 usb_read_ep_rxpacket(EP_MS_OUT, g_scsi_command, sizeof(g_scsi_command), NULL); 00216 00217 Usb_ack_out_received_free(EP_MS_OUT); 00218 00219 if (!scsi_decode_command() && g_scsi_data_remaining) 00220 { 00221 Usb_enable_stall_handshake(ms_endpoint); 00222 } 00223 }
static void usb_mass_storage_csw | ( | void | ) | [static] |
USB Command Status Wrapper (CSW) management.
This function sends the status in relation with the last CBW.
Write CSW Signature
Write stored CBW Tag
Write data residual value
Write command status
Definition at line 231 of file device_mass_storage_task.c.
References dCBWTag, EP_MS_IN, and EP_MS_OUT.
Referenced by device_mass_storage_task().
00232 { 00233 while (Is_usb_endpoint_stall_requested(EP_MS_IN)) 00234 { 00235 if (Is_usb_setup_received()) usb_process_request(); 00236 } 00237 00238 while (Is_usb_endpoint_stall_requested(EP_MS_OUT)) 00239 { 00240 if (Is_usb_setup_received()) usb_process_request(); 00241 } 00242 00243 while (!Is_usb_in_ready(EP_MS_IN)); 00244 00245 Usb_reset_endpoint_fifo_access(EP_MS_IN); 00246 00248 Usb_write_endpoint_data(EP_MS_IN, 32, *(U32 *)&"USBS"); 00249 00251 Usb_write_endpoint_data(EP_MS_IN, 32, dCBWTag); 00252 00254 Usb_write_endpoint_data(EP_MS_IN, 32, 00255 usb_format_mcu_to_usb_data(32, g_scsi_data_remaining)); 00256 00258 Usb_write_endpoint_data(EP_MS_IN, 8, g_scsi_status); 00259 00260 Usb_ack_in_ready_send(EP_MS_IN); 00261 }
U32 dCBWTag [static] |
Definition at line 78 of file device_mass_storage_task.c.
Referenced by usb_mass_storage_cbw(), and usb_mass_storage_csw().
volatile Bool ms_multiple_drive |
Definition at line 69 of file usb_specific_request.c.
Referenced by usb_mass_storage_cbw(), usb_user_endpoint_init(), and usb_user_read_request().
U8 usb_LUN |