From 42b6f30edaf91196ecdaaebb36385bcc3f312d17 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 18 Oct 2024 18:33:50 +0700 Subject: [PATCH] add VBUS drive via MFX for h743 eval, but does not seems to work yet --- hw/bsp/stm32h7/boards/daisyseed/board.h | 2 +- hw/bsp/stm32h7/boards/stm32h723nucleo/board.h | 2 +- hw/bsp/stm32h7/boards/stm32h743eval/board.h | 153 +++++++++++++++++- .../stm32h743eval/cubemx/stm32h743eval.ioc | 27 ++-- hw/bsp/stm32h7/boards/stm32h743nucleo/board.h | 2 +- hw/bsp/stm32h7/boards/stm32h745disco/board.h | 2 +- hw/bsp/stm32h7/boards/stm32h750_weact/board.h | 2 +- hw/bsp/stm32h7/boards/stm32h750bdk/board.h | 2 +- .../boards/waveshare_openh743i/board.h | 2 +- hw/bsp/stm32h7/family.c | 7 +- hw/bsp/stm32h7/family.cmake | 7 + hw/bsp/stm32h7/stm32h7xx_hal_conf.h | 2 +- tools/get_deps.py | 3 + 13 files changed, 191 insertions(+), 22 deletions(-) diff --git a/hw/bsp/stm32h7/boards/daisyseed/board.h b/hw/bsp/stm32h7/boards/daisyseed/board.h index abc07488b..532e3bfd5 100644 --- a/hw/bsp/stm32h7/boards/daisyseed/board.h +++ b/hw/bsp/stm32h7/boards/daisyseed/board.h @@ -128,7 +128,7 @@ static inline void SystemClock_Config(void) HAL_EnableCompensationCell(); } -static inline void board_stm32h7_post_init(void) +static inline void board_init2(void) { // For this board does nothing } diff --git a/hw/bsp/stm32h7/boards/stm32h723nucleo/board.h b/hw/bsp/stm32h7/boards/stm32h723nucleo/board.h index 3d9344a87..b3da1348f 100644 --- a/hw/bsp/stm32h7/boards/stm32h723nucleo/board.h +++ b/hw/bsp/stm32h7/boards/stm32h723nucleo/board.h @@ -118,7 +118,7 @@ static inline void SystemClock_Config(void) HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct); } -static inline void board_stm32h7_post_init(void) +static inline void board_init2(void) { // For this board does nothing } diff --git a/hw/bsp/stm32h7/boards/stm32h743eval/board.h b/hw/bsp/stm32h7/boards/stm32h743eval/board.h index 22d66d735..9e2649d14 100644 --- a/hw/bsp/stm32h7/boards/stm32h743eval/board.h +++ b/hw/bsp/stm32h7/boards/stm32h743eval/board.h @@ -31,6 +31,8 @@ extern "C" { #endif +#include "mfxstm32l152.h" + #define LED_PORT GPIOA #define LED_PIN GPIO_PIN_4 #define LED_STATE_ON 1 @@ -58,6 +60,22 @@ {GPIOB, GPIO_PIN_5 }, {GPIOB, GPIO_PIN_10}, {GPIOB, GPIO_PIN_11}, {GPIOB, GPIO_PIN_12}, \ {GPIOB, GPIO_PIN_13}, {GPIOC, GPIO_PIN_0 }, {GPIOH, GPIO_PIN_4 }, {GPIOI, GPIO_PIN_11} +// vbus drive +#define BOARD_VBUS_DRIVE(_rhport, _on) do { \ + if ( mfx_io_drv ) { \ + uint32_t io_pin = (_rhport) ? MFXSTM32L152_GPIO_PIN_9 : MFXSTM32L152_GPIO_PIN_7; \ + mfx_io_drv->IO_WritePin(&Io_CompObj, io_pin, _on); \ + }\ + } while(0) + +/* Definition for I2C1 Pins */ +#define BUS_I2C1_SCL_PIN GPIO_PIN_6 +#define BUS_I2C1_SDA_PIN GPIO_PIN_7 +#define BUS_I2C1_SCL_GPIO_PORT GPIOB +#define BUS_I2C1_SDA_GPIO_PORT GPIOB +#define BUS_I2C1_SCL_AF GPIO_AF4_I2C1 +#define BUS_I2C1_SDA_AF GPIO_AF4_I2C1 + //--------------------------------------------------------------------+ // RCC Clock //--------------------------------------------------------------------+ @@ -132,11 +150,140 @@ static inline void SystemClock_Config(void) { HAL_EnableCompensationCell(); } -static inline void board_stm32h7_post_init(void) -{ - // For this board does nothing +//--------------------------------------------------------------------+ +// MFX +//--------------------------------------------------------------------+ +I2C_HandleTypeDef hbus_i2c1 = { .Instance = I2C1}; +static MFXSTM32L152_Object_t mfx_obj = { 0 }; +static MFXSTM32L152_IO_Mode_t* mfx_io_drv = NULL; + +HAL_StatusTypeDef MX_I2C1_Init(I2C_HandleTypeDef* hI2c, uint32_t timing) { + hI2c->Init.Timing = timing; + hI2c->Init.OwnAddress1 = 0; + hI2c->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; + hI2c->Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; + hI2c->Init.OwnAddress2 = 0; + hI2c->Init.OwnAddress2Masks = I2C_OA2_NOMASK; + hI2c->Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; + hI2c->Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; + + if (HAL_I2C_Init(hI2c) != HAL_OK) { + return HAL_ERROR; + } + if (HAL_I2CEx_ConfigAnalogFilter(hI2c, I2C_ANALOGFILTER_ENABLE) != HAL_OK) { + return HAL_ERROR; + } + if (HAL_I2CEx_ConfigDigitalFilter(hI2c, 0) != HAL_OK) { + return HAL_ERROR; + } + + return HAL_OK; } +int32_t BSP_I2C1_Init(void) { + // Init I2C + GPIO_InitTypeDef gpio_init_structure; + gpio_init_structure.Pin = BUS_I2C1_SCL_PIN; + gpio_init_structure.Mode = GPIO_MODE_AF_OD; + gpio_init_structure.Pull = GPIO_NOPULL; + gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH; + gpio_init_structure.Alternate = BUS_I2C1_SCL_AF; + HAL_GPIO_Init(BUS_I2C1_SCL_GPIO_PORT, &gpio_init_structure); + + gpio_init_structure.Pin = BUS_I2C1_SDA_PIN; + gpio_init_structure.Mode = GPIO_MODE_AF_OD; + gpio_init_structure.Pull = GPIO_NOPULL; + gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH; + gpio_init_structure.Alternate = BUS_I2C1_SDA_AF; + HAL_GPIO_Init(BUS_I2C1_SDA_GPIO_PORT, &gpio_init_structure); + + __HAL_RCC_I2C1_CLK_ENABLE(); + __HAL_RCC_I2C1_FORCE_RESET(); + __HAL_RCC_I2C1_RELEASE_RESET(); + + if (MX_I2C1_Init(&hbus_i2c1, /*0x10C0ECFF*/ 1890596921) != HAL_OK) { + return -1; + } + + return 0; +} + +int32_t BSP_I2C1_DeInit(void) { + return 0; +} + +int32_t BSP_I2C1_ReadReg(uint16_t DevAddr, uint16_t Reg, uint8_t *pData, uint16_t Length) { + if (HAL_OK != HAL_I2C_Mem_Read(&hbus_i2c1, DevAddr, Reg, I2C_MEMADD_SIZE_8BIT, pData, Length, 10000)) { + return -1; + } + return 0; +} + +int32_t BSP_I2C1_WriteReg(uint16_t DevAddr, uint16_t Reg, uint8_t *pData, uint16_t Length) { + if(HAL_OK != HAL_I2C_Mem_Write(&hbus_i2c1, DevAddr, Reg, I2C_MEMADD_SIZE_8BIT, pData, Length, 10000)) { + return -1; + } + return 0; +} + + +static inline void board_init2(void) { + // Init MFX IO expanding for vbus drive + BSP_I2C1_Init(); + + /* Configure the audio driver */ + MFXSTM32L152_IO_t IOCtx; + IOCtx.Init = BSP_I2C1_DeInit; + IOCtx.DeInit = BSP_I2C1_DeInit; + IOCtx.ReadReg = BSP_I2C1_ReadReg; + IOCtx.WriteReg = BSP_I2C1_WriteReg; + IOCtx.GetTick = (MFXSTM32L152_GetTick_Func) HAL_GetTick; + + uint8_t i2c_address[] = {0x84, 0x86}; + for(uint8_t i = 0U; i < 2U; i++) { + uint32_t mfx_id; + IOCtx.Address = (uint16_t)i2c_address[i]; + if (MFXSTM32L152_RegisterBusIO(&mfx_obj, &IOCtx) != MFXSTM32L152_OK) { + return; + } + if (MFXSTM32L152_ReadID(&mfx_obj, &mfx_id) != MFXSTM32L152_OK) { + return; + } + + if ((mfx_id == MFXSTM32L152_ID) || (mfx_id == MFXSTM32L152_ID_2)) { + if (MFXSTM32L152_Init(&mfx_obj) != MFXSTM32L152_OK) { + return; + } + break; + } + } + + mfx_io_drv = &MFXSTM32L152_IO_Driver; + + static MFXSTM32L152_IO_Init_t io_init = { 0 }; + mfx_io_drv->Init(&mfx_obj, &io_init); + + io_init.Pin = MFXSTM32L152_GPIO_PIN_7; + io_init.Mode = MFXSTM32L152_GPIO_MODE_OUTPUT_PP; + io_init.Pull = MFXSTM32L152_GPIO_PULLUP; + mfx_io_drv->Init(&mfx_obj, &io_init); // VBUS[0] + + io_init.Pin = MFXSTM32L152_GPIO_PIN_9; + mfx_io_drv->Init(&mfx_obj, &io_init); // VBUS[1] + +#if 1 // write then read IO7 but it does not seems to change value + int32_t pin_value; + pin_value = mfx_io_drv->IO_ReadPin(&mfx_obj, MFXSTM32L152_GPIO_PIN_7); + TU_LOG1_INT(pin_value); + + mfx_io_drv->IO_WritePin(&mfx_obj, MFXSTM32L152_GPIO_PIN_7, 1); + + pin_value = mfx_io_drv->IO_ReadPin(&mfx_obj, MFXSTM32L152_GPIO_PIN_7); + TU_LOG1_INT(pin_value); +#endif +} + + #ifdef __cplusplus } #endif diff --git a/hw/bsp/stm32h7/boards/stm32h743eval/cubemx/stm32h743eval.ioc b/hw/bsp/stm32h7/boards/stm32h743eval/cubemx/stm32h743eval.ioc index 01458a0a9..0e5a4cc00 100644 --- a/hw/bsp/stm32h7/boards/stm32h743eval/cubemx/stm32h743eval.ioc +++ b/hw/bsp/stm32h7/boards/stm32h743eval/cubemx/stm32h743eval.ioc @@ -4,17 +4,20 @@ CAD.pinconfig=Project naming CAD.provider= File.Version=6 GPIO.groupedBy=Group By Peripherals +I2C1.IPParameters=Timing +I2C1.Timing=0x10C0ECFF KeepUserPlacement=false Mcu.CPN=STM32H743XIH6 Mcu.Family=STM32H7 Mcu.IP0=CORTEX_M7 Mcu.IP1=DEBUG -Mcu.IP2=NVIC -Mcu.IP3=RCC -Mcu.IP4=SYS -Mcu.IP5=USB_OTG_FS -Mcu.IP6=USB_OTG_HS -Mcu.IPNb=7 +Mcu.IP2=I2C1 +Mcu.IP3=NVIC +Mcu.IP4=RCC +Mcu.IP5=SYS +Mcu.IP6=USB_OTG_FS +Mcu.IP7=USB_OTG_HS +Mcu.IPNb=8 Mcu.Name=STM32H743XIHx Mcu.Package=TFBGA240 Mcu.Pin0=PI6 @@ -190,8 +193,8 @@ Mcu.PinsNb=169 Mcu.ThirdPartyNb=0 Mcu.UserConstants= Mcu.UserName=STM32H743XIHx -MxCube.Version=6.8.1 -MxDb.Version=DB.6.0.81 +MxCube.Version=6.10.0 +MxDb.Version=DB.6.0.100 NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:true\:false\:false NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:true\:false\:false NVIC.ForceEnableDMAVector=true @@ -325,10 +328,12 @@ PB5.Signal=USB_OTG_HS_ULPI_D7 PB6.GPIOParameters=GPIO_Label PB6.GPIO_Label=I2C1_SCL [STM32L152CCT6_I2C_SCL] PB6.Locked=true +PB6.Mode=I2C PB6.Signal=I2C1_SCL PB7.GPIOParameters=GPIO_Label PB7.GPIO_Label=I2C1_SDA [STM32L152CCT6_I2C_SDA] PB7.Locked=true +PB7.Mode=I2C PB7.Signal=I2C1_SDA PB8.GPIOParameters=GPIO_Label PB8.GPIO_Label=SDIO1_CKIN @@ -880,7 +885,7 @@ ProjectManager.FreePins=false ProjectManager.HalAssertFull=false ProjectManager.HeapSize=0x200 ProjectManager.KeepUserCode=true -ProjectManager.LastFirmware=true +ProjectManager.LastFirmware=false ProjectManager.LibraryCopy=2 ProjectManager.MainLocation=Src ProjectManager.NoMain=false @@ -893,8 +898,10 @@ ProjectManager.RegisterCallBack= ProjectManager.StackSize=0x400 ProjectManager.TargetToolchain=Makefile ProjectManager.ToolChainLocation=Src/ +ProjectManager.UAScriptAfterPath= +ProjectManager.UAScriptBeforePath= ProjectManager.UnderRoot=false -ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_USB_OTG_FS_PCD_Init-USB_OTG_FS-false-HAL-true,4-MX_USB_OTG_HS_PCD_Init-USB_OTG_HS-false-HAL-true,0-MX_CORTEX_M7_Init-CORTEX_M7-false-HAL-true +ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_USB_OTG_HS_PCD_Init-USB_OTG_HS-false-HAL-true,4-MX_I2C1_Init-I2C1-false-HAL-true,5-MX_USB_OTG_FS_PCD_Init-USB_OTG_FS-false-HAL-true,0-MX_CORTEX_M7_Init-CORTEX_M7-false-HAL-true RCC.ADCFreq_Value=50390625 RCC.AHB12Freq_Value=200000000 RCC.AHB4Freq_Value=200000000 diff --git a/hw/bsp/stm32h7/boards/stm32h743nucleo/board.h b/hw/bsp/stm32h7/boards/stm32h743nucleo/board.h index 614e6e38b..513ce2bb7 100644 --- a/hw/bsp/stm32h7/boards/stm32h743nucleo/board.h +++ b/hw/bsp/stm32h7/boards/stm32h743nucleo/board.h @@ -122,7 +122,7 @@ static inline void SystemClock_Config(void) { } } -static inline void board_stm32h7_post_init(void) +static inline void board_init2(void) { // For this board does nothing } diff --git a/hw/bsp/stm32h7/boards/stm32h745disco/board.h b/hw/bsp/stm32h7/boards/stm32h745disco/board.h index 6d1506ca1..c0f85ddbe 100644 --- a/hw/bsp/stm32h7/boards/stm32h745disco/board.h +++ b/hw/bsp/stm32h7/boards/stm32h745disco/board.h @@ -127,7 +127,7 @@ static inline void SystemClock_Config(void) HAL_EnableCompensationCell(); } -static inline void board_stm32h7_post_init(void) +static inline void board_init2(void) { // For this board does nothing } diff --git a/hw/bsp/stm32h7/boards/stm32h750_weact/board.h b/hw/bsp/stm32h7/boards/stm32h750_weact/board.h index d117637a5..5beab20ce 100644 --- a/hw/bsp/stm32h7/boards/stm32h750_weact/board.h +++ b/hw/bsp/stm32h7/boards/stm32h750_weact/board.h @@ -121,7 +121,7 @@ static inline void SystemClock_Config(void) { HAL_PWREx_EnableUSBVoltageDetector(); } -static inline void board_stm32h7_post_init(void) { +static inline void board_init2(void) { // For this board does nothing } diff --git a/hw/bsp/stm32h7/boards/stm32h750bdk/board.h b/hw/bsp/stm32h7/boards/stm32h750bdk/board.h index c5922efc4..b0b063cda 100644 --- a/hw/bsp/stm32h7/boards/stm32h750bdk/board.h +++ b/hw/bsp/stm32h7/boards/stm32h750bdk/board.h @@ -132,7 +132,7 @@ static inline void SystemClock_Config(void) HAL_EnableCompensationCell(); } -static inline void board_stm32h7_post_init(void) +static inline void board_init2(void) { // For this board does nothing } diff --git a/hw/bsp/stm32h7/boards/waveshare_openh743i/board.h b/hw/bsp/stm32h7/boards/waveshare_openh743i/board.h index 8f4af6f48..46efca6fb 100644 --- a/hw/bsp/stm32h7/boards/waveshare_openh743i/board.h +++ b/hw/bsp/stm32h7/boards/waveshare_openh743i/board.h @@ -184,7 +184,7 @@ static inline void timer_board_delay(TIM_HandleTypeDef* tim_hdl, uint32_t ms) } } -static inline void board_stm32h7_post_init(void) +static inline void board_init2(void) { // walkaround for resetting the ULPI PHY using Timer since systick is not // available when RTOS is used. diff --git a/hw/bsp/stm32h7/family.c b/hw/bsp/stm32h7/family.c index 3bed1f5ec..c8b7c6fe3 100644 --- a/hw/bsp/stm32h7/family.c +++ b/hw/bsp/stm32h7/family.c @@ -236,7 +236,12 @@ void board_init(void) { HAL_PWREx_EnableUSBVoltageDetector(); // For waveshare openh743 ULPI PHY reset walkaround - board_stm32h7_post_init(); + board_init2(); + +#if CFG_TUH_ENABLED && defined(BOARD_VBUS_DRIVE) + BOARD_VBUS_DRIVE(BOARD_TUH_RHPORT, 1); +#endif + } //--------------------------------------------------------------------+ diff --git a/hw/bsp/stm32h7/family.cmake b/hw/bsp/stm32h7/family.cmake index e4161ffe5..d0fb963a9 100644 --- a/hw/bsp/stm32h7/family.cmake +++ b/hw/bsp/stm32h7/family.cmake @@ -5,6 +5,7 @@ set(ST_PREFIX stm32${ST_FAMILY}xx) set(ST_HAL_DRIVER ${TOP}/hw/mcu/st/stm32${ST_FAMILY}xx_hal_driver) set(ST_CMSIS ${TOP}/hw/mcu/st/cmsis_device_${ST_FAMILY}) +set(MFXSTM32L152 ${TOP}/hw/mcu/st/stm32-mfxstm32l152) set(CMSIS_5 ${TOP}/lib/CMSIS_5) # include board specific @@ -42,6 +43,8 @@ function(add_board_target BOARD_TARGET) ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_cortex.c ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_dma.c ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_gpio.c + ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_i2c.c + ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_i2c_ex.c ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_pwr.c ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_pwr_ex.c ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_rcc.c @@ -49,12 +52,16 @@ function(add_board_target BOARD_TARGET) ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_uart.c ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_uart_ex.c ${STARTUP_FILE_${CMAKE_C_COMPILER_ID}} + # MFXSTM32L152 + ${MFXSTM32L152}/mfxstm32l152.c + ${MFXSTM32L152}/mfxstm32l152_reg.c ) target_include_directories(${BOARD_TARGET} PUBLIC ${CMAKE_CURRENT_FUNCTION_LIST_DIR} ${CMSIS_5}/CMSIS/Core/Include ${ST_CMSIS}/Include ${ST_HAL_DRIVER}/Inc + ${MFXSTM32L152} ) #target_compile_options(${BOARD_TARGET} PUBLIC) #target_compile_definitions(${BOARD_TARGET} PUBLIC) diff --git a/hw/bsp/stm32h7/stm32h7xx_hal_conf.h b/hw/bsp/stm32h7/stm32h7xx_hal_conf.h index 216fc82f2..303dcc137 100644 --- a/hw/bsp/stm32h7/stm32h7xx_hal_conf.h +++ b/hw/bsp/stm32h7/stm32h7xx_hal_conf.h @@ -70,7 +70,7 @@ /* #define HAL_HCD_MODULE_ENABLED */ /* #define HAL_HRTIM_MODULE_ENABLED */ /* #define HAL_HSEM_MODULE_ENABLED */ -/* #define HAL_I2C_MODULE_ENABLED */ +#define HAL_I2C_MODULE_ENABLED /* #define HAL_I2S_MODULE_ENABLED */ /* #define HAL_IRDA_MODULE_ENABLED */ /* #define HAL_IWDG_MODULE_ENABLED */ diff --git a/tools/get_deps.py b/tools/get_deps.py index 519cb1d53..6d0ef9d0c 100755 --- a/tools/get_deps.py +++ b/tools/get_deps.py @@ -121,6 +121,9 @@ deps_optional = { 'hw/mcu/st/cmsis_device_wb': ['https://github.com/STMicroelectronics/cmsis_device_wb.git', '9c5d1920dd9fabbe2548e10561d63db829bb744f', 'stm32wb'], + 'hw/mcu/st/stm32-mfxstm32l152': ['https://github.com/STMicroelectronics/stm32-mfxstm32l152.git', + '7f4389efee9c6a655b55e5df3fceef5586b35f9b', + 'stm32h7'], 'hw/mcu/st/stm32f0xx_hal_driver': ['https://github.com/STMicroelectronics/stm32f0xx_hal_driver.git', '0e95cd88657030f640a11e690a8a5186c7712ea5', 'stm32f0'],