SAMV71 Xplained Ultra Software Package 1.4

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_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 "SAMv7_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 SAM V71 Xplained Ultra board.
00051  *  Please refer to the Getting Started with SAM V71 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 rates
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 with the OTG 
00060  *     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 "SAMv7_USBHostTest.txt" with the text "Test:- SAMV7 
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: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; // 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     TCHAR Drv_Num;
00123 
00124     /* Disable watchdog*/
00125     WDT_Disable( WDT );
00126   
00127     SCB_EnableICache();
00128     SCB_EnableDCache();
00129 
00130     /* Output example information */
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     // Start USB host stack
00140     USBH_start();
00141 
00142     // The USB management is entirely managed by interrupts.
00143     // As a consequence, the user application does only have :
00144     // - to play with the power modes
00145     // - to create a file on each new LUN connected
00146     while (true) {
00147         //sleepmgr_enter_sleep();
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             // Check if LUN has been already tested
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             // Mount drive
00166             memset(&fs, 0, sizeof(FATFS));
00167             res = f_mount(&fs, (TCHAR const*)&lun, 1);
00168             if (FR_INVALID_DRIVE == res) {
00169                 // LUN is not present
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             // Create a test file on the disk
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                 // LUN not ready
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                 // LUN test error
00196                 lun_states[lun] = TEST_ERROR;
00197                 f_close(&file_object);
00198                 printf("File system error\n\r");
00199                 continue;
00200             }
00201             // Write to test file
00202             f_puts((const TCHAR *)MSG_TEST, &file_object);
00203              printf("File Writing\n\r");
00204             // LUN test OK
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); // Test fail
00211             } else if (MAX_DRIVE != main_count_states(TEST_NULL)) {
00212                 if (main_count_states(TEST_ERROR)) {
00213                     ui_test_finish(false); // Test fail
00214                 }else if (main_count_states(TEST_OK)) {
00215                     ui_test_flag_reset();
00216                     ui_test_finish(true); // Test OK
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(); // LUN is unplugged, reset flags
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 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines