Source

cacomantis / arch / at90usb128 / modules / usb / device_chap9 / usb_device_task.c

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/**
 * @file
 *
 * @brief This file manages the USB device controller.
 *
 * The USB task checks the income of new requests from the USB Host.
 * When a Setup request occurs, this task will launch the processing
 * of this setup contained in the usb_standard_request.c file.
 * Other class specific requests are also processed in this file.
 *
 * - Compiler:           IAR EWAVR and GNU GCC for AVR
 * - Supported devices:  AT90USB1287, AT90USB1286, AT90USB647, AT90USB646
 *
 * @author               Atmel Corporation: http://www.atmel.com \n
 *                       Support and FAQ: http://support.atmel.no/
 *
 */
/* Copyright (c) 2009 Atmel Corporation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3. The name of Atmel may not be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * 4. This software may only be redistributed and used in connection with an Atmel
 * AVR product.
 *
 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
//_____  I N C L U D E S _______________________________________________________
#include <stdbool.h>
#include <avr/interrupt.h>
#include "config.h"
#include "conf_usb.h"
#include "usb_device_task.h"
#include "modules/usb/usb_task.h"
#if (USB_OTG_FEATURE == true)
#include "modules/usb/host_chap9/usb_host_task.h"
#endif
#include "lib_mcu/usb/usb_drv.h"
#include "usb_descriptors.h"
#include "modules/usb/device_chap9/usb_standard_request.h"
#include "lib_mcu/pll/pll_drv.h"
#if ((TARGET_BOARD==SPIDER) && (USB_OTG_FEATURE==true))
#include "lib_board/lcd/lcd_drv.h"
#endif
//_____ M A C R O S ____________________________________________________________
//_____ D E F I N I T I O N S ______________________________________________
/**
 * Public : (bool) usb_connected
 * usb_connected is set to true when VBUS has been detected
 * usb_connected is set to false otherwise
 */
bool usb_connected = false;
/**
 * Public : (bool) usb_suspended
 * usb_suspended is set to true when USB is in suspend mode
 * usb_suspended is set to false otherwise
 */
bool usb_suspended = false;

/**
 * Public : (uint8_t) usb_configuration_nb
 * Store the number of the USB configuration used by the USB device
 * when its value is different from zero, it means the device mode is enumerated
 * Used with USB_DEVICE_FEATURE == true only
 */
extern uint8_t usb_configuration_nb;

#if (USB_OTG_FEATURE == true)
/**
 * Public : (uint8_t) otg_b_device_state;
 * Store the current state of the B-Device
 */
uint8_t otg_b_device_state;

/**
 * Public : (uint8_t) otg_device_sessions;
 * Store some events and conditions specifics to OTG Devices sessions
 */
uint8_t otg_device_sessions;

/**
 * Public : (uint8_t) otg_b_device_hnp;
 * Store some events and conditions specifics to OTG B-Device HNP protocol
 */
uint8_t otg_b_device_hnp;

/**
 * Public : (uint16_t) otg_tb_srp_cpt;
 * Counter used to signal a SRP fail condition (SRP fails if Tb_Srp_Fail elapsed)
 */
uint16_t otg_tb_srp_cpt;

/**
 * Public : (uint8_t) sof_seen_in_session;
 * Indicates if a SOF has been received during the current session
 */
uint8_t sof_seen_in_session;
#endif

//_____ D E C L A R A T I O N S ____________________________________________

/**
 * @brief Initialize the USB device controller and system interrupt
 *
 * This function enables the USB controller and init the USB interrupts.
 * The aim is to allow the USB connection detection in order to send
 * the appropriate USB event to the operating mode manager.
 */
void usb_device_task_init( void )
    {
    sei();
    Usb_disable();
    Usb_enable();
    Usb_select_device();
#if (USB_LOW_SPEED_DEVICE==true)
    Usb_low_speed_mode();
#endif
    sei();
#if (USB_OTG_FEATURE == true)
    Usb_enable_id_interrupt();
    Clear_otg_features_from_host();
    otg_device_sessions = 0;
#endif
    }

/**
 * @brief Initialize the USB device controller
 *
 * This function enables the USB controller and init the USB interrupts.
 * The aim is to allow the USB connection detection in order to send
 * the appropriate USB event to the operating mode manager.
 * Start device function is executed once VBUS connection has been detected
 * either by the VBUS change interrupt either by the VBUS high level
 */
void usb_start_device( void )
    {
    Pll_start_auto();
    Wait_pll_ready()
        ;

    cli();
    Usb_unfreeze_clock();
    usb_init_device(); // configure the USB controller EP0
    Usb_attach();
    sei();

    Usb_enable_suspend_interrupt();
    Usb_enable_reset_interrupt();
#if (USB_OTG_FEATURE == true)
    Usb_enable_id_interrupt();
#endif
    }

