- Main Page
- Modules
- Data Structures
- Files
- Related Pages

# usb_task.h

Go to the documentation of this file.

    00001 /*This file has been prepared for Doxygen automatic documentation generation.*/
    00013
    00014 /* Copyright (c) 2007, Atmel Corporation All rights reserved.
    00015  *
    00016  * Redistribution and use in source and binary forms, with or without
    00017  * modification, are permitted provided that the following conditions are met:
    00018  *
    00019  * 1. Redistributions of source code must retain the above copyright notice,
    00020  * this list of conditions and the following disclaimer.
    00021  *
    00022  * 2. Redistributions in binary form must reproduce the above copyright notice,
    00023  * this list of conditions and the following disclaimer in the documentation
    00024  * and/or other materials provided with the distribution.
    00025  *
    00026  * 3. The name of ATMEL may not be used to endorse or promote products derived
    00027  * from this software without specific prior written permission.
    00028  *
    00029  * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
    00030  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    00031  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
    00032  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
    00033  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    00034  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    00035  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    00036  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00037  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    00038  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00039  */
    00040
    00041 #ifndef _USB_TASK_H_
    00042 #define _USB_TASK_H_
    00043
    00044 #include "modules/usb/usb_commun.h"
    00045
    00048
    00049 //_____ I N C L U D E S ____________________________________________________
    00050
    00051
    00052 //_____ M A C R O S ________________________________________________________
    00053
    00057 #define Usb_send_event(x)               (g_usb_event |= (1<<x))
    00058 #define Usb_ack_event(x)                (g_usb_event &= ~(1<<x))
    00059 #define Usb_clear_all_event()           (g_usb_event = 0)
    00060 #define Is_usb_event(x)                 ((g_usb_event & (1<<x)) ? TRUE : FALSE)
    00061 #define Is_not_usb_event(x)             ((g_usb_event & (1<<x)) ? FALSE: TRUE)
    00062 #define Is_host_emergency_exit()        (Is_usb_id_device())
    00063 #define Is_usb_device()                 (g_usb_mode==USB_MODE_DEVICE ? TRUE : FALSE)
    00064 #define Is_usb_host()                   (g_usb_mode==USB_MODE_HOST   ? TRUE : FALSE)
    00065
    00066 #define Otg_send_event(x)               (g_otg_event |= (1<<x))
    00067 #define Otg_ack_event(x)                (g_otg_event &= ~(1<<x))
    00068 #define Otg_clear_all_event()           (g_otg_event = 0)
    00069 #define Is_otg_event(x)                 ((g_otg_event & (1<<x)) ? TRUE : FALSE)
    00070 #define Is_not_otg_event(x)             ((g_otg_event & (1<<x)) ? FALSE: TRUE)
    00071
    00072 #define EVT_USB_POWERED               1         // USB plugged
    00073 #define EVT_USB_UNPOWERED             2         // USB un-plugged
    00074 #define EVT_USB_DEVICE_FUNCTION       3         // USB in device
    00075 #define EVT_USB_HOST_FUNCTION         4         // USB in host
    00076 #define EVT_USB_SUSPEND               5         // USB suspend
    00077 #define EVT_USB_WAKE_UP               6         // USB wake up
    00078 #define EVT_USB_RESUME                7         // USB resume
    00079 #define EVT_USB_RESET                 8         // USB reset
    00080 #define EVT_HOST_SOF                  9         // Host start of frame sent
    00081 #define EVT_HOST_HWUP                 10        // Host wakeup detected
    00082 #define EVT_HOST_DISCONNECTION        11        // The target device is disconnected
    00083 #define EVT_HOST_REMOTE_WAKEUP        12        // Remote WakeUp has been received (Host)
    00084
    00085 #define EVT_OTG_HNP_ERROR             1        // The HNP has failed (A has not responded)
    00086 #define EVT_OTG_HNP_SUCCESS           2        // The HNP has succedeed
    00087 #define EVT_OTG_SRP_RECEIVED          3        // A-Device received a SRP
    00088 #define EVT_OTG_DEV_UNSUPPORTED       4        // An error occured while the device was enumerated
    00089 #define EVT_OTG_DEVICE_CONNECTED      5        // B-Device has been configured
    00091
    00092 #define USB_MODE_UNDEFINED            0x00
    00093 #define USB_MODE_HOST                 0x01
    00094 #define USB_MODE_DEVICE               0x02
    00095
    00096
    00097 #define   VBUS_PULSE    1
    00098 #define   DATA_PULSE    0
    00099
    00100 #define   BDEV_HNP_NB_RETRY     3       // number of times that a B-Device retries a HNP with a failure as a result
    00101                                         // after "n" failures, the "b_hnp_enable" feature is cancelled
    00102
    00103
    00107 #define USER_RQST_SRP           0x01
    00108 #define USER_RQST_SUSPEND       0x02
    00109 #define USER_RQST_VBUS          0x04    // Vbus TOGGLE
    00110 #define USER_RQST_HNP           0x08
    00111 #define USER_RQST_RESUME        0x10
    00112 #define USER_RQST_RESET         0x20
    00113 #define USER_RQST_DISCONNECT    0x40
    00114
    00115 // Ask for the B-PERIPH to generate a SRP
    00116 #define Is_user_requested_srp()       (((otg_user_request&USER_RQST_SRP) != 0) ? TRUE : FALSE)
    00117 #define Set_user_request_srp()        (otg_user_request |= USER_RQST_SRP)
    00118 #define Ack_user_request_srp()        (otg_user_request &= ~USER_RQST_SRP)
    00119
    00120 // Ask for the A-HOST to enter suspend
    00121 #define Is_user_requested_suspend()   (((otg_user_request&USER_RQST_SUSPEND) != 0) ? TRUE : FALSE)
    00122 #define Set_user_request_suspend()    (otg_user_request |= USER_RQST_SUSPEND)
    00123 #define Ack_user_request_suspend()    (otg_user_request &= ~USER_RQST_SUSPEND)
    00124
    00125 // Ask for the A-HOST to toggle Vbus
    00126 #define Is_user_requested_vbus()      (((otg_user_request&USER_RQST_VBUS) != 0) ? TRUE : FALSE)
    00127 #define Set_user_request_vbus()       (otg_user_request |= USER_RQST_VBUS)
    00128 #define Ack_user_request_vbus()       (otg_user_request &= ~USER_RQST_VBUS)
    00129
    00130 // Ask for an HNP initiation on both devices
    00131 #define Is_user_requested_hnp()       (((otg_user_request&USER_RQST_HNP) != 0) ? TRUE : FALSE)
    00132 #define Set_user_request_hnp()        (otg_user_request |= USER_RQST_HNP)
    00133 #define Ack_user_request_hnp()        (otg_user_request &= ~USER_RQST_HNP)
    00134
    00135 // Ask for the B-PERIPH or the A-PERIPH to disconnect from the bus
    00136 #define Is_user_requested_disc()      (((otg_user_request&USER_RQST_DISCONNECT) != 0) ? TRUE : FALSE)
    00137 #define Set_user_request_disc()       (otg_user_request |= USER_RQST_DISCONNECT)
    00138 #define Ack_user_request_disc()       (otg_user_request &= ~USER_RQST_DISCONNECT)
    00139
    00140 /* NOT USED */
    00141 /*
    00142 // Ask for the A-HOST to resume the bus or the B-PERIPH to send an upstream resume
    00143 #define Is_user_requested_resume()    (((otg_user_request&USER_RQST_RESUME) != 0) ? TRUE : FALSE)
    00144 #define Set_user_request_resume()     (otg_user_request |= USER_RQST_RESUME)
    00145 #define Ack_user_request_resume()     (otg_user_request &= ~USER_RQST_RESUME)
    00146
    00147 // Ask for the A-HOST to reset the bus
    00148 #define Is_user_requested_reset()     (((otg_user_request&USER_RQST_RESET) != 0) ? TRUE : FALSE)
    00149 #define Set_user_request_reset()      (otg_user_request |= USER_RQST_RESET)
    00150 #define Ack_user_request_reset()      (otg_user_request &= ~USER_RQST_RESET)
    00151 */
    00152
    00153 #define Clear_all_user_request()      (otg_user_request = 0)
    00154
    00155
    00166 #define Otg_timer_init()              (Timer16_select(OTG_USE_TIMER), Timer16_set_clock(TIMER16_CLKIO_BY_256), \
    00167                                        Timer16_set_mode_output_a(TIMER16_COMP_MODE_NORMAL), \
    00168                                        Timer16_set_waveform_mode(TIMER16_WGM_CTC_OCR), \
    00169                                        Timer16_set_compare_a(62), Timer16_set_counter(0x0000), \
    00170                                        Timer16_clear_compare_a_it(), Timer16_compare_a_it_enable())
    00171
    00172 //_____ D E C L A R A T I O N S ____________________________________________
    00173
    00174 extern volatile U16 g_usb_event;
    00175 extern U8 g_usb_mode;
    00176 extern U8 remote_wakeup_feature;
    00177
    00191 void usb_task_init     (void);
    00192
    00203 void usb_task          (void);
    00204
    00205 extern volatile U8 private_sof_counter;
    00206
    00207
    00215 extern volatile U8 otg_features_supported;
    00216 extern U8 otg_user_request;
    00217
    00218 extern volatile U16 g_otg_event;
    00219
    00220 #if (OTG_VBUS_AUTO_AFTER_A_PLUG_INSERTION == ENABLED)
    00221   extern U8 id_changed_to_host_event;
    00222 #endif
    00223
    00224 extern void otg_not_supported_device(void);
    00225
    00226 #if (USB_OTG_FEATURE == ENABLED)
    00234   #ifndef   OTG_COMPLIANCE_TRICKS
    00235     #define OTG_COMPLIANCE_TRICKS                   DISABLED
    00236   #endif
    00237
    00244   #ifndef   OTG_VBUS_AUTO_AFTER_A_PLUG_INSERTION
    00245     #define OTG_VBUS_AUTO_AFTER_A_PLUG_INSERTION    DISABLED
    00246   #endif
    00247
    00252   #ifndef   OTG_B_DEVICE_AUTORUN_HNP_IF_REQUIRED
    00253     #define OTG_B_DEVICE_AUTORUN_HNP_IF_REQUIRED    ENABLED
    00254   #endif
    00255
    00259   #ifndef   OTG_RESET_LENGTH
    00260     #define OTG_RESET_LENGTH                        1
    00261   #endif
    00262 #endif
    00263
    00264
    00268
    00269 #define   MSG_DISPLAY_NODELAY     0xFFFF
    00270 #define   OTG_TEMPO_1SEC          0x01F4  // 500 (x2ms)
    00271 #define   OTG_TEMPO_2SEC          0x03E8  // 1000
    00272 #define   OTG_TEMPO_3SEC          0x05DC  // 1500
    00273 #define   OTG_TEMPO_4SEC          0x07D0  // 2000
    00274 #define   OTR_TEMPO_5SEC          0x09C4  // 2500
    00275
    00276 // These messages ID numbers must be used with the OTG messaging functions
    00277 #define   OTGMSG_SRP_STARTED      1      // Event
    00278 #define   OTGMSG_SRP_A_NO_RESP    2      // FAILURE msg
    00279 #define   OTGMSG_A_RESPONDED      3      // Event
    00280 #define   OTGMSG_CONNECTED_TO_A   4      // Event
    00281 #define   OTGMSG_UNSUPPORTED      5      // FAILURE msg
    00282 #define   OTGMSG_UNSUPPORTED_HUB  6      // FAILURE msg
    00283 #define   OTGMSG_SRP_RECEIVED     7      // Event
    00284 #define   OTGMSG_DEVICE_NO_RESP   8      // FAILURE msg
    00285 #define   OTGMSG_VBUS_SURCHARGE   9      // Event
    00286
    00287 // Messages definitions for OTG library, should not be used directly by user
    00288 #define   OTGSTR_SRP_STARTED      "SRP Initiated     "  // Event
    00289 #define   OTGSTR_SRP_A_NO_RESP    "A-Dev No Response "  // FAILURE
    00290 #define   OTGSTR_A_RESPONDED      "A-Device Responded"  // Event
    00291 #define   OTGSTR_CONNECTED_TO_A   "Connected to A-Dev"  // Event
    00292 #define   OTGSTR_UNSUPPORTED      "Unsupported Device"  // FAILURE
    00293 #define   OTGSTR_UNSUPPORTED_HUB  "Hub Unsuppported  "  // FAILURE
    00294 #define   OTGSTR_SRP_RECEIVED     "SRP Received      "  // Event
    00295 #define   OTGSTR_DEVICE_NO_RESP   "Device No Response"  // FAILURE
    00296 #define   OTGSTR_VBUS_SURCHARGE   "VBUS OverCurrent !"  // Event
    00297
    00298 extern U16 otg_msg_event_delay;
    00299 extern U16 otg_msg_failure_delay;
    00300
    00301 // Output messaging method choice (mode and functions defined in "conf_usb.h")
    00302 #define     OTGMSG_NONE     0   // no messages displayed
    00303 #define     OTGMSG_FAIL     1   // only failure messages displayed
    00304 #define     OTGMSG_ALL      2   // all messages displayed
    00305
    00306 #if (OTG_MESSAGING_OUTPUT == OTGMSG_ALL)            // ALL MESSAGES
    00307   extern void Otg_messaging_init(void);
    00308   extern void Otg_output_event_msg(U8);
    00309   extern void Otg_output_failure_msg(U8);
    00310   extern void Otg_output_event_clear(void);
    00311   extern void Otg_output_failure_clear(void);
    00313   #define   Otg_print_new_event_message(str,tm)     (Otg_output_event_msg(str), otg_msg_event_delay = tm)
    00314   #define   Otg_clear_event_message()               Otg_output_event_clear()
    00315   #define   Get_event_msg_delay()                   (otg_msg_event_delay)
    00316   #define   Decrement_event_msg_delay()             (otg_msg_event_delay--)
    00318   #define   Otg_print_new_failure_message(str,tm)   (Otg_output_failure_msg(str), otg_msg_failure_delay = tm)
    00319   #define   Otg_clear_failure_message()             Otg_output_failure_clear()
    00320   #define   Get_failure_msg_delay()                 (otg_msg_failure_delay)
    00321   #define   Decrement_failure_msg_delay()           (otg_msg_failure_delay--)
    00322 #elif (OTG_MESSAGING_OUTPUT == OTGMSG_FAIL)         // ONLY FAILURE MESSAGES (NEEDED FOR COMPLIANCE)
    00323   extern void Otg_messaging_init(void);
    00324   extern void Otg_output_failure_msg(U8);
    00325   extern void Otg_output_failure_clear(void);
    00326   #define   Otg_print_new_event_message(str,tm)
    00327   #define   Otg_clear_event_message()
    00328   #define   Get_event_msg_delay()
    00329   #define   Decrement_event_msg_delay()
    00330   #define   Otg_print_new_failure_message(str,tm)   (Otg_output_failure_msg(str), otg_msg_failure_delay = tm)
    00331   #define   Otg_clear_failure_message()             Otg_output_failure_clear()
    00332   #define   Get_failure_msg_delay()                 (otg_msg_failure_delay)
    00333   #define   Decrement_failure_msg_delay()           (otg_msg_failure_delay--)
    00334 #else                                               // NOT OTG COMPLIANT
    00335   #define   Otg_messaging_init()
    00336   #define   Otg_print_new_event_message(str,tm)
    00337   #define   Otg_clear_event_message()
    00338   #define   Get_event_msg_delay()
    00339   #define   Decrement_event_msg_delay()
    00340   #define   Otg_print_new_failure_message(str,tm)
    00341   #define   Otg_clear_failure_message()
    00342   #define   Get_failure_msg_delay()
    00343   #define   Decrement_failure_msg_delay()
    00344 #endif
    00346
    00347 #endif /* _USB_TASK_H_ */
    00348