Skip to content

Commit 68c8352

Browse files
committed
stm32: Add USB3320 ULPI low-power functions.
* This driver allows switching USB3320 ULPI to low-power mode. * Ported/adapted from ST stm32f7xx_lp_modes.c BSD-3 licensed code.
1 parent d242a9b commit 68c8352

File tree

3 files changed

+207
-0
lines changed

3 files changed

+207
-0
lines changed

ports/stm32/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ SRC_C += \
279279
pyb_spi.c \
280280
qspi.c \
281281
uart.c \
282+
ulpi.c \
282283
can.c \
283284
fdcan.c \
284285
pyb_can.c \

ports/stm32/ulpi.c

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/*
2+
* Copyright (c) 2016 STMicroelectronics. All rights reserved.
3+
*
4+
* This software component is licensed by ST under BSD 3-Clause license,
5+
* the "License"; You may not use this file except in compliance with the
6+
* License. You may obtain a copy of the License at:
7+
* opensource.org/licenses/BSD-3-Clause
8+
*
9+
* USB3320 ULPI functions ported from stm32f7xx_lp_modes.c
10+
*
11+
* Source file: https://github.com/STMicroelectronics/STM32CubeF7/blob/v1.15.0/Projects/STM32746G-Discovery/Examples/PWR/PWR_CurrentConsumption/Src/stm32f7xx_lp_modes.c
12+
*/
13+
#include <stdint.h>
14+
#include "py/mphal.h"
15+
#include "pin_static_af.h"
16+
#include "ulpi.h"
17+
18+
#if MICROPY_HW_USB_HS_ULPI3320
19+
#include STM32_HAL_H
20+
21+
#define USBULPI_PHYCR ((uint32_t)(0x40040000 + 0x034))
22+
#define USBULPI_D07 ((uint32_t)0x000000FF)
23+
#define USBULPI_New ((uint32_t)0x02000000)
24+
#define USBULPI_RW ((uint32_t)0x00400000)
25+
#define USBULPI_S_BUSY ((uint32_t)0x04000000)
26+
#define USBULPI_S_DONE ((uint32_t)0x08000000)
27+
#define USBULPI_TIMEOUT_COUNT (100)
28+
29+
#define USB_OTG_READ_REG32(reg) (*(__IO uint32_t *)(reg))
30+
#define USB_OTG_WRITE_REG32(reg,value) (*(__IO uint32_t *)(reg) = (value))
31+
32+
/**
33+
* @brief Read CR value
34+
* @param Addr the Address of the ULPI Register
35+
* @retval Returns value of PHY CR register
36+
*/
37+
static uint32_t USB_ULPI_Read(uint32_t Addr) {
38+
uint32_t val = 0;
39+
uint32_t timeout = USBULPI_TIMEOUT_COUNT;
40+
41+
USB_OTG_WRITE_REG32(USBULPI_PHYCR, USBULPI_New | (Addr << 16));
42+
val = USB_OTG_READ_REG32(USBULPI_PHYCR);
43+
while (((val & USBULPI_S_DONE) == 0) && (timeout--)) {
44+
val = USB_OTG_READ_REG32(USBULPI_PHYCR);
45+
}
46+
val = USB_OTG_READ_REG32(USBULPI_PHYCR);
47+
return val & 0x000000ff;
48+
}
49+
50+
/**
51+
* @brief Write CR value
52+
* @param Addr the Address of the ULPI Register
53+
* @param Data Data to write
54+
* @retval Returns value of PHY CR register
55+
*/
56+
static uint32_t USB_ULPI_Write(uint32_t Addr, uint32_t Data) {
57+
uint32_t val;
58+
uint32_t timeout = USBULPI_TIMEOUT_COUNT;
59+
60+
USB_OTG_WRITE_REG32(USBULPI_PHYCR, USBULPI_New | USBULPI_RW | (Addr << 16) | (Data & 0x000000ff));
61+
val = USB_OTG_READ_REG32(USBULPI_PHYCR);
62+
while (((val & USBULPI_S_DONE) == 0) && (timeout--)) {
63+
val = USB_OTG_READ_REG32(USBULPI_PHYCR);
64+
}
65+
66+
val = USB_OTG_READ_REG32(USBULPI_PHYCR);
67+
return 0;
68+
}
69+
70+
/**
71+
* @brief This function configures the USB PHY to enter the low power mode
72+
* @param None
73+
* @retval None
74+
*/
75+
int ulpi_enter_low_power(void) {
76+
uint32_t regval = 0;
77+
78+
// Disable ULPI_CLK by accessing ULPI_PHY
79+
// Read Vendor ID : (Low, High) 0x24,0x04 for USB3300
80+
regval = USB_ULPI_Read(0x00);
81+
if (regval != 0x24) {
82+
return MP_EIO;
83+
}
84+
85+
regval = USB_ULPI_Read(0x01);
86+
if (regval != 0x04) {
87+
return MP_EIO;
88+
}
89+
90+
// Read Product ID
91+
regval = USB_ULPI_Read(0x02);
92+
if (regval != 0x07) {
93+
return MP_EIO;
94+
}
95+
96+
regval = USB_ULPI_Read(0x03);
97+
if (regval != 0x00) {
98+
return MP_EIO;
99+
}
100+
101+
// Write to scratch register the pattern 0x55
102+
USB_ULPI_Write(0x16, 0x55);
103+
// Read to scratch Register and check-it again the written Pattern
104+
regval = USB_ULPI_Read(0x16);
105+
if (regval != 0x55) {
106+
return MP_EIO;
107+
}
108+
109+
// Write to scratch register the pattern 0xAA
110+
USB_ULPI_Write(0x16, 0xAA);
111+
// Read to scratch Register and check-it again the written Pattern
112+
regval = USB_ULPI_Read(0x16);
113+
if (regval != 0xAA) {
114+
return MP_EIO;
115+
}
116+
117+
// Read InterfaceControl reg
118+
regval = USB_ULPI_Read(0x07);
119+
120+
// Write InterfaceControl reg,to disable PullUp on stp,
121+
// to avoid USB_PHY wake up when MCU entering standby
122+
USB_ULPI_Write(0x07, regval | 0x80);
123+
124+
// Read InterfaceControl reg
125+
regval = USB_ULPI_Read(0x07);
126+
if (regval != 0x80) {
127+
return MP_EIO;
128+
}
129+
130+
// Read FunctionControl reg
131+
regval = USB_ULPI_Read(0x04);
132+
133+
// Reg 0x40 has a different value if USB is disconnected
134+
if (regval != 0x40 && regval != 0x45) {
135+
return MP_EIO;
136+
}
137+
138+
// Write FunctionControl reg,to put PHY into LowPower mode
139+
USB_ULPI_Write(0x04, regval & (~0x40));
140+
141+
// Read FunctionControl reg again
142+
regval = USB_ULPI_Read(0x04);
143+
if (regval != 0x00) {
144+
return MP_EIO;
145+
}
146+
147+
// Wait for the transceiver clock to stop.
148+
HAL_Delay(10);
149+
150+
return 0;
151+
}
152+
153+
/**
154+
* @brief This function wakeup the USB PHY from the Low power mode
155+
* @param None
156+
* @retval None
157+
*/
158+
int ulpi_leave_low_power(void) {
159+
// Configure STP as an output pin
160+
mp_hal_pin_config(MICROPY_HW_USB_HS_ULPI_STP, MP_HAL_PIN_MODE_OUTPUT, MP_HAL_PIN_PULL_NONE, 0);
161+
mp_hal_pin_config_speed(MICROPY_HW_USB_HS_ULPI_STP, MP_HAL_PIN_SPEED_VERY_HIGH);
162+
163+
// Configure DIR as an input pin
164+
mp_hal_pin_config(MICROPY_HW_USB_HS_ULPI_DIR, MP_HAL_PIN_MODE_INPUT, MP_HAL_PIN_PULL_NONE, 0);
165+
mp_hal_pin_config_speed(MICROPY_HW_USB_HS_ULPI_DIR, MP_HAL_PIN_SPEED_VERY_HIGH);
166+
167+
// Set STP pin high
168+
mp_hal_pin_high(MICROPY_HW_USB_HS_ULPI_STP);
169+
170+
// Wait for DIR to go low
171+
for (uint32_t i = 0; i < 500; i++, HAL_Delay(1)) {
172+
if (mp_hal_pin_read(MICROPY_HW_USB_HS_ULPI_DIR) == 0) {
173+
break;
174+
}
175+
}
176+
177+
// Save DIR pin state before switching to alternate function
178+
int dir_state = mp_hal_pin_read(MICROPY_HW_USB_HS_ULPI_DIR);
179+
180+
// Revert STP/DIR to their alternate functions
181+
mp_hal_pin_config(MICROPY_HW_USB_HS_ULPI_STP, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, GPIO_AF10_OTG_HS);
182+
mp_hal_pin_config_speed(MICROPY_HW_USB_HS_ULPI_STP, MP_HAL_PIN_SPEED_VERY_HIGH);
183+
184+
mp_hal_pin_config(MICROPY_HW_USB_HS_ULPI_DIR, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, GPIO_AF10_OTG_HS);
185+
mp_hal_pin_config_speed(MICROPY_HW_USB_HS_ULPI_DIR, MP_HAL_PIN_SPEED_VERY_HIGH);
186+
187+
return (dir_state == 0) ? 0 : -1;
188+
}
189+
#endif // MICROPY_HW_USB_HS_ULPI3320

ports/stm32/ulpi.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (c) 2016 STMicroelectronics. All rights reserved.
3+
*
4+
* This software component is licensed by ST under BSD 3-Clause license,
5+
* the "License"; You may not use this file except in compliance with the
6+
* License. You may obtain a copy of the License at:
7+
* opensource.org/licenses/BSD-3-Clause
8+
*
9+
* USB3320 ULPI functions ported from stm32f7xx_lp_modes.c
10+
*
11+
* Source file: https://github.com/STMicroelectronics/STM32CubeF7/blob/v1.15.0/Projects/STM32746G-Discovery/Examples/PWR/PWR_CurrentConsumption/Src/stm32f7xx_lp_modes.c
12+
*/
13+
#ifndef MICROPY_INCLUDED_STM32_ULPI_H
14+
#define MICROPY_INCLUDED_STM32_ULPI_H
15+
int ulpi_enter_low_power(void);
16+
int ulpi_leave_low_power(void);
17+
#endif // MICROPY_INCLUDED_STM32_ULPI_H

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy