00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 #include "board.h"
00083 #include "conf_usb_host.h"
00084 #include "ui.h"
00085 #include "main.h"
00086 #include "string.h"
00087
00088 #define MAX_DRIVE _VOLUMES
00089 #define TEST_FILE_NAME "0:SAMv7_USBHostTest.txt"
00090 #define MSG_TEST "Test:- SAMV7 USB Host MSC\n"
00091
00092 typedef enum test_state {
00093 TEST_NULL,
00094 TEST_OK,
00095 TEST_NO_PRESENT,
00096 TEST_ERROR
00097 } test_state_t;
00098
00099 static volatile uint16_t main_usb_sof_counter = 0;
00100
00101 static test_state_t lun_states[MAX_DRIVE];
00102
00103 static FATFS fs;
00104 static FIL file_object;
00105
00106 static char test_file_name[] = {
00107 TEST_FILE_NAME
00108 };
00109
00110 static void main_reset_states(void);
00111 static int main_count_states(test_state_t state);
00112
00113
00114
00115
00116 int main(void)
00117 {
00118 TCHAR Drv_Num;
00119
00120
00121 WDT_Disable( WDT );
00122
00123 SCB_EnableICache();
00124 SCB_EnableDCache();
00125
00126
00127 printf("-- USB Host Mass Storage Example %s --\n\r", SOFTPACK_VERSION);
00128 printf("-- %s\n\r", BOARD_NAME);
00129 printf( "-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME) ;
00130
00131 TimeTick_Configure();
00132
00133 ui_init();
00134
00135
00136 USBH_start();
00137
00138
00139
00140
00141
00142 while (true) {
00143
00144 if (main_usb_sof_counter > 2000) {
00145 main_usb_sof_counter = 0;
00146 volatile uint8_t lun;
00147 FRESULT res;
00148
00149 for (lun = LUN_ID_USB; (lun < LUN_ID_USB +
00150 uhi_msc_mem_get_lun()) &&
00151 (lun < MAX_DRIVE); lun++) {
00152
00153 if (TEST_OK == lun_states[lun] ||
00154 TEST_ERROR == lun_states[lun]) {
00155 continue;
00156 }
00157 else
00158 {
00159 printf("LUN ok\n\r");
00160 }
00161
00162 memset(&fs, 0, sizeof(FATFS));
00163 res = f_mount(&fs, (TCHAR const*)&lun, 1);
00164 if (FR_INVALID_DRIVE == res) {
00165
00166 lun_states[lun] = TEST_NO_PRESENT;
00167 printf("did not mount ok\n\r");
00168 continue;
00169 }
00170 else
00171 {
00172 printf("Mount ok\n\r");
00173 }
00174
00175 test_file_name[0] = lun + '0';
00176 res = f_open(&file_object,
00177 (TCHAR const *)test_file_name,
00178 FA_CREATE_ALWAYS | FA_WRITE);
00179 if (res == FR_NOT_READY) {
00180
00181 lun_states[lun] = TEST_NO_PRESENT;
00182 f_close(&file_object);
00183 printf("File create error\n\r");
00184 continue;
00185 }
00186 else
00187 {
00188 printf("File create ok\n\r");
00189 }
00190 if (res != FR_OK) {
00191
00192 lun_states[lun] = TEST_ERROR;
00193 f_close(&file_object);
00194 printf("File system error\n\r");
00195 continue;
00196 }
00197
00198 f_puts((const TCHAR *)MSG_TEST, &file_object);
00199 printf("File Writing\n\r");
00200
00201 lun_states[lun] = TEST_OK;
00202 f_close(&file_object);
00203 printf("File close\n\r");
00204 }
00205 if (main_count_states(TEST_NO_PRESENT) == MAX_DRIVE) {
00206 ui_test_finish(false);
00207 } else if (MAX_DRIVE != main_count_states(TEST_NULL)) {
00208 if (main_count_states(TEST_ERROR)) {
00209 ui_test_finish(false);
00210 }else if (main_count_states(TEST_OK)) {
00211 ui_test_flag_reset();
00212 ui_test_finish(true);
00213 }
00214 } else {
00215 ui_test_flag_reset();
00216 }
00217 }
00218 }
00219 }
00220
00221 void main_usb_sof_event(void)
00222 {
00223 main_usb_sof_counter++;
00224 ui_usb_sof_event();
00225 }
00226
00227 void main_usb_connection_event(USBH_device_t * dev, bool b_present)
00228 {
00229 if (!b_present) {
00230 main_reset_states();
00231 }
00232 ui_usb_connection_event(dev, b_present);
00233 }
00234
00235 static void main_reset_states(void)
00236 {
00237 int i;
00238 for (i = 0; i < MAX_DRIVE; i ++) {
00239 lun_states[i] = TEST_NULL;
00240 }
00241 }
00242
00243 static int main_count_states(test_state_t state)
00244 {
00245 int i, count = 0;
00246 for (i = 0; i < MAX_DRIVE; i ++) {
00247 if (lun_states[i] == state) {
00248 count ++;
00249 }
00250 }
00251 return count;
00252 }