SAMV71 Xplained Ultra Software Package 1.5

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