2011-02-10 06:58:51 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the libopencm3 project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Gareth McMullin <gareth@blacksphere.co.nz>
|
|
|
|
*
|
2012-03-02 10:23:11 +00:00
|
|
|
* This library is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
2011-02-10 06:58:51 +00:00
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2012-03-02 10:23:11 +00:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2011-02-10 06:58:51 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2012-03-02 10:23:11 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2011-02-10 06:58:51 +00:00
|
|
|
*
|
2012-03-02 10:23:11 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2011-02-10 06:58:51 +00:00
|
|
|
*/
|
|
|
|
|
2011-11-16 21:19:10 +00:00
|
|
|
#include <string.h>
|
2011-02-10 06:58:51 +00:00
|
|
|
#include <libopencm3/cm3/common.h>
|
|
|
|
#include <libopencm3/stm32/tools.h>
|
|
|
|
#include <libopencm3/stm32/otg_fs.h>
|
|
|
|
#include <libopencm3/usb/usbd.h>
|
|
|
|
#include "usb_private.h"
|
2012-08-17 01:50:38 +00:00
|
|
|
#include "usb_fx07_common.h"
|
2011-02-10 06:58:51 +00:00
|
|
|
|
2011-11-16 21:19:10 +00:00
|
|
|
/* Receive FIFO size in 32-bit words. */
|
|
|
|
#define RX_FIFO_SIZE 128
|
2011-03-15 07:33:02 +00:00
|
|
|
|
2012-08-17 01:50:38 +00:00
|
|
|
static usbd_device *stm32f107_usbd_init(void);
|
2011-03-29 18:17:02 +00:00
|
|
|
|
2012-08-17 01:50:38 +00:00
|
|
|
static struct _usbd_device usbd_dev;
|
2011-02-19 23:28:23 +00:00
|
|
|
|
2011-02-10 06:58:51 +00:00
|
|
|
const struct _usbd_driver stm32f107_usb_driver = {
|
|
|
|
.init = stm32f107_usbd_init,
|
2012-08-17 01:50:38 +00:00
|
|
|
.set_address = stm32fx07_set_address,
|
|
|
|
.ep_setup = stm32fx07_ep_setup,
|
|
|
|
.ep_reset = stm32fx07_endpoints_reset,
|
|
|
|
.ep_stall_set = stm32fx07_ep_stall_set,
|
|
|
|
.ep_stall_get = stm32fx07_ep_stall_get,
|
|
|
|
.ep_nak_set = stm32fx07_ep_nak_set,
|
|
|
|
.ep_write_packet = stm32fx07_ep_write_packet,
|
|
|
|
.ep_read_packet = stm32fx07_ep_read_packet,
|
|
|
|
.poll = stm32fx07_poll,
|
|
|
|
.disconnect = stm32fx07_disconnect,
|
|
|
|
.base_address = USB_OTG_FS_BASE,
|
|
|
|
.set_address_before_status = 1,
|
|
|
|
.rx_fifo_size = RX_FIFO_SIZE,
|
2011-02-10 06:58:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Initialize the USB device controller hardware of the STM32. */
|
2012-08-17 01:50:38 +00:00
|
|
|
static usbd_device *stm32f107_usbd_init(void)
|
2011-02-10 06:58:51 +00:00
|
|
|
{
|
2011-03-14 07:31:32 +00:00
|
|
|
OTG_FS_GINTSTS = OTG_FS_GINTSTS_MMIS;
|
|
|
|
|
2011-02-19 23:28:23 +00:00
|
|
|
OTG_FS_GUSBCFG |= OTG_FS_GUSBCFG_PHYSEL;
|
2011-11-16 21:19:10 +00:00
|
|
|
/* Enable VBUS sensing in device mode and power down the PHY. */
|
2011-02-19 23:28:23 +00:00
|
|
|
OTG_FS_GCCFG |= OTG_FS_GCCFG_VBUSBSEN | OTG_FS_GCCFG_PWRDWN;
|
|
|
|
|
2011-11-16 21:19:10 +00:00
|
|
|
/* Wait for AHB idle. */
|
|
|
|
while (!(OTG_FS_GRSTCTL & OTG_FS_GRSTCTL_AHBIDL)) ;
|
|
|
|
/* Do core soft reset. */
|
2011-02-19 23:28:23 +00:00
|
|
|
OTG_FS_GRSTCTL |= OTG_FS_GRSTCTL_CSRST;
|
2011-11-16 21:19:10 +00:00
|
|
|
while (OTG_FS_GRSTCTL & OTG_FS_GRSTCTL_CSRST) ;
|
2011-02-19 23:28:23 +00:00
|
|
|
|
|
|
|
/* Force peripheral only mode. */
|
2011-10-30 07:19:29 +00:00
|
|
|
OTG_FS_GUSBCFG |= OTG_FS_GUSBCFG_FDMOD | OTG_FS_GUSBCFG_TRDT_MASK;
|
2011-02-19 23:28:23 +00:00
|
|
|
|
2011-11-16 21:19:10 +00:00
|
|
|
/* Full speed device. */
|
2011-02-19 23:28:23 +00:00
|
|
|
OTG_FS_DCFG |= OTG_FS_DCFG_DSPD;
|
|
|
|
|
2011-11-16 21:19:10 +00:00
|
|
|
/* Restart the PHY clock. */
|
2011-02-19 23:28:23 +00:00
|
|
|
OTG_FS_PCGCCTL = 0;
|
|
|
|
|
2012-08-17 01:50:38 +00:00
|
|
|
OTG_FS_GRXFSIZ = stm32f107_usb_driver.rx_fifo_size;
|
|
|
|
usbd_dev.fifo_mem_top = stm32f107_usb_driver.rx_fifo_size;
|
2011-02-19 23:28:23 +00:00
|
|
|
|
2011-11-16 21:19:10 +00:00
|
|
|
/* Unmask interrupts for TX and RX. */
|
2011-04-04 05:39:05 +00:00
|
|
|
OTG_FS_GAHBCFG |= OTG_FS_GAHBCFG_GINT;
|
|
|
|
OTG_FS_GINTMSK = OTG_FS_GINTMSK_ENUMDNEM |
|
2011-11-16 21:19:10 +00:00
|
|
|
OTG_FS_GINTMSK_RXFLVLM |
|
|
|
|
OTG_FS_GINTMSK_IEPINT |
|
|
|
|
OTG_FS_GINTMSK_USBSUSPM |
|
|
|
|
OTG_FS_GINTMSK_WUIM |
|
|
|
|
OTG_FS_GINTMSK_SOFM;
|
2011-04-04 05:39:05 +00:00
|
|
|
OTG_FS_DAINTMSK = 0xF;
|
|
|
|
OTG_FS_DIEPMSK = OTG_FS_DIEPMSK_XFRCM;
|
2011-03-29 18:17:02 +00:00
|
|
|
|
2012-08-17 01:50:38 +00:00
|
|
|
return &usbd_dev;
|
2012-03-02 10:23:11 +00:00
|
|
|
}
|