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