/**
 * @brief Entry point of the USB device mamagement
 *
 * This function is the entry point of the USB management. Each USB
 * event is checked here in order to launch the appropriate action.
 * If a Setup request occurs on the Default Control Endpoint,
 * the usb_process_request() function is call in the usb_standard_request.c file
 */
void usb_device_task( void )
    {

#if (USB_OTG_FEATURE == true)
    // Check if a reset has been received
    if(Is_usb_event(EVT_USB_RESET))
        {
        Usb_ack_event(EVT_USB_RESET);
        Usb_reset_endpoint(0);
        usb_configuration_nb=0;
        otg_b_device_state = B_IDLE;
        Clear_otg_features_from_host();
        }

    // When OTG mode enabled, B-Device is managed thanks to its state machine
    switch (otg_b_device_state)
        {
        //------------------------------------------------------
        //   B_IDLE state
        //
        //   - waits for Vbus to rise
        //   - initiate SRP if asked by user
        //
        case B_IDLE:
        if (Is_usb_vbus_high())
            {
            // Vbus rise
            usb_connected = true;
            remote_wakeup_feature = false;
            usb_start_device();
            Usb_vbus_on_action();
            Usb_attach();
            otg_b_device_state = B_PERIPHERAL;
            Ack_user_request_srp();
            Clear_otg_features_from_host();
            remote_wakeup_feature = false;
            End_session_with_srp();
            if (Is_srp_sent_and_waiting_answer() && (sof_seen_in_session == true))
                {
                Ack_srp_sent_and_answer();
                Otg_print_new_failure_message(OTGMSG_A_RESPONDED,OTG_TEMPO_2SEC);
                }
            Usb_enable_sof_interrupt();

            }
        else
            {
            if (Is_user_requested_srp() && Is_usb_id_device())
                {
                // User has requested a SRP
                Ack_user_request_srp();
                if (!Is_srp_sent_and_waiting_answer())
                    {
                    Pll_start_auto(); // reinit device mode
                    Wait_pll_ready();
                    Usb_disable();
                    Usb_enable_uid_pin();
                    Usb_enable();
                    Usb_unfreeze_clock();
                    Usb_select_device();
                    Usb_attach();
                    otg_b_device_state = B_SRP_INIT;
                    Usb_device_initiate_srp(); // hardware waits for initial condition (SE0, Session End level)
                    sof_seen_in_session = false;
                    }
                }
            if ((Is_srp_sent_and_waiting_answer()) && (Is_tb_srp_counter_overflow()))
                {
                // SRP failed because A-Device did not respond
                End_session_with_srp();
                Ack_srp_sent_and_answer();
                Otg_print_new_failure_message(OTGMSG_SRP_A_NO_RESP,OTG_TEMPO_3SEC);
                }
            }
        break;

        //------------------------------------------------------
        //   B_SRP_INIT
        //
        //   - a SRP has been initiated
        //   - B-Device waits it is finished to initialize variables
        //
        case B_SRP_INIT:
        if (!Is_usb_device_initiating_srp())
            {
            otg_b_device_state = B_IDLE; // SRP initiated, return to Idle state (wait for Vbus to rise)
            Srp_sent_and_waiting_answer();
            Init_tb_srp_counter();
            Start_session_with_srp();
            Otg_print_new_event_message(OTGMSG_SRP_STARTED,TB_SRP_FAIL_MIN);
            }
        break;

        //------------------------------------------------------
        //   B_PERIPHERAL : the main state of OTG Peripheral
        //
        //   - all events are interrupt-handled
        //   - but they are saved and this function can execute alternate actions
        //   - also handle user requests (disconnect)
        //
        // ======================================================================================
        case B_PERIPHERAL:
        if (Is_otg_event(EVT_OTG_DEVICE_CONNECTED))
            {
            Otg_ack_event(EVT_OTG_DEVICE_CONNECTED); // set on a SetConfiguration descriptor reception
            Otg_print_new_event_message(OTGMSG_CONNECTED_TO_A,OTG_TEMPO_4SEC);
            }
        if (Is_usb_event(EVT_USB_SUSPEND)) // SUSPEND state

            {
            // Suspend and HNP operations are handled in the interrupt functions
            }
        if (Is_srp_sent_and_waiting_answer() && (sof_seen_in_session == true))
            {
            Ack_srp_sent_and_answer();
            Otg_print_new_failure_message(OTGMSG_A_RESPONDED,OTG_TEMPO_2SEC);
            }
        if ((Is_srp_sent_and_waiting_answer()) && (Is_tb_srp_counter_overflow()))
            {
            // SRP failed because A-Device did not respond
            End_session_with_srp();
            Ack_srp_sent_and_answer();
            Otg_print_new_failure_message(OTGMSG_SRP_A_NO_RESP,OTG_TEMPO_3SEC);
            }

        if (Is_usb_event(EVT_USB_RESUME) && !Is_usb_pending_remote_wake_up()) // RESUME signal detected

            {
            Usb_ack_event(EVT_USB_RESUME);
            Usb_ack_event(EVT_USB_SUSPEND);
            Usb_ack_remote_wake_up_start();
            }
        if (Is_usb_event(EVT_USB_UNPOWERED))
            {
            Usb_ack_event(EVT_USB_UNPOWERED);
            Clear_all_user_request();
            otg_b_device_state = B_IDLE;
            }
        if(Is_usb_event(EVT_USB_RESET))
            {
            Usb_ack_event(EVT_USB_RESET);
            Usb_reset_endpoint(0);
            usb_configuration_nb=0;
            Clear_otg_features_from_host();
            }
        if (Is_otg_event(EVT_OTG_HNP_ERROR))
            {
            Otg_ack_event(EVT_OTG_HNP_ERROR);
            Otg_print_new_failure_message(OTGMSG_DEVICE_NO_RESP,OTG_TEMPO_4SEC);
            PORTC &= ~0x10;
            }
        if (Is_user_requested_disc())
            {
            Ack_user_request_disc();
            if (Is_usb_id_device())
                {
                Usb_detach();
                Usb_freeze_clock();
                while (Is_usb_vbus_high()); // wait for Vbus to be under Va_vbus_valid
                otg_b_device_state = B_IDLE;
                usb_configuration_nb = 0;
                usb_connected = false;
                Clear_all_user_request();
                }
            }
        break;

        //------------------------------------------------------
        //   B_HOST
        //
        //   - state entered after an HNP success
        //   - handle user requests (disconnection, suspend, hnp)
        //   - call the "host_task()" for Host level handlers
        //
        // ======================================================================================
        case B_HOST:
        if (Is_otg_event(EVT_OTG_DEV_UNSUPPORTED))
            {
            Otg_ack_event(EVT_OTG_DEV_UNSUPPORTED);
            Clear_all_user_request();
            otg_b_device_state = B_IDLE;
            device_state = DEVICE_UNATTACHED;
            }
        if (Is_user_requested_disc() || Is_user_requested_suspend() || Is_user_requested_hnp())
            {
            Ack_user_request_disc(); // suspend and hnp requests cleared in B_END_HNP_SUSPEND stage
            Host_disable_sof(); // go into suspend mode
            Usb_host_reject_hnp();
            otg_b_device_state = B_END_HNP_SUSPEND;
            Usb_ack_suspend();
            Usb_enable_suspend_interrupt();
            }
        if (Is_usb_event(EVT_USB_UNPOWERED))
            {
            Usb_ack_event(EVT_USB_UNPOWERED);
            Usb_freeze_clock();
            otg_b_device_state = B_IDLE;
            device_state = DEVICE_UNATTACHED;
            }
        usb_host_task(); // call the host task
        break;

        //------------------------------------------------------
        //   B_END_HNP_SUSPEND
        //
        //   - device enters this state after being B_HOST, on a user request to stop bus activity (suspend, disconnect or hnp request)
        //   - macro is reset to peripheral mode
        //
        // ======================================================================================
        case B_END_HNP_SUSPEND:
        if (Is_usb_event(EVT_USB_SUSPEND))
            {
            Usb_ack_event(EVT_USB_SUSPEND);
            Usb_device_stop_hnp();
            Usb_select_device();
            device_state = DEVICE_UNATTACHED;
            if (Is_user_requested_hnp() || Is_user_requested_suspend())
                {
                otg_b_device_state = B_PERIPHERAL;
                Ack_user_request_suspend();
                Ack_user_request_hnp();
                }
            else
                {
                otg_b_device_state = B_IDLE;
                Usb_detach();
                Usb_freeze_clock();
                }
            }
        break;

        default:
        otg_b_device_state = B_IDLE;
        Clear_all_user_request();
        device_state = DEVICE_UNATTACHED;
        break;
        }

#else

    // Non-OTG exclusives Device operations

    // VBUS state detection
    if( Is_usb_vbus_high() && ( usb_connected == false ) )
        {
        usb_connected = true;
        remote_wakeup_feature = false;
        Usb_vbus_on_action();
        Usb_send_event(EVT_USB_POWERED);
        usb_start_device();
        }
    if( Is_usb_vbus_low() && ( usb_connected == true ) )
        {
        usb_connected = false;
        usb_configuration_nb = 0;
        Usb_send_event(EVT_USB_UNPOWERED);
        Usb_detach();
        Usb_freeze_clock();
        Usb_vbus_off_action();
        }

    if( Is_usb_event(EVT_USB_RESET) )
        {
        Usb_ack_event(EVT_USB_RESET);
        Usb_reset_endpoint(0);
        usb_configuration_nb = 0;
        }

#endif

    // =======================================
    // Common Standard Device Control Requests
    // =======================================
    //   - device enumeration process
    //   - device control commands and features
    Usb_select_endpoint(EP_CONTROL);
    if( Is_usb_receive_setup() )
        {
        usb_process_request();
        }
    }