mikroSDK Reference Manual
usb_hw.h
Go to the documentation of this file.
1/****************************************************************************
2**
3** Copyright (C) 2024 MikroElektronika d.o.o.
4** Contact: https://www.mikroe.com/contact
5**
6** This file is part of the mikroSDK package
7**
8** Commercial License Usage
9**
10** Licensees holding valid commercial NECTO compilers AI licenses may use this
11** file in accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The MikroElektronika Company.
14** For licensing terms and conditions see
15** https://www.mikroe.com/legal/software-license-agreement.
16** For further information use the contact form at
17** https://www.mikroe.com/contact.
18**
19**
20** GNU Lesser General Public License Usage
21**
22** Alternatively, this file may be used for
23** non-commercial projects under the terms of the GNU Lesser
24** General Public License version 3 as published by the Free Software
25** Foundation: https://www.gnu.org/licenses/lgpl-3.0.html.
26**
27** The above copyright notice and this permission notice shall be
28** included in all copies or substantial portions of the Software.
29**
30** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31** OF MERCHANTABILITY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
32** TO THE WARRANTIES FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
33** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
34** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
35** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
36** OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37**
38****************************************************************************/
45#ifndef _USB_HW_H_
46#define _USB_HW_H_
47
48#ifdef __cplusplus
49extern "C"{
50#endif
51
52#include "mcu.h"
53#include <stdint.h>
54
77#define analog_function_enable(_reg,_pin) (_reg |= 1U << _pin)
78
85#define alternate_function_enable(_pin) (GPIO_PORTB_AHB_AFSEL_R |= 1U << _pin)
86
90#define GPIO_PORTB_PIN0 (0)
91#define GPIO_PORTB_PIN1 (1)
92#define GPIO_PORTL_PIN6 (6)
93#define GPIO_PORTL_PIN7 (7)
94#define USB_DP_PIN GPIO_PORTL_PIN6
95#define USB_DN_PIN GPIO_PORTL_PIN7
96#define USB_ID_PIN GPIO_PORTB_PIN0
97#define USB_VBUS_PIN GPIO_PORTB_PIN1
98
109static const unsigned long assumed_mosc_value = 25;
110
111static unsigned long get_vco_in_mhz(void){
112 unsigned long result = 480;
113 const unsigned long sysctl_rsclkcfg_pllsrc24_mask = (0xFUL << 24),
114 mfrac = (SYSCTL_PLLFREQ0_R >> 10) & 0x3FF,
115 mint = SYSCTL_PLLFREQ0_R & 0x3FF,
116 q = (SYSCTL_PLLFREQ1_R >> 8) & 0x1F,
117 n = SYSCTL_PLLFREQ1_R & 0x1F;
118
119 if ( SYSCTL_RSCLKCFG_R & SYSCTL_RSCLKCFG_USEPLL ){
120 if ( ( SYSCTL_RSCLKCFG_R & sysctl_rsclkcfg_pllsrc24_mask ) == 0 ) {
121 result = ( 16 * ( mint + ( mfrac / 1024 ) ) ) / ( ( q + 1 ) * ( n + 1 ) );
122 } else if ( ( SYSCTL_RSCLKCFG_R & sysctl_rsclkcfg_pllsrc24_mask ) == ( 3UL << 24 ) ) {
127 result = ( assumed_mosc_value * ( mint + ( mfrac / 1024 ) ) ) / ( ( q + 1 ) * ( n + 1 ) );
128 }
129 }
130
131 return result;
132}
133
134static void usb_set_cc(void){
135 unsigned long reg = USB0_CC_R,
136 clkdiv = (get_vco_in_mhz() / 60 - 1);
137
138 reg &= ~(0xFUL | USB_CC_CSD);
139 reg |= (clkdiv & 0xF) | USB_CC_CLKEN;
140
141 USB0_CC_R = reg;
142}
143
151static inline void usb_hw_init(void) {
152 // Enable clock to USB0ID/USB0VBUS pins.
153 SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R1;
154 // Enable clock to DP/DM pins.
155 SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R10;
156 // Set pins as analog.
157 // The analog functions of the pins are enabled, the isolation is
158 // disabled, and the pins are capable of analog functions.
159 analog_function_enable(GPIO_PORTB_AHB_AMSEL_R,GPIO_PORTB_PIN0);
160 analog_function_enable(GPIO_PORTB_AHB_AMSEL_R,GPIO_PORTB_PIN1);
161 // Set pins alternate function.
162 // The associated pin functions as a peripheral signal and is
163 // controlled by the alternate hardware function.
165 alternate_function_enable(GPIO_PORTB_PIN1);
166 // Set pins as analog.
167 // The analog functions of the pins are enabled, the isolation is
168 // disabled, and the pins are capable of analog functions.
169 analog_function_enable(GPIO_PORTL_AMSEL_R,USB_DN_PIN);
170 analog_function_enable(GPIO_PORTL_AMSEL_R,USB_DP_PIN);
171 // Enable general USB clock.
172 SYSCTL_RCGCUSB_R |= SYSCTL_RCGCUSB_R0;
173 // Calculate and set apprpriate USB clock values.
174 usb_set_cc();
175}
176
177 // usb
178 // middleware
179
180#ifdef __cplusplus
181}
182#endif
183
184#endif // _USB_HW_H_
185// ------------------------------------------------------------------------- END
#define GPIO_PORTB_PIN0
Pin numbers.
Definition usb_hw.h:89
#define alternate_function_enable(_pin)
Enable alternate functionality.
Definition usb_hw.h:84
#define analog_function_enable(_pin)
Enable analog functionality.
Definition usb_hw.h:77