b34619a7dac14d7f32561e8e9da2601f38efb3b8
[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
46 #define USB_BASE_ADDR                   0x40005C00
47
48 #define USB_DM_PIN      (1 << 11)
49 #define USB_DP_PIN      (1 << 12)
50 #define USB_DISC_PIN    (1 << 11)
51
52 #define USB_EP0_MAX_SIZE        8
53 #define USB_RX_MAX_SIZE         64
54 #define USB_TX_MAX_SIZE         64
55
56 /* USB packet memory organization */
57 #define USB_PACKET_MEMORY_BASE          0x40006000
58 #define USB_PACKET_MEMORY_SIZE          512
59
60 /* Offset of the buffer descriptor table inside the packet memory */
61 #define USB_BDT_OFFSET \
62         ((USB_PACKET_MEMORY_SIZE - (sizeof(stm32_usb_bd_t) * ENP_MAX_NUMB)) & ~7)
63
64 #define addr2usbmem(Offset) \
65         (USB_PACKET_MEMORY_BASE + ((Offset << 1) & ~3) + (Offset & 1))
66
67 #define ReadEpDTB(Slot, Offset) \
68         (*((uint16_t *)(addr2usbmem((USB_BDT_OFFSET + \
69                                         (Slot) * sizeof(stm32_usb_bd_t) + \
70                                         (Offset))))))
71
72 #define WriteEpDTB(Slot, Offset, Data)  (ReadEpDTB(Slot, Offset) = Data)
73
74 #define AddrTxOffset    0
75 #define CountTxOffset   2
76 #define AddrRxOffset    4
77 #define CountRxOffset   6
78
79 #define ReadEpDTB_AddrRx(Slot)  ReadEpDTB(Slot,AddrRxOffset)
80 #define ReadEpDTB_CountRx(Slot) ReadEpDTB(Slot,CountRxOffset)
81 #define ReadEpDTB_AddrTx(Slot)  ReadEpDTB(Slot,AddrTxOffset)
82 #define ReadEpDTB_CountTx(Slot) ReadEpDTB(Slot,CountTxOffset)
83
84 #define WriteEpDTB_AddrRx(Slot,Data)  WriteEpDTB(Slot,AddrRxOffset,Data)
85 #define WriteEpDTB_CountRx(Slot,Data) WriteEpDTB(Slot,CountRxOffset,Data)
86 #define WriteEpDTB_AddrTx(Slot,Data)  WriteEpDTB(Slot,AddrTxOffset,Data)
87 #define WriteEpDTB_CountTx(Slot,Data) WriteEpDTB(Slot,CountTxOffset,Data)
88
89 #define USB_CTRL_RW_MASK          0x070F
90 #define USB_CTRL_CLEAR_ONLY_MASK  0x8080
91 #define USB_CTRL_TOGGLE_MASK      0x7070
92
93 /* CNTR register flags */
94 #define bmCTRM                      0x8000
95 #define bmPMAOVRM                   0x4000
96 #define bmERRM                      0x2000
97 #define bmWKUPM                     0x1000
98 #define bmSUSPM                     0x0800
99 #define bmRESETM                    0x0400
100 #define bmSOFM                      0x0200
101 #define bmESOFM                     0x0100
102
103 #define bmRESUME                    0x0010
104 #define bmFSUSP                     0x0008
105 #define bmLPMODE                    0x0004
106 #define bmPDWN                      0x0002
107 #define bmFRES                      0x0001
108
109 /* USB error codes */
110 enum stm32_usb_error
111 {
112         USB_OK = 0,
113         USB_INTR_ERROR,
114         USB_INVAL_ERROR,
115         USB_NODEV_ERROR,
116         USB_MEMORY_FULL,
117         USB_BUF_OVERFLOW,
118         USB_EP_STALLED,
119         USB_FATAL_ERROR,
120 };
121
122 /* STM32 USB endpoint types */
123 enum stm32_usb_ep_type
124 {
125         EP_BULK = 0,
126         EP_CTRL,
127         EP_ISO,
128         EP_INTERRUPT,
129
130         EP_TYPE_MAX
131 };
132
133 /* STM32 USB interrupt status register bits */
134 typedef union
135 {
136         uint32_t status;
137         struct {
138                 uint8_t EP_ID  : 4;
139                 uint8_t DIR    : 1;
140                 uint8_t        : 2;
141                 uint8_t SZDPR  : 1;
142                 uint8_t ESOF   : 1;
143                 uint8_t SOF    : 1;
144                 uint8_t RESET  : 1;
145                 uint8_t SUSP   : 1;
146                 uint8_t WKUP   : 1;
147                 uint8_t ERR    : 1;
148                 uint8_t PMAOVR : 1;
149                 uint8_t CTR    : 1;
150         };
151 } PACKED stm32_usb_irq_status_t;
152
153 /* Endpoint state */
154 typedef enum
155 {
156         EP_DISABLED = 0,
157         EP_STALL,
158         EP_NAK,
159         EP_VALID
160 } ep_state_t;
161
162 /* STM32 USB supported endpoints */
163 typedef enum
164 {
165         CTRL_ENP_OUT = 0, CTRL_ENP_IN,
166         ENP1_OUT, ENP1_IN,
167         ENP2_OUT, ENP2_IN,
168         ENP3_OUT, ENP3_IN,
169         ENP4_OUT, ENP4_IN,
170         ENP5_OUT, ENP5_IN,
171         ENP6_OUT, ENP6_IN,
172         ENP7_OUT, ENP7_IN,
173         ENP8_OUT, ENP8_IN,
174         ENP9_OUT, ENP9_IN,
175         ENP10_OUT, ENP10_IN,
176         ENP11_OUT, ENP11_IN,
177         ENP12_OUT, ENP12_IN,
178         ENP13_OUT, ENP13_IN,
179         ENP14_OUT, ENP14_IN,
180         ENP15_OUT, ENP15_IN,
181
182         ENP_MAX_NUMB
183 } stm32_usb_endpoint_t;
184
185 /* STM32 USB packet memory slot */
186 typedef struct pack_mem_slot_t
187 {
188         stm32_usb_endpoint_t ep_addr;
189         uint16_t Start;
190         uint16_t Size;
191         struct pack_mem_slot_t *next;
192 } pack_mem_slot_t;
193
194 /* STM32 USB buffer descriptor (packet memory) */
195 typedef struct
196 {
197         uint16_t AddrTx;
198         uint16_t CountTx;
199         uint16_t AddrRx;
200         uint16_t CountRx;
201 } PACKED stm32_usb_bd_t;
202
203 /* STM32 USB endpoint I/O status */
204 typedef enum
205 {
206         NOT_READY = 0,
207         NO_SERVICED,
208         BEGIN_SERVICED,
209         COMPLETE,
210         BUFFER_UNDERRUN,
211         BUFFER_OVERRUN,
212         SETUP_OVERWRITE,
213         STALLED,
214 } stm32_usb_io_status_t;
215
216 /* STM32 USB hardware endpoint descriptor */
217 typedef struct
218 {
219         reg32_t *hw;
220         uint8_t type;
221         void (*complete)(int);
222         ssize_t max_size;
223         ssize_t offset;
224         ssize_t size;
225         stm32_usb_io_status_t status;
226         union
227         {
228                 uint8_t *read_buffer;
229                 const uint8_t *write_buffer;
230         };
231         int32_t avail_data;
232         uint8_t flags;
233 } stm32_usb_ep_t;
234
235 /* STM32 USB hardware endpoint flags */
236 #define STM32_USB_EP_AVAIL_DATA         BV(0)
237 #define STM32_USB_EP_ZERO_PACKET        BV(1)
238 #define STM32_USB_EP_ZERO_POSSIBLE      BV(2)
239
240 #endif /* USB_STM32_H */