Skip to content

Commit 33d3469

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 33d3469

File tree

3 files changed

+211
-0
lines changed

3 files changed

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

ports/stm32/ulpi.h

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