Skip to content

Commit

Permalink
added SDMMC support to WeAct H723_LCD board
Browse files Browse the repository at this point in the history
  • Loading branch information
bshewan committed Aug 23, 2024
1 parent 9f4749f commit 6004174
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 18 deletions.
5 changes: 4 additions & 1 deletion ports/stm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ SRC_STM32 = $(addprefix $(HAL_DIR)/Src/stm32$(MCU_SERIES_LOWER)xx_,\
hal_gpio.c \
hal_i2c.c \
hal_i2c_ex.c \
hal_ospi.c \
hal_pcd.c \
hal_pcd_ex.c \
hal_pwr.c \
Expand Down Expand Up @@ -173,6 +172,10 @@ ifeq ($(MCU_VARIANT),$(filter $(MCU_VARIANT),STM32F765xx STM32F767xx STM32F769xx
SRC_STM32 += $(HAL_DIR)/Src/stm32$(MCU_SERIES_LOWER)xx_hal_uart_ex.c
endif

ifeq ($(MCU_VARIANT),$(filter $(MCU_VARIANT),STM32H723xx))
SRC_STM32 += $(HAL_DIR)/Src/stm32$(MCU_SERIES_LOWER)xx_hal_ospi.c
endif

SRC_STM32 += boards/system_stm32$(MCU_SERIES_LOWER)xx.c

SRC_C += \
Expand Down
1 change: 1 addition & 0 deletions ports/stm/boards/weact-h723vg-lcd/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ CIRCUITPY_AUDIOIO = 0
CIRCUITPY_ROTARYIO = 0
CIRCUITPY_RTC = 1
CIRCUITPY_SDCARDIO = 1
CIRCUITPY_SDIOIO = 1
CIRCUITPY_FRAMEBUFFERIO = 0
CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_I2CTARGET = 0
Expand Down
22 changes: 20 additions & 2 deletions ports/stm/common-hal/sdioio/SDCard.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void common_hal_sdioio_sdcard_construct(sdioio_sdcard_obj_t *self,
uint8_t num_data, const mcu_pin_obj_t **data, uint32_t frequency) {

int periph_index = check_pins(self, clock, command, num_data, data);
SDIO_TypeDef *SDIOx = mcu_sdio_banks[periph_index - 1];
SD_TypeDef *SDIOx = mcu_sdio_banks[periph_index - 1];

GPIO_InitTypeDef GPIO_InitStruct = {0};

Expand All @@ -128,14 +128,22 @@ void common_hal_sdioio_sdcard_construct(sdioio_sdcard_obj_t *self,
GPIO_InitStruct.Pin = pin_mask(clock->number);
HAL_GPIO_Init(pin_port(clock->port), &GPIO_InitStruct);

#if CPY_STM32H7
__HAL_RCC_SDMMC1_CLK_ENABLE();
self->handle.Init.ClockDiv = SDMMC_NSPEED_CLK_DIV;
self->handle.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
self->handle.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
self->handle.Init.BusWide = SDMMC_BUS_WIDE_1B;
self->handle.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_ENABLE;
#else
__HAL_RCC_SDIO_CLK_ENABLE();

self->handle.Init.ClockDiv = SDIO_TRANSFER_CLK_DIV;
self->handle.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
self->handle.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
self->handle.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
self->handle.Init.BusWide = SDIO_BUS_WIDE_1B;
self->handle.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
#endif
self->handle.Instance = SDIOx;

HAL_StatusTypeDef r = HAL_SD_Init(&self->handle);
Expand All @@ -149,6 +157,15 @@ void common_hal_sdioio_sdcard_construct(sdioio_sdcard_obj_t *self,
mp_raise_ValueError_varg(MP_ERROR_TEXT("SDIO GetCardInfo Error %d"), (int)r);
}

#if CPY_STM32H7
self->num_data = 1;
if (num_data == 4) {
if ((r = HAL_SD_ConfigWideBusOperation(&self->handle, SDMMC_BUS_WIDE_4B)) == HAL_SD_ERROR_NONE) {
self->handle.Init.BusWide = SDMMC_BUS_WIDE_4B;
self->num_data = 4;
}
}
#else
self->num_data = 1;
if (num_data == 4) {
if ((r = HAL_SD_ConfigWideBusOperation(&self->handle, SDIO_BUS_WIDE_4B)) == HAL_SD_ERROR_NONE) {
Expand All @@ -157,6 +174,7 @@ void common_hal_sdioio_sdcard_construct(sdioio_sdcard_obj_t *self,
} else {
}
}
#endif

self->capacity = info.BlockNbr * (info.BlockSize / 512);
self->frequency = 25000000;
Expand Down
9 changes: 8 additions & 1 deletion ports/stm/peripherals/stm32h7/clocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#endif
#ifdef STM32H723xx
#include "stm32h7/stm32h723xx/clocks.h"
#define RCC_PERIPHCLK_QSPI RCC_PERIPHCLK_OSPI
#endif

void stm32_peripherals_clocks_init(void) {
Expand Down Expand Up @@ -79,7 +80,7 @@ void stm32_peripherals_clocks_init(void) {
// Set up non-bus peripherals
// TODO: I2S settings go here
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC | RCC_PERIPHCLK_USART3
| RCC_PERIPHCLK_USB | RCC_PERIPHCLK_OSPI;
| RCC_PERIPHCLK_USB | RCC_PERIPHCLK_QSPI;
#if (BOARD_HAS_LOW_SPEED_CRYSTAL)
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
#else
Expand All @@ -99,12 +100,18 @@ void stm32_peripherals_clocks_init(void) {
PeriphClkInitStruct.PLL2.PLL2RGE = RCC_PLL2VCIRANGE_3;
PeriphClkInitStruct.PLL2.PLL2VCOSEL = RCC_PLL2VCOWIDE;
PeriphClkInitStruct.PLL2.PLL2FRACN = 0;
#ifdef RCC_OSPICLKSOURCE_PLL2
PeriphClkInitStruct.OspiClockSelection = RCC_OSPICLKSOURCE_PLL2;
#else
PeriphClkInitStruct.QspiClockSelection = RCC_QSPICLKSOURCE_PLL2;
#endif
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);

#ifdef __HAL_RCC_OSPI1_CLK_ENABLE
/* OCTOSPI1 clock enable */
__HAL_RCC_OCTOSPIM_CLK_ENABLE();
__HAL_RCC_OSPI1_CLK_ENABLE();
#endif

// Enable USB Voltage detector
HAL_PWREx_EnableUSBVoltageDetector();
Expand Down
22 changes: 22 additions & 0 deletions ports/stm/peripherals/stm32h7/stm32h723xx/periph.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,25 @@ const mcu_tim_pin_obj_t mcu_tim_pin_list[58] = {
TIM(8, 3, 3, &pin_PK00),
TIM(1, 1, 1, &pin_PK01),
};

// SDIO
SDMMC_TypeDef *mcu_sdio_banks[1] = {SDMMC1};

const mcu_periph_obj_t mcu_sdio_clock_list[1] = {
PERIPH(1, 12, &pin_PC12),
};
const mcu_periph_obj_t mcu_sdio_command_list[1] = {
PERIPH(1, 12, &pin_PD02),
};
const mcu_periph_obj_t mcu_sdio_data0_list[1] = {
PERIPH(1, 12, &pin_PC08),
};
const mcu_periph_obj_t mcu_sdio_data1_list[1] = {
PERIPH(1, 12, &pin_PC09),
};
const mcu_periph_obj_t mcu_sdio_data2_list[1] = {
PERIPH(1, 12, &pin_PC10),
};
const mcu_periph_obj_t mcu_sdio_data3_list[1] = {
PERIPH(1, 12, &pin_PC11),
};
10 changes: 10 additions & 0 deletions ports/stm/peripherals/stm32h7/stm32h723xx/periph.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ extern const mcu_periph_obj_t mcu_uart_rx_list[26];
#define TIM_PIN_ARRAY_LEN 58
extern TIM_TypeDef *mcu_tim_banks[TIM_BANK_ARRAY_LEN];
extern const mcu_tim_pin_obj_t mcu_tim_pin_list[TIM_PIN_ARRAY_LEN];

// SDIO
extern SDMMC_TypeDef *mcu_sdio_banks[1];

extern const mcu_periph_obj_t mcu_sdio_clock_list[1];
extern const mcu_periph_obj_t mcu_sdio_command_list[1];
extern const mcu_periph_obj_t mcu_sdio_data0_list[1];
extern const mcu_periph_obj_t mcu_sdio_data1_list[1];
extern const mcu_periph_obj_t mcu_sdio_data2_list[1];
extern const mcu_periph_obj_t mcu_sdio_data3_list[1];
4 changes: 2 additions & 2 deletions ports/stm/peripherals/stm32h7/stm32h743xx/periph.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ const mcu_periph_obj_t mcu_uart_rx_list[26] = {
// Timers
// TIM6 and TIM7 are basic timers that are only used by DAC, and don't have pins
// TODO: H7 has more timers than this, but are they tied to pins?
TIM_TypeDef *mcu_tim_banks[14] = {TIM1, TIM2, TIM3, TIM4, TIM5, NULL, NULL, TIM8, NULL, NULL,
NULL, TIM12, TIM13, TIM14};
TIM_TypeDef *mcu_tim_banks[17] = {TIM1, TIM2, TIM3, TIM4, TIM5, NULL, NULL, TIM8, NULL, NULL,
NULL, TIM12, TIM13, TIM14, TIM15, TIM16, TIM17};

const mcu_tim_pin_obj_t mcu_tim_pin_list[58] = {
TIM(2, 1, 1, &pin_PA00),
Expand Down
3 changes: 2 additions & 1 deletion ports/stm/peripherals/stm32h7/stm32h743xx/periph.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern const mcu_periph_obj_t mcu_uart_tx_list[25];
extern const mcu_periph_obj_t mcu_uart_rx_list[26];

// Timers
#define TIM_BANK_ARRAY_LEN 14
#define TIM_BANK_ARRAY_LEN 17
#define TIM_PIN_ARRAY_LEN 58
extern TIM_TypeDef *mcu_tim_banks[TIM_BANK_ARRAY_LEN];
extern const mcu_tim_pin_obj_t mcu_tim_pin_list[TIM_PIN_ARRAY_LEN];
26 changes: 15 additions & 11 deletions ports/stm/supervisor/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,38 @@

#include "common-hal/microcontroller/Pin.h"

#ifndef USB_OTG_FS
#define USB_OTG_FS USB_OTG_HS
#endif

static void init_usb_vbus_sense(void) {

#if (BOARD_NO_VBUS_SENSE)
// Disable VBUS sensing
#ifdef USB_OTG_GCCFG_VBDEN
// Deactivate VBUS Sensing B
USB_OTG_HS->GCCFG &= ~USB_OTG_GCCFG_VBDEN;
USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBDEN;

#if (BOARD_NO_USB_OTG_ID_SENSE)
USB_OTG_HS->GUSBCFG &= ~USB_OTG_GUSBCFG_FHMOD;
USB_OTG_HS->GUSBCFG |= USB_OTG_GUSBCFG_FDMOD;
USB_OTG_FS->GUSBCFG &= ~USB_OTG_GUSBCFG_FHMOD;
USB_OTG_FS->GUSBCFG |= USB_OTG_GUSBCFG_FDMOD;
#endif

// B-peripheral session valid override enable
USB_OTG_HS->GOTGCTL |= USB_OTG_GOTGCTL_BVALOEN;
USB_OTG_HS->GOTGCTL |= USB_OTG_GOTGCTL_BVALOVAL;
USB_OTG_FS->GOTGCTL |= USB_OTG_GOTGCTL_BVALOEN;
USB_OTG_FS->GOTGCTL |= USB_OTG_GOTGCTL_BVALOVAL;
#else
USB_OTG_HS->GCCFG |= USB_OTG_GCCFG_NOVBUSSENS;
USB_OTG_HS->GCCFG &= ~USB_OTG_GCCFG_VBUSBSEN;
USB_OTG_HS->GCCFG &= ~USB_OTG_GCCFG_VBUSASEN;
USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_NOVBUSSENS;
USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBUSBSEN;
USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBUSASEN;
#endif
#else
// Enable VBUS hardware sensing
#ifdef USB_OTG_GCCFG_VBDEN
USB_OTG_HS->GCCFG |= USB_OTG_GCCFG_VBDEN;
USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_VBDEN;
#else
USB_OTG_HS->GCCFG &= ~USB_OTG_GCCFG_NOVBUSSENS;
USB_OTG_HS->GCCFG |= USB_OTG_GCCFG_VBUSBSEN; // B Device sense
USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_NOVBUSSENS;
USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_VBUSBSEN; // B Device sense
#endif
#endif
}
Expand Down

0 comments on commit 6004174

Please sign in to comment.