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:SAMx7_USBHostTest.txt"
00090 #define MSG_TEST "Test:- SAMV7/E7 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
00119 WDT_Disable(WDT);
00120
00121 SCB_EnableICache();
00122 SCB_EnableDCache();
00123
00124
00125 printf("-- USB Host Mass Storage Example %s --\n\r", SOFTPACK_VERSION);
00126 printf("-- %s\n\r", BOARD_NAME);
00127 printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ ,
00128 COMPILER_NAME);
00129
00130 TimeTick_Configure();
00131
00132 ui_init();
00133
00134
00135 USBH_start();
00136
00137
00138
00139
00140
00141 while (true) {
00142
00143 if (main_usb_sof_counter > 2000) {
00144 main_usb_sof_counter = 0;
00145 volatile uint8_t lun;
00146 FRESULT res;
00147
00148 for (lun = LUN_ID_USB; (lun < LUN_ID_USB +
00149 uhi_msc_mem_get_lun()) &&
00150 (lun < MAX_DRIVE); lun++) {
00151
00152 if (TEST_OK == lun_states[lun] ||
00153 TEST_ERROR == lun_states[lun])
00154 continue;
00155 else
00156 printf("LUN ok\n\r");
00157
00158
00159 memset(&fs, 0, sizeof(FATFS));
00160 res = f_mount(&fs, (TCHAR const *)&lun, 1);
00161
00162 if (FR_INVALID_DRIVE == res) {
00163
00164 lun_states[lun] = TEST_NO_PRESENT;
00165 printf("did not mount ok\n\r");
00166 continue;
00167 } else
00168 printf("Mount ok\n\r");
00169
00170
00171 test_file_name[0] = lun + '0';
00172 res = f_open(&file_object,
00173 (TCHAR const *)test_file_name,
00174 FA_CREATE_ALWAYS | FA_WRITE);
00175
00176 if (res == FR_NOT_READY) {
00177
00178 lun_states[lun] = TEST_NO_PRESENT;
00179 f_close(&file_object);
00180 printf("File create error\n\r");
00181 continue;
00182 } else
00183 printf("File create ok\n\r");
00184
00185 if (res != FR_OK) {
00186
00187 lun_states[lun] = TEST_ERROR;
00188 f_close(&file_object);
00189 printf("File system error\n\r");
00190 continue;
00191 }
00192
00193
00194 f_puts((const TCHAR *)MSG_TEST, &file_object);
00195 printf("File Writing\n\r");
00196
00197 lun_states[lun] = TEST_OK;
00198 f_close(&file_object);
00199 printf("File close\n\r");
00200 }
00201
00202 if (main_count_states(TEST_NO_PRESENT) == MAX_DRIVE) {
00203 ui_test_finish(false);
00204 } else if (MAX_DRIVE != main_count_states(TEST_NULL)) {
00205 if (main_count_states(TEST_ERROR)) {
00206 ui_test_finish(false);
00207 } else if (main_count_states(TEST_OK)) {
00208 ui_test_flag_reset();
00209 ui_test_finish(true);
00210 }
00211 } else
00212 ui_test_flag_reset();
00213 }
00214 }
00215 }
00216
00217 void main_usb_sof_event(void)
00218 {
00219 main_usb_sof_counter++;
00220 ui_usb_sof_event();
00221 }
00222
00223 void main_usb_connection_event(USBH_device_t *dev, bool b_present)
00224 {
00225 if (!b_present) {
00226 main_reset_states();
00227 }
00228
00229 ui_usb_connection_event(dev, b_present);
00230 }
00231
00232 static void main_reset_states(void)
00233 {
00234 int i;
00235
00236 for (i = 0; i < MAX_DRIVE; i ++)
00237 lun_states[i] = TEST_NULL;
00238 }
00239
00240 static int main_count_states(test_state_t state)
00241 {
00242 int i, count = 0;
00243
00244 for (i = 0; i < MAX_DRIVE; i ++) {
00245 if (lun_states[i] == state)
00246 count ++;
00247 }
00248
00249 return count;
00250 }