SAMV71 Xplained Ultra Software Package 1.3

main.c

00001 /* ----------------------------------------------------------------------------
00002  *         SAM Software Package License
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2015, Atmel Corporation
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * - Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the disclaimer below.
00013  *
00014  * Atmel's name may not be used to endorse or promote products derived from
00015  * this software without specific prior written permission.
00016  *
00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  * ----------------------------------------------------------------------------
00028  */
00029 
00030 /**
00031  *  \page usb_host_msc USB Host Mass Storage Example
00032  *
00033  *  \section Purpose
00034  *
00035  *  This application shows how to implement a USB
00036  *  host mass storage on SAM V71 Xplained Ultra board.
00037  *
00038  *  \section Requirements
00039  *  This package can be used with SAM V71 Xplained Ultra board.
00040  *
00041  *  \section Description
00042  *
00043  *  Connect the board to a U-Disk (FAT/FAT32 are supported). This example will
00044  *  creates a file "SAMv7_USBHostTest.txt" on all present U-disks.
00045  *
00046  *  \section Usage
00047  *
00048  *  -# Build the program and download it inside the SAM V71 Xplained Ultra board.
00049  *  Please refer to the Getting Started with SAM V71 Microcontrollers.pdf
00050  *  -# On the computer, open and configure a terminal application
00051  *     (e.g. HyperTerminal on Microsoft Windows) with these settings:
00052  *    - 115200 baud rates
00053  *    - 8 bits of data
00054  *    - No parity
00055  *    - 1 stop bit
00056  *    - No flow control
00057  *  -# Connect the U-disk to SAM V71 Xplained Ultra board with the OTG wire.
00058  *  -# Start the application. In the terminal window, the
00059  *     following text should appear (values depend on the board and chip used):
00060  *     \code
00061  *      -- USB Host Mass Storage Example xxx --
00062  *      -- xxxxxx-xx
00063  *      -- Compiled: xxx xx xxxx xx:xx:xx --
00064  *     \endcode
00065  *  -# LED0 is on when a device is connected and blinks with different speed when 
00066  *  the device is enumerated and USB is in idle mode. LED1 is on when a read or 
00067  *  write access is on going. At last, LED1 is on when a LUN test is successful
00068  *  and blinks when failed In addition, a file "SAMv7_USBHostTest.txt" with the
00069  *  text "Test:- SAMV7 USB Host MSC" is created in the U-disk.
00070  *
00071  *  \section References
00072  *  - usb_host_msc/main.c
00073  *  - usb_host_msc/ui.c
00074  *  - led.c
00075  *  - USBH.c
00076  *  - USBH_HAL.c
00077  *  - uhi_msc.c
00078  *  - uhi_msc_mem.c
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; // Re-use fs for LUNs to reduce memory footprint
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 /*! \brief Main function. Execution starts here.
00115  */
00116 int main(void)
00117 {
00118     TCHAR Drv_Num;
00119 
00120     /* Disable watchdog*/
00121     WDT_Disable( WDT );
00122   
00123     SCB_EnableICache();
00124     SCB_EnableDCache();
00125 
00126     /* Output example information */
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     // Start USB host stack
00136     USBH_start();
00137 
00138     // The USB management is entirely managed by interrupts.
00139     // As a consequence, the user application does only have :
00140     // - to play with the power modes
00141     // - to create a file on each new LUN connected
00142     while (true) {
00143         //sleepmgr_enter_sleep();
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             // Check if LUN has been already tested
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             // Mount drive
00162             memset(&fs, 0, sizeof(FATFS));
00163             res = f_mount(&fs, (TCHAR const*)&lun, 1);
00164             if (FR_INVALID_DRIVE == res) {
00165                 // LUN is not present
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             // Create a test file on the disk
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                 // LUN not ready
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                 // LUN test error
00192                 lun_states[lun] = TEST_ERROR;
00193                 f_close(&file_object);
00194                 printf("File system error\n\r");
00195                 continue;
00196             }
00197             // Write to test file
00198             f_puts((const TCHAR *)MSG_TEST, &file_object);
00199              printf("File Writing\n\r");
00200             // LUN test OK
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); // Test fail
00207             } else if (MAX_DRIVE != main_count_states(TEST_NULL)) {
00208                 if (main_count_states(TEST_ERROR)) {
00209                     ui_test_finish(false); // Test fail
00210                 }else if (main_count_states(TEST_OK)) {
00211                     ui_test_flag_reset();
00212                     ui_test_finish(true); // Test OK
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(); // LUN is unplugged, reset flags
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 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines