libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
usb_efm32hg.c
Go to the documentation of this file.
1/** @addtogroup usb_file USB peripheral API
2 * @ingroup peripheral_apis
3 *
4 * @brief USB Peripheral for Happy Gecko
5 *
6 * The Happy Gecko uses the "standard" usb_dwc_otg core.
7 *
8 * @sa usb_defines
9 * @copyright See @ref lgpl_license
10 */
11/*
12 * This file is part of the libopencm3 project.
13 *
14 * Copyright (C) 2011 Gareth McMullin <gareth@blacksphere.co.nz>
15 * Copyright (C) 2018 Seb Holzapfel <schnommus@gmail.com>
16 *
17 * This library is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU Lesser General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
21 *
22 * This library is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public License
28 * along with this library. If not, see <http://www.gnu.org/licenses/>.
29 */
30
31#include <string.h>
36#include <libopencm3/usb/usbd.h>
38#include "usb_private.h"
39#include "usb_dwc_common.h"
40
41/**@{*/
42
43/* Receive FIFO size in 32-bit words. */
44#define RX_FIFO_SIZE 256
45
46/* FIXME: EFM32HG has 6 bidirectional endpoints.
47 * problem is "uint32_t doeptsiz[4];" in usb_private.h */
48
49#define ENDPOINT_COUNT 4
50
51static struct _usbd_device _usbd_dev;
52
53/** Initialize the USB device controller hardware of the EFM32HG. */
55{
56 /* Enable peripheral clocks required for USB */
60
61 /* Select LFRCO as LFCCLK clock */
63
64 /* Enable the USBLE peripheral clock (sits on LFCCLK) */
66
67 /* Calibrate USB based on communications */
69
70 /* Enable USHFRCO Clock Recovery mode. */
72
73 /* Select USHFRCO as clock source for USB */
76
77 /* Set up the USB clock source */
80
81 /* Turn off all Low Energy Mode (LEM) features. */
82 USB_CTRL = 0;
83
84 /* Initialize USB core */
85 USB_ROUTE = USB_ROUTE_PHYPEN; /* Enable PHY pins. */
86
87 /* Wait for AHB idle. */
89 /* Do core soft reset. */
92
93 /* Explicitly enable DP pullup (not all cores do this by default) */
94 OTG_FS_DCTL &= ~OTG_DCTL_SDIS;
95
96 /* Force peripheral only mode. */
98
100
101 /* Full speed device. */
103
104 /* Restart the PHY clock. */
105 OTG_FS_PCGCCTL = 0;
106
107 OTG_FS_GRXFSIZ = efm32hg_usb_driver.rx_fifo_size;
108 _usbd_dev.fifo_mem_top = efm32hg_usb_driver.rx_fifo_size;
109
110 /* Unmask interrupts for TX and RX. */
117 OTG_FS_DAINTMSK = 0xF;
119
120 return &_usbd_dev;
121}
122
123const struct _usbd_driver efm32hg_usb_driver = {
124 .init = efm32hg_usbd_init,
125 .set_address = dwc_set_address,
126 .ep_setup = dwc_ep_setup,
127 .ep_reset = dwc_endpoints_reset,
128 .ep_stall_set = dwc_ep_stall_set,
129 .ep_stall_get = dwc_ep_stall_get,
130 .ep_nak_set = dwc_ep_nak_set,
131 .ep_write_packet = dwc_ep_write_packet,
132 .ep_read_packet = dwc_ep_read_packet,
133 .poll = dwc_poll,
134 .disconnect = dwc_disconnect,
135 .base_address = USB_OTG_FS_BASE,
136 .set_address_before_status = 1,
137 .rx_fifo_size = RX_FIFO_SIZE,
138};
139
140/**@}*/
#define USB_OTG_FS_BASE
Dispatcher for the base address definitions, depending on the particular Gecko family.
void cmu_wait_for_usbclk_selected(enum cmu_osc osc)
Wait while USBCLK is not selected.
Definition: cmu.c:296
void cmu_periph_clock_enable(enum cmu_periph_clken periph)
Enable Peripheral Clock in running mode.
Definition: cmu.c:68
#define CMU_USBCRCTRL
Definition: hg/cmu.h:64
void cmu_set_usbclk_source(enum cmu_osc osc)
Set USBCLK clock source.
Definition: cmu.c:274
#define CMU_LFCLKSEL_LFC_LFRCO
Definition: hg/cmu.h:378
#define CMU_LFCLKSEL
Definition: hg/cmu.h:46
#define CMU_USBCRCTRL_EN
Definition: hg/cmu.h:580
void cmu_osc_on(enum cmu_osc osc)
Turn on Oscillator.
Definition: cmu.c:92
#define CMU_USHFRCOCONF
Definition: hg/cmu.h:67
void cmu_wait_for_osc_ready(enum cmu_osc osc)
Wait while oscillator is not ready.
Definition: cmu.c:189
#define CMU_USHFRCOCONF_BAND_48MHZ
Definition: hg/cmu.h:600
@ CMU_LE
Definition: hg/cmu.h:635
@ CMU_USBLE
Definition: hg/cmu.h:610
@ CMU_USBC
Definition: hg/cmu.h:634
@ CMU_USB
Definition: hg/cmu.h:633
@ USHFRCO
Internal, 48MHz.
Definition: hg/cmu.h:645
#define USB_ROUTE_PHYPEN
Definition: hg/usb.h:68
#define USB_CTRL
Definition: hg/usb.h:36
#define USB_ROUTE
Definition: hg/usb.h:42
struct _usbd_device usbd_device
Definition: usbd.h:53
static struct _usbd_device _usbd_dev
Definition: usb_efm32hg.c:51
static usbd_device * efm32hg_usbd_init(void)
Initialize the USB device controller hardware of the EFM32HG.
Definition: usb_efm32hg.c:54
const struct _usbd_driver efm32hg_usb_driver
Definition: usb_efm32hg.c:123
#define RX_FIFO_SIZE
Definition: usb_efm32hg.c:44
#define OTG_GUSBCFG_FDMOD
Definition: otg_common.h:125
#define OTG_DIEPMSK_XFRCM
Definition: otg_common.h:270
#define OTG_GINTMSK_IEPINT
Definition: otg_common.h:188
#define OTG_GRSTCTL_AHBIDL
Definition: otg_common.h:130
#define OTG_GINTMSK_RXFLVLM
Definition: otg_common.h:177
#define OTG_GRSTCTL_CSRST
Definition: otg_common.h:139
#define OTG_GAHBCFG_GINT
Definition: otg_common.h:114
#define OTG_GINTMSK_WUIM
Definition: otg_common.h:199
#define OTG_DCFG_DSPD
Definition: otg_common.h:251
#define OTG_GINTSTS_MMIS
Definition: otg_common.h:170
#define OTG_GINTMSK_ENUMDNEM
Definition: otg_common.h:184
#define OTG_GUSBCFG_TRDT_MASK
Definition: otg_common.h:122
#define OTG_GINTMSK_USBSUSPM
Definition: otg_common.h:182
#define OTG_FS_GINTSTS
Definition: otg_fs.h:47
#define OTG_FS_GRXFSIZ
Definition: otg_fs.h:51
#define OTG_FS_PCGCCTL
Definition: otg_fs.h:97
#define OTG_FS_GINTMSK
Definition: otg_fs.h:48
#define OTG_FS_GAHBCFG
Definition: otg_fs.h:44
#define OTG_FS_GUSBCFG
Definition: otg_fs.h:45
#define OTG_FS_DCFG
Definition: otg_fs.h:74
#define OTG_FS_DCTL
Definition: otg_fs.h:75
#define OTG_FS_DAINTMSK
Definition: otg_fs.h:80
#define OTG_FS_DIEPMSK
Definition: otg_fs.h:77
#define OTG_FS_GRSTCTL
Definition: otg_fs.h:46
uint8_t dwc_ep_stall_get(usbd_device *usbd_dev, uint8_t addr)
void dwc_disconnect(usbd_device *usbd_dev, bool disconnected)
void dwc_endpoints_reset(usbd_device *usbd_dev)
uint16_t dwc_ep_write_packet(usbd_device *usbd_dev, uint8_t addr, const void *buf, uint16_t len)
uint16_t dwc_ep_read_packet(usbd_device *usbd_dev, uint8_t addr, void *buf, uint16_t len)
void dwc_set_address(usbd_device *usbd_dev, uint8_t addr)
void dwc_ep_nak_set(usbd_device *usbd_dev, uint8_t addr, uint8_t nak)
void dwc_ep_stall_set(usbd_device *usbd_dev, uint8_t addr, uint8_t stall)
void dwc_ep_setup(usbd_device *usbd_dev, uint8_t addr, uint8_t type, uint16_t max_size, void(*callback)(usbd_device *usbd_dev, uint8_t ep))
void dwc_poll(usbd_device *usbd_dev)