summaryrefslogtreecommitdiff
path: root/MakefileBasedBuild/Atmel/sam3x/sam3x-ek/libraries/usb_host/enum/host/usb_host_task.h
blob: c72049942525069c9c66b7289946dcadda02cd45 (plain)
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
/* This header file is part of the AVR Software Framework 2.0.0 release */

/*This file is prepared for Doxygen automatic documentation generation.*/
/*! \file ******************************************************************
 *
 * \brief Management of the USB host controller.
 *
 * This file manages the host controller, the host enumeration process and
 * the suspend/resume host requests.
 *
 * - Compiler:           IAR EWAVR32 and GNU GCC for AVR32
 * - Supported devices:  All AVR32 devices with a USB module can be used.
 * - AppNote:
 *
 * \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
 *
 */

#ifndef _USB_HOST_TASK_H_
#define _USB_HOST_TASK_H_


#include "conf_usb.h"

#if USB_HOST_FEATURE == DISABLED
  #error usb_host_task.h is #included although USB_HOST_FEATURE is disabled
#endif


//! @defgroup usb_host_task USB host task module
//! @{

//_____ I N C L U D E S ____________________________________________________

#include "compiler.h"
#include "usb_host_enum.h"


//_____ T Y P E S  _________________________________________________________

typedef void Pipe_handler(Status_t status, U16 nb_byte);

typedef struct
{
  Bool          enable;
  U16           nb_byte_to_process;
  U16           nb_byte_processed;
  U16           nb_byte_on_going;
  void         *ptr_buf;
  Pipe_handler *handler;
  Status_t      status;
  U8            timeout;
  U16           nak_timeout;
} S_pipe_int;


//_____ M A C R O S ________________________________________________________

#define PIPE_GOOD           0x00
#define PIPE_DATA_TOGGLE    0x01
#define PIPE_DATA_PID       0x02
#define PIPE_PID            0x04
#define PIPE_TIMEOUT        0x08
#define PIPE_CRC16          0x10
#define PIPE_STALL          0x20
#define PIPE_NAK_TIMEOUT    0x40
#define PIPE_DELAY_TIMEOUT  0x80

//! @brief Returns TRUE when device connected and correctly enumerated.
//! The host high-level application should test this before performing any applicative request.
#define Is_host_ready()                   (device_state == DEVICE_READY)

//! Check if host controller is in suspend mode
#define Is_host_suspended()               (device_state == DEVICE_WAIT_RESUME ||\
                                           device_state == DEVICE_SUSPENDED)

//! Check if there is an error
#define Is_host_error()                   (device_state == DEVICE_ERROR)

//! Check if there is a device addressed by the host
#define Is_host_addressed()               (device_state >= DEVICE_ADDRESSED)

//! Check if there is a device powered by the host
#define Is_host_powered()                 (device_state >= DEVICE_POWERED)

//! Check if there is a device attached to the host
#define Is_host_attached()                (device_state >= DEVICE_ATTACHED)

//! Should be called to make the host controller enter USB suspend mode
#define Host_request_suspend()            (device_state = DEVICE_SUSPENDED)

//! Should be called to request the host controller to resume the USB bus
#define Host_request_resume()             (request_resume = TRUE)

//! Private ack for resume software event
#define Host_ack_request_resume()         (request_resume = FALSE)

//! Private check for resume sequence
#define Is_host_request_resume()          (request_resume == TRUE)

//! @defgroup device_state_value Host controller states
//! Defines for device state coding
//! \image html usb_host_task.jpg "USB Host Task Overview"
//! @{
#define DEVICE_UNATTACHED         0
#define DEVICE_ATTACHED           1
#define DEVICE_POWERED            2
#define DEVICE_DEFAULT            3
#define DEVICE_ADDRESSED          4
#define DEVICE_CONFIGURED         5
#define DEVICE_READY              6

#define DEVICE_ERROR              7

