a4cbb6b59d5a35ef68d807ed3e2db2452315d59f
[bertos.git] / bertos / cpu / cortex-m3 / drv / usb_stm32.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \author Andrea Righi <arighi@develer.com>
34  *
35  * \brief STM32: USB full-speed device driver
36  *
37  * Low-level USB device driver for the STM32 architecture.
38  */
39
40 #ifndef USB_STM32_H
41 #define USB_STM32_H
42
43 #include <cfg/compiler.h>
44 #include <drv/usb.h>
45 #include <drv/usb_endpoint.h>
46
47 #define USB_BASE_ADDR                   0x40005C00
48
49 #define USB_DM_PIN      (1 << 11)
50 #define USB_DP_PIN      (1 << 12)
51 #define USB_DISC_PIN    (1 << 11)
52
53 #define USB_EP0_MAX_SIZE        8
54 #define USB_XFER_MAX_SIZE       64
55
56 #define EP_MAX_SLOTS    USB_EP_MAX
57 #define EP_MAX_NUM      (EP_MAX_SLOTS << 1)
58
59 /* USB packet memory organization */
60 #define USB_PACKET_MEMORY_BASE          0x40006000
61 #define USB_PACKET_MEMORY_SIZE          512
62
63 /* Offset of the buffer descriptor table inside the packet memory */
64 #define USB_BDT_OFFSET \
65         ((USB_PACKET_MEMORY_SIZE - (sizeof(stm32_UsbBd) * EP_MAX_NUM)) & ~7)
66
67 #define USB_MEM_ADDR(offset) \
68         (USB_PACKET_MEMORY_BASE + ((offset << 1) & ~3) + (offset & 1))
69
70 #define EP_DTB_READ(slot, offset) \
71         (*((uint16_t *)(USB_MEM_ADDR((USB_BDT_OFFSET + \
72                                         (slot) * sizeof(stm32_UsbBd) + \
73                                         (offset))))))
74
75 #define EP_DTB_WRITE(slot, offset, data)  (EP_DTB_READ(slot, offset) = data)
76
77 #define ADDR_TX_OFFSET  offsetof(stm32_UsbBd, AddrTx)
78 #define COUNT_TX_OFFSET offsetof(stm32_UsbBd, CountTx)
79 #define ADDR_RX_OFFSET  offsetof(stm32_UsbBd, AddrRx)
80 #define COUNT_RX_OFFSET offsetof(stm32_UsbBd, CountRx)
81
82 #define USB_CTRL_RW_MASK          0x070F
83 #define USB_CTRL_CLEAR_ONLY_MASK  0x8080
84 #define USB_CTRL_TOGGLE_MASK      0x7070
85
86 /* CNTR register flags */
87 #define bmCTRM                      0x8000
88 #define bmPMAOVRM                   0x4000
89 #define bmERRM                      0x2000
90 #define bmWKUPM                     0x1000
91 #define bmSUSPM                     0x0800
92 #define bmRESETM                    0x0400
93 #define bmSOFM                      0x0200
94 #define bmESOFM                     0x0100
95
96 #define bmRESUME                    0x0010
97 #define bmFSUSP                     0x0008
98 #define bmLPMODE                    0x0004
99 #define bmPDWN                      0x0002
100 #define bmFRES                      0x0001
101
102 /* USB error codes */
103 enum stm32_usb_error
104 {
105         USB_OK = 0,
106         USB_INTR_ERROR,
107         USB_INVAL_ERROR,
108         USB_NODEV_ERROR,
109         USB_MEMORY_FULL,
110         USB_BUF_OVERFLOW,
111         USB_EP_STALLED,
112         USB_FATAL_ERROR,
113 };
114
115 /* STM32 USB endpoint types */
116 enum stm32_UsbEpype
117 {
118         EP_BULK = 0,
119         EP_CTRL,
120         EP_ISO,
121         EP_INTERRUPT,
122
123         EP_TYPE_MAX
124 };
125
126 /* STM32 USB interrupt status register bits */
127 typedef union
128 {
129         uint32_t status;
130         struct {
131                 uint8_t EP_ID  : 4;
132                 uint8_t DIR    : 1;
133                 uint8_t        : 2;
134                 uint8_t SZDPR  : 1;
135                 uint8_t ESOF   : 1;
136                 uint8_t SOF    : 1;
137                 uint8_t RESET  : 1;
138                 uint8_t SUSP   : 1;
139                 uint8_t WKUP   : 1;
140                 uint8_t ERR    : 1;
141                 uint8_t PMAOVR : 1;
142                 uint8_t CTR    : 1;
143         };
144 } PACKED stm32_usb_irq_status_t;
145
146 /* Endpoint state */
147 typedef enum
148 {
149         EP_DISABLED = 0,
150         EP_STALL,
151         EP_NAK,
152         EP_VALID
153 } stm32_UsbEpState;
154
155 /* STM32 USB supported endpoints */
156 typedef enum stm32_UsbEP
157 {
158         CTRL_ENP_OUT = 0, CTRL_ENP_IN,
159         ENP1_OUT, ENP1_IN,
160         ENP2_OUT, ENP2_IN,
161         ENP3_OUT, ENP3_IN,
162         ENP4_OUT, ENP4_IN,
163         ENP5_OUT, ENP5_IN,
164         ENP6_OUT, ENP6_IN,
165         ENP7_OUT, ENP7_IN,
166         ENP8_OUT, ENP8_IN,
167         ENP9_OUT, ENP9_IN,
168         ENP10_OUT, ENP10_IN,
169         ENP11_OUT, ENP11_IN,
170         ENP12_OUT, ENP12_IN,
171         ENP13_OUT, ENP13_IN,
172         ENP14_OUT, ENP14_IN,
173         ENP15_OUT, ENP15_IN,
174
175         EP_MAX_HW_NUM
176 } stm32_UsbEP;
177
178 /* STM32 USB packet memory slot */
179 typedef struct stm32_UsbMemSlot
180 {
181         stm32_UsbEP ep_addr;
182         uint16_t Start;
183         uint16_t Size;
184         struct stm32_UsbMemSlot *next;
185 } stm32_UsbMemSlot;
186
187 /* STM32 USB buffer descriptor (packet memory) */
188 typedef struct stm32_UsbBd
189 {
190         uint16_t AddrTx;
191         uint16_t CountTx;
192         uint16_t AddrRx;
193         uint16_t CountRx;
194 } PACKED stm32_UsbBd;
195
196 /* STM32 USB endpoint I/O status */
197 typedef enum stm32_UsbIoStatus
198 {
199         NOT_READY = 0,
200         NO_SERVICED,
201         BEGIN_SERVICED,
202         COMPLETE,
203         BUFFER_UNDERRUN,
204         BUFFER_OVERRUN,
205         SETUP_OVERWRITE,
206         STALLED,
207 } stm32_UsbIoStatus;
208
209 /* STM32 USB hardware endpoint descriptor */
210 typedef struct stm32_UsbEp
211 {
212         reg32_t *hw;
213         uint8_t type;
214         void (*complete)(int);
215         ssize_t max_size;
216         ssize_t offset;
217         ssize_t size;
218         stm32_UsbIoStatus status;
219         union
220         {
221                 uint8_t *read_buffer;
222                 const uint8_t *write_buffer;
223         };
224         int32_t avail_data;
225         uint8_t flags;
226 } stm32_UsbEp;
227
228 /* STM32 USB hardware endpoint flags */
229 #define STM32_USB_EP_AVAIL_DATA         BV(0)
230 #define STM32_USB_EP_ZERO_PACKET        BV(1)
231 #define STM32_USB_EP_ZERO_POSSIBLE      BV(2)
232
233 #endif /* USB_STM32_H */