SAMV71 Xplained Ultra Software Package 1.4

ui.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  * \defgroup UI User Interface
00031  *
00032  * Human interface on  SAM V71 Xplained Ultra board:
00033  * - Led 0 is continuously on when a device is connected
00034  * - Led 0 blinks when the device is enumerated and USB in idle mode
00035  *   - The blink is slow (1s) with low speed device
00036  *   - The blink is normal (0.5s) with full speed device
00037  *   - The blink is fast (0.25s) with high speed device
00038  * - Led 1 is on when a read or write access is on going or the mouse moves.
00039  */
00040 
00041 #include "board.h"
00042 #include "ui.h"
00043 #include "ctrl_access.h"
00044 #include "uhi_msc_mem.h"
00045 
00046 
00047 /**
00048  * \name Internal routines to manage asynchronous interrupt pin change
00049  * This interrupt is connected to a switch and allows to wakeup CPU in low sleep
00050  * mode.
00051  * This wakeup the USB devices connected via a downstream resume.
00052  * @{
00053  */
00054 static void ui_enable_asynchronous_interrupt(void);
00055 static void ui_disable_asynchronous_interrupt(void);
00056 
00057 /* Interrupt on "pin change" from RIGHT CLICK to do wakeup on USB
00058  *  Note:
00059  *  This interrupt is enable when the USB host enable remotewakeup feature
00060  *  This interrupt wakeup the CPU if this one is in idle mode */
00061 static void ui_wakeup_handler(uint32_t id, uint32_t mask)
00062 {
00063 }
00064 
00065 /**
00066  * \brief Initializes and enables interrupt pin change
00067  */
00068 static void ui_enable_asynchronous_interrupt(void)
00069 {
00070 }
00071 
00072 /**
00073  * \brief Disables interrupt pin change
00074  */
00075 static void ui_disable_asynchronous_interrupt(void)
00076 {
00077 }
00078 
00079 /*! @} */
00080 
00081 /**
00082  * \name Main user interface functions
00083  * @{
00084  */
00085 void ui_init(void)
00086 {
00087     /* Initialize LEDs */
00088     LED_Configure( LED_YELLOW0 ) ;
00089     LED_Configure( LED_YELLOW1 ) ;
00090 }
00091 
00092 void ui_usb_mode_change(bool b_host_mode)
00093 {
00094     UNUSED(b_host_mode);
00095     ui_init();
00096 }
00097 
00098 /*! @} */
00099 
00100 /**
00101  * \name Host mode user interface functions
00102  * @{
00103  */
00104 
00105 /*! Status of device enumeration */
00106 static USBH_enum_status_t ui_enum_status = UHC_ENUM_DISCONNECT;
00107 /*! Blink frequency depending on device speed */
00108 static uint16_t ui_device_speed_blink;
00109 /*! Notify the presence of a USB device mouse */
00110 static bool ui_hid_mouse_plug = false;
00111 /*! Notify the presence of a USB device MSC */
00112 static bool ui_msc_plug = false;
00113 /*! Status of the MSC test */
00114 static bool ui_test_done;
00115 /*! Result of the MSC test */
00116 static bool ui_test_result;
00117 /*! Manages device mouse moving */
00118 static int8_t ui_x, ui_y, ui_scroll;
00119 /*! Manages device mouse button down */
00120 static uint8_t ui_nb_down = 0;
00121 
00122 void ui_usb_vbus_change(bool b_vbus_present)
00123 {
00124     
00125 }
00126 
00127 void ui_usb_vbus_error(void)
00128 {
00129 }
00130 
00131 void ui_usb_connection_event(USBH_device_t *dev, bool b_present)
00132 {
00133     UNUSED(dev);
00134     if (b_present) {
00135         LED_Set(LED_YELLOW0);
00136     } else {
00137         LED_Clear(LED_YELLOW0);
00138         ui_enum_status = UHC_ENUM_DISCONNECT;
00139     }
00140 }
00141 
00142 void ui_usb_enum_event(USBH_device_t *dev, USBH_enum_status_t status)
00143 {
00144     UNUSED(dev);
00145     ui_enum_status = status;
00146     switch (dev->speed) {
00147     case UHD_SPEED_HIGH:
00148         ui_device_speed_blink = 250;
00149         break;
00150 
00151     case UHD_SPEED_FULL:
00152         ui_device_speed_blink = 500;
00153         break;
00154 
00155     case UHD_SPEED_LOW:
00156     default:
00157         ui_device_speed_blink = 1000;
00158         break;
00159     }
00160     ui_test_done = false;
00161 }
00162 void ui_uhi_hid_mouse_change(USBH_device_t * dev, bool b_plug)
00163 {
00164     UNUSED(dev);
00165     ui_hid_mouse_plug = b_plug;
00166 }
00167 
00168 void ui_uhi_msc_change(USBH_device_t * dev, bool b_plug)
00169 {
00170     UNUSED(dev);
00171     ui_msc_plug = b_plug;
00172 }
00173 
00174 void ui_usb_wakeup_event(void)
00175 {
00176     ui_disable_asynchronous_interrupt();
00177 }
00178 
00179 void ui_usb_sof_event(void)
00180 {
00181     bool b_btn_state;
00182     static bool btn_suspend_and_remotewakeup = false;
00183     static uint16_t counter_sof = 0;
00184 
00185     if (ui_enum_status == UHC_ENUM_SUCCESS) {
00186         /* Display device enumerated and in active mode */
00187         if (++counter_sof > ui_device_speed_blink) {
00188             counter_sof = 0;
00189             LED_Toggle(LED_YELLOW0);
00190         }
00191         /* Power on a LED when the mouse move */
00192         if (!ui_x && !ui_y && !ui_scroll) {
00193             LED_Clear(LED_YELLOW1);
00194         } else {
00195             ui_x = ui_y = ui_scroll = 0;
00196             LED_Set(LED_YELLOW1);
00197         }
00198     }
00199 }
00200 
00201 static void ui_uhi_hid_mouse_btn(bool b_state)
00202 {
00203     static uint8_t nb_down = 0;
00204     if (b_state) {
00205         nb_down++;
00206     } else {
00207         nb_down--;
00208     }
00209 }
00210 
00211 void ui_test_flag_reset(void)
00212 {
00213     ui_test_done = false;
00214     LED_Clear(LED_YELLOW0);
00215 }
00216 
00217 void ui_test_finish(bool b_success)
00218 {
00219     ui_test_done = true;
00220     ui_test_result = b_success;
00221     if (b_success) {
00222         LED_Set(LED_YELLOW1);
00223     }
00224 }
00225 
00226 /*! @} */
00227 
00228 /*! \name Callback to mange the HID mouse events
00229  *  @{ */
00230 void ui_uhi_hid_mouse_btn_left(bool b_state)
00231 {
00232     TRACE_INFO_WP("\r\t\t\t\tLeft Button clicked              ");
00233     ui_uhi_hid_mouse_btn(b_state);
00234 }
00235 
00236 void ui_uhi_hid_mouse_btn_right(bool b_state)
00237 {
00238     TRACE_INFO_WP("\r\t\t\t\tRight Button clicked             ");
00239     ui_uhi_hid_mouse_btn(b_state);
00240 }
00241 
00242 void ui_uhi_hid_mouse_btn_middle(bool b_state)
00243 {
00244     ui_uhi_hid_mouse_btn(b_state);
00245 }
00246 
00247 void ui_uhi_hid_mouse_move(int8_t x, int8_t y, int8_t scroll)
00248 {
00249     ui_x = x;
00250     ui_y = y;
00251     TRACE_INFO_WP("\r Mouse at X:%d, Y:%d      ", ui_x, ui_y);
00252     ui_scroll = scroll;
00253 }
00254 /*! @} */
00255 
00256 /*! \name Callback to show the MSC read and write access
00257  *  @{ */
00258 void ui_start_read(void)
00259 {
00260     LED_Set(LED_YELLOW1);
00261 }
00262 
00263 void ui_stop_read(void)
00264 {
00265     LED_Clear(LED_YELLOW1);
00266 }
00267 
00268 void ui_start_write(void)
00269 {
00270     LED_Set(LED_YELLOW1);
00271 }
00272 
00273 void ui_stop_write(void)
00274 {
00275     LED_Clear(LED_YELLOW1);
00276 }
00277 
00278 /*! @} */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines