Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Troubleshoot: New STM port, USB not mounting
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Troubleshoot: New STM port, USB not mounting

by jgillick on Mon Mar 16, 2020 2:13 am

I'm trying to port CircuitPython to STM32F412 for a board I've designed. Everything compiles and flashes alright, but USB is not connecting. It doesn't show up as a device or mounts the drive. However, it fades the "green" LED status LED, which tells me it hasn't crashed but is just not running any python code. (https://learn.adafruit.com/welcome-to-c ... ight-20-20)

Any troubleshooting help would be appreciated.

I based the port on the "stm32f4_discovery" board, in the circuitpython repo, and updated it for the LEDs and the 24Mhz crystal on my baord.

Links:
* Board schematic: https://github.com/jgillick/ThunderPack/blob/STM32F4/hardware/Standard_18650/schematic.pdf
* Porting code: https://github.com/jgillick/micropython/pull/1

What I've found so far
While stepping through main.c with GDB (black magic probe), I discovered that safe_mode is set to MANUAL_SAFE_MODE after wait_for_safe_mode_reset() (and I'm still not entirely sure why). When that happens, the code continues but does not try to execute boot.py or code.py.

If I manually bypass the safe mode issue, everything continues and the green LED lights up, but the device still does not mount as a drive or a serial port.

To confirm that the board design, I connected my board as a serial USB device using the pure STM32Cube HAL library, without circuitpython (https://github.com/jgillick/ThunderPack ... /USBSerial).

jgillick
 
Posts: 6
Joined: Mon Mar 07, 2016 4:26 am

Re: Troubleshoot: New STM port, USB not mounting

by tannewt on Mon Mar 16, 2020 12:37 pm

Hi jgillick, manual safe mode is entered when the board is reset during a 700ms window during CircuitPython's start up. (Here: https://github.com/adafruit/circuitpyth ... mode.c#L55) This includes if the restart is done through the debugger.

This problem is more likely if the clocks aren't setup correctly and the 700ms turns into an even longer period. Clock problems can also lead to USB problems. One way I ensure the clocks are right is by manually making a PWM output I can read with a logic analyzer or oscilloscope to confirm it's frequency is correct.

I'd recommend joining the Adafruit Discord server for help: https://adafru.it/discord There are a number of devs there including Lucian who has done most of the work on the STM port.

tannewt
 
Posts: 1727
Joined: Thu Oct 06, 2016 8:48 pm

Re: Troubleshoot: New STM port, USB not mounting

by jgillick on Tue Mar 17, 2020 3:10 am

Good idea. I just added a test 10kHz PWM signal (using (SystemCoreClock / 10000) - 1) after the run_boot_py() call and was able to confirm that it is coming in at the expected frequency. Screenshot attached, and PWM test code below, for reference. I'll also jump on the discord channel, thank you for the recommendation.

Code: Select all | TOGGLE FULL SIZE
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
TIM_HandleTypeDef htim2;

__HAL_RCC_TIM2_CLK_ENABLE();

htim2.Instance = TIM2;
htim2.Init.Prescaler = 0;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = (SystemCoreClock / 10000) - 1;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
HAL_TIM_Base_Init(&htim2);

sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig);
HAL_TIM_PWM_Init(&htim2);

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);

sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = htim2.Init.Period / 2;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3);

// GPIO
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

// Start
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);
Attachments
Screen Shot 2020-03-17 at 12.02.54 AM.png
Screen Shot 2020-03-17 at 12.02.54 AM.png (31.31 KiB) Viewed 69 times

jgillick
 
Posts: 6
Joined: Mon Mar 07, 2016 4:26 am

Re: Troubleshoot: New STM port, USB not mounting

by hierophect on Tue Mar 17, 2020 1:54 pm

Hi jgillick, you may want to check out the
Code: Select all | TOGGLE FULL SIZE
#define BOARD_NO_VBUS_SENSE (1)
parameter in the mpconfigboard.h file. This is required for any board that does not connect the PA09 VBUS pin to the USB power input. The Discovery does connect this pin so it won't have that parameter turned on.

It's also worthwhile to double check everything in mpconfigboard.mk and make sure that MCU_SUB_VARIANT and CMSIS_MCU are correctly set to match the existing F412 board we support, the F412 discovery. If you base it off the STM32 Discovery (confusingly named) which uses a F405 board and missed either of these parameters, the code might still compile, but silently fail as it doesn't correctly set some chip-specific settings that are different between these two MCUs.

hierophect
 
Posts: 9
Joined: Fri Jul 13, 2018 10:24 pm

Re: Troubleshoot: New STM port, USB not mounting

by jgillick on Tue Mar 17, 2020 2:12 pm

Hi Hierophect. Thank you for your input.

hierophect wrote:Hi jgillick, you may want to check out the
Code: Select all | TOGGLE FULL SIZE
#define BOARD_NO_VBUS_SENSE (1)
parameter in the mpconfigboard.h file.


Yes, I currently have that defined:
https://github.com/jgillick/micropython ... 2881f7eR34

hierophect wrote:It's also worthwhile to double check everything in mpconfigboard.mk and make sure that MCU_SUB_VARIANT and CMSIS_MCU are correctly set to match the existing F412 board we support,


I created a new variant, based on the Discovery stm32f412zx one, with changes to the clock and GPIO for my board. https://github.com/jgillick/micropython ... hunderpack

I've also tried changing the CMSIS_MCU value from "STM32F412Cx" (the actual chip on my board) to "STM32F412Zx".

jgillick
 
Posts: 6
Joined: Mon Mar 07, 2016 4:26 am

Re: Troubleshoot: New STM port, USB not mounting

by jgillick on Wed Mar 18, 2020 4:07 am

I think I have a solution. It looks like code to disable the VBUS sensing was incomplete and didn't set a couple registers.

Fix commit:
https://github.com/jgillick/micropython ... 82aeb30b68

These lines are based on code found in stm32f4xx_ll_usb.c, line 293-298:
https://github.com/hathach/st_driver/bl ... #L293-L298

Big thanks to tannewt and Hierophect for helping me troubleshoot it.

jgillick
 
Posts: 6
Joined: Mon Mar 07, 2016 4:26 am

Please be positive and constructive with your questions and comments.