4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
33 * \author Andrea Righi <arighi@develer.com>
35 * \brief STM32: USB full-speed device driver
37 * Low-level USB device driver for the STM32 architecture.
43 #include <cfg/compiler.h>
46 #define USB_BASE_ADDR 0x40005C00
48 #define USB_DM_PIN (1 << 11)
49 #define USB_DP_PIN (1 << 12)
50 #define USB_DISC_PIN (1 << 11)
52 #define USB_EP0_MAX_SIZE 8
53 #define USB_RX_MAX_SIZE 64
54 #define USB_TX_MAX_SIZE 64
56 /* USB packet memory organization */
57 #define USB_PACKET_MEMORY_BASE 0x40006000
58 #define USB_PACKET_MEMORY_SIZE 512
60 /* Offset of the buffer descriptor table inside the packet memory */
61 #define USB_BDT_OFFSET \
62 ((USB_PACKET_MEMORY_SIZE - (sizeof(stm32_UsbBd) * ENP_MAX_NUMB)) & ~7)
64 #define USB_MEM_ADDR(offset) \
65 (USB_PACKET_MEMORY_BASE + ((offset << 1) & ~3) + (offset & 1))
67 #define EP_DTB_READ(slot, offset) \
68 (*((uint16_t *)(USB_MEM_ADDR((USB_BDT_OFFSET + \
69 (slot) * sizeof(stm32_UsbBd) + \
72 #define EP_DTB_WRITE(slot, offset, data) (EP_DTB_READ(slot, offset) = data)
74 #define ADDR_TX_OFFSET offsetof(stm32_UsbBd, AddrTx)
75 #define COUNT_TX_OFFSET offsetof(stm32_UsbBd, CountTx)
76 #define ADDR_RX_OFFSET offsetof(stm32_UsbBd, AddrRx)
77 #define COUNT_RX_OFFSET offsetof(stm32_UsbBd, CountRx)
79 #define USB_CTRL_RW_MASK 0x070F
80 #define USB_CTRL_CLEAR_ONLY_MASK 0x8080
81 #define USB_CTRL_TOGGLE_MASK 0x7070
83 /* CNTR register flags */
85 #define bmPMAOVRM 0x4000
87 #define bmWKUPM 0x1000
88 #define bmSUSPM 0x0800
89 #define bmRESETM 0x0400
91 #define bmESOFM 0x0100
93 #define bmRESUME 0x0010
94 #define bmFSUSP 0x0008
95 #define bmLPMODE 0x0004
112 /* STM32 USB endpoint types */
123 /* STM32 USB interrupt status register bits */
141 } PACKED stm32_usb_irq_status_t;
152 /* STM32 USB supported endpoints */
153 typedef enum stm32_UsbEP
155 CTRL_ENP_OUT = 0, CTRL_ENP_IN,
175 /* STM32 USB packet memory slot */
176 typedef struct stm32_UsbMemSlot
181 struct stm32_UsbMemSlot *next;
184 /* STM32 USB buffer descriptor (packet memory) */
185 typedef struct stm32_UsbBd
191 } PACKED stm32_UsbBd;
193 /* STM32 USB endpoint I/O status */
194 typedef enum stm32_UsbIoStatus
206 /* STM32 USB hardware endpoint descriptor */
207 typedef struct stm32_UsbEp
211 void (*complete)(int);
215 stm32_UsbIoStatus status;
218 uint8_t *read_buffer;
219 const uint8_t *write_buffer;
225 /* STM32 USB hardware endpoint flags */
226 #define STM32_USB_EP_AVAIL_DATA BV(0)
227 #define STM32_USB_EP_ZERO_PACKET BV(1)
228 #define STM32_USB_EP_ZERO_POSSIBLE BV(2)
230 #endif /* USB_STM32_H */