#define DEVICE_SUSPENDED          8
#define DEVICE_WAIT_RESUME        9

#define Host_set_device_supported()       (Set_bits(device_status, 0x01))
#define Host_clear_device_supported()     (Clr_bits(device_status, 0x01))
#define Is_host_device_supported()        (Tst_bits(device_status, 0x01))

#define Host_set_device_ready()           (Set_bits(device_status, 0x02))
#define Host_clear_device_ready()         (Clr_bits(device_status, 0x02))
#define Is_host_device_ready()            (Tst_bits(device_status, 0x02))

#define Host_set_configured()             (Set_bits(device_status, 0x04))
#define Host_clear_configured()           (Clr_bits(device_status, 0x04))
#define Is_host_configured()              (Tst_bits(device_status, 0x04))

#define Host_clear_device_status()        (device_status = 0x00)
//! @}


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

//!
//! @brief This function initializes the USB host controller.
//!
//! This function enables the USB controller for host-mode operation.
//!
extern void usb_host_task_init(void);

//!
//! @brief Entry point of the USB host management
//!
//! The aim is to manage the target device connection and enumeration.
//! Depending on device_state, the function performs the required operations
//! to get the device enumerated and configured.
//! Once the device is operational, device_state is DEVICE_READY.
//! This state should be tested by the host task before sending any
//! applicative request to the device.
//! This function is called from the usb_task function depending on the USB
//! operating mode (device or host) currently engaged.
//!
#ifdef FREERTOS_USED
extern void usb_host_task(void *pvParameters);
#else
extern void usb_host_task(void);
#endif

//!
//! @brief This function sends nb_data bytes pointed to by ptr_buf on the specified pipe.
//!
//! @note This function activates the host SOF interrupt to detect time-outs.
//! The initial enable state of this interrupt will be restored.
//!
//! @param pipe
//! @param nb_data
//! @param ptr_buf
//!
//! @return Status_t: Pipe status
//!
extern Status_t host_send_data(U8 pipe, U16 nb_data, const void *ptr_buf);

//!
//! @brief This function receives nb_data bytes pointed to by ptr_buf on the specified pipe.
//!
//! *nb_data is updated with the final number of data bytes received.
//!
//! @note This function activates the host SOF interrupt to detect time-outs.
//! The initial enable state of this interrupt will be restored.
//!
//! @param pipe
//! @param nb_data
//! @param ptr_buf
//!
//! @return Status_t: Pipe status
//!
extern Status_t host_get_data(U8 pipe, U16 *nb_data, void *ptr_buf);

#if USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE

extern void reset_it_pipe_str(void);

extern Bool is_any_interrupt_pipe_active(void);

//!
//! @brief This function sends nb_data bytes pointed to by ptr_buf on the specified pipe.
//!
//! @param pipe
//! @param nb_data
//! @param ptr_buf
//! @param handler Call-back function pointer
//!
//! @return Bool: Status
//!
extern Bool host_send_data_interrupt(U8 pipe, U16 nb_data, const void *ptr_buf, Pipe_handler *handler);

//!
//! @brief This function receives nb_data bytes pointed to by ptr_buf on the specified pipe.
//!
//! *nb_data is updated with the final number of data bytes received.
//!
//! @param pipe
//! @param nb_data
//! @param ptr_buf
//! @param handler Call-back function pointer
//!
//! @return Bool: Status
//!
extern Bool host_get_data_interrupt(U8 pipe, U16 nb_data, void *ptr_buf, Pipe_handler *handler);

//!
//! @brief USB pipe interrupt subroutine
//!
//! @param pipe
//!
extern void usb_pipe_interrupt(U8 pipe);

#endif  // USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE

extern volatile U8 device_state;
extern volatile S_usb_setup_data usb_request;
extern U8 data_stage[SIZEOF_DATA_STAGE];
extern volatile U8 device_status;
extern volatile Bool request_resume;

//! @}


#endif  // _USB_HOST_TASK_H_