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
00083
00084
00085
00086 #include "board.h"
00087 #include "conf_usb_host.h"
00088 #include "ui.h"
00089 #include "main.h"
00090 #include "string.h"
00091
00092 #define MAX_DRIVE _VOLUMES
00093 #define TEST_FILE_NAME "0:SAMx7_USBHostTest.txt"
00094 #define MSG_TEST "Test:- SAMV7/E7 USB Host MSC\n"
00095
00096 typedef enum test_state {
00097 TEST_NULL,
00098 TEST_OK,
00099 TEST_NO_PRESENT,
00100 TEST_ERROR
00101 } test_state_t;
00102
00103 static volatile uint16_t main_usb_sof_counter = 0;
00104
00105 static test_state_t lun_states[MAX_DRIVE];
00106
00107 static FATFS fs;
00108 static FIL file_object;
00109
00110 static char test_file_name[] = {
00111 TEST_FILE_NAME
00112 };
00113
00114 static void main_reset_states(void);
00115 static int main_count_states(test_state_t state);
00116
00117
00118
00119
00120 int main(void)
00121 {
00122
00123 WDT_Disable(WDT);
00124
00125 SCB_EnableICache();
00126 SCB_EnableDCache();
00127
00128
00129 printf("-- USB Host Mass Storage Example %s --\n\r", SOFTPACK_VERSION);
00130 printf("-- %s\n\r", BOARD_NAME);
00131 printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ ,
00132 COMPILER_NAME);
00133
00134 TimeTick_Configure();
00135
00136 ui_init();
00137
00138
00139 USBH_start();
00140
00141
00142
00143
00144
00145 while (true) {
00146
00147 if (main_usb_sof_counter > 2000) {
00148 main_usb_sof_counter = 0;
00149 volatile uint8_t lun;
00150 FRESULT res;
00151
00152 for (lun = LUN_ID_USB; (lun < LUN_ID_USB +
00153 uhi_msc_mem_get_lun()) &&
00154 (lun < MAX_DRIVE); lun++) {
00155
00156 if (TEST_OK == lun_states[lun] ||
00157 TEST_ERROR == lun_states[lun])
00158 continue;
00159 else
00160 printf("LUN ok\n\r");
00161
00162
00163 memset(&fs, 0, sizeof(FATFS));
00164 res = f_mount(&fs, (TCHAR const *)&lun, 1);
00165
00166 if (FR_INVALID_DRIVE == res) {
00167
00168 lun_states[lun] = TEST_NO_PRESENT;
00169 printf("did not mount ok\n\r");
00170 continue;
00171 } else
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
00180 if (res == FR_NOT_READY) {
00181
00182 lun_states[lun] = TEST_NO_PRESENT;
00183 f_close(&file_object);
00184 printf("File create error\n\r");
00185 continue;
00186 } else
00187 printf("File create ok\n\r");
00188
00189 if (res != FR_OK) {
00190
00191 lun_states[lun] = TEST_ERROR;
00192 f_close(&file_object);
00193 printf("File system error\n\r");
00194 continue;
00195 }
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
00206 if (main_count_states(TEST_NO_PRESENT) == MAX_DRIVE) {
00207 ui_test_finish(false);
00208 } else if (MAX_DRIVE != main_count_states(TEST_NULL)) {
00209 if (main_count_states(TEST_ERROR)) {
00210 ui_test_finish(false);
00211 } else if (main_count_states(TEST_OK)) {
00212 ui_test_flag_reset();
00213 ui_test_finish(true);
00214 }
00215 } else
00216 ui_test_flag_reset();
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
00233 ui_usb_connection_event(dev, b_present);
00234 }
00235
00236 static void main_reset_states(void)
00237 {
00238 int i;
00239
00240 for (i = 0; i < MAX_DRIVE; i ++)
00241 lun_states[i] = TEST_NULL;
00242 }
00243
00244 static int main_count_states(test_state_t state)
00245 {
00246 int i, count = 0;
00247
00248 for (i = 0; i < MAX_DRIVE; i ++) {
00249 if (lun_states[i] == state)
00250 count ++;
00251 }
00252
00253 return count;
00254 }