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_hid USB Host HID + MSD Example
00032  *
00033  *  \section Purpose
00034  *
00035  *  This application shows how to implement a USB
00036  *  host compound mass storage & HID mouse 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 "SAMx7_USBHostTest.txt" on all present U-disks.
00045  *  Connect the HID mouse device to board. This example will display events of
00046  *  the mouse.
00047  *
00048  *  \section Usage
00049  *
00050  *  -# Build the program and download it inside the board.
00051  *    Please refer to the Getting Started with SAM V71/E70 Microcontrollers.pdf
00052  *  -# On the computer, open and configure a terminal application
00053  *     (e.g. HyperTerminal on Microsoft Windows) with these settings:
00054  *    - 115200 baud rate
00055  *    - 8 bits of data
00056  *    - No parity
00057  *    - 1 stop bit
00058  *    - No flow control
00059  *  -# Connect the U-disk/USB mouse to SAM V71 Xplained Ultra board or SAME70 Xplained board
00060  *     with the OTG wire.
00061  *  -# Start the application. In the terminal window, the
00062  *     following text should appear (values depend on the board and chip used):
00063  *     \code
00064  *      -- USB Host Mass Storage Example xxx --
00065  *      -- xxxxxx-xx
00066  *      -- Compiled: xxx xx xxxx xx:xx:xx --
00067  *     \endcode
00068  *  -# LED0 is on when a device is connected and blinks with different speed when
00069  *  the device is enumerated and USB is in idle mode.
00070  *  -# LED1 is on when a read or write access is on going or the mouse moves.
00071  *  -# In addition, a file "SAMx7_USBHostTest.txt" with the text "Test:- SAMV7/E7
00072  *  USB Host MSC" is created in the U-disk.
00073  *
00074  *  \section References
00075  *  - usb_host_msc/main.c
00076  *  - usb_host_msc/ui.c
00077  *  - led.c
00078  *  - USBH.c
00079  *  - USBH_HAL.c
00080  *  - uhi_msc.c
00081  *  - uhi_msc_mem.c
00082  *  - uhi_hid_mouse.c
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; // Re-use fs for LUNs to reduce memory footprint
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 /*! \brief Main function. Execution starts here.
00119  */
00120 int main(void)
00121 {
00122     /* Disable watchdog*/
00123     WDT_Disable(WDT);
00124 
00125     SCB_EnableICache();
00126     SCB_EnableDCache();
00127 
00128     /* Output example information */
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     // Start USB host stack
00139     USBH_start();
00140 
00141     // The USB management is entirely managed by interrupts.
00142     // As a consequence, the user application does only have :
00143     // - to play with the power modes
00144     // - to create a file on each new LUN connected
00145     while (true) {
00146         //sleepmgr_enter_sleep();
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                 // Check if LUN has been already tested
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                 // Mount drive
00163                 memset(&fs, 0, sizeof(FATFS));
00164                 res = f_mount(&fs, (TCHAR const *)&lun, 1);
00165 
00166                 if (FR_INVALID_DRIVE == res) {
00167                     // LUN is not present
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                 // 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 
00180                 if (res == FR_NOT_READY) {
00181                     // LUN not ready
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                     // LUN test error
00191                     lun_states[lun] = TEST_ERROR;
00192                     f_close(&file_object);
00193                     printf("File system error\n\r");
00194                     continue;
00195                 }
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 
00206             if (main_count_states(TEST_NO_PRESENT) == MAX_DRIVE) {
00207                 ui_test_finish(false); // Test fail
00208             } else if (MAX_DRIVE != main_count_states(TEST_NULL)) {
00209                 if (main_count_states(TEST_ERROR)) {
00210                     ui_test_finish(false); // Test fail
00211                 } else if (main_count_states(TEST_OK)) {
00212                     ui_test_flag_reset();
00213                     ui_test_finish(true); // Test OK
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(); // LUN is unplugged, reset flags
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 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines