version 0.1 written by Akiba, taken from here:
[chibi.git] / freakusb / usb / freakusb.h
blobb8d736c79dcb5477dc33b0a6376f94da1cd69c5c
1 /*******************************************************************
2 Copyright (C) 2009 FreakLabs
3 All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 3. Neither the name of the the copyright holder nor the names of its contributors
15 may be used to endorse or promote products derived from this software
16 without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED BY THE THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 SUCH DAMAGE.
30 Originally written by Christopher Wang aka Akiba.
31 Please post support questions to the FreakLabs forum.
33 *******************************************************************/
34 /*!
35 \file freakusb.h
36 \ingroup usb
38 /*******************************************************************/
39 #ifndef FREAKUSB_H
40 #define FREAKUSB_H
42 #include <string.h>
43 #include <stdio.h>
44 #include "types.h"
46 // class specific
47 #include "cdc.h"
49 // hw specific
50 #include "hw.h"
52 // config
53 #define FLASHMEM PROGMEM ///< AVR Specific for storing data in flash
54 #define MAX_RX_ENTRIES 5
55 #define MAX_EPS 7 ///< AVR Specific for AT90USB USB controller
56 #define POWER_SRC BUS_POWERED
57 #define MAX_REQUEST_SIZE 32
58 #define CTRL_IN_REQ_SZ 8
60 // eps
61 #define EP_CTRL 0
62 #define EP_1 1
63 #define EP_2 2
64 #define EP_3 3
65 #define EP_4 4
66 #define EP_5 5
67 #define EP_6 6
68 #define EP_7 7
70 // packet sizes
71 #define PKTSZ_8 0
72 #define PKTSZ_16 1
73 #define PKTSZ_32 2
74 #define PKTSZ_64 3
75 #define PKTSZ_128 4
76 #define PKTSZ_256 5
77 #define PKTSZ_512 6
78 #define PKTSZ_1024 7
80 // xfer type
81 #define CONTROL 0
82 #define ISOCHRONOUS 1
83 #define BULK 2
84 #define INTP 3
86 // dir
87 #define DIR_OUT 0
88 #define DIR_IN 1
90 // token types
91 #define PKTTYP_SETUP 0
92 #define PKTTYP_IN 1
93 #define PKTTYP_OUT 2
95 // get requests
96 #define GET_STATUS 0x00
97 #define GET_DESCRIPTOR 0x06
98 #define GET_CONFIGURATION 0x08
99 #define GET_INTERFACE 0x0A
100 #define SYNC_FRAME 0x0C
102 // set requests
103 #define SET_FEATURE 0x03
104 #define SET_ADDRESS 0x05
105 #define SET_DESCRIPTOR 0x07
106 #define SET_CONFIGURATION 0x09
107 #define SET_INTERFACE 0x0B
108 #define CLEAR_FEATURE 0x01
110 // descriptor types
111 #define DEV_DESCR 1
112 #define CFG_DESCR 2
113 #define STR_DESCR 3
114 #define INTF_DESCR 4
115 #define EP_DESCR 5
116 #define DEV_QUAL_DESCR 6
118 // request types
119 #define HOST_TO_DEVICE 0x00
120 #define DEVICE_TO_HOST 0x80
121 #define TYPE_STD 0x00
122 #define TYPE_CLASS 0x20
123 #define TYPE_VENDOR 0x40
124 #define RECIPIENT_DEV 0x00
125 #define RECIPIENT_INTF 0x01
126 #define RECIPIENT_EP 0x02
128 // get status requests
129 #define GET_DEVICE_STATUS 0x80
130 #define GET_INTF_STATUS 0x81
131 #define GET_EP_STATUS 0x82
133 // string indexes
134 #define LANG_DESC_IDX 0
135 #define MANUF_DESC_IDX 1
136 #define PROD_DESC_IDX 2
137 #define SERIAL_DESC_IDX 3
139 // control features
140 #define ENDPOINT_HALT 0
141 #define REMOTE_WAKEUP 1
143 // flags defines
144 #define RX_DATA_AVAIL 0
145 #define TX_DATA_AVAIL 1
146 #define SETUP_DATA_AVAIL 2
147 #define REMOTE_WAKEUP_ENB 3
148 #define ENUMERATED 4
150 // power sources
151 #define SELF_POWERED 1
152 #define BUS_POWERED 0
154 // get desc request
155 typedef struct req_t
157 U8 type;
158 U8 req;
159 U16 val;
160 U16 idx;
161 U16 len;
162 U8 data[];
163 } req_t;
165 // buffer used for circular fifo
166 typedef struct _usb_buffer_t
168 U8 ep_dir;
169 volatile U8 len; // this may change in an interrupt and the value would be required
170 volatile U8 wr_ptr;
171 volatile U8 rd_ptr;
172 U8 buf[MAX_BUF_SZ];
173 } usb_buffer_t;
175 // protocol control block
176 typedef struct _usb_pcb_t
178 bool connected;
179 volatile U8 flags;
180 U8 intp_flags;
181 U8 cfg_num;
182 U8 ep_stall;
183 U8 test;
184 usb_buffer_t fifo[NUM_EPS];
185 void (*class_init)();
186 void (*class_req_handler)(req_t *req);
187 void (*class_rx_handler)();
188 } usb_pcb_t;
190 // prototypes
192 // usb.c
193 void usb_init();
194 usb_pcb_t *usb_pcb_get();
195 void usb_reg_class_drvr(void (*class_cfg_init)(),
196 void (*class_req_handler)(),
197 void (*class_rx_handler)());
198 void usb_poll();
200 // req.c
201 void ctrl_handler();
203 // ep.c
204 void ep_init();
205 void ep_select(U8 ep_num);
206 void ep_write_from_flash(U8 ep_num, U8 *data, U8 len);
207 void ep_write(U8 ep_num);
208 void ep_write_ctrl(U8 *data, U8 len, bool read_from_flash);
209 void ep_read(U8 ep_num);
210 void ep_set_addr(U8 addr);
211 U8 ep_intp_get_num();
212 U8 ep_intp_get_src();
213 void ep_set_stall(U8 ep_num);
214 void ep_clear_stall(U8 ep_num);
215 void ep_reset_toggle(U8 ep_num);
216 void ep_send_zlp(U8 ep_num);
217 void ep_config(U8 ep_num, U8 type, U8 dir, U8 size);
219 // desc.c
220 U8 *desc_dev_get();
221 U8 desc_dev_get_len();
222 U8 *desc_cfg_get();
223 U8 desc_cfg_get_len();
224 U8 *desc_dev_qual_get();
225 U8 desc_dev_qual_get_len();
226 U8 *desc_str_get(U8 index);
227 U8 desc_str_get_len(U8 index);
229 // buf
230 void usb_buf_init(U8 ep_num, U8 ep_dir);
231 U8 usb_buf_read(U8 ep_num);
232 void usb_buf_write(U8 ep_num, U8 data);
233 void usb_buf_clear_fifo(U8 ep_num);
234 U8 usb_buf_data_pending(U8 ep_dir);
236 // misc.c
237 //void dbg_led_init();
238 //void dbg_led_enb(U8 led_num);
239 //void dbg_led_dis(U8 led_num);
240 //void dbg_joystick_init();
241 //U8 dbg_joystick_get_status();
242 //void dbg_hwb_init();
243 //bool dbg_hwb_get_status();
244 #endif