SAMV71 Xplained Ultra Software Package 1.5

main.c

00001 /* ---------------------------------------------------------------------------- */
00002 /*                  Atmel Microcontroller Software Support                      */
00003 /*                       SAM Software Package License                           */
00004 /* ---------------------------------------------------------------------------- */
00005 /* Copyright (c) 2015, Atmel Corporation                                        */
00006 /*                                                                              */
00007 /* All rights reserved.                                                         */
00008 /*                                                                              */
00009 /* Redistribution and use in source and binary forms, with or without           */
00010 /* modification, are permitted provided that the following condition is met:    */
00011 /*                                                                              */
00012 /* - Redistributions of source code must retain the above copyright notice,     */
00013 /* this list of conditions and the disclaimer below.                            */
00014 /*                                                                              */
00015 /* Atmel's name may not be used to endorse or promote products derived from     */
00016 /* this software without specific prior written permission.                     */
00017 /*                                                                              */
00018 /* DISCLAIMER:  THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR   */
00019 /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
00020 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE   */
00021 /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,      */
00022 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
00023 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,  */
00024 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    */
00025 /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING         */
00026 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
00027 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                           */
00028 /* ---------------------------------------------------------------------------- */
00029 
00030 /**
00031  *  \page usb_host_msc USB Host Mass Storage Example
00032  *
00033  *  \section Purpose
00034  *
00035  * This example shows how to implement a USB host CDC on SAMV7/E7 Microcontrollers.
00036  *
00037  *  \section Requirements
00038  *  This package can be used with SAMV71 Xplained Ultra board or SAME70 Xplained board
00039  *
00040  *  \section Description
00041  *
00042  *  Connect the board to a U-Disk (FAT/FAT32 are supported). This example will
00043  *  creates a file "SAMx7_USBHostTest.txt" on all present U-disks.
00044  *
00045  *  \section Usage
00046  *
00047  *  -# Build the program and download it inside the board.
00048  *    Please refer to the Getting Started with SAM V71/E70 Microcontrollers.pdf
00049  *  -# On the computer, open and configure a terminal application
00050  *     (e.g. HyperTerminal on Microsoft Windows) with these settings:
00051  *    - 115200 baud rate
00052  *    - 8 bits of data
00053  *    - No parity
00054  *    - 1 stop bit
00055  *    - No flow control
00056  *  -# Connect the HID mouse device to SAM V71 Xplained Ultra board or SAME70 Xplained board
00057  *     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 "SAMx7_USBHostTest.txt" with the
00069  *  text "Test:- SAMV7/E7 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: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; // 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     /* Disable watchdog*/
00119     WDT_Disable(WDT);
00120 
00121     SCB_EnableICache();
00122     SCB_EnableDCache();
00123 
00124     /* Output example information */
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     // Start USB host stack
00135     USBH_start();
00136 
00137     // The USB management is entirely managed by interrupts.
00138     // As a consequence, the user application does only have :
00139     // - to play with the power modes
00140     // - to create a file on each new LUN connected
00141     while (true) {
00142         //sleepmgr_enter_sleep();
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                 // Check if LUN has been already tested
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                 // Mount drive
00159                 memset(&fs, 0, sizeof(FATFS));
00160                 res = f_mount(&fs, (TCHAR const *)&lun, 1);
00161 
00162                 if (FR_INVALID_DRIVE == res) {
00163                     // LUN is not present
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                 // Create a test file on the disk
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                     // LUN not ready
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                     // LUN test error
00187                     lun_states[lun] = TEST_ERROR;
00188                     f_close(&file_object);
00189                     printf("File system error\n\r");
00190                     continue;
00191                 }
00192 
00193                 // Write to test file
00194                 f_puts((const TCHAR *)MSG_TEST, &file_object);
00195                 printf("File Writing\n\r");
00196                 // LUN test OK
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); // Test fail
00204             } else if (MAX_DRIVE != main_count_states(TEST_NULL)) {
00205                 if (main_count_states(TEST_ERROR)) {
00206                     ui_test_finish(false); // Test fail
00207                 } else if (main_count_states(TEST_OK)) {
00208                     ui_test_flag_reset();
00209                     ui_test_finish(true); // Test OK
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(); // LUN is unplugged, reset flags
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 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines