SAMV71 Xplained Ultra Software Package 1.3

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
00039  * - Led 1 is on when a LUN test is success
00040  * - Led 1 blinks when a LUN test is unsuccess
00041  */
00042 
00043 #include "board.h"
00044 #include "ui.h"
00045 #include "ctrl_access.h"
00046 #include "uhi_msc_mem.h"
00047 
00048 
00049 /**
00050  * \name Internal routines to manage asynchronous interrupt pin change
00051  * This interrupt is connected to a switch and allows to wakeup CPU in low sleep
00052  * mode.
00053  * This wakeup the USB devices connected via a downstream resume.
00054  * @{
00055  */
00056 static void ui_enable_asynchronous_interrupt(void);
00057 static void ui_disable_asynchronous_interrupt(void);
00058 
00059 /* Interrupt on "pin change" from RIGHT CLICK to do wakeup on USB
00060  *  Note:
00061  *  This interrupt is enable when the USB host enable remotewakeup feature
00062  *  This interrupt wakeup the CPU if this one is in idle mode */
00063 static void ui_wakeup_handler(uint32_t id, uint32_t mask)
00064 {
00065     /*if (RESUME_PIO_ID == id && RESUME_PIO_MASK == mask) {
00066         if (USBH_is_suspend()) {
00067             ui_disable_asynchronous_interrupt();
00068 
00069             // Wakeup host and device 
00070             USBH_resume();
00071         }
00072     }*/
00073 }
00074 
00075 /**
00076  * \brief Initializes and enables interrupt pin change
00077  */
00078 static void ui_enable_asynchronous_interrupt(void)
00079 {
00080     /* Enable interrupt for button pin */
00081     /*pio_get_interrupt_status(PIOB);
00082     pio_enable_pin_interrupt(GPIO_PUSH_BUTTON_2);
00083     /* Enable fastwakeup for button pin */
00084     //pmc_set_fast_startup_input(PMC_FSMR_FSTT14);
00085 }
00086 
00087 /**
00088  * \brief Disables interrupt pin change
00089  */
00090 static void ui_disable_asynchronous_interrupt(void)
00091 {
00092     /* Disable interrupt for button pin */
00093     //pio_disable_pin_interrupt(GPIO_PUSH_BUTTON_2);
00094     //pio_get_interrupt_status(PIOB);
00095     /* Enable fastwakeup for button pin */
00096     //pmc_clr_fast_startup_input(PMC_FSMR_FSTT14);
00097 }
00098 
00099 /*! @} */
00100 
00101 /**
00102  * \name Main user interface functions
00103  * @{
00104  */
00105 void ui_init(void)
00106 {
00107     /* Enable PIO clock for button inputs */
00108     //pmc_enable_periph_clk(ID_PIOB);
00109     //pmc_enable_periph_clk(ID_PIOE);
00110     /* Set handler for wakeup */
00111     //pio_handler_set(RESUME_PIO, RESUME_PIO_ID, RESUME_PIO_MASK,
00112         //  RESUME_PIO_ATTR, ui_wakeup_handler);
00113     /* Enable IRQ for button (PIOB) */
00114     //NVIC_EnableIRQ((IRQn_Type)RESUME_PIO_ID);
00115     /* Enable interrupt for button pin */
00116     //pio_get_interrupt_status(RESUME_PIO);
00117     //pio_configure_pin(RESUME_PIN, RESUME_PIO_ATTR);
00118     //pio_enable_pin_interrupt(RESUME_PIN);
00119     /* Enable fastwakeup for button pin */
00120     //pmc_set_fast_startup_input(RESUME_PMC_FSTT);
00121    /* Initialize LEDs */
00122     LED_Configure( LED_YELLOW0 ) ;
00123     LED_Configure( LED_YELLOW1 ) ;
00124 
00125     LED_Clear(LED_YELLOW0);
00126     LED_Clear(LED_YELLOW1);
00127 
00128 }
00129 
00130 void ui_usb_mode_change(bool b_host_mode)
00131 {
00132     UNUSED(b_host_mode);
00133     ui_init();
00134 }
00135 
00136 /*! @} */
00137 
00138 /**
00139  * \name Host mode user interface functions
00140  * @{
00141  */
00142 
00143 /*! Status of device enumeration */
00144 static USBH_enum_status_t ui_enum_status = UHC_ENUM_DISCONNECT;
00145 /*! Blink frequency depending on device speed */
00146 static uint16_t ui_device_speed_blink;
00147 /*! Notify the presence of a USB device mouse */
00148 static bool ui_hid_mouse_plug = false;
00149 /*! Notify the presence of a USB device MSC */
00150 static bool ui_msc_plug = false;
00151 /*! Status of the MSC test */
00152 static bool ui_test_done;
00153 /*! Result of the MSC test */
00154 static bool ui_test_result;
00155 /*! Manages device mouse moving */
00156 static int8_t ui_x, ui_y, ui_scroll;
00157 /*! Manages device mouse button down */
00158 static uint8_t ui_nb_down = 0;
00159 
00160 void ui_usb_vbus_change(bool b_vbus_present)
00161 {
00162     
00163 }
00164 
00165 void ui_usb_vbus_error(void)
00166 {
00167 }
00168 
00169 void ui_usb_connection_event(USBH_device_t *dev, bool b_present)
00170 {
00171     UNUSED(dev);
00172     if (b_present) {
00173         LED_Set(LED_YELLOW0);
00174     } else {
00175         LED_Clear(LED_YELLOW0);
00176         ui_enum_status = UHC_ENUM_DISCONNECT;
00177     }
00178 }
00179 
00180 void ui_usb_enum_event(USBH_device_t *dev, USBH_enum_status_t status)
00181 {
00182     UNUSED(dev);
00183     ui_enum_status = status;
00184     switch (dev->speed) {
00185     case UHD_SPEED_HIGH:
00186         ui_device_speed_blink = 250;
00187         break;
00188 
00189     case UHD_SPEED_FULL:
00190         ui_device_speed_blink = 500;
00191         break;
00192 
00193     case UHD_SPEED_LOW:
00194     default:
00195         ui_device_speed_blink = 1000;
00196         break;
00197     }
00198     ui_test_done = false;
00199 }
00200 void ui_uhi_hid_mouse_change(USBH_device_t * dev, bool b_plug)
00201 {
00202     UNUSED(dev);
00203     ui_hid_mouse_plug = b_plug;
00204 }
00205 
00206 void ui_uhi_msc_change(USBH_device_t * dev, bool b_plug)
00207 {
00208     UNUSED(dev);
00209     ui_msc_plug = b_plug;
00210 }
00211 
00212 void ui_usb_wakeup_event(void)
00213 {
00214     ui_disable_asynchronous_interrupt();
00215 }
00216 
00217 void ui_usb_sof_event(void)
00218 {
00219     bool b_btn_state;
00220     static bool btn_suspend_and_remotewakeup = false;
00221     static uint16_t counter_sof = 0;
00222 
00223     if (ui_enum_status == UHC_ENUM_SUCCESS) {
00224         /* Display device enumerated and in active mode */
00225         if (++counter_sof > ui_device_speed_blink) {
00226             counter_sof = 0;
00227         }
00228 
00229         /* Scan button to enter in suspend mode and remote wakeup */
00230         /*b_btn_state = (!gpio_pin_is_high(GPIO_PUSH_BUTTON_1)) ?
00231                 true : false;*/
00232         b_btn_state = true ;
00233         if (b_btn_state != btn_suspend_and_remotewakeup) {
00234             /* Button have changed */
00235             btn_suspend_and_remotewakeup = b_btn_state;
00236             if (b_btn_state) {
00237                 /* Button has been pressed */               
00238                 ui_enable_asynchronous_interrupt();
00239                 USBH_suspend(true);
00240                 return;
00241             }
00242         }
00243 
00244         /* Power on a LED when the mouse move */
00245         if (!ui_x && !ui_y && !ui_scroll) {
00246             LED_Clear(LED_YELLOW1);
00247         } else {
00248             ui_x = ui_y = ui_scroll = 0;
00249             LED_Set(LED_YELLOW1);
00250         }
00251     }
00252 }
00253 
00254 
00255 
00256 static void ui_uhi_hid_mouse_btn(bool b_state)
00257 {
00258     static uint8_t nb_down = 0;
00259     if (b_state) {
00260         nb_down++;
00261     } else {
00262         nb_down--;
00263     }
00264 
00265 }
00266 
00267 void ui_test_flag_reset(void)
00268 {
00269     ui_test_done = false;
00270     LED_Clear(LED_YELLOW0);
00271 }
00272 
00273 void ui_test_finish(bool b_success)
00274 {
00275     ui_test_done = true;
00276     ui_test_result = b_success;
00277     if (b_success) {
00278         LED_Set(LED_YELLOW1);
00279     }
00280 }
00281 
00282 /*! @} */
00283 
00284 /*! \name Callback to mange the HID mouse events
00285  *  @{ */
00286 void ui_uhi_hid_mouse_btn_left(bool b_state)
00287 {
00288     TRACE_INFO_WP("\r\t\t\t\tLeft Button clicked              ");
00289     ui_uhi_hid_mouse_btn(b_state);
00290 }
00291 
00292 void ui_uhi_hid_mouse_btn_right(bool b_state)
00293 {
00294     TRACE_INFO_WP("\r\t\t\t\tRight Button clicked             ");
00295     ui_uhi_hid_mouse_btn(b_state);
00296 }
00297 
00298 void ui_uhi_hid_mouse_btn_middle(bool b_state)
00299 {
00300     ui_uhi_hid_mouse_btn(b_state);
00301 }
00302 
00303 void ui_uhi_hid_mouse_move(int8_t x, int8_t y, int8_t scroll)
00304 {
00305     ui_x = x;
00306     ui_y = y;
00307     TRACE_INFO_WP("\r Mouse at X:%d, Y:%d      ", ui_x, ui_y);
00308     ui_scroll = scroll;
00309 }
00310 /*! @} */
00311 
00312 /*! \name Callback to show the MSC read and write access
00313  *  @{ */
00314 void ui_start_read(void)
00315 {
00316     LED_Set(LED_YELLOW1);
00317 }
00318 
00319 void ui_stop_read(void)
00320 {
00321     LED_Clear(LED_YELLOW1);
00322 }
00323 
00324 void ui_start_write(void)
00325 {
00326     LED_Set(LED_YELLOW1);
00327 }
00328 
00329 void ui_stop_write(void)
00330 {
00331     LED_Clear(LED_YELLOW1);
00332 }
00333 
00334 /*! @} */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines