Skip to content

Commit 014912d

Browse files
iabdalkaderdpgeorge
authored andcommitted
stm32/ulpi: Add USB3320 ULPI low-power functions.
This driver allows switching USB3320 ULPI to low-power mode. It is ported/ adapted from ST code, stm32f7xx_lp_modes.c, which is BSD-3 licensed code.
1 parent 32914c5 commit 014912d

File tree

3 files changed

+227
-0
lines changed

3 files changed

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

ports/stm32/ulpi.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef MICROPY_INCLUDED_STM32_ULPI_H
27+
#define MICROPY_INCLUDED_STM32_ULPI_H
28+
29+
int ulpi_enter_low_power(void);
30+
int ulpi_leave_low_power(void);
31+
32+
#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