diff options
Diffstat (limited to 'lib/chibios/os/hal/ports/STM32/LLD/USBv1/stm32_usb.h')
-rw-r--r-- | lib/chibios/os/hal/ports/STM32/LLD/USBv1/stm32_usb.h | 266 |
1 files changed, 266 insertions, 0 deletions
diff --git a/lib/chibios/os/hal/ports/STM32/LLD/USBv1/stm32_usb.h b/lib/chibios/os/hal/ports/STM32/LLD/USBv1/stm32_usb.h new file mode 100644 index 000000000..bd9be355c --- /dev/null +++ b/lib/chibios/os/hal/ports/STM32/LLD/USBv1/stm32_usb.h | |||
@@ -0,0 +1,266 @@ | |||
1 | /* | ||
2 | ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio | ||
3 | |||
4 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | you may not use this file except in compliance with the License. | ||
6 | You may obtain a copy of the License at | ||
7 | |||
8 | http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | |||
10 | Unless required by applicable law or agreed to in writing, software | ||
11 | distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | See the License for the specific language governing permissions and | ||
14 | limitations under the License. | ||
15 | */ | ||
16 | |||
17 | /** | ||
18 | * @file USBv1/stm32_usb.h | ||
19 | * @brief STM32 USB registers layout header. | ||
20 | * @note This file requires definitions from the ST STM32 header files | ||
21 | * stm32f10x.h or stm32l1xx.h. | ||
22 | * | ||
23 | * @addtogroup USB | ||
24 | * @{ | ||
25 | */ | ||
26 | |||
27 | #ifndef STM32_USB_H | ||
28 | #define STM32_USB_H | ||
29 | |||
30 | /** | ||
31 | * @brief Number of the available endpoints. | ||
32 | * @details This value does not include the endpoint 0 which is always present. | ||
33 | */ | ||
34 | #define USB_ENDOPOINTS_NUMBER 7 | ||
35 | |||
36 | /** | ||
37 | * @brief Width of USB packet memory accesses. | ||
38 | */ | ||
39 | #if STM32_USB_ACCESS_SCHEME_2x16 | ||
40 | typedef uint16_t stm32_usb_pma_t; | ||
41 | #else | ||
42 | typedef uint32_t stm32_usb_pma_t; | ||
43 | #endif | ||
44 | |||
45 | /** | ||
46 | * @brief USB registers block. | ||
47 | */ | ||
48 | typedef struct { | ||
49 | /** | ||
50 | * @brief Endpoint registers. | ||
51 | */ | ||
52 | volatile uint32_t EPR[USB_ENDOPOINTS_NUMBER + 1]; | ||
53 | /* | ||
54 | * @brief Reserved space. | ||
55 | */ | ||
56 | volatile uint32_t _r20[8]; | ||
57 | /* | ||
58 | * @brief Control Register. | ||
59 | */ | ||
60 | volatile uint32_t CNTR; | ||
61 | /* | ||
62 | * @brief Interrupt Status Register. | ||
63 | */ | ||
64 | volatile uint32_t ISTR; | ||
65 | /* | ||
66 | * @brief Frame Number Register. | ||
67 | */ | ||
68 | volatile uint32_t FNR; | ||
69 | /* | ||
70 | * @brief Device Address Register. | ||
71 | */ | ||
72 | volatile uint32_t DADDR; | ||
73 | /* | ||
74 | * @brief Buffer Table Address. | ||
75 | */ | ||
76 | volatile uint32_t BTABLE; | ||
77 | /* | ||
78 | * @brief LPM Control and Status Register. | ||
79 | */ | ||
80 | volatile uint32_t LPMCSR; | ||
81 | #if STM32_USB_HAS_BCDR | ||
82 | /* | ||
83 | * @brief Battery Charging Detector | ||
84 | */ | ||
85 | volatile uint32_t BCDR; | ||
86 | #endif | ||
87 | } stm32_usb_t; | ||
88 | |||
89 | /** | ||
90 | * @brief USB descriptor registers block. | ||
91 | */ | ||
92 | typedef struct { | ||
93 | /** | ||
94 | * @brief TX buffer offset register. | ||
95 | */ | ||
96 | volatile stm32_usb_pma_t TXADDR0; | ||
97 | /** | ||
98 | * @brief TX counter register 0. | ||
99 | */ | ||
100 | volatile stm32_usb_pma_t TXCOUNT0; | ||
101 | /** | ||
102 | * @brief RX buffer offset register. | ||
103 | */ | ||
104 | volatile stm32_usb_pma_t RXADDR0; | ||
105 | /** | ||
106 | * @brief RX counter register 0. | ||
107 | */ | ||
108 | volatile stm32_usb_pma_t RXCOUNT0; | ||
109 | } stm32_usb_descriptor_t; | ||
110 | |||
111 | /** | ||
112 | * @name Register aliases | ||
113 | * @{ | ||
114 | */ | ||
115 | #define RXCOUNT1 TXCOUNT0 | ||
116 | #define TXCOUNT1 RXCOUNT0 | ||
117 | #define RXADDR1 TXADDR0 | ||
118 | #define TXADDR1 RXADDR0 | ||
119 | /** @} */ | ||
120 | |||
121 | /** | ||
122 | * @brief USB registers block numeric address. | ||
123 | */ | ||
124 | #if defined(USB_BASE) || defined(__DOXYGEN__) | ||
125 | #define STM32_USB_BASE USB_BASE | ||
126 | #else | ||
127 | #define STM32_USB_BASE (APB1PERIPH_BASE + 0x5C00) | ||
128 | #endif | ||
129 | |||
130 | /** | ||
131 | * @brief USB RAM numeric address. | ||
132 | */ | ||
133 | #if defined(USB_PMAADDR) || defined(__DOXYGEN__) | ||
134 | #define STM32_USBRAM_BASE USB_PMAADDR | ||
135 | #else | ||
136 | #define STM32_USBRAM_BASE (APB1PERIPH_BASE + 0x6000) | ||
137 | #endif | ||
138 | |||
139 | /** | ||
140 | * @brief Pointer to the USB registers block. | ||
141 | */ | ||
142 | #define STM32_USB ((stm32_usb_t *)STM32_USB_BASE) | ||
143 | |||
144 | /** | ||
145 | * @brief Pointer to the USB RAM. | ||
146 | */ | ||
147 | #define STM32_USBRAM ((stm32_usb_pma_t *)STM32_USBRAM_BASE) | ||
148 | |||
149 | /** | ||
150 | * @brief Mask of all the toggling bits in the EPR register. | ||
151 | */ | ||
152 | #define EPR_TOGGLE_MASK (EPR_STAT_TX_MASK | EPR_DTOG_TX | \ | ||
153 | EPR_STAT_RX_MASK | EPR_DTOG_RX | \ | ||
154 | EPR_SETUP) | ||
155 | |||
156 | #define EPR_EA_MASK 0x000F | ||
157 | #define EPR_STAT_TX_MASK 0x0030 | ||
158 | #define EPR_STAT_TX_DIS 0x0000 | ||
159 | #define EPR_STAT_TX_STALL 0x0010 | ||
160 | #define EPR_STAT_TX_NAK 0x0020 | ||
161 | #define EPR_STAT_TX_VALID 0x0030 | ||
162 | #define EPR_DTOG_TX 0x0040 | ||
163 | #define EPR_SWBUF_RX EPR_DTOG_TX | ||
164 | #define EPR_CTR_TX 0x0080 | ||
165 | #define EPR_EP_KIND 0x0100 | ||
166 | #define EPR_EP_DBL_BUF EPR_EP_KIND | ||
167 | #define EPR_EP_STATUS_OUT EPR_EP_KIND | ||
168 | #define EPR_EP_TYPE_MASK 0x0600 | ||
169 | #define EPR_EP_TYPE_BULK 0x0000 | ||
170 | #define EPR_EP_TYPE_CONTROL 0x0200 | ||
171 | #define EPR_EP_TYPE_ISO 0x0400 | ||
172 | #define EPR_EP_TYPE_INTERRUPT 0x0600 | ||
173 | #define EPR_SETUP 0x0800 | ||
174 | #define EPR_STAT_RX_MASK 0x3000 | ||
175 | #define EPR_STAT_RX_DIS 0x0000 | ||
176 | #define EPR_STAT_RX_STALL 0x1000 | ||
177 | #define EPR_STAT_RX_NAK 0x2000 | ||
178 | #define EPR_STAT_RX_VALID 0x3000 | ||
179 | #define EPR_DTOG_RX 0x4000 | ||
180 | #define EPR_SWBUF_TX EPR_DTOG_RX | ||
181 | #define EPR_CTR_RX 0x8000 | ||
182 | |||
183 | #define CNTR_FRES 0x0001 | ||
184 | #define CNTR_PDWN 0x0002 | ||
185 | #define CNTR_LP_MODE 0x0004 | ||
186 | #define CNTR_FSUSP 0x0008 | ||
187 | #define CNTR_RESUME 0x0010 | ||
188 | #define CNTR_ESOFM 0x0100 | ||
189 | #define CNTR_SOFM 0x0200 | ||
190 | #define CNTR_RESETM 0x0400 | ||
191 | #define CNTR_SUSPM 0x0800 | ||
192 | #define CNTR_WKUPM 0x1000 | ||
193 | #define CNTR_ERRM 0x2000 | ||
194 | #define CNTR_PMAOVRM 0x4000 | ||
195 | #define CNTR_CTRM 0x8000 | ||
196 | |||
197 | #define ISTR_EP_ID_MASK 0x000F | ||
198 | #define ISTR_DIR 0x0010 | ||
199 | #define ISTR_ESOF 0x0100 | ||
200 | #define ISTR_SOF 0x0200 | ||
201 | #define ISTR_RESET 0x0400 | ||
202 | #define ISTR_SUSP 0x0800 | ||
203 | #define ISTR_WKUP 0x1000 | ||
204 | #define ISTR_ERR 0x2000 | ||
205 | #define ISTR_PMAOVR 0x4000 | ||
206 | #define ISTR_CTR 0x8000 | ||
207 | |||
208 | #define FNR_FN_MASK 0x07FF | ||
209 | #define FNR_LSOF 0x1800 | ||
210 | #define FNR_LCK 0x2000 | ||
211 | #define FNR_RXDM 0x4000 | ||
212 | #define FNR_RXDP 0x8000 | ||
213 | |||
214 | #define DADDR_ADD_MASK 0x007F | ||
215 | #define DADDR_EF 0x0080 | ||
216 | |||
217 | #define RXCOUNT_COUNT_MASK 0x03FF | ||
218 | #define TXCOUNT_COUNT_MASK 0x03FF | ||
219 | |||
220 | #define EPR_CTR_MASK (EPR_CTR_TX | EPR_CTR_RX) | ||
221 | |||
222 | #define EPR_SET(ep, epr) \ | ||
223 | STM32_USB->EPR[ep] = ((epr) & ~EPR_TOGGLE_MASK) | EPR_CTR_MASK | ||
224 | |||
225 | #define EPR_TOGGLE(ep, epr) \ | ||
226 | STM32_USB->EPR[ep] = (STM32_USB->EPR[ep] ^ ((epr) & EPR_TOGGLE_MASK)) \ | ||
227 | | EPR_CTR_MASK | ||
228 | |||
229 | #define EPR_SET_STAT_RX(ep, epr) \ | ||
230 | STM32_USB->EPR[ep] = ((STM32_USB->EPR[ep] & \ | ||
231 | ~(EPR_TOGGLE_MASK & ~EPR_STAT_RX_MASK)) ^ \ | ||
232 | (epr)) | EPR_CTR_MASK | ||
233 | |||
234 | #define EPR_SET_STAT_TX(ep, epr) \ | ||
235 | STM32_USB->EPR[ep] = ((STM32_USB->EPR[ep] & \ | ||
236 | ~(EPR_TOGGLE_MASK & ~EPR_STAT_TX_MASK)) ^ \ | ||
237 | (epr)) | EPR_CTR_MASK | ||
238 | |||
239 | #define EPR_CLEAR_CTR_RX(ep) \ | ||
240 | STM32_USB->EPR[ep] = (STM32_USB->EPR[ep] & ~EPR_CTR_RX & ~EPR_TOGGLE_MASK)\ | ||
241 | | EPR_CTR_TX | ||
242 | |||
243 | #define EPR_CLEAR_CTR_TX(ep) \ | ||
244 | STM32_USB->EPR[ep] = (STM32_USB->EPR[ep] & ~EPR_CTR_TX & ~EPR_TOGGLE_MASK)\ | ||
245 | | EPR_CTR_RX | ||
246 | |||
247 | /** | ||
248 | * @brief Returns an endpoint descriptor pointer. | ||
249 | */ | ||
250 | #define USB_GET_DESCRIPTOR(ep) \ | ||
251 | ((stm32_usb_descriptor_t *)((uint32_t)STM32_USBRAM_BASE + \ | ||
252 | (uint32_t)STM32_USB->BTABLE + \ | ||
253 | (uint32_t)(ep) * \ | ||
254 | sizeof(stm32_usb_descriptor_t))) | ||
255 | |||
256 | /** | ||
257 | * @brief Converts from a PMA address to a physical address. | ||
258 | */ | ||
259 | #define USB_ADDR2PTR(addr) \ | ||
260 | ((stm32_usb_pma_t *)((addr) * \ | ||
261 | (sizeof(stm32_usb_pma_t) / 2) + \ | ||
262 | STM32_USBRAM_BASE)) | ||
263 | |||
264 | #endif /* STM32_USB_H */ | ||
265 | |||
266 | /** @} */ | ||